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 "shopitem.h"
23
24
#include "units.h"
25
26
#include "resources/iteminfo.h"
27
28
ShopItem::ShopItem(int inventoryIndex, int id,
29
                   int quantity, int price) :
30
    Item(id, 0),
31
    mPrice(price)
32
{
33
    mDisplayName = getInfo().getName() +
34
                        " (" + Units::formatCurrency(mPrice).c_str() + ")";
35
    setInvIndex(inventoryIndex);
36
    addDuplicate(inventoryIndex, quantity);
37
}
38
39
ShopItem::ShopItem (int id, int price) : Item (id, 0), mPrice(price)
40
{
41
    mDisplayName = getInfo().getName() +
42
                        " (" + Units::formatCurrency(mPrice).c_str() + ")";
43
    setInvIndex(-1);
44
    addDuplicate(-1, 0);
45
}
46
47
ShopItem::~ShopItem()
48
{
49
    /** Clear all remaining duplicates on Object destruction. */
50
    while (!mDuplicates.empty())
51
    {
52
        delete mDuplicates.top();
53
        mDuplicates.pop();
54
    }
55
}
56
57
void ShopItem::addDuplicate(int inventoryIndex, int quantity)
58
{
59
    DuplicateItem* di = new DuplicateItem;
60
    di->inventoryIndex = inventoryIndex;
61
    di->quantity = quantity;
62
    mDuplicates.push(di);
63
    mQuantity += quantity;
64
}
65
66
void ShopItem::addDuplicate()
67
{
68
    DuplicateItem* di = new DuplicateItem;
69
    di->inventoryIndex = -1;
70
    di->quantity = 0;
71
    mDuplicates.push(di);
72
}
73
74
int ShopItem::sellCurrentDuplicate(int quantity)
75
{
76
    DuplicateItem* dupl = mDuplicates.top();
77
    int sellCount = quantity <= dupl->quantity ? quantity : dupl->quantity;
78
    dupl->quantity -= sellCount;
79
    mQuantity -= sellCount;
80
    if (dupl->quantity == 0)
81
    {
82
        delete dupl;
83
        mDuplicates.pop();
84
    }
85
    return sellCount;
86
}