summaryrefslogtreecommitdiff
path: root/src/game-server/itemmanager.cpp
blob: ea060d065758c4450a896ccf1d9caabdccdf287c (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
/*
 *  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 "resourcemanager.h"
#include "game-server/itemmanager.hpp"
#include "utils/logger.h"
#include "utils/xml.hpp"

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

    if (!data) {
        LOG_ERROR("Item Manager: Could not find " << itemReferenceFile << "!");
        free(data);
        return;
    }

    xmlDocPtr doc = xmlParseMemory(data, size);
    free(data);

    if (!doc)
    {
        LOG_ERROR("Item Manager: Error while parsing item database ("
                  << itemReferenceFile << ")!");
        return;
    }

    xmlNodePtr node = xmlDocGetRootElement(doc);
    if (!node || !xmlStrEqual(node->name, BAD_CAST "items"))
    {
        LOG_ERROR("Item Manager: " << itemReferenceFile
                  << " is not a valid database file!");
        xmlFreeDoc(doc);
        return;
    }

    LOG_INFO("Loading item reference...");
    unsigned nbItems = 0;
    for (node = node->xmlChildrenNode; node != NULL; node = node->next)
    {
        if (!xmlStrEqual(node->name, BAD_CAST "item"))
        {
            continue;
        }

        unsigned id = XML::getProperty(node, "id", 0);

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

        int itemType = XML::getProperty(node, "type", 0);
        int weight = XML::getProperty(node, "weight", 0);
        int value = XML::getProperty(node, "value", 0);
        int maxPerSlot = XML::getProperty(node, "max_per_slot", 0);
        std::string scriptName = XML::getProperty(node, "script_name", std::string());

        Modifiers modifiers;
        modifiers.element = XML::getProperty(node, "element", 0);
        modifiers.lifetime = XML::getProperty(node, "lifetime", 0);
        modifiers.stat[Player::STRENGTH]     = XML::getProperty(node, "strength",     0);
        modifiers.stat[Player::AGILITY]      = XML::getProperty(node, "agility",      0);
        modifiers.stat[Player::VITALITY]     = XML::getProperty(node, "vitality",     0);
        modifiers.stat[Player::INTELLIGENCE] = XML::getProperty(node, "intelligence", 0);
        modifiers.stat[Player::DEXTERITY]    = XML::getProperty(node, "dexterity",    0);
        modifiers.stat[Player::WILLPOWER]    = XML::getProperty(node, "willpower",    0);
        modifiers.stat[Player::CHARISMA]     = XML::getProperty(node, "charisma",     0);

        modifiers.stat[Being::STAT_HP_MAXIMUM]              = XML::getProperty(node, "hp",     0);
        modifiers.stat[Being::STAT_PHYSICAL_ATTACK_MINIMUM] = XML::getProperty(node, "attack",     0);
        modifiers.stat[Being::STAT_PHYSICAL_DEFENCE]        = XML::getProperty(node, "defence",     0);


        modifiers.range = XML::getProperty(node, "range", 0);
        modifiers.weaponType = XML::getProperty(node, "weapon_type", 0);
        modifiers.beingStateEffect = XML::getProperty(node, "status_effect", 0);

        ItemClass *item = new ItemClass(id, itemType);
        item->setWeight(weight);
        item->setCost(value);
        item->setMaxPerSlot(maxPerSlot);
        item->setScriptName(scriptName);
        item->setModifiers(modifiers);
        mItemReference[id] = item;
        ++nbItems;

        if (maxPerSlot == 0)
        {
            LOG_WARN("Item Manager: Missing max per slot properties for item: "
                     << id << " in " << itemReferenceFile << ".");
        }
        if (weight == 0)
        {
            LOG_WARN("Item Manager: Missing weight for item: "
                     << id << " in " << itemReferenceFile << ".");
        }

        LOG_DEBUG("Item: ID: " << id << ", itemType: " << itemType
                  << ", weight: " << weight << ", value: " << value <<
                  ", scriptName: " << scriptName << ", maxPerSlot: " << maxPerSlot << ".");
    }

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

    xmlFreeDoc(doc);
}

ItemClass *ItemManager::getItem(int itemId) const
{
    std::map< int, ItemClass * >::const_iterator i = mItemReference.find(itemId);
    return i != mItemReference.end() ? i->second : NULL;
}