|
3 | 3 |
|
4 | 4 | local utils = require('utils')
|
5 | 5 |
|
| 6 | +local nobles = reqscript('internal/emigration/emigrate-nobles') |
| 7 | +local unit_link_utils = reqscript('internal/emigration/unit-link-utils') |
| 8 | + |
6 | 9 | local GLOBAL_KEY = 'emigration' -- used for state change hooks and persistence
|
7 | 10 |
|
8 | 11 | local function get_default_state()
|
@@ -37,121 +40,40 @@ function desert(u,method,civ)
|
37 | 40 | local line = dfhack.units.getReadableName(u) .. " has "
|
38 | 41 | if method == 'merchant' then
|
39 | 42 | line = line.."joined the merchants"
|
40 |
| - u.flags1.merchant = true |
41 |
| - u.civ_id = civ |
| 43 | + unit_link_utils.markUnitForEmigration(u, civ, false) |
42 | 44 | else
|
43 | 45 | line = line.."abandoned the settlement in search of a better life."
|
44 |
| - u.civ_id = civ |
45 |
| - u.flags1.forest = true |
46 |
| - u.flags2.visitor = true |
47 |
| - u.animal.leave_countdown = 2 |
| 46 | + unit_link_utils.markUnitForEmigration(u, civ, true) |
48 | 47 | end
|
49 |
| - local hf_id = u.hist_figure_id |
| 48 | + |
50 | 49 | local hf = df.historical_figure.find(u.hist_figure_id)
|
51 | 50 | local fort_ent = df.global.plotinfo.main.fortress_entity
|
52 | 51 | local civ_ent = df.historical_entity.find(hf.civ_id)
|
53 | 52 | local newent_id = -1
|
54 | 53 | local newsite_id = -1
|
55 | 54 |
|
56 |
| - -- free owned rooms |
57 |
| - for i = #u.owned_buildings-1, 0, -1 do |
58 |
| - local temp_bld = df.building.find(u.owned_buildings[i].id) |
59 |
| - dfhack.buildings.setOwner(temp_bld, nil) |
60 |
| - end |
61 |
| - |
62 |
| - -- remove from workshop profiles |
63 |
| - for _, bld in ipairs(df.global.world.buildings.other.WORKSHOP_ANY) do |
64 |
| - for k, v in ipairs(bld.profile.permitted_workers) do |
65 |
| - if v == u.id then |
66 |
| - bld.profile.permitted_workers:erase(k) |
67 |
| - break |
68 |
| - end |
69 |
| - end |
70 |
| - end |
71 |
| - for _, bld in ipairs(df.global.world.buildings.other.FURNACE_ANY) do |
72 |
| - for k, v in ipairs(bld.profile.permitted_workers) do |
73 |
| - if v == u.id then |
74 |
| - bld.profile.permitted_workers:erase(k) |
75 |
| - break |
76 |
| - end |
77 |
| - end |
78 |
| - end |
79 |
| - |
80 |
| - -- disassociate from work details |
81 |
| - for _, detail in ipairs(df.global.plotinfo.labor_info.work_details) do |
82 |
| - for k, v in ipairs(detail.assigned_units) do |
83 |
| - if v == u.id then |
84 |
| - detail.assigned_units:erase(k) |
85 |
| - break |
86 |
| - end |
87 |
| - end |
88 |
| - end |
89 |
| - |
90 |
| - -- unburrow |
91 |
| - for _, burrow in ipairs(df.global.plotinfo.burrows.list) do |
92 |
| - dfhack.burrows.setAssignedUnit(burrow, u, false) |
93 |
| - end |
94 |
| - |
95 |
| - -- erase the unit from the fortress entity |
96 |
| - for k,v in ipairs(fort_ent.histfig_ids) do |
97 |
| - if v == hf_id then |
98 |
| - df.global.plotinfo.main.fortress_entity.histfig_ids:erase(k) |
99 |
| - break |
100 |
| - end |
101 |
| - end |
102 |
| - for k,v in ipairs(fort_ent.hist_figures) do |
103 |
| - if v.id == hf_id then |
104 |
| - df.global.plotinfo.main.fortress_entity.hist_figures:erase(k) |
105 |
| - break |
106 |
| - end |
107 |
| - end |
108 |
| - for k,v in ipairs(fort_ent.nemesis) do |
109 |
| - if v.figure.id == hf_id then |
110 |
| - df.global.plotinfo.main.fortress_entity.nemesis:erase(k) |
111 |
| - df.global.plotinfo.main.fortress_entity.nemesis_ids:erase(k) |
112 |
| - break |
113 |
| - end |
114 |
| - end |
115 |
| - |
116 |
| - -- remove the old entity link and create new one to indicate former membership |
117 |
| - hf.entity_links:insert("#", {new = df.histfig_entity_link_former_memberst, entity_id = fort_ent.id, link_strength = 100}) |
118 |
| - for k,v in ipairs(hf.entity_links) do |
119 |
| - if v._type == df.histfig_entity_link_memberst and v.entity_id == fort_ent.id then |
120 |
| - hf.entity_links:erase(k) |
121 |
| - break |
122 |
| - end |
123 |
| - end |
| 55 | + unit_link_utils.removeUnitAssociations(u) |
| 56 | + unit_link_utils.removeHistFigFromEntity(hf, fort_ent) |
124 | 57 |
|
125 | 58 | -- try to find a new entity for the unit to join
|
126 |
| - for k,v in ipairs(civ_ent.entity_links) do |
127 |
| - if v.type == df.entity_entity_link_type.CHILD and v.target ~= fort_ent.id then |
128 |
| - newent_id = v.target |
| 59 | + for _,entity_link in ipairs(civ_ent.entity_links) do |
| 60 | + if entity_link.type == df.entity_entity_link_type.CHILD and entity_link.target ~= fort_ent.id then |
| 61 | + newent_id = entity_link.target |
129 | 62 | break
|
130 | 63 | end
|
131 | 64 | end
|
132 | 65 |
|
133 | 66 | if newent_id > -1 then
|
134 |
| - hf.entity_links:insert("#", {new = df.histfig_entity_link_memberst, entity_id = newent_id, link_strength = 100}) |
135 |
| - |
136 | 67 | -- try to find a new site for the unit to join
|
137 |
| - for k,v in ipairs(df.global.world.entities.all[hf.civ_id].site_links) do |
| 68 | + for _,site_link in ipairs(df.global.world.entities.all[hf.civ_id].site_links) do |
138 | 69 | local site_id = df.global.plotinfo.site_id
|
139 |
| - if v.type == df.entity_site_link_type.Claim and v.target ~= site_id then |
140 |
| - newsite_id = v.target |
| 70 | + if site_link.type == df.entity_site_link_type.Claim and site_link.target ~= site_id then |
| 71 | + newsite_id = site_link.target |
141 | 72 | break
|
142 | 73 | end
|
143 | 74 | end
|
144 | 75 | local newent = df.historical_entity.find(newent_id)
|
145 |
| - newent.histfig_ids:insert('#', hf_id) |
146 |
| - newent.hist_figures:insert('#', hf) |
147 |
| - local hf_event_id = df.global.hist_event_next_id |
148 |
| - df.global.hist_event_next_id = df.global.hist_event_next_id+1 |
149 |
| - df.global.world.history.events:insert("#", {new = df.history_event_add_hf_entity_linkst, year = df.global.cur_year, seconds = df.global.cur_year_tick, id = hf_event_id, civ = newent_id, histfig = hf_id, link_type = 0}) |
150 |
| - if newsite_id > -1 then |
151 |
| - local hf_event_id = df.global.hist_event_next_id |
152 |
| - df.global.hist_event_next_id = df.global.hist_event_next_id+1 |
153 |
| - df.global.world.history.events:insert("#", {new = df.history_event_change_hf_statest, year = df.global.cur_year, seconds = df.global.cur_year_tick, id = hf_event_id, hfid = hf_id, state = 1, reason = -1, site = newsite_id}) |
154 |
| - end |
| 76 | + unit_link_utils.addHistFigToSite(hf, newsite_id, newent) |
155 | 77 | end
|
156 | 78 | print(dfhack.df2console(line))
|
157 | 79 | dfhack.gui.showAnnouncement(line, COLOR_WHITE)
|
@@ -251,6 +173,9 @@ if args[1] == "enable" then
|
251 | 173 | state.enabled = true
|
252 | 174 | elseif args[1] == "disable" then
|
253 | 175 | state.enabled = false
|
| 176 | +elseif args[1] == "nobles" then |
| 177 | + table.remove(args, 1) |
| 178 | + nobles.run(args) |
254 | 179 | else
|
255 | 180 | print('emigration is ' .. (state.enabled and 'enabled' or 'not enabled'))
|
256 | 181 | return
|
|
0 commit comments