3 Copyright (C) 2020 system32
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.
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.
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.
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"
27 ClientObjectRef *ClientObjectRef::checkobject(lua_State *L, int narg)
29 luaL_checktype(L, narg, LUA_TUSERDATA);
30 void *userdata = luaL_checkudata(L, narg, className);
32 luaL_typerror(L, narg, className);
33 return *(ClientObjectRef **)userdata;
36 ClientActiveObject *ClientObjectRef::get_cao(ClientObjectRef *ref)
38 ClientActiveObject *obj = ref->m_object;
42 GenericCAO *ClientObjectRef::get_generic_cao(ClientObjectRef *ref, lua_State *L)
44 ClientActiveObject *obj = get_cao(ref);
45 ClientEnvironment &env = getClient(L)->getEnv();
46 GenericCAO *gcao = env.getGenericCAO(obj->getId());
50 int ClientObjectRef::l_get_pos(lua_State *L)
52 ClientObjectRef *ref = checkobject(L, 1);
53 ClientActiveObject *cao = get_cao(ref);
54 push_v3f(L, cao->getPosition() / BS);
58 int ClientObjectRef::l_get_velocity(lua_State *L)
60 ClientObjectRef *ref = checkobject(L, 1);
61 GenericCAO *gcao = get_generic_cao(ref, L);
62 push_v3f(L, gcao->getVelocity() / BS);
66 int ClientObjectRef::l_get_acceleration(lua_State *L)
68 ClientObjectRef *ref = checkobject(L, 1);
69 GenericCAO *gcao = get_generic_cao(ref, L);
70 push_v3f(L, gcao->getAcceleration() / BS);
74 int ClientObjectRef::l_get_rotation(lua_State *L)
76 ClientObjectRef *ref = checkobject(L, 1);
77 GenericCAO *gcao = get_generic_cao(ref, L);
78 push_v3f(L, gcao->getRotation());
82 int ClientObjectRef::l_is_player(lua_State *L)
84 ClientObjectRef *ref = checkobject(L, 1);
85 GenericCAO *gcao = get_generic_cao(ref, L);
86 lua_pushboolean(L, gcao->isPlayer());
90 int ClientObjectRef::l_is_local_player(lua_State *L)
92 ClientObjectRef *ref = checkobject(L, 1);
93 GenericCAO *gcao = get_generic_cao(ref, L);
94 lua_pushboolean(L, gcao->isLocalPlayer());
98 int ClientObjectRef::l_get_name(lua_State *L)
100 ClientObjectRef *ref = checkobject(L, 1);
101 GenericCAO *gcao = get_generic_cao(ref, L);
102 lua_pushstring(L, gcao->getName().c_str());
106 int ClientObjectRef::l_get_attach(lua_State *L)
108 ClientObjectRef *ref = checkobject(L, 1);
109 GenericCAO *gcao = get_generic_cao(ref, L);
110 create(L, gcao->getParent());
114 int ClientObjectRef::l_get_nametag(lua_State *L)
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());
123 int ClientObjectRef::l_get_item_textures(lua_State *L)
125 ClientObjectRef *ref = checkobject(L, 1);
126 GenericCAO *gcao = get_generic_cao(ref, L);
127 ObjectProperties *props = gcao->getProperties();
130 for (std::string &texture : props->textures) {
131 lua_pushstring(L, texture.c_str());
136 int ClientObjectRef::l_get_max_hp(lua_State *L)
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);
145 int ClientObjectRef::l_punch(lua_State *L)
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);
154 int ClientObjectRef::l_rightclick(lua_State *L)
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);
163 ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object)
167 void ClientObjectRef::create(lua_State *L, ClientActiveObject *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);
177 void ClientObjectRef::create(lua_State *L, s16 id)
179 create(L, ((ClientEnvironment *)getEnv(L))->getActiveObject(id));
182 int ClientObjectRef::gc_object(lua_State *L)
184 ClientObjectRef *obj = *(ClientObjectRef **)(lua_touserdata(L, 1));
189 // taken from LuaLocalPlayer
190 void ClientObjectRef::Register(lua_State *L)
193 int methodtable = lua_gettop(L);
194 luaL_newmetatable(L, className);
195 int metatable = lua_gettop(L);
197 lua_pushliteral(L, "__metatable");
198 lua_pushvalue(L, methodtable);
199 lua_settable(L, metatable); // hide metatable from lua getmetatable()
201 lua_pushliteral(L, "__index");
202 lua_pushvalue(L, methodtable);
203 lua_settable(L, metatable);
205 lua_pushliteral(L, "__gc");
206 lua_pushcfunction(L, gc_object);
207 lua_settable(L, metatable);
209 lua_pop(L, 1); // Drop metatable
211 luaL_openlib(L, 0, methods, 0); // fill methodtable
212 lua_pop(L, 1); // Drop methodtable
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}};