Barbarian Scripting
Since a few weeks i'm busy reading and editing scripts (mostly small adjustments) tryinf to get a better understanding of how and why some events occur during the game. But this time i'm stuck, underneath the script that I want working on my co-op map (which i'll publish later on).
procedure aSwitchToBarbarian(aUnitID: Integer; aGroupID: Integer); // Starting a procedure, which is basiclly telling the game to start 'doing something'
var aBarracks: Integer; // This makes the barracks a reference point within this script that is linked to the game, correct ?
var aDone: Boolean; // This creates a two way variable, in this case checken whether the player is AI or human, correct ?
begin // Begin describing what the script should 'do'
if States.UnitType(aUnitID) = 14 then begin // This says "If the unit type is Miltia(14) then begin..", correct ?
aBarracks := States.ClosestHouse(States.UnitOwner(aUnitID), States.UnitPositionX(aUnitID), States.UnitPositionY(aUnitID), 21); // This says "Replace the Miltia (14) with Barbarian (21)", correct ?
if aBarracks > -1 then begin // Don't know what this says, I figured it said something like "If barracks is existing"
aDone := False; // This is the trigger for being AI or not, False means that an human is controlling the player
if States.PlayerIsAI(States.UnitOwner(aUnitID)) = True then Actions.HouseAddWaresTo(aBarracks, 20, 1); // This line says "When the player is AI, Add 1 x item 20 to the barracks" (which is an axe, i Figure)
if (States.HouseResourceAmount(aBarracks, 20) >= 1) AND (aDone = False) then begin // This checks if the resource amount in the barracks of item 20 (axe) is more than, or equal to 1 AND the player is AI/human
Actions.HouseTakeWaresFrom(aBarracks, 20, 1); // If the previous line is correct than this line takes 1 x item 20 from the barracks
Actions.GroupOrderLink(Actions.GiveGroup(States.UnitOwner(aUnitID), 23, States.UnitPositionX(aUnitID), States.UnitPositionY(aUnitID), States.UnitDirection(aUnitID),1,1), aGroupID); // Don't know what this says, I figured this gives the 'trained' militia a certain groupID to wich he is added (training 10 miltia would each give them the same groupID, otherwise they would all spawn as individual groups). This is needed for the script to work, not sure about this.
aDone := True; // This checks if the player is AI or not (True, means an player is AI (correct?))
end;
if (States.HouseResourceAmount(aBarracks, 20) < 1) AND (aDone = False) then begin // This line tells the exact opposite for what to do if the first part is not met (above 3 lines)
Actions.HouseBarracksGiveRecruit(aBarracks);
Actions.HouseAddWaresTo(aBarracks, 20, 1);
aDone := True;
end;
Actions.UnitKill(aUnitID, True);// This will remove the militia from the game
end;
end;
end;
procedure OnWarriorEquipped(aUnitID: Integer; aGroupID: Integer); // This should tell the moment in the game for when to start a certain 'procedure', on warrior equip would occure when someone trains a specific unit (militia for this script)
begin
if States.UnitType (aUnitID) = 14 then begin // This should only tell the script to start when the trained unit type is '14' (thus a militia)
aSwitchToBarbarian(aUnitID, aGroupID); // This should tell the script to start the above procedure (thus replacing the militia for a barbarian)
end;
end;
I've added my comment on each scripting line for what I think about the script(Correct me if i'm wrong ). I've cut this part from a script of a 'bonus' map. The end result should be that player 8, AI controlled (only player that can train militia, I've denied all other players to train militia, so I wouldn't have to exclude the other players from the script), can train barbarians by default.
Script without my comments:
procedure aSwitchToBarbarian(aUnitID: Integer; aGroupID: Integer);
var aBarracks: Integer;
var aDone: Boolean;
begin
if States.UnitType(aUnitID) = 14 then begin
aBarracks := States.ClosestHouse(States.UnitOwner(aUnitID), States.UnitPositionX(aUnitID), States.UnitPositionY(aUnitID), 21);
if aBarracks > -1 then begin
aDone := False;
if States.PlayerIsAI(States.UnitOwner(aUnitID)) = True then Actions.HouseAddWaresTo(aBarracks, 20, 1);
if (States.HouseResourceAmount(aBarracks, 20) >= 1) AND (aDone = False) then begin
Actions.HouseTakeWaresFrom(aBarracks, 20, 1);
Actions.GroupOrderLink(Actions.GiveGroup(States.UnitOwner(aUnitID), 23, States.UnitPositionX(aUnitID), States.UnitPositionY(aUnitID), States.UnitDirection(aUnitID),1,1), aGroupID);
aDone := True;
end;
if (States.HouseResourceAmount(aBarracks, 20) < 1) AND (aDone = False) then begin
Actions.HouseBarracksGiveRecruit(aBarracks);
Actions.HouseAddWaresTo(aBarracks, 20, 1);
aDone := True;
end;
Actions.UnitKill(aUnitID, True);
end;
end;
end;
procedure OnWarriorEquipped(aUnitID: Integer; aGroupID: Integer);
begin
if States.UnitType (aUnitID) = 14 then begin
aSwitchToBarbarian(aUnitID, aGroupID);
end;
end;
Julian Snow