I've tried countless variations to this code but nothing works.
The code looks ok to me. I suggest you try debugging: Make it display a message when it detects a unit standing in the passage, then you can see whether it's the detecting or the ordering to attack that is the problem. Are you aware that the coordinate system used in the game (and .script files) is different to the one used in .dat? In the game/.script the top left tile of the map is 1;1, but in the .dat scripts it's 0;0. So maybe you've used the wrong coordinates? Also, check the latest log file, it might contain errors that were caused by the script.
Okay I got it working now. I didn't realise GroupAt was influenced by this as well. And I was a bit unlucky with the detecting, as I tested all tiles around it except for the one that should have triggered the attack (which was a mountain tile due to these wrong coordinates). I thought it couldn't go wrong because it was a 2 tile broad entrance, so a 1 tile difference shouldn't have mattered. However, to keep things simple I disabled part of the script so I could only test 1 tile at a time, and I chose the wrong tile... -_-
Anyway! The attack works now, using this code:
- Code:
if (States.GameTime < 7250) then
begin
SneakingUnit1 := States.UnitAt(58, 53);
SneakingUnit2 := States.UnitAt(58, 54);
if SneakingUnit1 <> -1 then
begin
Actions.GroupOrderAttackUnit(Scout3, SneakingUnit1);
end;
if SneakingUnit2 <> -1 then
begin
Actions.GroupOrderAttackUnit(Scout2, SneakingUnit2);
end;
end;
However, I'm noticing several things:
- While I use only one unit to step on tile (58, 54), both Scout2 and Scout3 are triggered.
- After killing my unit, Scout2 and Scout3 rush back to (58, 53) and (58, 54) and keep 'attacking' that location; even though there just isn't anything to attack
I figured the AI was trying to attack his own units because he stepped on those tiles as well. I expanded my code:
- Code:
if (States.GameTime < 7250) then
begin
SneakingUnit1 := States.UnitAt(58, 53);
SneakingUnit2 := States.UnitAt(58, 54);
if (SneakingUnit1 <> -1) and (States.UnitOwner(SneakingUnit1) = 0) then
begin
Actions.GroupOrderAttackUnit(Scout3, SneakingUnit1);
end;
if (States.UnitDead(SneakingUnit1) = True) and (States.GroupMemberCount(Scout3) = 8) then
begin
Actions.GroupOrderWalk(Scout3, 66, 40, 6);
end;
if (SneakingUnit2 <> -1) and (States.UnitOwner(SneakingUnit2) = 0) then
begin
Actions.GroupOrderAttackUnit(Scout2, SneakingUnit2);
end;
if (States.UnitDead(SneakingUnit2) = True) and (States.GroupMemberCount(Scout2) = 8) then
begin
Actions.GroupOrderWalk(Scout2, 63, 45, 6);
end;
end;
As you can see I also tried to make the AI retreat once the unit was killed. However, I noticed a bug. Ignoring the fact that this script doesn't work as I want it to (the AI retreats immediately when you leave the 'trigger tiles', so apparently it already assumes SneakingUnit is dead), the pathfinding seems bugged. This is what happens:
1. I step on tile (58, 53)
2. Scout3 is triggered and attacks me
3. I step on tile (57, 53)
4. Scout3 retreats immediately
5. Now I step on tile (58, 54)
6. Scout2 TRIES to attack me but is somehow unable to walk past Scout 3. Scout 2 seems to be trying to go through Scout 3, but Scout3 doesn't move an inch.
Also, both Scout 2 and Scout 3 do not face the correct direction (west) when they retreat.
I'll now try to script the fearsome all-in attack.

And maybe if I come up with something I'll attempt to make the AI retreat after he actually killed SneakingUnit. Thanks for your answer (and sado too, guess I should've listened to you after all

).
In that case I'd have a global Boolean variable DoMassiveAttack which you set to true when you want the AI to attack. Then in OnTick you can check if DoMassiveAttack is true, and if it is, order the AI to attack the player every 10 seconds or so (no need to do it every tick).
As Sado said you can write a function which makes all the AIs groups attack. You could also write a function to give you the closest unit (or house) of the human player (using PlayerGetAllGroups), although I'm considering providing GetClosestUnit/Group/House functions because it's a pretty common thing to want.
Yeah, the problem is the target. I do not want to make the AI attack units only, or houses only. They have to attack everything. Surely I could write a script where the AI attacks all units first and if unit count = 0 then they start destroying houses. But this means they will chase one pathetic newly trained soldier to the border of the map before they continue destroying houses, which will just look silly...