Minetest  5.4.0
inventorymanager.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 #pragma once
21 
22 #include "inventory.h"
23 #include <iostream>
24 #include <string>
25 class ServerActiveObject;
26 
28 {
29  enum Type{
35  } type;
36 
37  std::string name; // PLAYER, DETACHED
38  v3s16 p; // NODEMETA
39 
41  {
42  setUndefined();
43  }
44  void setUndefined()
45  {
46  type = UNDEFINED;
47  }
49  {
51  }
52  void setPlayer(const std::string &name_)
53  {
54  type = PLAYER;
55  name = name_;
56  }
57  void setNodeMeta(const v3s16 &p_)
58  {
59  type = NODEMETA;
60  p = p_;
61  }
62  void setDetached(const std::string &name_)
63  {
64  type = DETACHED;
65  name = name_;
66  }
67 
68  bool operator==(const InventoryLocation &other) const
69  {
70  if(type != other.type)
71  return false;
72  switch(type){
73  case UNDEFINED:
74  return false;
75  case CURRENT_PLAYER:
76  return true;
77  case PLAYER:
78  return (name == other.name);
79  case NODEMETA:
80  return (p == other.p);
81  case DETACHED:
82  return (name == other.name);
83  }
84  return false;
85  }
86  bool operator!=(const InventoryLocation &other) const
87  {
88  return !(*this == other);
89  }
90 
91  void applyCurrentPlayer(const std::string &name_)
92  {
93  if(type == CURRENT_PLAYER)
94  setPlayer(name_);
95  }
96 
97  std::string dump() const;
98  void serialize(std::ostream &os) const;
99  void deSerialize(std::istream &is);
100  void deSerialize(const std::string &s);
101 };
102 
103 struct InventoryAction;
104 
106 {
107 public:
108  InventoryManager() = default;
109  virtual ~InventoryManager() = default;
110 
111  // Get an inventory (server and client)
112  virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
113  // Set modified (will be saved and sent over network; only on server)
114  virtual void setInventoryModified(const InventoryLocation &loc) {}
115  // Send inventory action to server (only on client)
116  virtual void inventoryAction(InventoryAction *a){}
117 };
118 
119 enum class IAction : u16 {
120  Move,
121  Drop,
122  Craft
123 };
124 
126 {
127  static InventoryAction *deSerialize(std::istream &is);
128 
129  virtual IAction getType() const = 0;
130  virtual void serialize(std::ostream &os) const = 0;
131  virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
132  IGameDef *gamedef) = 0;
133  virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
134  virtual ~InventoryAction() = default;;
135 };
136 
138 {
140  std::string from_list;
141  s16 from_i = -1;
143  std::string to_list;
144  s16 to_i = -1;
145 };
146 
147 struct IMoveAction : public InventoryAction, public MoveAction
148 {
149  // count=0 means "everything"
150  u16 count = 0;
151  bool move_somewhere = false;
152 
153  // treat these as private
154  // related to movement to somewhere
156  u32 move_count = 0;
157 
158  IMoveAction() = default;
159 
160  IMoveAction(std::istream &is, bool somewhere);
161 
162  IAction getType() const
163  {
164  return IAction::Move;
165  }
166 
167  void serialize(std::ostream &os) const
168  {
169  if (!move_somewhere)
170  os << "Move ";
171  else
172  os << "MoveSomewhere ";
173  os << count << " ";
174  os << from_inv.dump() << " ";
175  os << from_list << " ";
176  os << from_i << " ";
177  os << to_inv.dump() << " ";
178  os << to_list;
179  if (!move_somewhere)
180  os << " " << to_i;
181  }
182 
183  void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
184 
185  void clientApply(InventoryManager *mgr, IGameDef *gamedef);
186 
187  void swapDirections();
188 
189  void onPutAndOnTake(const ItemStack &src_item, ServerActiveObject *player) const;
190 
191  void onMove(int count, ServerActiveObject *player) const;
192 
193  int allowPut(const ItemStack &dst_item, ServerActiveObject *player) const;
194 
195  int allowTake(const ItemStack &src_item, ServerActiveObject *player) const;
196 
197  int allowMove(int try_take_count, ServerActiveObject *player) const;
198 };
199 
200 struct IDropAction : public InventoryAction, public MoveAction
201 {
202  // count=0 means "everything"
203  u16 count = 0;
204 
205  IDropAction() = default;
206 
207  IDropAction(std::istream &is);
208 
209  IAction getType() const
210  {
211  return IAction::Drop;
212  }
213 
214  void serialize(std::ostream &os) const
215  {
216  os<<"Drop ";
217  os<<count<<" ";
218  os<<from_inv.dump()<<" ";
219  os<<from_list<<" ";
220  os<<from_i;
221  }
222 
223  void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
224 
225  void clientApply(InventoryManager *mgr, IGameDef *gamedef);
226 };
227 
229 {
230  // count=0 means "everything"
231  u16 count = 0;
233 
234  ICraftAction() = default;
235 
236  ICraftAction(std::istream &is);
237 
238  IAction getType() const
239  {
240  return IAction::Craft;
241  }
242 
243  void serialize(std::ostream &os) const
244  {
245  os<<"Craft ";
246  os<<count<<" ";
247  os<<craft_inv.dump()<<" ";
248  }
249 
250  void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
251 
252  void clientApply(InventoryManager *mgr, IGameDef *gamedef);
253 };
254 
255 // Crafting helper
256 bool getCraftingResult(Inventory *inv, ItemStack &result,
257  std::vector<ItemStack> &output_replacements,
258  bool decrementInput, IGameDef *gamedef);
Definition: gamedef.h:49
Definition: inventorymanager.h:106
virtual void inventoryAction(InventoryAction *a)
Definition: inventorymanager.h:116
virtual void setInventoryModified(const InventoryLocation &loc)
Definition: inventorymanager.h:114
InventoryManager()=default
virtual Inventory * getInventory(const InventoryLocation &loc)
Definition: inventorymanager.h:112
virtual ~InventoryManager()=default
Definition: inventory.h:282
Definition: serveractiveobject.h:52
bool getCraftingResult(Inventory *inv, ItemStack &result, std::vector< ItemStack > &output_replacements, bool decrementInput, IGameDef *gamedef)
Definition: inventorymanager.cpp:957
IAction
Definition: inventorymanager.h:119
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
Definition: inventorymanager.h:229
ICraftAction()=default
void serialize(std::ostream &os) const
Definition: inventorymanager.h:243
IAction getType() const
Definition: inventorymanager.h:238
u16 count
Definition: inventorymanager.h:231
void clientApply(InventoryManager *mgr, IGameDef *gamedef)
Definition: inventorymanager.cpp:949
void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
Definition: inventorymanager.cpp:840
InventoryLocation craft_inv
Definition: inventorymanager.h:232
Definition: inventorymanager.h:201
void serialize(std::ostream &os) const
Definition: inventorymanager.h:214
IAction getType() const
Definition: inventorymanager.h:209
void clientApply(InventoryManager *mgr, IGameDef *gamedef)
Definition: inventorymanager.cpp:798
void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
Definition: inventorymanager.cpp:650
u16 count
Definition: inventorymanager.h:203
IDropAction()=default
Definition: inventorymanager.h:148
int allowMove(int try_take_count, ServerActiveObject *player) const
Definition: inventorymanager.cpp:229
void onPutAndOnTake(const ItemStack &src_item, ServerActiveObject *player) const
Definition: inventorymanager.cpp:164
void clientApply(InventoryManager *mgr, IGameDef *gamedef)
Definition: inventorymanager.cpp:599
void swapDirections()
Definition: inventorymanager.cpp:157
void onMove(int count, ServerActiveObject *player) const
Definition: inventorymanager.cpp:186
u32 move_count
Definition: inventorymanager.h:156
IMoveAction()=default
IAction getType() const
Definition: inventorymanager.h:162
int allowPut(const ItemStack &dst_item, ServerActiveObject *player) const
Definition: inventorymanager.cpp:199
bool move_somewhere
Definition: inventorymanager.h:151
int allowTake(const ItemStack &src_item, ServerActiveObject *player) const
Definition: inventorymanager.cpp:214
void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
Definition: inventorymanager.cpp:244
void serialize(std::ostream &os) const
Definition: inventorymanager.h:167
bool caused_by_move_somewhere
Definition: inventorymanager.h:155
u16 count
Definition: inventorymanager.h:150
Definition: inventorymanager.h:126
virtual IAction getType() const =0
virtual void serialize(std::ostream &os) const =0
virtual ~InventoryAction()=default
virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef)=0
virtual void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)=0
static InventoryAction * deSerialize(std::istream &is)
Definition: inventorymanager.cpp:106
Definition: inventorymanager.h:28
void setCurrentPlayer()
Definition: inventorymanager.h:48
std::string dump() const
Definition: inventorymanager.cpp:38
enum InventoryLocation::Type type
bool operator!=(const InventoryLocation &other) const
Definition: inventorymanager.h:86
void setUndefined()
Definition: inventorymanager.h:44
void setNodeMeta(const v3s16 &p_)
Definition: inventorymanager.h:57
InventoryLocation()
Definition: inventorymanager.h:40
void setPlayer(const std::string &name_)
Definition: inventorymanager.h:52
v3s16 p
Definition: inventorymanager.h:38
void serialize(std::ostream &os) const
Definition: inventorymanager.cpp:45
void applyCurrentPlayer(const std::string &name_)
Definition: inventorymanager.h:91
bool operator==(const InventoryLocation &other) const
Definition: inventorymanager.h:68
void deSerialize(std::istream &is)
Definition: inventorymanager.cpp:68
std::string name
Definition: inventorymanager.h:37
void setDetached(const std::string &name_)
Definition: inventorymanager.h:62
Type
Definition: inventorymanager.h:29
@ CURRENT_PLAYER
Definition: inventorymanager.h:31
@ DETACHED
Definition: inventorymanager.h:34
@ UNDEFINED
Definition: inventorymanager.h:30
@ PLAYER
Definition: inventorymanager.h:32
@ NODEMETA
Definition: inventorymanager.h:33
Definition: inventory.h:34
Definition: inventorymanager.h:138
InventoryLocation from_inv
Definition: inventorymanager.h:139
std::string to_list
Definition: inventorymanager.h:143
s16 from_i
Definition: inventorymanager.h:141
InventoryLocation to_inv
Definition: inventorymanager.h:142
s16 to_i
Definition: inventorymanager.h:144
std::string from_list
Definition: inventorymanager.h:140