Skip to content

Commit 3d9d903

Browse files
Add emigration script
1 parent 28321ef commit 3d9d903

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

emigration.lua

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
--Allow stressed dwarves to emigrate from the fortress
2+
-- For 34.11 by IndigoFenix; update and cleanup by PeridexisErrant
3+
-- old version: http://dffd.bay12games.com/file.php?id=8404
4+
--[[=begin
5+
6+
emigration
7+
==========
8+
Allows dwarves to emigrate from the fortress when stressed,
9+
in proportion to how badly stressed they are and adjusted
10+
for who they would have to leave with - a dwarven merchant
11+
being more attractive than leaving alone (or with an elf).
12+
The check is made monthly.
13+
14+
A happy dwarf (ie with negative stress) will never emigrate.
15+
16+
Usage: ``emigration enable|disable``
17+
18+
=end]]
19+
20+
local args = {...}
21+
if args[1] == "enable" then
22+
enabled = true
23+
elseif args[1] == "disable" then
24+
enabled = false
25+
end
26+
27+
function desireToStay(unit,method,civ_id)
28+
-- on a percentage scale
29+
value = 100 - unit.status.current_soul.personality.stress_level / 5000
30+
if method == 'merchant' or method == 'diplomat' then
31+
if civ_id ~= unit.civ_id then value = value*2 end end
32+
if method == 'wild' then
33+
value = value*5 end
34+
return value
35+
end
36+
37+
function desert(u,method,civ)
38+
u.relations.following = nil
39+
local line = dfhack.TranslateName(dfhack.units.getVisibleName(u)) .. " has "
40+
if method == 'merchant' then
41+
line = line.."joined the merchants"
42+
u.flags1.merchant = true
43+
u.civ_id = civ
44+
elseif method == 'diplomat' then
45+
line = line.."followed the diplomat"
46+
u.flags1.diplomat = true
47+
u.civ_id = civ
48+
else
49+
line = line.."abandoned the settlement in search of a better life."
50+
u.civ_id = -1
51+
u.flags1.forest = true
52+
u.animal.leave_countdown = 2
53+
end
54+
print(line)
55+
dfhack.gui.showAnnouncement(line, COLOR_WHITE)
56+
end
57+
58+
function canLeave(unit)
59+
for _, skill in pairs(unit.status.current_soul.skills) do
60+
if skill.rating > 14 then return false end
61+
end
62+
if unit.flags1.caged
63+
or u.race ~= df.global.ui.race_id
64+
or u.civ_id ~= df.global.ui.civ_id
65+
or dfhack.units.isDead(u)
66+
or dfhack.units.isOpposedToLife(u)
67+
or u.flags1.merchant
68+
or u.flags1.diplomat
69+
or unit.flags1.chained
70+
or dfhack.units.getNoblePositions(unit) ~= nil
71+
or unit.military.squad_id ~= -1
72+
or dfhack.units.isCitizen(unit)
73+
or dfhack.units.isSane(unit)
74+
or unit.profession ~= 103
75+
or not dfhack.units.isDead(unit)
76+
then return false end
77+
return true
78+
end
79+
80+
function checkForDeserters(method,civ_id)
81+
local allUnits = df.global.world.units.active
82+
for i=#allUnits-1,0,-1 do -- search list in reverse
83+
local u = allUnits[i]
84+
if canLeave(u) and math.random(100) < desireToStay(u,method,civ_id) then
85+
desert(u,method,civ_id)
86+
end
87+
end
88+
end
89+
90+
function checkmigrationnow()
91+
local merchant_civ_ids = {}
92+
local diplomat_civ_ids = {}
93+
local allUnits = df.global.world.units.active
94+
for i=0, #allUnits-1 do
95+
local unit = allUnits[i]
96+
if dfhack.units.isSane(unit)
97+
and not dfhack.units.isDead(unit)
98+
and not dfhack.units.isOpposedToLife(unit)
99+
and not unit.flags1.tame
100+
then
101+
if unit.flags1.merchant then table.insert(merchant_civ_ids, unit.civ_id) end
102+
if unit.flags1.diplomat then table.insert(diplomat_civ_ids, unit.civ_id) end
103+
end
104+
end
105+
106+
for _, civ_id in pairs(merchant_civ_ids) do checkForDeserters('merchant', civ_id) end
107+
for _, civ_id in pairs(diplomat_civ_ids) do checkForDeserters('diplomat', civ_id) end
108+
checkForDeserters('wild', -1)
109+
end
110+
111+
local function event_loop()
112+
checkmigrationnow()
113+
dfhack.timeout(1, 'months', event_loop)
114+
end
115+
116+
dfhack.onStateChange.loadEmigration = function(code)
117+
if code==SC_MAP_LOADED then
118+
if enabled then
119+
print("Emigration enabled.")
120+
event_loop()
121+
else
122+
print("Emigration disabled.")
123+
end
124+
end
125+
end
126+
127+
if dfhack.isMapLoaded() then
128+
dfhack.onStateChange.loadEmigration(SC_MAP_LOADED)
129+
end
130+

0 commit comments

Comments
 (0)