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_get_name(lua_State *L)
92 ClientObjectRef *ref = checkobject(L, 1);
93 GenericCAO *gcao = get_generic_cao(ref, L);
94 lua_pushstring(L, gcao->getName().c_str());
98 int ClientObjectRef::l_get_attach(lua_State *L)
100 ClientObjectRef *ref = checkobject(L, 1);
101 GenericCAO *gcao = get_generic_cao(ref, L);
102 create(L, gcao->getParent());
106 int ClientObjectRef::l_get_nametag(lua_State *L)
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());
115 int ClientObjectRef::l_get_item_textures(lua_State *L)
117 ClientObjectRef *ref = checkobject(L, 1);
118 GenericCAO *gcao = get_generic_cao(ref, L);
119 ObjectProperties *props = gcao->getProperties();
122 for (std::string &texture : props->textures) {
123 lua_pushstring(L, texture.c_str());
128 int ClientObjectRef::l_get_max_hp(lua_State *L)
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);
137 int ClientObjectRef::l_punch(lua_State *L)
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);
146 int ClientObjectRef::l_rightclick(lua_State *L)
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);
155 ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object)
159 void ClientObjectRef::create(lua_State *L, ClientActiveObject *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);
169 void ClientObjectRef::create(lua_State *L, s16 id)
171 create(L, ((ClientEnvironment *)getEnv(L))->getActiveObject(id));
174 int ClientObjectRef::gc_object(lua_State *L)
176 ClientObjectRef *obj = *(ClientObjectRef **)(lua_touserdata(L, 1));
181 // taken from LuaLocalPlayer
182 void ClientObjectRef::Register(lua_State *L)
185 int methodtable = lua_gettop(L);
186 luaL_newmetatable(L, className);
187 int metatable = lua_gettop(L);
189 lua_pushliteral(L, "__metatable");
190 lua_pushvalue(L, methodtable);
191 lua_settable(L, metatable); // hide metatable from lua getmetatable()
193 lua_pushliteral(L, "__index");
194 lua_pushvalue(L, methodtable);
195 lua_settable(L, metatable);
197 lua_pushliteral(L, "__gc");
198 lua_pushcfunction(L, gc_object);
199 lua_settable(L, metatable);
201 lua_pop(L, 1); // Drop metatable
203 luaL_openlib(L, 0, methods, 0); // fill methodtable
204 lua_pop(L, 1); // Drop methodtable
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}};