3 Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
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 "l_inventoryaction.h"
21 #include "l_internal.h"
22 #include "client/client.h"
24 int LuaInventoryAction::gc_object(lua_State *L)
26 LuaInventoryAction *o = *(LuaInventoryAction **)(lua_touserdata(L, 1));
31 int LuaInventoryAction::mt_tostring(lua_State *L)
33 LuaInventoryAction *o = checkobject(L, 1);
34 std::ostringstream os(std::ios::binary);
35 o->m_action->serialize(os);
36 lua_pushfstring(L, "InventoryAction(\"%s\")", os.str().c_str());
40 int LuaInventoryAction::l_apply(lua_State *L)
42 LuaInventoryAction *o = checkobject(L, 1);
44 std::ostringstream os(std::ios::binary);
45 o->m_action->serialize(os);
47 std::istringstream is(os.str(), std::ios_base::binary);
49 InventoryAction *a = InventoryAction::deSerialize(is);
51 getClient(L)->inventoryAction(a);
55 int LuaInventoryAction::l_from(lua_State *L)
58 readFullInventoryLocationInto(L, &act->from_inv, &act->from_list, &act->from_i);
62 int LuaInventoryAction::l_to(lua_State *L)
65 readFullInventoryLocationInto(L, &act->to_inv, &act->to_list, &act->to_i);
69 int LuaInventoryAction::l_craft(lua_State *L)
71 LuaInventoryAction *o = checkobject(L, 1);
73 if (o->m_action->getType() != IAction::Craft)
77 InventoryLocation loc;
79 locStr = readParam<std::string>(L, 2);
82 loc.deSerialize(locStr);
83 dynamic_cast<ICraftAction *>(o->m_action)->craft_inv = loc;
84 } catch (SerializationError &) {
90 int LuaInventoryAction::l_set_count(lua_State *L)
92 LuaInventoryAction *o = checkobject(L, 1);
94 s16 count = luaL_checkinteger(L, 2);
96 switch (o->m_action->getType()) {
98 ((IMoveAction *)o->m_action)->count = count;
101 ((IDropAction *)o->m_action)->count = count;
104 ((ICraftAction *)o->m_action)->count = count;
111 LuaInventoryAction::LuaInventoryAction(const IAction &type) : m_action(nullptr)
115 m_action = new IMoveAction();
118 m_action = new IDropAction();
121 m_action = new ICraftAction();
126 LuaInventoryAction::~LuaInventoryAction()
131 void LuaInventoryAction::readFullInventoryLocationInto(
132 lua_State *L, InventoryLocation *loc, std::string *list, s16 *index)
135 loc->deSerialize(readParam<std::string>(L, 2));
136 std::string l = readParam<std::string>(L, 3);
138 *index = luaL_checkinteger(L, 4) - 1;
139 } catch (SerializationError &) {
143 int LuaInventoryAction::create_object(lua_State *L)
148 typeStr = readParam<std::string>(L, 1);
150 if (typeStr == "move")
151 type = IAction::Move;
152 else if (typeStr == "drop")
153 type = IAction::Drop;
154 else if (typeStr == "craft")
155 type = IAction::Craft;
159 LuaInventoryAction *o = new LuaInventoryAction(type);
160 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
161 luaL_getmetatable(L, className);
162 lua_setmetatable(L, -2);
166 int LuaInventoryAction::create(lua_State *L, const IAction &type)
168 LuaInventoryAction *o = new LuaInventoryAction(type);
169 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
170 luaL_getmetatable(L, className);
171 lua_setmetatable(L, -2);
175 LuaInventoryAction *LuaInventoryAction::checkobject(lua_State *L, int narg)
177 return *(LuaInventoryAction **)luaL_checkudata(L, narg, className);
180 void LuaInventoryAction::Register(lua_State *L)
183 int methodtable = lua_gettop(L);
184 luaL_newmetatable(L, className);
185 int metatable = lua_gettop(L);
187 lua_pushliteral(L, "__metatable");
188 lua_pushvalue(L, methodtable);
189 lua_settable(L, metatable);
191 lua_pushliteral(L, "__index");
192 lua_pushvalue(L, methodtable);
193 lua_settable(L, metatable);
195 lua_pushliteral(L, "__gc");
196 lua_pushcfunction(L, gc_object);
197 lua_settable(L, metatable);
199 lua_pushliteral(L, "__tostring");
200 lua_pushcfunction(L, mt_tostring);
201 lua_settable(L, metatable);
205 luaL_openlib(L, 0, methods, 0);
208 lua_register(L, className, create_object);
211 const char LuaInventoryAction::className[] = "InventoryAction";
212 const luaL_Reg LuaInventoryAction::methods[] = {luamethod(LuaInventoryAction, apply),
213 luamethod(LuaInventoryAction, from), luamethod(LuaInventoryAction, to),
214 luamethod(LuaInventoryAction, craft),
215 luamethod(LuaInventoryAction, set_count), {0, 0}};