summaryrefslogtreecommitdiffstats
path: root/sigmod/MapTrainerTeamMember.cpp
blob: ac49965f4a99db0e0b14843e867fb965c4c0211b (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
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
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright 2007-2009 Ben Boeckel <MathStuf@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file sigmod/MapTrainerTeamMember.cpp
 */

// Header include
#include "MapTrainerTeamMember.h"

// Sigmod includes
#include "Game.h"
#include "Item.h"
#include "Macros.h"
#include "MapTrainer.h"
#include "Rules.h"
#include "Species.h"
#include "SpeciesMove.h"

// Qt includes
#include <QtCore/QSet>

using namespace Sigmod;

MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainerTeamMember& teamMember) :
        Object(teamMember.parent(), teamMember.id())
{
    *this = teamMember;
}

MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainer* parent, const int id) :
        Object(parent, id),
        m_species(INT_MAX),
        m_level(INT_MAX)
{
}

MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainerTeamMember& teamMember, const MapTrainer* parent, const int id) :
        Object(parent, id)
{
    *this = teamMember;
}

MapTrainerTeamMember::MapTrainerTeamMember(const QDomElement& xml, const MapTrainer* parent, const int id) :
        Object(parent, id)
{
    LOAD_ID();
    load(xml);
}

void MapTrainerTeamMember::validate()
{
    TEST_BEGIN();
    TEST(species);
    TEST(level);
    if (game()->rules()->maxAbilities() < m_ability.size())
        emit(error("Too many abilities"));
    TEST_LIST(ability);
    if (game()->rules()->maxHeldItems() < m_item.size())
        emit(error("Too many held items"));
    TEST_MAP(item);
    const Species* species = game()->speciesById(m_species);
    if (species && ((species->maxHoldWeight() < heldWeight())))
        emit(error("Cannot carry that much weight"));
    if (game()->rules()->maxMoves() < m_move.size())
        emit(error("Too many moves"));
    TEST_LIST(move);
    if (game()->rules()->maxNatures() < m_nature.size())
        emit(error("Too many natures"));
    TEST_LIST(nature);
    TEST_END();
}

void MapTrainerTeamMember::load(const QDomElement& xml)
{
    LOAD_BEGIN();
    LOAD(species);
    LOAD(level);
    LOAD_LIST(ability);
    LOAD_MAP(item, count);
    LOAD_LIST(move);
    LOAD_LIST(nature);
}

QDomElement MapTrainerTeamMember::save() const
{
    SAVE_CREATE();
    SAVE(species);
    SAVE(level);
    SAVE_LIST(ability);
    SAVE_MAP(item, count);
    SAVE_LIST(move);
    SAVE_LIST(nature);
    return xml;
}

SETTER(MapTrainerTeamMember, int, Species, species)
SETTER(MapTrainerTeamMember, int, Level, level)
SETTER_LIST_LIMIT(MapTrainerTeamMember, Ability, ability, game()->rules()->maxAbilities(), "Cannot have anymore abilities")

void MapTrainerTeamMember::setItem(const int item, const int count)
{
    if (count && itemCheck(item, count) && (!m_item.contains(item) || (count != m_item[item])))
    {
        const QList<int> items = m_item.keys();
        int total = count;
        foreach (int itemCount, items)
        {
            if (itemCount == item)
                continue;
            total += m_item[itemCount];
        }
        if (total <= game()->rules()->maxHeldItems())
        {
            if (checkWeight(item, count))
            {
                m_item[item] = count;
                emit(changed());
            }
            else
                ERROR("Cannot carry that much weight");
        }
        else
            ERROR("Cannot hold that many items");
    }
    else if (!count && m_item.contains(item))
    {
        m_item.remove(item);
        emit(changed());
    }
}

SETTER_LIST_LIMIT(MapTrainerTeamMember, Move, move, game()->rules()->maxMoves(), "Cannot know anymore moves")
SETTER_LIST_LIMIT(MapTrainerTeamMember, Nature, nature, game()->rules()->maxNatures(), "Cannot have anymore natures")

GETTER(MapTrainerTeamMember, int, species)
GETTER(MapTrainerTeamMember, int, level)
GETTER_LIST(MapTrainerTeamMember, ability)
GETTER_MAP(MapTrainerTeamMember, item)
GETTER_LIST(MapTrainerTeamMember, move)
GETTER_LIST(MapTrainerTeamMember, nature)

CHECK_INDEX(MapTrainerTeamMember, int, species, game(), species)
CHECK_BOUNDS(MapTrainerTeamMember, int, level, 1, game()->rules()->maxLevel())
CHECK_INDEX(MapTrainerTeamMember, int, ability, game(), ability)
CHECK_BEGIN_ARRAY(MapTrainerTeamMember, int, item, count, int, item)
    IBOUNDS(item, game(), item);
    TBOUNDS_MOD(item_count, 0, game()->rules()->maxHeldItems(), count)
CHECK_END()
CHECK_BEGIN(MapTrainerTeamMember, int, move)
    IBOUNDS(move, game(), move);
    if (!canLearn(move))
    {
        ERROR("Cannot learn the move");
        return false;
    }
CHECK_END()
CHECK_INDEX(MapTrainerTeamMember, int, nature, game(), nature)

MapTrainerTeamMember& MapTrainerTeamMember::operator=(const MapTrainerTeamMember& rhs)
{
    if (this == &rhs)
        return *this;
    clear();
    COPY(species);
    COPY(level);
    COPY(ability);
    COPY(item);
    COPY(move);
    COPY(nature);
    return *this;
}

int MapTrainerTeamMember::heldWeight(const QMap<int, int>& items) const
{
    int totalWeight = 0;
    QMap<int, int> itemsToCheck = (items.empty() ? m_item : items);
    QList<int> itemIds = itemsToCheck.keys();
    foreach (int itemId, itemIds)
    {
        const Item* item = game()->itemById(itemId);
        if (item)
            totalWeight += itemsToCheck[itemId] * item->weight();
    }
    return totalWeight;
}

bool MapTrainerTeamMember::checkWeight(const int item, const int count) const
{
    const Species* species = game()->speciesById(m_species);
    if (!species)
        return true;
    QMap<int, int> temp = m_item;
    temp[item] = count;
    return (heldWeight(temp) <= species->maxHoldWeight());
}

bool MapTrainerTeamMember::canLearn(const int move) const
{
    const Species* species = game()->speciesById(m_species);
    if (species)
    {
        for (int i = 0; i < species->moveCount(); ++i)
        {
            if (species->move(i)->move() == move)
                return true;
        }
    }
    return !species;
}

void MapTrainerTeamMember::clear()
{
    m_ability.clear();
    m_item.clear();
    m_move.clear();
    m_nature.clear();
}