From 6f7505b6c613816988736e92d68ec77653cf2333 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Wed, 8 Feb 2023 03:12:55 +0000 Subject: [PATCH 1/4] Change kelp to use ABMs to solve performance issues --- mods/ITEMS/mcl_ocean/kelp.lua | 133 +++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 50 deletions(-) diff --git a/mods/ITEMS/mcl_ocean/kelp.lua b/mods/ITEMS/mcl_ocean/kelp.lua index 5957bafb2..c5ac8cb22 100644 --- a/mods/ITEMS/mcl_ocean/kelp.lua +++ b/mods/ITEMS/mcl_ocean/kelp.lua @@ -76,7 +76,7 @@ kelp.MAX_AGE_QUEUE = 20 -- kelp.ROLL_GROWTH_DENOMINATOR = 100 * 1200 * kelp.ROLL_GROWTH_PRECISION kelp.ROLL_GROWTH_PRECISION = 1 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. 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); 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 function kelp.store_meta() @@ -242,10 +253,11 @@ function kelp.store_meta() for i=1,#kelp.age_queue do local pos_hash = kelp.age_queue[i] 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. - if pos then - mt_get_meta(pos):set_int("mcl_ocean:kelp_age", kelp.age_pool[pos_hash]) - end + store_age(pos, age) end kelp.age_queue = {} kelp.age_queue_pos = {} @@ -258,6 +270,7 @@ function kelp.store_age(age, pos, pos_hash) -- Optional params: pos_hash local pos_hash = pos_hash or mt_hash_node_position(pos) + -- TODO really needed? Store on meta... kelp.age_pool[pos_hash] = age if not kelp.age_queue_pos[pos_hash] then table.insert(kelp.age_queue, pos_hash) @@ -273,26 +286,28 @@ end function kelp.init_age(pos, age, pos_hash, meta) -- Watched params: pos -- 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 age = age + + local stored_age = retrieve_age(pos) + minetest.log("age: " .. tostring(age)) + minetest.log("stored_age: " .. tostring(stored_age)) + if age then - kelp.store_age(age, pos, pos_hash) - elseif not meta:contains("mcl_ocean:kelp_age") then + store_age(pos, age) + elseif not stored_age then age = kelp.roll_init_age() - kelp.store_age(age, pos, pos_hash) + store_age(pos, age) else - age = meta:get_int("mcl_ocean:kelp_age") - if not kelp.age_pool[pos_hash] then - kelp.age_pool[pos_hash] = age - end + age = stored_age end return age, pos_hash, meta end - +-- TODO remove -- Initialise kelp nodetimer. function kelp.init_timer(pos, 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 kelp.next_height(pos, node, pos_tip, node_tip, submerged, downward_flowing) - - return kelp.store_age(age, pos, pos_hash), node, pos_hash, pos_tip, node_tip, submerged, downward_flowing + --kelp.store_age(age, pos, pos_hash) + store_age(pos, age) + return true, pos_hash, node, pos_hash, pos_tip, node_tip, submerged, downward_flowing end @@ -427,22 +443,29 @@ end function kelp.surface_on_timer(pos) + --minetest.log("action", "Timer") local node = mt_get_node(pos) local pos_hash + + -- Update detahed kelps local dig_pos,_, height = kelp.find_unsubmerged(pos, node) if dig_pos then pos_hash = mt_hash_node_position(pos) 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.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 -- Grow kelp on chance if kelp.roll_growth() then 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 kelp.next_grow(age+1, pos, node, pos_hash) end @@ -452,15 +475,13 @@ function kelp.surface_on_timer(pos) end function kelp.surface_on_construct(pos) - local pos_hash = mt_hash_node_position(pos) - kelp.init_age(pos, nil, pos_hash) - kelp.init_timer(pos, pos_hash) + kelp.init_age(pos, nil) end function kelp.surface_on_destruct(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. if kelp.is_falling(pos, node) then @@ -468,13 +489,14 @@ function kelp.surface_on_destruct(pos) end -- Removes position from queue - kelp.age_queue_pos[pos_hash] = nil + --kelp.age_queue_pos[pos_hash] = nil end function kelp.surface_on_mvps_move(pos, node, oldpos, nodemeta) -- 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) end @@ -576,22 +598,24 @@ function kelp.kelp_on_place(itemstack, placer, pointed_thing) end -- 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 - kelp.init_age(pos_under, nil, pos_hash) - kelp.init_timer(pos_under, pos_hash) + kelp.init_age(pos_under, nil) + --kelp.init_timer(pos_under, pos_hash) 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 return itemstack end - +-- At load function kelp.lbm_register_nodetimer(pos, node) local pos_hash = mt_hash_node_position(pos) kelp.init_age(pos, nil, pos_hash) - kelp.init_timer(pos, pos_hash) + --kelp.init_timer(pos, pos_hash) end @@ -664,7 +688,7 @@ kelp.surface_deftemplate = { on_destruct = kelp.surface_on_destruct, on_dig = kelp.surface_on_dig, 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, }, drop = "", -- drops are handled in on_dig --_mcl_falling_node_alternative = is_falling and nodename or nil, @@ -812,34 +836,43 @@ minetest.register_craft({ }) -- Global registration ------------------------------------------------------------------------ - +--[[ minetest.register_lbm({ label = "Kelp initialise", name = "mcl_ocean:kelp_init", nodenames = { "group:kelp" }, run_at_every_load = true, -- so old kelps are also initialised action = kelp.lbm_register_nodetimer, -}) +})--]] -minetest.register_globalstep(kelp.globalstep) -minetest.register_on_shutdown(kelp.on_shutdown) +--minetest.register_globalstep(kelp.globalstep) +--minetest.register_on_shutdown(kelp.on_shutdown) -- NOTE: Old ABM implementation. --- minetest.register_abm({ --- label = "Kelp drops", --- nodenames = { "group:kelp" }, --- interval = 1.0, --- chance = 1, --- catch_up = false, --- action = surface_unsubmerged_abm, --- }) --- --- minetest.register_abm({ --- label = "Kelp growth", --- nodenames = { "group:kelp" }, --- interval = 45, --- chance = 12, --- catch_up = false, --- action = grow_abm, --- }) +--[[ +minetest.register_abm({ + label = "Kelp drops", + nodenames = { "group:kelp" }, + interval = 1.0, + chance = 1, + catch_up = false, + action = surface_unsubmerged_abm, +}) +--]] + +-- 50% growth over a minute https://minecraft.fandom.com/wiki/Tutorials/Kelp_farming +-- 14% chance every random tick +-- On average, blocks are updated every 68.27 seconds (1365.33 game ticks) +-- 1 in 7 every 68 +-- 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, +}) From a1f10205f6a2715c867e0fc8a1f09699b368ccfc Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Wed, 8 Feb 2023 16:17:44 +0000 Subject: [PATCH 2/4] Init age on plant so not all kelp grow to top of ocean --- mods/ITEMS/mcl_ocean/kelp.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mods/ITEMS/mcl_ocean/kelp.lua b/mods/ITEMS/mcl_ocean/kelp.lua index c5ac8cb22..8b84368e8 100644 --- a/mods/ITEMS/mcl_ocean/kelp.lua +++ b/mods/ITEMS/mcl_ocean/kelp.lua @@ -467,7 +467,10 @@ function kelp.surface_on_timer(pos) local age = retrieve_age(pos) if kelp.is_age_growable(age) then + minetest.log("age growable: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos)) kelp.next_grow(age+1, pos, node, pos_hash) + else + minetest.log("age not: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos)) end end @@ -599,12 +602,14 @@ function kelp.kelp_on_place(itemstack, placer, pointed_thing) -- Initialize age and timer when it's planted on a new surface. --local pos_hash = mt_hash_node_position(pos_under) + local init_age = kelp.roll_init_age() + if new_surface then - kelp.init_age(pos_under, nil) + kelp.init_age(pos_under, init_age) --kelp.init_timer(pos_under, pos_hash) else -- TODO needed? - store_age(pos_under, kelp.roll_init_age()) + store_age(pos_under, init_age) --kelp.store_age(kelp.roll_init_age(), pos_under, pos_hash) end @@ -871,8 +876,8 @@ minetest.register_abm({ minetest.register_abm({ label = "Kelp growth", nodenames = { "group:kelp" }, - interval = 17, --17 target was 45 - chance = 28, -- 3 target was 12 + interval = 17, --17 + chance = 28, catch_up = false, action = kelp.surface_on_timer, }) From b8b45210e9de50c1133a371a543be252bcbe15d1 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Wed, 8 Feb 2023 21:03:26 +0000 Subject: [PATCH 3/4] Clean up and remove old code --- mods/ITEMS/mcl_ocean/kelp.lua | 189 ++++++---------------------------- 1 file changed, 31 insertions(+), 158 deletions(-) diff --git a/mods/ITEMS/mcl_ocean/kelp.lua b/mods/ITEMS/mcl_ocean/kelp.lua index 8b84368e8..1a4a4f208 100644 --- a/mods/ITEMS/mcl_ocean/kelp.lua +++ b/mods/ITEMS/mcl_ocean/kelp.lua @@ -25,7 +25,6 @@ local mt_get_node = minetest.get_node local mt_get_node_level = minetest.get_node_level local mt_get_node_max_level = minetest.get_node_max_level local mt_get_node_or_nil = minetest.get_node_or_nil -local mt_get_node_timer = minetest.get_node_timer local mt_get_meta = minetest.get_meta local mt_hash_node_position = minetest.hash_node_position local mt_set_node = minetest.set_node @@ -52,18 +51,11 @@ local table = table local kelp = {} mcl_ocean.kelp = kelp --- Kelp minimum and maximum age. Once reached the maximum, kelp no longer grows. +-- Once reach the maximum, kelp no longer grows. kelp.MIN_AGE = 0 kelp.MAX_AGE = 25 --- Tick interval (in seconds) for updating kelp. -kelp.TICK = 0.2 - --- Tick interval (in seconds) to store kelp meta. -kelp.META_TICK = 2 - --- Max age queue length -kelp.MAX_AGE_QUEUE = 20 +kelp.TICK = 0.2 -- Tick interval (in seconds) for updating kelp. -- The average amount of growth for kelp in a day is 2.16 (https://youtu.be/5Bp4lAjAk3I) -- Normally, a day lasts 20 minutes, meaning kelp.next_grow() is executed @@ -81,19 +73,6 @@ kelp.ROLL_GROWTH_DENOMINATOR = 1 -- 100 * 1200 -- Sounds used to dig and place kelp. kelp.leaf_sounds = mcl_sounds.node_sound_leaves_defaults() --- Pool storing nodetimers -kelp.timers_pool = {} - --- Pool storing age, indexed by pos_hash. -kelp.age_pool = {} - --- Queue(List) of hashed positions to save their ages. --- Invalid ones may still persist in this queue. -kelp.age_queue = {} --- Stores only valid positions of each hashed postiions. -kelp.age_queue_pos = {} - - -- is age in the growable range? function kelp.is_age_growable(age) return age >= 0 and age < kelp.MAX_AGE @@ -242,46 +221,6 @@ local function retrieve_age (pos) return meta:get_int("mcl_ocean:kelp_age") end --- Stores age from kelp.age_queue* into their respective meta -function kelp.store_meta() - local count = 0 - for _ in pairs(kelp.age_queue_pos) do - count = count + 1 - end - -- chatlog(string.format("Storing age metadata: %d in queue", #kelp.age_queue)) - -- chatlog(string.format("Storing age metadata: %d valid in queue", count)) - for i=1,#kelp.age_queue do - local pos_hash = kelp.age_queue[i] - 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. - store_age(pos, age) - end - kelp.age_queue = {} - kelp.age_queue_pos = {} -end - - --- Store and queue a kelp's age to be saved into meta later. -function kelp.store_age(age, pos, pos_hash) - -- Watched params: pos - -- Optional params: pos_hash - local pos_hash = pos_hash or mt_hash_node_position(pos) - - -- TODO really needed? Store on meta... - kelp.age_pool[pos_hash] = age - if not kelp.age_queue_pos[pos_hash] then - table.insert(kelp.age_queue, pos_hash) - kelp.age_queue_pos[pos_hash] = pos - return true, pos_hash - end - - return false, pos_hash -end - - -- Initialise a kelp's age. function kelp.init_age(pos, age, pos_hash, meta) -- Watched params: pos @@ -307,25 +246,6 @@ function kelp.init_age(pos, age, pos_hash, meta) return age, pos_hash, meta end --- TODO remove --- Initialise kelp nodetimer. -function kelp.init_timer(pos, pos_hash) - -- Optional params: pos_hash - local pos_hash = pos_hash or mt_hash_node_position(pos) - - local timer = kelp.timers_pool[pos_hash] - if not timer then - timer = mt_get_node_timer(pos) - kelp.timers_pool[pos_hash] = timer - end - if not timer:is_started() then - timer:start(kelp.TICK) - end - - return pos_hash -end - - -- Apply next kelp height. The surface is swapped. so on_construct is skipped. function kelp.next_height(pos, node, pos_tip, node_tip, submerged, downward_flowing) -- Modified params: node @@ -441,39 +361,48 @@ function kelp.surface_after_dig_node(pos, node) return mt_set_node(pos, {name=minetest.registered_nodes[node.name].node_dig_prediction}) end +-- NOTE: Old ABM implementation. +-- TODO have node already +-- local function surface_unsubmerged_abm(pos, node) +-- local dig_pos = find_unsubmerged(pos, node) +-- if dig_pos then +-- detach_dig(dig_pos, pos, node, true) +-- end +-- return true +-- end -function kelp.surface_on_timer(pos) - --minetest.log("action", "Timer") +local function detach_unsubmerged(pos) local node = mt_get_node(pos) - local pos_hash - - - -- Update detahed kelps local dig_pos,_, height = kelp.find_unsubmerged(pos, node) if dig_pos then - pos_hash = mt_hash_node_position(pos) 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) local new_age = kelp.roll_init_age() store_age(pos, new_age) - --kelp.store_age(new_age, pos, pos_hash) end +end + +local function grow_kelp (pos) + local node = mt_get_node(pos) - -- Grow kelp on chance if kelp.roll_growth() then - pos_hash = pos_hash or mt_hash_node_position(pos) - --local age = kelp.age_pool[pos_hash] - local age = retrieve_age(pos) + if kelp.is_age_growable(age) then - minetest.log("age growable: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos)) - kelp.next_grow(age+1, pos, node, pos_hash) + --minetest.log("age growable: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos)) + kelp.next_grow(age+1, pos, node) else - minetest.log("age not: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos)) + --minetest.log("age not: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos)) end end +end +function kelp.surface_on_timer(pos) + --minetest.log("action", "Timer") + detach_unsubmerged(pos) + + grow_kelp (pos) return true end @@ -484,15 +413,12 @@ end function kelp.surface_on_destruct(pos) local node = mt_get_node(pos) - --local pos_hash = mt_hash_node_position(pos) -- on_falling callback. Activated by pistons for falling nodes too. + -- I'm not sure this works. I think piston digs water and the unsubmerged nature drops kelp. if kelp.is_falling(pos, node) then kelp.detach_drop(pos, kelp.get_height(node.param2)) end - - -- Removes position from queue - --kelp.age_queue_pos[pos_hash] = nil end @@ -504,16 +430,6 @@ function kelp.surface_on_mvps_move(pos, node, oldpos, nodemeta) end --- NOTE: Old ABM implementation. --- local function surface_unsubmerged_abm(pos, node) --- local dig_pos = find_unsubmerged(pos, node) --- if dig_pos then --- detach_dig(dig_pos, pos, node, true) --- end --- return true --- end - - function kelp.kelp_on_place(itemstack, placer, pointed_thing) if pointed_thing.type ~= "node" or not placer then return itemstack @@ -601,21 +517,18 @@ function kelp.kelp_on_place(itemstack, placer, pointed_thing) end -- Initialize age and timer when it's planted on a new surface. - --local pos_hash = mt_hash_node_position(pos_under) local init_age = kelp.roll_init_age() if new_surface then kelp.init_age(pos_under, init_age) - --kelp.init_timer(pos_under, pos_hash) else - -- TODO needed? store_age(pos_under, init_age) - --kelp.store_age(kelp.roll_init_age(), pos_under, pos_hash) end return itemstack end +-- TODO ensure freshmap kelp has age. maybe set if it doesn't already have on grow -- At load function kelp.lbm_register_nodetimer(pos, node) local pos_hash = mt_hash_node_position(pos) @@ -624,30 +537,6 @@ function kelp.lbm_register_nodetimer(pos, node) end -local gstep_time = 0 -function kelp.globalstep(dtime) - if #kelp.age_queue > kelp.MAX_AGE_QUEUE then - kelp.store_meta() - end - - gstep_time = gstep_time + dtime - if gstep_time < kelp.META_TICK then - return - end - gstep_time = 0 - - if #kelp.age_queue > 0 then - kelp.store_meta() - end -end - - -function kelp.on_shutdown() - if #kelp.age_queue > 0 then - kelp.store_meta() - end -end - -------------------------------------------------------------------------------- -- Kelp registration API -------------------------------------------------------------------------------- @@ -840,31 +729,15 @@ minetest.register_craft({ burntime = 200, }) --- Global registration ------------------------------------------------------------------------ ---[[ -minetest.register_lbm({ - label = "Kelp initialise", - name = "mcl_ocean:kelp_init", - nodenames = { "group:kelp" }, - run_at_every_load = true, -- so old kelps are also initialised - action = kelp.lbm_register_nodetimer, -})--]] - - ---minetest.register_globalstep(kelp.globalstep) ---minetest.register_on_shutdown(kelp.on_shutdown) - --- NOTE: Old ABM implementation. ---[[ minetest.register_abm({ label = "Kelp drops", nodenames = { "group:kelp" }, interval = 1.0, chance = 1, catch_up = false, - action = surface_unsubmerged_abm, + action = detach_unsubmerged, --surface_unsubmerged_abm, }) ---]] + -- 50% growth over a minute https://minecraft.fandom.com/wiki/Tutorials/Kelp_farming -- 14% chance every random tick @@ -879,5 +752,5 @@ minetest.register_abm({ interval = 17, --17 chance = 28, catch_up = false, - action = kelp.surface_on_timer, + action = grow_kelp, }) From e3c8d995b583e034b927d29538f2349088147df9 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Wed, 8 Feb 2023 22:53:23 +0000 Subject: [PATCH 4/4] Finish clean up# --- mods/ITEMS/mcl_ocean/kelp.lua | 92 +++++++++++++++++------------------ 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/mods/ITEMS/mcl_ocean/kelp.lua b/mods/ITEMS/mcl_ocean/kelp.lua index 1a4a4f208..8827546a3 100644 --- a/mods/ITEMS/mcl_ocean/kelp.lua +++ b/mods/ITEMS/mcl_ocean/kelp.lua @@ -26,7 +26,6 @@ local mt_get_node_level = minetest.get_node_level local mt_get_node_max_level = minetest.get_node_max_level local mt_get_node_or_nil = minetest.get_node_or_nil local mt_get_meta = minetest.get_meta -local mt_hash_node_position = minetest.hash_node_position local mt_set_node = minetest.set_node local mt_swap_node = minetest.swap_node local mt_pos_to_string = minetest.pos_to_string @@ -68,7 +67,7 @@ kelp.TICK = 0.2 -- Tick interval (in seconds) for updating kelp. -- kelp.ROLL_GROWTH_DENOMINATOR = 100 * 1200 * kelp.ROLL_GROWTH_PRECISION kelp.ROLL_GROWTH_PRECISION = 1 kelp.ROLL_GROWTH_NUMERATOR = 216 * kelp.TICK -kelp.ROLL_GROWTH_DENOMINATOR = 1 -- 100 * 1200 +kelp.ROLL_GROWTH_DENOMINATOR = 100 * 1200 -- Sounds used to dig and place kelp. kelp.leaf_sounds = mcl_sounds.node_sound_leaves_defaults() @@ -156,7 +155,8 @@ end -- Roll whether to grow kelp or not. function kelp.roll_growth(numerator, denominator) -- Optional params: numerator, denominator - return math.random(denominator or kelp.ROLL_GROWTH_DENOMINATOR) <= (numerator or kelp.ROLL_GROWTH_NUMERATOR) + --return math.random(denominator or kelp.ROLL_GROWTH_DENOMINATOR) <= (numerator or kelp.ROLL_GROWTH_NUMERATOR) + return true -- probability done by ABM end @@ -211,39 +211,46 @@ end local function store_age (pos, age) if pos then - minetest.log("age: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos)) + --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 function retrieve_age (pos, include_nil) local meta = mt_get_meta(pos) + + if include_nil then + local age_set = meta:contains("mcl_ocean:kelp_age") + if not age_set then + return nil + end + end return meta:get_int("mcl_ocean:kelp_age") end -- Initialise a kelp's age. -function kelp.init_age(pos, age, pos_hash, meta) +function kelp.init_age(pos, age, from_lbm) -- Watched params: pos - -- Optional params: age, pos_hash, meta - --local pos_hash = pos_hash or mt_hash_node_position(pos) - local meta = meta or mt_get_meta(pos) + -- Optional params: age, from_lbm - local age = age + local new_age - local stored_age = retrieve_age(pos) - minetest.log("age: " .. tostring(age)) - minetest.log("stored_age: " .. tostring(stored_age)) + local stored_age = retrieve_age(pos, from_lbm) if age then + --minetest.log("age: " .. tostring(age)) store_age(pos, age) + new_age = age elseif not stored_age then - age = kelp.roll_init_age() - store_age(pos, age) + new_age = kelp.roll_init_age() + --minetest.log("no kelp age set so init with: " .. tostring(new_age)) + store_age(pos, new_age) else - age = stored_age + --minetest.log("stored_age: " .. tostring(stored_age)) + new_age = stored_age end - return age, pos_hash, meta + return new_age end -- Apply next kelp height. The surface is swapped. so on_construct is skipped. @@ -277,12 +284,11 @@ end -- Grow next kelp. -function kelp.next_grow(age, pos, node, pos_hash, pos_tip, node_tip, submerged, downward_flowing) +function kelp.next_grow(age, pos, node, pos_tip, node_tip, submerged, downward_flowing) -- Watched params: pos -- Modified params: node - -- Optional params: node, pos_hash, pos_tip, node_tip, submerged, downward_flowing + -- Optional params: node, pos_tip, node_tip, submerged, downward_flowing local node = node or mt_get_node(pos) - local pos_hash = pos_hash or mt_hash_node_position(pos) local pos_tip = pos_tip local node_tip = node_tip or (pos_tip and mt_get_node(pos_tip)) if not pos_tip then @@ -296,9 +302,8 @@ function kelp.next_grow(age, pos, node, pos_hash, pos_tip, node_tip, submerged, end kelp.next_height(pos, node, pos_tip, node_tip, submerged, downward_flowing) - --kelp.store_age(age, pos, pos_hash) store_age(pos, age) - return true, pos_hash, node, pos_hash, pos_tip, node_tip, submerged, downward_flowing + return true, node, pos_tip, node_tip, submerged, downward_flowing end @@ -356,20 +361,10 @@ function kelp.surface_on_dig(pos, node, digger) kelp.detach_dig(pos, pos, true, node) end - function kelp.surface_after_dig_node(pos, node) return mt_set_node(pos, {name=minetest.registered_nodes[node.name].node_dig_prediction}) end --- NOTE: Old ABM implementation. --- TODO have node already --- local function surface_unsubmerged_abm(pos, node) --- local dig_pos = find_unsubmerged(pos, node) --- if dig_pos then --- detach_dig(dig_pos, pos, node, true) --- end --- return true --- end local function detach_unsubmerged(pos) local node = mt_get_node(pos) @@ -389,6 +384,11 @@ local function grow_kelp (pos) if kelp.roll_growth() then local age = retrieve_age(pos) + if not age then + --minetest.log("init a new age as not set: " .. mt_pos_to_string(pos)) + kelp.init_age(pos, nil) + end + if kelp.is_age_growable(age) then --minetest.log("age growable: ".. tostring(age) .. ", pos: ".. mt_pos_to_string(pos)) kelp.next_grow(age+1, pos, node) @@ -398,15 +398,8 @@ local function grow_kelp (pos) end end -function kelp.surface_on_timer(pos) - --minetest.log("action", "Timer") - detach_unsubmerged(pos) - - grow_kelp (pos) - return true -end - function kelp.surface_on_construct(pos) + --minetest.log("on construct kelp called") kelp.init_age(pos, nil) end @@ -425,7 +418,7 @@ end function kelp.surface_on_mvps_move(pos, node, oldpos, nodemeta) -- Pistons moving falling nodes will have already activated on_falling callback. - minetest.log("kelp.surface_on_mvps_move: " .. mt_pos_to_string(pos)) + --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) end @@ -528,14 +521,18 @@ function kelp.kelp_on_place(itemstack, placer, pointed_thing) return itemstack end --- TODO ensure freshmap kelp has age. maybe set if it doesn't already have on grow --- At load -function kelp.lbm_register_nodetimer(pos, node) - local pos_hash = mt_hash_node_position(pos) - kelp.init_age(pos, nil, pos_hash) - --kelp.init_timer(pos, pos_hash) +function kelp.lbm_register(pos) + kelp.init_age(pos, nil, true) end +minetest.register_lbm({ + label = "Kelp initialise", + name = "mcl_ocean:kelp_init", + nodenames = { "group:kelp" }, + run_at_every_load = true, -- so old kelps are also initialised + action = kelp.lbm_register, +}) + -------------------------------------------------------------------------------- -- Kelp registration API @@ -582,7 +579,6 @@ kelp.surface_deftemplate = { on_destruct = kelp.surface_on_destruct, on_dig = kelp.surface_on_dig, after_dig_node = kelp.surface_after_dig_node, - --on_timer = kelp.surface_on_timer, mesecon = { on_mvps_move = kelp.surface_on_mvps_move, }, drop = "", -- drops are handled in on_dig --_mcl_falling_node_alternative = is_falling and nodename or nil,