From 8ad3dad137df740ce63101bf224e977e66b9df2c Mon Sep 17 00:00:00 2001 From: Vincent Glize Date: Sat, 8 Apr 2017 12:26:45 +0200 Subject: LocalPlayer api lua --- src/script/lua_api/l_localplayer.h | 95 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/script/lua_api/l_localplayer.h (limited to 'src/script/lua_api/l_localplayer.h') diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h new file mode 100644 index 000000000..19012f01f --- /dev/null +++ b/src/script/lua_api/l_localplayer.h @@ -0,0 +1,95 @@ +/* +Minetest +Copyright (C) 2017 Dumbeldor, Vincent Glize + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef MINETEST_L_LOCALPLAYER_H +#define MINETEST_L_LOCALPLAYER_H + +#include "l_base.h" + + +class LocalPlayer; + +class LuaLocalPlayer : public ModApiBase { +private: + + static const char className[]; + static const luaL_Reg methods[]; + + // garbage collector + static int gc_object(lua_State *L); + + static int l_get_velocity(lua_State *L); + + static int l_get_hp(lua_State *L); + + static int l_get_name(lua_State *L); + + static int l_is_teleported(lua_State *L); + static int l_is_attached(lua_State *L); + static int l_is_touching_ground(lua_State *L); + static int l_is_in_liquid(lua_State *L); + static int l_is_in_liquid_stable(lua_State *L); + static int l_get_liquid_viscosity(lua_State *L); + static int l_is_climbing(lua_State *L); + static int l_swimming_vertical(lua_State *L); + + static int l_get_physics_override(lua_State *L); + + static int l_get_override_pos(lua_State *L); + + static int l_get_last_pos(lua_State *L); + static int l_get_last_velocity(lua_State *L); + static int l_get_last_look_vertical(lua_State *L); + static int l_get_last_look_horizontal(lua_State *L); + static int l_get_key_pressed(lua_State *L); + + static int l_get_breath(lua_State *L); + + static int l_get_look_dir(lua_State *L); + static int l_get_look_horizontal(lua_State *L); + static int l_get_look_vertical(lua_State *L); + + static int l_get_pos(lua_State *L); + + static int l_get_eye_pos(lua_State *L); + static int l_get_eye_offset(lua_State *L); + + static int l_get_movement_acceleration(lua_State *L); + + static int l_get_movement_speed(lua_State *L); + + static int l_get_movement(lua_State *L); + + LocalPlayer *m_localplayer; + +public: + LuaLocalPlayer(LocalPlayer *m); + ~LuaLocalPlayer() {} + + static void create(lua_State *L, LocalPlayer *m); + + static LuaLocalPlayer *checkobject(lua_State *L, int narg); + static LocalPlayer *getobject(LuaLocalPlayer *ref); + static LocalPlayer *getobject(lua_State *L, int narg); + + static void Register(lua_State *L); +}; + + +#endif //MINETEST_L_LOCALPLAYER_H -- cgit v1.2.3 From 0419552c923d7088215cf711657d6223e1b8569a Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Mon, 10 Apr 2017 08:05:03 +0200 Subject: Clang format: fix LINT on old PR which doesn't have LINT enabled --- src/script/lua_api/l_localplayer.cpp | 29 ++++++++++------------------- src/script/lua_api/l_localplayer.h | 8 +++----- src/wieldmesh.h | 9 +++------ 3 files changed, 16 insertions(+), 30 deletions(-) (limited to 'src/script/lua_api/l_localplayer.h') diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index dc07d2445..87190cb39 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -17,19 +17,16 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "script/common/c_converter.h" #include "l_localplayer.h" #include "l_internal.h" +#include "script/common/c_converter.h" -LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) -{ - m_localplayer = m; -} +LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) { m_localplayer = m; } void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m) { LuaLocalPlayer *o = new LuaLocalPlayer(m); - *(void **) (lua_newuserdata(L, sizeof(void *))) = o; + *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); lua_setmetatable(L, -2); @@ -332,13 +329,10 @@ LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg) if (!ud) luaL_typerror(L, narg, className); - return *(LuaLocalPlayer **) ud; + return *(LuaLocalPlayer **)ud; } -LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref) -{ - return ref->m_localplayer; -} +LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref) { return ref->m_localplayer; } LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg) { @@ -351,7 +345,7 @@ LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg) int LuaLocalPlayer::gc_object(lua_State *L) { - LuaLocalPlayer *o = *(LuaLocalPlayer **) (lua_touserdata(L, 1)); + LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1)); delete o; return 0; } @@ -378,14 +372,12 @@ void LuaLocalPlayer::Register(lua_State *L) lua_pop(L, 1); // Drop metatable luaL_openlib(L, 0, methods, 0); // fill methodtable - lua_pop(L, 1); // Drop methodtable + lua_pop(L, 1); // Drop methodtable } const char LuaLocalPlayer::className[] = "LocalPlayer"; -const luaL_reg LuaLocalPlayer::methods[] = { - luamethod(LuaLocalPlayer, get_velocity), - luamethod(LuaLocalPlayer, get_hp), - luamethod(LuaLocalPlayer, get_name), +const luaL_reg LuaLocalPlayer::methods[] = {luamethod(LuaLocalPlayer, get_velocity), + luamethod(LuaLocalPlayer, get_hp), luamethod(LuaLocalPlayer, get_name), luamethod(LuaLocalPlayer, is_teleported), luamethod(LuaLocalPlayer, is_attached), luamethod(LuaLocalPlayer, is_touching_ground), @@ -412,5 +404,4 @@ const luaL_reg LuaLocalPlayer::methods[] = { luamethod(LuaLocalPlayer, get_movement_speed), luamethod(LuaLocalPlayer, get_movement), - {0, 0} -}; + {0, 0}}; diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h index 19012f01f..1070857c5 100644 --- a/src/script/lua_api/l_localplayer.h +++ b/src/script/lua_api/l_localplayer.h @@ -22,12 +22,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "l_base.h" - class LocalPlayer; -class LuaLocalPlayer : public ModApiBase { +class LuaLocalPlayer : public ModApiBase +{ private: - static const char className[]; static const luaL_Reg methods[]; @@ -91,5 +90,4 @@ public: static void Register(lua_State *L); }; - -#endif //MINETEST_L_LOCALPLAYER_H +#endif // MINETEST_L_LOCALPLAYER_H diff --git a/src/wieldmesh.h b/src/wieldmesh.h index 94edb1de6..dee18ca23 100644 --- a/src/wieldmesh.h +++ b/src/wieldmesh.h @@ -30,18 +30,15 @@ struct TileSpec; struct ItemMesh { - scene::IMesh* mesh; + scene::IMesh *mesh; /*! * Stores the color of each mesh buffer. * If the boolean is true, the color is fixed, else * palettes can modify it. */ - std::vector > buffer_colors; + std::vector> buffer_colors; - ItemMesh(): - mesh(NULL), - buffer_colors() - {} + ItemMesh() : mesh(NULL), buffer_colors() {} }; /* -- cgit v1.2.3 From f9fdb48dc85e53253ef93972355d3c7bc9d0ffb0 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sat, 1 Apr 2017 20:38:14 +0200 Subject: Sneak: Improve and fix various things Remove useless `got_teleported`. Fix jitter when walking against the sneak limits. Fix damage evading on sneak ladders. --- doc/client_lua_api.md | 2 -- src/localplayer.cpp | 48 +++++++++++++++++++++++------------- src/localplayer.h | 7 ++++-- src/network/clientpackethandler.cpp | 1 - src/script/lua_api/l_localplayer.cpp | 9 ------- src/script/lua_api/l_localplayer.h | 1 - 6 files changed, 36 insertions(+), 32 deletions(-) (limited to 'src/script/lua_api/l_localplayer.h') diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index d435c4aae..3e81818ae 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -810,8 +810,6 @@ Methods: * returns player HP * `get_name()` * returns player name -* `got_teleported()` - * returns true if player was teleported * `is_attached()` * returns true if player is attached * `is_touching_ground()` diff --git a/src/localplayer.cpp b/src/localplayer.cpp index ab44f155f..9e30aeac8 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -35,7 +35,6 @@ LocalPlayer::LocalPlayer(Client *client, const char *name): Player(name, client->idef()), parent(0), hp(PLAYER_MAX_HP), - got_teleported(false), isAttached(false), touching_ground(false), in_liquid(false), @@ -305,29 +304,43 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d, if (control.sneak && m_sneak_node_exists && !(fly_allowed && g_settings->getBool("free_move")) && !in_liquid && !is_climbing && - physics_override_sneak && !got_teleported) { - v3f sn_f = intToFloat(m_sneak_node, BS); - const v3f bmin = m_sneak_node_bb_top.MinEdge; - const v3f bmax = m_sneak_node_bb_top.MaxEdge; + physics_override_sneak) { + const v3f sn_f = intToFloat(m_sneak_node, BS); + const v3f bmin = sn_f + m_sneak_node_bb_top.MinEdge; + const v3f bmax = sn_f + m_sneak_node_bb_top.MaxEdge; + const v3f old_pos = position; + const v3f old_speed = m_speed; position.X = rangelim(position.X, - sn_f.X+bmin.X - sneak_max.X, sn_f.X+bmax.X + sneak_max.X); + bmin.X - sneak_max.X, bmax.X + sneak_max.X); position.Z = rangelim(position.Z, - sn_f.Z+bmin.Z - sneak_max.Z, sn_f.Z+bmax.Z + sneak_max.Z); + bmin.Z - sneak_max.Z, bmax.Z + sneak_max.Z); + + if (position.X != old_pos.X) + m_speed.X = 0; + if (position.Z != old_pos.Z) + m_speed.Z = 0; // Because we keep the player collision box on the node, limiting // position.Y is not necessary but useful to prevent players from // being inside a node if sneaking on e.g. the lower part of a stair if (!m_sneak_ladder_detected) { - position.Y = MYMAX(position.Y, sn_f.Y+bmax.Y); + position.Y = MYMAX(position.Y, bmax.Y); } else { // legacy behaviour that sometimes causes some weird slow sinking m_speed.Y = MYMAX(m_speed.Y, 0); } - } - if (got_teleported) - got_teleported = false; + if (collision_info != NULL && + m_speed.Y - old_speed.Y > BS) { + // Collide with sneak node, report fall damage + CollisionInfo sn_info; + sn_info.node_p = m_sneak_node; + sn_info.old_speed = old_speed; + sn_info.new_speed = m_speed; + collision_info->push_back(sn_info); + } + } // TODO: this shouldn't be hardcoded but transmitted from server float player_stepheight = touching_ground ? (BS*0.6) : (BS*0.2); @@ -449,9 +462,11 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d, m_ledge_detected = detectLedge(map, nodemgr, floatToInt(position, BS)); /* - Set new position + Set new position but keep sneak node set */ + bool sneak_node_exists = m_sneak_node_exists; setPosition(position); + m_sneak_node_exists = sneak_node_exists; /* Report collisions @@ -917,7 +932,7 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d, */ if (control.sneak && m_sneak_node_exists && !(fly_allowed && g_settings->getBool("free_move")) && !in_liquid && - physics_override_sneak && !got_teleported) { + physics_override_sneak) { f32 maxd = 0.5 * BS + sneak_max; v3f lwn_f = intToFloat(m_sneak_node, BS); position.X = rangelim(position.X, lwn_f.X - maxd, lwn_f.X + maxd); @@ -938,9 +953,6 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d, } } - if (got_teleported) - got_teleported = false; - // this shouldn't be hardcoded but transmitted from server float player_stepheight = touching_ground ? (BS * 0.6) : (BS * 0.2); @@ -1055,9 +1067,11 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d, } /* - Set new position + Set new position but keep sneak node set */ + bool sneak_node_exists = m_sneak_node_exists; setPosition(position); + m_sneak_node_exists = sneak_node_exists; /* Report collisions diff --git a/src/localplayer.h b/src/localplayer.h index d8d2f310b..dcfefcb1a 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -47,7 +47,6 @@ public: ClientActiveObject *parent; u16 hp; - bool got_teleported; bool isAttached; bool touching_ground; // This oscillates so that the player jumps a bit above the surface @@ -126,7 +125,11 @@ public: f32 getPitch() const { return m_pitch; } - void setPosition(const v3f &position) { m_position = position; } + inline void setPosition(const v3f &position) + { + m_position = position; + m_sneak_node_exists = false; + } v3f getPosition() const { return m_position; } v3f getEyePosition() const { return m_position + getEyeOffset(); } diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index a895acc84..4316a77d4 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -561,7 +561,6 @@ void Client::handleCommand_MovePlayer(NetworkPacket* pkt) *pkt >> pos >> pitch >> yaw; - player->got_teleported = true; player->setPosition(pos); infostream << "Client got TOCLIENT_MOVE_PLAYER" diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index 4b44bb709..177df55f3 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -68,14 +68,6 @@ int LuaLocalPlayer::l_get_name(lua_State *L) return 1; } -int LuaLocalPlayer::l_is_teleported(lua_State *L) -{ - LocalPlayer *player = getobject(L, 1); - - lua_pushboolean(L, player->got_teleported); - return 1; -} - int LuaLocalPlayer::l_is_attached(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -386,7 +378,6 @@ const luaL_Reg LuaLocalPlayer::methods[] = { luamethod(LuaLocalPlayer, get_velocity), luamethod(LuaLocalPlayer, get_hp), luamethod(LuaLocalPlayer, get_name), - luamethod(LuaLocalPlayer, is_teleported), luamethod(LuaLocalPlayer, is_attached), luamethod(LuaLocalPlayer, is_touching_ground), luamethod(LuaLocalPlayer, is_in_liquid), diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h index 1070857c5..6ec3f4c09 100644 --- a/src/script/lua_api/l_localplayer.h +++ b/src/script/lua_api/l_localplayer.h @@ -39,7 +39,6 @@ private: static int l_get_name(lua_State *L); - static int l_is_teleported(lua_State *L); static int l_is_attached(lua_State *L); static int l_is_touching_ground(lua_State *L); static int l_is_in_liquid(lua_State *L); -- cgit v1.2.3 From de028fc056b26e03ee00324888f870f64e28c756 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Fri, 5 May 2017 15:07:36 -0500 Subject: [CSM] Add camera API (#5609) * [CSM] Add camera API roper rebase & squash * Address nerzhul's review --- build/android/jni/Android.mk | 1 + doc/client_lua_api.md | 52 ++++++--- src/camera.cpp | 5 + src/camera.h | 8 +- src/script/lua_api/CMakeLists.txt | 1 + src/script/lua_api/l_camera.cpp | 206 +++++++++++++++++++++++++++++++++++ src/script/lua_api/l_camera.h | 46 ++++++++ src/script/lua_api/l_localplayer.cpp | 49 --------- src/script/lua_api/l_localplayer.h | 7 -- src/script/scripting_client.cpp | 7 ++ src/script/scripting_client.h | 2 + 11 files changed, 314 insertions(+), 70 deletions(-) create mode 100644 src/script/lua_api/l_camera.cpp create mode 100644 src/script/lua_api/l_camera.h (limited to 'src/script/lua_api/l_localplayer.h') diff --git a/build/android/jni/Android.mk b/build/android/jni/Android.mk index 6525942bd..d4a8d988f 100644 --- a/build/android/jni/Android.mk +++ b/build/android/jni/Android.mk @@ -305,6 +305,7 @@ LOCAL_SRC_FILES += \ jni/src/script/cpp_api/s_server.cpp \ jni/src/script/lua_api/l_areastore.cpp \ jni/src/script/lua_api/l_base.cpp \ + jni/src/script/lua_api/l_camera.cpp \ jni/src/script/lua_api/l_client.cpp \ jni/src/script/lua_api/l_craft.cpp \ jni/src/script/lua_api/l_env.cpp \ diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index b76a406ea..a4293500f 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -694,7 +694,7 @@ Call these functions only at load time! * `minetest.get_wielded_item()` * Returns the itemstack the local player is holding * `minetest.localplayer` - * Reference to the LocalPlayer object. See `LocalPlayer` class reference for methods. + * Reference to the LocalPlayer object. See [`LocalPlayer`](#localplayer) class reference for methods. ### Client Environment * `minetest.get_player_names()` @@ -750,7 +750,7 @@ Call these functions only at load time! * Encodes a string in base64. * `minetest.decode_base64(string)`: returns string * Decodes a string encoded in base64. -* `minetest.gettext(string) : returns string +* `minetest.gettext(string)` : returns string * look up the translation of a string in the gettext message catalog * `fgettext_ne(string, ...)` * call minetest.gettext(string), replace "$1"..."$9" with the given @@ -762,7 +762,9 @@ Call these functions only at load time! ### UI * `minetest.ui.minimap` - * Reference to the minimap object. See `Minimap` class reference for methods. + * Reference to the minimap object. See [`Minimap`](#minimap) class reference for methods. +* `minetest.camera` + * Reference to the camera object. See [`Camera`](#camera) class reference for methods. * `minetest.show_formspec(formname, formspec)` : returns true on success * Shows a formspec to the player * `minetest.display_chat_message(message)` returns true on success @@ -785,6 +787,40 @@ An interface to manipulate minimap on client UI * `set_shape(shape)`: Sets the minimap shape. (0 = square, 1 = round) * `get_shape()`: Gets the minimap shape. (0 = square, 1 = round) +### Camera +An interface to get or set information about the camera and cameranode. +Please do not try to access the reference until the camera is initialized, otherwise the reference will be nil. + +#### Methods +* `set_camera_mode(mode)` + * Pass `0` for first-person, `1` for third person, and `2` for third person front +* `get_camera_mode()` + * Returns with same syntax as above +* `get_fov()` + * Returns: + +```lua + { + x = number, + y = number, + max = number, + actual = number + } +``` + +* `get_pos()` + * Returns position of camera with view bobbing +* `get_offset()` + * Returns eye offset vector +* `get_look_dir()` + * Returns eye direction unit vector +* `get_look_vertical()` + * Returns pitch in radians +* `get_look_horizontal()` + * Returns yaw in radians +* `get_aspect_ratio()` + * Returns aspect ratio of screen + ### LocalPlayer An interface to retrieve information about the player. The player is not accessible until the client is fully done loading and therefore @@ -844,16 +880,6 @@ Methods: * returns last player speed * `get_breath()` * returns the player's breath -* `get_look_dir()` - * returns look direction vector -* `get_look_horizontal()` - * returns look horizontal angle -* `get_look_vertical()` - * returns look vertical angle -* `get_eye_pos()` - * returns the player's eye position -* `get_eye_offset()` - * returns the player's eye shift vector * `get_movement_acceleration()` * returns acceleration of the player in different environments: diff --git a/src/camera.cpp b/src/camera.cpp index b119bbfbb..949494b00 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "constants.h" #include "fontengine.h" +#include "script/scripting_client.h" #define CAMERA_OFFSET_STEP 200 @@ -125,6 +126,10 @@ bool Camera::successfullyCreated(std::string &error_message) } else { error_message.clear(); } + + if (g_settings->getBool("enable_client_modding")) { + m_client->getScript()->on_camera_ready(this); + } return error_message.empty(); } diff --git a/src/camera.h b/src/camera.h index e4c1d0b25..ca2e4ddcc 100644 --- a/src/camera.h +++ b/src/camera.h @@ -160,7 +160,13 @@ public: else m_camera_mode = CAMERA_MODE_FIRST; } - + + // Set the current camera mode + inline void setCameraMode(CameraMode mode) + { + m_camera_mode = mode; + } + //read the current camera mode inline CameraMode getCameraMode() { diff --git a/src/script/lua_api/CMakeLists.txt b/src/script/lua_api/CMakeLists.txt index b03e94a09..1a78580e6 100644 --- a/src/script/lua_api/CMakeLists.txt +++ b/src/script/lua_api/CMakeLists.txt @@ -29,4 +29,5 @@ set(client_SCRIPT_LUA_API_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/l_storage.cpp ${CMAKE_CURRENT_SOURCE_DIR}/l_sound.cpp ${CMAKE_CURRENT_SOURCE_DIR}/l_localplayer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/l_camera.cpp PARENT_SCOPE) diff --git a/src/script/lua_api/l_camera.cpp b/src/script/lua_api/l_camera.cpp new file mode 100644 index 000000000..e6229ffe8 --- /dev/null +++ b/src/script/lua_api/l_camera.cpp @@ -0,0 +1,206 @@ +#include "script/common/c_converter.h" +#include "l_camera.h" +#include "l_internal.h" +#include "content_cao.h" +#include "camera.h" + +LuaCamera::LuaCamera(Camera *m) +{ + m_camera = m; +} + +void LuaCamera::create(lua_State *L, Camera *m) +{ + LuaCamera *o = new LuaCamera(m); + *(void **) (lua_newuserdata(L, sizeof(void *))) = o; + luaL_getmetatable(L, className); + lua_setmetatable(L, -2); + + int camera_object = lua_gettop(L); + + lua_getglobal(L, "core"); + luaL_checktype(L, -1, LUA_TTABLE); + int coretable = lua_gettop(L); + + lua_pushvalue(L, camera_object); + lua_setfield(L, coretable, "camera"); +} + +int LuaCamera::l_set_camera_mode(lua_State *L) +{ + Camera *camera = getobject(L, 1); + GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO(); + if (!camera) + return 0; + sanity_check(playercao); + if (!lua_isnumber(L, 2)) + return 0; + + camera->setCameraMode((CameraMode)((int)lua_tonumber(L, 2))); + playercao->setVisible(camera->getCameraMode() > CAMERA_MODE_FIRST); + playercao->setChildrenVisible(camera->getCameraMode() > CAMERA_MODE_FIRST); + return 0; +} + +int LuaCamera::l_get_camera_mode(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + lua_pushnumber(L, (int)camera->getCameraMode()); + + return 1; +} + +int LuaCamera::l_get_fov(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + lua_newtable(L); + lua_pushnumber(L, camera->getFovX() * core::DEGTORAD); + lua_setfield(L, -2, "x"); + lua_pushnumber(L, camera->getFovY() * core::DEGTORAD); + lua_setfield(L, -2, "y"); + lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG); + lua_setfield(L, -2, "actual"); + lua_pushnumber(L, camera->getFovMax() * core::RADTODEG); + lua_setfield(L, -2, "max"); + return 1; +} + +int LuaCamera::l_get_pos(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + push_v3f(L, camera->getPosition()); + return 1; +} + +int LuaCamera::l_get_offset(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + push_v3s16(L, camera->getOffset()); + return 1; +} + +int LuaCamera::l_get_look_dir(lua_State *L) +{ + LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer(); + sanity_check(player); + + float pitch = -1.0 * player->getPitch() * core::DEGTORAD; + float yaw = (player->getYaw() + 90.) * core::DEGTORAD; + v3f v(cos(pitch) * cos(yaw), sin(pitch), cos(pitch) * sin(yaw)); + + push_v3f(L, v); + return 1; +} + +int LuaCamera::l_get_look_horizontal(lua_State *L) +{ + LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer(); + sanity_check(player); + + lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD); + return 1; +} + +int LuaCamera::l_get_look_vertical(lua_State *L) +{ + LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer(); + sanity_check(player); + + lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD); + return 1; +} + +int LuaCamera::l_get_aspect_ratio(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + lua_pushnumber(L, camera->getCameraNode()->getAspectRatio()); + return 1; +} + +LuaCamera *LuaCamera::checkobject(lua_State *L, int narg) +{ + luaL_checktype(L, narg, LUA_TUSERDATA); + + void *ud = luaL_checkudata(L, narg, className); + if (!ud) + luaL_typerror(L, narg, className); + + return *(LuaCamera **) ud; +} + +Camera *LuaCamera::getobject(LuaCamera *ref) +{ + return ref->m_camera; +} + +Camera *LuaCamera::getobject(lua_State *L, int narg) +{ + LuaCamera *ref = checkobject(L, narg); + assert(ref); + Camera *camera = getobject(ref); + if (!camera) + return NULL; + return camera; +} + +int LuaCamera::gc_object(lua_State *L) +{ + LuaCamera *o = *(LuaCamera **) (lua_touserdata(L, 1)); + delete o; + return 0; +} + +void LuaCamera::Register(lua_State *L) +{ + lua_newtable(L); + int methodtable = lua_gettop(L); + luaL_newmetatable(L, className); + int metatable = lua_gettop(L); + + lua_pushliteral(L, "__metatable"); + lua_pushvalue(L, methodtable); + lua_settable(L, metatable); + + lua_pushliteral(L, "__index"); + lua_pushvalue(L, methodtable); + lua_settable(L, metatable); + + lua_pushliteral(L, "__gc"); + lua_pushcfunction(L, gc_object); + lua_settable(L, metatable); + + lua_pop(L, 1); + + luaL_openlib(L, 0, methods, 0); + lua_pop(L, 1); +} + +const char LuaCamera::className[] = "Camera"; +const luaL_reg LuaCamera::methods[] = { + luamethod(LuaCamera, set_camera_mode), + luamethod(LuaCamera, get_camera_mode), + luamethod(LuaCamera, get_fov), + luamethod(LuaCamera, get_pos), + luamethod(LuaCamera, get_offset), + luamethod(LuaCamera, get_look_dir), + luamethod(LuaCamera, get_look_vertical), + luamethod(LuaCamera, get_look_horizontal), + luamethod(LuaCamera, get_aspect_ratio), + + {0, 0} +}; diff --git a/src/script/lua_api/l_camera.h b/src/script/lua_api/l_camera.h new file mode 100644 index 000000000..82ab6a47f --- /dev/null +++ b/src/script/lua_api/l_camera.h @@ -0,0 +1,46 @@ +#ifndef L_CAMERA_H +#define L_CAMERA_H + +#include "l_base.h" + +class Camera; + +class LuaCamera : public ModApiBase { +private: + + static const char className[]; + static const luaL_Reg methods[]; + + // garbage collector + static int gc_object(lua_State *L); + + static int l_set_camera_mode(lua_State *L); + static int l_get_camera_mode(lua_State *L); + + static int l_get_fov(lua_State *L); + + static int l_get_pos(lua_State *L); + static int l_get_offset(lua_State *L); + static int l_get_look_dir(lua_State *L); + static int l_get_look_vertical(lua_State *L); + static int l_get_look_horizontal(lua_State *L); + static int l_get_aspect_ratio(lua_State *L); + + Camera *m_camera; + +public: + + LuaCamera(Camera *m); + ~LuaCamera() {} + + static void create(lua_State *L, Camera *m); + + static LuaCamera *checkobject(lua_State *L, int narg); + static Camera *getobject(LuaCamera *ref); + static Camera *getobject(lua_State *L, int narg); + + static void Register(lua_State *L); + +}; + +#endif // L_CAMERA_H diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index 177df55f3..7ec4eaa62 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -203,34 +203,6 @@ int LuaLocalPlayer::l_get_breath(lua_State *L) return 1; } -int LuaLocalPlayer::l_get_look_dir(lua_State *L) -{ - LocalPlayer *player = getobject(L, 1); - - float pitch = -1.0 * player->getPitch() * core::DEGTORAD; - float yaw = (player->getYaw() + 90.) * core::DEGTORAD; - v3f v(cos(pitch) * cos(yaw), sin(pitch), cos(pitch) * sin(yaw)); - - push_v3f(L, v); - return 1; -} - -int LuaLocalPlayer::l_get_look_horizontal(lua_State *L) -{ - LocalPlayer *player = getobject(L, 1); - - lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD); - return 1; -} - -int LuaLocalPlayer::l_get_look_vertical(lua_State *L) -{ - LocalPlayer *player = getobject(L, 1); - - lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD); - return 1; -} - int LuaLocalPlayer::l_get_pos(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -239,22 +211,6 @@ int LuaLocalPlayer::l_get_pos(lua_State *L) return 1; } -int LuaLocalPlayer::l_get_eye_pos(lua_State *L) -{ - LocalPlayer *player = getobject(L, 1); - - push_v3f(L, player->getEyePosition()); - return 1; -} - -int LuaLocalPlayer::l_get_eye_offset(lua_State *L) -{ - LocalPlayer *player = getobject(L, 1); - - push_v3f(L, player->getEyeOffset()); - return 1; -} - int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -393,12 +349,7 @@ const luaL_Reg LuaLocalPlayer::methods[] = { luamethod(LuaLocalPlayer, get_last_look_vertical), luamethod(LuaLocalPlayer, get_key_pressed), luamethod(LuaLocalPlayer, get_breath), - luamethod(LuaLocalPlayer, get_look_dir), - luamethod(LuaLocalPlayer, get_look_horizontal), - luamethod(LuaLocalPlayer, get_look_vertical), luamethod(LuaLocalPlayer, get_pos), - luamethod(LuaLocalPlayer, get_eye_pos), - luamethod(LuaLocalPlayer, get_eye_offset), luamethod(LuaLocalPlayer, get_movement_acceleration), luamethod(LuaLocalPlayer, get_movement_speed), luamethod(LuaLocalPlayer, get_movement), diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h index 6ec3f4c09..e56ec808f 100644 --- a/src/script/lua_api/l_localplayer.h +++ b/src/script/lua_api/l_localplayer.h @@ -59,15 +59,8 @@ private: static int l_get_breath(lua_State *L); - static int l_get_look_dir(lua_State *L); - static int l_get_look_horizontal(lua_State *L); - static int l_get_look_vertical(lua_State *L); - static int l_get_pos(lua_State *L); - static int l_get_eye_pos(lua_State *L); - static int l_get_eye_offset(lua_State *L); - static int l_get_movement_acceleration(lua_State *L); static int l_get_movement_speed(lua_State *L); diff --git a/src/script/scripting_client.cpp b/src/script/scripting_client.cpp index 8ff5abcc4..b5a5085be 100644 --- a/src/script/scripting_client.cpp +++ b/src/script/scripting_client.cpp @@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_item.h" #include "lua_api/l_nodemeta.h" #include "lua_api/l_localplayer.h" +#include "lua_api/l_camera.h" ClientScripting::ClientScripting(Client *client): ScriptApiBase() @@ -71,6 +72,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top) LuaMinimap::Register(L); NodeMetaRef::RegisterClient(L); LuaLocalPlayer::Register(L); + LuaCamera::Register(L); } void ClientScripting::on_client_ready(LocalPlayer *localplayer) @@ -78,3 +80,8 @@ void ClientScripting::on_client_ready(LocalPlayer *localplayer) lua_State *L = getStack(); LuaLocalPlayer::create(L, localplayer); } + +void ClientScripting::on_camera_ready(Camera *camera) +{ + LuaCamera::create(getStack(), camera); +} diff --git a/src/script/scripting_client.h b/src/script/scripting_client.h index b3451e409..c13fde607 100644 --- a/src/script/scripting_client.h +++ b/src/script/scripting_client.h @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., class Client; class LocalPlayer; +class Camera; class ClientScripting: virtual public ScriptApiBase, public ScriptApiSecurity, @@ -36,6 +37,7 @@ class ClientScripting: public: ClientScripting(Client *client); void on_client_ready(LocalPlayer *localplayer); + void on_camera_ready(Camera *camera); private: virtual void InitializeModApi(lua_State *L, int top); -- cgit v1.2.3