-
Notifications
You must be signed in to change notification settings - Fork 509
Units::teleport: keep occupancy flags while other units remain on the tile #5946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| config.mode = 'fortress' | ||
| config.target = 'core' | ||
|
|
||
| local function tile_occupancy(pos) | ||
| return select(2, dfhack.maps.getTileFlags(pos)) | ||
| end | ||
|
SilasD marked this conversation as resolved.
|
||
|
|
||
| local function two_citizens() | ||
| local a, b | ||
| for _, unit in ipairs(df.global.world.units.active) do | ||
| if dfhack.units.isCitizen(unit) then | ||
| if not a then | ||
| a = unit | ||
| else | ||
| b = unit | ||
| break | ||
| end | ||
| end | ||
| end | ||
| return a, b | ||
| end | ||
|
|
||
| -- find an allocated, walkable tile with no unit occupancy near pos | ||
| local function free_tile_near(pos) | ||
| for dx = -4, 4 do for dy = -4, 4 do | ||
| if dx ~= 0 or dy ~= 0 then | ||
| local other = xyz2pos(pos.x + dx, pos.y + dy, pos.z) | ||
| local occ = tile_occupancy(other) | ||
| if occ and not occ.unit and not occ.unit_grounded | ||
| and occ.building == df.tile_building_occ.None | ||
| and df.tiletype_shape.attrs[ | ||
| df.tiletype.attrs[ | ||
| dfhack.maps.getTileType(other)].shape].walkable then | ||
| return other | ||
| end | ||
| end | ||
| end end | ||
| return nil, 'no free tile near the shared tile' | ||
| end | ||
|
SilasD marked this conversation as resolved.
|
||
|
|
||
| -- recompute the unit occupancy flags of the given tiles from the given | ||
| -- units, so fabricated flag states do not leak into later tests | ||
| local function resync_occupancy(tiles, units) | ||
| for _, pos in ipairs(tiles) do | ||
| local occ = tile_occupancy(pos) | ||
| if occ then | ||
| occ.unit = false | ||
| occ.unit_grounded = false | ||
| end | ||
| end | ||
| for _, unit in ipairs(units) do | ||
| local occ = tile_occupancy(unit.pos) | ||
| if occ then | ||
| if unit.flags1.on_ground then | ||
| occ.unit_grounded = true | ||
| else | ||
| occ.unit = true | ||
| end | ||
|
Comment on lines
+46
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this works.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked this in-game: getTileFlags pushes the pointer returned by Maps::getTileOccupancy, which is &block->occupancy[x&15][y&15], so the Lua object is a live ref into the block (same address as getTileBlock(pos).occupancy[x%16][y%16]), and direct field writes do propagate. The mutation sites work as written; the only change needed was reading via getTileFlags, which I have now adopted.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I have always treated it as a copy, so I've been doing extra work. (I know the C++ version is a pointer.) static int maps_getTileFlags(lua_State *L)
{
auto pos = CheckCoordXYZ(L, 1, true);
Lua::PushDFObject(L, Maps::getTileDesignation(pos));
Lua::PushDFObject(L, Maps::getTileOccupancy(pos));
return 2;
}so that doesn't copy, it is live data, interesting. in contrast, it's been on my TODO list to add setters for these, but clearly |
||
| end | ||
|
SilasD marked this conversation as resolved.
|
||
| end | ||
| end | ||
|
|
||
| -- teleporting one of two grounded units off a shared tile must keep the | ||
| -- unit_grounded flag, since the other unit is still there (#5938) | ||
| function test.teleport_keeps_grounded_flag_with_other_grounded_unit() | ||
| local a, b = two_citizens() | ||
| expect.ne(nil, b, 'need at least two citizens') | ||
| if not a or not b then return end | ||
|
|
||
| local shared = copyall(b.pos) | ||
| local dest, err = free_tile_near(b.pos) | ||
| expect.ne(nil, dest, err) | ||
| if not dest then return end | ||
|
|
||
| local orig_a_pos, orig_a_ground = copyall(a.pos), a.flags1.on_ground | ||
| local orig_b_ground = b.flags1.on_ground | ||
|
|
||
| return dfhack.with_finalize(function() | ||
| dfhack.units.teleport(a, orig_a_pos) | ||
| dfhack.units.teleport(b, shared) | ||
| a.flags1.on_ground = orig_a_ground | ||
| b.flags1.on_ground = orig_b_ground | ||
| resync_occupancy({orig_a_pos, shared, dest}, {a, b}) | ||
| end, function() | ||
| -- teleporting onto a standing unit forces the mover to lie down | ||
| expect.true_(dfhack.units.teleport(a, shared)) | ||
| expect.true_(a.flags1.on_ground) | ||
| expect.true_(tile_occupancy(shared).unit_grounded) | ||
|
|
||
| -- fabricate a second grounded unit on the shared tile | ||
| b.flags1.on_ground = true | ||
|
|
||
| expect.true_(dfhack.units.teleport(a, dest)) | ||
| -- b is still grounded on the shared tile, so the flag must remain | ||
| expect.true_(tile_occupancy(shared).unit_grounded) | ||
| expect.true_(tile_occupancy(dest).unit_grounded) | ||
|
|
||
| -- removing the last grounded unit still clears the flag | ||
| expect.true_(dfhack.units.teleport(b, orig_a_pos)) | ||
| expect.false_(tile_occupancy(shared).unit_grounded) | ||
| end) | ||
| end | ||
|
|
||
| -- the same invariant applies to the standing 'unit' flag | ||
| function test.teleport_keeps_unit_flag_with_other_standing_unit() | ||
| local a, b = two_citizens() | ||
| expect.ne(nil, b, 'need at least two citizens') | ||
| if not a or not b then return end | ||
|
|
||
| local shared = copyall(b.pos) | ||
| local dest, err = free_tile_near(b.pos) | ||
| expect.ne(nil, dest, err) | ||
| if not dest then return end | ||
|
|
||
| local orig_a_pos, orig_a_ground = copyall(a.pos), a.flags1.on_ground | ||
| local orig_b_ground = b.flags1.on_ground | ||
|
|
||
| return dfhack.with_finalize(function() | ||
| dfhack.units.teleport(a, orig_a_pos) | ||
| dfhack.units.teleport(b, shared) | ||
| a.flags1.on_ground = orig_a_ground | ||
| b.flags1.on_ground = orig_b_ground | ||
| resync_occupancy({orig_a_pos, shared, dest}, {a, b}) | ||
| end, function() | ||
| expect.true_(dfhack.units.teleport(a, shared)) | ||
| expect.true_(a.flags1.on_ground) | ||
|
|
||
| -- make a stand again so both units are standing on the shared tile | ||
| a.flags1.on_ground = false | ||
| tile_occupancy(shared).unit_grounded = false | ||
|
|
||
| expect.true_(dfhack.units.teleport(a, dest)) | ||
| expect.true_(tile_occupancy(shared).unit) | ||
| expect.true_(tile_occupancy(dest).unit) | ||
|
|
||
| -- removing the last standing unit still clears the flag | ||
| expect.true_(dfhack.units.teleport(b, orig_a_pos)) | ||
| expect.false_(tile_occupancy(shared).unit) | ||
| end) | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.