-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharging_bay.lua
More file actions
366 lines (319 loc) · 11.1 KB
/
charging_bay.lua
File metadata and controls
366 lines (319 loc) · 11.1 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
local S = core.get_translator(core.get_current_modname())
-- local S = technic.getter
local fs_helpers = pipeworks.fs_helpers
local connect_sides = {"bottom"}
local cable_entry = "^technic_cable_connection_overlay.png"
local digilines_rules = {{
x = 0,
y = 0,
z = 1
}, {
x = 0,
y = 0,
z = -1
}, {
x = 1,
y = 0,
z = 0
}, {
x = -1,
y = 0,
z = 0
}, {
x = 0,
y = 1,
z = 0
}, {
x = 0,
y = -1,
z = 0
}}
local time_scl = 200
local function round(v)
return math.floor(v + 0.5)
end
local function charge_robot_battery(pos, charge, slot)
local meta = core.get_meta(pos)
if not meta then
return false, 0
end
local inv = meta:get_inventory()
if not inv then
return false, 0
end
if not slot then
slot = 1
end
local f = 0
local stack = nil
local slot_index = 0
local slots = inv:get_size("storage")
for s = 1, slots do
local _stack = inv:get_stack("storage", s)
if _stack ~= nil then
if _stack:get_name() == "technic:battery" then
local tool_charge, item_max_charge = technic.get_RE_charge(_stack)
if tool_charge < item_max_charge then
stack = _stack
slot_index = s
f = f + 1
if f >= slot then
break
end
end
end
end
end
if not stack or stack:is_empty() then
return false, 0
end
local tool_charge, item_max_charge = technic.get_RE_charge(stack)
if tool_charge >= item_max_charge or item_max_charge == 0 then
return false, charge
end
tool_charge = tool_charge + charge
local charge_rem = 0
if tool_charge > item_max_charge then
charge_rem = tool_charge - item_max_charge
tool_charge = item_max_charge
end
local tool_name = stack:get_name()
if technic.power_tools[tool_name] then
technic.set_RE_charge(stack, tool_charge)
inv:set_stack("storage", slot_index, stack)
return true, charge_rem
end
return false, charge_rem
end
local function register_machine_charging_bay(data)
local typename = data.typename
local machine_name = data.machine_name
local machine_desc = data.machine_desc
local tier = data.tier
local ltier = string.lower(tier)
data.modname = data.modname or core.get_current_modname()
local groups = {
cracky = 2,
technic_machine = 1,
["technic_" .. ltier] = 1,
ctg_machine = 1,
metal = 1
}
local active_groups = {
not_in_creative_inventory = 1
}
for k, v in pairs(groups) do
active_groups[k] = v
end
local digiline = {
receptor = {
rules = digilines_rules
},
wire = {
rules = digilines_rules
}
}
local run = function(pos, node)
local meta = core.get_meta(pos)
local inv = meta:get_inventory()
local eu_input = meta:get_int(tier .. "_EU_input")
local machine_desc_tier = machine_desc:format(tier)
local machine_node = data.modname .. ":" .. ltier .. "_" .. machine_name
local machine_demand = data.demand
local pos_down = {
x = pos.x,
y = pos.y - 1,
z = pos.z
}
local name_down = core.get_node(pos_down).name
local cable_in = technic.get_cable_tier(name_down)
if not cable_in then
return
end
-- Setup meta data if it does not exist.
if not eu_input then
meta:set_int(tier .. "_EU_demand", machine_demand[1])
meta:set_int(tier .. "_EU_input", 0)
return
end
local EU_upgrade, tube_upgrade = 0, 0
if data.upgrade then
EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
end
local powered = eu_input >= machine_demand[EU_upgrade + 1]
if powered then
meta:set_int("src_time", meta:get_int("src_time") + round(data.speed * 10))
end
while true do
-- do power draw and activate machine
meta:set_int(tier .. "_EU_demand", machine_demand[EU_upgrade + 1])
meta:set_string("infotext", machine_desc_tier .. S(" Active"))
technic.swap_node(pos, machine_node .. "_active")
if powered == false then
technic.swap_node(pos, machine_node)
meta:set_string("infotext", machine_desc_tier .. S(" Unpowered"))
-- local formspec = update_formspec(data, meta)
-- meta:set_string("formspec", formspec)
return
end
local range = 1
local pos1 = vector.subtract(pos, {
x = 0,
y = 0,
z = 0
})
local pos2 = vector.add(pos, {
x = 0,
y = range,
z = 0
})
local robot = core.find_nodes_in_area(pos1, pos2,
{"lwcomputers:computer_robot", "lwcomputers:computer_robot_on"})
if #robot > 0 then
local charge = data.charge
local result, charge_rem = charge_robot_battery(robot[1], charge)
if result then
meta:set_string("infotext", machine_desc_tier .. S(" Charging Active"))
if charge_rem > 0 then
result, charge_rem = charge_robot_battery(robot[1], charge_rem)
end
meta:set_int(tier .. "_EU_demand", machine_demand[EU_upgrade + 1] * 3 - charge_rem)
end
end
if meta:get_int("src_time") < round(time_scl * 10) then
-- local formspec = update_formspec(data, meta)
-- meta:set_string("formspec", formspec)
return
end
meta:set_int("src_time", meta:get_int("src_time") - round(time_scl * 10))
end
end
local tr = ""
if ltier == "mv" then
tr = "^[colorize:#00000020"
elseif ltier == "hv" then
tr = "^[colorize:#e8e8e820"
end
local node_name = data.modname .. ":" .. ltier .. "_" .. machine_name
core.register_node(node_name, {
description = machine_desc:format(tier),
-- up, down, right, left, back, front
tiles = {machine_name .. "_top.png" .. tr, machine_name .. "_bottom.png" .. tr .. cable_entry,
machine_name .. "_side.png" .. tr, machine_name .. "_side.png" .. tr,
machine_name .. "_side.png" .. tr, machine_name .. "_side.png" .. tr},
paramtype2 = "facedir",
groups = groups,
connect_sides = connect_sides,
legacy_facedir_simple = true,
sounds = default.node_sound_metal_defaults(),
after_place_node = function(pos, placer, itemstack, pointed_thing)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
return technic.machine_after_dig_node
end,
on_rotate = screwdriver.disallow,
on_construct = function(pos)
local node = core.get_node(pos)
local meta = core.get_meta(pos)
meta:set_string("infotext", machine_desc:format(tier))
local inv = meta:get_inventory()
inv:set_size("upgrade1", 1)
inv:set_size("upgrade2", 1)
end,
can_dig = technic.machine_can_dig,
technic_run = run,
on_receive_fields = function(pos, formname, fields, sender)
if fields.quit then
return
end
end,
mesecons = {},
digiline = digiline
})
core.register_node(data.modname .. ":" .. ltier .. "_" .. machine_name .. "_active", {
description = machine_desc:format(tier),
tiles = {{
name = machine_name .. "_top_active.png" .. tr,
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 1
}
}, machine_name .. "_bottom.png" .. tr .. cable_entry, machine_name .. "_side.png" .. tr,
machine_name .. "_side.png" .. tr, machine_name .. "_side.png" .. tr, machine_name .. "_side.png" .. tr},
light_source = 5,
paramtype = "light",
paramtype2 = "facedir",
drop = data.modname .. ":" .. ltier .. "_" .. machine_name,
groups = active_groups,
connect_sides = connect_sides,
legacy_facedir_simple = true,
sounds = default.node_sound_metal_defaults(),
after_place_node = function(pos, placer, itemstack, pointed_thing)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
return technic.machine_after_dig_node
end,
on_rotate = screwdriver.disallow,
can_dig = technic.machine_can_dig,
technic_run = run,
technic_disabled_machine_name = data.modname .. ":" .. ltier .. "_" .. machine_name,
on_receive_fields = function(pos, formname, fields, sender)
if fields.quit then
return
end
end,
mesecons = {},
digiline = digiline
})
technic.register_machine(tier, node_name, technic.receiver)
technic.register_machine(tier, node_name .. "_active", technic.receiver)
end -- End registration
function ctg_machines.register_machine_charger(data)
data.machine_name = "charging_bay"
data.typename = "charging_bay"
data.machine_desc = S("%s Robot Charger")
register_machine_charging_bay(data)
end
ctg_machines.register_machine_charger({
tier = "LV",
demand = {100},
speed = 5,
charge = 250
})
ctg_machines.register_machine_charger({
tier = "MV",
demand = {200},
speed = 4,
charge = 500
})
ctg_machines.register_machine_charger({
tier = "HV",
demand = {300},
speed = 2,
charge = 1000
})
local es = "basic_materials:empty_spool"
core.register_craft({
output = "ctg_machines:lv_charging_bay 1",
recipe = {{"basic_materials:copper_wire", "moreores:silver_ingot", "technic:stainless_steel_ingot"},
{"basic_materials:copper_wire", "technic:machine_casing", "default:mese_crystal"},
{"basic_materials:copper_wire", "default:steelblock", "basic_materials:copper_wire"}},
replacements = {{"basic_materials:copper_wire", es .. " 4"}}
})
core.register_craft({
output = "ctg_machines:mv_charging_bay 1",
recipe = {{"", "technic:chromium_ingot", ""},
{"basic_materials:copper_wire", "ctg_machines:lv_charging_bay", "basic_materials:copper_wire"},
{"", "technic:mv_transformer", ""}},
replacements = {{"basic_materials:copper_wire", es .. " 2"}}
})
core.register_craft({
output = "ctg_machines:hv_charging_bay 1",
recipe = {{"", "technic:chromium_ingot", ""},
{"basic_materials:copper_wire", "ctg_machines:mv_charging_bay", "basic_materials:copper_wire"},
{"", "technic:hv_transformer", ""}},
replacements = {{"basic_materials:copper_wire", es .. " 2"}}
})