AI can do many things at the same time, but there are also many things that AI never did before. For example: repairing, laying mines or deploying hyperspace gates.
So we can add customcode to certain ships and tell them to do what we want them to, if they are controlled by AI. Well, that’s the idea, but there’s a problem.
AI constantly give order to every ship it controls, and will just override our commands. So we need to remove those ships from AI by using function CPU_RemoveSobGroup(<iPlayerIndex>, <sSobGroupName>)
Unfortunately, that’s not a valid function in CustomCode Scope, so we need to add following code into the game rule LUA file:
function Oninit()
...
SobGroup_CreateIfNotExist("AITempRemoveGroup")
SobGroup_CreateIfNotExist("AITempAddGroup")
Rule_AddInterval("UnitAddToCPU",1)
Rule_AddInterval("UnitRemoveFromCPU",1)
...
end
function UnitAddToCPU()
if(SobGroup_Empty("AITempAddGroup")==0)then
CPU_AddSobGroup(SobGroup_OwnedBy("AITempAddGroup"), "AITempAddGroup")
SobGroup_Clear("AITempAddGroup")
end
end
function UnitRemoveFromCPU()
if(SobGroup_Empty("AITempRemoveGroup")==0)then
CPU_RemoveSobGroup(SobGroup_OwnedBy("AITempRemoveGroup"), "AITempRemoveGroup")
SobGroup_Clear("AITempRemoveGroup")
end
end
In this case, any ship that was put into sobgroup “AITempRemoveGroup” will be removed from AI control, and any ship that was put into sobgroup “AITempAddGroup” will be added back. And we can finally do our job now.
I’ll just leave a customcode here as an example which will make the ship repair it’s entire fleet, it should only be applied to repair corvettes or support frigates instead of HW2 RU collectors, because AI will not be able to harvest in that case
function Create_Repair(CustomGroup, playerIndex, shipID)
SobGroup_CreateIfNotExist("AIRepairGroup")
end
function Update_Repair(CustomGroup, playerIndex, shipID)
if(Player_GetLevelOfDifficulty(playerIndex) > 0)then
if (SobGroup_Empty("AITempRemoveGroup")==1) and (SobGroup_GroupInGroup("AIRepairGroup",CustomGroup)==0)then
SobGroup_SobGroupAdd("AIRepairGroup",CustomGroup)
SobGroup_SobGroupAdd("AITempRemoveGroup",CustomGroup)
end
if(SobGroup_IsDoingAbility(CustomGroup,AB_Repair)==0)then
SobGroup_RepairSobGroup(CustomGroup, "Player_Ships"..playerIndex)
end
end
end
And if you don’t know what CustomCode is, here’s a tutorial: [TUTORIAL] Simple Custom Code