-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.cpp
149 lines (134 loc) · 2.73 KB
/
container.cpp
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
#include "container.h"
#include "game.h"
#include "player.h"
extern Game Game;
Container::Container(unsigned int _type) : Item(_type)
{
maxSize = 30;////////////////////
totalWeight = 0;
}
Container::~Container()
{
itemList.clear();
}
Container* Container::getParentContainer()
{
if(parent && dynamic_cast<Container*>(parent))
{
return dynamic_cast<Container*>(parent);
}
return NULL;
}
void Container::addItem(Item* item)
{
item->parent = this;
itemList.push_back(item);
}
Item* Container::getItem(unsigned char index)
{
size_t n = 0;
for (ItemList::const_iterator cit = getItems(); cit != getEnd(); ++cit)
{
if(n == index)
return *cit;
else
++n;
}
return NULL;
}
Item* Container::getItemId(unsigned int id, unsigned short count)
{
std::list<Item*>::iterator minCount;
unsigned short minimumCount = 1337;
for(std::list<Item*>::iterator cit = itemList.begin(); cit != itemList.end(); ++cit)
{
if((*cit)->mID == id && minimumCount > (*cit)->count)
{
minCount = cit;
minimumCount = (*cit)->count;
}
}
if(minimumCount != 1337)
return *minCount;
else
return NULL;
}
unsigned char Container::getItemHoldingCount()
{
unsigned char counter = 0;
for(std::list<Item*>::const_iterator iter = itemList.begin(); iter != itemList.end(); ++iter)
{
++counter;
}
return counter;
}
bool Container::isHoldingItem(Item* item)
{
for(std::list<Item*>::const_iterator cit = itemList.begin(); cit != itemList.end(); ++cit)
{
if(*cit == item)
return true;
}
return false;
}
bool Container::isHoldingItemId(unsigned int id, unsigned short count)
{
for(std::list<Item*>::const_iterator cit = itemList.begin(); cit != itemList.end(); ++cit)
{
if((*cit)->mID == id && (*cit)->count >= count)
return true;
}
return false;
}
void Container::eraseItem(unsigned char index)
{
size_t n = 0;
for (ItemList::const_iterator cit = getItems(); cit != getEnd(); ++cit)
{
if(n == index)
{
itemList.erase(cit);
break;
}
else
++n;
}
}
void Container::eraseItem(Item *item)
{
for (ItemList::const_iterator cit = getItems(); cit != getEnd(); ++cit)
{
if(((*cit)->mID == item->mID) && ((*cit)->count == item->count))
{
itemList.erase(cit);
break;
}
}
}
void Container::eraseItemId(unsigned int id, unsigned short count)
{
for (ItemList::const_iterator cit = getItems(); cit != getEnd(); ++cit)
{
if((*cit)->mID == id)
{
if((*cit)->count == count)
itemList.erase(cit);
else if((*cit)->count > count)
(*cit)->count -= count;
break;
}
}
}
unsigned char Container::getItemIndex(Item *item)
{
unsigned char index = 0;
for (ItemList::const_iterator cit = getItems(); cit != getEnd(); ++cit)
{
if(((*cit) == item))
{
return index;
}
index++;
}
return 255;
}