forked from joe7575/signs_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtechage.lua
More file actions
371 lines (344 loc) · 10.1 KB
/
techage.lua
File metadata and controls
371 lines (344 loc) · 10.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
367
368
369
370
--[[
Signs Bot
=========
Copyright (C) 2019-2021 Joachim Stolberg
GPLv3
See LICENSE.txt for more information
Signs Bot: interface for techage
]]--
-- Load support for I18n.
local S = signs_bot.S
local MAX_CAPA = signs_bot.MAX_CAPA
local PWR_NEEDED = 8
if minetest.global_exists("techage") then
local function on_power(pos)
local mem = tubelib2.get_mem(pos)
mem.power_available = true
mem.charging = true
signs_bot.infotext(pos, S("charging"))
end
local function on_nopower(pos)
local mem = tubelib2.get_mem(pos)
mem.power_available = false
signs_bot.infotext(pos, S("no power"))
end
local Cable = techage.ElectricCable
local power = networks.power
signs_bot.register_inventory({"techage:chest_ta2", "techage:chest_ta3", "techage:chest_ta4",
"techage:ta3_silo", "techage:ta4_silo", "techage:ta4_sensor_chest",
"techage:ta4_reactor"}, {
allow_inventory_put = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
end,
allow_inventory_take = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
end,
put = {
listname = "main",
},
take = {
listname = "main",
},
})
signs_bot.register_inventory({"techage:meltingpot", "techage:meltingpot_active"}, {
allow_inventory_put = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
end,
allow_inventory_take = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
end,
put = {
listname = "src",
},
take = {
listname = "dst",
},
})
signs_bot.register_inventory({
"techage:ta2_autocrafter_pas", "techage:ta2_autocrafter_act",
"techage:ta3_autocrafter_pas", "techage:ta3_autocrafter_act"}, {
allow_inventory_put = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
end,
allow_inventory_take = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
end,
put = {
listname = "src",
},
take = {
listname = "dst",
},
})
signs_bot.register_inventory({
"techage:ta2_distributor_pas", "techage:ta2_distributor_act",
"techage:ta3_distributor_pas", "techage:ta3_distributor_act",
"techage:ta4_distributor_pas", "techage:ta4_distributor_act",
"techage:ta4_high_performance_distributor_pas", "techage:ta4_high_performance_distributor_act"}, {
allow_inventory_put = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
end,
allow_inventory_take = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
end,
put = {
listname = "src",
},
take = {
listname = "src",
},
})
local function percent_value(max_val, curr_val)
return math.min(math.ceil(((curr_val or 0) * 100.0) / (max_val or 1.0)), 100)
end
signs_bot.percent_value = percent_value
function signs_bot.formspec_battery_capa(max_capa, current_capa)
local percent = percent_value(max_capa, current_capa)
return "image[0.1,0;0.5,1;signs_bot_form_level_bg.png^[lowpart:"..
percent..":signs_bot_form_level_fg.png]"
end
signs_bot.register_botcommand("ignite", {
mod = "techage",
params = "",
num_param = 0,
description = S("Ignite the techage charcoal lighter"),
cmnd = function(base_pos, mem)
local pos = signs_bot.lib.dest_pos(mem.robot_pos, mem.robot_param2, {0})
local node = tubelib2.get_node_lvm(pos)
if minetest.registered_nodes[node.name]
and minetest.registered_nodes[node.name].on_ignite then
minetest.registered_nodes[node.name].on_ignite(pos)
end
return signs_bot.DONE
end,
})
signs_bot.register_botcommand("low_batt", {
mod = "techage",
params = "<percent>",
num_param = 1,
description = S("Turns the bot off if the\nbattery power is below the\ngiven value in percent (1..99)"),
check = function(val)
val = tonumber(val) or 5
return val and val > 0 and val < 100
end,
cmnd = function(base_pos, mem, val)
val = tonumber(val) or 5
local pwr = percent_value(signs_bot.MAX_CAPA, mem.capa)
if pwr < val then
signs_bot.stop_robot(base_pos, mem)
return signs_bot.TURN_OFF
end
return signs_bot.DONE
end,
})
signs_bot.register_botcommand("jump_low_batt", {
mod = "techage",
params = "<percent> <label>",
num_param = 2,
description = S("Jump to <label> if the\nbattery power is below the\ngiven value in percent (1..99)"),
check = function(val, lbl)
val = tonumber(val) or 5
return val and val > 0 and val < 100 and signs_bot.check_label(lbl)
end,
cmnd = function(base_pos, mem, val, addr)
val = tonumber(val) or 5
local pwr = percent_value(signs_bot.MAX_CAPA, mem.capa)
if pwr < val then
mem.pc = addr - 3
return signs_bot.DONE
end
return signs_bot.DONE
end,
})
signs_bot.register_botcommand("send_cmnd", {
mod = "techage",
params = "<receiver> <command>",
num_param = 2,
description = S([[Sends a techage command
to a given node.
Receiver is addressed by
the techage node number.
For commands with two or more
words, use the '*' character
instead of spaces, e.g.:
send_cmnd 3465 pull*default:dirt*2]]),
check = function(address, command)
address = tonumber(address)
return address ~= nil and command ~= nil and command ~= ""
end,
cmnd = function(base_pos, mem, address, command)
command = command:gsub("*", " ")
address = tostring(tonumber(address))
local meta = minetest.get_meta(base_pos)
local number = tostring(meta:get_int("number") or 0)
local topic, payload = unpack(string.split(command, " ", false, 1))
techage.send_single(number, address, topic, payload)
return signs_bot.DONE
end,
})
-- Bot in the box
function signs_bot.while_charging(pos, mem)
mem.capa = mem.capa or 0
if mem.capa < signs_bot.MAX_CAPA then
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
mem.capa = mem.capa + consumed
else
minetest.get_node_timer(pos):stop()
mem.charging = false
if not mem.running then
signs_bot.infotext(pos, S("fully charged"))
end
return false
end
return true
end
power.register_nodes({"signs_bot:box"}, Cable, "con")
techage.register_node({"signs_bot:box"}, {
on_inv_request = function(pos, in_dir, access_type)
local meta = minetest.get_meta(pos)
return meta:get_inventory(), "main"
end,
on_pull_item = function(pos, in_dir, num, item_name)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if item_name then
local taken = inv:remove_item("main", {name = item_name, count = num})
if taken:get_count() > 0 then
return taken
end
else -- no item given
return techage.get_items(pos, inv, "main", num)
end
end,
on_push_item = function(pos, in_dir, stack, idx)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return techage.put_items(inv, "main", stack, idx)
end,
on_unpull_item = function(pos, in_dir, stack)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return techage.put_items(inv, "main", stack)
end,
on_recv_message = function(pos, src, topic, payload)
local mem = tubelib2.get_mem(pos)
if topic == "state" then
if mem.error then
return "fault"
elseif mem.running then
if mem.curr_cmnd == "stop" then
return "standby"
elseif mem.blocked then
return "blocked"
else
return "running"
end
elseif mem.capa then
if mem.capa <= 0 then
return "nopower"
elseif mem.capa >= signs_bot.MAX_CAPA then
return "stopped"
else
return "charging"
end
else
return "stopped"
end
elseif topic == "load" then
return signs_bot.percent_value(signs_bot.MAX_CAPA, mem.capa)
elseif topic == "on" then
if not mem.running then
signs_bot.start_robot(pos)
end
elseif topic == "off" then
if mem.running then
signs_bot.stop_robot(pos, mem)
end
else
return "unsupported"
end
end,
on_beduino_receive_cmnd = function(pos, src, topic, payload)
local mem = tubelib2.get_mem(pos)
if topic == 1 then
if payload[1] == 1 then -- on
if not mem.running then
signs_bot.start_robot(pos)
return 0, {1}
end
else
if mem.running then
signs_bot.stop_robot(pos, mem)
return 0, {1}
end
end
else
return 2, "" -- topic is unknown or invalid
end
end,
on_beduino_request_data = function(pos, src, topic, payload)
local mem = tubelib2.get_mem(pos)
if topic == 128 then -- state
if mem.error then
return 0, {5}
elseif mem.running then
if mem.curr_cmnd == "stop" then
return 0, {3}
elseif mem.blocked then
return 0, {2}
else
return 0, {1} -- running
end
elseif mem.capa then
if mem.capa <= 0 then
return 0, {4} -- nopower
elseif mem.capa >= signs_bot.MAX_CAPA then
return 0, {6} -- stopped
else
return 0, {7} -- charging
end
else
return 0, {6} -- stopped
end
else
return 2, "" -- topic is unknown or invalid
end
end,
})
techage.register_node({"signs_bot:chest"}, {
on_inv_request = function(pos, in_dir, access_type)
local meta = minetest.get_meta(pos)
return meta:get_inventory(), "main"
end,
on_pull_item = function(pos, in_dir, num, item_name)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if item_name then
local taken = inv:remove_item("main", {name = item_name, count = num})
if taken:get_count() > 0 then
return taken
end
else -- no item given
return techage.get_items(pos, inv, "main", num)
end
end,
on_push_item = function(pos, in_dir, stack, idx)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return techage.put_items(inv, "main", stack, idx)
end,
on_unpull_item = function(pos, in_dir, stack)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return techage.put_items(inv, "main", stack)
end,
})
techage.register_node_for_v1_transition({"signs_bot:box"}, function(pos, node)
power.update_network(pos, nil, Cable)
end)
techage.disable_block_for_assembly_tool("signs_bot:box")
else
function signs_bot.formspec_battery_capa(max_capa, current_capa)
return ""
end
end