Fix next sluice bug

This commit is contained in:
Joachim Stolberg 2021-08-18 19:48:37 +02:00
parent 83d9991ffb
commit 692777ec6f

View File

@ -12,74 +12,62 @@
]]-- ]]--
local M = minetest.get_meta
local S = techage.S local S = techage.S
local M = minetest.get_meta
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local S2P = minetest.string_to_pos
local function check_position(pos, facedir) local function check_position(pos, facedir)
local dir = minetest.facedir_to_dir(facedir) local dir = minetest.facedir_to_dir(facedir)
local pos_ = vector.add(pos, dir) local pos_ = vector.add(pos, dir)
local node = minetest.get_node(pos_) local node = minetest.get_node(pos_)
return node.name == "default:water_source" or node.name == "default:water_flowing" return (node.name == "default:water_source" or node.name == "default:water_flowing"), pos_
end end
-- Function checks if a millpond is avaliable and -- Function checks if water is avaliable and
-- returns the pos for the new water block, and the type of block. -- returns the pos for the new water block, and the result (true/false).
local function has_water(pos, facedir) local function has_water(pos, facedir)
local res, dir, pos2 local res1, pos1 = check_position(pos, (facedir + 1) % 4)
local res2, pos2 = check_position(pos, (facedir + 3) % 4)
-- check left side if res1 and not res2 then
res = check_position(pos, (facedir + 3) % 4) M(pos):set_string("millrace_pos", P2S(pos2))
dir = minetest.facedir_to_dir((facedir + 1) % 4) return pos2, true
pos2 = vector.add(pos, dir)
if res == true then
return pos2, "water"
else
return pos2, "air"
end end
-- check right side if not res1 and res2 then
res = check_position(pos, (facedir + 1) % 4) M(pos):set_string("millrace_pos", P2S(pos1))
dir = minetest.facedir_to_dir((facedir + 3) % 4) return pos1, true
pos2 = vector.add(pos, dir)
if res == true then
return pos2, "water"
else
return pos2, "air"
end end
local pos3 = S2P(M(pos):get_string("millrace_pos"))
if pos3 then
return pos3, true
end
return pos1, false
end end
local function on_rightclick(pos, node, clicker, itemstack, pointed_thing) local function on_rightclick(pos, node, clicker, itemstack, pointed_thing)
local pos2 = vector.add(pos, {x = 0, y = -1, z = 0}) local pos2 = vector.add(pos, {x = 0, y = -1, z = 0})
local node2 = minetest.get_node(pos2) local node2 = minetest.get_node(pos2)
local pos3, res = has_water(pos2, node2.param2, clicker) local pos3, res = has_water(pos2, node2.param2)
local node3 = minetest.get_node(pos3) local node3 = minetest.get_node(pos3)
if node2.name == "techage:ta1_sluice_closed" then if node2.name == "techage:ta1_sluice_closed" then
minetest.swap_node(pos, {name = "techage:ta1_sluice_handle_open", param2 = node.param2}) minetest.swap_node(pos, {name = "techage:ta1_sluice_handle_open", param2 = node.param2})
minetest.swap_node(pos2, {name = "techage:ta1_sluice_open", param2 = node.param2}) minetest.swap_node(pos2, {name = "techage:ta1_sluice_open", param2 = node.param2})
if res == "water" then if res then
if node3.name == "air" or node3.name == "default:water_flowing" then minetest.add_node(pos3, {name = "default:water_source"})
minetest.add_node(pos3, {name = "default:water_source"}) minetest.get_node_timer(pos3):start(2)
minetest.get_node_timer(pos3):start(2)
end
else
minetest.add_node(pos3, {name = "air"})
end end
minetest.sound_play("doors_door_open", {gain = 0.5, pos = pos, minetest.sound_play("doors_door_open", {gain = 0.5, pos = pos,
max_hear_distance = 10}, true) max_hear_distance = 10}, true)
elseif node2.name == "techage:ta1_sluice_open" then elseif node2.name == "techage:ta1_sluice_open" then
minetest.swap_node(pos, {name = "techage:ta1_sluice_handle_closed", param2 = node.param2}) minetest.swap_node(pos, {name = "techage:ta1_sluice_handle_closed", param2 = node.param2})
minetest.swap_node(pos2, {name = "techage:ta1_sluice_closed", param2 = node.param2}) minetest.swap_node(pos2, {name = "techage:ta1_sluice_closed", param2 = node.param2})
if res == "water" then if res then
if node3.name == "default:water_source" then minetest.add_node(pos3, {name = "air"})
minetest.add_node(pos3, {name = "default:water_flowing"})
end
else
if node3.name == "default:water_flowing" then
minetest.add_node(pos3, {name = "air"})
end
end end
minetest.sound_play("doors_door_close", {gain = 0.5, pos = pos, minetest.sound_play("doors_door_close", {gain = 0.5, pos = pos,
max_hear_distance = 10}, true) max_hear_distance = 10}, true)