1
0
Fork 0

Change kelp to use ABMs to solve performance issues

This commit is contained in:
ancientmarinerdev 2023-02-08 03:12:55 +00:00 committed by Gitea
parent e323ab6e88
commit 6f7505b6c6
1 changed files with 83 additions and 50 deletions

View File

@ -76,7 +76,7 @@ kelp.MAX_AGE_QUEUE = 20
-- kelp.ROLL_GROWTH_DENOMINATOR = 100 * 1200 * kelp.ROLL_GROWTH_PRECISION -- kelp.ROLL_GROWTH_DENOMINATOR = 100 * 1200 * kelp.ROLL_GROWTH_PRECISION
kelp.ROLL_GROWTH_PRECISION = 1 kelp.ROLL_GROWTH_PRECISION = 1
kelp.ROLL_GROWTH_NUMERATOR = 216 * kelp.TICK kelp.ROLL_GROWTH_NUMERATOR = 216 * kelp.TICK
kelp.ROLL_GROWTH_DENOMINATOR = 100 * 1200 kelp.ROLL_GROWTH_DENOMINATOR = 1 -- 100 * 1200
-- Sounds used to dig and place kelp. -- Sounds used to dig and place kelp.
kelp.leaf_sounds = mcl_sounds.node_sound_leaves_defaults() kelp.leaf_sounds = mcl_sounds.node_sound_leaves_defaults()
@ -230,6 +230,17 @@ function kelp.next_param2(param2)
return math.min(param2+16 - param2 % 16, 255); return math.min(param2+16 - param2 % 16, 255);
end end
local function store_age (pos, age)
if pos then
minetest.log("age: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos))
mt_get_meta(pos):set_int("mcl_ocean:kelp_age", age)
end
end
local function retrieve_age (pos)
local meta = mt_get_meta(pos)
return meta:get_int("mcl_ocean:kelp_age")
end
-- Stores age from kelp.age_queue* into their respective meta -- Stores age from kelp.age_queue* into their respective meta
function kelp.store_meta() function kelp.store_meta()
@ -242,10 +253,11 @@ function kelp.store_meta()
for i=1,#kelp.age_queue do for i=1,#kelp.age_queue do
local pos_hash = kelp.age_queue[i] local pos_hash = kelp.age_queue[i]
local pos = kelp.age_queue_pos[pos_hash] local pos = kelp.age_queue_pos[pos_hash]
local age = kelp.age_pool[pos_hash]
-- queued hashes may no longer point to a valid pos, e.g. kelp is destroyed. -- queued hashes may no longer point to a valid pos, e.g. kelp is destroyed.
if pos then store_age(pos, age)
mt_get_meta(pos):set_int("mcl_ocean:kelp_age", kelp.age_pool[pos_hash])
end
end end
kelp.age_queue = {} kelp.age_queue = {}
kelp.age_queue_pos = {} kelp.age_queue_pos = {}
@ -258,6 +270,7 @@ function kelp.store_age(age, pos, pos_hash)
-- Optional params: pos_hash -- Optional params: pos_hash
local pos_hash = pos_hash or mt_hash_node_position(pos) local pos_hash = pos_hash or mt_hash_node_position(pos)
-- TODO really needed? Store on meta...
kelp.age_pool[pos_hash] = age kelp.age_pool[pos_hash] = age
if not kelp.age_queue_pos[pos_hash] then if not kelp.age_queue_pos[pos_hash] then
table.insert(kelp.age_queue, pos_hash) table.insert(kelp.age_queue, pos_hash)
@ -273,26 +286,28 @@ end
function kelp.init_age(pos, age, pos_hash, meta) function kelp.init_age(pos, age, pos_hash, meta)
-- Watched params: pos -- Watched params: pos
-- Optional params: age, pos_hash, meta -- Optional params: age, pos_hash, meta
local pos_hash = pos_hash or mt_hash_node_position(pos) --local pos_hash = pos_hash or mt_hash_node_position(pos)
local meta = meta or mt_get_meta(pos) local meta = meta or mt_get_meta(pos)
local age = age local age = age
local stored_age = retrieve_age(pos)
minetest.log("age: " .. tostring(age))
minetest.log("stored_age: " .. tostring(stored_age))
if age then if age then
kelp.store_age(age, pos, pos_hash) store_age(pos, age)
elseif not meta:contains("mcl_ocean:kelp_age") then elseif not stored_age then
age = kelp.roll_init_age() age = kelp.roll_init_age()
kelp.store_age(age, pos, pos_hash) store_age(pos, age)
else else
age = meta:get_int("mcl_ocean:kelp_age") age = stored_age
if not kelp.age_pool[pos_hash] then
kelp.age_pool[pos_hash] = age
end
end end
return age, pos_hash, meta return age, pos_hash, meta
end end
-- TODO remove
-- Initialise kelp nodetimer. -- Initialise kelp nodetimer.
function kelp.init_timer(pos, pos_hash) function kelp.init_timer(pos, pos_hash)
-- Optional params: pos_hash -- Optional params: pos_hash
@ -361,8 +376,9 @@ function kelp.next_grow(age, pos, node, pos_hash, pos_tip, node_tip, submerged,
end end
kelp.next_height(pos, node, pos_tip, node_tip, submerged, downward_flowing) kelp.next_height(pos, node, pos_tip, node_tip, submerged, downward_flowing)
--kelp.store_age(age, pos, pos_hash)
return kelp.store_age(age, pos, pos_hash), node, pos_hash, pos_tip, node_tip, submerged, downward_flowing store_age(pos, age)
return true, pos_hash, node, pos_hash, pos_tip, node_tip, submerged, downward_flowing
end end
@ -427,22 +443,29 @@ end
function kelp.surface_on_timer(pos) function kelp.surface_on_timer(pos)
--minetest.log("action", "Timer")
local node = mt_get_node(pos) local node = mt_get_node(pos)
local pos_hash local pos_hash
-- Update detahed kelps -- Update detahed kelps
local dig_pos,_, height = kelp.find_unsubmerged(pos, node) local dig_pos,_, height = kelp.find_unsubmerged(pos, node)
if dig_pos then if dig_pos then
pos_hash = mt_hash_node_position(pos) pos_hash = mt_hash_node_position(pos)
mt_sound_play(mt_registered_nodes[node.name].sounds.dug, { gain = 0.5, pos = dig_pos }, true) mt_sound_play(mt_registered_nodes[node.name].sounds.dug, { gain = 0.5, pos = dig_pos }, true)
kelp.detach_dig(dig_pos, pos, true, node, height) kelp.detach_dig(dig_pos, pos, true, node, height)
kelp.store_age(kelp.roll_init_age(), pos, pos_hash) local new_age = kelp.roll_init_age()
store_age(pos, new_age)
--kelp.store_age(new_age, pos, pos_hash)
end end
-- Grow kelp on chance -- Grow kelp on chance
if kelp.roll_growth() then if kelp.roll_growth() then
pos_hash = pos_hash or mt_hash_node_position(pos) pos_hash = pos_hash or mt_hash_node_position(pos)
local age = kelp.age_pool[pos_hash] --local age = kelp.age_pool[pos_hash]
local age = retrieve_age(pos)
if kelp.is_age_growable(age) then if kelp.is_age_growable(age) then
kelp.next_grow(age+1, pos, node, pos_hash) kelp.next_grow(age+1, pos, node, pos_hash)
end end
@ -452,15 +475,13 @@ function kelp.surface_on_timer(pos)
end end
function kelp.surface_on_construct(pos) function kelp.surface_on_construct(pos)
local pos_hash = mt_hash_node_position(pos) kelp.init_age(pos, nil)
kelp.init_age(pos, nil, pos_hash)
kelp.init_timer(pos, pos_hash)
end end
function kelp.surface_on_destruct(pos) function kelp.surface_on_destruct(pos)
local node = mt_get_node(pos) local node = mt_get_node(pos)
local pos_hash = mt_hash_node_position(pos) --local pos_hash = mt_hash_node_position(pos)
-- on_falling callback. Activated by pistons for falling nodes too. -- on_falling callback. Activated by pistons for falling nodes too.
if kelp.is_falling(pos, node) then if kelp.is_falling(pos, node) then
@ -468,13 +489,14 @@ function kelp.surface_on_destruct(pos)
end end
-- Removes position from queue -- Removes position from queue
kelp.age_queue_pos[pos_hash] = nil --kelp.age_queue_pos[pos_hash] = nil
end end
function kelp.surface_on_mvps_move(pos, node, oldpos, nodemeta) function kelp.surface_on_mvps_move(pos, node, oldpos, nodemeta)
-- Pistons moving falling nodes will have already activated on_falling callback. -- Pistons moving falling nodes will have already activated on_falling callback.
minetest.log("kelp.surface_on_mvps_move: " .. mt_pos_to_string(pos))
kelp.detach_dig(pos, pos, mt_get_item_group(node.name, "falling_node") ~= 1, node) kelp.detach_dig(pos, pos, mt_get_item_group(node.name, "falling_node") ~= 1, node)
end end
@ -576,22 +598,24 @@ function kelp.kelp_on_place(itemstack, placer, pointed_thing)
end end
-- Initialize age and timer when it's planted on a new surface. -- Initialize age and timer when it's planted on a new surface.
local pos_hash = mt_hash_node_position(pos_under) --local pos_hash = mt_hash_node_position(pos_under)
if new_surface then if new_surface then
kelp.init_age(pos_under, nil, pos_hash) kelp.init_age(pos_under, nil)
kelp.init_timer(pos_under, pos_hash) --kelp.init_timer(pos_under, pos_hash)
else else
kelp.store_age(kelp.roll_init_age(), pos_under, pos_hash) -- TODO needed?
store_age(pos_under, kelp.roll_init_age())
--kelp.store_age(kelp.roll_init_age(), pos_under, pos_hash)
end end
return itemstack return itemstack
end end
-- At load
function kelp.lbm_register_nodetimer(pos, node) function kelp.lbm_register_nodetimer(pos, node)
local pos_hash = mt_hash_node_position(pos) local pos_hash = mt_hash_node_position(pos)
kelp.init_age(pos, nil, pos_hash) kelp.init_age(pos, nil, pos_hash)
kelp.init_timer(pos, pos_hash) --kelp.init_timer(pos, pos_hash)
end end
@ -664,7 +688,7 @@ kelp.surface_deftemplate = {
on_destruct = kelp.surface_on_destruct, on_destruct = kelp.surface_on_destruct,
on_dig = kelp.surface_on_dig, on_dig = kelp.surface_on_dig,
after_dig_node = kelp.surface_after_dig_node, after_dig_node = kelp.surface_after_dig_node,
on_timer = kelp.surface_on_timer, --on_timer = kelp.surface_on_timer,
mesecon = { on_mvps_move = kelp.surface_on_mvps_move, }, mesecon = { on_mvps_move = kelp.surface_on_mvps_move, },
drop = "", -- drops are handled in on_dig drop = "", -- drops are handled in on_dig
--_mcl_falling_node_alternative = is_falling and nodename or nil, --_mcl_falling_node_alternative = is_falling and nodename or nil,
@ -812,34 +836,43 @@ minetest.register_craft({
}) })
-- Global registration ------------------------------------------------------------------------ -- Global registration ------------------------------------------------------------------------
--[[
minetest.register_lbm({ minetest.register_lbm({
label = "Kelp initialise", label = "Kelp initialise",
name = "mcl_ocean:kelp_init", name = "mcl_ocean:kelp_init",
nodenames = { "group:kelp" }, nodenames = { "group:kelp" },
run_at_every_load = true, -- so old kelps are also initialised run_at_every_load = true, -- so old kelps are also initialised
action = kelp.lbm_register_nodetimer, action = kelp.lbm_register_nodetimer,
}) })--]]
minetest.register_globalstep(kelp.globalstep) --minetest.register_globalstep(kelp.globalstep)
minetest.register_on_shutdown(kelp.on_shutdown) --minetest.register_on_shutdown(kelp.on_shutdown)
-- NOTE: Old ABM implementation. -- NOTE: Old ABM implementation.
-- minetest.register_abm({ --[[
-- label = "Kelp drops", minetest.register_abm({
-- nodenames = { "group:kelp" }, label = "Kelp drops",
-- interval = 1.0, nodenames = { "group:kelp" },
-- chance = 1, interval = 1.0,
-- catch_up = false, chance = 1,
-- action = surface_unsubmerged_abm, catch_up = false,
-- }) action = surface_unsubmerged_abm,
-- })
-- minetest.register_abm({ --]]
-- label = "Kelp growth",
-- nodenames = { "group:kelp" }, -- 50% growth over a minute https://minecraft.fandom.com/wiki/Tutorials/Kelp_farming
-- interval = 45, -- 14% chance every random tick
-- chance = 12, -- On average, blocks are updated every 68.27 seconds (1365.33 game ticks)
-- catch_up = false, -- 1 in 7 every 68
-- action = grow_abm, -- 1 in 28 every 17
-- }) -- 1 in 21 every 22
-- https://minecraft.fandom.com/wiki/Tick#Random_tick
minetest.register_abm({
label = "Kelp growth",
nodenames = { "group:kelp" },
interval = 17, --17 target was 45
chance = 28, -- 3 target was 12
catch_up = false,
action = kelp.surface_on_timer,
})