diff options
author | sfan5 <sfan5@live.de> | 2020-04-14 14:11:33 +0200 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2020-04-27 20:45:46 +0200 |
commit | 3475759d1adbd4a64c6250fd87981f783e64f69c (patch) | |
tree | 50e1a4402a2ecc83b6f11a76ff27c7b5b3ac59ad /src/script | |
parent | aef59f2ad9a5a5a217ddadc05c46fd4d23cef47f (diff) | |
download | minetest-3475759d1adbd4a64c6250fd87981f783e64f69c.tar.gz minetest-3475759d1adbd4a64c6250fd87981f783e64f69c.tar.bz2 minetest-3475759d1adbd4a64c6250fd87981f783e64f69c.zip |
Expose collision information to LuaEntity on_step
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/common/c_content.cpp | 54 | ||||
-rw-r--r-- | src/script/common/c_content.h | 4 | ||||
-rw-r--r-- | src/script/cpp_api/s_entity.cpp | 12 | ||||
-rw-r--r-- | src/script/cpp_api/s_entity.h | 4 |
4 files changed, 68 insertions, 6 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 8335fccb5..6ff642738 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_types.h" #include "nodedef.h" #include "object_properties.h" +#include "collision.h" #include "cpp_api/s_node.h" #include "lua_api/l_object.h" #include "lua_api/l_item.h" @@ -2002,3 +2003,56 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value) } return stat; } + +/******************************************************************************/ + +// Indices must match values in `enum CollisionType` exactly!! +static const char *collision_type_str[] = { + "node", + "object", +}; + +// Indices must match values in `enum CollisionAxis` exactly!! +static const char *collision_axis_str[] = { + "x", + "y", + "z", +}; + +void push_collision_move_result(lua_State *L, const collisionMoveResult &res) +{ + lua_createtable(L, 0, 4); + + setboolfield(L, -1, "touching_ground", res.touching_ground); + setboolfield(L, -1, "collides", res.collides); + setboolfield(L, -1, "standing_on_object", res.standing_on_object); + + /* collisions */ + lua_createtable(L, res.collisions.size(), 0); + int i = 1; + for (const auto &c : res.collisions) { + lua_createtable(L, 0, 5); + + lua_pushstring(L, collision_type_str[c.type]); + lua_setfield(L, -2, "type"); + + assert(c.axis != COLLISION_AXIS_NONE); + lua_pushstring(L, collision_axis_str[c.axis]); + lua_setfield(L, -2, "axis"); + + if (c.type == COLLISION_NODE) { + push_v3s16(L, c.node_p); + lua_setfield(L, -2, "node_pos"); + } + + push_v3f(L, c.old_speed / BS); + lua_setfield(L, -2, "old_speed"); + + push_v3f(L, c.new_speed / BS); + lua_setfield(L, -2, "new_speed"); + + lua_rawseti(L, -2, i++); + } + lua_setfield(L, -2, "collisions"); + /**/ +} diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 9e755682f..8f32e58eb 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -63,7 +63,9 @@ struct EnumString; struct NoiseParams; class Schematic; class ServerActiveObject; +struct collisionMoveResult; +extern struct EnumString es_TileAnimationType[]; ContentFeatures read_content_features (lua_State *L, int index); void push_content_features (lua_State *L, @@ -196,4 +198,4 @@ void push_hud_element (lua_State *L, HudElement *elem); HudElementStat read_hud_change (lua_State *L, HudElement *elem, void **value); -extern struct EnumString es_TileAnimationType[]; +void push_collision_move_result(lua_State *L, const collisionMoveResult &res); diff --git a/src/script/cpp_api/s_entity.cpp b/src/script/cpp_api/s_entity.cpp index 26c7e8cd4..ea9320051 100644 --- a/src/script/cpp_api/s_entity.cpp +++ b/src/script/cpp_api/s_entity.cpp @@ -178,12 +178,11 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id, lua_pop(L, 1); } -void ScriptApiEntity::luaentity_Step(u16 id, float dtime) +void ScriptApiEntity::luaentity_Step(u16 id, float dtime, + const collisionMoveResult *moveresult) { SCRIPTAPI_PRECHECKHEADER - //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl; - int error_handler = PUSH_ERROR_HANDLER(L); // Get core.luaentities[id] @@ -199,9 +198,14 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime) luaL_checktype(L, -1, LUA_TFUNCTION); lua_pushvalue(L, object); // self lua_pushnumber(L, dtime); // dtime + /* moveresult */ + if (moveresult) + push_collision_move_result(L, *moveresult); + else + lua_pushnil(L); setOriginFromTable(object); - PCALL_RES(lua_pcall(L, 2, 0, error_handler)); + PCALL_RES(lua_pcall(L, 3, 0, error_handler)); lua_pop(L, 2); // Pop object and error handler } diff --git a/src/script/cpp_api/s_entity.h b/src/script/cpp_api/s_entity.h index cc08c46e8..b5f7a6586 100644 --- a/src/script/cpp_api/s_entity.h +++ b/src/script/cpp_api/s_entity.h @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., struct ObjectProperties; struct ToolCapabilities; +struct collisionMoveResult; class ScriptApiEntity : virtual public ScriptApiBase @@ -36,7 +37,8 @@ public: std::string luaentity_GetStaticdata(u16 id); void luaentity_GetProperties(u16 id, ServerActiveObject *self, ObjectProperties *prop); - void luaentity_Step(u16 id, float dtime); + void luaentity_Step(u16 id, float dtime, + const collisionMoveResult *moveresult); bool luaentity_Punch(u16 id, ServerActiveObject *puncher, float time_from_last_punch, const ToolCapabilities *toolcap, v3f dir, s16 damage); |