Skip to content

Commit 0b6e4a5

Browse files
committed
bodyswap: Use unix line endings
Ref DFHack#55
1 parent bce5c0b commit 0b6e4a5

1 file changed

Lines changed: 142 additions & 142 deletions

File tree

bodyswap.lua

Lines changed: 142 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,142 @@
1-
-- Shifts player control over to another unit in adventure mode.
2-
-- author: Atomic Chicken
3-
-- based on "assumecontrol.lua" by maxthyme, as well as the defunct advtools plugin "adv-bodyswap"
4-
-- calls "modtools/create-unit" for nemesis and histfig creation
5-
6-
--@ module = true
7-
8-
local utils = require 'utils'
9-
validArgs = validArgs or utils.invert({
10-
'unit',
11-
'help'
12-
})
13-
local args = utils.processArgs({...}, validArgs)
14-
15-
local usage = [====[
16-
17-
bodyswap
18-
========
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.
21-
22-
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
24-
and enter "bodyswap" in the DFHack console.
25-
26-
Alternatively, the target unit can be specified by its unit id as shown below.
27-
28-
Arguments::
29-
30-
-unit id
31-
replace "id" with the unit id of your target
32-
example:
33-
bodyswap -unit 42
34-
35-
]====]
36-
37-
if args.help then
38-
print(usage)
39-
return
40-
end
41-
42-
if not dfhack.world.isAdventureMode() then
43-
qerror("This script can only be used in adventure mode!")
44-
end
45-
46-
function setNewAdvNemFlags(nem)
47-
nem.flags.ACTIVE_ADVENTURER = true
48-
nem.flags.RETIRED_ADVENTURER = false
49-
nem.flags.ADVENTURER = true
50-
end
51-
function setOldAdvNemFlags(nem)
52-
nem.flags.ACTIVE_ADVENTURER = false
53-
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
57-
end
58-
59-
function clearNemesisFromLinkedSites(nem)
60-
-- this is a workaround for a bug which tends to cause duplication of the unit entry in df.global.world.units.active when the site to which a historical figure is linked is reloaded with the unit present
61-
-- appears to fix the problem without causing any noticeable issues
62-
if not nem.figure then
63-
return
64-
end
65-
for _,link in ipairs(nem.figure.site_links) do
66-
local site = df.world_site.find(link.site)
67-
for i = #site.unk_1.nemesis-1,0,-1 do
68-
if site.unk_1.nemesis[i] == nem.id then
69-
site.unk_1.nemesis:erase(i)
70-
end
71-
end
72-
end
73-
end
74-
75-
function createNemesis(unit)
76-
local nemesis = reqscript('modtools/create-unit').createNemesis(unit,unit.civ_id)
77-
nemesis.figure.flags.never_cull = true
78-
return nemesis
79-
end
80-
81-
function swapAdvUnit(newUnit)
82-
83-
if not newUnit then
84-
qerror('Target unit not specified!')
85-
end
86-
87-
local oldUnit = df.nemesis_record.find(df.global.ui_advmode.player_id).unit
88-
if newUnit == oldUnit then
89-
return
90-
end
91-
92-
local activeUnits = df.global.world.units.active
93-
local oldUnitIndex
94-
if activeUnits[0] == oldUnit then
95-
oldUnitIndex = 0
96-
else -- unlikely; this is just in case
97-
for i,u in pairs(activeUnits) do
98-
if u == oldUnit then
99-
oldUnitIndex = i
100-
break
101-
end
102-
end
103-
end
104-
local newUnitIndex
105-
for i,u in pairs(activeUnits) do
106-
if u == newUnit then
107-
newUnitIndex = i
108-
break
109-
end
110-
end
111-
112-
if not newUnitIndex then
113-
qerror("Target unit index not found!")
114-
end
115-
116-
activeUnits[newUnitIndex] = oldUnit
117-
activeUnits[oldUnitIndex] = newUnit
118-
119-
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
128-
end
129-
end
130-
131-
if not dfhack_flags.module then
132-
local unit = args.unit and df.unit.find(tonumber(args.unit)) or dfhack.gui.getSelectedUnit()
133-
if not unit then
134-
print("Enter the following if you require assistance: bodyswap -help")
135-
if args.unit then
136-
qerror("Invalid unit id: "..args.unit)
137-
else
138-
qerror("Target unit not specified!")
139-
end
140-
end
141-
swapAdvUnit(unit)
142-
end
1+
-- Shifts player control over to another unit in adventure mode.
2+
-- author: Atomic Chicken
3+
-- based on "assumecontrol.lua" by maxthyme, as well as the defunct advtools plugin "adv-bodyswap"
4+
-- calls "modtools/create-unit" for nemesis and histfig creation
5+
6+
--@ module = true
7+
8+
local utils = require 'utils'
9+
validArgs = validArgs or utils.invert({
10+
'unit',
11+
'help'
12+
})
13+
local args = utils.processArgs({...}, validArgs)
14+
15+
local usage = [====[
16+
17+
bodyswap
18+
========
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.
21+
22+
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
24+
and enter "bodyswap" in the DFHack console.
25+
26+
Alternatively, the target unit can be specified by its unit id as shown below.
27+
28+
Arguments::
29+
30+
-unit id
31+
replace "id" with the unit id of your target
32+
example:
33+
bodyswap -unit 42
34+
35+
]====]
36+
37+
if args.help then
38+
print(usage)
39+
return
40+
end
41+
42+
if not dfhack.world.isAdventureMode() then
43+
qerror("This script can only be used in adventure mode!")
44+
end
45+
46+
function setNewAdvNemFlags(nem)
47+
nem.flags.ACTIVE_ADVENTURER = true
48+
nem.flags.RETIRED_ADVENTURER = false
49+
nem.flags.ADVENTURER = true
50+
end
51+
function setOldAdvNemFlags(nem)
52+
nem.flags.ACTIVE_ADVENTURER = false
53+
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
57+
end
58+
59+
function clearNemesisFromLinkedSites(nem)
60+
-- this is a workaround for a bug which tends to cause duplication of the unit entry in df.global.world.units.active when the site to which a historical figure is linked is reloaded with the unit present
61+
-- appears to fix the problem without causing any noticeable issues
62+
if not nem.figure then
63+
return
64+
end
65+
for _,link in ipairs(nem.figure.site_links) do
66+
local site = df.world_site.find(link.site)
67+
for i = #site.unk_1.nemesis-1,0,-1 do
68+
if site.unk_1.nemesis[i] == nem.id then
69+
site.unk_1.nemesis:erase(i)
70+
end
71+
end
72+
end
73+
end
74+
75+
function createNemesis(unit)
76+
local nemesis = reqscript('modtools/create-unit').createNemesis(unit,unit.civ_id)
77+
nemesis.figure.flags.never_cull = true
78+
return nemesis
79+
end
80+
81+
function swapAdvUnit(newUnit)
82+
83+
if not newUnit then
84+
qerror('Target unit not specified!')
85+
end
86+
87+
local oldUnit = df.nemesis_record.find(df.global.ui_advmode.player_id).unit
88+
if newUnit == oldUnit then
89+
return
90+
end
91+
92+
local activeUnits = df.global.world.units.active
93+
local oldUnitIndex
94+
if activeUnits[0] == oldUnit then
95+
oldUnitIndex = 0
96+
else -- unlikely; this is just in case
97+
for i,u in pairs(activeUnits) do
98+
if u == oldUnit then
99+
oldUnitIndex = i
100+
break
101+
end
102+
end
103+
end
104+
local newUnitIndex
105+
for i,u in pairs(activeUnits) do
106+
if u == newUnit then
107+
newUnitIndex = i
108+
break
109+
end
110+
end
111+
112+
if not newUnitIndex then
113+
qerror("Target unit index not found!")
114+
end
115+
116+
activeUnits[newUnitIndex] = oldUnit
117+
activeUnits[oldUnitIndex] = newUnit
118+
119+
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
128+
end
129+
end
130+
131+
if not dfhack_flags.module then
132+
local unit = args.unit and df.unit.find(tonumber(args.unit)) or dfhack.gui.getSelectedUnit()
133+
if not unit then
134+
print("Enter the following if you require assistance: bodyswap -help")
135+
if args.unit then
136+
qerror("Invalid unit id: "..args.unit)
137+
else
138+
qerror("Target unit not specified!")
139+
end
140+
end
141+
swapAdvUnit(unit)
142+
end

0 commit comments

Comments
 (0)