From 88fd292d7a3b0d41e26d8d04473f92333aead8c9 Mon Sep 17 00:00:00 2001 From: Floris Date: Fri, 24 Jan 2025 21:36:02 +0100 Subject: [PATCH] simpleai: added localization (#4205) localized most Spring. calls and UnitDefs table --- luarules/gadgets/ai_simpleai.lua | 210 ++++++++++++++----------------- 1 file changed, 91 insertions(+), 119 deletions(-) diff --git a/luarules/gadgets/ai_simpleai.lua b/luarules/gadgets/ai_simpleai.lua index 91645789633..36459c1e1ce 100644 --- a/luarules/gadgets/ai_simpleai.lua +++ b/luarules/gadgets/ai_simpleai.lua @@ -7,7 +7,6 @@ local wind = Game.windMax local mapsizeX = Game.mapSizeX local mapsizeZ = Game.mapSizeZ local random = math.random -local debugmode = false --Spring.Echo("tracy", tracy) @@ -59,9 +58,6 @@ if Game.gameShortName == "BYAR" then "cordl", } end -local function RandomChoiceArray(t) - return t[random(1,#t)] -end local function RandomChoice(self) -- lazy initialization @@ -86,7 +82,6 @@ end -- SimpleCommanderDefs:RandomChoice() -- SimpleCommanderDefs.RandomChoice(SimpleCommanderDefs) -local BadUnitDefs = {RandomChoice = RandomChoice} local SimpleCommanderDefs = {RandomChoice = RandomChoice} local SimpleFactoriesDefs = {RandomChoice = RandomChoice} local SimpleConstructorDefs = {RandomChoice = RandomChoice} @@ -99,8 +94,13 @@ local SimpleUndefinedUnitDefs = {RandomChoice = RandomChoice} local BuildOptions = {} -- {unitDefHasBuildOptions = {1= buildOpt0, RandomChoice = RandomChoice}} - +local isBuilding = {} +local isCommander = {} for unitDefID, unitDef in pairs(UnitDefs) do + if unitDef.isBuilding then + isBuilding[unitDefID] = {unitDef.xsize, unitDef.zsize} + end + local skip = false for a = 1, #BadUnitsList do if BadUnitsList[a] == unitDef.name then @@ -108,7 +108,6 @@ for unitDefID, unitDef in pairs(UnitDefs) do break end end - -- stockpilers --if unitDef.canStockpile then -- skip = true @@ -138,6 +137,7 @@ for unitDefID, unitDef in pairs(UnitDefs) do if not skip then if unitDef.customParams.iscommander then + isCommander[unitDefID] = true SimpleCommanderDefs[unitDefID] = 1 elseif unitDef.isFactory and #unitDef.buildOptions > 0 then SimpleFactoriesDefs[unitDefID] = 1 @@ -165,7 +165,20 @@ for unitDefID, unitDef in pairs(UnitDefs) do end end --------- functions + +local spGiveOrderToUnit = Spring.GiveOrderToUnit +local spGetUnitNearestEnemy = Spring.GetUnitNearestEnemy +local spGetUnitsInCylinder = Spring.GetUnitsInCylinder +local spGetGroundHeight = Spring.GetGroundHeight +local spGetUnitDefID = Spring.GetUnitDefID +local spGetUnitTeam = Spring.GetUnitTeam +local spGetUnitPosition = Spring.GetUnitPosition +local spGetCommandQueue = Spring.GetCommandQueue +local spGetUnitHealth = Spring.GetUnitHealth +local spGetUnitAllyTeam = Spring.GetUnitAllyTeam +local spGetTeamResources = Spring.GetTeamResources +local spTestBuildOrder = Spring.TestBuildOrder + local function SimpleGetClosestMexSpot(x, z) --tracy.ZoneBeginN("SimpleAI:SimpleGetClosestMexSpot") @@ -178,10 +191,8 @@ local function SimpleGetClosestMexSpot(x, z) local dx, dz = x - spot.x, z - spot.z local dist = dx * dx + dz * dz if dist < bestDist then - local units = Spring.GetUnitsInCylinder(spot.x, spot.z, 128) - --local height = Spring.GetGroundHeight(spot.x, spot.z) + local units = spGetUnitsInCylinder(spot.x, spot.z, 128) if #units == 0 then - --and height > 0 then bestSpot = spot bestDist = dist end @@ -189,18 +200,13 @@ local function SimpleGetClosestMexSpot(x, z) end else -- optimize for metal maps a bit - local canBuildMex = false - local numtries = 0 - local maxtries = HashPosTable.numPos - - local hashPos = HashPosTable:hashPos(x,z) local searchwidth = HashPosTable.resolution / 2 - 32 for hashposindex = 1, HashPosTable.numPos do local tilecenterx, tilecenterz = HashPosTable:GetNthCenter(x,z,hashposindex) for attempt = 1,5 do local posx = tilecenterx + random(-searchwidth, searchwidth) local posz = tilecenterz + random(-searchwidth, searchwidth) - local posy = Spring.GetGroundHeight(posx, posz) + local posy = spGetGroundHeight(posx, posz) local _,_,hasmetal = Spring.GetGroundInfo(posx, posz) if hasmetal > 0.1 then local flat = positionCheckLibrary.FlatAreaCheck(posx, posy, posz, 64, 25, true) @@ -215,33 +221,6 @@ local function SimpleGetClosestMexSpot(x, z) end if bestSpot then break end end - - --[[ - -- old method left here as a reference - for i = 128,10000 do - - canBuildMex = false - local posx = x + random(-i,i) - local posz = z + random(-i,i) - local posy = Spring.GetGroundHeight(posx, posz) - canBuildMex = positionCheckLibrary.FlatAreaCheck(posx, posy, posz, 64, 25, true) - if canBuildMex then - canBuildMex = positionCheckLibrary.OccupancyCheck(posx, posy, posz, 64) - end - if canBuildMex then - canBuildMex = positionCheckLibrary.MapEdgeCheck(posx, posy, posz, 64) - end - if canBuildMex and select(3, Spring.GetGroundInfo(posx, posz)) > 0.1 then - canBuildMex = true - else - canBuildMex = false - end - if canBuildMex then - bestSpot = {x = posx, y = posy, z = posz} - break - end - end - ]]-- end --tracy.ZoneEnd() return bestSpot @@ -250,27 +229,25 @@ end local function SimpleBuildOrder(cUnitID, building) --tracy.ZoneBeginN("SimpleAI:SimpleBuildOrder") - --Spring.Echo( UnitDefs[Spring.GetUnitDefID(cUnitID)].name, " ordered to build", UnitDefs[building].name) + --Spring.Echo( UnitDefs[spGetUnitDefID(cUnitID)].name, " ordered to build", UnitDefs[building].name) local searchRange = 0 local numtests = 0 --Spring.Echo("SBO", cUnitID,"Start") for b2 = 1,20 do searchRange = searchRange + 300 -- WARNING, THIS EVENTUALLY ENDS UP BEING A 6000 RADIUS CIRCLE! - local team = Spring.GetUnitTeam(cUnitID) - local cunitposx, _, cunitposz = Spring.GetUnitPosition(cUnitID) - local units = Spring.GetUnitsInCylinder(cunitposx, cunitposz, searchRange, team) + local team = spGetUnitTeam(cUnitID) + local cunitposx, _, cunitposz = spGetUnitPosition(cUnitID) + local units = spGetUnitsInCylinder(cunitposx, cunitposz, searchRange, team) if #units > 1 then local gaveOrder = false for k=1,math.min(#units, 5 + b2 * 2) do numtests = numtests+1 local buildnear = units[random(1, #units)] - local refDefID = Spring.GetUnitDefID(buildnear) - local isBuilding = UnitDefs[refDefID].isBuilding - local isCommander = (UnitDefs[refDefID].name == "armcom" or UnitDefs[refDefID].name == "corcom") - if (isBuilding or isCommander) then - local refx, refy, refz = Spring.GetUnitPosition(buildnear) - local reffootx = UnitDefs[refDefID].xsize * 8 - local reffootz = UnitDefs[refDefID].zsize * 8 + local refDefID = spGetUnitDefID(buildnear) + if isBuilding[unitDefID] or isCommander[refDefID] then + local refx, _, refz = spGetUnitPosition(buildnear) + local reffootx = isBuilding[refDefID] and isBuilding[refDefID][1] * 8 or 16 + local reffootz = isBuilding[refDefID] and isBuilding[refDefID][2] * 8 or 16 local spacing = random(64, 128) local testspacing = spacing * 0.75 local buildingDefID = building @@ -289,19 +266,17 @@ local function SimpleBuildOrder(cUnitID, building) local bposx = refx + rx local bposz = refz + rz - local bposy = Spring.GetGroundHeight(bposx, bposz)--+100 - local testpos = Spring.TestBuildOrder(buildingDefID, bposx, bposy, bposz, r) + local bposy = spGetGroundHeight(bposx, bposz)--+100 + local testpos = spTestBuildOrder(buildingDefID, bposx, bposy, bposz, r) if testpos == 2 then local nearbyunits = Spring.GetUnitsInRectangle(bposx - testspacing, bposz - testspacing, bposx + testspacing, bposz + testspacing) if #nearbyunits == 0 then - Spring.GiveOrderToUnit(cUnitID, -buildingDefID, { bposx, bposy, bposz, r }, { "shift" }) + spGiveOrderToUnit(cUnitID, -buildingDefID, { bposx, bposy, bposz, r }, { "shift" }) gaveOrder = true break end end end - --local buildingDefID = UnitDefNames.building.id - --local testpos = Spring.TestBuildOrder(buildingDefID, bposx, bposy, bposz, facing) end if gaveOrder then break end end @@ -311,15 +286,13 @@ local function SimpleBuildOrder(cUnitID, building) end local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, units, type) - --tracy.ZoneBeginN("SimpleAI:SimpleConstructionProjectSelection") local success = false - local mcurrent, mstorage, _, mincome, mexpense = Spring.GetTeamResources(unitTeam, "metal") - local ecurrent, estorage, _, eincome, eexpense = Spring.GetTeamResources(unitTeam, "energy") - local unitposx, unitposy, unitposz = Spring.GetUnitPosition(unitID) + local mcurrent, mstorage, _, _, _ = spGetTeamResources(unitTeam, "metal") + local ecurrent, estorage, _, _, _ = spGetTeamResources(unitTeam, "energy") + local unitposx, _, unitposz = spGetUnitPosition(unitID) - --local buildOptions = UnitDefs[unitDefID].buildOptions local buildOptions = BuildOptions[unitDefID] -- Builders for b1 = 1,10 do @@ -331,7 +304,7 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u if (mexspotpos and SimpleT1Mexes[unitTeam] < 3) and type == "Commander" then local project = SimpleExtractorDefs:RandomChoice() if buildOptions and buildOptions[project] then - Spring.GiveOrderToUnit(unitID, -project, { mexspotpos.x, mexspotpos.y, mexspotpos.z, 0 }, { "shift" }) + spGiveOrderToUnit(unitID, -project, { mexspotpos.x, mexspotpos.y, mexspotpos.z, 0 }, { "shift" }) --Spring.Echo("Success! Project Type: Extractor.") success = true end @@ -347,9 +320,9 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u -- if type == "Commander" then -- for t = 1,10 do -- local targetUnit = units[math.random(1,#units)] - -- if UnitDefs[Spring.GetUnitDefID(targetUnit)].isBuilding == true then - -- local tUnitX, tUnitY, tUnitZ = Spring.GetUnitPosition(targetUnit) - -- Spring.GiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + math.random(-100, 100), tUnitY, tUnitZ + math.random(-100, 100) }, { "shift", "alt", "ctrl" }) + -- if isBuilding[spGetUnitDefID(targetUnit)] then + -- local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit) + -- spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + math.random(-100, 100), tUnitY, tUnitZ + math.random(-100, 100) }, { "shift", "alt", "ctrl" }) -- success = true -- break -- end @@ -367,13 +340,13 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u local xoffsets = {0, 100, -100} local zoffsets = {0, 100, -100} if buildOptions and buildOptions[project] then - Spring.GiveOrderToUnit(unitID, -project, { mexspotpos.x, mexspotpos.y, mexspotpos.z, 0 }, { "shift" }) + spGiveOrderToUnit(unitID, -project, { mexspotpos.x, mexspotpos.y, mexspotpos.z, 0 }, { "shift" }) for _, xoffset in ipairs(xoffsets) do for _, zoffset in ipairs(zoffsets) do if xoffset ~= 0 and zoffset ~= 0 then local projectturret = SimpleTurretDefs:RandomChoice() if buildOptions[projectturret] then - Spring.GiveOrderToUnit(unitID, -projectturret, { mexspotpos.x + xoffset, mexspotpos.y, mexspotpos.z + zoffset , random(0,3) }, { "shift" }) + spGiveOrderToUnit(unitID, -projectturret, { mexspotpos.x + xoffset, mexspotpos.y, mexspotpos.z + zoffset , random(0,3) }, { "shift" }) end end end @@ -398,9 +371,9 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u elseif r == 11 then for t = 1,10 do local targetUnit = units[random(1,#units)] - if UnitDefs[Spring.GetUnitDefID(targetUnit)].isBuilding == true then - local tUnitX, tUnitY, tUnitZ = Spring.GetUnitPosition(targetUnit) - Spring.GiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" }) + if isBuilding[spGetUnitDefID(targetUnit)] then + local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit) + spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" }) success = true break end @@ -408,16 +381,16 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u elseif r == 12 and type ~= "Commander" then local mapcenterX = mapsizeX/2 local mapcenterZ = mapsizeZ/2 - local mapcenterY = Spring.GetGroundHeight(mapcenterX, mapcenterZ) + local mapcenterY = spGetGroundHeight(mapcenterX, mapcenterZ) local mapdiagonal = math.ceil(math.sqrt((mapsizeX*mapsizeX)+(mapsizeZ*mapsizeZ))) - Spring.GiveOrderToUnit(unitID, CMD.RECLAIM,{mapcenterX+random(-100,100),mapcenterY,mapcenterZ+random(-100,100),mapdiagonal}, 0) + spGiveOrderToUnit(unitID, CMD.RECLAIM,{mapcenterX+random(-100,100),mapcenterY,mapcenterZ+random(-100,100),mapdiagonal}, 0) success = true elseif r == 13 and type ~= "Commander" then local mapcenterX = mapsizeX/2 local mapcenterZ = mapsizeZ/2 - local mapcenterY = Spring.GetGroundHeight(mapcenterX, mapcenterZ) + local mapcenterY = spGetGroundHeight(mapcenterX, mapcenterZ) local mapdiagonal = math.ceil(math.sqrt((mapsizeX*mapsizeX)+(mapsizeZ*mapsizeZ))) - Spring.GiveOrderToUnit(unitID, CMD.REPAIR,{mapcenterX+random(-100,100),mapcenterY,mapcenterZ+random(-100,100),mapdiagonal}, 0) + spGiveOrderToUnit(unitID, CMD.REPAIR,{mapcenterX+random(-100,100),mapcenterY,mapcenterZ+random(-100,100),mapdiagonal}, 0) success = true else local r2 = random(0, 1) @@ -444,16 +417,16 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u if r == 0 or mcurrent > mstorage*0.9 or string.sub(luaAI, 1, 19) == 'SimpleConstructorAI' then local project = SimpleConstructorDefs:RandomChoice() if buildOptions and buildOptions[project] then - local x, y, z = Spring.GetUnitPosition(unitID) - Spring.GiveOrderToUnit(unitID, -project, { x, y, z, 0 }, 0) + local x, y, z = spGetUnitPosition(unitID) + spGiveOrderToUnit(unitID, -project, { x, y, z, 0 }, 0) --Spring.Echo("Success! Project Type: Constructor.") success = true end else local project = SimpleUndefinedUnitDefs:RandomChoice() if buildOptions and buildOptions[project] then - local x, y, z = Spring.GetUnitPosition(unitID) - Spring.GiveOrderToUnit(unitID, -project, { x, y, z, 0 }, 0) + local x, y, z = spGetUnitPosition(unitID) + spGiveOrderToUnit(unitID, -project, { x, y, z, 0 }, 0) --Spring.Echo("Success! Project Type: Unit.") success = true end @@ -485,8 +458,8 @@ if gadgetHandler:IsSyncedCode() then --tracy.ZoneBeginN("SimpleAI:GameFrame") local teamID = SimpleAITeamIDs[i] local _, _, _, _, _, allyTeamID = Spring.GetTeamInfo(teamID) - local mcurrent, mstorage = Spring.GetTeamResources(teamID, "metal") - local ecurrent, estorage = Spring.GetTeamResources(teamID, "energy") + local mcurrent, mstorage = spGetTeamResources(teamID, "metal") + local ecurrent, estorage = spGetTeamResources(teamID, "energy") for j = 1, #SimpleAITeamIDs do if teamID == SimpleAITeamIDs[j] then -- --cheats @@ -502,44 +475,44 @@ if gadgetHandler:IsSyncedCode() then local units = Spring.GetTeamUnits(teamID) for k = 1, #units do local unitID = units[k] - local unitDefID = Spring.GetUnitDefID(unitID) + local unitDefID = spGetUnitDefID(unitID) local unitTeam = teamID - local unitHealth, unitMaxHealth, _, _, _ = Spring.GetUnitHealth(unitID) - local unitCommands = Spring.GetCommandQueue(unitID, 0) - local unitposx, unitposy, unitposz = Spring.GetUnitPosition(unitID) + local unitHealth, unitMaxHealth, _, _, _ = spGetUnitHealth(unitID) + local unitCommands = spGetCommandQueue(unitID, 0) + local unitposx, unitposy, unitposz = spGetUnitPosition(unitID) --Spring.Echo(UnitDefs[unitDefID].name, "has commands:",unitCommands, SimpleConstructorDefs[unitDefID] , SimpleCommanderDefs[unitDefID], SimpleFactoriesDefs[unitDefID] ,SimpleUndefinedUnitDefs[unitDefID] ) -- Commanders if SimpleCommanderDefs[unitDefID] then - local nearestEnemyCloak = Spring.GetUnitNearestEnemy(unitID, 2000, false) + local nearestEnemyCloak = spGetUnitNearestEnemy(unitID, 2000, false) if nearestEnemyCloak and ecurrent > 1000 then - Spring.GiveOrderToUnit(unitID, 37382, {1}, 0) + spGiveOrderToUnit(unitID, 37382, {1}, 0) else - Spring.GiveOrderToUnit(unitID, 37382, {0}, 0) + spGiveOrderToUnit(unitID, 37382, {0}, 0) end - local nearestEnemy = Spring.GetUnitNearestEnemy(unitID, 250, true) + local nearestEnemy = spGetUnitNearestEnemy(unitID, 250, true) local unitHealthPercentage = (unitHealth/unitMaxHealth)*100 if nearestEnemy and unitHealthPercentage > 30 then if ecurrent < estorage*0.9 then Spring.SetTeamResource(teamID, "e", estorage*0.9) end - Spring.GiveOrderToUnit(unitID, CMD.DGUN, {nearestEnemy}, 0) - local nearestEnemies = Spring.GetUnitsInCylinder(unitposx, unitposz, 300) + spGiveOrderToUnit(unitID, CMD.DGUN, {nearestEnemy}, 0) + local nearestEnemies = spGetUnitsInCylinder(unitposx, unitposz, 300) for x = 1,#nearestEnemies do local enemy = nearestEnemies[x] - if Spring.GetUnitTeam(enemy) == Spring.GetUnitTeam(nearestEnemy) and enemy ~= nearestEnemy then - Spring.GiveOrderToUnit(unitID, CMD.DGUN, {enemy}, {"shift"}) + if spGetUnitTeam(enemy) == spGetUnitTeam(nearestEnemy) and enemy ~= nearestEnemy then + spGiveOrderToUnit(unitID, CMD.DGUN, {enemy}, {"shift"}) end end - Spring.GiveOrderToUnit(unitID, CMD.MOVE, {unitposx, unitposy, unitposz}, {"shift"}) + spGiveOrderToUnit(unitID, CMD.MOVE, {unitposx, unitposy, unitposz}, {"shift"}) elseif nearestEnemy then for x = 1,10 do local targetUnit = units[random(1,#units)] - if UnitDefs[Spring.GetUnitDefID(targetUnit)].isBuilding == true then - local tUnitX, tUnitY, tUnitZ = Spring.GetUnitPosition(targetUnit) - Spring.GiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0) + if isBuilding[spGetUnitDefID(targetUnit)] then + local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit) + spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0) break end end @@ -549,15 +522,15 @@ if gadgetHandler:IsSyncedCode() then -- Constructors if SimpleConstructorDefs[unitDefID] then local unitHealthPercentage = (unitHealth/unitMaxHealth)*100 - local nearestEnemy = Spring.GetUnitNearestEnemy(unitID, 500, true) + local nearestEnemy = spGetUnitNearestEnemy(unitID, 500, true) if nearestEnemy and unitHealthPercentage > 90 then - Spring.GiveOrderToUnit(unitID, CMD.RECLAIM, {nearestEnemy}, 0) + spGiveOrderToUnit(unitID, CMD.RECLAIM, {nearestEnemy}, 0) elseif nearestEnemy then for x = 1,100 do local targetUnit = units[random(1,#units)] - if UnitDefs[Spring.GetUnitDefID(targetUnit)].isBuilding == true then - local tUnitX, tUnitY, tUnitZ = Spring.GetUnitPosition(targetUnit) - Spring.GiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0) + if isBuilding[spGetUnitDefID(targetUnit)] then + local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit) + spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0) break end end @@ -579,29 +552,28 @@ if gadgetHandler:IsSyncedCode() then end -- army - if SimpleUndefinedUnitDefs[unitDefID] then local luaAI = Spring.GetTeamLuaAI(teamID) if string.sub(luaAI, 1, 16) == 'SimpleDefenderAI' then allunits = allunits or Spring.GetAllUnits() for t = 1,10 do local targetUnit = allunits[random(1,#allunits)] - if Spring.GetUnitAllyTeam(targetUnit) == allyTeamID then - local tUnitX, tUnitY, tUnitZ = Spring.GetUnitPosition(targetUnit) - Spring.GiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" }) + if spGetUnitAllyTeam(targetUnit) == allyTeamID then + local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit) + spGiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" }) break end end else - local targetUnitNear = Spring.GetUnitNearestEnemy(unitID, 2000, false) + local targetUnitNear = spGetUnitNearestEnemy(unitID, 2000, false) if targetUnitNear then - local tUnitX, tUnitY, tUnitZ = Spring.GetUnitPosition(targetUnitNear) - Spring.GiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" }) + local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnitNear) + spGiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" }) elseif n%3600 <= 15*SimpleAITeamIDsCount then - local targetUnit = Spring.GetUnitNearestEnemy(unitID, 999999, false) + local targetUnit = spGetUnitNearestEnemy(unitID, 999999, false) if targetUnit then - local tUnitX, tUnitY, tUnitZ = Spring.GetUnitPosition(targetUnit) - Spring.GiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" }) + local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit) + spGiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" }) end end end @@ -618,8 +590,8 @@ if gadgetHandler:IsSyncedCode() then function gadget:UnitCreated(unitID, unitDefID, unitTeam, builderID) for i = 1, SimpleAITeamIDsCount do if SimpleAITeamIDs[i] == unitTeam then - Spring.GiveOrderToUnit(unitID,CMD.FIRE_STATE,{2},0) - Spring.GiveOrderToUnit(unitID,CMD.MOVE_STATE,{2},0) + spGiveOrderToUnit(unitID,CMD.FIRE_STATE,{2},0) + spGiveOrderToUnit(unitID,CMD.MOVE_STATE,{2},0) if SimpleFactoriesDefs[unitDefID] then SimpleFactoriesCount[unitTeam] = SimpleFactoriesCount[unitTeam] + 1 @@ -654,8 +626,8 @@ if gadgetHandler:IsSyncedCode() then function gadget:UnitFinished(unitID, unitDefID, unitTeam) for i = 1, SimpleAITeamIDsCount do if SimpleAITeamIDs[i] == unitTeam then - Spring.GiveOrderToUnit(unitID,CMD.FIRE_STATE,{2},0) - Spring.GiveOrderToUnit(unitID,CMD.MOVE_STATE,{2},0) + spGiveOrderToUnit(unitID,CMD.FIRE_STATE,{2},0) + spGiveOrderToUnit(unitID,CMD.MOVE_STATE,{2},0) end end end