3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "script/common/c_converter.h"
23 #include "l_internal.h"
24 #include "content_cao.h"
28 LuaCamera::LuaCamera(Camera *m) : m_camera(m)
32 void LuaCamera::create(lua_State *L, Camera *m)
34 LuaCamera *o = new LuaCamera(m);
35 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
36 luaL_getmetatable(L, className);
37 lua_setmetatable(L, -2);
39 int camera_object = lua_gettop(L);
41 lua_getglobal(L, "core");
42 luaL_checktype(L, -1, LUA_TTABLE);
43 int coretable = lua_gettop(L);
45 lua_pushvalue(L, camera_object);
46 lua_setfield(L, coretable, "camera");
49 int LuaCamera::l_set_camera_mode(lua_State *L)
51 Camera *camera = getobject(L, 1);
52 GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO();
55 sanity_check(playercao);
56 if (!lua_isnumber(L, 2))
59 camera->setCameraMode((CameraMode)((int)lua_tonumber(L, 2)));
60 playercao->setVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
61 playercao->setChildrenVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
65 int LuaCamera::l_get_camera_mode(lua_State *L)
67 Camera *camera = getobject(L, 1);
71 lua_pushnumber(L, (int)camera->getCameraMode());
76 int LuaCamera::l_get_fov(lua_State *L)
78 Camera *camera = getobject(L, 1);
83 lua_pushnumber(L, camera->getFovX() * core::DEGTORAD);
84 lua_setfield(L, -2, "x");
85 lua_pushnumber(L, camera->getFovY() * core::DEGTORAD);
86 lua_setfield(L, -2, "y");
87 lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG);
88 lua_setfield(L, -2, "actual");
89 lua_pushnumber(L, camera->getFovMax() * core::RADTODEG);
90 lua_setfield(L, -2, "max");
94 int LuaCamera::l_get_pos(lua_State *L)
96 Camera *camera = getobject(L, 1);
100 push_v3f(L, camera->getPosition());
104 int LuaCamera::l_get_offset(lua_State *L)
106 Camera *camera = getobject(L, 1);
110 push_v3s16(L, camera->getOffset());
114 int LuaCamera::l_get_look_dir(lua_State *L)
116 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
117 sanity_check(player);
119 float pitch = -1.0 * player->getPitch() * core::DEGTORAD;
120 float yaw = (player->getYaw() + 90.) * core::DEGTORAD;
121 v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch),
122 std::cos(pitch) * std::sin(yaw));
128 int LuaCamera::l_get_look_horizontal(lua_State *L)
130 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
131 sanity_check(player);
133 lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
137 int LuaCamera::l_get_look_vertical(lua_State *L)
139 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
140 sanity_check(player);
142 lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
146 int LuaCamera::l_get_aspect_ratio(lua_State *L)
148 Camera *camera = getobject(L, 1);
152 lua_pushnumber(L, camera->getCameraNode()->getAspectRatio());
156 LuaCamera *LuaCamera::checkobject(lua_State *L, int narg)
158 luaL_checktype(L, narg, LUA_TUSERDATA);
160 void *ud = luaL_checkudata(L, narg, className);
162 luaL_typerror(L, narg, className);
164 return *(LuaCamera **)ud;
167 Camera *LuaCamera::getobject(LuaCamera *ref)
169 return ref->m_camera;
172 Camera *LuaCamera::getobject(lua_State *L, int narg)
174 LuaCamera *ref = checkobject(L, narg);
176 Camera *camera = getobject(ref);
182 int LuaCamera::gc_object(lua_State *L)
184 LuaCamera *o = *(LuaCamera **)(lua_touserdata(L, 1));
189 void LuaCamera::Register(lua_State *L)
192 int methodtable = lua_gettop(L);
193 luaL_newmetatable(L, className);
194 int metatable = lua_gettop(L);
196 lua_pushliteral(L, "__metatable");
197 lua_pushvalue(L, methodtable);
198 lua_settable(L, metatable);
200 lua_pushliteral(L, "__index");
201 lua_pushvalue(L, methodtable);
202 lua_settable(L, metatable);
204 lua_pushliteral(L, "__gc");
205 lua_pushcfunction(L, gc_object);
206 lua_settable(L, metatable);
210 luaL_openlib(L, 0, methods, 0);
214 const char LuaCamera::className[] = "Camera";
215 const luaL_Reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode),
216 luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov),
217 luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset),
218 luamethod(LuaCamera, get_look_dir),
219 luamethod(LuaCamera, get_look_vertical),
220 luamethod(LuaCamera, get_look_horizontal),
221 luamethod(LuaCamera, get_aspect_ratio),