2014-08-14 16:22:03 +02:00
|
|
|
----------------------
|
|
|
|
-- Vector functions --
|
|
|
|
----------------------
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.vector_cross(a, b)
|
2014-08-14 16:22:03 +02:00
|
|
|
return {
|
|
|
|
x = a.y * b.z - a.z * b.y,
|
|
|
|
y = a.z * b.x - a.x * b.z,
|
|
|
|
z = a.x * b.y - a.y * b.x
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.vector_dot(a, b)
|
2014-08-14 16:22:03 +02:00
|
|
|
return a.x * b.x + a.y * b.y + a.z * b.z
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------
|
|
|
|
-- Facedir functions --
|
|
|
|
-----------------------
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.facedir_to_top_dir(facedir)
|
2014-08-14 16:22:03 +02:00
|
|
|
return ({[0] = {x = 0, y = 1, z = 0},
|
|
|
|
{x = 0, y = 0, z = 1},
|
|
|
|
{x = 0, y = 0, z = -1},
|
|
|
|
{x = 1, y = 0, z = 0},
|
|
|
|
{x = -1, y = 0, z = 0},
|
|
|
|
{x = 0, y = -1, z = 0}})
|
|
|
|
[math.floor(facedir / 4)]
|
|
|
|
end
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.facedir_to_right_dir(facedir)
|
|
|
|
return pipeworks.vector_cross(
|
|
|
|
pipeworks.facedir_to_top_dir(facedir),
|
2014-08-14 16:22:03 +02:00
|
|
|
minetest.facedir_to_dir(facedir)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
local directions = {}
|
|
|
|
pipeworks.directions = directions
|
2014-08-14 16:22:03 +02:00
|
|
|
function directions.side_to_dir(side)
|
|
|
|
return ({[0] = vector.new(),
|
|
|
|
vector.new( 0, 1, 0),
|
|
|
|
vector.new( 0, -1, 0),
|
|
|
|
vector.new( 1, 0, 0),
|
|
|
|
vector.new(-1, 0, 0),
|
|
|
|
vector.new( 0, 0, 1),
|
|
|
|
vector.new( 0, 0, -1)
|
|
|
|
})[side]
|
|
|
|
end
|
|
|
|
|
|
|
|
function directions.dir_to_side(dir)
|
2017-04-04 09:25:27 +02:00
|
|
|
local c = pipeworks.vector_dot(dir, vector.new(1, 2, 3)) + 4
|
2014-08-14 16:22:03 +02:00
|
|
|
return ({6, 2, 4, 0, 3, 1, 5})[c]
|
|
|
|
end
|
|
|
|
|
|
|
|
----------------------
|
|
|
|
-- String functions --
|
|
|
|
----------------------
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
--[[function pipeworks.string_split(str, sep)
|
2014-08-14 16:22:03 +02:00
|
|
|
local fields = {}
|
|
|
|
local index = 1
|
|
|
|
local expr = "([^"..sep.."])+"
|
|
|
|
string.gsub(str, expr, function(substring)
|
|
|
|
fields[index] = substring
|
|
|
|
index = index + 1
|
|
|
|
end)
|
|
|
|
return fields
|
|
|
|
end]]
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.string_startswith(str, substr)
|
2014-08-14 16:22:03 +02:00
|
|
|
return str:sub(1, substr:len()) == substr
|
|
|
|
end
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
-- Table functions --
|
|
|
|
---------------------
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.table_contains(tbl, element)
|
2014-08-14 16:22:03 +02:00
|
|
|
for _, elt in pairs(tbl) do
|
|
|
|
if elt == element then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.table_extend(tbl, tbl2)
|
2014-08-14 16:22:03 +02:00
|
|
|
local index = #tbl + 1
|
|
|
|
for _, elt in ipairs(tbl2) do
|
|
|
|
tbl[index] = elt
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.table_recursive_replace(tbl, pattern, replace_with)
|
2014-08-14 16:22:03 +02:00
|
|
|
if type(tbl) == "table" then
|
|
|
|
local tbl2 = {}
|
|
|
|
for key, value in pairs(tbl) do
|
2017-04-04 09:25:27 +02:00
|
|
|
tbl2[key] = pipeworks.table_recursive_replace(value, pattern, replace_with)
|
2014-08-14 16:22:03 +02:00
|
|
|
end
|
|
|
|
return tbl2
|
|
|
|
elseif type(tbl) == "string" then
|
|
|
|
return tbl:gsub(pattern, replace_with)
|
|
|
|
else
|
|
|
|
return tbl
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
------------------------
|
|
|
|
-- Formspec functions --
|
|
|
|
------------------------
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
local fs_helpers = {}
|
|
|
|
pipeworks.fs_helpers = fs_helpers
|
2014-08-14 16:22:03 +02:00
|
|
|
function fs_helpers.on_receive_fields(pos, fields)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
for field, value in pairs(fields) do
|
2017-04-04 09:25:27 +02:00
|
|
|
if pipeworks.string_startswith(field, "fs_helpers_cycling:") then
|
2014-08-14 16:22:03 +02:00
|
|
|
local l = field:split(":")
|
|
|
|
local new_value = tonumber(l[2])
|
|
|
|
local meta_name = l[3]
|
|
|
|
meta:set_int(meta_name, new_value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function fs_helpers.cycling_button(meta, base, meta_name, values)
|
|
|
|
local current_value = meta:get_int(meta_name)
|
|
|
|
local new_value = (current_value + 1) % (#values)
|
2015-01-26 23:23:44 +01:00
|
|
|
local val = values[current_value + 1]
|
|
|
|
local text
|
|
|
|
local texture_name = nil
|
2015-01-27 23:14:05 +01:00
|
|
|
local addopts = nil
|
2015-01-26 23:23:44 +01:00
|
|
|
--when we get a table, we know the caller wants an image_button
|
|
|
|
if type(val) == "table" then
|
|
|
|
text = val["text"]
|
|
|
|
texture_name = val["texture"]
|
2015-01-27 23:14:05 +01:00
|
|
|
addopts = val["addopts"]
|
2015-01-26 23:23:44 +01:00
|
|
|
else
|
|
|
|
text = val
|
|
|
|
end
|
2014-08-14 16:22:03 +02:00
|
|
|
local field = "fs_helpers_cycling:"..new_value..":"..meta_name
|
2015-01-27 23:14:05 +01:00
|
|
|
return base..";"..(texture_name and texture_name..";" or "")..field..";"..minetest.formspec_escape(text)..(addopts and ";"..addopts or "").."]"
|
2014-08-14 16:22:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---------
|
|
|
|
-- Env --
|
|
|
|
---------
|
|
|
|
|
2017-04-04 09:25:27 +02:00
|
|
|
function pipeworks.load_position(pos)
|
2014-08-23 23:59:32 +02:00
|
|
|
if pos.x < -30912 or pos.y < -30912 or pos.z < -30912 or
|
|
|
|
pos.x > 30927 or pos.y > 30927 or pos.z > 30927 then return end
|
2014-08-14 16:22:03 +02:00
|
|
|
if minetest.get_node_or_nil(pos) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local vm = minetest.get_voxel_manip()
|
|
|
|
vm:read_from_map(pos, pos)
|
2014-08-17 15:53:21 +02:00
|
|
|
end
|