Repository navigation
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
Merged
ab9rf
merged 2 commits into
DFHack:develop
from
Alistair-Afton:fix-teleport-grounded-flag
Sep 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
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.
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 | ||
|
SilasD marked this conversation as resolved.
Resolved
|
||
| end | ||
|
SilasD marked this conversation as resolved.
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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.