Skip to content

Commit 7f84116

Browse files
authored
dig nodes once and only once per cycle (#119)
* dig nodes once and only once per cycle * Update dual-diggers handling * whitespace fix * whitespace fix (again!) * combine checks to can_dig_pos() combine checks of protection and already dug nodes into one function.
1 parent 87b448b commit 7f84116

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

nodes/node_diggers.lua

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ minetest.register_node("digtron:digger", {
142142
execute_dig = function(pos, protected_nodes, nodes_dug, _, _, player)
143143
local facing = minetest.get_node(pos).param2
144144
local digpos = digtron.find_new_pos(pos, facing)
145-
146-
if protected_nodes:get(digpos.x, digpos.y, digpos.z) then
145+
if not digtron.can_dig_pos(digpos, protected_nodes, nodes_dug) then
147146
return 0
148147
end
149148

@@ -225,8 +224,7 @@ minetest.register_node("digtron:intermittent_digger", {
225224

226225
local facing = minetest.get_node(pos).param2
227226
local digpos = digtron.find_new_pos(pos, facing)
228-
229-
if protected_nodes:get(digpos.x, digpos.y, digpos.z) then
227+
if not digtron.can_dig_pos(digpos, protected_nodes, nodes_dug) then
230228
return 0
231229
end
232230

@@ -287,8 +285,7 @@ minetest.register_node("digtron:soft_digger", {
287285
execute_dig = function(pos, protected_nodes, nodes_dug, _, _, player)
288286
local facing = minetest.get_node(pos).param2
289287
local digpos = digtron.find_new_pos(pos, facing)
290-
291-
if protected_nodes:get(digpos.x, digpos.y, digpos.z) then
288+
if not digtron.can_dig_pos(digpos, protected_nodes, nodes_dug) then
292289
return 0
293290
end
294291

@@ -352,8 +349,7 @@ minetest.register_node("digtron:intermittent_soft_digger", {
352349

353350
local facing = minetest.get_node(pos).param2
354351
local digpos = digtron.find_new_pos(pos, facing)
355-
356-
if protected_nodes:get(digpos.x, digpos.y, digpos.z) then
352+
if not digtron.can_dig_pos(digpos, protected_nodes, nodes_dug) then
357353
return 0
358354
end
359355

@@ -431,7 +427,7 @@ minetest.register_node("digtron:dual_digger", {
431427
local items = {}
432428
local cost = 0
433429

434-
if protected_nodes:get(digpos.x, digpos.y, digpos.z) ~= true then
430+
if digtron.can_dig_pos(digpos, protected_nodes, nodes_dug) then
435431
local forward_cost, forward_items = digtron.mark_diggable(digpos, nodes_dug, player)
436432
if forward_items ~= nil then
437433
for _, item in pairs(forward_items) do
@@ -440,15 +436,18 @@ minetest.register_node("digtron:dual_digger", {
440436
end
441437
cost = cost + forward_cost
442438
end
443-
if protected_nodes:get(digdown.x, digdown.y, digdown.z) ~= true then
444-
local down_cost, down_items = digtron.mark_diggable(digdown, nodes_dug, player)
445-
if down_items ~= nil then
446-
for _, item in pairs(down_items) do
447-
table.insert(items, item)
448-
end
439+
440+
if not digtron.can_dig_pos(digdown, protected_nodes, nodes_dug) then
441+
return cost, items
442+
end
443+
444+
local down_cost, down_items = digtron.mark_diggable(digdown, nodes_dug, player)
445+
if down_items ~= nil then
446+
for _, item in pairs(down_items) do
447+
table.insert(items, item)
449448
end
450-
cost = cost + down_cost
451449
end
450+
cost = cost + down_cost
452451

453452
return cost, items
454453
end,
@@ -509,11 +508,16 @@ minetest.register_node("digtron:dual_soft_digger", {
509508
local facing = minetest.get_node(pos).param2
510509
local digpos = digtron.find_new_pos(pos, facing)
511510
local digdown = digtron.find_new_pos_downward(pos, facing)
511+
if nodes_dug:get_pos(digpos) or nodes_dug:get_pos(digdown) then
512+
return 0
513+
end
512514

513515
local items = {}
514516
local cost = 0
515517

516-
if protected_nodes:get(digpos.x, digpos.y, digpos.z) ~= true and digtron.is_soft_material(digpos) then
518+
if digtron.can_dig_pos(digpos, protected_nodes, nodes_dug)
519+
and digtron.is_soft_material(digpos)
520+
then
517521
local forward_cost, forward_items = digtron.mark_diggable(digpos, nodes_dug, player)
518522
if forward_items ~= nil then
519523
for _, item in pairs(forward_items) do
@@ -522,7 +526,12 @@ minetest.register_node("digtron:dual_soft_digger", {
522526
end
523527
cost = cost + forward_cost
524528
end
525-
if protected_nodes:get(digdown.x, digdown.y, digdown.z) ~= true and digtron.is_soft_material(digdown) then
529+
530+
if not digtron.can_dig_pos(digdown, protected_nodes, nodes_dug) then
531+
return cost, items
532+
end
533+
534+
if digtron.is_soft_material(digdown) then
526535
local down_cost, down_items = digtron.mark_diggable(digdown, nodes_dug, player)
527536
if down_items ~= nil then
528537
for _, item in pairs(down_items) do
@@ -541,3 +550,4 @@ minetest.register_node("digtron:dual_soft_digger", {
541550
digtron.damage_creatures(player, pos, digtron.find_new_pos_downward(pos, facing), damage_hp_half, items_dropped)
542551
end,
543552
})
553+

util.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ digtron.can_build_to = function(pos, protected_nodes, dug_nodes)
111111
return false
112112
end
113113

114+
digtron.can_dig_pos = function(pos, protected_nodes, dug_nodes)
115+
return not (protected_nodes:get_pos(pos) or dug_nodes:get_pos(pos))
116+
end
117+
114118
digtron.can_move_to = function(pos, protected_nodes, dug_nodes)
115119
-- Same as can_build_to, but also checks if the current node is part of the digtron.
116120
-- this allows us to disregard obstructions that *will* move out of the way.
@@ -460,3 +464,4 @@ end
460464
digtron.protected_allow_metadata_inventory_take = function(pos, _, _, stack, player)
461465
return digtron.check_protected_and_record(pos, player) and 0 or stack:get_count()
462466
end
467+

0 commit comments

Comments
 (0)