inventorymanager.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "inventory.h"
  18. #include <iostream>
  19. #include <string>
  20. class ServerActiveObject;
  21. struct InventoryLocation
  22. {
  23. enum Type{
  24. UNDEFINED,
  25. CURRENT_PLAYER,
  26. PLAYER,
  27. NODEMETA,
  28. DETACHED,
  29. } type;
  30. std::string name; // PLAYER, DETACHED
  31. v3s16 p; // NODEMETA
  32. InventoryLocation()
  33. {
  34. setUndefined();
  35. }
  36. void setUndefined()
  37. {
  38. type = UNDEFINED;
  39. }
  40. void setCurrentPlayer()
  41. {
  42. type = CURRENT_PLAYER;
  43. }
  44. void setPlayer(const std::string &name_)
  45. {
  46. type = PLAYER;
  47. name = name_;
  48. }
  49. void setNodeMeta(const v3s16 &p_)
  50. {
  51. type = NODEMETA;
  52. p = p_;
  53. }
  54. void setDetached(const std::string &name_)
  55. {
  56. type = DETACHED;
  57. name = name_;
  58. }
  59. bool operator==(const InventoryLocation &other) const
  60. {
  61. if(type != other.type)
  62. return false;
  63. switch(type){
  64. case UNDEFINED:
  65. return false;
  66. case CURRENT_PLAYER:
  67. return true;
  68. case PLAYER:
  69. return (name == other.name);
  70. case NODEMETA:
  71. return (p == other.p);
  72. case DETACHED:
  73. return (name == other.name);
  74. }
  75. return false;
  76. }
  77. bool operator!=(const InventoryLocation &other) const
  78. {
  79. return !(*this == other);
  80. }
  81. void applyCurrentPlayer(const std::string &name_)
  82. {
  83. if(type == CURRENT_PLAYER)
  84. setPlayer(name_);
  85. }
  86. std::string dump() const;
  87. void serialize(std::ostream &os) const;
  88. void deSerialize(std::istream &is);
  89. void deSerialize(const std::string &s);
  90. };
  91. struct InventoryAction;
  92. class InventoryManager
  93. {
  94. public:
  95. InventoryManager() = default;
  96. virtual ~InventoryManager() = default;
  97. // Get an inventory (server and client)
  98. virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
  99. // Set modified (will be saved and sent over network; only on server)
  100. virtual void setInventoryModified(const InventoryLocation &loc) {}
  101. // Send inventory action to server (only on client)
  102. virtual void inventoryAction(InventoryAction *a){}
  103. };
  104. enum class IAction : u16 {
  105. Move,
  106. Drop,
  107. Craft
  108. };
  109. struct InventoryAction
  110. {
  111. static InventoryAction *deSerialize(std::istream &is);
  112. virtual IAction getType() const = 0;
  113. virtual void serialize(std::ostream &os) const = 0;
  114. virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
  115. IGameDef *gamedef) = 0;
  116. virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
  117. virtual ~InventoryAction() = default;;
  118. };
  119. struct MoveAction
  120. {
  121. InventoryLocation from_inv;
  122. std::string from_list;
  123. s16 from_i = -1;
  124. InventoryLocation to_inv;
  125. std::string to_list;
  126. s16 to_i = -1;
  127. };
  128. struct IMoveAction : public InventoryAction, public MoveAction
  129. {
  130. // count=0 means "everything"
  131. u16 count = 0;
  132. bool move_somewhere = false;
  133. // treat these as private
  134. // related to movement to somewhere
  135. bool caused_by_move_somewhere = false;
  136. u32 move_count = 0;
  137. IMoveAction() = default;
  138. IMoveAction(std::istream &is, bool somewhere);
  139. IAction getType() const
  140. {
  141. return IAction::Move;
  142. }
  143. void serialize(std::ostream &os) const
  144. {
  145. if (!move_somewhere)
  146. os << "Move ";
  147. else
  148. os << "MoveSomewhere ";
  149. os << count << " ";
  150. os << from_inv.dump() << " ";
  151. os << from_list << " ";
  152. os << from_i << " ";
  153. os << to_inv.dump() << " ";
  154. os << to_list;
  155. if (!move_somewhere)
  156. os << " " << to_i;
  157. }
  158. void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
  159. void clientApply(InventoryManager *mgr, IGameDef *gamedef);
  160. void swapDirections();
  161. void onPutAndOnTake(const ItemStack &src_item, ServerActiveObject *player) const;
  162. void onMove(int count, ServerActiveObject *player) const;
  163. int allowPut(const ItemStack &dst_item, ServerActiveObject *player) const;
  164. int allowTake(const ItemStack &src_item, ServerActiveObject *player) const;
  165. int allowMove(int try_take_count, ServerActiveObject *player) const;
  166. };
  167. struct IDropAction : public InventoryAction, public MoveAction
  168. {
  169. // count=0 means "everything"
  170. u16 count = 0;
  171. IDropAction() = default;
  172. IDropAction(std::istream &is);
  173. IAction getType() const
  174. {
  175. return IAction::Drop;
  176. }
  177. void serialize(std::ostream &os) const
  178. {
  179. os<<"Drop ";
  180. os<<count<<" ";
  181. os<<from_inv.dump()<<" ";
  182. os<<from_list<<" ";
  183. os<<from_i;
  184. }
  185. void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
  186. void clientApply(InventoryManager *mgr, IGameDef *gamedef);
  187. };
  188. struct ICraftAction : public InventoryAction
  189. {
  190. // count=0 means "everything"
  191. u16 count = 0;
  192. InventoryLocation craft_inv;
  193. ICraftAction() = default;
  194. ICraftAction(std::istream &is);
  195. IAction getType() const
  196. {
  197. return IAction::Craft;
  198. }
  199. void serialize(std::ostream &os) const
  200. {
  201. os<<"Craft ";
  202. os<<count<<" ";
  203. os<<craft_inv.dump()<<" ";
  204. }
  205. void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
  206. void clientApply(InventoryManager *mgr, IGameDef *gamedef);
  207. };
  208. // Crafting helper
  209. bool getCraftingResult(Inventory *inv, ItemStack &result,
  210. std::vector<ItemStack> &output_replacements,
  211. bool decrementInput, IGameDef *gamedef);