inventorymanager.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irr_v3d.h"
  18. #include <iostream>
  19. #include <string>
  20. #include <vector>
  21. class ServerActiveObject;
  22. struct ItemStack;
  23. class Inventory;
  24. class IGameDef;
  25. struct InventoryLocation
  26. {
  27. enum Type{
  28. UNDEFINED,
  29. CURRENT_PLAYER,
  30. PLAYER,
  31. NODEMETA,
  32. DETACHED,
  33. } type;
  34. std::string name; // PLAYER, DETACHED
  35. v3s16 p; // NODEMETA
  36. InventoryLocation()
  37. {
  38. setUndefined();
  39. }
  40. void setUndefined()
  41. {
  42. type = UNDEFINED;
  43. }
  44. void setCurrentPlayer()
  45. {
  46. type = CURRENT_PLAYER;
  47. }
  48. void setPlayer(const std::string &name_)
  49. {
  50. type = PLAYER;
  51. name = name_;
  52. }
  53. void setNodeMeta(const v3s16 &p_)
  54. {
  55. type = NODEMETA;
  56. p = p_;
  57. }
  58. void setDetached(const std::string &name_)
  59. {
  60. type = DETACHED;
  61. name = name_;
  62. }
  63. bool operator==(const InventoryLocation &other) const
  64. {
  65. if(type != other.type)
  66. return false;
  67. switch(type){
  68. case UNDEFINED:
  69. return false;
  70. case CURRENT_PLAYER:
  71. return true;
  72. case PLAYER:
  73. return (name == other.name);
  74. case NODEMETA:
  75. return (p == other.p);
  76. case DETACHED:
  77. return (name == other.name);
  78. }
  79. return false;
  80. }
  81. bool operator!=(const InventoryLocation &other) const
  82. {
  83. return !(*this == other);
  84. }
  85. void applyCurrentPlayer(const std::string &name_)
  86. {
  87. if(type == CURRENT_PLAYER)
  88. setPlayer(name_);
  89. }
  90. std::string dump() const;
  91. void serialize(std::ostream &os) const;
  92. void deSerialize(std::istream &is);
  93. void deSerialize(const std::string &s);
  94. };
  95. struct InventoryAction;
  96. class InventoryManager
  97. {
  98. public:
  99. InventoryManager() = default;
  100. virtual ~InventoryManager() = default;
  101. // Get an inventory (server and client)
  102. virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
  103. // Set modified (will be saved and sent over network; only on server)
  104. virtual void setInventoryModified(const InventoryLocation &loc) {}
  105. // Send inventory action to server (only on client)
  106. virtual void inventoryAction(InventoryAction *a){}
  107. };
  108. enum class IAction : u16 {
  109. Move,
  110. Drop,
  111. Craft
  112. };
  113. struct InventoryAction
  114. {
  115. static InventoryAction *deSerialize(std::istream &is);
  116. virtual IAction getType() const = 0;
  117. virtual void serialize(std::ostream &os) const = 0;
  118. virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
  119. IGameDef *gamedef) = 0;
  120. virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
  121. virtual ~InventoryAction() = default;;
  122. };
  123. struct MoveAction
  124. {
  125. InventoryLocation from_inv;
  126. std::string from_list;
  127. s16 from_i = -1;
  128. InventoryLocation to_inv;
  129. std::string to_list;
  130. s16 to_i = -1;
  131. };
  132. struct IMoveAction : public InventoryAction, public MoveAction
  133. {
  134. // count=0 means "everything"
  135. u16 count = 0;
  136. bool move_somewhere = false;
  137. // treat these as private
  138. // related to movement to somewhere
  139. bool caused_by_move_somewhere = false;
  140. u32 move_count = 0;
  141. IMoveAction() = default;
  142. IMoveAction(std::istream &is, bool somewhere);
  143. IAction getType() const
  144. {
  145. return IAction::Move;
  146. }
  147. void serialize(std::ostream &os) const
  148. {
  149. if (!move_somewhere)
  150. os << "Move ";
  151. else
  152. os << "MoveSomewhere ";
  153. os << count << " ";
  154. os << from_inv.dump() << " ";
  155. os << from_list << " ";
  156. os << from_i << " ";
  157. os << to_inv.dump() << " ";
  158. os << to_list;
  159. if (!move_somewhere)
  160. os << " " << to_i;
  161. }
  162. void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
  163. void clientApply(InventoryManager *mgr, IGameDef *gamedef);
  164. void swapDirections();
  165. void onTake(const ItemStack &src_item, ServerActiveObject *player) const;
  166. void onPut(const ItemStack &src_item, ServerActiveObject *player) const;
  167. void onMove(int count, ServerActiveObject *player) const;
  168. int allowPut(const ItemStack &dst_item, ServerActiveObject *player) const;
  169. int allowTake(const ItemStack &src_item, ServerActiveObject *player) const;
  170. int allowMove(int try_take_count, ServerActiveObject *player) const;
  171. };
  172. struct IDropAction : public InventoryAction, public MoveAction
  173. {
  174. // count=0 means "everything"
  175. u16 count = 0;
  176. IDropAction() = default;
  177. IDropAction(std::istream &is);
  178. IAction getType() const
  179. {
  180. return IAction::Drop;
  181. }
  182. void serialize(std::ostream &os) const
  183. {
  184. os<<"Drop ";
  185. os<<count<<" ";
  186. os<<from_inv.dump()<<" ";
  187. os<<from_list<<" ";
  188. os<<from_i;
  189. }
  190. void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
  191. void clientApply(InventoryManager *mgr, IGameDef *gamedef);
  192. };
  193. struct ICraftAction : public InventoryAction
  194. {
  195. // count=0 means "everything"
  196. u16 count = 0;
  197. InventoryLocation craft_inv;
  198. ICraftAction() = default;
  199. ICraftAction(std::istream &is);
  200. IAction getType() const
  201. {
  202. return IAction::Craft;
  203. }
  204. void serialize(std::ostream &os) const
  205. {
  206. os<<"Craft ";
  207. os<<count<<" ";
  208. os<<craft_inv.dump()<<" ";
  209. }
  210. void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
  211. void clientApply(InventoryManager *mgr, IGameDef *gamedef);
  212. };
  213. // Crafting helper
  214. bool getCraftingResult(Inventory *inv, ItemStack &result,
  215. std::vector<ItemStack> &output_replacements,
  216. bool decrementInput, IGameDef *gamedef);