refactor nature_classic to avoid lag caused by its blossom abms

This commit is contained in:
Vanessa Ezekowitz 2015-02-08 23:17:02 -05:00
parent 117cab1474
commit 40f5d66505
4 changed files with 78 additions and 52 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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")