Add get_look_dir(), get_look_pitch() and get_look_yaw() for players
authorPerttu Ahola <celeron55@gmail.com>
Wed, 28 Dec 2011 15:34:07 +0000 (17:34 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Wed, 28 Dec 2011 15:34:07 +0000 (17:34 +0200)
data/mods/default/init.lua
data/mods/experimental/init.lua
src/player.h
src/scriptapi.cpp

index 95d9c19ff2e9ebb630bc41836fccd7fe5acdad7b..1edbd3cbbca5d9d56ab2333528eca542e8b83f17 100644 (file)
 --   ^ time_from_last_punch = time since last punch action of the puncher
 -- - right_click(clicker); clicker = an another ObjectRef
 -- - get_wield_digging_properties() -> digging property table
+-- - damage_wielded_item(num) (item damage/wear range is 0-65535)
+-- - add_to_inventory(itemstring): add an item to object inventory
 -- - add_to_inventory_later(itemstring): like above, but after callback returns (only allowed for craftitem callbacks)
 -- - get_hp(): returns number of hitpoints (2 * number of hearts)
 -- - set_hp(hp): set number of hitpoints (2 * number of hearts)
 -- - get_player_name(): will return nil if is not a player
 -- - inventory_set_list(name, {item1, item2, ...})
 -- - inventory_get_list(name) -> {item1, item2, ...}
--- - damage_wielded_item(num) (item damage/wear range is 0-65535)
--- - add_to_inventory(itemstring): add an item to object inventory
+-- - get_look_dir(): get camera direction as a unit vector
+-- - get_look_pitch(): pitch in radians
+-- - get_look_yaw(): yaw in radians (wraps around pretty randomly as of now)
 --
 -- Registered entities:
 -- - Functions receive a "luaentity" as self:
index d180d4778e5f09580b16447ef6efb578a1d5bfb7..52a729a4a0eb492a7c0498effa7ed122b22421b7 100644 (file)
@@ -2,6 +2,21 @@
 -- Experimental things
 --
 
+-- For testing random stuff
+
+function on_step(dtime)
+       -- print("experimental on_step")
+       --[[
+       print("celeron55 dir: "..dump(
+                       minetest.env:get_player_by_name("celeron55"):get_look_dir()))
+       print("celeron55 pitch: "..dump(
+                       minetest.env:get_player_by_name("celeron55"):get_look_pitch()))
+       print("celeron55 yaw: "..dump(
+                       minetest.env:get_player_by_name("celeron55"):get_look_yaw()))
+       --]]
+end
+minetest.register_globalstep(on_step)
+
 -- An example furnace-thing implemented in Lua
 
 minetest.register_node("experimental:luafurnace", {
index 107c829f8c52405c4b9845e4779d2885b3844801..1c9dde7e0aab7c369f58164a74f71577de08f048 100644 (file)
@@ -102,6 +102,16 @@ public:
                return m_yaw;
        }
 
+       f32 getRadPitch()
+       {
+               return -1.0 * m_pitch * core::DEGTORAD;
+       }
+
+       f32 getRadYaw()
+       {
+               return (m_yaw + 90.) * core::DEGTORAD;
+       }
+
        virtual void updateName(const char *name)
        {
                snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
index 4839f2ac494f9c31805543f043a00523e5f0b804..23336e32dc0e22124a362b703636978ca4140fd1 100644 (file)
@@ -167,6 +167,43 @@ void check_modname_prefix(lua_State *L, std::string &name)
                                +"\"contains unallowed characters");
 }
 
+static void push_v3f(lua_State *L, v3f p)
+{
+       lua_newtable(L);
+       lua_pushnumber(L, p.X);
+       lua_setfield(L, -2, "x");
+       lua_pushnumber(L, p.Y);
+       lua_setfield(L, -2, "y");
+       lua_pushnumber(L, p.Z);
+       lua_setfield(L, -2, "z");
+}
+
+static v2s16 read_v2s16(lua_State *L, int index)
+{
+       v2s16 p;
+       luaL_checktype(L, index, LUA_TTABLE);
+       lua_getfield(L, index, "x");
+       p.X = lua_tonumber(L, -1);
+       lua_pop(L, 1);
+       lua_getfield(L, index, "y");
+       p.Y = lua_tonumber(L, -1);
+       lua_pop(L, 1);
+       return p;
+}
+
+static v2f read_v2f(lua_State *L, int index)
+{
+       v2f p;
+       luaL_checktype(L, index, LUA_TTABLE);
+       lua_getfield(L, index, "x");
+       p.X = lua_tonumber(L, -1);
+       lua_pop(L, 1);
+       lua_getfield(L, index, "y");
+       p.Y = lua_tonumber(L, -1);
+       lua_pop(L, 1);
+       return p;
+}
+
 static v3f readFloatPos(lua_State *L, int index)
 {
        v3f pos;
@@ -187,13 +224,7 @@ static v3f readFloatPos(lua_State *L, int index)
 static void pushFloatPos(lua_State *L, v3f p)
 {
        p /= BS;
-       lua_newtable(L);
-       lua_pushnumber(L, p.X);
-       lua_setfield(L, -2, "x");
-       lua_pushnumber(L, p.Y);
-       lua_setfield(L, -2, "y");
-       lua_pushnumber(L, p.Z);
-       lua_setfield(L, -2, "z");
+       push_v3f(L, p);
 }
 
 static void pushpos(lua_State *L, v3s16 p)
@@ -293,32 +324,6 @@ static core::aabbox3d<f32> read_aabbox3df32(lua_State *L, int index, f32 scale)
        return box;
 }
 
-static v2s16 read_v2s16(lua_State *L, int index)
-{
-       v2s16 p;
-       luaL_checktype(L, index, LUA_TTABLE);
-       lua_getfield(L, index, "x");
-       p.X = lua_tonumber(L, -1);
-       lua_pop(L, 1);
-       lua_getfield(L, index, "y");
-       p.Y = lua_tonumber(L, -1);
-       lua_pop(L, 1);
-       return p;
-}
-
-static v2f read_v2f(lua_State *L, int index)
-{
-       v2f p;
-       luaL_checktype(L, index, LUA_TTABLE);
-       lua_getfield(L, index, "x");
-       p.X = lua_tonumber(L, -1);
-       lua_pop(L, 1);
-       lua_getfield(L, index, "y");
-       p.Y = lua_tonumber(L, -1);
-       lua_pop(L, 1);
-       return p;
-}
-
 static bool getstringfield(lua_State *L, int table,
                const char *fieldname, std::string &result)
 {
@@ -2222,6 +2227,42 @@ private:
                return 1;
        }
 
+       // get_look_dir(self)
+       static int l_get_look_dir(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               ServerRemotePlayer *player = getplayer(ref);
+               if(player == NULL) return 0;
+               // Do it
+               float pitch = player->getRadPitch();
+               float yaw = player->getRadYaw();
+               v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
+               push_v3f(L, v);
+               return 1;
+       }
+
+       // get_look_pitch(self)
+       static int l_get_look_pitch(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               ServerRemotePlayer *player = getplayer(ref);
+               if(player == NULL) return 0;
+               // Do it
+               lua_pushnumber(L, player->getRadPitch());
+               return 1;
+       }
+
+       // get_look_yaw(self)
+       static int l_get_look_yaw(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               ServerRemotePlayer *player = getplayer(ref);
+               if(player == NULL) return 0;
+               // Do it
+               lua_pushnumber(L, player->getRadYaw());
+               return 1;
+       }
+
 public:
        ObjectRef(ServerActiveObject *object):
                m_object(object)
@@ -2310,6 +2351,9 @@ const luaL_reg ObjectRef::methods[] = {
        method(ObjectRef, inventory_get_list),
        method(ObjectRef, get_wielded_itemstring),
        method(ObjectRef, get_wielded_item),
+       method(ObjectRef, get_look_dir),
+       method(ObjectRef, get_look_pitch),
+       method(ObjectRef, get_look_yaw),
        {0,0}
 };