master
Juraj Vajda 2021-03-14 16:10:13 -04:00
parent 44f5015d57
commit d88886e79a
8 changed files with 299 additions and 124 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.DS_Store

316
api.lua
View File

@ -10,7 +10,7 @@ bonemeal = {}
--- Get creative mode setting from minetest.conf
-- @local
local creative_mode_cache = minetest.settings:get_bool("creative_mode")
local creative_mode_cache = minetest.settings:get_bool('creative_mode')
--- Check if creating mode is enabled or player has creative privs
-- @function
@ -26,7 +26,19 @@ end
-- @return Boolean
function bonemeal.is_on_soil(under)
local below = minetest.get_node({x = under.x, y = under.y - 1, z = under.z})
if minetest.get_item_group(below.name, "soil") == 0 then
if minetest.get_item_group(below.name, 'soil') == 0 then
return false
end
return true
end
--- Check if node has a sand below its self
-- @function
-- @param under table of position
-- @return Boolean
function bonemeal.is_on_sand(under)
local below = minetest.get_node({x = under.x, y = under.y - 1, z = under.z})
if minetest.get_item_group(below.name, 'sand') == 0 then
return false
end
return true
@ -36,80 +48,251 @@ end
-- @table farming_steps
-- @local
local farming_steps = {
["farming:wheat"] = 8,
["farming:cotton"] = 8,
["farming_addons:coffee"] = 5,
["farming_addons:corn"] = 10,
["farming_addons:obsidian_wart"] = 6,
["farming_addons:melon"] = 8,
["farming_addons:pumpkin"] = 8,
["farming_addons:carrot"] = 8,
["farming_addons:potato"] = 8,
["farming_addons:beetroot"] = 8,
['farming:wheat'] = 8,
['farming:cotton'] = 8,
['farming_addons:coffee'] = 5,
['farming_addons:corn'] = 10,
['farming_addons:obsidian_wart'] = 6,
['farming_addons:melon'] = 8,
['farming_addons:pumpkin'] = 8,
['farming_addons:carrot'] = 8,
['farming_addons:potato'] = 8,
['farming_addons:beetroot'] = 8,
}
--- Particle and sound effect after the bone meal is successfully used
-- @function
-- @param pos table containing position
function bonemeal.particle_effect(pos)
minetest.sound_play("bonemeal_grow", {
minetest.sound_play('bonemeal_grow', {
pos = pos,
gain = 0.5,
})
minetest.add_particlespawner({
amount = 4,
time = 0.15,
minpos = pos,
maxpos = pos,
minvel = {x = -0.5, y = 0.5, z = -0.5},
maxvel = {x = 0.5, y = 1, z = 0.5},
minacc = {x = -0.5, y = -0.5, z = -0.5},
maxacc = {x = 0.5, y = 0.5, z = 0.5},
minexptime = 1,
maxexptime = 1,
amount = 6,
time = 3,
minpos = {x = pos.x - 0.4, y = pos.y - 0.4, z = pos.z - 0.4},
maxpos = {x = pos.x + 0.4, y = pos.y, z = pos.z + 0.4},
minvel = {x = 0, y = 0, z = 0},
maxvel = {x = 0, y = 0.1, z = 0},
minacc = vector.new({x = 0, y = 0, z = 0}),
maxacc = vector.new({x = 0, y = 0.1, z = 0}),
minexptime = 2,
maxexptime = 3,
minsize = 1,
maxsize = 3,
texture = "bonemeal_particle.png",
texture = 'bonemeal_particles.png',
animation = {
type = 'vertical_frames',
aspect_w = 8,
aspect_h = 8,
length = 3,
},
})
end
--- Handle growth of grass, flowers, mushrooms
-- @function
-- @param pos table containing position
-- @param find_node_name node what has to be around the pos for successful growth
-- @param replace_node_name what are we growing (grass, flower, mushroom)
function bonemeal.grow_grass_and_flowers(pos, itemstack, user, find_node_name, replace_node_name)
if not pos or not find_node_name or not replace_node_name then return itemstack end
function bonemeal.tableContains(table, value)
local found = false
local pos0 = vector.subtract(pos, 2)
local pos1 = vector.add(pos, 2)
local positions = minetest.find_nodes_in_area_under_air(pos0, pos1, find_node_name)
local howmany = math.random(5)
print(#positions)
if #positions == 0 then return itemstack end
if howmany > #positions then
howmany = #positions
for k, v in ipairs(table) do
if v == value then
found = true
break
end
end
for i = 1, howmany do
local idx = math.random(#positions)
local grass = replace_node_name
return found
end
--- Handle growth of decorations based on biome
-- @function
function bonemeal.grow_grass_and_flowers(itemstack, user, pointed_thing)
local node = minetest.get_node(pointed_thing.under)
if not node then
return itemstack
end
local pos0 = vector.subtract(pointed_thing.under, 3)
local pos1 = vector.add(pointed_thing.under, 3)
local biome_data = minetest.get_biome_data(pointed_thing.under)
local biome_name = minetest.get_biome_name(biome_data.biome)
local random_number = math.random(2, 6)
local registered_decorations_filtered = {}
local returned_itemstack = ItemStack(node.name ..' 1')
local node_def = minetest.registered_nodes[node.name]
local below_water = false
local floats_on_water = false
local node_in_decor = false
local positions_dirty = {}
local positions = {}
local decor_place_on = {}
-- check 1 node below pointed node (floats on water)
local test_node = minetest.get_node({x = pointed_thing.under.x, y = pointed_thing.under.y - 1, z = pointed_thing.under.z})
local test_node_def = minetest.registered_nodes[test_node.name]
if test_node_def and test_node_def.liquidtype == 'source' and minetest.get_item_group(test_node_def.name, 'water') > 0 then
floats_on_water = true
end
-- check 2 nodes above pointed nodes (below water)
local water_nodes_above = 0
for i = 1, 2 do
local test_node = minetest.get_node({x = pointed_thing.under.x, y = pointed_thing.under.y + i, z = pointed_thing.under.z})
local test_node_def = minetest.registered_nodes[test_node.name]
if test_node_def and test_node_def.liquidtype == 'source' and minetest.get_item_group(test_node_def.name, 'water') > 0 then
water_nodes_above = water_nodes_above + 1
end
end
if water_nodes_above == 2 then
below_water = true
end
if below_water then
positions_dirty = minetest.find_nodes_in_area(pos0, pos1, node.name)
elseif floats_on_water then
positions_dirty = minetest.find_nodes_in_area(pos0, pos1, 'air')
else
positions_dirty = minetest.find_nodes_in_area_under_air(pos0, pos1, node.name)
end
-- find suitable decorations
for k, v in pairs(minetest.registered_decorations) do
-- only for 'simple' decoration types
if v.deco_type == 'simple' then
local found = false
-- filter based on biome name in `biomes` table and node name in `place_on` table
if bonemeal.tableContains(v.biomes, biome_name) then
table.insert(registered_decorations_filtered, v)
end
if type(replace_node_name) == "table" then
grass = replace_node_name[math.random(#replace_node_name)]
end
bonemeal.particle_effect({x = positions[idx].x, y = positions[idx].y + 1, z = positions[idx].z})
-- clicked node is in decoration
if v.decoration == node.name then
node_in_decor = true
end
minetest.set_node({x = positions[idx].x, y = positions[idx].y + 1, z = positions[idx].z}, { name = grass })
table.remove(positions, idx)
-- all nodes on which decoration can be placed on
-- indexed by name
if not decor_place_on[v.place_on] then
for k, v in ipairs(v.place_on) do
decor_place_on[v] = true
end
end
end
-- take item if not in creative
if not bonemeal.is_creative(user:get_player_name()) then
-- find suitable positions
for j, pos_value in ipairs(positions_dirty) do
local node_at_pos = minetest.get_node(pos_value)
if below_water then
-- below water
local water_nodes_above = 0
-- check if 2 nodes above are water
for i = 1, 2 do
local test_node = minetest.get_node({x = pos_value.x, y = pos_value.y + i, z = pos_value.z})
local test_node_def = minetest.registered_nodes[test_node.name]
if test_node_def and test_node_def.liquidtype == 'source' and minetest.get_item_group(test_node_def.name, 'water') > 0 then
water_nodes_above = water_nodes_above + 1
end
end
if water_nodes_above == 2 and decor_place_on[test_node.name] then
table.insert(positions, pos_value)
end
else
-- above water (not on water)
if decor_place_on[node_at_pos.name] then
table.insert(positions, pos_value)
end
end
end
-- find suitable positions (float on water)
if floats_on_water then
for j, pos_value in ipairs(positions_dirty) do
local node_at_pos = minetest.get_node(pos_value)
local node_at_pos_below = minetest.get_node({x = pos_value.x, y = pos_value.y - 1, z = pos_value.z})
local test_node_def = minetest.registered_nodes[node_at_pos_below.name]
if test_node_def and test_node_def.liquidtype == 'source' and minetest.get_item_group(test_node_def.name, 'water') > 0 then
table.insert(positions, pos_value)
end
end
end
-- place decorations on random positions
if #positions > 0 and #registered_decorations_filtered > 0 then
for i = 1, random_number do
local idx = math.random(#positions)
local random_pos = positions[idx]
local random_decor = registered_decorations_filtered[math.random(#registered_decorations_filtered)]
local random_decor_item = random_decor.decoration
if floats_on_water and node_in_decor then
random_decor_item = node.name
elseif type(random_decor.decoration) == 'table' then
random_decor_item = random_decor.decoration[math.random(#random_decor.decoration)]
end
local random_decor_item_def = minetest.registered_nodes[random_decor_item]
if random_pos ~= nil then
if random_decor_item_def.on_place ~= nil and node_def and not node_def.on_rightclick then
-- on_place
local pt = {
type = 'node',
above = {
x = random_pos.x,
y = random_pos.y + 1,
z = random_pos.z
},
under = {
x = random_pos.x,
y = random_pos.y,
z = random_pos.z
}
}
if floats_on_water then
pt.above.y = random_pos.y
pt.under.y = random_pos.y - 1
end
returned_itemstack = random_decor_item_def.on_place(ItemStack(random_decor_item), user, pt)
if returned_itemstack and returned_itemstack:is_empty() then
bonemeal.particle_effect(pt.above)
end
elseif random_decor_item_def ~= nil then
-- everything else
local pos_y = 1
if random_decor.place_offset_y ~= nil then
pos_y = random_decor.place_offset_y
end
returned_itemstack = ItemStack('')
bonemeal.particle_effect(random_pos)
minetest.set_node({x = random_pos.x, y = random_pos.y + pos_y, z = random_pos.z}, { name = random_decor_item })
end
table.remove(positions, idx)
end
end
end
-- take item
if returned_itemstack and returned_itemstack:is_empty() and not bonemeal.is_creative(user:get_player_name()) then
itemstack:take_item()
end
@ -123,47 +306,42 @@ end
function bonemeal.grow_farming(pos, itemstack, user, replace_node_name)
local ndef = minetest.registered_nodes[replace_node_name]
if not ndef.next_plant or ndef.next_plant == "farming_addons:pumpkin_fruit" or ndef.next_plant == "farming_addons:melon_fruit" then return itemstack end
if not ndef.next_plant or ndef.next_plant == 'farming_addons:pumpkin_fruit' or ndef.next_plant == 'farming_addons:melon_fruit' then return itemstack end
-- check if on wet soil
local below = minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z})
if minetest.get_item_group(below.name, "soil") < 3 then
if minetest.get_item_group(below.name, 'soil') < 3 then
return itemstack
end
local plant = replace_node_name:split("_")[1]
local current_step = tonumber(string.reverse(replace_node_name):split("_")[1])
local max_step = farming_steps[replace_node_name:gsub("_%d+", "", 1)]
local plant = replace_node_name:split('_')[1]
local current_step = tonumber(string.reverse(replace_node_name):split('_')[1])
local max_step = farming_steps[replace_node_name:gsub('_%d+', '', 1)]
-- check if seed
-- farming:seed_wheat
local mod_plant = replace_node_name:split(":")
local mod_plant = replace_node_name:split(':')
-- seed_wheat
local seed_plant = mod_plant[2]:split("_")
-- print("seed: ", mod_plant[1]..":"..seed_plant[2])
local seed_plant = mod_plant[2]:split('_')
if seed_plant[1] == "seed" then
if seed_plant[1] == 'seed' then
current_step = 0
if replace_node_name == "farming_addons:seed_obsidian_wart" then
replace_node_name = mod_plant[1]..":"..seed_plant[2].."_"..seed_plant[3]
if replace_node_name == 'farming_addons:seed_obsidian_wart' then
replace_node_name = mod_plant[1]..':'..seed_plant[2]..'_'..seed_plant[3]
else
replace_node_name = mod_plant[1]..":"..seed_plant[2]
replace_node_name = mod_plant[1]..':'..seed_plant[2]
end
max_step = farming_steps[replace_node_name]
replace_node_name = replace_node_name.."_"..current_step
replace_node_name = replace_node_name..'_'..current_step
end
-- print(dump(ndef))
-- print("current_step: ", current_step)
-- print("max_step: ", max_step)
if not current_step or not max_step then return itemstack end
local available_steps = max_step - current_step
local new_step = max_step - available_steps + math.random(available_steps)
local new_plant = replace_node_name:gsub("_%d+", "_"..new_step, 1)
local new_plant = replace_node_name:gsub('_%d+', '_'..new_step, 1)
-- print("new_plant: ", new_plant)
local placenode = {name = new_plant}
if ndef.place_param2 then

View File

@ -1,5 +0,0 @@
default
farming
bones
flowers
x_default

View File

@ -1,7 +0,0 @@
Mod for Minetest. It provides one kind of simple bone meal (fertilizer) for fast growing plants, flowers, saplings, papyrus, grass...
Based on default minetest_game API with as less additions as possible what makes this mod super light-weight and suitable for online multiplayer servers.
Supports default minetest game blocks/plants/flowers... and any mod what uses minetest game API, e.g. farming_additions - though other mods can be easily implemented too.
This mod aims to be simple, light-weight and fast, therefore it will have no bone meal 'strength', multiple types of bone meals nor diamond/mese bone meal.

View File

@ -15,10 +15,8 @@ dofile(path.."/api.lua")
minetest.register_craftitem("bonemeal:bonemeal", {
description = "Bonemeal - use it as a fertilizer for most plants",
inventory_image = "bonemeal_bonemeal.png",
wield_scale = {x=1.5, y=1.5, z=1},
sound = {breaks = "default_tool_breaks"},
-- wield_scale = {x=1.5, y=1.5, z=1},
on_use = function(itemstack, user, pointed_thing)
local pt = pointed_thing
local under = pointed_thing.under
if not under then return end
@ -32,50 +30,11 @@ minetest.register_craftitem("bonemeal:bonemeal", {
local mod = node.name:split(":")[1]
--
-- Nodes
--
-- default:grass
-- default:junglegrass
if node.name == "default:dirt_with_grass" then
bonemeal.grow_grass_and_flowers(under, itemstack, user, node.name, { "default:grass_1", "default:grass_2", "default:grass_3", "default:grass_4", "default:grass_5", "default:junglegrass" })
-- default:dry_grass
elseif node.name == "default:dirt_with_dry_grass" then
bonemeal.grow_grass_and_flowers(under, itemstack, user, node.name, { "default:dry_grass_1", "default:dry_grass_2", "default:dry_grass_3", "default:dry_grass_4", "default:dry_grass_5" })
-- default:dry_shrub
elseif minetest.get_item_group(node.name, "sand") == 1 or
node.name == "default:dirt_with_snow" then
bonemeal.grow_grass_and_flowers(under, itemstack, user, node.name, "default:dry_shrub")
--
-- Flowers
--
elseif mod == "flowers" then
if node.name == "flowers:waterlily" then
-- waterlily
bonemeal.grow_grass_and_flowers(under, itemstack, user, "default:water_source", node.name)
else
bonemeal.grow_grass_and_flowers(under, itemstack, user, "default:dirt_with_grass", node.name)
end
-- bakedclay mod
elseif mod == "bakedclay" then
if node.name == "bakedclay:delphinium" or
node.name == "bakedclay:thistle" or
node.name == "bakedclay:lazarus" or
node.name == "bakedclay:mannagrass" then
bonemeal.grow_grass_and_flowers(under, itemstack, user, "default:dirt_with_grass", node.name)
end
--
-- Farming
--
elseif mod == "farming" or mod == "farming_addons" then
if mod == "farming" or mod == "farming_addons" then
bonemeal.grow_farming(under, itemstack, user, node.name)
--
@ -224,6 +183,36 @@ minetest.register_craftitem("bonemeal:bonemeal", {
end
return itemstack
-- Pine bush
elseif node.name == "default:pine_bush_sapling" then
local chance = math.random(2)
if chance == 1 then
if not bonemeal.is_on_soil(under) then return end
default.grow_pine_bush(under)
bonemeal.particle_effect({x = under.x, y = under.y + 1, z = under.z})
end
-- take item if not in creative
if not bonemeal.is_creative(user:get_player_name()) then
itemstack:take_item()
end
return itemstack
-- Blueberry bush
elseif node.name == "default:blueberry_bush_sapling" then
local chance = math.random(2)
if chance == 1 then
if not bonemeal.is_on_soil(under) then return end
default.grow_blueberry_bush(under)
bonemeal.particle_effect({x = under.x, y = under.y + 1, z = under.z})
end
-- take item if not in creative
if not bonemeal.is_creative(user:get_player_name()) then
itemstack:take_item()
end
return itemstack
-- Papyrus
elseif node.name == "default:papyrus" then
local chance = math.random(2)
@ -238,6 +227,23 @@ minetest.register_craftitem("bonemeal:bonemeal", {
itemstack:take_item()
end
return itemstack
-- Large Cactus
elseif node.name == "default:large_cactus_seedling" then
local chance = math.random(2)
if chance == 1 then
if not bonemeal.is_on_sand(under) then return end
default.grow_large_cactus(under, node)
bonemeal.particle_effect({x = under.x, y = under.y + 1, z = under.z})
end
-- take item if not in creative
if not bonemeal.is_creative(user:get_player_name()) then
itemstack:take_item()
end
return itemstack
else
bonemeal.grow_grass_and_flowers(itemstack, user, pointed_thing)
end
return itemstack

View File

@ -1 +1,3 @@
name = bonemeal
description = Mod bonemeal for Minetest.
depends = default, x_default, farming, bones, flowers

Binary file not shown.

Before

Width:  |  Height:  |  Size: 141 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 235 B