-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_upgradebuilding.lua
More file actions
274 lines (250 loc) · 9.02 KB
/
base_upgradebuilding.lua
File metadata and controls
274 lines (250 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
--base_upgradebuilding.lua v1.0
--MUST BE LOADED IN DFHACK.INIT
--[[
upgradebuilding - used to upgrade buildings from one to another, or to change them from one to another
Requires a reaction an inorganic and corresponding buildings
REACTION OPTIONS:
All reagents with [PRESERVE_REAGENT] will be added to the building materials of the upgraded building and can be reclaimed by destroying the upgraded building
Reagents without the [PRESERVE_REAGENT] tag will be consumed and will not be able to be reclaimed
The first product must be the inorganic with the syndrome attached to it
Subsequent products will be created as normal
EXAMPLE REACTION:
[REACTION:LUA_HOOK_UPGRADE_BUILDING_EXAMPLE_1] <- LUA_HOOK_UPGRADE_BUILDING is required
[NAME:upgrade building]
[BUILDING:TEST_BUILDING_1:NONE]
[REAGENT:A:1:BOULDER:NONE:INORGANIC:NONE][PRESERVE_REAGENT]
[REAGENT:B:1:BOULDER:NONE:INORGANIC:NONE][PRESERVE_REAGENT]
[REAGENT:C:1500:COIN:NONE:INORGANIC:SILVER]
[PRODUCT:100:0:BOULDER:NONE:INORGANIC:UPGRADE_BUILDING]
INORGANIC OPTIONS:
Inorganics must have a syndrome with at least two [SYN_CLASS:] tags
Valid arguments for the first SYN_CLASS;
here - this will change this particular building
BUILDING_TOKEN - this will change a randomly selected building of the given token (e.g. TEST_BUILDING_1)
Valid arguments for the second SYN_CLASS;
upgrade - this will upgrade the building from the name to one higher (i.e. TEST_BUILDING_1 -> TEST_BUILDING_2)
downgrade - this will downgrade the building from the name to one lower (i.e. TEST_BUILDING_2 -> TEST_BUILDING_1)
BUILDING_TOKEN - this will change the building to a completely new token (i.e. TEST_BUILDING_1 -> RESEARCH_BUILDING)
(OPTIONAL) A third SYN_CLASS can be added to specify duration of the change. Defaults to permanent. Duration is in in-game ticks
EXAMPLE INORGANIC:
[INORGANIC:UPGRADE_BUILDING]
[USE_MATERIAL_TEMPLATE:STONE_VAPOR_TEMPLATE]
[SPECIAL]
[SYNDROME]
[SYN_CLASS:here]
[SYN_CLASS:upgrade]
[MATERIAL_VALUE:0]
BUILDING OPTIONS:
Nothing special is required from buildings except when using the upgrade/downgrade option
then the buildings you wish to change need to be named in the convention BLAH_BLAH_BLAH_1, BLAH_BLAH_BLAH_2, etc...
EXAMPLE BUILDINGS:
[BUILDING_WORKSHOP:TEST_BUILDING_1]
[NAME:Soul Forge]
[NAME_COLOR:7:0:1]
[DIM:3:3]
[WORK_LOCATION:1:1]
[BLOCK:1:0]
[TILE:0:1:207]
[COLOR:0:1:0:7:0]
[TILE:1:1:207]
[COLOR:1:1:MAT]
[BUILD_ITEM:1:NONE:NONE:NONE:NONE]
[BUILDMAT]
[WORTHLESS_STONE_ONLY]
[BUILDING_WORKSHOP:TEST_BUILDING_2]
[NAME:Upgraded Soul Forge]
[NAME_COLOR:7:0:1]
[DIM:3:3]
[WORK_LOCATION:1:1]
[BLOCK:1:0]
[TILE:0:1:207]
[COLOR:0:1:0:7:0]
[TILE:1:1:207]
[COLOR:1:1:MAT]
RESTRICTIONS!
Only upgrade between buildings of the same type (i.e. WORKSHOP->WORKSHOP)
Only upgrade to buildings of the same size (i.e. 3x3 -> 3x3)
Only upgrade to buildings with the same center and work spot (I don't know if this is strictly necessary, haven't done much testing)
Be careful about forbiden tiles in a building, the effect of changing these tiles without redefining the building is unknown
--]]
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
function createcallback(x,sid)
return function(resetbuilding)
x.custom_type = sid
end
end
function upgradebuilding(reaction,unit,input_items,input_reagents,output_items,call_native)
local sitems = {}
for i,x in ipairs(input_reagents) do
if x.flags.PRESERVE_REAGENT then sitems[i] = input_items[i] end
end
local pos = unit.pos
printall(input_reagents[1])
local ptype = reaction.products[0].mat_type
local pindx = reaction.products[0].mat_index
local product = dfhack.matinfo.decode(ptype,pindx)
local args = {}
for i,x in ipairs(product.material.syndrome[0].syn_class) do
args[i] = x.value
end
local dur = 0
if #args == 3 then dur = tonumber(args[2]) end
local bldg
if args[0] == 'here' then
-- Upgrade only the building that runs the reaction
bldg = dfhack.buildings.findAtTile(pos)
elseif args[0] == 'all' then
-- Upgrade all buildings of this type (doesn't currently work)
else
-- Upgrade random building of specific type
local bldga = {}
local i = 0
for _,x in ipairs(df.global.world.buildings.all) do
if df.building_furnacest:is_instance(x) or df.building_workshopst:is_instance(x) then
local ctype = x.custom_type
if ctype >= 0 then
if df.global.world.raws.buildings.all[ctype].code == args[0] then
bldga[i] = x
i = i+1
end
end
end
end
local rando = dfhack.random.new()
bldg = bldga[rando:random(#bldga)]
end
if args[1] == 'upgrade' then
-- Increase buildings number by one
local name = df.global.world.raws.buildings.all[bldg.custom_type].code
if dur > 0 then sid = bldg.custom_type end
local namea = split(name,'_')
local num = tonumber(namea[#namea])
num = num + 1
namea[#namea] = tostring(num)
name = table.concat(namea,'_')
local ctype = nil
for _,x in ipairs(df.global.world.raws.buildings.all) do
if x.code == name then ctype = x.id end
end
if ctype == nil then
print('Cant find upgrade building, possibly upgraded to max')
return
end
bldg.custom_type=ctype
for _,x in pairs(bldg.contained_items) do
for _,y in pairs(sitems) do
if x.item == y then
x.use_mode = 2
x.item.flags.in_building = true
end
end
end
if dur > 0 then dfhack.timeout(dur,'ticks',createcallback(bldg,sid)) end
elseif args[1] == 'downgrade' then
-- Decrease buildings number by one
local name = df.global.world.raws.buildings.all[bldg.custom_type].code
if dur > 0 then sid = bldg.custom_type end
local namea = split(name,'_')
local num = tonumber(namea[#namea])
num = num - 1
if num > 0 then namea[#namea] = tostring(num) end
name = table.concat(namea,'_')
local ctype = nil
for _,x in ipairs(df.global.world.raws.buildings.all) do
if x.code == name then ctype = x.id end
end
if ctype == nil then
print('Cant find upgrade building, possibly upgraded to max')
return
end
bldg.custom_type=ctype
for _,x in pairs(bldg.contained_items) do
for _,y in pairs(sitems) do
if x.item == y then
x.use_mode = 2
x.item.flags.in_building = true
end
end
end
if dur > 0 then dfhack.timeout(dur,'ticks',createcallback(bldg,sid)) end
else
-- Change building to new building
if dur > 0 then sid = bldg.custom_type end
local name = args[1]
local ctype = nil
for _,x in ipairs(df.global.world.raws.buildings.all) do
if x.code == name then ctype = x.id end
end
if ctype == nil then
print('Cant find upgrade building, possibly upgraded to max')
return
end
bldg.custom_type=ctype
for _,x in pairs(bldg.contained_items) do
for _,y in pairs(sitems) do
if x.item == y then
x.use_mode = 2
x.item.flags.in_building = true
end
end
end
if dur > 0 then dfhack.timeout(dur,'ticks',createcallback(bldg,sid)) end
end
end
-- START Taken from hire-guard.lua
local eventful = require 'plugins.eventful'
local utils = require 'utils'
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
dfhack.onStateChange.loadUpgradeBuilding = function(code)
local registered_reactions
if code==SC_MAP_LOADED then
--registered_reactions = {}
for i,reaction in ipairs(df.global.world.raws.reactions) do
-- register each applicable reaction (to avoid doing string check
-- for every lua hook reaction (not just ours), this way uses identity check
if string.starts(reaction.code,'LUA_HOOK_UPGRADE_BUILDING') then
-- register reaction.code
eventful.registerReaction(reaction.code,upgradebuilding)
-- save reaction.code
--table.insert(registered_reactions,reaction.code)
registered_reactions = true
end
end
--if #registered_reactions > 0 then print('HireGuard: Loaded') end
if registered_reactions then print('Upgradable Buildings: Loaded') end
elseif code==SC_MAP_UNLOADED then
--[[ doesn't seem to be working, and probably not needed
registered_reactions = registered_reactions or {}
if #registered_reactions > 0 then print('HireGuard: Unloaded') end
for i,reaction in ipairs(registered_reactions) do
-- un register each registered reaction (to prevent persistance between
-- differing worlds (probably irrelavant, but doesn't hurt)
-- un register reaction.code
eventful.registerReaction(reaction.code,nil)
end
registered_reactions = nil -- clear registered_reactions
--]]
end
end
-- if dfhack.init has already been run, force it to think SC_WORLD_LOADED to that reactions get refreshed
if dfhack.isMapLoaded() then dfhack.onStateChange.loadUpgradeBuilding(SC_MAP_LOADED) end
-- END Taken from hire-guard.lua