Much more MC-like pumpkin growth, fix stem bugs

- Pumpkins now grow randomly
- Stems now disconnect properly after removing pumpkin
- Remove lots of ugly code duplication
This commit is contained in:
Wuzzy 2017-03-13 21:25:11 +01:00
parent 12bb62727d
commit a841d02325
1 changed files with 54 additions and 63 deletions

View File

@ -79,24 +79,21 @@ minetest.register_node("mcl_farming:pumpkin_face", {
paramtype2 = "facedir", paramtype2 = "facedir",
tiles = {"farming_pumpkin_top.png", "farming_pumpkin_top.png", "farming_pumpkin_side.png", "farming_pumpkin_side.png", "farming_pumpkin_side.png", "farming_pumpkin_face.png"}, tiles = {"farming_pumpkin_top.png", "farming_pumpkin_top.png", "farming_pumpkin_side.png", "farming_pumpkin_side.png", "farming_pumpkin_side.png", "farming_pumpkin_face.png"},
groups = {handy=1,axey=1, building_block=1}, groups = {handy=1,axey=1, building_block=1},
after_dig_node = function(pos, oldnode, oldmetadata, user) after_dig_node = function(blockpos, oldnode, oldmetadata, user)
local have_change = 0 -- Disconnect any connected stems, turning them back to normal stems
for x=-1,1 do local neighbors = {
local p = {x=pos.x+x, y=pos.y, z=pos.z} { { x=-1, y=0, z=0 }, "mcl_farming:pumpkintige_linked_r" },
local n = minetest.get_node(p) { { x=1, y=0, z=0 }, "mcl_farming:pumpkintige_linked_l" },
if string.find(n.name, "pumpkintige_linked_") and have_change == 0 then { { x=0, y=0, z=-1 }, "mcl_farming:pumpkintige_linked_t" },
have_change = 1 { { x=0, y=0, z=1 }, "mcl_farming:pumpkintige_linked_b" },
minetest.add_node(p, {name="mcl_farming:pumpkintige_unconnect"}) }
end for n=1, #neighbors do
end local offset = neighbors[n][1]
if have_change == 0 then local expected_stem = neighbors[n][2]
for z=-1,1 do local stempos = vector.add(blockpos, offset)
local p = {x=pos.x, y=pos.y, z=pos.z+z} local stem = minetest.get_node(stempos)
local n = minetest.get_node(p) if stem.name == expected_stem then
if string.find(n.name, "pumpkintige_linked_") and have_change == 0 then minetest.add_node(stempos, {name="mcl_farming:pumpkintige_unconnect"})
have_change = 1
minetest.add_node(p, {name="mcl_farming:pumpkintige_unconnect"})
end
end end
end end
end, end,
@ -256,55 +253,49 @@ minetest.register_abm({
neighbors = {"air"}, neighbors = {"air"},
interval = 1, interval = 1,
chance = 1, chance = 1,
action = function(pos) action = function(stempos)
local have_change = 0 local light = minetest.get_node_light(stempos)
local newpos = {x=pos.x, y=pos.y, z=pos.z} if light and light > 10 then
local light = minetest.get_node_light(pos) -- Check the four neighbors and filter out neighbors where pumpkins can't grow
if light or light > 10 then local neighbors = {
for x=-1,1 do { x=-1, y=0, z=0 },
local p = {x=pos.x+x, y=pos.y-1, z=pos.z} { x=1, y=0, z=0 },
newpos = {x=pos.x+x, y=pos.y, z=pos.z} { x=0, y=0, z=-1 },
local n = minetest.get_node(p) { x=0, y=0, z=1 },
local nod = minetest.get_node(newpos) }
local soilgroup = minetest.get_item_group(n.name, "soil") for n=#neighbors, 1, -1 do
if n.name=="mcl_core:dirt_with_grass" and nod.name=="air" and have_change == 0 local offset = neighbors[n]
or n.name=="mcl_core:dirt" and nod.name=="air" and have_change == 0 local blockpos = vector.add(stempos, offset)
or (soilgroup == 2 or soilgroup == 3) and nod.name=="air" and have_change == 0 then local floorpos = { x=blockpos.x, y=blockpos.y-1, z=blockpos.z }
have_change = 1 local floor = minetest.get_node(floorpos)
local p2 local block = minetest.get_node(blockpos)
if x == 1 then local soilgroup = minetest.get_item_group(floor.name, "soil")
minetest.add_node(pos, {name="mcl_farming:pumpkintige_linked_r" }) if not ((floor.name=="mcl_core:dirt_with_grass" or floor.name=="mcl_core:dirt" or soilgroup == 2 or soilgroup == 3) and block.name == "air") then
p2 = 3 table.remove(neighbors, n)
else
minetest.add_node(pos, {name="mcl_farming:pumpkintige_linked_l"})
p2 = 1
end
minetest.add_node(newpos, {name="mcl_farming:pumpkin_face", param2=p2})
end end
end end
if have_change == 0 then
for z=-1,1 do
local p = {x=pos.x, y=pos.y-1, z=pos.z+z}
newpos = {x=pos.x, y=pos.y, z=pos.z+z}
local n = minetest.get_node(p)
local nod2 = minetest.get_node(newpos)
local soilgroup = minetest.get_item_group(n.name, "soil")
if n.name=="mcl_core:dirt_with_grass" and nod2.name=="air" and have_change == 0
or n.name=="mcl_core:dirt" and nod2.name=="air" and have_change == 0
or (soilgroup == 2 or soilgroup == 3) and nod2.name=="air" and have_change == 0 then
have_change = 1
-- Pumpkins need at least 1 free neighbor to grow
if #neighbors > 0 then
-- From the remaining neighbors, grow randomly
local r = math.random(1, #neighbors)
local offset = neighbors[r]
local blockpos = vector.add(stempos, offset)
local p2 local p2
if z == 1 then if offset.x == 1 then
minetest.add_node(pos, {name="mcl_farming:pumpkintige_linked_t" }) minetest.set_node(stempos, {name="mcl_farming:pumpkintige_linked_r" })
p2 = 3
elseif offset.x == -1 then
minetest.set_node(stempos, {name="mcl_farming:pumpkintige_linked_l"})
p2 = 1
elseif offset.z == 1 then
minetest.set_node(stempos, {name="mcl_farming:pumpkintige_linked_t"})
p2 = 2 p2 = 2
else elseif offset.z == -1 then
minetest.add_node(pos, {name="mcl_farming:pumpkintige_linked_b" }) minetest.set_node(stempos, {name="mcl_farming:pumpkintige_linked_b"})
p2 = 0 p2 = 0
end end
minetest.add_node(newpos, {name="mcl_farming:pumpkin_face", param2=p2}) minetest.add_node(blockpos, {name="mcl_farming:pumpkin_face", param2=p2})
end
end
end end
end end
end, end,