Fix various bugs (Anticheat, Lua helpers) (#8013)
[oweals/minetest.git] / src / script / lua_api / l_camera.cpp
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
28 LuaCamera::LuaCamera(Camera *m) : m_camera(m)
29 {
30 }
31
32 void LuaCamera::create(lua_State *L, Camera *m)
33 {
34         LuaCamera *o = new LuaCamera(m);
35         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
36         luaL_getmetatable(L, className);
37         lua_setmetatable(L, -2);
38
39         int camera_object = lua_gettop(L);
40
41         lua_getglobal(L, "core");
42         luaL_checktype(L, -1, LUA_TTABLE);
43         int coretable = lua_gettop(L);
44
45         lua_pushvalue(L, camera_object);
46         lua_setfield(L, coretable, "camera");
47 }
48
49 int LuaCamera::l_set_camera_mode(lua_State *L)
50 {
51         Camera *camera = getobject(L, 1);
52         GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO();
53         if (!camera)
54                 return 0;
55         sanity_check(playercao);
56         if (!lua_isnumber(L, 2))
57                 return 0;
58
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);
62         return 0;
63 }
64
65 int LuaCamera::l_get_camera_mode(lua_State *L)
66 {
67         Camera *camera = getobject(L, 1);
68         if (!camera)
69                 return 0;
70
71         lua_pushnumber(L, (int)camera->getCameraMode());
72
73         return 1;
74 }
75
76 int LuaCamera::l_get_fov(lua_State *L)
77 {
78         Camera *camera = getobject(L, 1);
79         if (!camera)
80                 return 0;
81
82         lua_newtable(L);
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");
91         return 1;
92 }
93
94 int LuaCamera::l_get_pos(lua_State *L)
95 {
96         Camera *camera = getobject(L, 1);
97         if (!camera)
98                 return 0;
99
100         push_v3f(L, camera->getPosition());
101         return 1;
102 }
103
104 int LuaCamera::l_get_offset(lua_State *L)
105 {
106         Camera *camera = getobject(L, 1);
107         if (!camera)
108                 return 0;
109
110         push_v3s16(L, camera->getOffset());
111         return 1;
112 }
113
114 int LuaCamera::l_get_look_dir(lua_State *L)
115 {
116         LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
117         sanity_check(player);
118
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));
123
124         push_v3f(L, v);
125         return 1;
126 }
127
128 int LuaCamera::l_get_look_horizontal(lua_State *L)
129 {
130         LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
131         sanity_check(player);
132
133         lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
134         return 1;
135 }
136
137 int LuaCamera::l_get_look_vertical(lua_State *L)
138 {
139         LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
140         sanity_check(player);
141
142         lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
143         return 1;
144 }
145
146 int LuaCamera::l_get_aspect_ratio(lua_State *L)
147 {
148         Camera *camera = getobject(L, 1);
149         if (!camera)
150                 return 0;
151
152         lua_pushnumber(L, camera->getCameraNode()->getAspectRatio());
153         return 1;
154 }
155
156 LuaCamera *LuaCamera::checkobject(lua_State *L, int narg)
157 {
158         luaL_checktype(L, narg, LUA_TUSERDATA);
159
160         void *ud = luaL_checkudata(L, narg, className);
161         if (!ud)
162                 luaL_typerror(L, narg, className);
163
164         return *(LuaCamera **)ud;
165 }
166
167 Camera *LuaCamera::getobject(LuaCamera *ref)
168 {
169         return ref->m_camera;
170 }
171
172 Camera *LuaCamera::getobject(lua_State *L, int narg)
173 {
174         LuaCamera *ref = checkobject(L, narg);
175         assert(ref);
176         Camera *camera = getobject(ref);
177         if (!camera)
178                 return NULL;
179         return camera;
180 }
181
182 int LuaCamera::gc_object(lua_State *L)
183 {
184         LuaCamera *o = *(LuaCamera **)(lua_touserdata(L, 1));
185         delete o;
186         return 0;
187 }
188
189 void LuaCamera::Register(lua_State *L)
190 {
191         lua_newtable(L);
192         int methodtable = lua_gettop(L);
193         luaL_newmetatable(L, className);
194         int metatable = lua_gettop(L);
195
196         lua_pushliteral(L, "__metatable");
197         lua_pushvalue(L, methodtable);
198         lua_settable(L, metatable);
199
200         lua_pushliteral(L, "__index");
201         lua_pushvalue(L, methodtable);
202         lua_settable(L, metatable);
203
204         lua_pushliteral(L, "__gc");
205         lua_pushcfunction(L, gc_object);
206         lua_settable(L, metatable);
207
208         lua_pop(L, 1);
209
210         luaL_openlib(L, 0, methods, 0);
211         lua_pop(L, 1);
212 }
213
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),
222
223                 {0, 0}};