[SOLVED][AI] Attack even when there are no RUs?

I worked this out for HW2 in the past, but don’t recall the details. My question is, how do I get the AI to go on the offensive even if there are no resources on the map or collectors in the AI’s fleet? Otherwise, it just kind of sits there. Thanks.

I am guessing the ai function Logic_military_attackrules is causing cp_attackPercent to be set to 0 (Lines 72-82 of cpumilitary.lua).

If you where able to add a condition that if there are no resources left then to set cp_attackPercent to, say, 100, It might make the ai continue attacking. Also to note, an ai set to easy will not change the cp_attackPercent.

You could try adding at the end of that function

if (GetActiveBlobCount()== 0) then
  cp_attackPercent = 100

Also, to use the aitrace that is in the ai lua files,
change Line 11 of default.lua from
aitrace = function() end
aitrace = function(msg) print(msg) end

There are also lines through out the ai’s lua files where aitrace has been commented out. Un-commenting them may help in better understand what the ai does and when…


I tried this. The enemy will attack if I am in range of its military ships and carrier. If I give the enemy probes, it will move the probes but not the military ships and carrier. I will keep looking.

It might be GetActiveBlobCount() doesn’t return 0.

I believe I’ve got it.

change the function Logic_military_groupvars to this

function Logic_military_groupvars()
  if (GetActiveBlobCount()== 0) then
    cp_minSquadGroupSize = 0
    cp_minSquadGroupValue = 0
    cp_forceAttackGroupSize = 0
    local scaleComp = SelfRace_GetNumber("persona_group_scale", 1.0)	-- 5

    cp_minSquadGroupSize = 4*scaleComp
    cp_minSquadGroupValue = 150*scaleComp

    if (sg_moreEnemies > 0 and s_selfTotalValue < s_enemyTotalValue*2) then
      cp_minSquadGroupSize = cp_minSquadGroupSize + (2*scaleComp)
      cp_minSquadGroupValue = cp_minSquadGroupValue + (75*scaleComp)
    elseif (s_militaryStrength > (120*scaleComp)) then
      cp_minSquadGroupSize = 3*scaleComp
      cp_minSquadGroupValue = 120*scaleComp

cp_minSquadGroupSize is the minimum ships an attack group can be.
cp_minSquadGroupValue is the minimum military? value the group can be.
cp_forceAttackGroupSize is the forced size of the attack group.

Also, you may want to be patient. For me, the attack timer kept setting to 227 seconds, so I forced it to 30 seconds by adding wavedelay = 30 to just before Rule_AddInterval("attack_now_timer", wavedelay )

1 Like

I tried all this as you suggested and waited over 8 minutes, but they’re still just sitting there. Not sure what is going on.


Okay, 11 minutes and they started attacking. A good start!

It may be Logic_military_setattacktimer
It has the possibility to delay the attack for upto 1000 minutes (Easy CPU set to Dynamic) or as low as 20.15 seconds (Expert CPU set to Aggressive)

1 Like

How do I set the CPU to Expert and Aggressive? This is a singleplayer mission.

From the OnInit function of the mission lua, add

CPU_DoString(1, "setLevelOfDifficulty(2)")
CPU_DoString(1, "g_LOD = 3")

The 1 is the cpu players id. 0 is generally the first player (human player)
setLevelOfDifficulty(2) should set the ai’s difficulty to hard I don’t think 3 can be set
g_LOD is what the ai uses to hold the level of difficulty, but it may change, that’s why to use setLevelOfDifficulty(2) above.
Flag_Research is hopefully granting the research that the ai uses to make certain functions be aggressive

There is also the ‘noRushTime’ that has a minimum of 60 seconds.
Depending on what works, expect the attack call to occur between 80.15 seconds and 110 seconds.

‘noRushTime’ has a max of 15.3 minutes, but only if you grant the cpu CPUPLAYERS_NORUSHTIME15 research.


This function is not present in my mod. Did you create it yourself?

It’s in lib.lua
Try, instead of Flag_Research

if (Player_ResearchOptionIsRestricted(1, "CPUPLAYERS_AGGRESSIVE")== 1) then
  Player_UnrestrictResearchOption(1, "CPUPLAYERS_AGGRESSIVE")
Player_GrantResearchOption(1, "CPUPLAYERS_AGGRESSIVE")
1 Like

That indeed works! Thanks so much!

I think I am running into this in skirmish/multiplayer. and it didn’t occur to me that the map has no resources is why they aren’t building. It looks like the AI will do resource build first unless it is underattack or the resource criteria has been satisfied, but since the function to build resources in cpu_resource returns 0 or negative demand on maps with no resources it never gets to the military build. My attempted fix will be to short circuit the resource logic completely if there are no blobs and make the AI think it is satisfied

If you jump ships near the enemy it will call UnderAttack() and force military build. Not sure how to call under attack for a specific AI though

1 Like

if you want to override the return for UnderAttack from the gamerules, then in default.lua (ai) add

o_UnderAttack = UnderAttack
rawset(globals(), "UnderAttack", function ()
  if (overrideUnderAttack ~= -1) then
    return overrideUnderAttack
  return o_UnderAttack

overrideUnderAttack = -1

then in the gamerule use CPU_DoString (_player_, "overrideUnderAttack = _val_")
player is the player id for the cpu.
val is what you want UnderAttack to return. Use -1 to not override the returned value of UnderAttack.


putting one asteroid that is resource sable on the map make a HUGE difference for AI behavior. I’d suggest that your base map has at least one small asteroid or salvage at the center to force the AI to operate well (and draw it away from jump points)

1 Like

One weird thing I think happens, sometimes, is that the AI won’t build, even when the build cost is zero.

Ive been modifying the default AI to get the various races to build a set baseline of ships and one thing I found is that the AI will default to eAntiFrigate class once it hits some caps on fleet value and counter values. What I did is added a call to the BuildSpecialDemand in this case so that it will build a pre determined mix of ships scaled by available RUs. Eventually it will cap out on that as well and return to eAntiFrigate

another thing to check is put a print statement in the AI that tells you who their selected enemy is. If there is another player in the map that has no ships (some sort of neutral faction) the AI may never see a threat until you attack it (and if there are no ships, nothing to attack)

basic build logic is
build resourcers until you hit a cap
build a set amount of basic ships until the enemy either has enough ships or you are under attack
build counter demand for the selected enemy
build antifrigate until you run out of RUs or have reached the difficulty cap

make sure you have a buildable ship you want in eAntiFrigate