2016-08-25 21:58:28 +02:00
|
|
|
--[[
|
2018-02-01 16:54:55 +01:00
|
|
|
display_api mod for Minetest - Library to add dynamic display
|
2016-08-25 21:58:28 +02:00
|
|
|
capabilities to nodes
|
|
|
|
(c) Pierre-Yves Rollo
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
--]]
|
2015-11-11 14:26:39 +01:00
|
|
|
|
2018-02-01 16:54:55 +01:00
|
|
|
display_api = {}
|
2015-11-11 14:26:39 +01:00
|
|
|
|
2018-01-18 21:46:10 +01:00
|
|
|
-- Prefered gap between node and entity
|
|
|
|
-- Entity positionment is up to mods but it is a good practice to use this
|
|
|
|
-- variable as spacing between entity and node
|
2018-02-01 16:54:55 +01:00
|
|
|
display_api.entity_spacing = 0.002
|
2018-01-18 21:46:10 +01:00
|
|
|
|
2015-11-11 14:26:39 +01:00
|
|
|
-- Miscelaneous values depending on wallmounted param2
|
|
|
|
local wallmounted_values = {
|
2015-11-28 20:44:04 +01:00
|
|
|
[0]={dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0}, -- Should never be used
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=1}, -- Should never be used
|
|
|
|
{dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2, rotate=5},
|
|
|
|
{dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2, rotate=4},
|
|
|
|
{dx=0, dz=-1, rx=1, rz=0, yaw=0, rotate=2},
|
|
|
|
{dx=0, dz=1, rx=-1, rz=0, yaw=math.pi, rotate=3}
|
2015-11-11 14:26:39 +01:00
|
|
|
}
|
|
|
|
|
2015-11-28 20:44:04 +01:00
|
|
|
-- Miscelaneous values depending on facedir param2
|
|
|
|
local facedir_values = {
|
|
|
|
[0]={dx=0, dz=-1, rx=1, rz=0, yaw=0, rotate=1},
|
|
|
|
{dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2, rotate=2},
|
|
|
|
{dx=0, dz=1, rx=-1, rz=0, yaw=math.pi, rotate=3},
|
|
|
|
{dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2, rotate=0},
|
|
|
|
-- Forbiden values :
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
|
|
|
|
}
|
|
|
|
|
|
|
|
-- dx/dy = depth vector, rx/ly = right vector, yaw = yaw of entity,
|
|
|
|
-- rotate = next facedir/wallmount on rotate
|
|
|
|
|
|
|
|
local function get_values(node)
|
|
|
|
local ndef = minetest.registered_nodes[node.name]
|
|
|
|
|
|
|
|
if ndef then
|
|
|
|
if ndef.paramtype2 == "wallmounted" then
|
|
|
|
return wallmounted_values[node.param2]
|
|
|
|
end
|
|
|
|
if ndef.paramtype2 == "facedir" then
|
|
|
|
return facedir_values[node.param2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-15 17:26:59 +01:00
|
|
|
--- Checks if the object is related to the given position
|
|
|
|
local function check_entity_pos(pos, objref)
|
|
|
|
local real_pos = vector.round(objref:get_pos())
|
|
|
|
local pos_hash = objref:get_luaentity().pos
|
|
|
|
if pos_hash == nil then
|
|
|
|
return vector.equals(real_pos, vector.round(pos))
|
|
|
|
else
|
|
|
|
return vector.equals(minetest.get_position_from_hash(pos_hash), pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-11 14:26:39 +01:00
|
|
|
--- Gets the display entities attached with a node. Removes extra ones
|
|
|
|
local function get_entities(pos)
|
|
|
|
local objrefs = {}
|
|
|
|
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
|
|
|
|
if ndef and ndef.display_entities then
|
2018-02-15 17:26:59 +01:00
|
|
|
for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do
|
2015-11-11 14:26:39 +01:00
|
|
|
local entity = objref:get_luaentity()
|
2018-02-15 17:26:59 +01:00
|
|
|
if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then
|
2015-11-11 14:26:39 +01:00
|
|
|
if objrefs[entity.name] then
|
|
|
|
objref:remove()
|
|
|
|
else
|
|
|
|
objrefs[entity.name] = objref
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return objrefs
|
|
|
|
end
|
|
|
|
|
|
|
|
local function clip_pos_prop(posprop)
|
|
|
|
if posprop then
|
2018-02-15 17:26:59 +01:00
|
|
|
return math.max(-1.5, math.min(1.5, posprop))
|
2015-11-11 14:26:39 +01:00
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- (Create and) place display entities according to the node orientation
|
|
|
|
local function place_entities(pos)
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local ndef = minetest.registered_nodes[node.name]
|
2015-11-28 20:44:04 +01:00
|
|
|
local values = get_values(node)
|
2015-11-11 14:26:39 +01:00
|
|
|
local objrefs = get_entities(pos)
|
|
|
|
|
2015-11-28 20:44:04 +01:00
|
|
|
if values and ndef and ndef.display_entities then
|
|
|
|
|
2015-11-11 14:26:39 +01:00
|
|
|
for entity_name, props in pairs(ndef.display_entities) do
|
|
|
|
local depth = clip_pos_prop(props.depth)
|
2015-11-28 20:44:04 +01:00
|
|
|
local height = clip_pos_prop(props.height)
|
|
|
|
local right = clip_pos_prop(props.right)
|
2015-11-11 14:26:39 +01:00
|
|
|
if not objrefs[entity_name] then
|
|
|
|
objrefs[entity_name] = minetest.add_entity(pos, entity_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
objrefs[entity_name]:setpos({
|
2015-11-28 20:44:04 +01:00
|
|
|
x = pos.x - values.dx * depth + values.rx * right,
|
|
|
|
y = pos.y + height,
|
|
|
|
z = pos.z - values.dz * depth + values.rz * right})
|
2015-11-11 14:26:39 +01:00
|
|
|
|
|
|
|
objrefs[entity_name]:setyaw(values.yaw)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return objrefs
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Call on_display_update callback of a node for one of its display entities
|
|
|
|
local function call_node_on_display_update(pos, objref)
|
|
|
|
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
|
|
|
|
local entity = objref:get_luaentity()
|
|
|
|
if ndef and ndef.display_entities and entity and ndef.display_entities[entity.name] then
|
|
|
|
ndef.display_entities[entity.name].on_display_update(pos, objref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Force entity update
|
2018-02-01 16:54:55 +01:00
|
|
|
function display_api.update_entities(pos)
|
2015-11-11 14:26:39 +01:00
|
|
|
local objrefs = place_entities(pos)
|
|
|
|
for _, objref in pairs(objrefs) do
|
2018-02-15 17:26:59 +01:00
|
|
|
objref:get_luaentity().pos = minetest.hash_node_position(pos)
|
2015-11-11 14:26:39 +01:00
|
|
|
call_node_on_display_update(pos, objref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-01 16:54:55 +01:00
|
|
|
--- On_activate callback for display_api entities. Calls on_display_update callbacks
|
2015-11-11 14:26:39 +01:00
|
|
|
--- of corresponding node for each entity.
|
2018-02-01 16:54:55 +01:00
|
|
|
function display_api.on_activate(entity, staticdata)
|
2018-02-15 17:26:59 +01:00
|
|
|
if entity then
|
|
|
|
if string.sub(staticdata, 1, string.len("return")) == "return" then
|
|
|
|
local data = core.deserialize(staticdata)
|
|
|
|
if data and type(data) == "table" then
|
|
|
|
entity.pos = data.pos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
entity.object:set_armor_groups({immortal=1})
|
|
|
|
local pos
|
|
|
|
if entity.pos then
|
|
|
|
pos = minetest.get_position_from_hash(entity.pos)
|
|
|
|
else
|
|
|
|
pos = entity.object:getpos()
|
|
|
|
end
|
|
|
|
display_api.update_entities(pos)
|
|
|
|
end
|
2015-11-11 14:26:39 +01:00
|
|
|
end
|
|
|
|
|
2018-02-01 16:54:55 +01:00
|
|
|
--- On_place callback for display_api items. Does nothing more than preventing item
|
2015-11-11 14:26:39 +01:00
|
|
|
--- from being placed on ceiling or ground
|
2018-02-01 16:54:55 +01:00
|
|
|
function display_api.on_place(itemstack, placer, pointed_thing)
|
2017-08-26 14:12:38 +02:00
|
|
|
local ndef = itemstack:get_definition()
|
2015-11-11 14:26:39 +01:00
|
|
|
local above = pointed_thing.above
|
|
|
|
local under = pointed_thing.under
|
|
|
|
local dir = {x = under.x - above.x,
|
|
|
|
y = under.y - above.y,
|
|
|
|
z = under.z - above.z}
|
|
|
|
|
2017-08-26 14:12:38 +02:00
|
|
|
if ndef then
|
|
|
|
if ndef.paramtype2 == "wallmounted" then
|
2015-11-28 20:44:04 +01:00
|
|
|
|
2017-08-26 14:12:38 +02:00
|
|
|
local wdir = minetest.dir_to_wallmounted(dir)
|
2015-11-28 20:44:04 +01:00
|
|
|
|
2017-08-26 14:12:38 +02:00
|
|
|
if wdir == 0 or wdir == 1 then
|
|
|
|
dir = placer:get_look_dir()
|
|
|
|
dir.y = 0
|
|
|
|
wdir = minetest.dir_to_wallmounted(dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
return minetest.item_place(itemstack, placer, pointed_thing, wdir)
|
|
|
|
else
|
|
|
|
return minetest.item_place(itemstack, placer, pointed_thing, minetest.dir_to_facedir(dir))
|
|
|
|
end
|
2015-11-11 14:26:39 +01:00
|
|
|
end
|
2017-08-26 14:12:38 +02:00
|
|
|
|
2015-11-11 14:26:39 +01:00
|
|
|
end
|
|
|
|
|
2018-02-01 16:54:55 +01:00
|
|
|
--- On_construct callback for display_api items. Creates entities and update them.
|
|
|
|
function display_api.on_construct(pos)
|
|
|
|
display_api.update_entities(pos)
|
2015-11-11 14:26:39 +01:00
|
|
|
end
|
|
|
|
|
2018-02-01 16:54:55 +01:00
|
|
|
--- On_destruct callback for display_api items. Removes entities.
|
|
|
|
function display_api.on_destruct(pos)
|
2015-11-11 14:26:39 +01:00
|
|
|
local objrefs = get_entities(pos)
|
|
|
|
|
|
|
|
for _, objref in pairs(objrefs) do
|
|
|
|
objref:remove()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-01 16:54:55 +01:00
|
|
|
-- On_rotate (screwdriver) callback for display_api items. Prevents axis rotation and reorients entities.
|
|
|
|
function display_api.on_rotate(pos, node, user, mode, new_param2)
|
2017-08-26 14:12:38 +02:00
|
|
|
if mode ~= 1 then return false end
|
2015-11-11 14:26:39 +01:00
|
|
|
|
2015-11-28 20:44:04 +01:00
|
|
|
local values = get_values(node)
|
|
|
|
|
|
|
|
if values then
|
|
|
|
minetest.swap_node(pos, {name = node.name, param1 = node.param1, param2 = values.rotate})
|
2015-11-11 14:26:39 +01:00
|
|
|
place_entities(pos)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Creates display entity with some fields and the on_activate callback
|
2018-02-01 16:54:55 +01:00
|
|
|
function display_api.register_display_entity(entity_name)
|
2015-11-11 14:26:39 +01:00
|
|
|
if not minetest.registered_entity then
|
|
|
|
minetest.register_entity(':'..entity_name, {
|
|
|
|
collisionbox = { 0, 0, 0, 0, 0, 0 },
|
|
|
|
visual = "upright_sprite",
|
|
|
|
textures = {},
|
2018-02-01 16:54:55 +01:00
|
|
|
on_activate = display_api.on_activate,
|
2018-02-15 17:26:59 +01:00
|
|
|
get_staticdata = function(self)
|
|
|
|
return minetest.serialize({
|
|
|
|
pos = self.pos,
|
|
|
|
})
|
|
|
|
end,
|
2015-11-11 14:26:39 +01:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-27 17:40:42 +01:00
|
|
|
minetest.register_lbm({
|
2018-02-01 16:54:55 +01:00
|
|
|
label = "Update display_api entities",
|
|
|
|
name = "display_api:update_entities",
|
2018-01-27 17:40:42 +01:00
|
|
|
run_at_every_load = true,
|
2018-02-01 16:54:55 +01:00
|
|
|
nodenames = {"group:display_modpack_node", "group:display_lib_node"},
|
|
|
|
action = function(pos, node) display_api.update_entities(pos) end,
|
2018-01-27 17:40:42 +01:00
|
|
|
})
|
2015-11-11 14:26:39 +01:00
|
|
|
|
2018-02-01 16:54:55 +01:00
|
|
|
-- Compatibility
|
|
|
|
display_lib = display_api
|