]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_clientobject.cpp
90f0bcd15043e8fa6ccf6899ac92b530b08929ac
[dragonfireclient.git] / src / script / lua_api / l_clientobject.cpp
1 /*
2 Dragonfire
3 Copyright (C) 2020 system32
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 "lua_api/l_clientobject.h"
21 #include "l_internal.h"
22 #include "common/c_converter.h"
23 #include "client/client.h"
24 #include "object_properties.h"
25 #include "util/pointedthing.h"
26
27 ClientObjectRef *ClientObjectRef::checkobject(lua_State *L, int narg)
28 {
29         luaL_checktype(L, narg, LUA_TUSERDATA);
30         void *userdata = luaL_checkudata(L, narg, className);
31         if (!userdata)
32                 luaL_typerror(L, narg, className);
33         return *(ClientObjectRef **)userdata;
34 }
35
36 ClientActiveObject *ClientObjectRef::get_cao(ClientObjectRef *ref)
37 {
38         ClientActiveObject *obj = ref->m_object;
39         return obj;
40 }
41
42 GenericCAO *ClientObjectRef::get_generic_cao(ClientObjectRef *ref, lua_State *L)
43 {
44         ClientActiveObject *obj = get_cao(ref);
45         ClientEnvironment &env = getClient(L)->getEnv();
46         GenericCAO *gcao = env.getGenericCAO(obj->getId());
47         return gcao;
48 }
49
50 int ClientObjectRef::l_get_pos(lua_State *L)
51 {
52         ClientObjectRef *ref = checkobject(L, 1);
53         ClientActiveObject *cao = get_cao(ref);
54         push_v3f(L, cao->getPosition() / BS);
55         return 1;
56 }
57
58 int ClientObjectRef::l_get_velocity(lua_State *L)
59 {
60         ClientObjectRef *ref = checkobject(L, 1);
61         GenericCAO *gcao = get_generic_cao(ref, L);
62         push_v3f(L, gcao->getVelocity() / BS);
63         return 1;
64 }
65
66 int ClientObjectRef::l_get_acceleration(lua_State *L)
67 {
68         ClientObjectRef *ref = checkobject(L, 1);
69         GenericCAO *gcao = get_generic_cao(ref, L);
70         push_v3f(L, gcao->getAcceleration() / BS);
71         return 1;
72 }
73
74 int ClientObjectRef::l_get_rotation(lua_State *L)
75 {
76         ClientObjectRef *ref = checkobject(L, 1);
77         GenericCAO *gcao = get_generic_cao(ref, L);
78         push_v3f(L, gcao->getRotation());
79         return 1;
80 }
81
82 int ClientObjectRef::l_is_player(lua_State *L)
83 {
84         ClientObjectRef *ref = checkobject(L, 1);
85         GenericCAO *gcao = get_generic_cao(ref, L);
86         lua_pushboolean(L, gcao->isPlayer());
87         return 1;
88 }
89
90 int ClientObjectRef::l_get_name(lua_State *L)
91 {
92         ClientObjectRef *ref = checkobject(L, 1);
93         GenericCAO *gcao = get_generic_cao(ref, L);
94         lua_pushstring(L, gcao->getName().c_str());
95         return 1;
96 }
97
98 int ClientObjectRef::l_get_attach(lua_State *L)
99 {
100         ClientObjectRef *ref = checkobject(L, 1);
101         GenericCAO *gcao = get_generic_cao(ref, L);
102         create(L, gcao->getParent());
103         return 1;
104 }
105
106 int ClientObjectRef::l_get_nametag(lua_State *L)
107 {
108         ClientObjectRef *ref = checkobject(L, 1);
109         GenericCAO *gcao = get_generic_cao(ref, L);
110         ObjectProperties *props = gcao->getProperties();
111         lua_pushstring(L, props->nametag.c_str());
112         return 1;
113 }
114
115 int ClientObjectRef::l_get_item_textures(lua_State *L)
116 {
117         ClientObjectRef *ref = checkobject(L, 1);
118         GenericCAO *gcao = get_generic_cao(ref, L);
119         ObjectProperties *props = gcao->getProperties();
120         lua_newtable(L);
121
122         for (std::string &texture : props->textures) {
123                 lua_pushstring(L, texture.c_str());
124         }
125         return 1;
126 }
127
128 int ClientObjectRef::l_get_max_hp(lua_State *L)
129 {
130         ClientObjectRef *ref = checkobject(L, 1);
131         GenericCAO *gcao = get_generic_cao(ref, L);
132         ObjectProperties *props = gcao->getProperties();
133         lua_pushnumber(L, props->hp_max);
134         return 1;
135 }
136
137 int ClientObjectRef::l_punch(lua_State *L)
138 {
139         ClientObjectRef *ref = checkobject(L, 1);
140         GenericCAO *gcao = get_generic_cao(ref, L);
141         PointedThing pointed(gcao->getId(), v3f(0,0,0), v3s16(0,0,0), 0);
142         getClient(L)->interact(INTERACT_START_DIGGING, pointed);
143         return 0;
144 }
145
146 int ClientObjectRef::l_rightclick(lua_State *L)
147 {
148         ClientObjectRef *ref = checkobject(L, 1);
149         GenericCAO *gcao = get_generic_cao(ref, L);
150         PointedThing pointed(gcao->getId(), v3f(0,0,0), v3s16(0,0,0), 0);
151         getClient(L)->interact(INTERACT_PLACE, pointed);
152         return 0;
153 }
154
155 ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object)
156 {
157 }
158
159 void ClientObjectRef::create(lua_State *L, ClientActiveObject *object)
160 {
161         if (object) {
162                 ClientObjectRef *o = new ClientObjectRef(object);
163                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
164                 luaL_getmetatable(L, className);
165                 lua_setmetatable(L, -2);
166         }
167 }
168
169 void ClientObjectRef::create(lua_State *L, s16 id)
170 {
171         create(L, ((ClientEnvironment *)getEnv(L))->getActiveObject(id));
172 }
173
174 int ClientObjectRef::gc_object(lua_State *L)
175 {
176         ClientObjectRef *obj = *(ClientObjectRef **)(lua_touserdata(L, 1));
177         delete obj;
178         return 0;
179 }
180
181 // taken from LuaLocalPlayer
182 void ClientObjectRef::Register(lua_State *L)
183 {
184         lua_newtable(L);
185         int methodtable = lua_gettop(L);
186         luaL_newmetatable(L, className);
187         int metatable = lua_gettop(L);
188
189         lua_pushliteral(L, "__metatable");
190         lua_pushvalue(L, methodtable);
191         lua_settable(L, metatable); // hide metatable from lua getmetatable()
192
193         lua_pushliteral(L, "__index");
194         lua_pushvalue(L, methodtable);
195         lua_settable(L, metatable);
196
197         lua_pushliteral(L, "__gc");
198         lua_pushcfunction(L, gc_object);
199         lua_settable(L, metatable);
200
201         lua_pop(L, 1); // Drop metatable
202
203         luaL_openlib(L, 0, methods, 0); // fill methodtable
204         lua_pop(L, 1);                  // Drop methodtable
205 }
206
207 const char ClientObjectRef::className[] = "ClientObjectRef";
208 luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos),
209                 luamethod(ClientObjectRef, get_velocity),
210                 luamethod(ClientObjectRef, get_acceleration),
211                 luamethod(ClientObjectRef, get_rotation),
212                 luamethod(ClientObjectRef, is_player),
213                 luamethod(ClientObjectRef, get_name),
214                 luamethod(ClientObjectRef, get_attach),
215                 luamethod(ClientObjectRef, get_nametag),
216                 luamethod(ClientObjectRef, get_item_textures),
217                 luamethod(ClientObjectRef, get_max_hp),
218                 luamethod(ClientObjectRef, punch),
219                 luamethod(ClientObjectRef, rightclick), {0, 0}};