]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_inventoryaction.cpp
Fix and run the Lint autocorrect script
[dragonfireclient.git] / src / script / lua_api / l_inventoryaction.cpp
1 /*
2 Dragonfire
3 Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
4
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.
9
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.
14
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.
18 */
19
20 #include "l_inventoryaction.h"
21 #include "l_internal.h"
22 #include "client/client.h"
23
24 int LuaInventoryAction::gc_object(lua_State *L)
25 {
26         LuaInventoryAction *o = *(LuaInventoryAction **)(lua_touserdata(L, 1));
27         delete o;
28         return 0;
29 }
30
31 int LuaInventoryAction::mt_tostring(lua_State *L)
32 {
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;
38 }
39
40 int LuaInventoryAction::l_apply(lua_State *L)
41 {
42         LuaInventoryAction *o = checkobject(L, 1);
43
44         std::ostringstream os(std::ios::binary);
45         o->m_action->serialize(os);
46
47         std::istringstream is(os.str(), std::ios_base::binary);
48
49         InventoryAction *a = InventoryAction::deSerialize(is);
50
51         getClient(L)->inventoryAction(a);
52         return 0;
53 }
54
55 int LuaInventoryAction::l_from(lua_State *L)
56 {
57         GET_MOVE_ACTION
58         readFullInventoryLocationInto(L, &act->from_inv, &act->from_list, &act->from_i);
59         return 0;
60 }
61
62 int LuaInventoryAction::l_to(lua_State *L)
63 {
64         GET_MOVE_ACTION
65         readFullInventoryLocationInto(L, &act->to_inv, &act->to_list, &act->to_i);
66         return 0;
67 }
68
69 int LuaInventoryAction::l_craft(lua_State *L)
70 {
71         LuaInventoryAction *o = checkobject(L, 1);
72
73         if (o->m_action->getType() != IAction::Craft)
74                 return 0;
75
76         std::string locStr;
77         InventoryLocation loc;
78
79         locStr = readParam<std::string>(L, 2);
80
81         try {
82                 loc.deSerialize(locStr);
83                 dynamic_cast<ICraftAction *>(o->m_action)->craft_inv = loc;
84         } catch (SerializationError &) {
85         }
86
87         return 0;
88 }
89
90 int LuaInventoryAction::l_set_count(lua_State *L)
91 {
92         LuaInventoryAction *o = checkobject(L, 1);
93
94         s16 count = luaL_checkinteger(L, 2);
95
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         }
107
108         return 0;
109 }
110
111 LuaInventoryAction::LuaInventoryAction(const IAction &type) : m_action(nullptr)
112 {
113         switch (type) {
114         case IAction::Move:
115                 m_action = new IMoveAction();
116                 break;
117         case IAction::Drop:
118                 m_action = new IDropAction();
119                 break;
120         case IAction::Craft:
121                 m_action = new ICraftAction();
122                 break;
123         }
124 }
125
126 LuaInventoryAction::~LuaInventoryAction()
127 {
128         delete m_action;
129 }
130
131 void LuaInventoryAction::readFullInventoryLocationInto(
132                 lua_State *L, InventoryLocation *loc, std::string *list, s16 *index)
133 {
134         try {
135                 loc->deSerialize(readParam<std::string>(L, 2));
136                 std::string l = readParam<std::string>(L, 3);
137                 *list = l;
138                 *index = luaL_checkinteger(L, 4) - 1;
139         } catch (SerializationError &) {
140         }
141 }
142
143 int LuaInventoryAction::create_object(lua_State *L)
144 {
145         IAction type;
146         std::string typeStr;
147
148         typeStr = readParam<std::string>(L, 1);
149
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;
156         else
157                 return 0;
158
159         LuaInventoryAction *o = new LuaInventoryAction(type);
160         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
161         luaL_getmetatable(L, className);
162         lua_setmetatable(L, -2);
163         return 1;
164 }
165
166 int LuaInventoryAction::create(lua_State *L, const IAction &type)
167 {
168         LuaInventoryAction *o = new LuaInventoryAction(type);
169         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
170         luaL_getmetatable(L, className);
171         lua_setmetatable(L, -2);
172         return 1;
173 }
174
175 LuaInventoryAction *LuaInventoryAction::checkobject(lua_State *L, int narg)
176 {
177         return *(LuaInventoryAction **)luaL_checkudata(L, narg, className);
178 }
179
180 void LuaInventoryAction::Register(lua_State *L)
181 {
182         lua_newtable(L);
183         int methodtable = lua_gettop(L);
184         luaL_newmetatable(L, className);
185         int metatable = lua_gettop(L);
186
187         lua_pushliteral(L, "__metatable");
188         lua_pushvalue(L, methodtable);
189         lua_settable(L, metatable);
190
191         lua_pushliteral(L, "__index");
192         lua_pushvalue(L, methodtable);
193         lua_settable(L, metatable);
194
195         lua_pushliteral(L, "__gc");
196         lua_pushcfunction(L, gc_object);
197         lua_settable(L, metatable);
198
199         lua_pushliteral(L, "__tostring");
200         lua_pushcfunction(L, mt_tostring);
201         lua_settable(L, metatable);
202
203         lua_pop(L, 1);
204
205         luaL_openlib(L, 0, methods, 0);
206         lua_pop(L, 1);
207
208         lua_register(L, className, create_object);
209 }
210
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}};