summaryrefslogtreecommitdiff
path: root/src/resources/attributes.cpp
blob: 58bff6f123ae75a9412ddb365054fd8ccbe0eb43 (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 *  The Mana Client
 *  Copyright (C) 2010-2013  The Mana Developers
 *
 *  This file is part of The Mana Client.
 *
 *  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 2 of the License, or
 *  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/>.
 */

#include "resources/attributes.h"

#include "log.h"
#include "playerinfo.h"

#include "gui/statuswindow.h"

#include "resources/itemdb.h"

#include "utils/gettext.h"
#include "utils/stringutils.h"

#include <list>
#include <map>

#define DEFAULT_ATTRIBUTESDB_FILE "attributes.xml"
#define DEFAULT_POINTS 30
#define DEFAULT_MIN_PTS 1
#define DEFAULT_MAX_PTS 9

namespace Attributes {

    typedef struct
    {
        unsigned int id;
        std::string name;
        std::string description;
        /** Whether the attribute value can be modified by the player */
        bool modifiable;
        /**< Attribute scope. */
        std::string scope;
        /** The playerInfo core Id the attribute is linked with or -1 if not */
        int playerInfoId;
    } Attribute;

    /** Map for attributes. */
    typedef std::map<unsigned int, Attribute> AttributeMap;
    static AttributeMap attributes;

    /** tags = effects on attributes. */
    typedef std::map< std::string, std::string > TagMap;
    static TagMap tags;

    /** List of modifiable attribute names used at character's creation. */
    static std::vector<std::string> attributeLabels;

    /** Characters creation points. */
    static unsigned int creationPoints = DEFAULT_POINTS;
    static unsigned int attributeMinimum = DEFAULT_MIN_PTS;
    static unsigned int attributeMaximum = DEFAULT_MAX_PTS;

    unsigned int getCreationPoints()
    {
        return creationPoints;
    }

    unsigned int getAttributeMinimum()
    {
        return attributeMinimum;
    }

    unsigned int getAttributeMaximum()
    {
        return attributeMaximum;
    }

    std::vector<std::string>& getLabels()
    {
        return attributeLabels;
    }

    /**
     * Fills the list of base attribute labels.
     */
    static void fillLabels()
    {
        // Fill up the modifiable attribute label list.
        attributeLabels.clear();
        AttributeMap::const_iterator it, it_end;
        for (it = attributes.begin(), it_end = attributes.end(); it != it_end;
                                                                           it++)
        {
            if (it->second.modifiable &&
               (it->second.scope == "character" || it->second.scope == "being"))
                attributeLabels.push_back(it->second.name + ":");
        }
    }

    /**
     * Fills the list of base attribute labels.
     */
    static int getPlayerInfoIdFromAttrType(std::string attrType)
    {
        toLower(attrType);
        if (attrType == "level")
            return ::LEVEL;
        else if (attrType == "hp")
            return ::HP;
        else if (attrType == "max-hp")
            return ::MAX_HP;
        else if (attrType == "mp")
            return ::MP;
        else if (attrType == "max-mp")
            return ::MAX_MP;
        else if (attrType == "exp")
            return ::EXP;
        else if (attrType == "exp-needed")
            return ::EXP_NEEDED;
        else if (attrType == "money")
            return ::MONEY;
        else if (attrType == "total-weight")
            return ::TOTAL_WEIGHT;
        else if (attrType == "max-weight")
            return ::MAX_WEIGHT;
        else if (attrType == "skill-points")
            return ::SKILL_POINTS;
        else if (attrType == "char-points")
            return ::CHAR_POINTS;
        else if (attrType == "corr-points")
            return ::CORR_POINTS;
        else if (attrType == "none")
            return -2; // Used to hide the attribute display.

        return -1; // Not linked to a playerinfo stat.
    }

    int getPlayerInfoIdFromAttrId(int attrId)
    {
        AttributeMap::const_iterator it = attributes.find(attrId);

        if (it != attributes.end())
        {
            return it->second.playerInfoId;
        }

        return -1;
    }

    static void loadBuiltins()
    {
        {
            Attribute a;
            a.id = 16;
            a.name = _("Strength");
            a.description = "";
            a.modifiable = true;
            a.scope = "character";
            a.playerInfoId = -1;

            attributes[a.id] = a;
            tags.insert(std::make_pair("str", _("Strength %+.1f")));
        }

        {
            Attribute a;
            a.id = 17;
            a.name = _("Agility");
            a.description = "";
            a.modifiable = true;
            a.scope = "character";
            a.playerInfoId = -1;

            attributes[a.id] = a;
            tags.insert(std::make_pair("agi", _("Agility %+.1f")));
        }

        {
            Attribute a;
            a.id = 18;
            a.name = _("Dexterity");
            a.description = "";
            a.modifiable = true;
            a.scope = "character";
            a.playerInfoId = -1;

            attributes[a.id] = a;
            tags.insert(std::make_pair("dex", _("Dexterity %+.1f")));
        }

        {
            Attribute a;
            a.id = 19;
            a.name = _("Vitality");
            a.description = "";
            a.modifiable = true;
            a.scope = "character";
            a.playerInfoId = -1;

            attributes[a.id] = a;
            tags.insert(std::make_pair("vit", _("Vitality %+.1f")));
        }

        {
            Attribute a;
            a.id = 20;
            a.name = _("Intelligence");
            a.description = "";
            a.modifiable = true;
            a.scope = "character";
            a.playerInfoId = -1;

            attributes[a.id] = a;
            tags.insert(std::make_pair("int", _("Intelligence %+.1f")));
        }

        {
            Attribute a;
            a.id = 21;
            a.name = _("Willpower");
            a.description = "";
            a.modifiable = true;
            a.scope = "character";
            a.playerInfoId = -1;

            attributes[a.id] = a;
            tags.insert(std::make_pair("wil", _("Willpower %+.1f")));
        }
    }

    void init()
    {
        if (attributes.size())
            unload();
    }

    /**
     * Read attribute node
     */
    void readAttributeNode(xmlNodePtr node, const std::string &filename)
    {
        int id = XML::getProperty(node, "id", 0);

        if (!id)
        {
            logger->log("Attributes: Invalid or missing stat ID in "
                        DEFAULT_ATTRIBUTESDB_FILE "!");
            return;
        }
        else if (attributes.find(id) != attributes.end())
        {
            logger->log("Attributes: Redefinition of stat ID %d", id);
        }

        std::string name = XML::getProperty(node, "name", "");

        if (name.empty())
        {
            logger->log("Attributes: Invalid or missing stat name in "
                        DEFAULT_ATTRIBUTESDB_FILE "!");
            return;
        }

        // Create the attribute.
        Attribute a;
        a.id = id;
        a.name = name;
        a.description = XML::getProperty(node, "desc", "");
        a.modifiable = XML::getBoolProperty(node, "modifiable", false);
        a.scope = XML::getProperty(node, "scope", "none");
        a.playerInfoId = getPlayerInfoIdFromAttrType(
                         XML::getProperty(node, "player-info", ""));

        attributes[id] = a;

        unsigned int count = 0;
        for_each_xml_child_node(effectNode, node)
        {
            if (!xmlStrEqual(effectNode->name, BAD_CAST "modifier"))
                 continue;
            ++count;
            std::string tag = XML::getProperty(effectNode, "tag", "");
            if (tag.empty())
            {
                if (name.empty())
                {
                    logger->log("Attribute modifier in attribute %u:%s: "
                                "Empty name definition "
                                "on empty tag definition, skipping.",
                                a.id, a.name.c_str());
                    --count;
                    continue;
                }
                tag = name.substr(0, name.size() > 3 ? 3 : name.size());
                tag = toLower(tag) + toString(count);
             }

            std::string effect = XML::getProperty(effectNode, "effect", "");
            if (effect.empty())
             {
                if (name.empty())
                {
                    logger->log("Attribute modifier in attribute %u:%s: "
                                "Empty name definition "
                                "on empty effect definition, skipping.",
                                a.id, a.name.c_str());
                    --count;
                    continue;
                }
                else
                    effect = name + " %+f";
             }
            tags.insert(std::make_pair(tag, effect));
         }
        logger->log("Found %d tags for attribute %d.", count, id);

    }

    /**
     * Read points node
     */
    void readPointsNode(xmlNodePtr node, const std::string &filename)
    {
        creationPoints = XML::getProperty(node, "start",DEFAULT_POINTS);
        attributeMinimum = XML::getProperty(node, "minimum",
                                                       DEFAULT_MIN_PTS);
        attributeMaximum = XML::getProperty(node, "maximum",
                                                       DEFAULT_MAX_PTS);
        logger->log("Loaded points: start: %i, min: %i, max: %i.",
                    creationPoints, attributeMinimum, attributeMaximum);
    }

    /**
     * Check if all the data loaded by readPointsNode and readAttributeNode is ok
     */
    void checkStatus()
    {
        logger->log("Found %d tags for %d attributes.", int(tags.size()),
                                                        int(attributes.size()));

        if (attributes.size() == 0)
        {
            loadBuiltins();
        }

        fillLabels();

        // Sanity checks on starting points
        float modifiableAttributeCount = (float) attributeLabels.size();
        float averageValue = ((float) creationPoints) / modifiableAttributeCount;
        if (averageValue > attributeMaximum || averageValue < attributeMinimum
            || creationPoints < 1)
        {
            logger->log("Attributes: Character's point values make "
                        "the character's creation impossible. "
                        "Switch back to defaults.");
            creationPoints = DEFAULT_POINTS;
            attributeMinimum = DEFAULT_MIN_PTS;
            attributeMaximum = DEFAULT_MAX_PTS;
        }
    }

    void unload()
    {
        attributes.clear();
    }

    void informItemDB()
    {
        std::list<ItemStat> dbStats;

        TagMap::const_iterator it, it_end;
        for (it = tags.begin(), it_end = tags.end(); it != it_end; ++it)
            dbStats.push_back(ItemStat(it->first, it->second));

        setStatsList(dbStats);
    }

    void informStatusWindow()
    {
        AttributeMap::const_iterator it, it_end;
        for (it = attributes.begin(), it_end = attributes.end(); it != it_end;
                                                                           it++)
        {
            if (it->second.playerInfoId == -1 &&
                (it->second.scope == "character" || it->second.scope == "being"))
            {
                statusWindow->addAttribute(it->second.id,
                                           it->second.name,
                                           it->second.modifiable,
                                           it->second.description);
            }
        }
    }

} // namespace Attributes