Skip to content

Commit

Permalink
Generate plants in the map (so one can get seeds)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeg9 authored and Vanessa Ezekowitz committed Nov 3, 2013
1 parent 7d86ca7 commit 7f36bcb
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion init.lua
@@ -1,3 +1,6 @@

farming.registered_plants = {}

function farming:add_plant(full_grown, names, interval, chance)
minetest.register_abm({
nodenames = names,
Expand Down Expand Up @@ -31,7 +34,14 @@ function farming:add_plant(full_grown, names, interval, chance)
end
minetest.env:set_node(pos, new_node)
end
} )
})

table.insert(farming.registered_plants, {
full_grown = full_grown,
names = names,
interval = interval,
chance = chance,
})
end

function farming:generate_tree(pos, trunk, leaves, underground, replacements)
Expand Down Expand Up @@ -138,6 +148,60 @@ farming.seeds = {
["farming_plus:carrot_seed"]=30,
}

-- ========= GENERATE PLANTS IN THE MAP =========
minetest.register_on_generated(function(minp, maxp, seed)
if maxp.y >= 2 and minp.y <= 0 then
-- Generate plants (code from flowers)
local perlin1 = minetest.get_perlin(974, 3, 0.6, 100)
-- Assume X and Z lengths are equal
local divlen = 16
local divs = (maxp.x-minp.x)/divlen+1;
for divx=0,divs-1 do
for divz=0,divs-1 do
local x0 = minp.x + math.floor((divx+0)*divlen)
local z0 = minp.z + math.floor((divz+0)*divlen)
local x1 = minp.x + math.floor((divx+1)*divlen)
local z1 = minp.z + math.floor((divz+1)*divlen)
-- Determine flowers amount from perlin noise
local grass_amount = math.floor(perlin1:get2d({x=x0, y=z0}) ^ 3 * 9)
-- Find random positions for flowers based on this random
local pr = PseudoRandom(seed+456)
for i=0,grass_amount do
local x = pr:next(x0, x1)
local z = pr:next(z0, z1)
-- Find ground level (0...15)
local ground_y = nil
for y=30,0,-1 do
if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
ground_y = y
break
end
end

if ground_y then
local p = {x=x,y=ground_y+1,z=z}
local nn = minetest.get_node(p).name
-- Check if the node can be replaced
if minetest.registered_nodes[nn] and
minetest.registered_nodes[nn].buildable_to then
nn = minetest.get_node({x=x,y=ground_y,z=z}).name
if nn == "default:dirt_with_grass" then
--local plant_choice = pr:next(1, #farming.registered_plants)
local plant_choice = math.floor(perlin1:get2d({x=x,y=z})*(#farming.registered_plants))
local plant = farming.registered_plants[plant_choice]
if plant then
minetest.set_node(p, {name=plant.full_grown})
end
end
end
end

end
end
end
end
end)

-- ========= ALIASES FOR FARMING MOD BY SAPIER =========
-- potatoe -> potatoe
minetest.register_alias("farming:potatoe_node", "farming_plus:potatoe")
Expand Down

0 comments on commit 7f36bcb

Please sign in to comment.