]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_inventoryaction.cpp
add LUA_FCT
[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         return 0;
87 }
88
89 int LuaInventoryAction::l_set_count(lua_State *L)
90 {
91         LuaInventoryAction *o = checkobject(L, 1);
92         
93         s16 count = luaL_checkinteger(L, 2);
94         
95         switch (o->m_action->getType()) {
96         case IAction::Move:
97                 ((IMoveAction *)o->m_action)->count = count;
98                 break;
99         case IAction::Drop:
100                 ((IDropAction *)o->m_action)->count = count;
101                 break;
102         case IAction::Craft:
103                 ((ICraftAction *)o->m_action)->count = count;
104                 break;
105         }
106         
107         return 0;
108 }
109
110 LuaInventoryAction::LuaInventoryAction(const IAction &type) : m_action(nullptr)
111 {
112         switch (type) {
113         case IAction::Move:
114                 m_action = new IMoveAction();
115                 break;
116         case IAction::Drop:
117                 m_action = new IDropAction();
118                 break;
119         case IAction::Craft:
120                 m_action = new ICraftAction();
121                 break;
122         }
123 }
124
125 LuaInventoryAction::~LuaInventoryAction()
126 {
127         delete m_action;
128 }
129
130 void LuaInventoryAction::readFullInventoryLocationInto(lua_State *L, InventoryLocation *loc, std::string *list, s16 *index)
131 {
132         try {
133                 loc->deSerialize(readParam<std::string>(L, 2));
134                 std::string l = readParam<std::string>(L, 3);
135                 *list = l;
136                 *index = luaL_checkinteger(L, 4) - 1;
137         } catch (SerializationError &) {}
138 }
139
140 int LuaInventoryAction::create_object(lua_State *L)
141 {
142         IAction type;
143         std::string typeStr;
144         
145         typeStr = readParam<std::string>(L, 1);
146         
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;
153         else
154                 return 0;
155
156         LuaInventoryAction *o = new LuaInventoryAction(type);
157         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
158         luaL_getmetatable(L, className);
159         lua_setmetatable(L, -2);
160         return 1;
161 }
162
163 int LuaInventoryAction::create(lua_State *L, const IAction &type)
164 {
165         LuaInventoryAction *o = new LuaInventoryAction(type);
166         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
167         luaL_getmetatable(L, className);
168         lua_setmetatable(L, -2);
169         return 1;
170 }
171
172 LuaInventoryAction *LuaInventoryAction::checkobject(lua_State *L, int narg)
173 {
174         return *(LuaInventoryAction **)luaL_checkudata(L, narg, className);
175 }
176
177 void LuaInventoryAction::Register(lua_State *L)
178 {
179         lua_newtable(L);
180         int methodtable = lua_gettop(L);
181         luaL_newmetatable(L, className);
182         int metatable = lua_gettop(L);
183
184         lua_pushliteral(L, "__metatable");
185         lua_pushvalue(L, methodtable);
186         lua_settable(L, metatable);
187
188         lua_pushliteral(L, "__index");
189         lua_pushvalue(L, methodtable);
190         lua_settable(L, metatable);
191
192         lua_pushliteral(L, "__gc");
193         lua_pushcfunction(L, gc_object);
194         lua_settable(L, metatable);
195
196         lua_pushliteral(L, "__tostring");
197         lua_pushcfunction(L, mt_tostring);
198         lua_settable(L, metatable);
199
200         lua_pop(L, 1);
201
202         luaL_openlib(L, 0, methods, 0);
203         lua_pop(L, 1);
204
205         lua_register(L, className, create_object);
206 }
207
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),
215         {0,0}
216 };