Merge pull request 'Simple bone meal API (Fix #1862)' (#1892) from NO11/MineClone2:bone_meal into master

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/1892
This commit is contained in:
iliekprogrammar 2021-11-08 16:53:01 +00:00
commit 0491b814dd
2 changed files with 63 additions and 34 deletions

14
mods/ITEMS/mcl_dye/API.md Normal file
View File

@ -0,0 +1,14 @@
# mcl_dye
# Bone meal API
Callback and particle functions.
## mcl_dye.add_bone_meal_particle(pos, def)
Spawns standard or custom bone meal particles.
* `pos`: position, is ignored if you define def.minpos and def.maxpos
* `def`: (optional) particle definition
## mcl_dye.register_on_bone_meal_apply(function(pointed_thing, user))
Called when the bone meal is applied anywhere.
* `pointed_thing`: exact pointing location (see Minetest API), where the bone meal is applied
* `user`: ObjectRef of the player who aplied the bone meal, can be nil!

View File

@ -128,26 +128,35 @@ for _, row in ipairs(dyelocal.dyes) do
end end
-- Bone Meal -- Bone Meal
local function bone_meal_particle(pos) function mcl_dye.add_bone_meal_particle(pos, def)
if not def then
def = {}
end
minetest.add_particlespawner({ minetest.add_particlespawner({
amount = 10, amount = def.amount or 10,
time = 0.1, time = def.time or 0.1,
minpos = { x = pos.x - 0.5, y = pos.y - 0.5, z = pos.z - 0.5 }, minpos = def.minpos or vector.subtract(pos, 0.5),
maxpos = { x = pos.x + 0.5, y = pos.y + 0.5, z = pos.z + 0.5 }, maxpos = def.maxpos or vector.add(pos, 0.5),
minvel = { x = 0, y = 0, z = 0}, minvel = def.minvel or vector.new(0, 0, 0),
maxvel = { x = 0, y = 0, z = 0}, maxvel = def.maxvel or vector.new(0, 0, 0),
minacc = { x = 0, y = 0, z = 0}, minacc = def.minacc or vector.new(0, 0, 0),
maxacc = { x = 0, y = 0, z = 0}, maxacc = def.maxacc or vector.new(0, 0, 0),
minexptime = 1, minexptime = def.minexptime or 1,
maxexptime = 4, maxexptime = def.maxexptime or 4,
minsize = 0.7, minsize = def.minsize or 0.7,
maxsize = 2.4, maxsize = def.maxsize or 2.4,
texture = "mcl_particles_bonemeal.png^[colorize:#00EE00:125", -- TODO: real MC color texture = "mcl_particles_bonemeal.png^[colorize:#00EE00:125", -- TODO: real MC color
glow = 5, glow = def.glow or 5,
}) })
end end
function mcl_dye.apply_bone_meal(pointed_thing) mcl_dye.bone_meal_callbacks = {}
function mcl_dye.register_on_bone_meal_apply(func)
table.insert(mcl_dye.bone_meal_callbacks, func)
end
local function apply_bone_meal(pointed_thing)
-- Bone meal currently spawns all flowers found in the plains. -- Bone meal currently spawns all flowers found in the plains.
local flowers_table_plains = { local flowers_table_plains = {
"mcl_flowers:dandelion", "mcl_flowers:dandelion",
@ -183,14 +192,21 @@ function mcl_dye.apply_bone_meal(pointed_thing)
local pos = pointed_thing.under local pos = pointed_thing.under
local n = minetest.get_node(pos) local n = minetest.get_node(pos)
if n.name == "" then return false end if n.name == "" then return false end
for _, func in pairs(mcl_dye.bone_meal_callbacks) do
if func(pointed_thing, user) then
return true
end
end
if minetest.get_item_group(n.name, "sapling") >= 1 then if minetest.get_item_group(n.name, "sapling") >= 1 then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
-- Saplings: 45% chance to advance growth stage -- Saplings: 45% chance to advance growth stage
if math.random(1,100) <= 45 then if math.random(1,100) <= 45 then
return mcl_core.grow_sapling(pos, n) return mcl_core.grow_sapling(pos, n)
end end
elseif minetest.get_item_group(n.name, "mushroom") == 1 then elseif minetest.get_item_group(n.name, "mushroom") == 1 then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
-- Try to grow huge mushroom -- Try to grow huge mushroom
-- Must be on a dirt-type block -- Must be on a dirt-type block
@ -240,39 +256,37 @@ function mcl_dye.apply_bone_meal(pointed_thing)
return false return false
-- Wheat, Potato, Carrot, Pumpkin Stem, Melon Stem: Advance by 2-5 stages -- Wheat, Potato, Carrot, Pumpkin Stem, Melon Stem: Advance by 2-5 stages
elseif string.find(n.name, "mcl_farming:wheat_") then elseif string.find(n.name, "mcl_farming:wheat_") then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
local stages = math.random(2, 5) local stages = math.random(2, 5)
return mcl_farming:grow_plant("plant_wheat", pos, n, stages, true) return mcl_farming:grow_plant("plant_wheat", pos, n, stages, true)
elseif string.find(n.name, "mcl_farming:potato_") then elseif string.find(n.name, "mcl_farming:potato_") then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
local stages = math.random(2, 5) local stages = math.random(2, 5)
return mcl_farming:grow_plant("plant_potato", pos, n, stages, true) return mcl_farming:grow_plant("plant_potato", pos, n, stages, true)
elseif string.find(n.name, "mcl_farming:carrot_") then elseif string.find(n.name, "mcl_farming:carrot_") then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
local stages = math.random(2, 5) local stages = math.random(2, 5)
return mcl_farming:grow_plant("plant_carrot", pos, n, stages, true) return mcl_farming:grow_plant("plant_carrot", pos, n, stages, true)
elseif string.find(n.name, "mcl_farming:pumpkin_") then elseif string.find(n.name, "mcl_farming:pumpkin_") then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
local stages = math.random(2, 5) local stages = math.random(2, 5)
return mcl_farming:grow_plant("plant_pumpkin_stem", pos, n, stages, true) return mcl_farming:grow_plant("plant_pumpkin_stem", pos, n, stages, true)
elseif string.find(n.name, "mcl_farming:melontige_") then elseif string.find(n.name, "mcl_farming:melontige_") then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
local stages = math.random(2, 5) local stages = math.random(2, 5)
return mcl_farming:grow_plant("plant_melon_stem", pos, n, stages, true) return mcl_farming:grow_plant("plant_melon_stem", pos, n, stages, true)
elseif string.find(n.name, "mcl_farming:beetroot_") then elseif string.find(n.name, "mcl_farming:beetroot_") then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
-- Beetroot: 75% chance to advance to next stage -- Beetroot: 75% chance to advance to next stage
if math.random(1, 100) <= 75 then if math.random(1, 100) <= 75 then
return mcl_farming:grow_plant("plant_beetroot", pos, n, 1, true) return mcl_farming:grow_plant("plant_beetroot", pos, n, 1, true)
end end
elseif n.name == "mcl_cocoas:cocoa_1" or n.name == "mcl_cocoas:cocoa_2" then elseif n.name == "mcl_cocoas:cocoa_1" or n.name == "mcl_cocoas:cocoa_2" then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
-- Cocoa: Advance by 1 stage -- Cocoa: Advance by 1 stage
mcl_cocoas.grow(pos) mcl_cocoas.grow(pos)
return true return true
elseif minetest.get_item_group(n.name, "grass_block") == 1 then elseif minetest.get_item_group(n.name, "grass_block") == 1 then
local grass_block_pos = {x = pos.x, y = pos.y + 1, z = pos.z}
bone_meal_particle(grass_block_pos)
-- Grass Block: Generate tall grass and random flowers all over the place -- Grass Block: Generate tall grass and random flowers all over the place
for i = -2, 2 do for i = -2, 2 do
for j = -2, 2 do for j = -2, 2 do
@ -285,6 +299,7 @@ function mcl_dye.apply_bone_meal(pointed_thing)
-- Randomly generate flowers, tall grass or nothing -- Randomly generate flowers, tall grass or nothing
if math.random(1,100) <= 90 then if math.random(1,100) <= 90 then
-- 90% tall grass, 10% flower -- 90% tall grass, 10% flower
mcl_dye.add_bone_meal_particle(pos, {amount = 4})
if math.random(1,100) <= 90 then if math.random(1,100) <= 90 then
local col = n2.param2 local col = n2.param2
minetest.add_node(pos, {name="mcl_flowers:tallgrass", param2=col}) minetest.add_node(pos, {name="mcl_flowers:tallgrass", param2=col})
@ -314,24 +329,24 @@ function mcl_dye.apply_bone_meal(pointed_thing)
-- Double flowers: Drop corresponding item -- Double flowers: Drop corresponding item
elseif n.name == "mcl_flowers:rose_bush" or n.name == "mcl_flowers:rose_bush_top" then elseif n.name == "mcl_flowers:rose_bush" or n.name == "mcl_flowers:rose_bush_top" then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
minetest.add_item(pos, "mcl_flowers:rose_bush") minetest.add_item(pos, "mcl_flowers:rose_bush")
return true return true
elseif n.name == "mcl_flowers:peony" or n.name == "mcl_flowers:peony_top" then elseif n.name == "mcl_flowers:peony" or n.name == "mcl_flowers:peony_top" then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
minetest.add_item(pos, "mcl_flowers:peony") minetest.add_item(pos, "mcl_flowers:peony")
return true return true
elseif n.name == "mcl_flowers:lilac" or n.name == "mcl_flowers:lilac_top" then elseif n.name == "mcl_flowers:lilac" or n.name == "mcl_flowers:lilac_top" then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
minetest.add_item(pos, "mcl_flowers:lilac") minetest.add_item(pos, "mcl_flowers:lilac")
return true return true
elseif n.name == "mcl_flowers:sunflower" or n.name == "mcl_flowers:sunflower_top" then elseif n.name == "mcl_flowers:sunflower" or n.name == "mcl_flowers:sunflower_top" then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
minetest.add_item(pos, "mcl_flowers:sunflower") minetest.add_item(pos, "mcl_flowers:sunflower")
return true return true
elseif n.name == "mcl_flowers:tallgrass" then elseif n.name == "mcl_flowers:tallgrass" then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
-- Tall Grass: Grow into double tallgrass -- Tall Grass: Grow into double tallgrass
local toppos = { x=pos.x, y=pos.y+1, z=pos.z } local toppos = { x=pos.x, y=pos.y+1, z=pos.z }
local topnode = minetest.get_node(toppos) local topnode = minetest.get_node(toppos)
@ -342,7 +357,7 @@ function mcl_dye.apply_bone_meal(pointed_thing)
end end
elseif n.name == "mcl_flowers:fern" then elseif n.name == "mcl_flowers:fern" then
bone_meal_particle(pos) mcl_dye.add_bone_meal_particle(pos)
-- Fern: Grow into large fern -- Fern: Grow into large fern
local toppos = { x=pos.x, y=pos.y+1, z=pos.z } local toppos = { x=pos.x, y=pos.y+1, z=pos.z }
local topnode = minetest.get_node(toppos) local topnode = minetest.get_node(toppos)
@ -374,7 +389,7 @@ minetest.register_craftitem("mcl_dye:white", {
end end
-- Use the bone meal on the ground -- Use the bone meal on the ground
if(mcl_dye.apply_bone_meal(pointed_thing) and (not minetest.is_creative_enabled(user:get_player_name()))) then if (apply_bone_meal(pointed_thing, user) and (not minetest.is_creative_enabled(user:get_player_name()))) then
itemstack:take_item() itemstack:take_item()
end end
return itemstack return itemstack
@ -387,7 +402,7 @@ minetest.register_craftitem("mcl_dye:white", {
else else
pointed_thing = { above = pos, under = droppos } pointed_thing = { above = pos, under = droppos }
end end
local success = mcl_dye.apply_bone_meal(pointed_thing) local success = apply_bone_meal(pointed_thing, nil)
if success then if success then
stack:take_item() stack:take_item()
end end