From 3caad3f3c9e319ca67d63231e8c64b2ace855fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Juh=C3=A1sz?= Date: Sat, 23 Jul 2016 21:11:20 +0200 Subject: Expose getPointedThing to Lua This commit introduces Raycast, a Lua user object, which can be used to perform a raycast on the map. The ray is continuable, so one can also get hidden nodes (for example to see trough glass). --- src/script/lua_api/l_env.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/script/lua_api/l_env.h') diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index 7ce19b085..f380d8d6f 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_base.h" #include "serverenvironment.h" +#include "raycast.h" class ModApiEnvMod : public ModApiBase { private: @@ -159,6 +160,9 @@ private: // line_of_sight(pos1, pos2, stepsize) -> true/false static int l_line_of_sight(lua_State *L); + // raycast(pos1, pos2, objects, liquids) -> Raycast + static int l_raycast(lua_State *L); + // find_path(pos1, pos2, searchdistance, // max_jump, max_drop, algorithm) -> table containing path static int l_find_path(lua_State *L); @@ -245,6 +249,47 @@ public: virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n); }; +//! Lua wrapper for RaycastState objects +class LuaRaycast : public ModApiBase +{ +private: + static const char className[]; + static const luaL_Reg methods[]; + //! Inner state + RaycastState state; + + // Exported functions + + // garbage collector + static int gc_object(lua_State *L); + + /*! + * Raycast:next() -> pointed_thing + * Returns the next pointed thing on the ray. + */ + static int l_next(lua_State *L); +public: + //! Constructor with the same arguments as RaycastState. + LuaRaycast( + const core::line3d &shootline, + bool objects_pointable, + bool liquids_pointable) : + state(shootline, objects_pointable, liquids_pointable) + {} + + //! Creates a LuaRaycast and leaves it on top of the stack. + static int create_object(lua_State *L); + + /*! + * Returns the Raycast from the stack or throws an error. + * @param narg location of the RaycastState in the stack + */ + static LuaRaycast *checkobject(lua_State *L, int narg); + + //! Registers Raycast as a Lua userdata type. + static void Register(lua_State *L); +}; + struct ScriptCallbackState { ServerScripting *script; int callback_ref; -- cgit v1.2.3