speedup mcl_particles code

This commit is contained in:
AFCMS 2021-05-25 09:09:16 +02:00
parent 42d3830c48
commit 3f3abbbee1

@ -1,3 +1,12 @@
local vector = vector
local table = table
local hash_node_position = minetest.hash_node_position
local add_particlespawner = minetest.add_particlespawner
local delete_particlespawner = minetest.delete_particlespawner
local ipairs = ipairs
mcl_particles = {} mcl_particles = {}
-- Table of particlespawner IDs on a per-node hash basis -- Table of particlespawner IDs on a per-node hash basis
@ -32,11 +41,11 @@ function mcl_particles.add_node_particlespawner(pos, particlespawner_definition,
if allowed_level == 0 or levels[level] > allowed_level then if allowed_level == 0 or levels[level] > allowed_level then
return return
end end
local poshash = minetest.hash_node_position(pos) local poshash = hash_node_position(pos)
if not poshash then if not poshash then
return return
end end
local id = minetest.add_particlespawner(particlespawner_definition) local id = add_particlespawner(particlespawner_definition)
if id == -1 then if id == -1 then
return return
end end
@ -47,6 +56,8 @@ function mcl_particles.add_node_particlespawner(pos, particlespawner_definition,
return id return id
end end
local add_node_particlespawner = mcl_particles.add_node_particlespawner
-- Deletes all particlespawners that are assigned to a node position. -- Deletes all particlespawners that are assigned to a node position.
-- If no particlespawners exist for this position, nothing happens. -- If no particlespawners exist for this position, nothing happens.
-- pos: Node positon. MUST use integer values! -- pos: Node positon. MUST use integer values!
@ -55,11 +66,11 @@ function mcl_particles.delete_node_particlespawners(pos)
if allowed_level == 0 then if allowed_level == 0 then
return false return false
end end
local poshash = minetest.hash_node_position(pos) local poshash = hash_node_position(pos)
local ids = particle_nodes[poshash] local ids = particle_nodes[poshash]
if ids then if ids then
for i=1, #ids do for i=1, #ids do
minetest.delete_particlespawner(ids[i]) delete_particlespawner(ids[i])
end end
particle_nodes[poshash] = nil particle_nodes[poshash] = nil
return true return true
@ -72,7 +83,6 @@ end
local smoke_pdef_cached = {} local smoke_pdef_cached = {}
function mcl_particles.spawn_smoke(pos, name, smoke_pdef_base) function mcl_particles.spawn_smoke(pos, name, smoke_pdef_base)
local min = math.min
local new_minpos = vector.add(pos, smoke_pdef_base.minrelpos) local new_minpos = vector.add(pos, smoke_pdef_base.minrelpos)
local new_maxpos = vector.add(pos, smoke_pdef_base.maxrelpos) local new_maxpos = vector.add(pos, smoke_pdef_base.maxrelpos)
@ -81,7 +91,7 @@ function mcl_particles.spawn_smoke(pos, name, smoke_pdef_base)
for i, smoke_pdef in ipairs(smoke_pdef_cached[name]) do for i, smoke_pdef in ipairs(smoke_pdef_cached[name]) do
smoke_pdef.minpos = new_minpos smoke_pdef.minpos = new_minpos
smoke_pdef.maxpos = new_maxpos smoke_pdef.maxpos = new_maxpos
mcl_particles.add_node_particlespawner(pos, smoke_pdef, "high") add_node_particlespawner(pos, smoke_pdef, "high")
end end
-- cache already populated -- cache already populated
else else
@ -111,11 +121,9 @@ function mcl_particles.spawn_smoke(pos, name, smoke_pdef_base)
smoke_pdef.animation.length = exptime + 0.1 smoke_pdef.animation.length = exptime + 0.1
-- minexptime must be set such that the last frame is actully rendered, -- minexptime must be set such that the last frame is actully rendered,
-- even if its very short. Larger exptime -> larger range -- even if its very short. Larger exptime -> larger range
smoke_pdef.minexptime = min(exptime, (7.0/8.0 * (exptime + 0.1) + 0.1)) smoke_pdef.minexptime = math.min(exptime, (7.0/8.0 * (exptime + 0.1) + 0.1))
smoke_pdef.texture = "mcl_particles_smoke_anim.png^[colorize:#000000:" ..colorize smoke_pdef.texture = "mcl_particles_smoke_anim.png^[colorize:#000000:" ..colorize
add_node_particlespawner(pos, smoke_pdef, "high")
mcl_particles.add_node_particlespawner(pos, smoke_pdef, "high")
table.insert(smoke_pdef_cached[name], table.copy(smoke_pdef)) table.insert(smoke_pdef_cached[name], table.copy(smoke_pdef))
end end
end end