blob: 28327a36dfd389daa49bca766c9c3e58277137af (
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
|
/*
core/inventory.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include "core/application.h"
#include "core/inventory.h"
#include "sys/sys.h"
namespace core
{
/* ---- class Inventory -------------------------------------------- */
Inventory::Inventory(const float capacity)
{
inventory_timestamp = 0;
inventory_capacity = capacity;
inventory_capacity_used = 0;
inventory_dirty = false;
}
Inventory::~Inventory()
{
clear();
inventory_timestamp = 0;
inventory_capacity = 0;
inventory_capacity_used = 0;
}
void Inventory::set_capacity(const float capacity)
{
inventory_capacity = capacity;
}
void Inventory::set_timestamp(const unsigned long timestamp)
{
inventory_timestamp = timestamp;
}
void Inventory::set_dirty(const bool dirty)
{
inventory_dirty = dirty;
if (dirty)
recalculate();
}
void Inventory::add(Item *item)
{
for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); it++) {
// check if the item was already added
if ((*it) == item)
return;
}
inventory_items.push_back(item);
}
void Inventory::remove(Item *item)
{
for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); it++) {
if ((*it) == item) {
inventory_items.erase(it);
delete item;
return;
}
}
// FIXME remove doesn't work over network
}
Item *Inventory::find(const Info *info) const
{
// sarch the inventory for a specified item type
for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); it++) {
Item *item = (*it);
if (item->info() == info) {
return item;
}
}
// not found
return 0;
}
void Inventory::clear()
{
for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); it++) {
Item *item = (*it);
delete item;
}
inventory_items.clear();
inventory_capacity_used = 0;
}
void Inventory::recalculate()
{
inventory_capacity_used = 0;
for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); it++) {
const Item *item = (*it);
inventory_capacity_used += item->amount() * item->info()->volume();
}
}
void Inventory::serialize_server_update(std::ostream & os) const
{
os << capacity() << " ";
}
void Inventory::receive_server_update(std::istream &is)
{
is >> inventory_capacity;
}
const long Inventory::max_amount(const long credits, const long price, const float volume) const
{
if ((price * volume) == 0) {
return 0;
}
return math::min((long)(capacity_available() / volume), credits / price);
}
} // namespace core
|