diff options
| author | Emmanuel <emmanuel@chthonicsoftware.com> | 2020-09-07 11:59:13 -0600 |
|---|---|---|
| committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-03-05 09:45:58 +0100 |
| commit | 6b9057a1f2c0f3fabbf061f5bd49519920dd783b (patch) | |
| tree | e44766106e3de04a962be77422ddaf5d77823891 /src/script | |
| parent | ac8ac191691a13162667314358e96f07a65d0d1a (diff) | |
| download | minetest-6b9057a1f2c0f3fabbf061f5bd49519920dd783b.tar.xz | |
Ability to define and use LUA wield animations
Diffstat (limited to 'src/script')
| -rw-r--r-- | src/script/common/c_content.cpp | 109 | ||||
| -rw-r--r-- | src/script/common/c_content.h | 3 | ||||
| -rw-r--r-- | src/script/lua_api/l_item.cpp | 11 | ||||
| -rw-r--r-- | src/script/lua_api/l_item.h | 1 |
4 files changed, 123 insertions, 1 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 6995f6b61..90787e13d 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -34,6 +34,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "noise.h" #include "server/player_sao.h" #include "util/pointedthing.h" +#include <utility> +#include <algorithm> #include "debug.h" // For FATAL_ERROR #include <json/json.h> @@ -61,6 +63,7 @@ void read_item_definition(lua_State* L, int index, getstringfield(L, index, "inventory_overlay", def.inventory_overlay); getstringfield(L, index, "wield_image", def.wield_image); getstringfield(L, index, "wield_overlay", def.wield_overlay); + getstringfield(L, index, "wield_animation", def.wield_animation); getstringfield(L, index, "palette", def.palette_image); // Read item color. @@ -120,7 +123,113 @@ void read_item_definition(lua_State* L, int index, getstringfield(L, index, "node_placement_prediction", def.node_placement_prediction); } +/******************************************************************************/ +void read_wield_animation(lua_State *L, int index) +{ + if (index < 0) + index = lua_gettop(L) + 1 + index; + std::string name; + getstringfield(L, index, "name", name); + if (WieldAnimation::repository.size() == 0) + WieldAnimation::fillRepository(); + if (WieldAnimation::repository.find(name) != WieldAnimation::repository.end()) { + WieldAnimation::repository.erase(name); + } + WieldAnimation &anim = WieldAnimation::repository[name]; + anim.name = name; + float totalDuration; + getfloatfield(L, index, "duration", totalDuration); + lua_getfield(L, index, "translation"); + if (lua_istable(L, -1)) { + int table_translation = lua_gettop(L); + lua_pushnil(L); + lua_getfield(L, table_translation, "nodes"); + if (lua_istable(L, -1)) { + int table_nodes = lua_gettop(L); + lua_pushnil(L); + std::vector<std::pair<int, v3f>> nodes; + while (lua_next(L, table_nodes) != 0) { + int i = luaL_checkinteger(L, -2); + v3f node = check_v3f(L, -1); + std::pair<int, v3f> unorderedNode(i, node); + nodes.push_back(unorderedNode); + lua_pop(L, 1); + } + sort(nodes.begin(), nodes.end()); + for (std::size_t i = 0; i < nodes.size(); i++) { + anim.m_translationspline.addNode(nodes[i].second); + } + } + lua_pop(L, 1); + lua_getfield(L, table_translation, "indices"); + if (lua_istable(L, -1)) { + int table_indices = lua_gettop(L); + lua_pushnil(L); + while (lua_next(L, table_indices) != 0) { + if (lua_istable(L, -1)) { + float duration; + u32 offset = 0; + u32 length = 0; + getfloatfield(L, -1, "duration", duration); + getintfield(L, -1, "offset", offset); + getintfield(L, -1, "length", length); + anim.m_translationspline.addIndex(duration, offset, length); + } + lua_pop(L, 1); + } + } + lua_pop(L, 1); + } + lua_pop(L, 1); + + lua_getfield(L, index, "rotation"); + if (lua_istable(L, -1)) { + int table_rotation = lua_gettop(L); + lua_pushnil(L); + lua_getfield(L, table_rotation, "nodes"); + if (lua_istable(L, -1)) { + int table_nodes = lua_gettop(L); + lua_pushnil(L); + std::vector<std::pair<int, v3f>> nodes; + while (lua_next(L, table_nodes) != 0) { + int i = luaL_checkinteger(L, -2); + v3f node = check_v3f(L, -1); + std::pair<int, v3f> unorderedNode(i, node); + nodes.push_back(unorderedNode); + lua_pop(L, 1); + } + sort(nodes.begin(), nodes.end()); + for (std::size_t i = 0; i < nodes.size(); i++) { + anim.m_rotationspline.addNode( + WieldAnimation::quatFromAngles( + nodes[i].second.X, + nodes[i].second.Y, + nodes[i].second.Z)); + } + } + lua_getfield(L, table_rotation, "indices"); + if (lua_istable(L, -1)) { + int table_indices = lua_gettop(L); + lua_pushnil(L); + while (lua_next(L, table_indices) != 0) { + if (lua_istable(L, -1)) { + float duration; + u32 offset = 0; + u32 length = 0; + getfloatfield(L, -1, "duration", duration); + getintfield(L, -1, "offset", offset); + getintfield(L,-1, "length", length); + anim.m_rotationspline.addIndex(duration, offset, length); + } + lua_pop(L, 1); + } + } + lua_pop(L, 1); + } + anim.setDuration(totalDuration); + lua_pop(L, 1); +} /******************************************************************************/ void push_item_definition(lua_State *L, const ItemDefinition &i) { diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 29d576355..2733be663 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -33,7 +33,6 @@ extern "C" { #include <iostream> #include <vector> - #include "irrlichttypes_bloated.h" #include "util/string.h" #include "itemgroup.h" @@ -48,6 +47,7 @@ class NodeDefManager; struct PointedThing; struct ItemStack; struct ItemDefinition; + struct ToolCapabilities; struct ObjectProperties; struct SimpleSoundSpec; @@ -109,6 +109,7 @@ void push_item_definition (lua_State *L, const ItemDefinition &i); void push_item_definition_full (lua_State *L, const ItemDefinition &i); +void read_wield_animation(lua_State *L, int index); void read_object_properties (lua_State *L, int index, ServerActiveObject *sao, diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 9e0da4034..a765c96df 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -665,8 +665,19 @@ int ModApiItemMod::l_get_name_from_content_id(lua_State *L) return 1; /* number of results */ } +int ModApiItemMod::l_register_wield_animation(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + luaL_checktype(L, 1, LUA_TTABLE); + int table = 1; + + read_wield_animation(L, table); + return 0; /* number of results */ +} + void ModApiItemMod::Initialize(lua_State *L, int top) { + API_FCT(register_wield_animation); API_FCT(register_item_raw); API_FCT(unregister_item_raw); API_FCT(register_alias_raw); diff --git a/src/script/lua_api/l_item.h b/src/script/lua_api/l_item.h index 16878c101..771f1f504 100644 --- a/src/script/lua_api/l_item.h +++ b/src/script/lua_api/l_item.h @@ -152,6 +152,7 @@ private: static int l_register_alias_raw(lua_State *L); static int l_get_content_id(lua_State *L); static int l_get_name_from_content_id(lua_State *L); + static int l_register_wield_animation(lua_State *L); public: static void Initialize(lua_State *L, int top); }; |
