]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_clientobject.cpp
Moved Killaura to Lua; Added ForceField; Added Friendlist; Added ClientObjectRef...
[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_is_local_player(lua_State *L)
91 {
92         ClientObjectRef *ref = checkobject(L, 1);
93         GenericCAO *gcao = get_generic_cao(ref, L);
94         lua_pushboolean(L, gcao->isLocalPlayer());
95         return 1;
96 }
97
98 int ClientObjectRef::l_get_name(lua_State *L)
99 {
100         ClientObjectRef *ref = checkobject(L, 1);
101         GenericCAO *gcao = get_generic_cao(ref, L);
102         lua_pushstring(L, gcao->getName().c_str());
103         return 1;
104 }
105
106 int ClientObjectRef::l_get_attach(lua_State *L)
107 {
108         ClientObjectRef *ref = checkobject(L, 1);
109         GenericCAO *gcao = get_generic_cao(ref, L);
110         create(L, gcao->getParent());
111         return 1;
112 }
113
114 int ClientObjectRef::l_get_nametag(lua_State *L)
115 {
116         ClientObjectRef *ref = checkobject(L, 1);
117         GenericCAO *gcao = get_generic_cao(ref, L);
118         ObjectProperties *props = gcao->getProperties();
119         lua_pushstring(L, props->nametag.c_str());
120         return 1;
121 }
122
123 int ClientObjectRef::l_get_item_textures(lua_State *L)
124 {
125         ClientObjectRef *ref = checkobject(L, 1);
126         GenericCAO *gcao = get_generic_cao(ref, L);
127         ObjectProperties *props = gcao->getProperties();
128         lua_newtable(L);
129
130         for (std::string &texture : props->textures) {
131                 lua_pushstring(L, texture.c_str());
132         }
133         return 1;
134 }
135
136 int ClientObjectRef::l_get_max_hp(lua_State *L)
137 {
138         ClientObjectRef *ref = checkobject(L, 1);
139         GenericCAO *gcao = get_generic_cao(ref, L);
140         ObjectProperties *props = gcao->getProperties();
141         lua_pushnumber(L, props->hp_max);
142         return 1;
143 }
144
145 int ClientObjectRef::l_punch(lua_State *L)
146 {
147         ClientObjectRef *ref = checkobject(L, 1);
148         GenericCAO *gcao = get_generic_cao(ref, L);
149         PointedThing pointed(gcao->getId(), v3f(0,0,0), v3s16(0,0,0), 0);
150         getClient(L)->interact(INTERACT_START_DIGGING, pointed);
151         return 0;
152 }
153
154 int ClientObjectRef::l_rightclick(lua_State *L)
155 {
156         ClientObjectRef *ref = checkobject(L, 1);
157         GenericCAO *gcao = get_generic_cao(ref, L);
158         PointedThing pointed(gcao->getId(), v3f(0,0,0), v3s16(0,0,0), 0);
159         getClient(L)->interact(INTERACT_PLACE, pointed);
160         return 0;
161 }
162
163 ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object)
164 {
165 }
166
167 void ClientObjectRef::create(lua_State *L, ClientActiveObject *object)
168 {
169         if (object) {
170                 ClientObjectRef *o = new ClientObjectRef(object);
171                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
172                 luaL_getmetatable(L, className);
173                 lua_setmetatable(L, -2);
174         }
175 }
176
177 void ClientObjectRef::create(lua_State *L, s16 id)
178 {
179         create(L, ((ClientEnvironment *)getEnv(L))->getActiveObject(id));
180 }
181
182 int ClientObjectRef::gc_object(lua_State *L)
183 {
184         ClientObjectRef *obj = *(ClientObjectRef **)(lua_touserdata(L, 1));
185         delete obj;
186         return 0;
187 }
188
189 // taken from LuaLocalPlayer
190 void ClientObjectRef::Register(lua_State *L)
191 {
192         lua_newtable(L);
193         int methodtable = lua_gettop(L);
194         luaL_newmetatable(L, className);
195         int metatable = lua_gettop(L);
196
197         lua_pushliteral(L, "__metatable");
198         lua_pushvalue(L, methodtable);
199         lua_settable(L, metatable); // hide metatable from lua getmetatable()
200
201         lua_pushliteral(L, "__index");
202         lua_pushvalue(L, methodtable);
203         lua_settable(L, metatable);
204
205         lua_pushliteral(L, "__gc");
206         lua_pushcfunction(L, gc_object);
207         lua_settable(L, metatable);
208
209         lua_pop(L, 1); // Drop metatable
210
211         luaL_openlib(L, 0, methods, 0); // fill methodtable
212         lua_pop(L, 1);                  // Drop methodtable
213 }
214
215 const char ClientObjectRef::className[] = "ClientObjectRef";
216 luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos),
217                 luamethod(ClientObjectRef, get_velocity),
218                 luamethod(ClientObjectRef, get_acceleration),
219                 luamethod(ClientObjectRef, get_rotation),
220                 luamethod(ClientObjectRef, is_player),
221                 luamethod(ClientObjectRef, is_local_player),
222                 luamethod(ClientObjectRef, get_name),
223                 luamethod(ClientObjectRef, get_attach),
224                 luamethod(ClientObjectRef, get_nametag),
225                 luamethod(ClientObjectRef, get_item_textures),
226                 luamethod(ClientObjectRef, get_max_hp),
227                 luamethod(ClientObjectRef, punch),
228                 luamethod(ClientObjectRef, rightclick), {0, 0}};