MineClone2/mods/ITEMS/mcl_sponges/init.lua

286 lines
9.2 KiB
Lua

local S = minetest.get_translator("mcl_sponges")
local math = math
mcl_sponges = {}
mcl_sponges.registered_sponges = {}
function mcl_sponges.register_sponge(name, def)
if def.furnace_drying == nil then def.furnace_drying = true end
if not def.tiles then def.tiles = {"mcl_sponges_sponge_wet.png"} end
if not def.description then def.description = S("Logged Sponge") end
def.itemname = "mcl_sponges:sponge_"..name.."_logged"
mcl_sponges.registered_sponges[name] = def
minetest.register_node(def.itemname, {
description = def.description,
_tt_help = def.help,
_doc_items_longdesc = def.longdesc,
is_ground_content = false,
tiles = {"mcl_sponges_sponge_wet.png"},
sounds = mcl_sounds.node_sound_dirt_defaults(),
groups = {handy=1, hoey=1, building_block=1, sponge_logged=1},
on_place = function(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
return itemstack
end
-- Use pointed node's on_rightclick function first, if present
local node = minetest.get_node(pointed_thing.under)
local new_stack = mcl_util.call_on_rightclick(itemstack, player, pointed_thing)
if new_stack then
return new_stack
end
local name = placer:get_player_name()
if minetest.is_protected(pointed_thing.above, name) then
return itemstack
end
if def.on_place then
local new_item = def.on_place(itemstack, placer, pointed_thing)
if new_item then return new_item end
end
return minetest.item_place_node(itemstack, placer, pointed_thing)
end,
_mcl_blast_resistance = 0.6,
_mcl_hardness = 0.6,
})
--drying sponge
if def.furnace_drying then
minetest.register_craft({
type = "cooking",
output = "mcl_sponges:sponge",
recipe = def.itemname,
cooktime = 10,
})
end
end
function mcl_sponges.absorb(pos)
local change = false
-- Count number of absorbed liquid by type vs other nodes
-- to determine the wet sponge type.
local out = {}
local p, n
local count = 0
for i=-3,3 do
for j=-3,3 do
for k=-3,3 do
count = count + 1
pos = {x=pos.x+i, y=pos.y+j, z=pos.z+k}
node = minetest.get_node(pos)
for name,def in pairs(mcl_sponges.registered_sponges) do
if minetest.get_item_group(node.name, def.liquidgroup) ~= 0 then
minetest.add_node(pos, {name="air"})
change = true
if not out[name] then out[name] = 0 end
out[name] = out[name]+1
break
end
end
end
end
end
--DEBUG
minetest.chat_send_all(count)
-- The dominant liquid type wins. In case of a tie, normal water wins.
-- This slight bias is intentional.
local sponge_type
local biggest
for name, number in pairs(out) do
if biggest then
if biggest < number then
sponge_type = name
biggest = number
end
else
sponge_type = name
biggest = number
end
end
return change, mcl_sponges.registered_sponges[sponge_type].itemname
end
minetest.register_node("mcl_sponges:sponge", {
description = S("Sponge"),
_tt_help = S("Removes water on contact"),
_doc_items_longdesc = S("Sponges are blocks which remove certain liquid nodes around them when they are placed or come in contact with liquid, turning it into a wet sponge."),
is_ground_content = false,
tiles = {"mcl_sponges_sponge.png"},
sounds = mcl_sounds.node_sound_dirt_defaults(),
groups = {handy=1, hoey=1, building_block=1, sponge_dry=1},
on_place = function(itemstack, placer, pointed_thing)
local pn = placer:get_player_name()
if pointed_thing.type ~= "node" then
return itemstack
end
-- Use pointed node's on_rightclick function first, if present
local new_stack = mcl_util.call_on_rightclick(itemstack, player, pointed_thing)
if new_stack then
return new_stack
end
if minetest.is_protected(pointed_thing.above, pn) then
return itemstack
end
local pos = pointed_thing.above
local on_water = false
if minetest.get_item_group(minetest.get_node(pos).name, "liquid") ~= 0 then
on_water = true
end
local water_found = minetest.find_node_near(pos, 1, "group:liquid")
if water_found ~= nil then
on_water = true
end
if on_water then
-- Absorb water
-- FIXME: pos is not always the right placement position because of pointed_thing
local absorbed, wet_sponge = mcl_sponges.absorb(pos)
if absorbed then
minetest.item_place_node(ItemStack(wet_sponge), placer, pointed_thing)
if not minetest.is_creative_enabled(placer:get_player_name()) then
itemstack:take_item()
end
return itemstack
end
end
return minetest.item_place_node(itemstack, placer, pointed_thing)
end,
_mcl_blast_resistance = 0.6,
_mcl_hardness = 0.6,
})
function place_wet_sponge(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
return itemstack
end
-- Use pointed node's on_rightclick function first, if present
local node = minetest.get_node(pointed_thing.under)
local new_stack = mcl_util.call_on_rightclick(itemstack, player, pointed_thing)
if new_stack then
return new_stack
end
local name = placer:get_player_name()
if minetest.is_protected(pointed_thing.above, name) then
return itemstack
end
if mcl_worlds.pos_to_dimension(pointed_thing.above) == "nether" then
minetest.item_place_node(ItemStack("mcl_sponges:sponge"), placer, pointed_thing)
local pos = pointed_thing.above
for n = 0, 25 do
minetest.add_particle({
pos = {x = pos.x + math.random(-1, 1)*math.random()/2, y = pos.y + 0.6, z = pos.z + math.random(-1, 1)*math.random()/2},
velocity = {x = 0, y = math.random(), z = 0},
acceleration = {x=0, y=0, z=0},
expirationtime = math.random(),
collisiondetection = false,
vertical = false,
size = math.random(2, 5),
texture = "mcl_particles_sponge"..math.random(1, 5)..".png",
})
end
if not minetest.is_creative_enabled(name) then
itemstack:take_item()
end
return itemstack
end
return minetest.item_place_node(itemstack, placer, pointed_thing)
end
--[[if minetest.get_modpath("mclx_core") then
minetest.register_node("mcl_sponges:sponge_wet_river_water", {
description = S("Riverwaterlogged Sponge"),
_tt_help = S("Can be dried in furnace"),
_doc_items_longdesc = S("This is a sponge soaking wet with river water. It can be dried in the furnace to turn it into (dry) sponge. When there's an empty bucket in the fuel slot of the furnace, the river water will pour into the bucket.") .. "\n" .. S("A sponge becomes riverwaterlogged (instead of waterlogged) if it sucks up more river water than (normal) water."),
drawtype = "normal",
is_ground_content = false,
tiles = {"mcl_sponges_sponge_wet_river_water.png"},
walkable = true,
pointable = true,
diggable = true,
buildable_to = false,
stack_max = 64,
sounds = mcl_sounds.node_sound_dirt_defaults(),
groups = {handy=1, building_block=1},
on_place = place_wet_sponge,
_mcl_blast_resistance = 0.6,
_mcl_hardness = 0.6,
})
minetest.register_craft({
type = "cooking",
output = "mcl_sponges:sponge",
recipe = "mcl_sponges:sponge_wet_river_water",
cooktime = 10,
})
end]]
mcl_sponges.register_sponge("water", {
description = S("Waterlogged Sponge"),
help = S("Can be dried in furnace"),
longdesc = S("A waterlogged sponge can be dried in the furnace to turn it into (dry) sponge. When there's an empty bucket in the fuel slot of a furnace, the water will pour into the bucket."),
tiles = {"mcl_sponges_sponge_wet.png"},
liquidgroup = "legacity_water",
furnace_drying = true,
on_place = function(itemstack, placer, pointed_thing)
if mcl_worlds.pos_to_dimension(pointed_thing.above) == "nether" then
minetest.item_place_node(ItemStack("mcl_sponges:sponge"), placer, pointed_thing)
local pos = pointed_thing.above
for n = 0, 25 do
minetest.add_particle({
pos = {x = pos.x + math.random(-1, 1)*math.random()/2, y = pos.y + 0.6, z = pos.z + math.random(-1, 1)*math.random()/2},
velocity = {x = 0, y = math.random(), z = 0},
acceleration = {x=0, y=0, z=0},
expirationtime = math.random(),
collisiondetection = false,
vertical = false,
size = math.random(2, 5),
texture = "mcl_particles_sponge"..math.random(1, 5)..".png",
})
end
if not minetest.is_creative_enabled(name) then
itemstack:take_item()
end
return itemstack
end
end,
})
mcl_sponges.register_sponge("riverwater", {
description = S("Riverwaterlogged Sponge"),
help = S("Can be dried in furnace"),
longdesc = S("This is a sponge soaking wet with river water. It can be dried in the furnace to turn it into (dry) sponge. When there's an empty bucket in the fuel slot of the furnace, the river water will pour into the bucket.") .. "\n" .. S("A sponge becomes riverwaterlogged (instead of waterlogged) if it sucks up more river water than (normal) water."),
tiles = {"mcl_sponges_sponge_wet.png"},
liquidgroup = "extra_water",
furnace_drying = true,
})
minetest.register_abm({
label = "Sponge water absorbtion",
nodenames = { "mcl_sponges:sponge" },
neighbors = { "group:water" }, -- FIXME
interval = 1,
chance = 1,
action = function(pos)
local absorbed, wet_sponge = mcl_sponges.absorb(pos)
if absorbed then
minetest.add_node(pos, {name = wet_sponge})
end
end,
})
minetest.register_alias("mcl_sponges:sponge_wet", "mcl_sponges:sponge_water_logged")
minetest.register_alias("mcl_sponges:sponge_wet_river_water", "mcl_sponges:sponge_riverwater_logged")