@@ -16,11 +16,11 @@ local usage = [====[
1616
1717bodyswap
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
2222To 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,
2424and enter "bodyswap" in the DFHack console.
2525
2626Alternatively, the target unit can be specified by its unit id as shown below.
5151function 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
5754end
5855
5956function 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 )
129126end
130127
131128if not dfhack_flags .module then
0 commit comments