incrementaltp: respect physics overrides
[waspsaliva.git] / src / script / lua_api / l_inventoryaction.cpp
blob8fefe48bb48134ceae16c460b3fa784ad7eccf87
1 /*
2 Dragonfire
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));
27         delete o;
28         return 0;
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());
37         return 1;
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);
52         return 0;
55 int LuaInventoryAction::l_from(lua_State *L)
57         GET_MOVE_ACTION
58         readFullInventoryLocationInto(L, &act->from_inv, &act->from_list, &act->from_i);
59         return 0;
62 int LuaInventoryAction::l_to(lua_State *L)
64         GET_MOVE_ACTION
65         readFullInventoryLocationInto(L, &act->to_inv, &act->to_list, &act->to_i);
66         return 0;
69 int LuaInventoryAction::l_craft(lua_State *L)
71         LuaInventoryAction *o = checkobject(L, 1);
73         if (o->m_action->getType() != IAction::Craft)
74                 return 0;
76         std::string locStr;
77         InventoryLocation loc;
79         locStr = readParam<std::string>(L, 2);
81         try {
82                 loc.deSerialize(locStr);
83                 dynamic_cast<ICraftAction *>(o->m_action)->craft_inv = loc;
84         } catch (SerializationError &) {
85         }
87         return 0;
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()) {
97         case IAction::Move:
98                 ((IMoveAction *)o->m_action)->count = count;
99                 break;
100         case IAction::Drop:
101                 ((IDropAction *)o->m_action)->count = count;
102                 break;
103         case IAction::Craft:
104                 ((ICraftAction *)o->m_action)->count = count;
105                 break;
106         }
108         return 0;
111 int LuaInventoryAction::l_to_table(lua_State *L)
113         LuaInventoryAction *o = checkobject(L, 1);
114         MoveAction *act = dynamic_cast<MoveAction *>(o->m_action);
116         std::string type = "";
117         u16 count = 0;
118         switch (o->m_action->getType()) {
119         case IAction::Move:
120                 count = ((IMoveAction *)o->m_action)->count;
121                 type = "move";
122                 break;
123         case IAction::Drop:
124                 count = ((IDropAction *)o->m_action)->count;
125                 type = "drop";
126                 break;
127         case IAction::Craft:
128                 count = ((ICraftAction *)o->m_action)->count;
129                 type = "craft";
130                 break;
131         }
133         lua_newtable(L);
134         lua_pushinteger(L, count);
135         lua_setfield(L, -2, "count");
136         lua_pushstring(L, type.c_str());
137         lua_setfield(L, -2, "type");
139         lua_newtable(L);
140         std::ostringstream from_loc;
141         act->from_inv.serialize(from_loc);
142         lua_pushstring(L, from_loc.str().c_str());
143         lua_setfield(L, -2, "location");
144         lua_pushstring(L, act->from_list.c_str());
145         lua_setfield(L, -2, "inventory");
146         lua_pushinteger(L, act->from_i + 1);
147         lua_setfield(L, -2, "slot");
148         lua_setfield(L, -2, "from");
150         lua_newtable(L);
151         std::ostringstream to_loc;
152         act->to_inv.serialize(to_loc);
153         lua_pushstring(L, to_loc.str().c_str());
154         lua_setfield(L, -2, "location");
155         lua_pushstring(L, act->to_list.c_str());
156         lua_setfield(L, -2, "inventory");
157         lua_pushinteger(L, act->to_i + 1);
158         lua_setfield(L, -2, "slot");
159         lua_setfield(L, -2, "to");
161         return 1;
164 LuaInventoryAction::LuaInventoryAction(const IAction &type) : m_action(nullptr)
166         switch (type) {
167         case IAction::Move:
168                 m_action = new IMoveAction();
169                 break;
170         case IAction::Drop:
171                 m_action = new IDropAction();
172                 break;
173         case IAction::Craft:
174                 m_action = new ICraftAction();
175                 break;
176         }
179 LuaInventoryAction::~LuaInventoryAction()
181         delete m_action;
184 void LuaInventoryAction::readFullInventoryLocationInto(
185                 lua_State *L, InventoryLocation *loc, std::string *list, s16 *index)
187         try {
188                 loc->deSerialize(readParam<std::string>(L, 2));
189                 std::string l = readParam<std::string>(L, 3);
190                 *list = l;
191                 *index = luaL_checkinteger(L, 4) - 1;
192         } catch (SerializationError &) {
193         }
196 int LuaInventoryAction::create_object(lua_State *L)
198         IAction type;
199         std::string typeStr;
201         typeStr = readParam<std::string>(L, 1);
203         if (typeStr == "move")
204                 type = IAction::Move;
205         else if (typeStr == "drop")
206                 type = IAction::Drop;
207         else if (typeStr == "craft")
208                 type = IAction::Craft;
209         else
210                 return 0;
212         LuaInventoryAction *o = new LuaInventoryAction(type);
213         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
214         luaL_getmetatable(L, className);
215         lua_setmetatable(L, -2);
216         return 1;
219 int LuaInventoryAction::create(lua_State *L, const IAction &type)
221         LuaInventoryAction *o = new LuaInventoryAction(type);
222         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
223         luaL_getmetatable(L, className);
224         lua_setmetatable(L, -2);
225         return 1;
228 LuaInventoryAction *LuaInventoryAction::checkobject(lua_State *L, int narg)
230         return *(LuaInventoryAction **)luaL_checkudata(L, narg, className);
233 void LuaInventoryAction::Register(lua_State *L)
235         lua_newtable(L);
236         int methodtable = lua_gettop(L);
237         luaL_newmetatable(L, className);
238         int metatable = lua_gettop(L);
240         lua_pushliteral(L, "__metatable");
241         lua_pushvalue(L, methodtable);
242         lua_settable(L, metatable);
244         lua_pushliteral(L, "__index");
245         lua_pushvalue(L, methodtable);
246         lua_settable(L, metatable);
248         lua_pushliteral(L, "__gc");
249         lua_pushcfunction(L, gc_object);
250         lua_settable(L, metatable);
252         lua_pushliteral(L, "__tostring");
253         lua_pushcfunction(L, mt_tostring);
254         lua_settable(L, metatable);
256         lua_pop(L, 1);
258         luaL_openlib(L, 0, methods, 0);
259         lua_pop(L, 1);
261         lua_register(L, className, create_object);
264 const char LuaInventoryAction::className[] = "InventoryAction";
265 const luaL_Reg LuaInventoryAction::methods[] = {
266         luamethod(LuaInventoryAction, apply),
267         luamethod(LuaInventoryAction, from),
268         luamethod(LuaInventoryAction, to),
269         luamethod(LuaInventoryAction, craft),
270         luamethod(LuaInventoryAction, set_count),
271         luamethod(LuaInventoryAction, to_table),
272         {0,0}