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 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
= "";
118 switch (o
->m_action
->getType()) {
120 count
= ((IMoveAction
*)o
->m_action
)->count
;
124 count
= ((IDropAction
*)o
->m_action
)->count
;
128 count
= ((ICraftAction
*)o
->m_action
)->count
;
134 lua_pushinteger(L
, count
);
135 lua_setfield(L
, -2, "count");
136 lua_pushstring(L
, type
.c_str());
137 lua_setfield(L
, -2, "type");
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");
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");
164 LuaInventoryAction::LuaInventoryAction(const IAction
&type
) : m_action(nullptr)
168 m_action
= new IMoveAction();
171 m_action
= new IDropAction();
174 m_action
= new ICraftAction();
179 LuaInventoryAction::~LuaInventoryAction()
184 void LuaInventoryAction::readFullInventoryLocationInto(
185 lua_State
*L
, InventoryLocation
*loc
, std::string
*list
, s16
*index
)
188 loc
->deSerialize(readParam
<std::string
>(L
, 2));
189 std::string l
= readParam
<std::string
>(L
, 3);
191 *index
= luaL_checkinteger(L
, 4) - 1;
192 } catch (SerializationError
&) {
196 int LuaInventoryAction::create_object(lua_State
*L
)
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
;
212 LuaInventoryAction
*o
= new LuaInventoryAction(type
);
213 *(void **)(lua_newuserdata(L
, sizeof(void *))) = o
;
214 luaL_getmetatable(L
, className
);
215 lua_setmetatable(L
, -2);
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);
228 LuaInventoryAction
*LuaInventoryAction::checkobject(lua_State
*L
, int narg
)
230 return *(LuaInventoryAction
**)luaL_checkudata(L
, narg
, className
);
233 void LuaInventoryAction::Register(lua_State
*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
);
258 luaL_openlib(L
, 0, methods
, 0);
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
),