summaryrefslogtreecommitdiff
path: root/src/itemmanager.cpp
blob: d30fd2764de7b85ce58faa752910c55e1c8405c5 (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
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World 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.
 *
 *  The Mana World 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 The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id:$
 */

#include "itemmanager.h"

#include "resourcemanager.h"
#include "utils/logger.h"
#include <libxml/tree.h>

#define READ_PROP(node, prop, name, target, cast) \
        prop = xmlGetProp(node, BAD_CAST name); \
        if (prop) { \
            target = cast((const char*)prop); \
            xmlFree(prop); \
        }

ItemManager::ItemManager(const std::string &itemReferenceFile)
{
    ResourceManager *resman = ResourceManager::getInstance();
    int size;
    char *data = (char*)resman->loadFile(itemReferenceFile, size);

    if (!data) {
        LOG_ERROR("Item Manager: Could not find " << itemReferenceFile << "!", 0);
        free(data);
    }
    else
    {
        xmlDocPtr doc = xmlParseMemory(data, size);
        free(data);

        if (!doc)
        {
            LOG_ERROR("Item Manager: Error while parsing item database ("
            << itemReferenceFile << ")!", 0);
        }
        else
        {
            xmlNodePtr node = xmlDocGetRootElement(doc);
            if (!node || !xmlStrEqual(node->name, BAD_CAST "items"))
            {
                LOG_ERROR("Item Manager: " << itemReferenceFile
                << " is not a valid database file!", 0);
            }
            else
            {
                LOG_INFO("Loading item reference...", 0);
                unsigned int nbItems = 0;
                for (node = node->xmlChildrenNode; node != NULL; node = node->next)
                {
                    // Properties
                    unsigned int id = 0;
                    unsigned short itemType = 0;
                    unsigned int weight = 0;
                    unsigned int value = 0;
                    std::string scriptName = "";
                    Modifiers modifiers;

                    if (!xmlStrEqual(node->name, BAD_CAST "item")) {
                        continue;
                    }

                    xmlChar *prop = NULL;
                    // Properties
                    READ_PROP(node, prop, "id", id, atoi);
                    READ_PROP(node, prop, "type", itemType, atoi);
                    READ_PROP(node, prop, "weight", weight, atoi);
                    READ_PROP(node, prop, "value", value, atoi);
                    READ_PROP(node, prop, "script_name", scriptName, );

                    // --- Modifiers
                    // General
                    READ_PROP(node, prop, "element", modifiers.element,
                    (Element)atoi);
                    READ_PROP(node, prop, "lifetime", modifiers.lifetime, atoi);
                    // Raw Statistics
                    READ_PROP(node, prop, "strength",
                    modifiers.rawStats[STAT_STRENGTH], atoi);
                    READ_PROP(node, prop, "agility",
                    modifiers.rawStats[STAT_AGILITY], atoi);
                    READ_PROP(node, prop, "vitality",
                    modifiers.rawStats[STAT_VITALITY], atoi);
                    READ_PROP(node, prop, "intelligence",
                    modifiers.rawStats[STAT_INTELLIGENCE], atoi);
                    READ_PROP(node, prop, "dexterity",
                    modifiers.rawStats[STAT_DEXTERITY], atoi);
                    READ_PROP(node, prop, "luck",
                    modifiers.rawStats[STAT_LUCK], atoi);
                    // Computed Statistics
                    READ_PROP(node, prop, "heat",
                    modifiers.computedStats[STAT_HEAT], atoi);
                    READ_PROP(node, prop, "attack",
                    modifiers.computedStats[STAT_ATTACK], atoi);
                    READ_PROP(node, prop, "defence",
                    modifiers.computedStats[STAT_DEFENCE], atoi);
                    READ_PROP(node, prop, "magic",
                    modifiers.computedStats[STAT_MAGIC], atoi);
                    READ_PROP(node, prop, "accuracy",
                    modifiers.computedStats[STAT_ACCURACY], atoi);
                    READ_PROP(node, prop, "speed",
                    modifiers.computedStats[STAT_SPEED], atoi);
                    // Main Values
                    READ_PROP(node, prop, "hp", modifiers.hp, atoi);
                    READ_PROP(node, prop, "mp", modifiers.mp, atoi);
                    // Equipment
                    READ_PROP(node, prop, "range", modifiers.range, atoi);
                    // Status effect
                    READ_PROP(node, prop, "status_effect",
                    modifiers.beingStateEffect, (BeingStateEffect)atoi);

                    // Checks
                    if (id != 0)
                    {
                        ItemPtr item(new Item(modifiers, itemType, weight,
                                            value, scriptName));
                        mItemReference[id] = item;
                        nbItems++;
                    }

                    if (id == 0)
                    {
                        LOG_WARN("Item Manager: An (ignored) item has no ID in "
                        << itemReferenceFile << "!", 0);
                    }
                    if (weight == 0)
                    {
                        LOG_WARN("Item Manager: Missing weight for item: "
                        << id << " in " << itemReferenceFile << ".", 0);
                    }

                    LOG_INFO("Item: ID: " << id << ", itemType: " << itemType
                    << ", weight: " << weight << ", value: " << value <<
                    ", scriptName: " << scriptName << ".", 3);
                    //TODO: Log level 5 with everything
                }

                LOG_INFO("Loaded " << nbItems << " items from "
                  << itemReferenceFile << ".", 0);

            } // End if node "items"

            xmlFreeDoc(doc);

        } // End if doc?
    } // End if data?
}

ItemManager::~ItemManager()
{
    mItemReference.clear();
}