1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "l_camera.h"
21 #include <cmath>
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"
27
LuaCamera(Camera * m)28 LuaCamera::LuaCamera(Camera *m) : m_camera(m)
29 {
30 }
31
create(lua_State * L,Camera * m)32 void LuaCamera::create(lua_State *L, Camera *m)
33 {
34 lua_getglobal(L, "core");
35 luaL_checktype(L, -1, LUA_TTABLE);
36 int objectstable = lua_gettop(L);
37 lua_getfield(L, -1, "camera");
38
39 // Duplication check
40 if (lua_type(L, -1) == LUA_TUSERDATA) {
41 lua_pop(L, 1);
42 return;
43 }
44
45 LuaCamera *o = new LuaCamera(m);
46 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
47 luaL_getmetatable(L, className);
48 lua_setmetatable(L, -2);
49
50 lua_pushvalue(L, lua_gettop(L));
51 lua_setfield(L, objectstable, "camera");
52 }
53
54 // set_camera_mode(self, mode)
l_set_camera_mode(lua_State * L)55 int LuaCamera::l_set_camera_mode(lua_State *L)
56 {
57 Camera *camera = getobject(L, 1);
58 GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO();
59 if (!camera)
60 return 0;
61 sanity_check(playercao);
62 if (!lua_isnumber(L, 2))
63 return 0;
64
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);
69 return 0;
70 }
71
72 // get_camera_mode(self)
l_get_camera_mode(lua_State * L)73 int LuaCamera::l_get_camera_mode(lua_State *L)
74 {
75 Camera *camera = getobject(L, 1);
76 if (!camera)
77 return 0;
78
79 lua_pushinteger(L, (int)camera->getCameraMode());
80
81 return 1;
82 }
83
84 // get_fov(self)
l_get_fov(lua_State * L)85 int LuaCamera::l_get_fov(lua_State *L)
86 {
87 Camera *camera = getobject(L, 1);
88 if (!camera)
89 return 0;
90
91 lua_newtable(L);
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");
100 return 1;
101 }
102
103 // get_pos(self)
l_get_pos(lua_State * L)104 int LuaCamera::l_get_pos(lua_State *L)
105 {
106 Camera *camera = getobject(L, 1);
107 if (!camera)
108 return 0;
109
110 push_v3f(L, camera->getPosition() / BS);
111 return 1;
112 }
113
114 // get_offset(self)
l_get_offset(lua_State * L)115 int LuaCamera::l_get_offset(lua_State *L)
116 {
117 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
118 sanity_check(player);
119
120 push_v3f(L, player->getEyeOffset() / BS);
121 return 1;
122 }
123
124 // get_look_dir(self)
l_get_look_dir(lua_State * L)125 int LuaCamera::l_get_look_dir(lua_State *L)
126 {
127 Camera *camera = getobject(L, 1);
128 if (!camera)
129 return 0;
130
131 push_v3f(L, camera->getDirection());
132 return 1;
133 }
134
135 // get_look_horizontal(self)
136 // FIXME: wouldn't localplayer be a better place for this?
l_get_look_horizontal(lua_State * L)137 int LuaCamera::l_get_look_horizontal(lua_State *L)
138 {
139 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
140 sanity_check(player);
141
142 lua_pushnumber(L, (player->getYaw() + 90.f) * core::DEGTORAD);
143 return 1;
144 }
145
146 // get_look_vertical(self)
147 // FIXME: wouldn't localplayer be a better place for this?
l_get_look_vertical(lua_State * L)148 int LuaCamera::l_get_look_vertical(lua_State *L)
149 {
150 LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
151 sanity_check(player);
152
153 lua_pushnumber(L, -1.0f * player->getPitch() * core::DEGTORAD);
154 return 1;
155 }
156
157 // get_aspect_ratio(self)
l_get_aspect_ratio(lua_State * L)158 int LuaCamera::l_get_aspect_ratio(lua_State *L)
159 {
160 Camera *camera = getobject(L, 1);
161 if (!camera)
162 return 0;
163
164 lua_pushnumber(L, camera->getCameraNode()->getAspectRatio());
165 return 1;
166 }
167
checkobject(lua_State * L,int narg)168 LuaCamera *LuaCamera::checkobject(lua_State *L, int narg)
169 {
170 luaL_checktype(L, narg, LUA_TUSERDATA);
171
172 void *ud = luaL_checkudata(L, narg, className);
173 if (!ud)
174 luaL_typerror(L, narg, className);
175
176 return *(LuaCamera **)ud;
177 }
178
getobject(LuaCamera * ref)179 Camera *LuaCamera::getobject(LuaCamera *ref)
180 {
181 return ref->m_camera;
182 }
183
getobject(lua_State * L,int narg)184 Camera *LuaCamera::getobject(lua_State *L, int narg)
185 {
186 LuaCamera *ref = checkobject(L, narg);
187 assert(ref);
188 Camera *camera = getobject(ref);
189 if (!camera)
190 return NULL;
191 return camera;
192 }
193
gc_object(lua_State * L)194 int LuaCamera::gc_object(lua_State *L)
195 {
196 LuaCamera *o = *(LuaCamera **)(lua_touserdata(L, 1));
197 delete o;
198 return 0;
199 }
200
Register(lua_State * L)201 void LuaCamera::Register(lua_State *L)
202 {
203 lua_newtable(L);
204 int methodtable = lua_gettop(L);
205 luaL_newmetatable(L, className);
206 int metatable = lua_gettop(L);
207
208 lua_pushliteral(L, "__metatable");
209 lua_pushvalue(L, methodtable);
210 lua_settable(L, metatable);
211
212 lua_pushliteral(L, "__index");
213 lua_pushvalue(L, methodtable);
214 lua_settable(L, metatable);
215
216 lua_pushliteral(L, "__gc");
217 lua_pushcfunction(L, gc_object);
218 lua_settable(L, metatable);
219
220 lua_pop(L, 1);
221
222 luaL_openlib(L, 0, methods, 0);
223 lua_pop(L, 1);
224 }
225
226 // clang-format off
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),
238
239 {0, 0}
240 };
241 // clang-format on
242