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