1
0
Fork 0

Optimize ABMs

This commit is contained in:
cora 2022-07-18 06:10:38 +02:00
parent 0298cecbcd
commit d89ef0581f
1 changed files with 36 additions and 28 deletions

View File

@ -3,45 +3,53 @@
-- License of code, textures & sounds: CC0 -- License of code, textures & sounds: CC0
local math = math local math = math
local function make_drop(pos,liquid,sound,interval)
local function register_drop(liquid, glow, sound, nodes) local pt = {
local pdef = {
velocity = vector.new(0,0,0), velocity = vector.new(0,0,0),
collision_removal = false, collision_removal = false,
} }
local t = math.random() + math.random(1, interval)
minetest.after(t,function()
local x, z = math.random(-45, 45) / 100, math.random(-45, 45) / 100
pt.pos = vector.offset(pos,x,-0.52,z)
pt.acceleration = vector.new(0,0,0)
pt.collisiondetection = false
pt.expirationtime = t
pt.texture="[combine:2x2:" .. -math.random(1, 16) .. "," .. -math.random(1, 16) .. "=default_" .. liquid .. "_source_animated.png"
minetest.add_particle(pt)
minetest.after(t,function()
pt.acceleration = vector.new(0,-5,0)
pt.collisiondetection = true
pt.expirationtime = math.random() + math.random(1, interval/2)
minetest.add_particle(pt)
minetest.sound_play({name = "drippingwater_" .. sound .. "drip"}, {pos = ownpos, gain = 0.5, max_hear_distance = 8}, true)
end)
end)
end
local function register_drop(liquid, glow, sound, nodes, interval, chance)
minetest.register_abm({ minetest.register_abm({
label = "Create drops", label = "Create drops",
nodenames = nodes, nodenames = nodes,
neighbors = {"group:" .. liquid}, neighbors = {"group:" .. liquid},
interval = 2, interval = interval,
chance = 22, chance = chance,
action = function(pos) action = function(pos)
if minetest.get_item_group(minetest.get_node(vector.offset(pos, 0, 1, 0)).name, liquid) ~= 0 local r = math.ceil(interval / 20)
and minetest.get_node(vector.offset(pos, 0, -1, 0)).name == "air" then local nn=minetest.find_nodes_in_area(vector.offset(pos,-r,0,-r),vector.offset(pos,r,0,r),nodes)
minetest.after(math.random(0.1,1.5),function() --start a bunch of particle cycles to be able to get away
local pt = table.copy(pdef) --with longer abm cycles
local x, z = math.random(-45, 45) / 100, math.random(-45, 45) / 100 table.shuffle(nn)
pt.pos = vector.offset(pos,x,-0.52,z) for i=1,math.random(#nn) do
pt.acceleration = vector.new(0,0,0) if minetest.get_item_group(minetest.get_node(vector.offset(nn[i], 0, 1, 0)).name, liquid) ~= 0
pt.collisiondetection = false and minetest.get_node(vector.offset(nn[i], 0, -1, 0)).name == "air" then
pt.expirationtime = math.random(9.5,28.5) make_drop(nn[i],liquid,sound,interval)
end
pt.texture="[combine:2x2:" .. -math.random(1, 16) .. "," .. -math.random(1, 16) .. "=default_" .. liquid .. "_source_animated.png"
minetest.add_particle(pt)
minetest.after(pt.expirationtime,function()
pt.acceleration = vector.new(0,-5,0)
pt.collisiondetection = true
pt.expirationtime = math.random(6.2,17.5)
minetest.add_particle(pt)
minetest.sound_play({name = "drippingwater_" .. sound .. "drip"}, {pos = ownpos, gain = 0.5, max_hear_distance = 8}, true)
end)
end)
end end
end, end,
}) })
end end
register_drop("water", 1, "", {"group:opaque", "group:leaves"}) register_drop("water", 1, "", {"group:opaque", "group:leaves"},60,10)
register_drop("lava", math.max(7, minetest.registered_nodes["mcl_core:lava_source"].light_source - 3), "lava", {"group:opaque"}) register_drop("lava", math.max(7, minetest.registered_nodes["mcl_core:lava_source"].light_source - 3), "lava", {"group:opaque"},60,10)