]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_camera.cpp
Code modernization: subfolders (#6283)
[dragonfireclient.git] / src / script / lua_api / l_camera.cpp
1 #include "script/common/c_converter.h"
2 #include "l_camera.h"
3 #include "l_internal.h"
4 #include "content_cao.h"
5 #include "camera.h"
6 #include "client.h"
7
8 LuaCamera::LuaCamera(Camera *m) : m_camera(m)
9 {
10 }
11
12 void LuaCamera::create(lua_State *L, Camera *m)
13 {
14         LuaCamera *o = new LuaCamera(m);
15         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
16         luaL_getmetatable(L, className);
17         lua_setmetatable(L, -2);
18
19         int camera_object = lua_gettop(L);
20
21         lua_getglobal(L, "core");
22         luaL_checktype(L, -1, LUA_TTABLE);
23         int coretable = lua_gettop(L);
24
25         lua_pushvalue(L, camera_object);
26         lua_setfield(L, coretable, "camera");
27 }
28
29 int LuaCamera::l_set_camera_mode(lua_State *L)
30 {
31         Camera *camera = getobject(L, 1);
32         GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO();
33         if (!camera)
34                 return 0;
35         sanity_check(playercao);
36         if (!lua_isnumber(L, 2))
37                 return 0;
38
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);
42         return 0;
43 }
44
45 int LuaCamera::l_get_camera_mode(lua_State *L)
46 {
47         Camera *camera = getobject(L, 1);
48         if (!camera)
49                 return 0;
50
51         lua_pushnumber(L, (int)camera->getCameraMode());
52
53         return 1;
54 }
55
56 int LuaCamera::l_get_fov(lua_State *L)
57 {
58         Camera *camera = getobject(L, 1);
59         if (!camera)
60                 return 0;
61
62         lua_newtable(L);
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");
71         return 1;
72 }
73
74 int LuaCamera::l_get_pos(lua_State *L)
75 {
76         Camera *camera = getobject(L, 1);
77         if (!camera)
78                 return 0;
79
80         push_v3f(L, camera->getPosition());
81         return 1;
82 }
83
84 int LuaCamera::l_get_offset(lua_State *L)
85 {
86         Camera *camera = getobject(L, 1);
87         if (!camera)
88                 return 0;
89
90         push_v3s16(L, camera->getOffset());
91         return 1;
92 }
93
94 int LuaCamera::l_get_look_dir(lua_State *L)
95 {
96         LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
97         sanity_check(player);
98
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));
102
103         push_v3f(L, v);
104         return 1;
105 }
106
107 int LuaCamera::l_get_look_horizontal(lua_State *L)
108 {
109         LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
110         sanity_check(player);
111
112         lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
113         return 1;
114 }
115
116 int LuaCamera::l_get_look_vertical(lua_State *L)
117 {
118         LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
119         sanity_check(player);
120
121         lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
122         return 1;
123 }
124
125 int LuaCamera::l_get_aspect_ratio(lua_State *L)
126 {
127         Camera *camera = getobject(L, 1);
128         if (!camera)
129                 return 0;
130
131         lua_pushnumber(L, camera->getCameraNode()->getAspectRatio());
132         return 1;
133 }
134
135 LuaCamera *LuaCamera::checkobject(lua_State *L, int narg)
136 {
137         luaL_checktype(L, narg, LUA_TUSERDATA);
138
139         void *ud = luaL_checkudata(L, narg, className);
140         if (!ud)
141                 luaL_typerror(L, narg, className);
142
143         return *(LuaCamera **)ud;
144 }
145
146 Camera *LuaCamera::getobject(LuaCamera *ref)
147 {
148         return ref->m_camera;
149 }
150
151 Camera *LuaCamera::getobject(lua_State *L, int narg)
152 {
153         LuaCamera *ref = checkobject(L, narg);
154         assert(ref);
155         Camera *camera = getobject(ref);
156         if (!camera)
157                 return NULL;
158         return camera;
159 }
160
161 int LuaCamera::gc_object(lua_State *L)
162 {
163         LuaCamera *o = *(LuaCamera **)(lua_touserdata(L, 1));
164         delete o;
165         return 0;
166 }
167
168 void LuaCamera::Register(lua_State *L)
169 {
170         lua_newtable(L);
171         int methodtable = lua_gettop(L);
172         luaL_newmetatable(L, className);
173         int metatable = lua_gettop(L);
174
175         lua_pushliteral(L, "__metatable");
176         lua_pushvalue(L, methodtable);
177         lua_settable(L, metatable);
178
179         lua_pushliteral(L, "__index");
180         lua_pushvalue(L, methodtable);
181         lua_settable(L, metatable);
182
183         lua_pushliteral(L, "__gc");
184         lua_pushcfunction(L, gc_object);
185         lua_settable(L, metatable);
186
187         lua_pop(L, 1);
188
189         luaL_openlib(L, 0, methods, 0);
190         lua_pop(L, 1);
191 }
192
193 const char LuaCamera::className[] = "Camera";
194 const luaL_Reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode),
195                 luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov),
196                 luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset),
197                 luamethod(LuaCamera, get_look_dir),
198                 luamethod(LuaCamera, get_look_vertical),
199                 luamethod(LuaCamera, get_look_horizontal),
200                 luamethod(LuaCamera, get_aspect_ratio),
201
202                 {0, 0}};