From 40f5d6650541df1bf4f3c726f81a58cd13e2c19c Mon Sep 17 00:00:00 2001 From: Vanessa Ezekowitz Date: Sun, 8 Feb 2015 23:17:02 -0500 Subject: [PATCH] refactor nature_classic to avoid lag caused by its blossom abms --- nature_classic/blossom.lua | 57 +++++++++++------------------- nature_classic/config.lua | 4 +-- nature_classic/global_function.lua | 41 ++++++++++++++------- nature_classic/init.lua | 28 +++++++++++++++ 4 files changed, 78 insertions(+), 52 deletions(-) diff --git a/nature_classic/blossom.lua b/nature_classic/blossom.lua index fb1f3dd..5c7571a 100644 --- a/nature_classic/blossom.lua +++ b/nature_classic/blossom.lua @@ -1,21 +1,4 @@ --- Blossom - -local BLOSSOM_NODE = "nature:blossom" -local BLOSSOM_LEAVES = "default:leaves" -local BLOSSOM_TEXTURES = { "default_leaves.png^nature_blossom.png" } - -if minetest.get_modpath("moretrees") then - BLOSSOM_NODE = "moretrees:apple_blossoms" - BLOSSOM_LEAVES = "moretrees:apple_tree_leaves" - BLOSSOM_TEXTURES = { "moretrees_apple_tree_leaves.png^nature_blossom.png" } - minetest.register_alias("nature:blossom", "default:leaves") -end - -local BLOSSOM_CHANCE = 15 -local BLOSSOM_DELAY = 3600 - -local APPLE_CHANCE = 10 -local APPLE_SPREAD = 2 +-- Blossoms and such local function spawn_apple_under(pos) local below = { @@ -28,10 +11,10 @@ local function spawn_apple_under(pos) end end -minetest.register_node(":"..BLOSSOM_NODE, { +minetest.register_node(":"..nature.blossom_node, { description = "Apple blossoms", drawtype = "allfaces_optional", - tiles = BLOSSOM_TEXTURES, + tiles = nature.blossom_textures, paramtype = "light", groups = { snappy = 3, leafdecay = 3, flammable = 2 }, sounds = default.node_sound_leaves_defaults(), @@ -40,42 +23,42 @@ minetest.register_node(":"..BLOSSOM_NODE, { minetest.register_craft({ type = "fuel", - recipe = BLOSSOM_NODE, + recipe = nature.blossom_node, burntime = 2, }) --- Blossoming +-- these ABMs can get heavy, so just enqueue the nodes + +-- Adding Blossoms minetest.register_abm({ - nodenames = { BLOSSOM_LEAVES }, - interval = BLOSSOM_DELAY, - chance = BLOSSOM_CHANCE, + nodenames = { nature.blossom_leaves }, + interval = nature.blossom_delay, + chance = nature.blossom_chance, action = function(pos, node, active_object_count, active_object_count_wider) - if nature:is_near_water(pos) then - nature:grow_node(pos, BLOSSOM_NODE) - end + nature.enqueue_node(pos, node, true) end }) --- Removing blossom +-- Removing blossoms minetest.register_abm({ - nodenames = { BLOSSOM_NODE }, - interval = BLOSSOM_DELAY, - chance = BLOSSOM_CHANCE, + nodenames = { nature.blossom_node }, + interval = nature.blossom_delay, + chance = nature.blossom_chance, action = function(pos, node, active_object_count, active_object_count_wider) - nature:grow_node(pos, BLOSSOM_LEAVES) + nature.enqueue_node(pos, node, false) end }) -- Spawning apples minetest.register_abm({ - nodenames = { BLOSSOM_NODE }, - interval = BLOSSOM_DELAY, - chance = APPLE_CHANCE, + nodenames = { nature.blossom_node }, + interval = nature.blossom_delay, + chance = nature.apple_chance, action = function(pos, node, active_object_count, active_object_count_wider) - if not minetest.find_node_near(pos, APPLE_SPREAD, { "default:apple" }) then + if nature.dtime < 0.2 and not minetest.find_node_near(pos, nature.apple_spread, { "default:apple" }) then spawn_apple_under(pos) end end diff --git a/nature_classic/config.lua b/nature_classic/config.lua index 2862bc7..8f67b1c 100644 --- a/nature_classic/config.lua +++ b/nature_classic/config.lua @@ -1,6 +1,6 @@ -- Set on which distance from water can the tree still grow. -- Grows anywhere if set to -1. -DISTANCE_FROM_WATER = 20 +nature.distance_from_water = 20 -- Minimum light level needed to grow. Default is 8, which means daylight. -MINIMUM_GROWTH_LIGHT = 8 +nature.minimum_growth_light = 8 diff --git a/nature_classic/global_function.lua b/nature_classic/global_function.lua index 6d640c2..ec78942 100644 --- a/nature_classic/global_function.lua +++ b/nature_classic/global_function.lua @@ -1,20 +1,35 @@ -local NODE_YOUNG = "young" +-- helper functions -local SETTING_TRUE = "true" -local SETTING_FALSE = "false" +minetest.register_globalstep(function(dtime) + nature.dtime = dtime + if #nature.blossomqueue > 0 and dtime < 0.2 then + local pos = nature.blossomqueue[1][1] + local node = nature.blossomqueue[1][2] + if (nature.blossomqueue[1][3] and not nature:is_near_water(pos)) then + table.remove(nature.blossomqueue, 1) -- don't grow if it's not near water, pop from queue. + return + end + nature:grow_node(pos, nature.blossom_node) -- now actually grow it. + table.remove(nature.blossomqueue, 1) + end +end) -local YOUTH_DELAY = 5 - -nature = {} +function nature.enqueue_node(pos, node, fcn) + local idx = #nature.blossomqueue + nature.blossomqueue[idx+1] = {} + nature.blossomqueue[idx+1][1] = pos + nature.blossomqueue[idx+1][2] = node + nature.blossomqueue[idx+1][3] = fcn +end local function set_young_node(pos) local meta = minetest.get_meta(pos) - meta:set_string(NODE_YOUNG, SETTING_TRUE) - minetest.after(YOUTH_DELAY, + meta:set_string(nature.node_young, nature.setting_true) + minetest.after(nature.youth_delay, function(pos) local meta = minetest.get_meta(pos) - meta:set_string(NODE_YOUNG, SETTING_FALSE) + meta:set_string(nature.node_young, nature.setting_false) end, pos) end @@ -22,14 +37,14 @@ end local function is_not_young(pos) local meta = minetest.get_meta(pos) - local young = meta:get_string(NODE_YOUNG) - return young ~= SETTING_TRUE + local young = meta:get_string(nature.node_young) + return young ~= nature.setting_true end function nature:grow_node(pos, nodename) if pos ~= nil then local light_enough = minetest.get_node_light(pos, nil) - >= MINIMUM_GROWTH_LIGHT + >= nature.minimum_growth_light if is_not_young(pos) and light_enough then minetest.remove_node(pos) @@ -43,6 +58,6 @@ function nature:grow_node(pos, nodename) end function nature:is_near_water(pos) - return DISTANCE_FROM_WATER == -1 or minetest.find_node_near(pos, DISTANCE_FROM_WATER, + return nature.distance_from_water == -1 or minetest.find_node_near(pos, nature.distance_from_water, { "default:water_source" }) ~= nil end diff --git a/nature_classic/init.lua b/nature_classic/init.lua index 6bb4eb0..a3ce718 100644 --- a/nature_classic/init.lua +++ b/nature_classic/init.lua @@ -5,6 +5,34 @@ local current_mod_name = minetest.get_current_modname() +nature = {} +nature.blossomqueue = {} + +nature.blossom_node = "nature:blossom" +nature.blossom_leaves = "default:leaves" +nature.blossom_textures = { "default_leaves.png^nature_blossom.png" } + +if minetest.get_modpath("moretrees") then + nature.blossom_node = "moretrees:apple_blossoms" + nature.blossom_leaves = "moretrees:apple_tree_leaves" + nature.blossom_textures = { "moretrees_apple_tree_leaves.png^nature_blossom.png" } + minetest.register_alias("nature:blossom", "default:leaves") +end + +nature.blossom_chance = 15 +nature.blossom_delay = 10 --3600 +nature.apple_chance = 10 +nature.apple_spread = 2 + +nature.node_young = "young" +nature.setting_true = "true" +nature.setting_false = "false" +nature.youth_delay = 5 + +function dumppos(pos) + return "("..pos.x..","..pos.y..","..pos.z..")" +end + dofile(minetest.get_modpath(current_mod_name) .. "/config.lua") dofile(minetest.get_modpath(current_mod_name) .. "/global_function.lua") dofile(minetest.get_modpath(current_mod_name) .. "/blossom.lua")