1 #include "script/common/c_converter.h"
3 #include "l_internal.h"
4 #include "content_cao.h"
7 LuaCamera::LuaCamera(Camera *m)
12 void LuaCamera::create(lua_State *L, Camera *m)
14 LuaCamera *o = new LuaCamera(m);
15 *(void **) (lua_newuserdata(L, sizeof(void *))) = o;
16 luaL_getmetatable(L, className);
17 lua_setmetatable(L, -2);
19 int camera_object = lua_gettop(L);
21 lua_getglobal(L, "core");
22 luaL_checktype(L, -1, LUA_TTABLE);
23 int coretable = lua_gettop(L);
25 lua_pushvalue(L, camera_object);
26 lua_setfield(L, coretable, "camera");
29 int LuaCamera::l_set_camera_mode(lua_State *L)
31 Camera *camera = getobject(L, 1);
32 GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO();
35 sanity_check(playercao);
36 if (!lua_isnumber(L, 2))
39 camera->setCameraMode((CameraMode)((int)lua_tonumber(L, 2)));
40 playercao->setVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
41 playercao->setChildrenVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
45 int LuaCamera::l_get_camera_mode(lua_State *L)
47 Camera *camera = getobject(L, 1);
51 lua_pushnumber(L, (int)camera->getCameraMode());
56 int LuaCamera::l_get_fov(lua_State *L)
58 Camera *camera = getobject(L, 1);
63 lua_pushnumber(L, camera->getFovX() * core::DEGTORAD);
64 lua_setfield(L, -2, "x");
65 lua_pushnumber(L, camera->getFovY() * core::DEGTORAD);
66 lua_setfield(L, -2, "y");
67 lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG);
68 lua_setfield(L, -2, "actual");
69 lua_pushnumber(L, camera->getFovMax() * core::RADTODEG);
70 lua_setfield(L, -2, "max");
74 int LuaCamera::l_get_pos(lua_State *L)
76 Camera *camera = getobject(L, 1);
80 push_v3f(L, camera->getPosition());
84 int LuaCamera::l_get_offset(lua_State *L)
86 Camera *camera = getobject(L, 1);
90 push_v3s16(L, camera->getOffset());
94 int LuaCamera::l_get_look_dir(lua_State *L)
96 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
99 float pitch = -1.0 * player->getPitch() * core::DEGTORAD;
100 float yaw = (player->getYaw() + 90.) * core::DEGTORAD;
101 v3f v(cos(pitch) * cos(yaw), sin(pitch), cos(pitch) * sin(yaw));
107 int LuaCamera::l_get_look_horizontal(lua_State *L)
109 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
110 sanity_check(player);
112 lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
116 int LuaCamera::l_get_look_vertical(lua_State *L)
118 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
119 sanity_check(player);
121 lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
125 int LuaCamera::l_get_aspect_ratio(lua_State *L)
127 Camera *camera = getobject(L, 1);
131 lua_pushnumber(L, camera->getCameraNode()->getAspectRatio());
135 LuaCamera *LuaCamera::checkobject(lua_State *L, int narg)
137 luaL_checktype(L, narg, LUA_TUSERDATA);
139 void *ud = luaL_checkudata(L, narg, className);
141 luaL_typerror(L, narg, className);
143 return *(LuaCamera **) ud;
146 Camera *LuaCamera::getobject(LuaCamera *ref)
148 return ref->m_camera;
151 Camera *LuaCamera::getobject(lua_State *L, int narg)
153 LuaCamera *ref = checkobject(L, narg);
155 Camera *camera = getobject(ref);
161 int LuaCamera::gc_object(lua_State *L)
163 LuaCamera *o = *(LuaCamera **) (lua_touserdata(L, 1));
168 void LuaCamera::Register(lua_State *L)
171 int methodtable = lua_gettop(L);
172 luaL_newmetatable(L, className);
173 int metatable = lua_gettop(L);
175 lua_pushliteral(L, "__metatable");
176 lua_pushvalue(L, methodtable);
177 lua_settable(L, metatable);
179 lua_pushliteral(L, "__index");
180 lua_pushvalue(L, methodtable);
181 lua_settable(L, metatable);
183 lua_pushliteral(L, "__gc");
184 lua_pushcfunction(L, gc_object);
185 lua_settable(L, metatable);
189 luaL_openlib(L, 0, methods, 0);
193 const char LuaCamera::className[] = "Camera";
194 const luaL_reg LuaCamera::methods[] = {
195 luamethod(LuaCamera, set_camera_mode),
196 luamethod(LuaCamera, get_camera_mode),
197 luamethod(LuaCamera, get_fov),
198 luamethod(LuaCamera, get_pos),
199 luamethod(LuaCamera, get_offset),
200 luamethod(LuaCamera, get_look_dir),
201 luamethod(LuaCamera, get_look_vertical),
202 luamethod(LuaCamera, get_look_horizontal),
203 luamethod(LuaCamera, get_aspect_ratio),