Finding if there is a combat unit in a city for stacking

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
I am spawning a unit in a city. If I know a city plot will...

Plot:GetNumDefenders(PlayerID) < 1

if true, allow the spawn without causing overstacking? Or does it count civilians as defenders?

Or is there a more direct way to detect if a plot will be overstacked?

PS I found Plot:GetNumUnits(), but I think that may count civilians as well.
 
If you know the plot is a city plot, you can make use of:
Code:
if (myCity:GetGarrisonedUnit() ~= nil) then
which I borrowed directly off the modwiki sample usages of City:GetGarrisonedUnit().

----------------------------------------------------------------------------------

This is the way whoward did a unit-spawning routine:
Code:
function SpawnAtPlot(pPlayer, iUnitType, pPlot)
  local pUnit = pPlayer:InitUnit(iUnitType, pPlot:GetX(), pPlot:GetY())
  
  if (pUnit and pPlot:GetNumUnits() > 1) then
    pUnit:JumpToNearestValidPlot()
  end
  
  return pUnit
end
Whoward's code is more adaptable in that it doesn't need to know whether or not the plot is a city. It just makes the assumption that it is better to make the unit being spawned shift to an available empty plot if there is any other unit on the target plot. The caveat is you should not use whoward's code if you are spawning-in an air unit.
 
Back
Top Bottom