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
|
local controller_nodebox ={
{-0.3125, -0.3125, -0.3125, 0.3125, 0.3125, 0.3125}, -- Core
{-0.1875, 0.3125, -0.1875, 0.1875, 0.5, 0.1875}, -- +y_connector
{-0.1875, -0.5, -0.1875, 0.1875, -0.3125, 0.1875}, -- -y_Connector
{0.3125, -0.1875, -0.1875, 0.5, 0.1875, 0.1875}, -- +x_connector
{-0.5, -0.1875, -0.1875, -0.3125, 0.1875, 0.1875}, -- -x_connector
{-0.1875, -0.1875, 0.3125, 0.1875, 0.1875, 0.5}, -- +z_connector
{-0.5, 0.125, -0.5, -0.125, 0.5, -0.3125}, -- back_connector_3
{0.125, 0.125, -0.5, 0.5, 0.5, -0.3125}, -- back_connector_1
{0.125, -0.5, -0.5, 0.5, -0.125, -0.3125}, -- back_connector_2
{-0.5, -0.5, -0.5, -0.125, -0.125, -0.3125}, -- back_connector_4
}
-- Master controller. Most complicated part of the whole system. Determines which direction a digtron moves and triggers all of its component parts.
minetest.register_node("digtron:controller", {
description = "Digtron Control Unit",
groups = {cracky = 3, stone = 1, digtron = 1},
drop = 'digtron:controller',
paramtype2= 'facedir',
-- Aims in the +Z direction by default
tiles = {
"digtron_plate.png^[transformR90",
"digtron_plate.png^[transformR270",
"digtron_plate.png",
"digtron_plate.png^[transformR180",
"digtron_plate.png",
"digtron_control.png",
},
drawtype = "nodebox",
paramtype = "light",
node_box = {
type = "fixed",
fixed = controller_nodebox,
},
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
if meta:get_string("waiting") == "true" then
-- Been too soon since last time the digtron did a cycle.
return
end
local layout = digtron.get_all_digtron_neighbours(pos, clicker)
if layout.all == nil then
-- get_all_digtron_neighbours returns nil if the digtron array touches unloaded nodes, too dangerous to do anything in that situation. Abort.
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
meta:set_string("infotext", "Digtron is adjacent to unloaded nodes.")
return
end
if layout.traction * digtron.traction_factor < table.getn(layout.all) then
-- digtrons can't fly
minetest.sound_play("squeal", {gain=1.0, pos=pos})
meta:set_string("infotext", string.format("Digtron has %d nodes but only enough traction to move %d nodes.", table.getn(layout.all), layout.traction * digtron.traction_factor))
return
end
local facing = minetest.get_node(pos).param2
local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing)
----------------------------------------------------------------------------------------------------------------------
local nodes_dug = Pointset.create()
local items_dropped = {}
-- execute the execute_dig method on all digtron components that have one
-- This builds a set of nodes that will be dug and returns a list of products that will be generated
-- but doesn't actually dig the nodes yet. That comes later.
-- If we dug them now, sand would fall and some digtron nodes would die.
for k, location in pairs(layout.diggers) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
if targetdef.execute_dig ~= nil then
local dropped = targetdef.execute_dig(location, layout.protected, nodes_dug, controlling_coordinate)
if dropped ~= nil then
for _, itemname in pairs(dropped) do
table.insert(items_dropped, itemname)
end
end
else
minetest.log(string.format("%s has digger group but is missing execute_dig method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
----------------------------------------------------------------------------------------------------------------------
-- test if any digtrons are obstructed by non-digtron nodes that haven't been marked
-- as having been dug.
local can_move = true
for _, location in pairs(layout.all) do
local newpos = digtron.find_new_pos(location, facing)
if not digtron.can_move_to(newpos, layout.protected, nodes_dug) then
can_move = false
end
end
if not can_move then
-- mark this node as waiting, will clear this flag in digtron.refractory seconds
minetest.get_meta(pos):set_string("waiting", "true")
minetest.after(digtron.refractory,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
minetest.sound_play("squeal", {gain=1.0, pos=pos})
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
meta:set_string("infotext", "Digtron is obstructed.")
return --Abort, don't dig and don't build.
end
----------------------------------------------------------------------------------------------------------------------
-- ask each builder node if it can get what it needs from inventory to build this cycle.
-- This is a complicated test because each builder needs to actually *take* the item it'll
-- need from inventory, and then we put it all back afterward.
local can_build = true
local test_build_return_code = nil
local test_build_return_item = nil
local test_items = {}
for k, location in pairs(layout.builders) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
local test_location = digtron.find_new_pos(location, facing)
if targetdef.test_build ~= nil then
test_build_return_code, test_build_return_item = targetdef.test_build(location, test_location, layout.inventories, layout.protected, nodes_dug, controlling_coordinate, layout.controller)
if test_build_return_code > 1 then
can_build = false
break
end
if test_build_return_code == 1 then
table.insert(test_items, test_build_return_item)
end
else
minetest.log(string.format("%s has builder group but is missing test_build method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
for k, item_return in pairs(test_items) do
--Put everything back where it came from
digtron.place_in_specific_inventory(item_return.item, item_return.location, layout.inventories, layout.controller)
end
if not can_build then
minetest.get_meta(pos):set_string("waiting", "true")
minetest.after(digtron.refractory,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
if test_build_return_code == 3 then
minetest.sound_play("honk", {gain=0.5, pos=pos}) -- A builder is not configured
meta:set_string("infotext", "Digtron connected to at least one builder node that hasn't had an output material assigned.")
elseif test_build_return_code == 2 then
minetest.sound_play("dingding", {gain=1.0, pos=pos}) -- Insufficient inventory
meta:set_string("infotext", string.format("Digtron has insufficient materials in inventory to execute all build operations.\nNeeded: %s",
test_build_return_item:get_name()))
end
return --Abort, don't dig and don't build.
end
----------------------------------------------------------------------------------------------------------------------
-- All tests passed, ready to go for real!
minetest.sound_play("construction", {gain=1.0, pos=pos})
-- store or drop the products of the digger heads
for _, itemname in pairs(items_dropped) do
digtron.place_in_inventory(itemname, layout.inventories, pos)
end
-- if the player is standing within the array or next to it, move him too.
local player_pos = clicker:getpos()
local move_player = false
if player_pos.x >= layout.extents.min_x - 1 and player_pos.x <= layout.extents.max_x + 1 and
player_pos.y >= layout.extents.min_y - 1 and player_pos.y <= layout.extents.max_y + 1 and
player_pos.z >= layout.extents.min_z - 1 and player_pos.z <= layout.extents.max_z + 1 then
move_player = true
end
--move the array
digtron.move_digtron(facing, layout.all, layout.extents, nodes_dug)
local oldpos = {x=pos.x, y=pos.y, z=pos.z}
pos = digtron.find_new_pos(pos, facing)
if move_player then
clicker:moveto(digtron.find_new_pos(player_pos, facing), true)
end
-- Start the delay before digtron can run again. Do this after moving the array or pos will be wrong.
minetest.get_meta(pos):set_string("waiting", "true")
minetest.after(digtron.refractory,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
local strange_failure = false
-- execute_build on all digtron components that have one
for k, location in pairs(layout.builders) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
if targetdef.execute_build ~= nil then
--using the old location of the controller as fallback so that any leftovers land with the rest of the digger output. Not that there should be any.
if targetdef.execute_build(location, clicker, layout.inventories, layout.protected, nodes_dug, controlling_coordinate, oldpos) == false then
-- Don't interrupt the build cycle as a whole, we've already moved so might as well try to complete as much as possible.
strange_failure = true
end
else
minetest.log(string.format("%s has builder group but is missing execute_build method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
if strange_failure then
-- We weren't able to detect this build failure ahead of time, so make a big noise now. This is strange, shouldn't happen often.
minetest.sound_play("dingding", {gain=1.0, pos=pos})
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
meta:set_string("infotext", "Digtron unexpectedly failed to execute a build operation.")
end
-- finally, dig out any nodes remaining to be dug. Some of these will have had their flag revoked because
-- a builder put something there or because they're another digtron node.
local node_to_dig, whether_to_dig = nodes_dug:pop()
while node_to_dig ~= nil do
if whether_to_dig == true then
minetest.remove_node(node_to_dig)
end
node_to_dig, whether_to_dig = nodes_dug:pop()
end
end,
})
-- A much simplified control unit that only moves the digtron, and doesn't trigger the diggers or builders.
-- Handy for shoving a digtron to the side if it's been built a bit off.
minetest.register_node("digtron:pusher", {
description = "Digtron Pusher Unit",
groups = {cracky = 3, stone = 1, digtron = 1},
drop = 'digtron:pusher',
paramtype2= 'facedir',
-- Aims in the +Z direction by default
tiles = {
"digtron_plate.png^[transformR90^[colorize:#00880030",
"digtron_plate.png^[transformR270^[colorize:#00880030",
"digtron_plate.png^[colorize:#00880030",
"digtron_plate.png^[transformR180^[colorize:#00880030",
"digtron_plate.png^[colorize:#00880030",
"digtron_control.png^[colorize:#00880030",
},
drawtype = "nodebox",
paramtype = "light",
node_box = {
type = "fixed",
fixed = controller_nodebox,
},
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
if meta:get_string("waiting") == "true" then
-- Been too soon since last time the digtron did a cycle.
return
end
local layout = digtron.get_all_digtron_neighbours(pos, clicker)
if layout.all == nil then
-- get_all_digtron_neighbours returns nil if the digtron array touches unloaded nodes, too dangerous to do anything in that situation. Abort.
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
meta:set_string("infotext", "Digtron is adjacent to unloaded nodes.")
return
end
if layout.traction * digtron.traction_factor < table.getn(layout.all) then
-- digtrons can't fly
minetest.sound_play("squeal", {gain=1.0, pos=pos})
meta:set_string("infotext", string.format("Digtron has %d nodes but only enough traction to move %d nodes.", table.getn(layout.all), layout.traction * digtron.traction_factor))
return
end
local facing = minetest.get_node(pos).param2
local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing)
local nodes_dug = Pointset.create() -- empty set, we're not digging anything
-- test if any digtrons are obstructed by non-digtron nodes that haven't been marked
-- as having been dug.
local can_move = true
for _, location in pairs(layout.all) do
local newpos = digtron.find_new_pos(location, facing)
if not digtron.can_move_to(newpos, layout.protected, nodes_dug) then
can_move = false
end
end
if not can_move then
-- mark this node as waiting, will clear this flag in digtron.refractory seconds
meta:set_string("waiting", "true")
minetest.after(digtron.refractory,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
minetest.sound_play("squeal", {gain=1.0, pos=pos})
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
meta:set_string("infotext", "Digtron is obstructed.")
return --Abort
end
minetest.sound_play("truck", {gain=1.0, pos=pos})
-- if the player is standing within the array or next to it, move him too.
local player_pos = clicker:getpos()
local move_player = false
if player_pos.x >= layout.extents.min_x - 1 and player_pos.x <= layout.extents.max_x + 1 and
player_pos.y >= layout.extents.min_y - 1 and player_pos.y <= layout.extents.max_y + 1 and
player_pos.z >= layout.extents.min_z - 1 and player_pos.z <= layout.extents.max_z + 1 then
move_player = true
end
--move the array
digtron.move_digtron(facing, layout.all, layout.extents, nodes_dug)
local oldpos = {x=pos.x, y=pos.y, z=pos.z}
pos = digtron.find_new_pos(pos, facing)
if move_player then
clicker:moveto(digtron.find_new_pos(player_pos, facing), true)
end
-- Start the delay before digtron can run again. Do this after moving the array or pos will be wrong.
minetest.get_meta(pos):set_string("waiting", "true")
minetest.after(digtron.refractory,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
end,
})
|