fix global errors, remove the leaves remove stuff and delay the growing of coonifers by default

master
HybridDog 2015-03-30 13:50:11 +02:00
parent 40a7bb9aaa
commit 8b12618365
11 changed files with 122 additions and 34 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
## Generic ignorable patterns and files
*~
.*.swp
debug.txt

View File

@ -1 +1,2 @@
default
function_delayer?

72
function_delayer.lua Normal file
View File

@ -0,0 +1,72 @@
local load_time_start = os.clock()
local maxdelay = 1
-- used for the table.sort function
local function sort_times(a,b)
return a[1] < b[1]
end
local todo = {}
function minetest.delay_function(time, func, ...)
table.insert(todo, {time, func, {...}})
-- execute the functions with lower delays earlier
table.sort(todo, sort_times)
end
minetest.register_globalstep(function(dtime)
local count = #todo
-- abort if nothing is todo
if count == 0 then
return
end
-- get the start time
local ts = tonumber(os.clock())
-- execute expired functions
local n = 1
while n <= count do
local time = todo[n][1]
time = time-dtime
if time <= 0 then
local params = todo[n][3]
params[#params+1] = time
todo[n][2](unpack(params or {}))
table.remove(todo, n)
count = count-1
else
todo[n][1] = time
n = n+1
end
end
-- abort if too much time is used already
if tonumber(os.clock())-ts > maxdelay then
return
end
-- execute functions until the time limit is reached
n = 1
while n <= count do
local params = todo[n][3]
params[#params+1] = todo[n][1]
todo[n][2](unpack(params or {}))
table.remove(todo, n)
count = count-1
if tonumber(os.clock())-ts > maxdelay then
return
end
end
end)
local time = math.floor(tonumber(os.clock()-load_time_start)*100+0.5)/100
local msg = "[function_delayer] loaded after ca. "..time
if time > 0.05 then
print(msg)
else
minetest.log("info", msg)
end

View File

@ -11,8 +11,6 @@ local LEAVES_NARROWRADIUS = 3 -- For narrow typed conifers.
local CONIFERS_DISTANCE = 4
local CONIFERS_ALTITUDE = 30
local REMOVE_TREES = false -- Remove trees above CONIFERS_ALTITUDE? It kills default trees from the top.
local SAPLING_CHANCE = 100 -- 1/x chances to grow a sapling.
local INTERVAL = 3600
@ -22,6 +20,10 @@ local conifers_seed = 1435
-- End of structure definitions.
if not minetest.delay_function then
dofile(minetest.get_modpath("conifers").."/function_delayer.lua")
end
conifers = {}
@ -212,58 +214,65 @@ minetest.register_craft({
-- ABM definitions
--
-- Spawn random conifers.
local function get_conifers_random(pos)
return PseudoRandom(math.abs(pos.x+pos.y*3+pos.z*5)+conifers_seed)
end
local function conifer_abm_rand(pos)
local pr = get_conifers_random(pos)
local p = {x=pos.x, y=pos.y+1, z=pos.z}
if pr:next(1,23) == 1
and minetest.get_node(p).name == "air"
and pos.y >= CONIFERS_ALTITUDE
and (not conifers:is_node_in_cube({"conifers:trunk"}, pos, CONIFERS_DISTANCE)) then
conifers:make_conifer(p, math.random(0, 1))
end
end
local function conifer_abm_rand_delay(pos)
local node = minetest.get_node(pos)
if node.name == "default:dirt_with_grass" then
conifer_abm_rand(pos, node)
end
end
minetest.register_abm({
nodenames = "default:dirt_with_grass",
interval = INTERVAL,
chance = 9.1,
action = function(pos)
local pr = get_conifers_random(pos)
local p = {x=pos.x, y=pos.y+1, z=pos.z}
if pr:next(1,23) == 1
and minetest.get_node(p).name == "air"
and pos.y >= CONIFERS_ALTITUDE
and (not conifers:is_node_in_cube({"conifers:trunk"}, pos, CONIFERS_DISTANCE)) then
conifers:make_conifer(p, math.random(0, 1))
end
minetest.delay_function(INTERVAL-1, conifer_abm_rand_delay, pos)
end
})
-- Saplings.
local function conifer_abm_sapling(pos)
if minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name == "air" then
conifers:make_conifer(pos, math.random(0, 1))
end
end
local function conifer_abm_sapling_delay(pos)
local node = minetest.get_node(pos)
if node.name == "conifers:sapling" then
conifer_abm_sapling(pos, node)
end
end
minetest.register_abm({
nodenames = "conifers:sapling",
interval = INTERVAL,
chance = SAPLING_CHANCE,
action = function(pos, node)
if minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name == "air" then
conifers:make_conifer(pos, math.random(0, 1))
end
action = function(pos)
minetest.delay_function(INTERVAL-1, conifer_abm_sapling_delay, pos)
end
})
-- Should we remove all the trees above the conifers altitude?
if REMOVE_TREES == true then
minetest.register_abm({
nodenames = {
"default:tree",
"default:leaves"
},
interval = INTERVAL/100,
chance = 1,
action = function(pos, node)
if minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name == "air"
and pos.y >= CONIFERS_ALTITUDE then
minetest.add_node(pos , {name = "air"})
end
end
})
end
--------------------------------------------------------------------------------
@ -296,7 +305,7 @@ function conifers:is_node_in_cube(nodenames, pos, size)
for x = pos.x-size, pos.x+size do
for y = pos.y-hs, pos.y+hs do
for z = pos.z-size, pos.z+size do
n = minetest.get_node_or_nil({x=x, y=y, z=z})
local n = minetest.get_node_or_nil({x=x, y=y, z=z})
if n == nil
or n.name == 'ignore'
or conifers:table_contains(nodenames, n.name) then
@ -308,6 +317,8 @@ function conifers:is_node_in_cube(nodenames, pos, size)
return false
end
local area, nodes
--
-- are_leaves_surrounded(position)
--

BIN
rest/c1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 333 B

After

Width:  |  Height:  |  Size: 198 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 367 B

After

Width:  |  Height:  |  Size: 198 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 454 B

After

Width:  |  Height:  |  Size: 230 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 498 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 768 B

After

Width:  |  Height:  |  Size: 687 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 487 B