sponge/init.lua

220 lines
8.6 KiB
Lua
Raw Permalink Normal View History

--[[
2023-02-20 15:17:15 +01:00
- Minetest Sponge mod
- Water removing sponges for Minetest
- Created by Benjie/fiftysix/56 2018-08-27 - Last updated 2023-02-21
2023-02-20 15:17:15 +01:00
- Copyright Benjie 2018-2023
2020-12-22 02:12:06 +01:00
Soggy sponges can rarely be found deep in the sea where the darkness begins.
These can be cooked into dry sponges, and then placed near a liquid to remove the surrounding sphere of it
2020-12-22 02:12:06 +01:00
They will hold the water away until they are removed.
This will cause them to become soggy, so to use them again they have to be cooked.
Sponges create a diameter 9 sphere of air-like nodes that water can't flow through (checks for protection)
2020-12-22 02:12:06 +01:00
If sponges have cleared more than 3 nodes of liquid, they become soggy sponges
removing a sponge or soggy sponge will turn a diameter 9 sphere of air-like nodes back into air
(air-like nodes can be removed in protection by removing a sponge outside the protection, they are not meant to be permanent)
]]--
local modname = minetest.get_current_modname()
2020-12-22 02:12:06 +01:00
local keep_dry = 3 -- The maximum amount of water cleared where the sponge doesn't turn soggy
-- Load configurable options
local replace_air_nodes = minetest.settings:get_bool("sponge_replace_air_nodes", true)
local group_list = minetest.settings:get("sponge_group_list", false) or "water"
local group_list_type = minetest.settings:get("sponge_group_list_type") or "whitelist_source"
local radius = tonumber(minetest.settings:get("sponge_radius") or 4.5)
local shape_sphere = minetest.settings:get_bool("sponge_shape_sphere", true)
local replace_with_air = minetest.settings:get_bool("sponge_replace_with_air", false)
-- returns true if groups contains anything in list
local compare_groups = function(list, groups)
for group in list:gmatch("([^,]+)") do
if groups[group] and groups[group] > 0 then
return true
end
end
return false
end
2020-12-22 02:12:06 +01:00
-- called by after_destruct()
local destruct = function(pos) -- removing the air-like nodes
-- if air was used, there is nothing to remove
if replace_with_air then return end
local edge_distance = math.floor(radius)
2020-12-22 02:12:06 +01:00
-- find all sponges that intersect with this sponge's radius
2020-12-22 02:12:06 +01:00
local sponge_info = minetest.find_nodes_in_area(
vector.new(pos.x-edge_distance*2, pos.y-edge_distance*2, pos.z-edge_distance*2),
vector.new(pos.x+edge_distance*2, pos.y+edge_distance*2, pos.z+edge_distance*2),
2020-12-22 02:12:06 +01:00
{modname..":soggy_sponge", modname..":sponge"}, true
)
local sponges = {}
for _, p in pairs(sponge_info) do
2023-02-21 01:50:45 +01:00
local node = minetest.get_node(p[1])
2020-12-22 02:12:06 +01:00
if node.param1 == 1 or node.name == modname..":sponge" then
2023-02-21 01:50:45 +01:00
table.insert(sponges, p[1])
2020-12-22 02:12:06 +01:00
end
end
for x = pos.x-edge_distance, pos.x+edge_distance do
for y = pos.y-edge_distance, pos.y+edge_distance do
for z = pos.z-edge_distance, pos.z+edge_distance do
local p = vector.new(x, y, z)
if (not shape_sphere or p:distance(pos) <= radius) then
local n = minetest.get_node(p).name
if n == modname..":liquid_stop" then
-- check if position intersects with another sponge
local intersect = false
for _, s_pos in pairs(sponges) do
if (
(shape_sphere and p:distance(s_pos) <= radius) or
(not shape_sphere and
math.abs(s_pos.x-x) <= radius and math.abs(s_pos.y-y) <= radius and math.abs(s_pos.z-z) <= radius)
) then
intersect = true
break
end
end
if not intersect then
minetest.remove_node(p)
2020-12-22 02:12:06 +01:00
end
end
end
end
end
end
end
-- called by after_place_node()
local construct = function(pos, placer, itemstack, pointed_thing)
local playername = placer:get_player_name()
if not minetest.is_protected(pos, playername) then
local count = 0
local edge_distance = math.floor(radius)
for x = pos.x-edge_distance, pos.x+edge_distance do
for y = pos.y-edge_distance, pos.y+edge_distance do
for z = pos.z-edge_distance, pos.z+edge_distance do
local p = vector.new(x, y, z)
if (not shape_sphere or p:distance(pos) <= radius) then
local n = minetest.get_node(p).name
local def = minetest.registered_nodes[n]
local replace = false
if def == nil or minetest.is_protected(p, playername) then
-- replace = false
elseif replace_air_nodes and n == "air" then
replace = true
elseif def["drawtype"] == "flowingliquid" then
if group_list_type == "whitelist" then
if compare_groups(group_list, def.groups or {}) then
replace = true
end
elseif group_list_type == "blacklist" then
if not compare_groups(group_list, def.groups or {}) then
replace = true
end
else
replace = true
end
elseif def["drawtype"] == "liquid" then
if group_list_type == "whitelist" or group_list_type == "whitelist_source" then
if compare_groups(group_list, def.groups or {}) then
replace = true
end
elseif group_list_type == "blacklist" or group_list_type == "blacklist_source" then
if not compare_groups(group_list, def.groups or {}) then
replace = true
end
else
replace = true
end
end
if replace then
if n ~= "air" then
count = count + 1 -- counting liquids
end
if replace_with_air then
minetest.remove_node(p)
else
minetest.set_node(p, {name=modname..":liquid_stop"})
end
2020-12-22 02:12:06 +01:00
end
end
end
end
end
2020-12-22 02:12:06 +01:00
if count > keep_dry then -- turns soggy if it removed more than `keep_dry` nodes
minetest.swap_node(pos, {name=modname..":soggy_sponge", param1=1})
end
end
end
if replace_with_air then
minetest.register_alias(modname..":liquid_stop", "air")
else
minetest.register_node(modname..":liquid_stop", { -- air-like node
description = "liquid blocker for sponges",
drawtype = "airlike",
drop = {max_items=0, items={}},
groups = {not_in_creative_inventory=1},
pointable = false,
walkable = false,
floodable = false,
sunlight_propagates = true,
paramtype = "light",
buildable_to = true,
})
end
2020-12-22 02:12:06 +01:00
minetest.register_node(modname..":sponge", { -- dry sponge
description = "Sponge",
tiles = {"sponge_sponge.png"},
groups = {crumbly=3},
sounds = default.node_sound_dirt_defaults(),
2020-12-22 02:12:06 +01:00
after_place_node = construct,
2020-12-22 02:12:06 +01:00
after_destruct = destruct,
})
2020-12-22 02:12:06 +01:00
minetest.register_node(modname..":soggy_sponge", { -- soggy sponge
description = "Soggy sponge",
tiles = {"sponge_soggy_sponge.png"},
groups = {crumbly=3},
sounds = default.node_sound_dirt_defaults(),
2020-12-22 02:12:06 +01:00
after_destruct = destruct,
})
minetest.register_craft({ -- cooking soggy sponge back into dry sponge
type = "cooking",
2020-12-22 02:12:06 +01:00
recipe = modname..":soggy_sponge",
output = modname..":sponge",
cooktime = 2,
})
minetest.register_decoration({ -- sponges are found deep in the sea
2020-12-22 02:12:06 +01:00
name = modname..":sponges",
deco_type = "simple",
place_on = {"default:sand"},
spawn_by = "default:water_source",
num_spawn_by = 3,
fill_ratio = 0.0003,
y_max = minetest.settings:get("sponge_generation_depth") or -9,
flags = "force_placement",
2020-12-22 02:12:06 +01:00
decoration = modname..":soggy_sponge",
})