Skip to content

Commit 4de3d7e

Browse files
bodyswap: minor adjustments
slightly modified documentation, added error message and relocated a few lines
1 parent 0b6e4a5 commit 4de3d7e

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

bodyswap.lua

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ local usage = [====[
1616
1717
bodyswap
1818
========
19-
This script allows the player to gain control over a new unit in adventurer mode
20-
whilst simultaneously loosing control over their current character.
19+
This script allows the player to take direct control of any unit present in
20+
adventure mode whilst losing control of their current adventurer.
2121
2222
To specify the target unit, simply select it in the user interface,
23-
such as by opening the unit's status screen or viewing its description
23+
such as by opening the unit's status screen or viewing its description,
2424
and enter "bodyswap" in the DFHack console.
2525
2626
Alternatively, the target unit can be specified by its unit id as shown below.
@@ -51,9 +51,6 @@ end
5151
function setOldAdvNemFlags(nem)
5252
nem.flags.ACTIVE_ADVENTURER = false
5353
nem.flags.RETIRED_ADVENTURER = true
54-
nem.unit.idle_area.x = nem.unit.pos.x
55-
nem.unit.idle_area.y = nem.unit.pos.y
56-
nem.unit.idle_area.z = nem.unit.pos.z
5754
end
5855

5956
function clearNemesisFromLinkedSites(nem)
@@ -84,7 +81,8 @@ function swapAdvUnit(newUnit)
8481
qerror('Target unit not specified!')
8582
end
8683

87-
local oldUnit = df.nemesis_record.find(df.global.ui_advmode.player_id).unit
84+
local oldNem = df.nemesis_record.find(df.global.ui_advmode.player_id)
85+
local oldUnit = oldNem.unit
8886
if newUnit == oldUnit then
8987
return
9088
end
@@ -113,19 +111,18 @@ function swapAdvUnit(newUnit)
113111
qerror("Target unit index not found!")
114112
end
115113

116-
activeUnits[newUnitIndex] = oldUnit
117-
activeUnits[oldUnitIndex] = newUnit
118-
119114
local newNem = dfhack.units.getNemesis(newUnit) or createNemesis(newUnit)
120-
if newNem then
121-
local oldNem = dfhack.units.getNemesis(oldUnit)
122-
if oldNem then
123-
setOldAdvNemFlags(oldNem)
124-
end
125-
setNewAdvNemFlags(newNem)
126-
clearNemesisFromLinkedSites(newNem)
127-
df.global.ui_advmode.player_id = newNem.id
115+
if not newNem then
116+
qerror("Failed to obtain target nemesis!")
128117
end
118+
119+
setOldAdvNemFlags(oldNem)
120+
setNewAdvNemFlags(newNem)
121+
clearNemesisFromLinkedSites(newNem)
122+
df.global.ui_advmode.player_id = newNem.id
123+
activeUnits[newUnitIndex] = oldUnit
124+
activeUnits[oldUnitIndex] = newUnit
125+
oldUnit.idle_area:assign(oldUnit.pos)
129126
end
130127

131128
if not dfhack_flags.module then

0 commit comments

Comments
 (0)