Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 4ea835064c9619fe0cfb80d9de86d7961e108245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
   core/inventory.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#ifndef __INCLUDED_CORE_INVENTORY_H__
#define __INCLUDED_CORE_INVENTORY_H__

#include "core/item.h"

#include <vector>

namespace core
{

/**
 * @brief an entity inventory
 */
class Inventory
{
public:
	/**
	 * @brief type definition for items in the inventory
	 */
	typedef std::list<Item *> Items;
	
	/**
	 * @brief default constructor
	 */
	Inventory(const float capacity = 0);
	
	/**
	 * @brief default destructor
	 */
	~Inventory();
	
	/* ---- inspectors ------------------------------------------------- */
	
	/**
	 * @brief items in the inventory
	 */
	inline Items &items() {
		 return inventory_items;
	};

	/**
	 * @brief return the timestamp of the last client-side update
	 * This is a client-side property
	 */
	inline const unsigned long timestamp() const {
		return inventory_timestamp;
	}
	
	/**
	 * @brief return the timestamp of the last item erase
	 * */
	inline const unsigned long timestamp_erase() const {
		return inventory_timestamp_erase;
	}
	
	/**
	 * @brief return the maximal inventory capacity, in cubic meters
	 */
	inline const float capacity() const {
		return inventory_capacity;
	}
	
	/**
	 * @brief return the used inventory capacity, in cubic meters
	 */
	inline const float capacity_used() const {
		return inventory_capacity_used;
	}

	/**
	 * @brief return the available inventory capacity, in cubic meters
	 */
	inline const float capacity_available() const {
		return inventory_capacity - inventory_capacity_used;
	}
	
	inline const bool dirty() const {
		return inventory_dirty;
	}
	
	/**
	 * @brief returns the number of units of an item that can be stored in this inventory
	 * @param credits number of player credits, limits the amount
	 * @param price price of a single unit
	 * @param volume volume of a single unit
	 */
	const long max_amount(const long credits, const long price, const float volume) const;
	
	/**
	  * @brief search the inventory for a specific item type
	  */
	 Item *find(const Info *info) const;
	 
	 /**
	  * @brief search the inventory for a specific item id
	  */
	 Item *find(const unsigned int id) const;
	 
	 /**
	  * @brief search the inventory for a specific item
	  */
	 Item *find(Item *item) const;
	
	/* ---- mutators --------------------------------------------------- */
	
	/**
	 * @brief add an item to the inventory
	 */
	void add(Item *item);
	
	/**
	 * @brief erase an item from the inventory
	 * This will call the Item's destructor
	 */
	void erase(Item *item);
	
	void erase(const unsigned int id);

	/**
	 * @brief removes all items from the inventory and delete them
	 */
	 void clear();
	  	
	/**
	 * @brief set the timestamp
	 */
	 void set_timestamp(const unsigned long timestamp);
	 
	/**
	 * @brief mark the inventory as dirty
	 */	 
	 void set_dirty(const bool dirty = true);
	 
	 /**
	 * @brief set the maximal inventory capacity, in cubic meters
	 */
	 void set_capacity(const float capacity);

 	/// recalculate inventory capacity
	void recalculate();
	
	/// serialize a server-to-client update on a stream
	void serialize_server_update(std::ostream & os) const;

	/// receive a server-to-client update from a stream
	void receive_server_update(std::istream &is);

private:
	// items in the inventory
	Items			inventory_items;
	
	// timestamp, last time the inventory was updated
	unsigned long 		inventory_timestamp;
	// timestamp, last time there was an item deleted from the inventory
	unsigned long		inventory_timestamp_erase;
	
	// maximum inventory capacity, in cubic meters
	float			inventory_capacity;
	
	// current capacity used, in cubic meters
	float			inventory_capacity_used;
	
	bool			inventory_dirty;
};

} // namsepace core

#endif // __INCLUDED_CORE_INVENTORY_H__