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 &) {}
89 int LuaInventoryAction::l_set_count(lua_State *L)
91 LuaInventoryAction *o = checkobject(L, 1);
93 s16 count = luaL_checkinteger(L, 2);
95 switch (o->m_action->getType()) {
97 ((IMoveAction *)o->m_action)->count = count;
100 ((IDropAction *)o->m_action)->count = count;
103 ((ICraftAction *)o->m_action)->count = count;
110 LuaInventoryAction::LuaInventoryAction(const IAction &type) : m_action(nullptr)
114 m_action = new IMoveAction();
117 m_action = new IDropAction();
120 m_action = new ICraftAction();
125 LuaInventoryAction::~LuaInventoryAction()
130 void LuaInventoryAction::readFullInventoryLocationInto(lua_State *L, InventoryLocation *loc, std::string *list, s16 *index)
133 loc->deSerialize(readParam<std::string>(L, 2));
134 std::string l = readParam<std::string>(L, 3);
136 *index = luaL_checkinteger(L, 4) - 1;
137 } catch (SerializationError &) {}
140 int LuaInventoryAction::create_object(lua_State *L)
145 typeStr = readParam<std::string>(L, 1);
147 if (typeStr == "move")
148 type = IAction::Move;
149 else if (typeStr == "drop")
150 type = IAction::Drop;
151 else if (typeStr == "craft")
152 type = IAction::Craft;
156 LuaInventoryAction *o = new LuaInventoryAction(type);
157 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
158 luaL_getmetatable(L, className);
159 lua_setmetatable(L, -2);
163 int LuaInventoryAction::create(lua_State *L, const IAction &type)
165 LuaInventoryAction *o = new LuaInventoryAction(type);
166 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
167 luaL_getmetatable(L, className);
168 lua_setmetatable(L, -2);
172 LuaInventoryAction *LuaInventoryAction::checkobject(lua_State *L, int narg)
174 return *(LuaInventoryAction **)luaL_checkudata(L, narg, className);
177 void LuaInventoryAction::Register(lua_State *L)
180 int methodtable = lua_gettop(L);
181 luaL_newmetatable(L, className);
182 int metatable = lua_gettop(L);
184 lua_pushliteral(L, "__metatable");
185 lua_pushvalue(L, methodtable);
186 lua_settable(L, metatable);
188 lua_pushliteral(L, "__index");
189 lua_pushvalue(L, methodtable);
190 lua_settable(L, metatable);
192 lua_pushliteral(L, "__gc");
193 lua_pushcfunction(L, gc_object);
194 lua_settable(L, metatable);
196 lua_pushliteral(L, "__tostring");
197 lua_pushcfunction(L, mt_tostring);
198 lua_settable(L, metatable);
202 luaL_openlib(L, 0, methods, 0);
205 lua_register(L, className, create_object);
208 const char LuaInventoryAction::className[] = "InventoryAction";
209 const luaL_Reg LuaInventoryAction::methods[] = {
210 luamethod(LuaInventoryAction, apply),
211 luamethod(LuaInventoryAction, from),
212 luamethod(LuaInventoryAction, to),
213 luamethod(LuaInventoryAction, craft),
214 luamethod(LuaInventoryAction, set_count),