This code seems to work, but I've only made a couple small tests, and haven't tried to get the AI to pillage anything.2. Is there any way to totally prevent pillaging?
Code:
---@type unitObject|nil
local possiblePillager = nil
---@type tileObject|nil
local possiblePillagedTile = nil
---@type bitmask|nil
local possiblePillagedTileImprovements = nil
discreteEvents.onActivateUnit(function(unit)
if gen.isCanImproveTiles(unit.type) then
possiblePillager = nil
possiblePillagedTile = nil
possiblePillagedTileImprovements = nil
else
possiblePillager = unit
possiblePillagedTile = unit.location
possiblePillagedTileImprovements = unit.location.improvements
end
end)
discreteEvents.onFinalOrderGiven(function(unit)
if possiblePillager ~= unit or possiblePillagedTile ~= unit.location then
possiblePillager = nil
possiblePillagedTile = nil
possiblePillagedTileImprovements = nil
return
end
if possiblePillagedTileImprovements == nil then
possiblePillager = nil
possiblePillagedTile = nil
possiblePillagedTileImprovements = nil
return
end
-- can only get here if the possible pillaged tile isn't nil
---@cast possiblePillagedTile tileObject
possiblePillagedTile.improvements = possiblePillagedTileImprovements
possiblePillager = nil
possiblePillagedTile = nil
possiblePillagedTileImprovements = nil
return
end)