Compare commits

...

5 Commits

Author SHA1 Message Date
texmex c57fa04829 Typo 2017-07-28 00:14:24 +02:00
Yzel Junior 44e78b8527 Update init.lua
fixing tab identation
2017-06-07 12:59:48 -04:00
Yzel Junior 6f5064d79e Merge branch 'master' into master 2017-06-07 12:58:23 -04:00
texmex 17d3b1ea17 Let's make floodables global 👍 2017-06-07 18:16:35 +02:00
Yzel Junior 17fdc94b6a Rework and erosion added
In this first pr, the old loops used to override the nodes were replaced by functions, making the code more modularized and easier to understand and modify.
There's a function specially created to handle crop nodes, so we can easily add the flood funcion to all the crop variations and have different flood and drop chances too.
But the coolest thing is the soil erosion system. When a soil has space to erode, an abm will change its old position to a randon inferior position , and after making and nodeupdate to make it fall. Currently the abm is using the "air" neighbor but can be easily changed to erode only with flowing water.
2017-06-07 11:34:25 -04:00
1 changed files with 155 additions and 45 deletions

200
init.lua
View File

@ -1,47 +1,157 @@
local floodables = {
{ group = "torch", drop = "default:torch", sound = "floodables_torch", gain = 0.8 },
{ group = "flora", sound = "floodables_grass", gain = 0.2 },
{ group = "wheat", drop = "farming:seed_wheat", sound = "floodables_grass", gain = 0.2 },
{ name = "default:junglegrass", sound = "floodables_grass", gain = 0.2 },
{ group = "grass", drop = "default:grass_1", sound = "floodables_grass", gain = 0.2 },
{ group = "dry_grass", drop = "default:dry_grass_1", sound = "floodables_grass", gain = 0.2 },
-- { name = "default:dirt_with_grass", erode = true },
}
floodables = {}
for _,c in ipairs(floodables) do
if c.name ~=nil then
minetest.override_item(c.name, {
floodable = true,
on_flood = function(pos, oldnode, newnode)
minetest.remove_node(pos)
if c.sound ~= nil then
minetest.sound_play({ name = c.sound, gain = c.gain }, { pos = pos, max_hear_distance = 16 })
end
if c.drop == nil or c.drop ~= false then c.drop = c.name end
minetest.add_item(pos, {name = c.drop})
if c.erode == true then
minetest.remove_node( { x = pos.x, y = pos.y-1, z = pos.z } )
end
end
})
elseif c.group ~=nil then
for _,v in pairs(minetest.registered_nodes) do
if minetest.get_item_group(v.name, c.group) ~= 0 then
minetest.override_item(v.name, {
floodable = true,
on_flood = function(pos, oldnode, newnode)
minetest.remove_node(pos)
if c.sound ~= nil then
minetest.sound_play({ name = c.sound, gain = c.gain }, { pos = pos, max_hear_distance = 16 })
end
if c.drop == nil or c.drop == true then c.drop = v.name end
minetest.add_item(pos, {name = c.drop})
if c.erode == true then
minetest.remove_node( { x = pos.x, y = pos.y-1, z = pos.z } )
end
end
})
end
end
end
function floodables.register_soil(def)
for key,value in pairs(minetest.registered_nodes) do
if minetest.get_item_group(value.name, def.name) ~= 0 then
-- overrides all soil nodes to add the falling_group
minetest.override_item(value.name,{
groups = {soil = 1, falling_node = 1},
})
minetest.register_abm({
label = "soil erosion",
nodenames = value.name,
neighbors = "air",
interval = def.interval,
chance = def.chance,
-- This function check if there space under the soil, and if true, set "air" at
-- the old position and put the old node 1 Y lower
action = function(pos)
if math.random(def.erode_chance) == 1 then
local pmin = {x = pos.x - 1, y = pos.y - 1, z = pos.z - 1}
local pmax = {x = pos.x + 1, y = pos.y - 1, z = pos.z + 1}
local area = minetest.find_nodes_in_area(pmin,pmax,"air")
if table.getn(area) > 0 then
minetest.set_node(pos,{name = "air"})
local pos = area[math.random(table.getn(area))]
minetest.set_node(pos,{name = value.name})
minetest.check_single_for_falling(pos) -- make all floating dirt fall
end
end
end,
})
end
end
end
function floodables.register_crops(def)
local size = 1
while minetest.registered_nodes[def.name.."_"..size] ~= nil do
minetest.override_item(def.name.."_"..size, {
floodable = true,
on_flood = function(pos)
if math.random(def.func_chance) == 1 then
minetest.sound_play({ name = def.sound, gain = def.gain }, { pos = pos, max_hear_distance = 16 })
if math.random(def.drop_chance) == 1 then
local node = minetest.get_node(pos)
local drops = minetest.get_node_drops(node.name)
local index = math.random(table.getn(drops))
minetest.set_node(pos, {name="air"})
minetest.add_item(pos, drops[index])
else
minetest.set_node(pos, {name="air"})
end
end
end,
})
size = size + 1
end
end
function floodables.register_groups(def)
for key,value in pairs(minetest.registered_nodes) do
if minetest.get_item_group(value.name, def.name) ~= 0 then
minetest.override_item(value.name, {
floodable = true,
on_flood = function(pos)
if math.random(def.func_chance) == 1 then
minetest.sound_play({ name = def.sound, gain = def.gain }, { pos = pos, max_hear_distance = 16 })
if math.random(def.drop_chance) == 1 then
local drops = minetest.get_node_drops(value.name)
local index = math.random(table.getn(drops))
minetest.set_node(pos, {name="air"})
minetest.add_item(pos, drops[index])
else
minetest.set_node(pos, {name="air"})
end
end
end,
})
end
end
end
--------------------------------------------------------------------------------
-- Groups registration
--------------------------------------------------------------------------------
floodables.register_soil({
name = "soil",
chance = 4,
interval = 2,
erode_chance = 8,
})
--------------------------------------------------------------------------------
-- Groups registration
--------------------------------------------------------------------------------
floodables.register_groups({
name = "flora",
func_chance = 2,
drop_chance = 4,
sound = "floodables_grass",
gain = 0.2,
})
floodables.register_groups({
name = "flower",
func_chance = 2,
drop_chance = 4,
sound = "floodables_grass",
gain = 0.2,
})
floodables.register_groups({
name = "torch",
func_chance = 1,
drop_chance = 1,
sound = "floodables_torch",
gain = 0.8,
})
--------------------------------------------------------------------------------
-- Crops registration
--------------------------------------------------------------------------------
floodables.register_crops({
name = "farming:wheat",
func_chance = 1,
drop_chance = 1,
sound = "floodables_grass",
gain = 0.2,
})
floodables.register_crops({
name = "farming:cotton",
func_chance = 1,
drop_chance = 1,
sound = "floodables_grass",
gain = 0.2,
})
-- Although the grass is inside the flora group, i prefered to add is in a crop
-- function, just to be same with the dry grass, which has no group
floodables.register_crops({
name = "default:grass",
func_chance = 1,
drop_chance = 1,
sound = "floodables_grass",
gain = 0.2,
})
floodables.register_crops({
name = "default:dry_grass",
func_chance = 1,
drop_chance = 1,
sound = "floodables_grass",
gain = 0.2,
})