Skip to content

Commit 6fdfd25

Browse files
tenplus1paramat
authored andcommitted
Tnt: Various optimisations
Pass nodename to tnt.burn function where possible to reduce use of 'get_node'. Change 'ipairs' to 'pairs'. Use 'nodeupdate_single(pos)' instead of 'nodeupdate(pos)' to avoid every node triggering recursion, the loop itself takes the place of recursion and works upwards through horizontal planes as required.
1 parent 53179b8 commit 6fdfd25

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

.luacheckrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ read_globals = {
55
"DIR_DELIM",
66
"minetest", "core",
77
"dump",
8-
"vector", "nodeupdate",
8+
"vector", "nodeupdate", "nodeupdate_single",
99
"VoxelManip", "VoxelArea",
1010
"PseudoRandom", "ItemStack",
1111
}

game_api.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ TNT API
290290
* `position` The center of explosion.
291291
* `definition` The TNT definion as passed to `tnt.register`
292292

293-
`tnt.burn(position)`
293+
`tnt.burn(position, [nodename])`
294294

295-
^ Ignite TNT at position
295+
^ Ignite TNT at position, nodename isn't required unless already known.
296296

297297

298298
To make dropping items from node inventories easier, you can use the

mods/tnt/init.lua

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ local function destroy(drops, npos, cid, c_air, c_fire, on_blast_queue, ignore_p
9999
return c_fire
100100
else
101101
local node_drops = minetest.get_node_drops(def.name, "")
102-
for _, item in ipairs(node_drops) do
102+
for _, item in pairs(node_drops) do
103103
add_drop(drops, item)
104104
end
105105
return c_air
@@ -181,7 +181,7 @@ local function entity_physics(pos, radius, drops)
181181
}, nil)
182182
end
183183
end
184-
for _, item in ipairs(entity_drops) do
184+
for _, item in pairs(entity_drops) do
185185
add_drop(drops, item)
186186
end
187187
end
@@ -248,8 +248,8 @@ local function add_effects(pos, radius, drops)
248248
})
249249
end
250250

251-
function tnt.burn(pos)
252-
local name = minetest.get_node(pos).name
251+
function tnt.burn(pos, nodename)
252+
local name = nodename or minetest.get_node(pos).name
253253
local group = minetest.get_item_group(name, "tnt")
254254
if group > 0 then
255255
minetest.sound_play("tnt_ignite", {pos = pos})
@@ -333,24 +333,25 @@ local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast)
333333
vm:update_liquids()
334334

335335
-- call nodeupdate for everything within 1.5x blast radius
336+
for y = -radius * 1.5, radius * 1.5 do
336337
for z = -radius * 1.5, radius * 1.5 do
337338
for x = -radius * 1.5, radius * 1.5 do
338-
for y = -radius * 1.5, radius * 1.5 do
339-
local s = vector.add(pos, {x = x, y = y, z = z})
340-
local r = vector.distance(pos, s)
339+
local rad = {x = x, y = y, z = z}
340+
local s = vector.add(pos, rad)
341+
local r = vector.length(rad)
341342
if r / radius < 1.4 then
342-
nodeupdate(s)
343+
nodeupdate_single(s)
343344
end
344345
end
345346
end
346347
end
347348

348-
for _, queued_data in ipairs(on_blast_queue) do
349+
for _, queued_data in pairs(on_blast_queue) do
349350
local dist = math.max(1, vector.distance(queued_data.pos, pos))
350351
local intensity = (radius * radius) / (dist * dist)
351352
local node_drops = queued_data.on_blast(queued_data.pos, intensity)
352353
if node_drops then
353-
for _, item in ipairs(node_drops) do
354+
for _, item in pairs(node_drops) do
354355
add_drop(drops, item)
355356
end
356357
end
@@ -408,11 +409,11 @@ minetest.register_node("tnt:gunpowder", {
408409

409410
on_punch = function(pos, node, puncher)
410411
if puncher:get_wielded_item():get_name() == "default:torch" then
411-
tnt.burn(pos)
412+
tnt.burn(pos, node.name)
412413
end
413414
end,
414415
on_blast = function(pos, intensity)
415-
tnt.burn(pos)
416+
tnt.burn(pos, "tnt:gunpowder")
416417
end,
417418
})
418419

@@ -511,7 +512,9 @@ if enable_tnt then
511512
neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
512513
interval = 4,
513514
chance = 1,
514-
action = tnt.burn,
515+
action = function(pos, node)
516+
tnt.burn(pos, node.name)
517+
end,
515518
})
516519
end
517520

0 commit comments

Comments
 (0)