mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Expose collision information to LuaEntity on_step
This commit is contained in:
parent
aef59f2ad9
commit
3475759d1a
7 changed files with 98 additions and 10 deletions
|
@ -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");
|
||||
/**/
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue