Skip to content

Commit

Permalink
simpleai cleanup and minor localization (#4225)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruwetuin authored Jan 25, 2025
1 parent 5c6a0db commit bddc599
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions luarules/gadgets/ai_simpleai.lua
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
local enabled = false
local teams = Spring.GetTeamList()
local SimpleAITeamIDs = {}
local SimpleAITeamIDsCount = 0
--local UDN = UnitDefNames
local wind = Game.windMax
local mapsizeX = Game.mapSizeX
local mapsizeZ = Game.mapSizeZ
local random = math.random

--Spring.Echo("tracy", tracy)

local MakeHashedPosTable = VFS.Include("luarules/utilities/damgam_lib/hashpostable.lua")
local HashPosTable = MakeHashedPosTable()

local positionCheckLibrary = VFS.Include("luarules/utilities/damgam_lib/position_checks.lua")

-- team locals
local SimpleFactoriesCount = {}
local SimpleFactories = {}
local SimpleT1Mexes = {}
local SimpleFactoryDelay = {}

local teams = Spring.GetTeamList()
for i = 1, #teams do
local teamID = teams[i]
local luaAI = Spring.GetTeamLuaAI(teamID)
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
enabled = true
SimpleAITeamIDsCount = SimpleAITeamIDsCount + 1
SimpleAITeamIDs[SimpleAITeamIDsCount] = teamID

SimpleFactoriesCount[teamID] = 0
SimpleFactories[teamID] = {}
SimpleT1Mexes[teamID] = 0
SimpleFactoryDelay[teamID] = 0
end
end
teams = nil

function gadget:GetInfo()
return {
Expand All @@ -48,6 +33,23 @@ function gadget:GetInfo()
}
end

--Spring.Echo("tracy", tracy)

local wind = Game.windMax
local mapsizeX = Game.mapSizeX
local mapsizeZ = Game.mapSizeZ
local random = math.random
local min = math.min
local CMD_MOVE = CMD.MOVE
local CMD_RECLAIM = CMD.RECLAIM
local CMD_REPAIR = CMD.REPAIR
local CMD_FIGHT = CMD.FIGHT

local MakeHashedPosTable = VFS.Include("luarules/utilities/damgam_lib/hashpostable.lua")
local HashPosTable = MakeHashedPosTable()

local positionCheckLibrary = VFS.Include("luarules/utilities/damgam_lib/position_checks.lua")

-- manually appoint units to avoid making
-- (note that transports, stockpilers and objects/walls are auto skipped)
local BadUnitsList = {}
Expand Down Expand Up @@ -100,6 +102,9 @@ for unitDefID, unitDef in pairs(UnitDefs) do
if unitDef.isBuilding then
isBuilding[unitDefID] = {unitDef.xsize, unitDef.zsize}
end
if unitDef.customParams.iscommander then
isCommander[unitDefID] = {unitDef.xsize, unitDef.zsize}
end

local skip = false
for a = 1, #BadUnitsList do
Expand Down Expand Up @@ -137,7 +142,6 @@ 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
Expand Down Expand Up @@ -240,14 +244,14 @@ local function SimpleBuildOrder(cUnitID, building)
local units = spGetUnitsInCylinder(cunitposx, cunitposz, searchRange, team)
if #units > 1 then
local gaveOrder = false
for k=1,math.min(#units, 5 + b2 * 2) do
for k=1,min(#units, 5 + b2 * 2) do
numtests = numtests+1
local buildnear = units[random(1, #units)]
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 reffootx = (isBuilding[refDefID] and isBuilding[refDefID][1] or isCommander[refDefID][1]) * 8
local reffootz = (isBuilding[refDefID] and isBuilding[refDefID][2] or isCommander[refDefID][2]) * 8
local spacing = random(64, 128)
local testspacing = spacing * 0.75
local buildingDefID = building
Expand Down Expand Up @@ -322,7 +326,7 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u
-- local targetUnit = units[math.random(1,#units)]
-- 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" })
-- spGiveOrderToUnit(unitID, CMD_MOVE, { tUnitX + math.random(-100, 100), tUnitY, tUnitZ + math.random(-100, 100) }, { "shift", "alt", "ctrl" })
-- success = true
-- break
-- end
Expand Down Expand Up @@ -373,7 +377,7 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u
local targetUnit = units[random(1,#units)]
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" })
spGiveOrderToUnit(unitID, CMD_MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
success = true
break
end
Expand All @@ -383,14 +387,14 @@ local function SimpleConstructionProjectSelection(unitID, unitDefID, unitTeam, u
local mapcenterZ = mapsizeZ/2
local mapcenterY = spGetGroundHeight(mapcenterX, mapcenterZ)
local mapdiagonal = math.ceil(math.sqrt((mapsizeX*mapsizeX)+(mapsizeZ*mapsizeZ)))
spGiveOrderToUnit(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 = spGetGroundHeight(mapcenterX, mapcenterZ)
local mapdiagonal = math.ceil(math.sqrt((mapsizeX*mapsizeX)+(mapsizeZ*mapsizeZ)))
spGiveOrderToUnit(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)
Expand Down Expand Up @@ -506,13 +510,13 @@ if gadgetHandler:IsSyncedCode() then
spGiveOrderToUnit(unitID, CMD.DGUN, {enemy}, {"shift"})
end
end
spGiveOrderToUnit(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 isBuilding[spGetUnitDefID(targetUnit)] then
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0)
spGiveOrderToUnit(unitID, CMD_MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0)
break
end
end
Expand All @@ -524,13 +528,13 @@ if gadgetHandler:IsSyncedCode() then
local unitHealthPercentage = (unitHealth/unitMaxHealth)*100
local nearestEnemy = spGetUnitNearestEnemy(unitID, 500, true)
if nearestEnemy and unitHealthPercentage > 90 then
spGiveOrderToUnit(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 isBuilding[spGetUnitDefID(targetUnit)] then
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
spGiveOrderToUnit(unitID, CMD.MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0)
spGiveOrderToUnit(unitID, CMD_MOVE, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, 0)
break
end
end
Expand Down Expand Up @@ -560,20 +564,20 @@ if gadgetHandler:IsSyncedCode() then
local targetUnit = allunits[random(1,#allunits)]
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" })
spGiveOrderToUnit(unitID, CMD_FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
break
end
end
else
local targetUnitNear = spGetUnitNearestEnemy(unitID, 2000, false)
if targetUnitNear then
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnitNear)
spGiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
spGiveOrderToUnit(unitID, CMD_FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
elseif n%3600 <= 15*SimpleAITeamIDsCount then
local targetUnit = spGetUnitNearestEnemy(unitID, 999999, false)
if targetUnit then
local tUnitX, tUnitY, tUnitZ = spGetUnitPosition(targetUnit)
spGiveOrderToUnit(unitID, CMD.FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
spGiveOrderToUnit(unitID, CMD_FIGHT, { tUnitX + random(-100, 100), tUnitY, tUnitZ + random(-100, 100) }, { "shift", "alt", "ctrl" })
end
end
end
Expand Down

0 comments on commit bddc599

Please sign in to comment.