-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.hpp
221 lines (182 loc) · 5.86 KB
/
inventory.hpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
The MIT License (MIT)
Copyright (c) 2013 Daniel Mansfield
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef INVENTORY_HPP
#define INVENTORY_HPP
#include "item.hpp"
#include "weapon.hpp"
#include <list>
#include <utility>
#include <iostream>
class Inventory
{
public:
// Whilst weapons and armour are also items, they have their own
// specific properties and so cannot be stored inside the same
// list as the items. We use a list and not a vector as inventories
// are highly mutable. This way they can also be efficiently sorted
// The first element of the pair stores a pointer to the item in
// the item/weapon/armour atlas, defined in main(), and the second
// element stores the quantity of the item
std::list<std::pair<Item*, int>> items;
std::list<std::pair<Weapon*, int>> weapons;
Inventory()
{
}
Inventory(std::list<std::pair<Item*, int>> items,
std::list<std::pair<Weapon*, int>> weapons)
{
this->items = items;
this->weapons = weapons;
}
// Remove all items from the inventory, destroying them in the process
// (They remain in the atlas though)
void clear()
{
this->items.clear();
this->weapons.clear();
}
// Add an item to the inventory, specified by a pointer to it
// from the item atlas (technically does not need to point there,
// but it should anyway)
void add_item(Item* item, int count)
{
// Perform the same operation as merging, but for a single item
for(auto& it : this->items)
{
if(it.first == item)
{
it.second += count;
return;
}
}
// If the item doesn't already exist in the inventory, then a
// pair must be created too
this->items.push_back(std::make_pair(item, count));
}
// Same as for items
void add_weapon(Weapon* weapon, int count)
{
for(auto& it : this->weapons)
{
if(it.first == weapon)
{
it.second += count;
return;
}
}
this->weapons.push_back(std::make_pair(weapon, count));
}
// Remove the specified number of items from the inventory
void remove_item(Item* item, int count)
{
// Iterate through the items, and if they are found then decrease
// the quantity by the quantity removed
for(auto& it : this->items)
{
if(it.first == item) it.second -= count;
}
// Iterate through the list again, and remove any elements from
// the list that have zero or less for their quantity
// We do this in two passes because removing an element from
// a list during a for loop invalidates the iterators, and the
// loop stops working
this->items.remove_if([](std::pair<Item*, int>& element)
{
return element.second < 1;
});
}
// Same as for items
void remove_weapon(Weapon* weapon, int count)
{
for(auto& it : this->weapons)
{
if(it.first == weapon) it.second -= count;
}
this->weapons.remove_if([](std::pair<Weapon*, int>& element)
{
return element.second < 1;
});
}
// Merge the specified inventory with the current one, adding
// item quantities together if they already exist and adding the item
// into a new slot if they do not
void merge(Inventory* inventory)
{
// You can't merge an inventory with itself!
if(inventory == this) return;
// Loop through the items to be added, and add them. Our addition
// function will take care of everything else for us
for(auto it : inventory->items)
{
this->add_item(it.first, it.second);
}
// Do the same for the weapons
for(auto it : inventory->weapons)
{
this->add_weapon(it.first, it.second);
}
return;
}
// Output a list of the items onto stdout, formatted nicely and
// numbered if required
int print_items(bool label = false)
{
unsigned int i = 1;
for(auto it : this->items)
{
// Number the items if asked
if(label) std::cout << i++ << ": ";
// Output the item name, quantity and description, e.g.
// Gold Piece (29) - Glimmering discs of wealth
std::cout << it.first->name << " (" << it.second << ") - ";
std::cout << it.first->description << std::endl;
}
// Return the number of items outputted, for convenience
return this->items.size();
}
// Same as for items
int print_weapons(bool label = false)
{
unsigned int i = 1;
for(auto it : this->weapons)
{
if(label) std::cout << i++ << ": ";
std::cout << it.first->name << " (" << it.second << ") - ";
std::cout << it.first->description << std::endl;
}
return this->weapons.size();
}
// Print the entire inventory; items, then weapons, then armour,
// but if the inventory is empty then output "Nothing"
void print(bool label = false)
{
if(this->items.size() == 0 &&
this->weapons.size() == 0)
{
std::cout << "Nothing" << std::endl;
}
else
{
this->print_items(label);
this->print_weapons(label);
}
return;
}
};
#endif /* INVENTORY_HPP */