Skip to content

Commit bddc599

Browse files
authored
simpleai cleanup and minor localization (#4225)
1 parent 5c6a0db commit bddc599

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

luarules/gadgets/ai_simpleai.lua

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
11
local enabled = false
2-
local teams = Spring.GetTeamList()
32
local SimpleAITeamIDs = {}
43
local SimpleAITeamIDsCount = 0
5-
--local UDN = UnitDefNames
6-
local wind = Game.windMax
7-
local mapsizeX = Game.mapSizeX
8-
local mapsizeZ = Game.mapSizeZ
9-
local random = math.random
10-
11-
--Spring.Echo("tracy", tracy)
12-
13-
local MakeHashedPosTable = VFS.Include("luarules/utilities/damgam_lib/hashpostable.lua")
14-
local HashPosTable = MakeHashedPosTable()
15-
16-
local positionCheckLibrary = VFS.Include("luarules/utilities/damgam_lib/position_checks.lua")
17-
18-
-- team locals
194
local SimpleFactoriesCount = {}
205
local SimpleFactories = {}
216
local SimpleT1Mexes = {}
227
local SimpleFactoryDelay = {}
23-
8+
local teams = Spring.GetTeamList()
249
for i = 1, #teams do
2510
local teamID = teams[i]
2611
local luaAI = Spring.GetTeamLuaAI(teamID)
2712
if luaAI and luaAI ~= "" and (string.sub(luaAI, 1, 8) == 'SimpleAI' or string.sub(luaAI, 1, 15) == 'SimpleCheaterAI' or string.sub(luaAI, 1, 16) == 'SimpleDefenderAI' or string.sub(luaAI, 1, 19) == 'SimpleConstructorAI') then
2813
enabled = true
2914
SimpleAITeamIDsCount = SimpleAITeamIDsCount + 1
3015
SimpleAITeamIDs[SimpleAITeamIDsCount] = teamID
31-
3216
SimpleFactoriesCount[teamID] = 0
3317
SimpleFactories[teamID] = {}
3418
SimpleT1Mexes[teamID] = 0
3519
SimpleFactoryDelay[teamID] = 0
3620
end
3721
end
22+
teams = nil
3823

3924
function gadget:GetInfo()
4025
return {
@@ -48,6 +33,23 @@ function gadget:GetInfo()
4833
}
4934
end
5035

36+
--Spring.Echo("tracy", tracy)
37+
38+
local wind = Game.windMax
39+
local mapsizeX = Game.mapSizeX
40+
local mapsizeZ = Game.mapSizeZ
41+
local random = math.random
42+
local min = math.min
43+
local CMD_MOVE = CMD.MOVE
44+
local CMD_RECLAIM = CMD.RECLAIM
45+
local CMD_REPAIR = CMD.REPAIR
46+
local CMD_FIGHT = CMD.FIGHT
47+
48+
local MakeHashedPosTable = VFS.Include("luarules/utilities/damgam_lib/hashpostable.lua")
49+
local HashPosTable = MakeHashedPosTable()
50+
51+
local positionCheckLibrary = VFS.Include("luarules/utilities/damgam_lib/position_checks.lua")
52+
5153
-- manually appoint units to avoid making
5254
-- (note that transports, stockpilers and objects/walls are auto skipped)
5355
local BadUnitsList = {}
@@ -100,6 +102,9 @@ for unitDefID, unitDef in pairs(UnitDefs) do
100102
if unitDef.isBuilding then
101103
isBuilding[unitDefID] = {unitDef.xsize, unitDef.zsize}
102104
end
105+
if unitDef.customParams.iscommander then
106+
isCommander[unitDefID] = {unitDef.xsize, unitDef.zsize}
107+
end
103108

104109
local skip = false
105110
for a = 1, #BadUnitsList do
@@ -137,7 +142,6 @@ for unitDefID, unitDef in pairs(UnitDefs) do
137142

138143
if not skip then
139144
if unitDef.customParams.iscommander then
140-
isCommander[unitDefID] = true
141145
SimpleCommanderDefs[unitDefID] = 1
142146
elseif unitDef.isFactory and #unitDef.buildOptions > 0 then
143147
SimpleFactoriesDefs[unitDefID] = 1
@@ -240,14 +244,14 @@ local function SimpleBuildOrder(cUnitID, building)
240244
local units = spGetUnitsInCylinder(cunitposx, cunitposz, searchRange, team)
241245
if #units > 1 then
242246
local gaveOrder = false
243-
for k=1,math.min(#units, 5 + b2 * 2) do
247+
for k=1,min(#units, 5 + b2 * 2) do
244248
numtests = numtests+1
245249
local buildnear = units[random(1, #units)]
246250
local refDefID = spGetUnitDefID(buildnear)
247251
if isBuilding[unitDefID] or isCommander[refDefID] then
248252
local refx, _, refz = spGetUnitPosition(buildnear)
249-
local reffootx = isBuilding[refDefID] and isBuilding[refDefID][1] * 8 or 16
250-
local reffootz = isBuilding[refDefID] and isBuilding[refDefID][2] * 8 or 16
253+
local reffootx = (isBuilding[refDefID] and isBuilding[refDefID][1] or isCommander[refDefID][1]) * 8
254+
local reffootz = (isBuilding[refDefID] and isBuilding[refDefID][2] or isCommander[refDefID][2]) * 8
251255
local spacing = random(64, 128)
252256
local testspacing = spacing * 0.75
253257
local buildingDefID = building
@@ -322,7 +326,7 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u
322326
-- local targetUnit = units[math.random(1,#units)]
323327
-- if isBuilding[spGetUnitDefID(targetUnit)] then
324328
-- local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
325-
-- spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + math.random(-100, 100), tUnitY, tUnitZ + math.random(-100, 100) }, { "shift", "alt", "ctrl" })
329+
-- spGiveOrderToUnit(unitID, CMD_MOVE, { tUnitX + math.random(-100, 100), tUnitY, tUnitZ + math.random(-100, 100) }, { "shift", "alt", "ctrl" })
326330
-- success = true
327331
-- break
328332
-- end
@@ -373,7 +377,7 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u
373377
local targetUnit = units[random(1,#units)]
374378
if isBuilding[spGetUnitDefID(targetUnit)] then
375379
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
376-
spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
380+
spGiveOrderToUnit(unitID, CMD_MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
377381
success = true
378382
break
379383
end
@@ -383,14 +387,14 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u
383387
local mapcenterZ = mapsizeZ/2
384388
local mapcenterY = spGetGroundHeight(mapcenterX, mapcenterZ)
385389
local mapdiagonal = math.ceil(math.sqrt((mapsizeX*mapsizeX)+(mapsizeZ*mapsizeZ)))
386-
spGiveOrderToUnit(unitID, CMD.RECLAIM,{mapcenterX+random(-100,100),mapcenterY,mapcenterZ+random(-100,100),mapdiagonal}, 0)
390+
spGiveOrderToUnit(unitID, CMD_RECLAIM,{mapcenterX+random(-100,100),mapcenterY,mapcenterZ+random(-100,100),mapdiagonal}, 0)
387391
success = true
388392
elseif r == 13 and type ~= "Commander" then
389393
local mapcenterX = mapsizeX/2
390394
local mapcenterZ = mapsizeZ/2
391395
local mapcenterY = spGetGroundHeight(mapcenterX, mapcenterZ)
392396
local mapdiagonal = math.ceil(math.sqrt((mapsizeX*mapsizeX)+(mapsizeZ*mapsizeZ)))
393-
spGiveOrderToUnit(unitID, CMD.REPAIR,{mapcenterX+random(-100,100),mapcenterY,mapcenterZ+random(-100,100),mapdiagonal}, 0)
397+
spGiveOrderToUnit(unitID, CMD_REPAIR,{mapcenterX+random(-100,100),mapcenterY,mapcenterZ+random(-100,100),mapdiagonal}, 0)
394398
success = true
395399
else
396400
local r2 = random(0, 1)
@@ -506,13 +510,13 @@ if gadgetHandler:IsSyncedCode() then
506510
spGiveOrderToUnit(unitID, CMD.DGUN, {enemy}, {"shift"})
507511
end
508512
end
509-
spGiveOrderToUnit(unitID, CMD.MOVE, {unitposx, unitposy, unitposz}, {"shift"})
513+
spGiveOrderToUnit(unitID, CMD_MOVE, {unitposx, unitposy, unitposz}, {"shift"})
510514
elseif nearestEnemy then
511515
for x = 1,10 do
512516
local targetUnit = units[random(1,#units)]
513517
if isBuilding[spGetUnitDefID(targetUnit)] then
514518
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
515-
spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0)
519+
spGiveOrderToUnit(unitID, CMD_MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0)
516520
break
517521
end
518522
end
@@ -524,13 +528,13 @@ if gadgetHandler:IsSyncedCode() then
524528
local unitHealthPercentage = (unitHealth/unitMaxHealth)*100
525529
local nearestEnemy = spGetUnitNearestEnemy(unitID, 500, true)
526530
if nearestEnemy and unitHealthPercentage > 90 then
527-
spGiveOrderToUnit(unitID, CMD.RECLAIM, {nearestEnemy}, 0)
531+
spGiveOrderToUnit(unitID, CMD_RECLAIM, {nearestEnemy}, 0)
528532
elseif nearestEnemy then
529533
for x = 1,100 do
530534
local targetUnit = units[random(1,#units)]
531535
if isBuilding[spGetUnitDefID(targetUnit)] then
532536
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
533-
spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0)
537+
spGiveOrderToUnit(unitID, CMD_MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0)
534538
break
535539
end
536540
end
@@ -560,20 +564,20 @@ if gadgetHandler:IsSyncedCode() then
560564
local targetUnit = allunits[random(1,#allunits)]
561565
if spGetUnitAllyTeam(targetUnit) == allyTeamID then
562566
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
563-
spGiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
567+
spGiveOrderToUnit(unitID, CMD_FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
564568
break
565569
end
566570
end
567571
else
568572
local targetUnitNear = spGetUnitNearestEnemy(unitID, 2000, false)
569573
if targetUnitNear then
570574
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnitNear)
571-
spGiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
575+
spGiveOrderToUnit(unitID, CMD_FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
572576
elseif n%3600 <= 15*SimpleAITeamIDsCount then
573577
local targetUnit = spGetUnitNearestEnemy(unitID, 999999, false)
574578
if targetUnit then
575579
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
576-
spGiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
580+
spGiveOrderToUnit(unitID, CMD_FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
577581
end
578582
end
579583
end

0 commit comments

Comments
 (0)