mirror of
https://github.com/pyrollo/display_modpack.git
synced 2024-08-20 04:34:05 +02:00
Add 'display_api/' from commit '3a43e4ad81dc6595fabfa22c88e229c393f93529'
git-subtree-dir: display_api git-subtree-mainline: 135cae721906d312cd0084f7343a15760e3aa722 git-subtree-split: 3a43e4ad81dc6595fabfa22c88e229c393f93529
This commit is contained in:
commit
f27b5478b9
92
display_api/API.md
Normal file
92
display_api/API.md
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
# Display API
|
||||||
|
This document describes Display API. Display API allows to add a dynamic display on a node. Display API limits node rotations. For wallmounted, only vertical positionning is available. For facedir, only first four position are availabel (those with default axis).
|
||||||
|
|
||||||
|
## Provided methods
|
||||||
|
### update\_entities
|
||||||
|
**display\_api.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\_api.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\_api.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\_api.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\_api.on_destruct(pos)**
|
||||||
|
|
||||||
|
`on_destruct` node callback implementation. Display nodes should have this callback (removes display entities on node destruction).
|
||||||
|
### on_rotate
|
||||||
|
**display\_api.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\_api.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_api` 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 `top`: 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), `top` goes from bottom (-0.5) to top (0.5) and `right` goes from left (-0.5) to right (0.5).\
|
||||||
|
`yaw`: Entity yaw in radians, regarding to main axis. Default is 0, aligned to node face.
|
||||||
|
|
||||||
|
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_api = 1, ... },
|
||||||
|
...
|
||||||
|
display_entities = {
|
||||||
|
["mymod:entity1"] = {
|
||||||
|
depth = 0.3,
|
||||||
|
on_display_update = my_display_update1 },
|
||||||
|
["mymod:entity1"] = {
|
||||||
|
depth = 0.2, top = 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,
|
||||||
|
...
|
||||||
|
})
|
166
display_api/LICENSE.txt
Normal file
166
display_api/LICENSE.txt
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
GNU LESSER GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||||
|
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.
|
||||||
|
|
34
display_api/README.md
Normal file
34
display_api/README.md
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
# 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.
|
||||||
|
|
||||||
|
**Limitations**: This lib uses entities to draw display. This means display has to be vertical (and "upside up") on Minetest before version 5.0.
|
||||||
|
|
||||||
|
**Dependancies**:default
|
||||||
|
|
||||||
|
**License**: LGPLv2
|
||||||
|
|
||||||
|
**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.
|
||||||
|
|
||||||
|
## Deprecation notice (for modders)
|
||||||
|
|
||||||
|
### December 2018
|
||||||
|
Following objects are deprecated, shows a warning in log when used:
|
||||||
|
* `display_modpack_node` group (use `display_api` group instead);
|
||||||
|
* `display_lib_node` group (use `display_api` group instead);
|
||||||
|
* `display_lib` global table (use `display_api` global table instead);
|
||||||
|
|
||||||
|
These objects will be removed in the future.
|
||||||
|
|
||||||
|
## Change log
|
||||||
|
### 2019-03-14
|
||||||
|
- __dispay_api__: Display API now detects automatically whenr rotation restrictions have to be applied.
|
||||||
|
|
||||||
|
### 2019-03-09
|
||||||
|
- __display_api__: Display nodes can be rotated in every directions (if running Minetest 5 or above).
|
||||||
|
- __display_api__: New setting to restrict rotations to Minetest 0.4 abilities (restriction enabled by default).
|
||||||
|
|
||||||
|
### 2018-12-14
|
||||||
|
- __display_api__: New `yaw` attributes, entities can now have different angles with node.
|
5
display_api/copyright.txt
Normal file
5
display_api/copyright.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Code by Pierre-Yves Rollo (pyrollo)
|
||||||
|
Contributors:
|
||||||
|
(gpcf): Compatibility with signs lib
|
||||||
|
(Thomas--S): Fix /clearobjects bug
|
||||||
|
(12Me21): on_place and on_rotate improvements
|
0
display_api/depends.txt
Normal file
0
display_api/depends.txt
Normal file
78
display_api/deprecation.lua
Normal file
78
display_api/deprecation.lua
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
--[[
|
||||||
|
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 <http://www.gnu.org/licenses/>.
|
||||||
|
--]]
|
||||||
|
|
||||||
|
-- Deprecation
|
||||||
|
|
||||||
|
function deprecated_group(deprecated_group, replacement_group)
|
||||||
|
for name, ndef in pairs(minetest.registered_nodes) do
|
||||||
|
if ndef.groups and ndef.groups[deprecated_group] then
|
||||||
|
minetest.log("warning", string.format(
|
||||||
|
'Node %s belongs to deprecated "%s" group which should be replaced with new "%s" group.',
|
||||||
|
name, deprecated_group, replacement_group))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function deprecated_global_table(deprecated_global_name, replacement_global_name)
|
||||||
|
assert(type(deprecated_global_name) == 'string', "deprecated_global_name should be a string.")
|
||||||
|
assert(type(replacement_global_name) == 'string', "replacement_global_name should be a string.")
|
||||||
|
assert(deprecated_global_name ~= '', "deprecated_global_name should not be empty.")
|
||||||
|
assert(replacement_global_name ~= '', "replacement_global_name should not be empty.")
|
||||||
|
assert(rawget(_G, deprecated_global_name) == nil, "deprecated global does not exist.")
|
||||||
|
if _G[replacement_global_name] == nil then
|
||||||
|
minetest.log('warning', string.format(
|
||||||
|
'Replacement global "%s" does not exists.', replacement_global_name))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local meta = {
|
||||||
|
deprecated = deprecated_global_name,
|
||||||
|
replacement = replacement_global_name,
|
||||||
|
__index = function(table, key)
|
||||||
|
local meta = getmetatable(table)
|
||||||
|
local dbg = debug.getinfo(2, "lS")
|
||||||
|
minetest.log("warning", string.format(
|
||||||
|
'Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).',
|
||||||
|
meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'),
|
||||||
|
(dbg.currentline or 0)))
|
||||||
|
return _G[meta.replacement][key]
|
||||||
|
end,
|
||||||
|
__newindex = function(table, key, value)
|
||||||
|
local meta = getmetatable(table)
|
||||||
|
local dbg = debug.getinfo(2, "lS")
|
||||||
|
minetest.log("warning", string.format(
|
||||||
|
'Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).',
|
||||||
|
meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'),
|
||||||
|
(dbg.currentline or 0)))
|
||||||
|
_G[meta.replacement][key]=value
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
rawset(_G, deprecated_global_name, {})
|
||||||
|
setmetatable(_G[deprecated_global_name], meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- deprecated(1) -- December 2018 - Deprecation of groups display_modpack_node and display_lib_node
|
||||||
|
-- Group to be removed from display API register_lbm
|
||||||
|
minetest.after(0, function()
|
||||||
|
deprecated_group("display_modpack_node", "display_api")
|
||||||
|
deprecated_group("display_lib_node", "display_api")
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- deprecated(2) -- December 2018 - Deprecation of display_lib
|
||||||
|
deprecated_global_table('display_lib', 'display_api')
|
325
display_api/display.lua
Normal file
325
display_api/display.lua
Normal file
@ -0,0 +1,325 @@
|
|||||||
|
--[[
|
||||||
|
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 <http://www.gnu.org/licenses/>.
|
||||||
|
--]]
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
|
||||||
|
-- Maximum entity position relative to the node pos
|
||||||
|
local max_entity_pos = 1.5
|
||||||
|
|
||||||
|
local wallmounted_rotations = {
|
||||||
|
[0]={x=1, y=0, z=0}, [1]={x=3, y=0, z=0},
|
||||||
|
[2]={x=0, y=3, z=0}, [3]={x=0, y=1, z=0},
|
||||||
|
[4]={x=0, y=0, z=0}, [5]={x=0, y=2, z=0},
|
||||||
|
[6]={x=1, y=0, z=0}, [7]={x=1, y=1, z=1},
|
||||||
|
}
|
||||||
|
|
||||||
|
local facedir_rotations = {
|
||||||
|
[ 0]={x=0, y=0, z=0}, [ 1]={x=0, y=3, z=0},
|
||||||
|
[ 2]={x=0, y=2, z=0}, [ 3]={x=0, y=1, z=0},
|
||||||
|
[ 4]={x=3, y=0, z=0}, [ 5]={x=0, y=3, z=3},
|
||||||
|
[ 6]={x=1, y=0, z=2}, [ 7]={x=0, y=1, z=1},
|
||||||
|
[ 8]={x=1, y=0, z=0}, [ 9]={x=0, y=3, z=1},
|
||||||
|
[10]={x=3, y=0, z=2}, [11]={x=0, y=1, z=3},
|
||||||
|
[12]={x=0, y=0, z=1}, [13]={x=3, y=0, z=1},
|
||||||
|
[14]={x=2, y=0, z=1}, [15]={x=1, y=0, z=1},
|
||||||
|
[16]={x=0, y=0, z=3}, [17]={x=1, y=0, z=3},
|
||||||
|
[18]={x=2, y=0, z=3}, [19]={x=3, y=0, z=3},
|
||||||
|
[20]={x=0, y=0, z=2}, [21]={x=0, y=1, z=2},
|
||||||
|
[22]={x=0, y=2, z=2}, [23]={x=0, y=3, z=2},
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Compute other useful values depending on wallmounted and facedir param
|
||||||
|
local wallmounted_values = {}
|
||||||
|
local facedir_values = {}
|
||||||
|
|
||||||
|
local function compute_values(r)
|
||||||
|
local function rx(v) return { x=v.x, y=v.z, z=-v.y} end
|
||||||
|
local function ry(v) return { x=-v.z, y=v.y, z=v.x} end
|
||||||
|
local function rz(v) return { x=v.y, y=-v.x, z=v.z} end
|
||||||
|
|
||||||
|
local d = { x = 0, y = 0, z = 1 }
|
||||||
|
local w = { x = 1, y = 0, z = 0 }
|
||||||
|
local h = { x = 0, y = 1, z = 0 }
|
||||||
|
|
||||||
|
-- Important to keep z rotation first (not same results)
|
||||||
|
for _ = 1, r.z do d, w, h = rz(d), rz(w), rz(h) end
|
||||||
|
for _ = 1, r.x do d, w, h = rx(d), rx(w), rx(h) end
|
||||||
|
for _ = 1, r.y do d, w, h = ry(d), ry(w), ry(h) end
|
||||||
|
|
||||||
|
return {
|
||||||
|
rotation=r, depth=d, width=w, height=h,
|
||||||
|
restricted=(r.x==0 and r.z==0) }
|
||||||
|
end
|
||||||
|
|
||||||
|
for i, r in pairs(facedir_rotations) do
|
||||||
|
facedir_values[i] = compute_values(r)
|
||||||
|
end
|
||||||
|
|
||||||
|
for i, r in pairs(wallmounted_rotations) do
|
||||||
|
wallmounted_values[i] = compute_values(r)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Detect rotation restriction
|
||||||
|
local rotation_restricted = nil
|
||||||
|
minetest.register_entity('display_api:dummy_entity', {
|
||||||
|
collisionbox = { 0, 0, 0, 0, 0, 0 },
|
||||||
|
visual = "upright_sprite",
|
||||||
|
textures = {} })
|
||||||
|
|
||||||
|
function display_api.is_rotation_restricted()
|
||||||
|
if rotation_restricted == nil then
|
||||||
|
local objref = minetest.add_entity(
|
||||||
|
{x=0, y=0, z=0}, 'display_api:dummy_entity')
|
||||||
|
if objref then
|
||||||
|
rotation_restricted = objref.set_rotation == nil
|
||||||
|
objref:remove()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return rotation_restricted
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Clip position property to maximum entity position
|
||||||
|
|
||||||
|
local function clip_pos_prop(posprop)
|
||||||
|
if posprop then
|
||||||
|
return math.max(-max_entity_pos, math.min(max_entity_pos, posprop))
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Get values needed for orientation computation of node
|
||||||
|
|
||||||
|
local function get_orientation_values(node)
|
||||||
|
local ndef = minetest.registered_nodes[node.name]
|
||||||
|
|
||||||
|
if ndef then
|
||||||
|
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]
|
||||||
|
else
|
||||||
|
-- No orientation or unknown orientation type
|
||||||
|
return facedir_values[0]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Gets the display entities attached with a node.
|
||||||
|
-- Add missing and remove duplicates
|
||||||
|
|
||||||
|
local function get_display_objrefs(pos, create)
|
||||||
|
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, max_entity_pos)) do
|
||||||
|
local entity = objref:get_luaentity()
|
||||||
|
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() -- Remove duplicates
|
||||||
|
else
|
||||||
|
objrefs[entity.name] = objref
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if create then
|
||||||
|
-- Add missing
|
||||||
|
for name, _ in pairs(ndef.display_entities) do
|
||||||
|
if not objrefs[name] then
|
||||||
|
objrefs[name] = minetest.add_entity(pos, name,
|
||||||
|
minetest.serialize({ nodepos = pos }))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return objrefs
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Force entity update : position and texture
|
||||||
|
function display_api.update_entities(pos)
|
||||||
|
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
local ndef = minetest.registered_nodes[node.name]
|
||||||
|
local ov = get_orientation_values(node)
|
||||||
|
if not ndef or not ov then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, objref in pairs(get_display_objrefs(pos, true)) do
|
||||||
|
local edef = ndef.display_entities[objref:get_luaentity().name]
|
||||||
|
local depth = clip_pos_prop(edef.depth)
|
||||||
|
local right = clip_pos_prop(edef.right)
|
||||||
|
local top = clip_pos_prop(edef.top)
|
||||||
|
|
||||||
|
objref:set_pos({
|
||||||
|
x = pos.x + ov.depth.x*depth + ov.width.x*right - ov.height.x*top,
|
||||||
|
y = pos.y + ov.depth.y*depth + ov.width.y*right - ov.height.y*top,
|
||||||
|
z = pos.z + ov.depth.z*depth + ov.width.z*right - ov.height.z*top,
|
||||||
|
})
|
||||||
|
|
||||||
|
if objref.set_rotation then
|
||||||
|
objref:set_rotation({
|
||||||
|
x = ov.rotation.x*math.pi/2,
|
||||||
|
y = ov.rotation.y*math.pi/2 + (edef.yaw or 0),
|
||||||
|
z = ov.rotation.z*math.pi/2,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
if ov.rotation.x ~=0 or ov.rotation.y ~= 0 then
|
||||||
|
minetest.log("warning", string.format(
|
||||||
|
"[display_api] unable to rotate correctly entity for node at %s without set_rotation method.",
|
||||||
|
minetest.pos_to_string(pos)))
|
||||||
|
end
|
||||||
|
objref:set_yaw(ov.rotation.y*math.pi/2 + (edef.yaw or 0))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Call on_display_update callback of a node for one of its display entities
|
||||||
|
if edef.on_display_update then
|
||||||
|
edef.on_display_update(pos, objref)
|
||||||
|
end
|
||||||
|
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 = minetest.deserialize(staticdata)
|
||||||
|
if data and type(data) == "table" then
|
||||||
|
entity.nodepos = data.nodepos
|
||||||
|
end
|
||||||
|
entity.object:set_armor_groups({immortal=1})
|
||||||
|
end
|
||||||
|
|
||||||
|
if entity.nodepos then
|
||||||
|
local node = minetest.get_node(entity.nodepos)
|
||||||
|
local ndef = minetest.registered_nodes[node.name]
|
||||||
|
if ndef and ndef.display_entities then
|
||||||
|
local edef = ndef.display_entities[entity.name]
|
||||||
|
if edef then
|
||||||
|
-- Call on_display_update callback of the entity to build texture
|
||||||
|
if edef.on_display_update then
|
||||||
|
edef.on_display_update(entity.nodepos, entity.object)
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- If we got here, this display entity is buggy and should be removed
|
||||||
|
entity.object:remove()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- On_place callback for display_api items.
|
||||||
|
-- Does nothing more than preventing node from being placed on ceiling or ground
|
||||||
|
-- TODO:When MT<5 is not in use anymore, simplify this
|
||||||
|
function display_api.on_place(itemstack, placer, pointed_thing, override_param2)
|
||||||
|
local ndef = itemstack:get_definition()
|
||||||
|
local dir = {
|
||||||
|
x = pointed_thing.under.x - pointed_thing.above.x,
|
||||||
|
y = pointed_thing.under.y - pointed_thing.above.y,
|
||||||
|
z = pointed_thing.under.z - pointed_thing.above.z,
|
||||||
|
}
|
||||||
|
|
||||||
|
local rotation_restriction = display_api.is_rotation_restricted()
|
||||||
|
|
||||||
|
if rotation_restriction then
|
||||||
|
-- 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()
|
||||||
|
end
|
||||||
|
dir.y = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
local param2 = 0
|
||||||
|
if ndef then
|
||||||
|
if ndef.paramtype2 == "wallmounted" or
|
||||||
|
ndef.paramtype2 == "colorwallmounted" then
|
||||||
|
param2 = minetest.dir_to_wallmounted(dir)
|
||||||
|
|
||||||
|
elseif ndef.paramtype2 == "facedir" or
|
||||||
|
ndef.paramtype2 == "colorfacedir" then
|
||||||
|
param2 = minetest.dir_to_facedir(dir, not rotation_restriction)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
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.
|
||||||
|
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)
|
||||||
|
for _, objref in pairs(get_display_objrefs(pos)) do
|
||||||
|
objref:remove()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
local ov = get_orientation_values(node)
|
||||||
|
if not ov then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if ov.restricted or not display_api.is_rotation_restricted() then
|
||||||
|
minetest.swap_node(pos, node)
|
||||||
|
display_api.update_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_entities[entity_name] 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({ nodepos = self.nodepos })
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_lbm({
|
||||||
|
label = "Update display_api entities",
|
||||||
|
name = "display_api:update_entities",
|
||||||
|
run_at_every_load = true,
|
||||||
|
nodenames = {"group:display_api",
|
||||||
|
"group:display_modpack_node", "group:display_lib_node"}, -- See deprecated(1)
|
||||||
|
action = function(pos, node) display_api.update_entities(pos) end,
|
||||||
|
})
|
31
display_api/init.lua
Normal file
31
display_api/init.lua
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
--[[
|
||||||
|
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 <http://www.gnu.org/licenses/>.
|
||||||
|
--]]
|
||||||
|
|
||||||
|
-- Global variables
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
display_api = {}
|
||||||
|
display_api.name = minetest.get_current_modname()
|
||||||
|
display_api.path = minetest.get_modpath(display_api.name)
|
||||||
|
|
||||||
|
-- Inclusions
|
||||||
|
-------------
|
||||||
|
|
||||||
|
dofile(display_api.path.."/display.lua")
|
||||||
|
dofile(display_api.path.."/deprecation.lua")
|
2
display_api/mod.conf
Normal file
2
display_api/mod.conf
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
name=display_api
|
||||||
|
description=A library for adding dynamic textures on nodes
|
Loading…
Reference in New Issue
Block a user