Skip to content

Commit

Permalink
Only allow builders to guard out of map (#3969)
Browse files Browse the repository at this point in the history
* Out of map guard only for builder to builder.
  • Loading branch information
saurtron authored Nov 28, 2024
1 parent a6ac9d9 commit 38c5745
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions luarules/gadgets/unit_prevent_aircraft_hax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@ local mapZ = Game.mapSizeZ
local allMobileUnits = {}
local spGetUnitPosition = Spring.GetUnitPosition
local spGetUnitTeam = Spring.GetUnitTeam
local spGetUnitDefID = Spring.GetUnitDefID
local spValidUnitID = Spring.ValidUnitID
local spIsPosInMap = Spring.IsPosInMap
local CMD_STOP = CMD.STOP
local CMD_GUARD = CMD.GUARD

local isMobileUnit = {}
local isBuilder = {}
for unitDefID, udef in pairs(UnitDefs) do
if not (udef.isBuilding or udef.speed == 0) then
isMobileUnit[unitDefID] = true
end
if udef.isBuilder and (udef.buildSpeed and udef.buildSpeed > 0) and (udef.buildDistance and udef.buildDistance > 0) then
isBuilder[unitDefID] = true
end
end

function gadget:Initialize()
gadgetHandler:RegisterAllowCommand(CMD_STOP)
gadgetHandler:RegisterAllowCommand(CMD_GUARD)
for _, unitID in pairs(Spring.GetAllUnits()) do
gadget:UnitCreated(unitID, Spring.GetUnitDefID(unitID), spGetUnitTeam(unitID))
end
Expand Down Expand Up @@ -76,12 +85,22 @@ function gadget:GameFrame(f)
end
end

local function isInsideMap(unitID)
local x,_,z = spGetUnitPosition(unitID)
return spIsPosInMap(x, z)
end

function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions, cmdTag, fromSynced, fromLua)
-- accepts: CMD_STOP
if isMobileUnit[unitDefID] then
local x,_,z = Spring.GetUnitPosition(unitID)
if z < 0 or x < 0 or z > mapZ or x > mapX then
return false
if cmdID == CMD_STOP and isMobileUnit[unitDefID] then
return isInsideMap(unitID)
elseif cmdID == CMD_GUARD then
-- To guard out of map both units must be builders
local guardeeID = cmdParams[1]
if guardeeID and spValidUnitID(guardeeID) and not isInsideMap(guardeeID) then
local guardeeUnitDefID = spGetUnitDefID(guardeeID)
if not (isBuilder[unitDefID] and isBuilder[guardeeUnitDefID]) then
return false
end
end
end
return true
Expand Down

0 comments on commit 38c5745

Please sign in to comment.