1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2004  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#include <algorithm>
23
24
#include "inventory.h"
25
#include "item.h"
26
#include "log.h"
27
28
struct SlotUsed : public std::unary_function<Item*, bool>
29
{
30
    bool operator()(const Item *item) const
31
    {
32
        return item && item->getId() >= 0 && item->getQuantity() > 0;
33
    }
34
};
35
36
Inventory::Inventory(int size):
37
    mSize(size)
38
{
39
    mItems = new Item*[mSize];
40
    std::fill_n(mItems, mSize, (Item*) 0);
41
}
42
43
Inventory::~Inventory()
44
{
45
    for (int i = 0; i < mSize; i++)
46
        delete mItems[i];
47
48
    delete [] mItems;
49
}
50
51
Item *Inventory::getItem(int index) const
52
{
53
    if (index < 0 || index >= mSize || !mItems[index] || mItems[index]->getQuantity() <= 0)
54
        return 0;
55
56
    return mItems[index];
57
}
58
59
Item *Inventory::findItem(int itemId) const
60
{
61
    for (int i = 0; i < mSize; i++)
62
        if (mItems[i] && mItems[i]->getId() == itemId)
63
            return mItems[i];
64
65
    return NULL;
66
}
67
68
void Inventory::addItem(int id, int quantity, bool equipment)
69
{
70
    setItem(getFreeSlot(), id, quantity, equipment);
71
}
72
73
void Inventory::setItem(int index, int id, int quantity, bool equipment)
74
{
75
    if (index < 0 || index >= mSize)
76
    {
77
        logger->log("Warning: invalid inventory index: %d", index);
78
        return;
79
    }
80
81
    if (!mItems[index] && id > 0)
82
    {
83
        Item *item = new Item(id, quantity, equipment);
84
        item->setInvIndex(index);
85
        mItems[index] = item;
86
        mItems[index]->setEquipment(equipment);
87
    }
88
    else if (id > 0)
89
    {
90
        mItems[index]->setId(id);
91
        mItems[index]->setQuantity(quantity);
92
        mItems[index]->setEquipment(equipment);
93
    }
94
    else if (mItems[index])
95
    {
96
        removeItemAt(index);
97
    }
98
}
99
100
void Inventory::clear()
101
{
102
    for (int i = 0; i < mSize; i++)
103
        removeItemAt(i);
104
}
105
106
void Inventory::removeItem(int id)
107
{
108
    for (int i = 0; i < mSize; i++)
109
        if (mItems[i] && mItems[i]->getId() == id)
110
            removeItemAt(i);
111
}
112
113
void Inventory::removeItemAt(int index)
114
{
115
    delete mItems[index];
116
    mItems[index] = 0;
117
}
118
119
bool Inventory::contains(Item *item) const
120
{
121
    for (int i = 0; i < mSize; i++)
122
        if (mItems[i] && mItems[i]->getId() == item->getId())
123
            return true;
124
125
    return false;
126
}
127
128
int Inventory::getFreeSlot() const
129
{
130
    Item **i = std::find_if(mItems, mItems + mSize,
131
            std::not1(SlotUsed()));
132
    return (i == mItems + mSize) ? -1 : (i - mItems);
133
}
134
135
int Inventory::getNumberOfSlotsUsed() const
136
{
137
    return count_if(mItems, mItems + mSize, SlotUsed());
138
}
139
140
int Inventory::getLastUsedSlot() const
141
{
142
    for (int i = mSize - 1; i >= 0; i--)
143
        if (SlotUsed()(mItems[i]))
144
            return i;
145
146
    return -1;
147
}