LUA to randomly move unit 1 tile

Tekamthi

Emperor
Joined
Aug 12, 2016
Messages
1,662
I've been trying to have a unit "drift" while in ocean tiles... I have created some basic lua to accomplish this -- problem is I'm not super familiar with how X/Y coordinates work on a hex grid... result is sometimes my unit drifts 2 tiles.

edit: nvm.. got it, posted below in case anyone else comes across this:

Code:
for pUnit in pPlayer:Units() do
        local pPlot = pUnit:GetPlot()
        local pType = pUnit:GetUnitType()
        if ... then
            local pDrift = Game.Rand(7, "Drift Roll") - 1
            local pAdjacentPlot = Map.PlotDirection(pPlot:GetX(), pPlot:GetY(), pDrift)
            pUnit:SetXY(pAdjacentPlot:GetX(), pAdjacentPlot:GetY())
        end
i still get the occasional exception from AI units concerning nil pPlot, but it seems to work as intended nonetheless
 
Last edited:
i still get the occasional exception from AI units concerning nil pPlot, but it seems to work as intended nonetheless
That happens when a Unit is at the very edge of the map and is trying to move off the map. A simple fix would be to check if pAdjacentPlot is not nil. I.e.:
Code:
local pAdjacentPlot = Map.PlotDirection(pPlot:GetX(), pPlot:GetY(), pDrift)
if pAdjacentPlot ~= nil then
           pUnit:SetXY(pAdjacentPlot:GetX(), pAdjacentPlot:GetY())
end
 
A bit late to the party, but I figured I'd add my 2 cents.
If the world wraps, then the correct behavior would be to allow the unit to drift to the other side of the map.
Code:
...
local mapW, mapH = Map.GetGridSize()
local adjacentX = pAdjacentPlot:GetX()
local adjacentY = pAdjacentPlot:GetY()
if (Map.IsWrapX()) then
    if (adjacentX >= mapW) then
        adjacentX = 0
    elseif (adjacentX < 0) then
        adjacentX = mapW - 1
    end
end
if (Map.IsWrapY()) then
    if (adjacentY >= mapH) then
        adjacentY = 0
    elseif (adjacentY < 0) then
        adjacentY = mapH - 1
    end
end
pUnit:SetXY(adjacentX, adjacentY)

EDIT: Made a change to above code so it wraps BOTH ways. ;)
 
Last edited:
A bit late to the party, but I figured I'd add my 2 cents.
If the world wraps, then the correct behavior would be to allow the unit to drift to the other side of the map.
[snip]

IIRC, the last time I checked the wrapping was done automatically on the X-axis when using Map.PlotDirection(...). I hadn't checked for the Y-axis though since that seemed logical for me to work as well at that time, but you may never know with Firaxis!

I also checked Whoward's plotIterators to verify (assuming that the wrapping was simply not overlooked by whoward), and it does not seem to have special handling for wrapping either. It seems that either (or both) Map.GetPlot(...) and ToGridFromHex(...) also have the 'implicit wrapping'.
Maybe all Map functions have the implicit wrapping?
 
Maybe all Map functions have the implicit wrapping?
Not sure about that. I did something similar to the OP for a BE mod I was working on where certain units followed other units as an escort, but with some random variation thrown in, and I remember there was an issue with wrapping I had to work around like this, but my logic was a lot more complicated and I don't remember which calls were the problematic ones.
 
strangely it was either my pPlot assignment, or my if statement pPlot reference that threw the occasional exception.. but just for some AI units, not all.

Code:
pPlot:GetTerrainType() == TerrainTypes.TERRAIN_OCEAN

I'd have to open game/firetuner again to reconfirm, but pretty sure it was the above line that was/is the problem.

guessing some AI units are parked in memory but off the map during some obscure scenario, before being positioned somewhere as their turn gets rolling? I have no idea why really, but my if statement is supposed to fail anyway under this condition, so it works fine for my needs.

Otherwise seems to work as intended w/o error.. so I would infer that wrapping is working alright w/o express provision for it? I'm guessing Map.PlotDirection must do it on its own

Anyway thanks for advice!
 
Last edited:
You'll also need several checks that the drift to plot is available, eg no ice, no incompatible unit, etc
 
Top Bottom