forked from Ekdohibs/itest
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomponents.lua
More file actions
350 lines (332 loc) · 12 KB
/
components.lua
File metadata and controls
350 lines (332 loc) · 12 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
components = {}
voltbuild.metadata_check.components = function (pos,listname,stack,maxtier)
local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
if get_item_field(stack:get_name(),"component") == 1 then
if stack:peek_item()["on_placement"] then
stack:peek_item()["on_placement"](pos)
end
return 1
end
return 0
end
voltbuild.metadata_check_move.components = function (pos,to_list,stack,maxtier,from_list,from_index,to_index,count,player)
local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
if get_item_field(stack:get_name(),"component") == 1 then
if to_list ~= "components" then
if stack:peek_item()["on_removal"] then
stack:peek_item()["on_removal"](pos)
end
end
return 1
end
return 0
end
function components.each_with_method(component_inv,method_name)
local ret_comps = {}
for i=1,component_inv:get_size("components") do
component_stack = component_inv:get_stack("components",i)
if not component_stack:is_empty() then
local ret_comp = component_stack:peek_item():get_definition()["voltbuild"]
if ret_comp and ret_comp[method_name] then
table.insert(ret_comps,ret_comp)
end
end
end
return ret_comps
end
function components.abm_wrapper(pos,node,active_object_count,active_object_count_wider,abm)
local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
local run_abm = true
for i,comp in ipairs(components.each_with_method(inv,"can_run")) do
if not comp.can_run(pos) then
run_abm = false
break
end
end
for i,comp in ipairs(components.each_with_method(inv,"before_effects")) do
comp.before_effects(pos)
end
if run_abm then
for i,comp in ipairs(components.each_with_method(inv,"run_before_effects")) do
comp.run_before_effects(pos)
end
abm(pos,node,active_object_count,active_object_count_wider)
for i,comp in ipairs(components.each_with_method(inv,"run_after_effects")) do
comp.run_after_effects(pos)
end
else
for i,comp in ipairs(components.each_with_method(inv,"not_run_effects")) do
comp.not_run_effects(pos)
end
end
for i,comp in ipairs(components.each_with_method(inv,"after_effects")) do
comp.after_effects(pos)
end
local stress = meta:get_int("stress")
local max_stress = minetest.registered_nodes[node.name]["voltbuild"]["max_stress"]
if stress >= max_stress then
voltbuild.blast(pos)
end
end
if voltbuild.upgrade then
function components.register_abm(table)
local register_action = table.action
table.action = function (pos,node,active_object_count,active_object_count_wider)
local meta = minetest.env:get_meta(pos)
if meta:get_string("energy") ~= "" then
local max_energy
local current_max = meta:get_int("max_energy")
if minetest.registered_nodes[node.name]["voltbuild"] then
max_energy = minetest.registered_nodes[node.name]["voltbuild"]["max_energy"]
end
if max_energy and max_energy < meta:get_int("energy") then
meta:set_int("energy",math.min(meta:get_int("energy"),max_energy))
end
if current_max ~= 0 and current_max > max_energy then
meta:set_string("max_energy","")
end
if meta:get_string("max_psize") ~= "" then
meta:set_string("max_psize","")
end
end
components.abm_wrapper(pos,node,active_object_count,active_object_count_wider,register_action)
end
minetest.register_abm(table)
end
else
function components.register_abm(table)
local register_action = table.action
table.action = function (pos,node,active_object_count,active_object_count_wider)
components.abm_wrapper(pos,node,active_object_count,active_object_count_wider,register_action)
end
minetest.register_abm(table)
end
end
function components.register_clockitem(name, properties)
properties.voltbuild.run_before_effects = function(pos)
local node = minetest.get_node(pos)
local meta = minetest.env:get_meta(pos)
local active = string.find(node.name,"_active") or
minetest.registered_nodes[node.name]["voltbuild"]["active"]
if active == nil then
if meta:get_string("active") ~= "" then
active = meta:get_int("active")
end
end
if active and active ~= 0 then
local speed = minetest.registered_nodes[node.name]["voltbuild"]["speed"] or 1.0
local operation_time = minetest.registered_nodes[node.name]["voltbuild"]["optime"]
local fuel_time = minetest.registered_nodes[node.name]["voltbuild"]["fueltime"]
local clock_speed_effect = properties.voltbuild.clock_speed_effect
local clock_optime_effect = properties.voltbuild.clock_optime_effect
local clock_fueltime_effect = properties.voltbuild.clock_fueltime_effect
if type(speed)== "function" then
speed = clock_speed_effect(speed(pos))
else
speed = clock_speed_effect(speed)
end
if operation_time then
if clock_optime_effect then
if type(operation_time)== "function" then
if meta:get_string("optime_prev") ~= "" then
operation_time = clock_optime_effect(meta:get_float("optime"))
if operation_time then
meta:set_float("optime_prev",meta:get_float("optime"))
meta:set_float("optime",operation_time)
end
else
operation_time = clock_optime_effect(operation_time(pos))
if operation_time then
meta:set_float("optime_prev",-1.0)
meta:set_float("optime",operation_time)
end
end
else
if meta:get_string("optime_prev") ~= "" then
operation_time = clock_optime_effect(meta:get_float("optime"))
meta:set_float("optime_prev",meta:get_float("optime"))
else
operation_time = clock_optime_effect(operation_time)
meta:set_float("optime_prev",-1.0)
end
meta:set_float("optime",operation_time)
end
end
end
if fuel_time then
if clock_fueltime_effect then
if type(fuel_time)== "function" then
if meta:get_string("fueltime") ~= "" then
fuel_time = clock_fueltime_effect(meta:get_float("fueltime"))
meta:set_float("ftime_prev",meta:get_float("fueltime"))
else
fuel_time = clock_fueltime_effect(fuel_time(pos))
meta:set_float("ftime_prev",-1.0)
end
else
if meta:get_string("ftime_prev") ~= "" then
fuel_time = clock_fueltime_effect(meta:get_float("fueltime"))
meta:set_float("ftime_prev",meta:get_float("fueltime"))
else
fuel_time = clock_fueltime_effect(fuel_time)
meta:set_float("ftime_prev",-1.0)
end
end
meta:set_float("fueltime",fuel_time)
end
end
local stress = meta:get_int("stress")
local stress_cost_effect = properties.voltbuild.stress_cost_effect
meta:set_int("stress",stress_cost_effect(stress))
if meta:get_string("stime") ~= "" then
local stime = meta:get_float("stime")
meta:set_float("stime",stime+speed)
end
end
end
properties.voltbuild.run_after_effects = function(pos)
local meta = minetest.env:get_meta(pos)
local clock_optime_effect = properties.voltbuild.clock_optime_effect
if clock_optime_effect then
if meta:get_string("optime") ~= "" then
meta:set_string("optime","")
meta:set_string("optime_prev","")
end
if meta:get_string("fueltime") ~= "" then
meta:set_string("fueltime","")
meta:set_string("ftime_prev","")
end
end
end
minetest.register_craftitem(name,properties)
end
components.register_clockitem("voltbuild:overclock", {
description = "Overclock",
inventory_image = "voltbuild_overclock.png",
voltbuild = {component=1,
stress_cost_effect = function(stress)
return stress+20
end,
clock_speed_effect = function (x)
return x
end},
documentation = {summary="Speeds up a machine causing the machine to slowly build up stress."}
})
minetest.register_craft({
output = "voltbuild:overclock",
recipe = {{"default:bronze_ingot","voltbuild:hv_cable0_000000","default:bronze_ingot"},
{"voltbuild;energy_crystal","voltbuild:circuit","voltbuild:energy_crystal"}}
})
minetest.register_craftitem("voltbuild:halt", {
description = "Halt",
inventory_image = "voltbuild_halt.png",
voltbuild = {component=1,
can_run = function(pos)
return false
end},
documentation = {summary="Stops a machine from working (stops it abm) while this component is inside."}
})
minetest.register_craft({
output = "voltbuild:halt",
recipe = {{"default:gold_ingot","voltbuild:splitter_cable_000000","default:gold_ingot"},
{"voltbuild:re_battery","voltbuild:circuit","voltbuild:re_battery"}}
})
minetest.register_craftitem("voltbuild:fan",{
description = "Fan",
inventory_image = "voltbuild_fan.png",
voltbuild = {component=1,
after_effects = function(pos)
local meta = minetest.env:get_meta(pos)
local stress = meta:get_int("stress")
meta:set_int("stress",math.max(stress-20,0))
end},
documentation = {summary="Slowly relieves a machine's stress."}
})
minetest.register_craft({
output = "voltbuild:fan",
recipe = {{"voltbuild:refined_iron_ingot","voltbuild:refined_iron_ingot","voltbuild:refined_iron_ingot"},
{"voltbuild:windmill","voltbuild:batbox",""},
{"voltbuild:refined_iron_ingot","voltbuild:refined_iron_ingot","voltbuild:refined_iron_ingot"}},
})
--added to have it in the documentation
voltbuild.recipes.air_compressing = {}
voltbuild.register_machine_recipe("air","voltbuild:air_cell","air_compressing")
--A component specific to the compressor
minetest.register_craftitem("voltbuild:air_compressor", {
description = "Air Compressor",
inventory_image = "voltbuild_air_compressor.png",
voltbuild = {component=1,
can_run = function(pos)
if minetest.get_node(pos)["name"] == "voltbuild:compressor" then
return false
end
end,
not_run_effects = function(pos)
local meta = minetest.env:get_meta(pos)
local node = minetest.get_node(pos)
local optime
local speed = minetest.registered_nodes[node.name]["voltbuild"]["speed"]
if meta:get_string("speed") ~= "" then
speed = meta:get_float("speed")
elseif speed then
else
speed = 1.0
end
local optime = 20
if meta:get_int("energy") > 6 then
local inv = meta:get_inventory()
if meta:get_string("stime") == "" then
meta:set_float("stime", 0.0)
end
meta:set_float("stime",meta:get_float("stime")+speed)
if meta:get_float("stime") > optime then
meta:set_float("stime",meta:get_float("stime")-optime)
if inv:room_for_item("dst",ItemStack("voltbuild:air_cell")) then
inv:add_item("dst",ItemStack("voltbuild:air_cell"))
end
end
meta:set_int("energy",meta:get_int("energy")-6)
voltbuild_hacky_swap_node(pos,"voltbuild:compressor_active")
else
voltbuild_hacky_swap_node(pos,"voltbuild:compressor")
end
meta:set_string("formspec", consumers.get_formspec(pos)..
voltbuild.production_spec..
consumers.get_progressbar(meta:get_float("stime"),optime,
"itest_extractor_progress_bg.png",
"itest_extractor_progress_fg.png"))
end},
documentation = {summary="Add to a compressor to make it compress air into air cells instead of items."}
})
minetest.register_craft({
output = "voltbuild:air_compressor",
recipe = {{"voltbuild:advanced_circuit","voltbuild:extractor","default:mese_crystal_fragment"},
{"voltbuild:fan","","voltbuild:casing"},
{"voltbuild:advanced_circuit","voltbuild:extractor","default:mese_crystal_fragment"}},
})
minetest.register_craftitem("voltbuild:mobile_solar_panel", {
description = "Mobile Solar Panel",
inventory_image = "voltbuild_mobile_solar_panel.png",
voltbuild = {component=1,
before_effects = function(pos)
local meta = minetest.env:get_meta(pos)
local node = minetest.get_node(pos)
local node_def = minetest.registered_nodes["voltbuild:solar_panel"]
local psize = node_def.voltbuild.psize
local speed = node_def.voltbuild.speed
local max_energy = minetest.registered_nodes[node.name]["voltbuild"]["max_energy"]
if max_energy and speed(pos) >= 1 then
meta:set_int("energy",math.min(meta:get_int("energy")+psize,max_energy))
end
end},
documentation = {summary="Add to a machine to make it slowly generate electricity in the sun."}
})
minetest.register_craft({
output = "voltbuild:mobile_solar_panel",
recipe = {{"","voltbuild:solar_panel",""},
{"voltbuild:energy_crystal","voltbuild:alunra_gem","voltbuild:energy_crystal"},
{"voltbuild:alunra_gem","voltbuild:advanced_circuit","voltbuild:alunra_gem"}}
})