Code:
function AIScript_SpawnAndCreateArmy(_AIPlayerID, _SpawnEntity, _Sword, _Bow, _Catapults, _SiegeTower, _Rams, _AmmoCarts, _UseRedPrinceTroops, _ReuseExistingMilitaryOnSameSector)
local SpawnedTroops = {}
local ArmyPositionID = AIScriptHelper_GetEntityID(_SpawnEntity)
local X,Y = Logic.GetEntityPosition(ArmyPositionID)
if Logic.IsBuilding( ArmyPositionID ) == 1 then
X, Y = Logic.GetBuildingApproachPosition( ArmyPositionID )
end
local ArmyID = AICore.CreateArmy(_AIPlayerID)
-- Try to find idle military units on the same sector as the spawnpoint.
-- If they are on the same sector, add them to the army and spawn the remaining amount of units at the spawnpoint
-- Doesn't reuse ammo carts :-)
-- If at least one unit isn't on the same sector forget about reusing units for simplicity reasons [ChMo]
if _ReuseExistingMilitaryOnSameSector then
local SectorSpawn = Logic.GetPlayerSectorAtPosition( _AIPlayerID, X, Y )
local SwordNew = _Sword
local BowNew = _Bow
local CatapultsNew = _Catapults
local SiegeTowerNew = _SiegeTower
local RamsNew = _Rams
if _Sword then
for i = _Sword, 1, -1 do
if AICore.CheckFreeUnits( _AIPlayerID, i, 0, 0, 0, 0 ) then
AICore.AddSwordsmenToArmy( _AIPlayerID, ArmyID, i, i, i )
SwordNew = _Sword - i
break
end
end
end
if _Bow then
for i = _Bow, 1, -1 do
if AICore.CheckFreeUnits( _AIPlayerID, 0, i, 0, 0, 0 ) then
AICore.AddBowmenToArmy( _AIPlayerID, ArmyID, i, i, i )
BowNew = _Bow - i
break
end
end
end
if _Catapults then
for i = _Catapults, 1, -1 do
if AICore.CheckFreeUnits( _AIPlayerID, 0, 0, 0, 0, i ) then
AICore.AddCatapultToArmy( _AIPlayerID, ArmyID, i, i, i )
CatapultsNew = _Catapults - i
break
end
end
end
if _SiegeTower then
for i = _SiegeTower, 1, -1 do
if AICore.CheckFreeUnits( _AIPlayerID, 0, 0, 0, i, 0 ) then
AICore.AddTowerToArmy( _AIPlayerID, ArmyID, i, i, i )
SiegeTowerNew = _SiegeTower - i
break
end
end
end
if _Rams then
for i = _Rams, 1, -1 do
if AICore.CheckFreeUnits( _AIPlayerID, 0, 0, i, 0, 0 ) then
AICore.AddRamToArmy( _AIPlayerID, ArmyID, i, i, i )
RamsNew = _Rams - i
break
end
end
end
local TroopIDs = AICore.ArmyGetEntities( _AIPlayerID, ArmyID )
if TroopIDs then
local AreOnSameSector = true
for _, ID in ipairs( TroopIDs ) do
local XTroop, YTroop = Logic.GetEntityPosition( ID )
if SectorSpawn ~= Logic.GetPlayerSectorAtPosition( _AIPlayerID, XTroop, YTroop ) then
AreOnSameSector = false
break
end
table.insert( SpawnedTroops, ID )
end
if #SpawnedTroops > 0 then
if AreOnSameSector then
_Sword = SwordNew
_Bow = BowNew
_Catapults = CatapultsNew
_SiegeTower = SiegeTowerNew
_Rams = RamsNew
else
SpawnedTroops = {}
AICore.DisbandUnusedArmy(ArmyID)
ArmyID = AICore.CreateArmy(_AIPlayerID)
end
end
else
SpawnedTroops = {}
end
end
if (_Sword ~= nil) then
for i=1, _Sword do
local FighterType = AIScriptHelper_GetTroopTypeOverride( Entities.U_MilitarySword, _UseRedPrinceTroops )
local EntityID = Logic.CreateBattalionOnUnblockedLand(FighterType, X, Y, 0, _AIPlayerID);
AICore.AddEntityToArmy(_AIPlayerID, ArmyID, EntityID)
table.insert(SpawnedTroops, EntityID)
end
end
if (_Bow ~= nil) then
for i=1, _Bow do
local FighterType = AIScriptHelper_GetTroopTypeOverride( Entities.U_MilitaryBow, _UseRedPrinceTroops )
local EntityID = Logic.CreateBattalionOnUnblockedLand(FighterType, X, Y, 0, _AIPlayerID);
AICore.AddEntityToArmy(_AIPlayerID, ArmyID, EntityID)
table.insert(SpawnedTroops, EntityID)
end
end
if (_Rams ~= nil) then
for i=1, _Rams do
local EntityID = Logic.CreateEntityOnUnblockedLand(Entities.U_BatteringRamCart, X, Y, 0, _AIPlayerID);
AICore.AddEntityToArmy(_AIPlayerID, ArmyID, EntityID)
table.insert(SpawnedTroops, EntityID)
end
end
if (_SiegeTower ~= nil) then
for i=1, _SiegeTower do
local EntityID = Logic.CreateEntityOnUnblockedLand(Entities.U_SiegeTowerCart, X, Y, 0, _AIPlayerID);
AICore.AddEntityToArmy(_AIPlayerID, ArmyID, EntityID)
table.insert(SpawnedTroops, EntityID)
end
end
if (_Catapults ~= nil) then
for i=1, _Catapults do
local EntityID = Logic.CreateEntityOnUnblockedLand(Entities.U_CatapultCart, X, Y, 0, _AIPlayerID);
AICore.AddEntityToArmy(_AIPlayerID, ArmyID, EntityID)
table.insert(SpawnedTroops, EntityID)
end
end
if (_AmmoCarts ~= nil) then
for i=1, _AmmoCarts do
local EntityID = Logic.CreateEntityOnUnblockedLand(Entities.U_AmmunitionCart, X, Y, 0, _AIPlayerID);
AICore.AddEntityToArmy(_AIPlayerID, ArmyID, EntityID)
table.insert(SpawnedTroops, EntityID)
end
end
return ArmyID, SpawnedTroops
end
Lesezeichen