From f3970f641eb60bf92492b6715084273cc1bceb74 Mon Sep 17 00:00:00 2001 From: Thomas--S Date: Thu, 1 Feb 2018 16:54:55 +0100 Subject: Seperate signs API from signs definitions Change modnames from *_lib to *_api --- display_api/API.md | 91 +++++++++++++++++ display_api/LICENSE.txt | 166 +++++++++++++++++++++++++++++++ display_api/README.md | 14 +++ display_api/copyright.txt | 4 + display_api/depends.txt | 0 display_api/init.lua | 242 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 517 insertions(+) create mode 100644 display_api/API.md create mode 100644 display_api/LICENSE.txt create mode 100644 display_api/README.md create mode 100644 display_api/copyright.txt create mode 100644 display_api/depends.txt create mode 100644 display_api/init.lua (limited to 'display_api') diff --git a/display_api/API.md b/display_api/API.md new file mode 100644 index 0000000..b133c8a --- /dev/null +++ b/display_api/API.md @@ -0,0 +1,91 @@ +# Display Lib API +This document describes Display Lib API. Display Lib allows to add a dynamic display on a node. Display Lib limits node rotations. For wallmounted, only vertical positionning is available, and for facedir, only first four position are availabel (those with default axis). + +## Provided methods +### update\_entities +**display\_lib.update\_entities(pos)** + +This method triggers entities update for the display node at pos. Actual entity update is made by `on_display_update` callback associated to the entity. + +`pos`: Position of the node +### register\_display\_entity +**display\_lib.register\_display\_entity(entity_name)** + +This is a helper to register entities used for display. + +`entity_name`: Name of the entity to register. +## Provided callback implementations +### on_place +**display\_lib.on\_place(itemstack, placer, pointed\_thing)** + +`on_place` node callback implementation. Display nodes should have this callback (avoid placement of horizontal display node). +### on_construct +**display\_lib.on\_construct(pos)** + +`on_construct` node callback implementation. Display nodes should have this callback (creates, places and updates display entities on node construction). +### on_destruct +**display\_lib.on_destruct(pos)** + +`on_destruct` node callback implementation. Display nodes should have this callback (removes display entities on node destruction). +### on_rotate +**display\_lib.on\_rotate(pos, node, user, mode, new_param2)** + +`on_rotate` node callback implementation. Display nodes should have this callback (restricts rotations and rotates display entities associated with node). +### on_activate +**display\_lib.on_activate(entity, staticdata)** + +`On_activate` entity callback implementation for display entities. No need of this method if display entities have been registered using `register_display_entity` (callback is already set). + +## Howto register a display node +* Register display entities with `register_display_entity` + +* Register node with : + - `on_place`, `on_construct`, `on_destruct` and `on_rotate` callbacks using display_api callbacks. +  + - `display_modpack_node` group. This will make this node have their entities updated as soon as the mapblock is loaded (Useful after /clearobjects). +  + - a `display_entities` field in node definition containing a entity name indexed table. See below for description of each display_entities fields. + +### Display_entities fields +`on_display_update` is a callback in charge of setting up entity texture. If not set, entity will have no texture and will be displayed as unknown item. + +`depth`, `right` and `heigh` : Entity position regarding to node facedir/wallmounted main axis. Values for these fields can be any number between -0.5 and 0.5 (default value is 0). Position 0,0,0 is the center of the node. `depth` goes from front (-0.5) to rear (0.5), `height` goes from bottom (-0.5) to top (0.5) and `right` goes from left (-0.5) to right (0.5). + +In order to avoid flickering text, it's better to have text a little behind node surface. A good spacing value is given by `display_api.entity_spacing` variable. + +### Example + + display_api.register_display_entity("mymod:entity1") + display_api.register_display_entity("mymod:entity2") + + function my_display_update1(pos, objref) + objref:set_properties({ textures= {"mytexture1.png"}, + visual_size = {x=1, y=1} }) + end + + function my_display_update2(pos, objref) + objref:set_properties({ textures= {"mytexture2.png"}, +                         visual_size = {x=1, y=1} }) + end + + minetest.register_node("mymod:test_display_node", { + ... + paramtype2 = "facedir", + ... + groups = { display_modpack_node = 1, ... }, + ... + display_entities = { + ["mymod:entity1"] = { + depth = 0.3, + on_display_update = my_display_update1 }, + ["mymod:entity1"] = { + depth = 0.2, height = 0.1, + on_display_update = my_display_update2 }, + }, + ... + on_place = display_api.on_place, + on_construct = display_api.on_construct, + on_destruct = display_api.on_destruct, + on_rotate = display_api.on_rotate, + ... + }) diff --git a/display_api/LICENSE.txt b/display_api/LICENSE.txt new file mode 100644 index 0000000..341c30b --- /dev/null +++ b/display_api/LICENSE.txt @@ -0,0 +1,166 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + diff --git a/display_api/README.md b/display_api/README.md new file mode 100644 index 0000000..f51996d --- /dev/null +++ b/display_api/README.md @@ -0,0 +1,14 @@ +# Display Lib + +This library's purpose is to ease creation of nodes with one or more displays on sides. For example, signs and clocks. Display can be dynamic and/or different for each node instance. + +**Limitations**: This lib uses entities to draw display. This means display has to be vertical. So display nodes rotation are limitated to "upside up" positions. + +**Dependancies**:default + +**License**: LPGL + +**API**: See [API.md](https://github.com/pyrollo/display_modpack/blob/master/display_api/API.md) document please. + +For more information, see the [forum topic](https://forum.minetest.net/viewtopic.php?t=19365) at the Minetest forums. + diff --git a/display_api/copyright.txt b/display_api/copyright.txt new file mode 100644 index 0000000..e3a15e9 --- /dev/null +++ b/display_api/copyright.txt @@ -0,0 +1,4 @@ +Code by Pierre-Yves Rollo (pyrollo) +Contributors: +(gpcf): Compatibility with signs lib +(Thomas--S): Fix /clearobjects bug diff --git a/display_api/depends.txt b/display_api/depends.txt new file mode 100644 index 0000000..e69de29 diff --git a/display_api/init.lua b/display_api/init.lua new file mode 100644 index 0000000..7439119 --- /dev/null +++ b/display_api/init.lua @@ -0,0 +1,242 @@ +--[[ + display_api mod for Minetest - Library to add dynamic display + 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 . +--]] + +display_api = {} + +-- 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 +display_api.entity_spacing = 0.002 + +-- Miscelaneous values depending on wallmounted param2 +local wallmounted_values = { + [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} +} + +-- 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 + +--- 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 + for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do + local entity = objref:get_luaentity() + if entity and ndef.display_entities[entity.name] then + 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 + return math.max(-0.5, math.min(0.5, posprop)) + 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] + local values = get_values(node) + local objrefs = get_entities(pos) + + if values and ndef and ndef.display_entities then + + for entity_name, props in pairs(ndef.display_entities) do + local depth = clip_pos_prop(props.depth) + local height = clip_pos_prop(props.height) + local right = clip_pos_prop(props.right) + if not objrefs[entity_name] then + objrefs[entity_name] = minetest.add_entity(pos, entity_name) + end + + objrefs[entity_name]:setpos({ + x = pos.x - values.dx * depth + values.rx * right, + y = pos.y + height, + z = pos.z - values.dz * depth + values.rz * right}) + + 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 +function display_api.update_entities(pos) + local objrefs = place_entities(pos) + for _, objref in pairs(objrefs) do + call_node_on_display_update(pos, objref) + end +end + +--- On_activate callback for display_api entities. Calls on_display_update callbacks +--- of corresponding node for each entity. +function display_api.on_activate(entity, staticdata) + if entity then + entity.object:set_armor_groups({immortal=1}) + call_node_on_display_update(entity.object:getpos(), entity.object) + end +end + +--- On_place callback for display_api items. Does nothing more than preventing item +--- from being placed on ceiling or ground +function display_api.on_place(itemstack, placer, pointed_thing) + local ndef = itemstack:get_definition() + 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} + + if ndef then + if ndef.paramtype2 == "wallmounted" then + + local wdir = minetest.dir_to_wallmounted(dir) + + 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 + end + +end + +--- On_construct callback for display_api items. Creates entities and update them. +function display_api.on_construct(pos) + display_api.update_entities(pos) +end + +--- On_destruct callback for display_api items. Removes entities. +function display_api.on_destruct(pos) + local objrefs = get_entities(pos) + + for _, objref in pairs(objrefs) do + objref:remove() + end +end + +-- 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) + if mode ~= 1 then return false end + + local values = get_values(node) + + if values then + minetest.swap_node(pos, {name = node.name, param1 = node.param1, param2 = values.rotate}) + place_entities(pos) + return true + else + return false + end +end + +--- Creates display entity with some fields and the on_activate callback +function display_api.register_display_entity(entity_name) + if not minetest.registered_entity then + minetest.register_entity(':'..entity_name, { + collisionbox = { 0, 0, 0, 0, 0, 0 }, + visual = "upright_sprite", + textures = {}, + on_activate = display_api.on_activate, + }) + end +end + +minetest.register_lbm({ + label = "Update display_api entities", + name = "display_api:update_entities", + run_at_every_load = true, + nodenames = {"group:display_modpack_node", "group:display_lib_node"}, + action = function(pos, node) display_api.update_entities(pos) end, +}) + +-- Compatibility +display_lib = display_api \ No newline at end of file -- cgit v1.2.3 From ec48743fb9a51b9e88914ad231af6b99fa0cd2ec Mon Sep 17 00:00:00 2001 From: Thomas--S Date: Thu, 15 Feb 2018 17:26:59 +0100 Subject: Allow a greater object offset This allows e.g. polemounted signs. Required for display_modpack to be usable in the streets mod. Backwards compatible. --- display_api/API.md | 5 ++++- display_api/init.lua | 43 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 8 deletions(-) (limited to 'display_api') diff --git a/display_api/API.md b/display_api/API.md index b133c8a..0d52e29 100644 --- a/display_api/API.md +++ b/display_api/API.md @@ -49,7 +49,10 @@ This is a helper to register entities used for display. ### Display_entities fields `on_display_update` is a callback in charge of setting up entity texture. If not set, entity will have no texture and will be displayed as unknown item. -`depth`, `right` and `heigh` : Entity position regarding to node facedir/wallmounted main axis. Values for these fields can be any number between -0.5 and 0.5 (default value is 0). Position 0,0,0 is the center of the node. `depth` goes from front (-0.5) to rear (0.5), `height` goes from bottom (-0.5) to top (0.5) and `right` goes from left (-0.5) to right (0.5). +`depth`, `right` and `height` : Entity position regarding to node facedir/wallmounted main axis. +Values for these fields can be any number between -1.5 and 1.5 (default value is 0). +Position 0,0,0 is the center of the node. +`depth` goes from front (-0.5) to rear (0.5), `height` goes from bottom (-0.5) to top (0.5) and `right` goes from left (-0.5) to right (0.5). In order to avoid flickering text, it's better to have text a little behind node surface. A good spacing value is given by `display_api.entity_spacing` variable. diff --git a/display_api/init.lua b/display_api/init.lua index 7439119..240ce21 100644 --- a/display_api/init.lua +++ b/display_api/init.lua @@ -79,14 +79,25 @@ local function get_values(node) end end +--- 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 + --- 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 - for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do + for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do local entity = objref:get_luaentity() - if entity and ndef.display_entities[entity.name] then + if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then if objrefs[entity.name] then objref:remove() else @@ -100,7 +111,7 @@ end local function clip_pos_prop(posprop) if posprop then - return math.max(-0.5, math.min(0.5, posprop)) + return math.max(-1.5, math.min(1.5, posprop)) else return 0 end @@ -147,6 +158,7 @@ end function display_api.update_entities(pos) local objrefs = place_entities(pos) for _, objref in pairs(objrefs) do + objref:get_luaentity().pos = minetest.hash_node_position(pos) call_node_on_display_update(pos, objref) end end @@ -154,10 +166,22 @@ end --- On_activate callback for display_api entities. Calls on_display_update callbacks --- of corresponding node for each entity. function display_api.on_activate(entity, staticdata) - if entity then - entity.object:set_armor_groups({immortal=1}) - call_node_on_display_update(entity.object:getpos(), entity.object) - end + 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 end --- On_place callback for display_api items. Does nothing more than preventing item @@ -226,6 +250,11 @@ function display_api.register_display_entity(entity_name) visual = "upright_sprite", textures = {}, on_activate = display_api.on_activate, + get_staticdata = function(self) + return minetest.serialize({ + pos = self.pos, + }) + end, }) end end -- cgit v1.2.3 From 8c7557e45d4744fe35ad058950062cf771640126 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 13 Jul 2018 20:41:53 +0200 Subject: Rework all nodes displaying text according to new font_api --- display_api/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'display_api') diff --git a/display_api/init.lua b/display_api/init.lua index 240ce21..2cd15a0 100644 --- a/display_api/init.lua +++ b/display_api/init.lua @@ -128,15 +128,15 @@ local function place_entities(pos) for entity_name, props in pairs(ndef.display_entities) do local depth = clip_pos_prop(props.depth) - local height = clip_pos_prop(props.height) local right = clip_pos_prop(props.right) + local top = clip_pos_prop(props.top) if not objrefs[entity_name] then objrefs[entity_name] = minetest.add_entity(pos, entity_name) end objrefs[entity_name]:setpos({ x = pos.x - values.dx * depth + values.rx * right, - y = pos.y + height, + y = pos.y - top, z = pos.z - values.dz * depth + values.rz * right}) objrefs[entity_name]:setyaw(values.yaw) @@ -268,4 +268,4 @@ minetest.register_lbm({ }) -- Compatibility -display_lib = display_api \ No newline at end of file +display_lib = display_api -- cgit v1.2.3 From dfaf64cd619550df24bcc7f7e45901202eabd759 Mon Sep 17 00:00:00 2001 From: 12Me21 <12Me21.MC@gmail.com> Date: Thu, 20 Sep 2018 16:07:43 -0400 Subject: Improved on_place Now uses player view direction when trying to place nodes on the floor or ceiling, for both wallmounted and facedir nodes. Also supports nodes where paramtype2 is not wallmounted or facedir. --- display_api/init.lua | 80 +++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 41 deletions(-) (limited to 'display_api') diff --git a/display_api/init.lua b/display_api/init.lua index 2cd15a0..ca7b779 100644 --- a/display_api/init.lua +++ b/display_api/init.lua @@ -37,30 +37,30 @@ local wallmounted_values = { -- 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=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}, + -- 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, @@ -103,7 +103,7 @@ local function get_entities(pos) else objrefs[entity.name] = objref end - end + end end end return objrefs @@ -160,7 +160,7 @@ function display_api.update_entities(pos) for _, objref in pairs(objrefs) do objref:get_luaentity().pos = minetest.hash_node_position(pos) call_node_on_display_update(pos, objref) - end + end end --- On_activate callback for display_api entities. Calls on_display_update callbacks @@ -191,26 +191,24 @@ function display_api.on_place(itemstack, placer, pointed_thing) 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} - + y = 0, + z = under.z - above.z} + + -- If item is not placed on a wall, use the player's view direction instead + if dir.x == 0 and dir.z == 0 then + dir = placer:get_look_dir() + dir.y = 0 + end + + local param2 if ndef then if ndef.paramtype2 == "wallmounted" then - - local wdir = minetest.dir_to_wallmounted(dir) - - 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)) + param2 = minetest.dir_to_wallmounted(dir) + elseif ndef.paramtype2 == "facedir" then + param2 = minetest.dir_to_facedir(dir) end end - + return minetest.item_place(itemstack, placer, pointed_thing, param2) end --- On_construct callback for display_api items. Creates entities and update them. -- cgit v1.2.3 From 508db92633c49a2aeaeaac5fe18823decd50498b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 21 Sep 2018 17:08:17 +0200 Subject: Update copyright.txt --- display_api/copyright.txt | 2 ++ 1 file changed, 2 insertions(+) (limited to 'display_api') diff --git a/display_api/copyright.txt b/display_api/copyright.txt index e3a15e9..02b8467 100644 --- a/display_api/copyright.txt +++ b/display_api/copyright.txt @@ -2,3 +2,5 @@ Code by Pierre-Yves Rollo (pyrollo) Contributors: (gpcf): Compatibility with signs lib (Thomas--S): Fix /clearobjects bug +(12Me21): on_place improvement and code style fixing + -- cgit v1.2.3 From 12b556e92907e934a029297b6c6cc8db0bed2c70 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 21 Sep 2018 19:37:38 +0200 Subject: Turned display_api into a submodule --- display_api | 1 + display_api/API.md | 94 ---------------- display_api/LICENSE.txt | 166 ---------------------------- display_api/README.md | 14 --- display_api/copyright.txt | 6 -- display_api/depends.txt | 0 display_api/init.lua | 269 ---------------------------------------------- 7 files changed, 1 insertion(+), 549 deletions(-) create mode 160000 display_api delete mode 100644 display_api/API.md delete mode 100644 display_api/LICENSE.txt delete mode 100644 display_api/README.md delete mode 100644 display_api/copyright.txt delete mode 100644 display_api/depends.txt delete mode 100644 display_api/init.lua (limited to 'display_api') diff --git a/display_api b/display_api new file mode 160000 index 0000000..246b7f9 --- /dev/null +++ b/display_api @@ -0,0 +1 @@ +Subproject commit 246b7f958863e7f6b7f604b09361696226928a7b diff --git a/display_api/API.md b/display_api/API.md deleted file mode 100644 index 0d52e29..0000000 --- a/display_api/API.md +++ /dev/null @@ -1,94 +0,0 @@ -# Display Lib API -This document describes Display Lib API. Display Lib allows to add a dynamic display on a node. Display Lib limits node rotations. For wallmounted, only vertical positionning is available, and for facedir, only first four position are availabel (those with default axis). - -## Provided methods -### update\_entities -**display\_lib.update\_entities(pos)** - -This method triggers entities update for the display node at pos. Actual entity update is made by `on_display_update` callback associated to the entity. - -`pos`: Position of the node -### register\_display\_entity -**display\_lib.register\_display\_entity(entity_name)** - -This is a helper to register entities used for display. - -`entity_name`: Name of the entity to register. -## Provided callback implementations -### on_place -**display\_lib.on\_place(itemstack, placer, pointed\_thing)** - -`on_place` node callback implementation. Display nodes should have this callback (avoid placement of horizontal display node). -### on_construct -**display\_lib.on\_construct(pos)** - -`on_construct` node callback implementation. Display nodes should have this callback (creates, places and updates display entities on node construction). -### on_destruct -**display\_lib.on_destruct(pos)** - -`on_destruct` node callback implementation. Display nodes should have this callback (removes display entities on node destruction). -### on_rotate -**display\_lib.on\_rotate(pos, node, user, mode, new_param2)** - -`on_rotate` node callback implementation. Display nodes should have this callback (restricts rotations and rotates display entities associated with node). -### on_activate -**display\_lib.on_activate(entity, staticdata)** - -`On_activate` entity callback implementation for display entities. No need of this method if display entities have been registered using `register_display_entity` (callback is already set). - -## Howto register a display node -* Register display entities with `register_display_entity` - -* Register node with : - - `on_place`, `on_construct`, `on_destruct` and `on_rotate` callbacks using display_api callbacks. -  - - `display_modpack_node` group. This will make this node have their entities updated as soon as the mapblock is loaded (Useful after /clearobjects). -  - - a `display_entities` field in node definition containing a entity name indexed table. See below for description of each display_entities fields. - -### Display_entities fields -`on_display_update` is a callback in charge of setting up entity texture. If not set, entity will have no texture and will be displayed as unknown item. - -`depth`, `right` and `height` : Entity position regarding to node facedir/wallmounted main axis. -Values for these fields can be any number between -1.5 and 1.5 (default value is 0). -Position 0,0,0 is the center of the node. -`depth` goes from front (-0.5) to rear (0.5), `height` goes from bottom (-0.5) to top (0.5) and `right` goes from left (-0.5) to right (0.5). - -In order to avoid flickering text, it's better to have text a little behind node surface. A good spacing value is given by `display_api.entity_spacing` variable. - -### Example - - display_api.register_display_entity("mymod:entity1") - display_api.register_display_entity("mymod:entity2") - - function my_display_update1(pos, objref) - objref:set_properties({ textures= {"mytexture1.png"}, - visual_size = {x=1, y=1} }) - end - - function my_display_update2(pos, objref) - objref:set_properties({ textures= {"mytexture2.png"}, -                         visual_size = {x=1, y=1} }) - end - - minetest.register_node("mymod:test_display_node", { - ... - paramtype2 = "facedir", - ... - groups = { display_modpack_node = 1, ... }, - ... - display_entities = { - ["mymod:entity1"] = { - depth = 0.3, - on_display_update = my_display_update1 }, - ["mymod:entity1"] = { - depth = 0.2, height = 0.1, - on_display_update = my_display_update2 }, - }, - ... - on_place = display_api.on_place, - on_construct = display_api.on_construct, - on_destruct = display_api.on_destruct, - on_rotate = display_api.on_rotate, - ... - }) diff --git a/display_api/LICENSE.txt b/display_api/LICENSE.txt deleted file mode 100644 index 341c30b..0000000 --- a/display_api/LICENSE.txt +++ /dev/null @@ -1,166 +0,0 @@ - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. - - 0. Additional Definitions. - - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. - - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. - - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. - - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". - - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. - - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. - - 1. Exception to Section 3 of the GNU GPL. - - You may convey a covered work under sections 3 and 4 of this License -without being bound by section 3 of the GNU GPL. - - 2. Conveying Modified Versions. - - If you modify a copy of the Library, and, in your modifications, a -facility refers to a function or data to be supplied by an Application -that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified -version: - - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. - - 3. Object Code Incorporating Material from Library Header Files. - - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object -code under terms of your choice, provided that, if the incorporated -material is not limited to numerical parameters, data structure -layouts and accessors, or small macros, inline functions and templates -(ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license - document. - - 4. Combined Works. - - You may convey a Combined Work under terms of your choice that, -taken together, effectively do not restrict modification of the -portions of the Library contained in the Combined Work and reverse -engineering for debugging such modifications, if you also do each of -the following: - - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the -Library side by side in a single library together with other library -facilities that are not Applications and are not covered by this -License, and convey such a combined library under terms of your -choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. - - 6. Revised Versions of the GNU Lesser General Public License. - - The Free Software Foundation may publish revised and/or new versions -of the GNU Lesser General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - - Each version is given a distinguishing version number. If the -Library as you received it specifies that a certain numbered version -of the GNU Lesser General Public License "or any later version" -applies to it, you have the option of following the terms and -conditions either of that published version or of any later version -published by the Free Software Foundation. If the Library as you -received it does not specify a version number of the GNU Lesser -General Public License, you may choose any version of the GNU Lesser -General Public License ever published by the Free Software Foundation. - - If the Library as you received it specifies that a proxy can decide -whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is -permanent authorization for you to choose that version for the -Library. - diff --git a/display_api/README.md b/display_api/README.md deleted file mode 100644 index f51996d..0000000 --- a/display_api/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# Display Lib - -This library's purpose is to ease creation of nodes with one or more displays on sides. For example, signs and clocks. Display can be dynamic and/or different for each node instance. - -**Limitations**: This lib uses entities to draw display. This means display has to be vertical. So display nodes rotation are limitated to "upside up" positions. - -**Dependancies**:default - -**License**: LPGL - -**API**: See [API.md](https://github.com/pyrollo/display_modpack/blob/master/display_api/API.md) document please. - -For more information, see the [forum topic](https://forum.minetest.net/viewtopic.php?t=19365) at the Minetest forums. - diff --git a/display_api/copyright.txt b/display_api/copyright.txt deleted file mode 100644 index 02b8467..0000000 --- a/display_api/copyright.txt +++ /dev/null @@ -1,6 +0,0 @@ -Code by Pierre-Yves Rollo (pyrollo) -Contributors: -(gpcf): Compatibility with signs lib -(Thomas--S): Fix /clearobjects bug -(12Me21): on_place improvement and code style fixing - diff --git a/display_api/depends.txt b/display_api/depends.txt deleted file mode 100644 index e69de29..0000000 diff --git a/display_api/init.lua b/display_api/init.lua deleted file mode 100644 index ca7b779..0000000 --- a/display_api/init.lua +++ /dev/null @@ -1,269 +0,0 @@ ---[[ - display_api mod for Minetest - Library to add dynamic display - 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 . ---]] - -display_api = {} - --- 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 -display_api.entity_spacing = 0.002 - --- Miscelaneous values depending on wallmounted param2 -local wallmounted_values = { - [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} -} - --- 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 - ---- 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 - ---- 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 - for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do - local entity = objref:get_luaentity() - if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then - 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 - return math.max(-1.5, math.min(1.5, posprop)) - 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] - local values = get_values(node) - local objrefs = get_entities(pos) - - if values and ndef and ndef.display_entities then - - for entity_name, props in pairs(ndef.display_entities) do - local depth = clip_pos_prop(props.depth) - local right = clip_pos_prop(props.right) - local top = clip_pos_prop(props.top) - if not objrefs[entity_name] then - objrefs[entity_name] = minetest.add_entity(pos, entity_name) - end - - objrefs[entity_name]:setpos({ - x = pos.x - values.dx * depth + values.rx * right, - y = pos.y - top, - z = pos.z - values.dz * depth + values.rz * right}) - - 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 -function display_api.update_entities(pos) - local objrefs = place_entities(pos) - for _, objref in pairs(objrefs) do - objref:get_luaentity().pos = minetest.hash_node_position(pos) - call_node_on_display_update(pos, objref) - end -end - ---- On_activate callback for display_api entities. Calls on_display_update callbacks ---- of corresponding node for each entity. -function display_api.on_activate(entity, staticdata) - 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 -end - ---- On_place callback for display_api items. Does nothing more than preventing item ---- from being placed on ceiling or ground -function display_api.on_place(itemstack, placer, pointed_thing) - local ndef = itemstack:get_definition() - local above = pointed_thing.above - local under = pointed_thing.under - local dir = {x = under.x - above.x, - y = 0, - z = under.z - above.z} - - -- If item is not placed on a wall, use the player's view direction instead - if dir.x == 0 and dir.z == 0 then - dir = placer:get_look_dir() - dir.y = 0 - end - - local param2 - if ndef then - if ndef.paramtype2 == "wallmounted" then - param2 = minetest.dir_to_wallmounted(dir) - elseif ndef.paramtype2 == "facedir" then - param2 = minetest.dir_to_facedir(dir) - end - end - return minetest.item_place(itemstack, placer, pointed_thing, param2) -end - ---- On_construct callback for display_api items. Creates entities and update them. -function display_api.on_construct(pos) - display_api.update_entities(pos) -end - ---- On_destruct callback for display_api items. Removes entities. -function display_api.on_destruct(pos) - local objrefs = get_entities(pos) - - for _, objref in pairs(objrefs) do - objref:remove() - end -end - --- 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) - if mode ~= 1 then return false end - - local values = get_values(node) - - if values then - minetest.swap_node(pos, {name = node.name, param1 = node.param1, param2 = values.rotate}) - place_entities(pos) - return true - else - return false - end -end - ---- Creates display entity with some fields and the on_activate callback -function display_api.register_display_entity(entity_name) - if not minetest.registered_entity then - minetest.register_entity(':'..entity_name, { - collisionbox = { 0, 0, 0, 0, 0, 0 }, - visual = "upright_sprite", - textures = {}, - on_activate = display_api.on_activate, - get_staticdata = function(self) - return minetest.serialize({ - pos = self.pos, - }) - end, - }) - end -end - -minetest.register_lbm({ - label = "Update display_api entities", - name = "display_api:update_entities", - run_at_every_load = true, - nodenames = {"group:display_modpack_node", "group:display_lib_node"}, - action = function(pos, node) display_api.update_entities(pos) end, -}) - --- Compatibility -display_lib = display_api -- cgit v1.2.3 From d22362a4acfffa2853974a0682b953a8f867e4e4 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 21 Sep 2018 21:19:48 +0200 Subject: Reverting submodule change until zip file does not include submodule --- display_api | 1 - display_api/API.md | 94 ++++++++++++++++ display_api/LICENSE.txt | 166 ++++++++++++++++++++++++++++ display_api/README.md | 14 +++ display_api/copyright.txt | 6 ++ display_api/depends.txt | 0 display_api/init.lua | 269 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 549 insertions(+), 1 deletion(-) delete mode 160000 display_api create mode 100644 display_api/API.md create mode 100644 display_api/LICENSE.txt create mode 100644 display_api/README.md create mode 100644 display_api/copyright.txt create mode 100644 display_api/depends.txt create mode 100644 display_api/init.lua (limited to 'display_api') diff --git a/display_api b/display_api deleted file mode 160000 index 246b7f9..0000000 --- a/display_api +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 246b7f958863e7f6b7f604b09361696226928a7b diff --git a/display_api/API.md b/display_api/API.md new file mode 100644 index 0000000..0d52e29 --- /dev/null +++ b/display_api/API.md @@ -0,0 +1,94 @@ +# Display Lib API +This document describes Display Lib API. Display Lib allows to add a dynamic display on a node. Display Lib limits node rotations. For wallmounted, only vertical positionning is available, and for facedir, only first four position are availabel (those with default axis). + +## Provided methods +### update\_entities +**display\_lib.update\_entities(pos)** + +This method triggers entities update for the display node at pos. Actual entity update is made by `on_display_update` callback associated to the entity. + +`pos`: Position of the node +### register\_display\_entity +**display\_lib.register\_display\_entity(entity_name)** + +This is a helper to register entities used for display. + +`entity_name`: Name of the entity to register. +## Provided callback implementations +### on_place +**display\_lib.on\_place(itemstack, placer, pointed\_thing)** + +`on_place` node callback implementation. Display nodes should have this callback (avoid placement of horizontal display node). +### on_construct +**display\_lib.on\_construct(pos)** + +`on_construct` node callback implementation. Display nodes should have this callback (creates, places and updates display entities on node construction). +### on_destruct +**display\_lib.on_destruct(pos)** + +`on_destruct` node callback implementation. Display nodes should have this callback (removes display entities on node destruction). +### on_rotate +**display\_lib.on\_rotate(pos, node, user, mode, new_param2)** + +`on_rotate` node callback implementation. Display nodes should have this callback (restricts rotations and rotates display entities associated with node). +### on_activate +**display\_lib.on_activate(entity, staticdata)** + +`On_activate` entity callback implementation for display entities. No need of this method if display entities have been registered using `register_display_entity` (callback is already set). + +## Howto register a display node +* Register display entities with `register_display_entity` + +* Register node with : + - `on_place`, `on_construct`, `on_destruct` and `on_rotate` callbacks using display_api callbacks. +  + - `display_modpack_node` group. This will make this node have their entities updated as soon as the mapblock is loaded (Useful after /clearobjects). +  + - a `display_entities` field in node definition containing a entity name indexed table. See below for description of each display_entities fields. + +### Display_entities fields +`on_display_update` is a callback in charge of setting up entity texture. If not set, entity will have no texture and will be displayed as unknown item. + +`depth`, `right` and `height` : Entity position regarding to node facedir/wallmounted main axis. +Values for these fields can be any number between -1.5 and 1.5 (default value is 0). +Position 0,0,0 is the center of the node. +`depth` goes from front (-0.5) to rear (0.5), `height` goes from bottom (-0.5) to top (0.5) and `right` goes from left (-0.5) to right (0.5). + +In order to avoid flickering text, it's better to have text a little behind node surface. A good spacing value is given by `display_api.entity_spacing` variable. + +### Example + + display_api.register_display_entity("mymod:entity1") + display_api.register_display_entity("mymod:entity2") + + function my_display_update1(pos, objref) + objref:set_properties({ textures= {"mytexture1.png"}, + visual_size = {x=1, y=1} }) + end + + function my_display_update2(pos, objref) + objref:set_properties({ textures= {"mytexture2.png"}, +                         visual_size = {x=1, y=1} }) + end + + minetest.register_node("mymod:test_display_node", { + ... + paramtype2 = "facedir", + ... + groups = { display_modpack_node = 1, ... }, + ... + display_entities = { + ["mymod:entity1"] = { + depth = 0.3, + on_display_update = my_display_update1 }, + ["mymod:entity1"] = { + depth = 0.2, height = 0.1, + on_display_update = my_display_update2 }, + }, + ... + on_place = display_api.on_place, + on_construct = display_api.on_construct, + on_destruct = display_api.on_destruct, + on_rotate = display_api.on_rotate, + ... + }) diff --git a/display_api/LICENSE.txt b/display_api/LICENSE.txt new file mode 100644 index 0000000..341c30b --- /dev/null +++ b/display_api/LICENSE.txt @@ -0,0 +1,166 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + diff --git a/display_api/README.md b/display_api/README.md new file mode 100644 index 0000000..f51996d --- /dev/null +++ b/display_api/README.md @@ -0,0 +1,14 @@ +# Display Lib + +This library's purpose is to ease creation of nodes with one or more displays on sides. For example, signs and clocks. Display can be dynamic and/or different for each node instance. + +**Limitations**: This lib uses entities to draw display. This means display has to be vertical. So display nodes rotation are limitated to "upside up" positions. + +**Dependancies**:default + +**License**: LPGL + +**API**: See [API.md](https://github.com/pyrollo/display_modpack/blob/master/display_api/API.md) document please. + +For more information, see the [forum topic](https://forum.minetest.net/viewtopic.php?t=19365) at the Minetest forums. + diff --git a/display_api/copyright.txt b/display_api/copyright.txt new file mode 100644 index 0000000..02b8467 --- /dev/null +++ b/display_api/copyright.txt @@ -0,0 +1,6 @@ +Code by Pierre-Yves Rollo (pyrollo) +Contributors: +(gpcf): Compatibility with signs lib +(Thomas--S): Fix /clearobjects bug +(12Me21): on_place improvement and code style fixing + diff --git a/display_api/depends.txt b/display_api/depends.txt new file mode 100644 index 0000000..e69de29 diff --git a/display_api/init.lua b/display_api/init.lua new file mode 100644 index 0000000..ca7b779 --- /dev/null +++ b/display_api/init.lua @@ -0,0 +1,269 @@ +--[[ + display_api mod for Minetest - Library to add dynamic display + 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 . +--]] + +display_api = {} + +-- 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 +display_api.entity_spacing = 0.002 + +-- Miscelaneous values depending on wallmounted param2 +local wallmounted_values = { + [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} +} + +-- 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 + +--- 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 + +--- 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 + for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do + local entity = objref:get_luaentity() + if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then + 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 + return math.max(-1.5, math.min(1.5, posprop)) + 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] + local values = get_values(node) + local objrefs = get_entities(pos) + + if values and ndef and ndef.display_entities then + + for entity_name, props in pairs(ndef.display_entities) do + local depth = clip_pos_prop(props.depth) + local right = clip_pos_prop(props.right) + local top = clip_pos_prop(props.top) + if not objrefs[entity_name] then + objrefs[entity_name] = minetest.add_entity(pos, entity_name) + end + + objrefs[entity_name]:setpos({ + x = pos.x - values.dx * depth + values.rx * right, + y = pos.y - top, + z = pos.z - values.dz * depth + values.rz * right}) + + 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 +function display_api.update_entities(pos) + local objrefs = place_entities(pos) + for _, objref in pairs(objrefs) do + objref:get_luaentity().pos = minetest.hash_node_position(pos) + call_node_on_display_update(pos, objref) + end +end + +--- On_activate callback for display_api entities. Calls on_display_update callbacks +--- of corresponding node for each entity. +function display_api.on_activate(entity, staticdata) + 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 +end + +--- On_place callback for display_api items. Does nothing more than preventing item +--- from being placed on ceiling or ground +function display_api.on_place(itemstack, placer, pointed_thing) + local ndef = itemstack:get_definition() + local above = pointed_thing.above + local under = pointed_thing.under + local dir = {x = under.x - above.x, + y = 0, + z = under.z - above.z} + + -- If item is not placed on a wall, use the player's view direction instead + if dir.x == 0 and dir.z == 0 then + dir = placer:get_look_dir() + dir.y = 0 + end + + local param2 + if ndef then + if ndef.paramtype2 == "wallmounted" then + param2 = minetest.dir_to_wallmounted(dir) + elseif ndef.paramtype2 == "facedir" then + param2 = minetest.dir_to_facedir(dir) + end + end + return minetest.item_place(itemstack, placer, pointed_thing, param2) +end + +--- On_construct callback for display_api items. Creates entities and update them. +function display_api.on_construct(pos) + display_api.update_entities(pos) +end + +--- On_destruct callback for display_api items. Removes entities. +function display_api.on_destruct(pos) + local objrefs = get_entities(pos) + + for _, objref in pairs(objrefs) do + objref:remove() + end +end + +-- 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) + if mode ~= 1 then return false end + + local values = get_values(node) + + if values then + minetest.swap_node(pos, {name = node.name, param1 = node.param1, param2 = values.rotate}) + place_entities(pos) + return true + else + return false + end +end + +--- Creates display entity with some fields and the on_activate callback +function display_api.register_display_entity(entity_name) + if not minetest.registered_entity then + minetest.register_entity(':'..entity_name, { + collisionbox = { 0, 0, 0, 0, 0, 0 }, + visual = "upright_sprite", + textures = {}, + on_activate = display_api.on_activate, + get_staticdata = function(self) + return minetest.serialize({ + pos = self.pos, + }) + end, + }) + end +end + +minetest.register_lbm({ + label = "Update display_api entities", + name = "display_api:update_entities", + run_at_every_load = true, + nodenames = {"group:display_modpack_node", "group:display_lib_node"}, + action = function(pos, node) display_api.update_entities(pos) end, +}) + +-- Compatibility +display_lib = display_api -- cgit v1.2.3 From 44b911be9e57dccea606da2b6907135af75d5033 Mon Sep 17 00:00:00 2001 From: 12Me21 <12Me21.MC@gmail.com> Date: Wed, 26 Sep 2018 13:41:30 -0400 Subject: Improve on_rotate functions (#21) Fixed bugs and improve on_rotate and support color- paramtype2s --- display_api/init.lua | 93 +++++++++++++++++++--------------------------------- 1 file changed, 33 insertions(+), 60 deletions(-) (limited to 'display_api') diff --git a/display_api/init.lua b/display_api/init.lua index ca7b779..b65eb89 100644 --- a/display_api/init.lua +++ b/display_api/init.lua @@ -26,55 +26,30 @@ display_api.entity_spacing = 0.002 -- Miscelaneous values depending on wallmounted param2 local wallmounted_values = { - [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} + [2]={dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2}, + [3]={dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2 }, + [4]={dx=0, dz=-1, rx=1, rz=0, yaw=0 }, + [5]={dx=0, dz=1, rx=-1, rz=0, yaw=math.pi } } -- 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}, - } + [0]={dx=0, dz=-1, rx=1, rz=0, yaw=0 }, + [1]={dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2}, + [2]={dx=0, dz=1, rx=-1, rz=0, yaw=math.pi }, + [3]={dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2 } +} -- 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] + local paramtype2 = ndef.paramtype2 + if paramtype2 == "wallmounted" or paramtype2 == "colorwallmounted" then + return wallmounted_values[node.param2 % 8] + elseif paramtype2 == "facedir" or paramtype2 == "colorfacedir" then + return facedir_values[node.param2 % 32] end end end @@ -97,9 +72,9 @@ local function get_entities(pos) if ndef and ndef.display_entities then for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do local entity = objref:get_luaentity() - if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then + if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then if objrefs[entity.name] then - objref:remove() + objref:remove() else objrefs[entity.name] = objref end @@ -123,9 +98,9 @@ local function place_entities(pos) local ndef = minetest.registered_nodes[node.name] local values = get_values(node) local objrefs = get_entities(pos) - + if values and ndef and ndef.display_entities then - + for entity_name, props in pairs(ndef.display_entities) do local depth = clip_pos_prop(props.depth) local right = clip_pos_prop(props.right) @@ -133,12 +108,12 @@ local function place_entities(pos) if not objrefs[entity_name] then objrefs[entity_name] = minetest.add_entity(pos, entity_name) end - + objrefs[entity_name]:setpos({ x = pos.x - values.dx * depth + values.rx * right, y = pos.y - top, z = pos.z - values.dz * depth + values.rz * right}) - + objrefs[entity_name]:setyaw(values.yaw) end end @@ -151,7 +126,7 @@ local function call_node_on_display_update(pos, objref) 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 end --- Force entity update @@ -186,7 +161,7 @@ end --- On_place callback for display_api items. Does nothing more than preventing item --- from being placed on ceiling or ground -function display_api.on_place(itemstack, placer, pointed_thing) +function display_api.on_place(itemstack, placer, pointed_thing, override_param2) local ndef = itemstack:get_definition() local above = pointed_thing.above local under = pointed_thing.under @@ -200,15 +175,16 @@ function display_api.on_place(itemstack, placer, pointed_thing) dir.y = 0 end - local param2 + local param2 = 0 if ndef then - if ndef.paramtype2 == "wallmounted" then + local paramtype2 = ndef.paramtype2 + if paramtype2 == "wallmounted" or paramtype2 == "colorwallmounted" then param2 = minetest.dir_to_wallmounted(dir) - elseif ndef.paramtype2 == "facedir" then + elseif paramtype2 == "facedir" or paramtype2 == "colorfacedir" then param2 = minetest.dir_to_facedir(dir) end end - return minetest.item_place(itemstack, placer, pointed_thing, param2) + return minetest.item_place(itemstack, placer, pointed_thing, param2 + (override_param2 or 0)) end --- On_construct callback for display_api items. Creates entities and update them. @@ -225,14 +201,11 @@ function display_api.on_destruct(pos) end end --- 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) - if mode ~= 1 then return false end - - local values = get_values(node) - - if values then - minetest.swap_node(pos, {name = node.name, param1 = node.param1, param2 = values.rotate}) +-- On_rotate (screwdriver) callback for display_api items. Prevents invalid rotations and reorients entities. +function display_api.on_rotate(pos, node, user, _, new_param2) + node.param2 = new_param2 + if get_values(node) then + minetest.swap_node(pos, node) place_entities(pos) return true else -- cgit v1.2.3 From 5586b9d1c4598903a08f9b633fdc9543a15f23ce Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Wed, 26 Sep 2018 19:43:33 +0200 Subject: Update copyright.txt --- display_api/copyright.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'display_api') diff --git a/display_api/copyright.txt b/display_api/copyright.txt index 02b8467..e242c7c 100644 --- a/display_api/copyright.txt +++ b/display_api/copyright.txt @@ -2,5 +2,4 @@ Code by Pierre-Yves Rollo (pyrollo) Contributors: (gpcf): Compatibility with signs lib (Thomas--S): Fix /clearobjects bug -(12Me21): on_place improvement and code style fixing - +(12Me21): on_place and on_rotate improvements -- cgit v1.2.3 From 9c5752f0d93634cb048f85424ff08da2e2593353 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 9 Nov 2018 15:28:28 +0100 Subject: Update README.md --- display_api/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'display_api') diff --git a/display_api/README.md b/display_api/README.md index f51996d..af8f358 100644 --- a/display_api/README.md +++ b/display_api/README.md @@ -1,4 +1,4 @@ -# Display Lib +# Display API This library's purpose is to ease creation of nodes with one or more displays on sides. For example, signs and clocks. Display can be dynamic and/or different for each node instance. -- cgit v1.2.3 From 77c8b0a5ec9b1fc97e2bc65d7c90cc90c2221b31 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 9 Nov 2018 15:28:54 +0100 Subject: Update README.md --- display_api/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'display_api') diff --git a/display_api/README.md b/display_api/README.md index af8f358..26b26f0 100644 --- a/display_api/README.md +++ b/display_api/README.md @@ -6,7 +6,7 @@ This library's purpose is to ease creation of nodes with one or more displays on **Dependancies**:default -**License**: LPGL +**License**: LGPLv2 **API**: See [API.md](https://github.com/pyrollo/display_modpack/blob/master/display_api/API.md) document please. -- cgit v1.2.3 From 03983f081ed98ba6793e4f2c9ab461f89938c3b5 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Thu, 29 Nov 2018 14:27:36 +0100 Subject: Atempt to fix "unknown object" issue + code rework --- display_api/init.lua | 106 ++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 56 deletions(-) (limited to 'display_api') diff --git a/display_api/init.lua b/display_api/init.lua index b65eb89..c9d8c19 100644 --- a/display_api/init.lua +++ b/display_api/init.lua @@ -24,6 +24,9 @@ display_api = {} -- variable as spacing between entity and node display_api.entity_spacing = 0.002 +-- Maximum entity position relative to the node pos +local max_entity_pos = 1.5 + -- Miscelaneous values depending on wallmounted param2 local wallmounted_values = { [2]={dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2}, @@ -43,7 +46,7 @@ local facedir_values = { -- dx/dy = depth vector, rx/ly = right vector, yaw = yaw of entity, local function get_values(node) local ndef = minetest.registered_nodes[node.name] - + if ndef then local paramtype2 = ndef.paramtype2 if paramtype2 == "wallmounted" or paramtype2 == "colorwallmounted" then @@ -54,27 +57,18 @@ local function get_values(node) end end ---- 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 - --- 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 - for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do + for _, objref in + ipairs(minetest.get_objects_inside_radius(pos, max_entity_pos)) do local entity = objref:get_luaentity() - if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then + if entity and ndef.display_entities[entity.name] and + entity.nodepos and vector.equals(pos, entity.nodepos) then if objrefs[entity.name] then - objref:remove() + objref:remove() -- Remove duplicates else objrefs[entity.name] = objref end @@ -86,7 +80,7 @@ end local function clip_pos_prop(posprop) if posprop then - return math.max(-1.5, math.min(1.5, posprop)) + return math.max(-max_entity_pos, math.min(max_entity_pos, posprop)) else return 0 end @@ -98,43 +92,52 @@ local function place_entities(pos) local ndef = minetest.registered_nodes[node.name] local values = get_values(node) local objrefs = get_entities(pos) - + if values and ndef and ndef.display_entities then - for entity_name, props in pairs(ndef.display_entities) do local depth = clip_pos_prop(props.depth) local right = clip_pos_prop(props.right) local top = clip_pos_prop(props.top) if not objrefs[entity_name] then - objrefs[entity_name] = minetest.add_entity(pos, entity_name) + objrefs[entity_name] = minetest.add_entity(pos, entity_name, + minetest.serialize({ nodepos = pos })) end - + objrefs[entity_name]:setpos({ x = pos.x - values.dx * depth + values.rx * right, y = pos.y - top, z = pos.z - values.dz * depth + values.rz * right}) - + 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) + +--- Entity update +function update_entity(entity) + if not entity.nodepos then + entity.object:remove() -- Remove old/buggy entity + return + end + + local node = minetest.get_node(entity.nodepos) + local ndef = minetest.registered_nodes[node.name] + if ndef and ndef.display_entities and + ndef.display_entities[entity.name] and + ndef.display_entities[entity.name].on_display_update + then + -- Call on_display_update callback of a node for one of its display entities + ndef.display_entities[entity.name].on_display_update(entity.nodepos, + entity.object) end end --- Force entity update function display_api.update_entities(pos) - local objrefs = place_entities(pos) - for _, objref in pairs(objrefs) do - objref:get_luaentity().pos = minetest.hash_node_position(pos) - call_node_on_display_update(pos, objref) + for _, objref in pairs(place_entities(pos)) do + update_entity(objref:get_luaentity()) end end @@ -145,36 +148,28 @@ function display_api.on_activate(entity, staticdata) 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 + entity.nodepos = data.nodepos end + entity.object:set_armor_groups({immortal=1}) 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) + update_entity(entity) end end ---- On_place callback for display_api items. Does nothing more than preventing item ---- from being placed on ceiling or ground +--- On_place callback for display_api items. +-- Does nothing more than preventing node from being placed on ceiling or ground function display_api.on_place(itemstack, placer, pointed_thing, override_param2) local ndef = itemstack:get_definition() local above = pointed_thing.above local under = pointed_thing.under - local dir = {x = under.x - above.x, - y = 0, - z = under.z - above.z} - + local dir = {x = under.x - above.x, y = 0, z = under.z - above.z} + -- If item is not placed on a wall, use the player's view direction instead if dir.x == 0 and dir.z == 0 then dir = placer:get_look_dir() dir.y = 0 end - + local param2 = 0 if ndef then local paramtype2 = ndef.paramtype2 @@ -184,19 +179,20 @@ function display_api.on_place(itemstack, placer, pointed_thing, override_param2) param2 = minetest.dir_to_facedir(dir) end end - return minetest.item_place(itemstack, placer, pointed_thing, param2 + (override_param2 or 0)) + return minetest.item_place(itemstack, placer, pointed_thing, + param2 + (override_param2 or 0)) end ---- On_construct callback for display_api items. Creates entities and update them. +--- On_construct callback for display_api items. +-- Creates entities and update them. function display_api.on_construct(pos) display_api.update_entities(pos) end ---- On_destruct callback for display_api items. Removes entities. +--- On_destruct callback for display_api items. +-- Removes entities. function display_api.on_destruct(pos) - local objrefs = get_entities(pos) - - for _, objref in pairs(objrefs) do + for _, objref in pairs(get_entities(pos)) do objref:remove() end end @@ -222,9 +218,7 @@ function display_api.register_display_entity(entity_name) textures = {}, on_activate = display_api.on_activate, get_staticdata = function(self) - return minetest.serialize({ - pos = self.pos, - }) + return minetest.serialize({ nodepos = self.nodepos }) end, }) end -- cgit v1.2.3 From bcc181cb154b614007e595b1b991ab218534cdce Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sat, 1 Dec 2018 20:44:10 +0100 Subject: Replaced core reference by usual minetest reference --- display_api/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'display_api') diff --git a/display_api/init.lua b/display_api/init.lua index c9d8c19..4c0be22 100644 --- a/display_api/init.lua +++ b/display_api/init.lua @@ -146,7 +146,7 @@ end function display_api.on_activate(entity, staticdata) if entity then if string.sub(staticdata, 1, string.len("return")) == "return" then - local data = core.deserialize(staticdata) + local data = minetest.deserialize(staticdata) if data and type(data) == "table" then entity.nodepos = data.nodepos end -- cgit v1.2.3 From b88f67f01512f878486eb34b9160aeb2ba9794ac Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 2 Dec 2018 15:22:37 +0100 Subject: Avoid crash if entity not defined. --- display_api/init.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'display_api') diff --git a/display_api/init.lua b/display_api/init.lua index 4c0be22..bf95ded 100644 --- a/display_api/init.lua +++ b/display_api/init.lua @@ -117,6 +117,10 @@ end --- Entity update function update_entity(entity) + if not entity then + return + end + if not entity.nodepos then entity.object:remove() -- Remove old/buggy entity return -- cgit v1.2.3