Skip to content

Commit

Permalink
gaia_critters: remove critters unconditionally when killed (#4099)
Browse files Browse the repository at this point in the history
* gaia_critters: remove critters from list even if no attacker was
reported.

* Add a flag so we know when UnitDestroyed comes from ourselves.

* Only block commands or remove from critter list if the critter is alive.
  • Loading branch information
saurtron authored Jan 3, 2025
1 parent a3f7a89 commit e3acab6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions luarules/gadgets/gaia_critters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ local companionRadius = companionRadiusStart
local processOrders = true
local addedInitialCritters

local ownCritterDestroy = false

local function randomPatrolInBox(unitID, box, minWaterDepth) -- only define minWaterDepth if unit is a submarine
local ux,_,uz = GetUnitPosition(unitID,true,true)
local waterRadius = 1000 -- max distance a submarine unit will travel
Expand Down Expand Up @@ -275,6 +277,7 @@ local function adjustCritters(newAliveCritters)

local removeKeys = {}
local removeKeysCount = 0
ownCritterDestroy = true -- flag to skip UnitDestroyed callin
for unitID, critter in pairs(critterUnits) do
if add and not critter.alive or not add and critter.alive then
if add then
Expand All @@ -299,6 +302,7 @@ local function adjustCritters(newAliveCritters)
if critterDifference > -1 and critterDifference < 1 then break end
end
end
ownCritterDestroy = false
if add then
for i, unitID in ipairs(removeKeys) do
critterUnits[unitID] = nil -- this however leaves these keys still being iterated
Expand Down Expand Up @@ -570,7 +574,7 @@ end

function gadget:UnitDestroyed(unitID, unitDefID, teamID, attackerID, attackerDefID, attackerTeamID)
commanders[unitID] = nil
if critterUnits[unitID] ~= nil and attackerID ~= nil then
if not ownCritterDestroy and critterUnits[unitID] and critterUnits[unitID].alive then
critterUnits[unitID] = nil
totalCritters = totalCritters - 1
end
Expand All @@ -580,7 +584,8 @@ end
function gadget:AllowCommand(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOptions, cmdTag, playerID, fromSynced, fromLua)
-- accepts: CMD.ATTACK
if cmdParams and #cmdParams == 1 then
if critterUnits[cmdParams[1]] ~= nil then
local critter = critterUnits[cmdParams[1]]
if critter and critter.alive then
return false
end
end
Expand Down

0 comments on commit e3acab6

Please sign in to comment.