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 "client/content_cao.h"
25 #include "client/camera.h"
26 #include "client/client.h"
28 LuaCamera::LuaCamera(Camera *m) : m_camera(m)
32 void LuaCamera::create(lua_State *L, Camera *m)
34 lua_getglobal(L, "core");
35 luaL_checktype(L, -1, LUA_TTABLE);
36 int objectstable = lua_gettop(L);
37 lua_getfield(L, -1, "camera");
40 if (lua_type(L, -1) == LUA_TUSERDATA) {
45 LuaCamera *o = new LuaCamera(m);
46 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
47 luaL_getmetatable(L, className);
48 lua_setmetatable(L, -2);
50 lua_pushvalue(L, lua_gettop(L));
51 lua_setfield(L, objectstable, "camera");
54 // set_camera_mode(self, mode)
55 int LuaCamera::l_set_camera_mode(lua_State *L)
57 Camera *camera = getobject(L, 1);
58 GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO();
61 sanity_check(playercao);
62 if (!lua_isnumber(L, 2))
65 camera->setCameraMode((CameraMode)((int)lua_tonumber(L, 2)));
66 // Make the player visible depending on camera mode.
67 playercao->updateMeshCulling();
68 playercao->setChildrenVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
72 // get_camera_mode(self)
73 int LuaCamera::l_get_camera_mode(lua_State *L)
75 Camera *camera = getobject(L, 1);
79 lua_pushinteger(L, (int)camera->getCameraMode());
85 int LuaCamera::l_get_fov(lua_State *L)
87 Camera *camera = getobject(L, 1);
92 lua_pushnumber(L, camera->getFovX() * core::RADTODEG);
93 lua_setfield(L, -2, "x");
94 lua_pushnumber(L, camera->getFovY() * core::RADTODEG);
95 lua_setfield(L, -2, "y");
96 lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG);
97 lua_setfield(L, -2, "actual");
98 lua_pushnumber(L, camera->getFovMax() * core::RADTODEG);
99 lua_setfield(L, -2, "max");
104 int LuaCamera::l_get_pos(lua_State *L)
106 Camera *camera = getobject(L, 1);
110 push_v3f(L, camera->getPosition() / BS);
115 int LuaCamera::l_get_offset(lua_State *L)
117 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
118 sanity_check(player);
120 push_v3f(L, player->getEyeOffset() / BS);
124 // get_look_dir(self)
125 int LuaCamera::l_get_look_dir(lua_State *L)
127 Camera *camera = getobject(L, 1);
131 push_v3f(L, camera->getDirection());
135 // get_look_horizontal(self)
136 // FIXME: wouldn't localplayer be a better place for this?
137 int LuaCamera::l_get_look_horizontal(lua_State *L)
139 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
140 sanity_check(player);
142 lua_pushnumber(L, (player->getYaw() + 90.f) * core::DEGTORAD);
146 // get_look_vertical(self)
147 // FIXME: wouldn't localplayer be a better place for this?
148 int LuaCamera::l_get_look_vertical(lua_State *L)
150 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
151 sanity_check(player);
153 lua_pushnumber(L, -1.0f * player->getPitch() * core::DEGTORAD);
157 // get_aspect_ratio(self)
158 int LuaCamera::l_get_aspect_ratio(lua_State *L)
160 Camera *camera = getobject(L, 1);
164 lua_pushnumber(L, camera->getCameraNode()->getAspectRatio());
168 LuaCamera *LuaCamera::checkobject(lua_State *L, int narg)
170 luaL_checktype(L, narg, LUA_TUSERDATA);
172 void *ud = luaL_checkudata(L, narg, className);
174 luaL_typerror(L, narg, className);
176 return *(LuaCamera **)ud;
179 Camera *LuaCamera::getobject(LuaCamera *ref)
181 return ref->m_camera;
184 Camera *LuaCamera::getobject(lua_State *L, int narg)
186 LuaCamera *ref = checkobject(L, narg);
188 Camera *camera = getobject(ref);
194 int LuaCamera::gc_object(lua_State *L)
196 LuaCamera *o = *(LuaCamera **)(lua_touserdata(L, 1));
201 void LuaCamera::Register(lua_State *L)
204 int methodtable = lua_gettop(L);
205 luaL_newmetatable(L, className);
206 int metatable = lua_gettop(L);
208 lua_pushliteral(L, "__metatable");
209 lua_pushvalue(L, methodtable);
210 lua_settable(L, metatable);
212 lua_pushliteral(L, "__index");
213 lua_pushvalue(L, methodtable);
214 lua_settable(L, metatable);
216 lua_pushliteral(L, "__gc");
217 lua_pushcfunction(L, gc_object);
218 lua_settable(L, metatable);
222 luaL_openlib(L, 0, methods, 0);
227 const char LuaCamera::className[] = "Camera";
228 const luaL_Reg LuaCamera::methods[] = {
229 luamethod(LuaCamera, set_camera_mode),
230 luamethod(LuaCamera, get_camera_mode),
231 luamethod(LuaCamera, get_fov),
232 luamethod(LuaCamera, get_pos),
233 luamethod(LuaCamera, get_offset),
234 luamethod(LuaCamera, get_look_dir),
235 luamethod(LuaCamera, get_look_vertical),
236 luamethod(LuaCamera, get_look_horizontal),
237 luamethod(LuaCamera, get_aspect_ratio),