summaryrefslogtreecommitdiff
path: root/src/game-server/itemmanager.cpp
blob: 34a6170a9db6df0f101e41e0bd50064ace46b519 (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
/*
 *  The Mana Server
 *  Copyright (C) 2004-2010  The Mana World Development Team
 *
 *  This file is part of The Mana Server.
 *
 *  The Mana Server 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 Server 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 Server.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "game-server/itemmanager.hpp"

#include "defines.h"
#include "common/resourcemanager.hpp"
#include "game-server/attributemanager.hpp"
#include "game-server/item.hpp"
#include "game-server/skillmanager.hpp"
#include "scripting/script.hpp"
#include "utils/logger.h"
#include "utils/xml.hpp"

#include <map>
#include <set>
#include <sstream>

void ItemManager::initialize()
{
    mVisibleEquipSlotCount = 0;
    reload();
}

void ItemManager::reload()
{
    std::string absPathFile;
    xmlNodePtr rootNode;

    // ####################################################################
    // ### Load the equip slots that a character has available to them. ###
    // ####################################################################

    absPathFile = ResourceManager::resolve(mEquipCharSlotReferenceFile);
    if (absPathFile.empty()) {
        LOG_ERROR("Item Manager: Could not find " << mEquipCharSlotReferenceFile << "!");
        return;
    }

    XML::Document doc(absPathFile, int());
    rootNode = doc.rootNode();

    if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "equip-slots"))
    {
        LOG_ERROR("Item Manager: Error while parsing equip slots database ("
                  << absPathFile << ")!");
        return;
    }

    LOG_INFO("Loading equip slots: " << absPathFile);

    {
        unsigned int totalCount = 0, slotCount = 0, visibleSlotCount = 0;
        for_each_xml_child_node(node, rootNode)
        {
            if (xmlStrEqual(node->name, BAD_CAST "slot"))
            {
                std::string name = XML::getProperty(node, "name", "");
                int count = XML::getProperty(node, "count", 0);
                if (name.empty() || !count || count < 0)
                    LOG_WARN("Item Manager: equip slot has no name or zero count");
                else
                {
                    bool visible = XML::getProperty(node, "visible", "false") != "false";
                    if (visible)
                    {
                        visibleEquipSlots.push_back(equipSlots.size());
                        if (++visibleSlotCount > 7)
                            LOG_WARN("Item Manager: More than 7 visible equip slot!"
                                     "This will not work with current netcode!");
                    }
                    equipSlots.push_back(std::pair<std::string, unsigned int>
                                         (name, count));
                    totalCount += count;
                    ++slotCount;
                }
            }
        }
        LOG_INFO("Loaded '" << slotCount << "' slot types with '"
                 << totalCount << "' slots.");
    }

    // ####################################
    // ### Load the main item database. ###
    // ####################################

    absPathFile = ResourceManager::resolve(mItemReferenceFile);
    if (absPathFile.empty()) {
        LOG_ERROR("Item Manager: Could not find " << mItemReferenceFile << "!");
        return;
    }

    XML::Document doc2(absPathFile, int());
    rootNode = doc2.rootNode();

    if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "items"))
    {
        LOG_ERROR("Item Manager: Error while parsing item database ("
                  << absPathFile << ")!");
        return;
    }

    LOG_INFO("Loading item reference: " << absPathFile);

    unsigned nbItems = 0;
    for_each_xml_child_node(node, rootNode)
    {
        if (!xmlStrEqual(node->name, BAD_CAST "item"))
            continue;

        int id = XML::getProperty(node, "id", 0);
        if (id < 1)
        {
            LOG_WARN("Item Manager: Item ID: " << id << " is invalid in "
                     << mItemReferenceFile << ", and will be ignored.");
            continue;
        }


        // Type is mostly unused, but still serves for
        // hairsheets and race sheets.
        std::string sItemType = XML::getProperty(node, "type", "");
        if (sItemType == "hairsprite" || sItemType == "racesprite")
            continue;

        ItemClass *item;
        ItemClasses::iterator i = itemClasses.find(id);

        unsigned int maxPerSlot = XML::getProperty(node, "max-per-slot", 0);
        if (!maxPerSlot)
        {
            LOG_WARN("Item Manager: Missing max-per-slot property for "
                     "item " << id << " in " << mItemReferenceFile << '.');
            maxPerSlot = 1;
        }

        if (i == itemClasses.end())
        {
            item = new ItemClass(id, maxPerSlot);
            itemClasses[id] = item;
        }
        else
        {
            LOG_WARN("Multiple defintions of item '" << id << "'!");
            item = i->second;
        }

        int value = XML::getProperty(node, "value", 0);
        // Should have multiple value definitions for multiple currencies?
        item->mCost = value;

        for_each_xml_child_node(subnode, node)
        {
            if (xmlStrEqual(subnode->name, BAD_CAST "equip"))
            {
                ItemEquipInfo req;
                for_each_xml_child_node(equipnode, subnode)
                    if (xmlStrEqual(equipnode->name, BAD_CAST "slot"))
                    {
                        std::string slot = XML::getProperty(equipnode, "type", "");
                        if (slot.empty())
                        {
                            LOG_WARN("Item Manager: empty equip slot definition!");
                            continue;
                        }
                        req.push_back(std::make_pair(getEquipIdFromName(slot),
                                       XML::getProperty(equipnode, "required",
                                                        1)));
                    }
                if (req.empty())
                {
                    LOG_WARN("Item Manager: empty equip requirement "
                             "definition for item " << id << "!");
                    continue;
                }
                item->mEquip.push_back(req);
            }
            else if (xmlStrEqual(subnode->name, BAD_CAST "effect"))
            {
                std::pair< ItemTriggerType, ItemTriggerType> triggerTypes;
                {
                    std::string triggerName = XML::getProperty(subnode, "trigger", ""),
                             dispellTrigger = XML::getProperty(subnode, "dispell", "");
                    // label -> { trigger (apply), trigger (cancel (default)) }
                    // The latter can be overridden.
                    static std::map<const std::string,
                                    std::pair<ItemTriggerType, ItemTriggerType> >
                                    triggerTable;
                    if (triggerTable.empty())
                    {
                        /*
                         * The following is a table of all triggers for item
                         *     effects.
                         * The first element defines the trigger used for this
                         *     trigger, and the second defines the default
                         *     trigger to use for dispelling.
                         */
                        triggerTable["existence"].first         = ITT_IN_INVY;
                        triggerTable["existence"].second        = ITT_LEAVE_INVY;
                        triggerTable["activation"].first        = ITT_ACTIVATE;
                        triggerTable["activation"].second       = ITT_NULL;
                        triggerTable["equip"].first             = ITT_EQUIP;
                        triggerTable["equip"].second            = ITT_UNEQUIP;
                        triggerTable["leave-inventory"].first   = ITT_LEAVE_INVY;
                        triggerTable["leave-inventory"].second  = ITT_NULL;
                        triggerTable["unequip"].first           = ITT_UNEQUIP;
                        triggerTable["unequip"].second          = ITT_NULL;
                        triggerTable["equip-change"].first      = ITT_EQUIPCHG;
                        triggerTable["equip-change"].second     = ITT_NULL;
                        triggerTable["null"].first              = ITT_NULL;
                        triggerTable["null"].second             = ITT_NULL;
                    }
                    std::map<const std::string, std::pair<ItemTriggerType,
                                                ItemTriggerType> >::iterator
                             it = triggerTable.find(triggerName);

                    if (it == triggerTable.end()) {
                        LOG_WARN("Item Manager: Unable to find effect trigger type \""
                                 << triggerName << "\", skipping!");
                        continue;
                    }
                    triggerTypes = it->second;
                    if (!dispellTrigger.empty())
                    {
                        if ((it = triggerTable.find(dispellTrigger))
                             == triggerTable.end())
                            LOG_WARN("Item Manager: Unable to find dispell effect "
                                     "trigger type \"" << dispellTrigger << "\"!");
                        else
                            triggerTypes.second = it->second.first;
                    }
                }
                for_each_xml_child_node(effectnode, subnode)
                {
                    if (xmlStrEqual(effectnode->name, BAD_CAST "modifier"))
                    {
                        std::string tag = XML::getProperty(effectnode, "attribute", "");
                        if (tag.empty())
                        {
                            LOG_WARN("Item Manager: Warning, modifier found "
                                     "but no attribute specified!");
                            continue;
                        }
                        unsigned int duration = XML::getProperty(effectnode,
                                                                 "duration",
                                                                 0);
                        std::pair<unsigned int, unsigned int> info = attributeManager->getInfoFromTag(tag);
                        double value = XML::getFloatProperty(effectnode, "value", 0.0);
                        item->addEffect(new ItemEffectAttrMod(info.first,
                                                              info.second,
                                                              value, id,
                                                              duration),
                                        triggerTypes.first, triggerTypes.second);
                    }
                    else if (xmlStrEqual(effectnode->name, BAD_CAST "autoattack"))
                    {
                        // TODO - URGENT
                    }
                    // Having a dispell for the next three is nonsensical.
                    else if (xmlStrEqual(effectnode->name, BAD_CAST "cooldown"))
                    {
                        LOG_WARN("Item Manager: Cooldown property not implemented yet!");
                        // TODO: Also needs unique items before this action will work
                    }
                    else if (xmlStrEqual(effectnode->name, BAD_CAST "g-cooldown"))
                    {
                        LOG_WARN("Item Manager: G-Cooldown property not implemented yet!");
                        // TODO
                    }
                    else if (xmlStrEqual(effectnode->name, BAD_CAST "consumes"))
                        item->addEffect(new ItemEffectConsumes(), triggerTypes.first);
                    else if (xmlStrEqual(effectnode->name, BAD_CAST "script"))
                    {
                        std::string src = XML::getProperty(effectnode, "src", "");
                        if (src.empty())
                        {
                            LOG_WARN("Item Manager: Empty src definition for script effect, skipping!");
                            continue;
                        }
                        std::string func = XML::getProperty(effectnode, "function", "");
                        if (func.empty())
                        {
                            LOG_WARN ("Item Manager: Empty func definition for script effect, skipping!");
                            continue;
                        }
                        for_each_xml_child_node(scriptnode, effectnode)
                        {
                            // TODO: Load variables from variable subnodes
                        }
                        std::string dfunc = XML::getProperty(effectnode, "dispell-function", "");
                        // STUB
                        item->addEffect(new ItemEffectScript(), triggerTypes.first, triggerTypes.second);
                    }
                }
            }
            // More properties go here
        }
        ++nbItems;
    }

    LOG_INFO("Loaded " << nbItems << " items from "
             << absPathFile << ".");
}

void ItemManager::deinitialize()
{
    for (ItemClasses::iterator i = itemClasses.begin(), i_end = itemClasses.end(); i != i_end; ++i)
    {
        delete i->second;
    }
    itemClasses.clear();
}

ItemClass *ItemManager::getItem(int itemId) const
{
    ItemClasses::const_iterator i = itemClasses.find(itemId);
    return i != itemClasses.end() ? i->second : NULL;
}

unsigned int ItemManager::getDatabaseVersion() const
{
    return mItemDatabaseVersion;
}

const std::string &ItemManager::getEquipNameFromId(unsigned int id) const
{
    return equipSlots.at(id).first;
}

unsigned int ItemManager::getEquipIdFromName(const std::string &name) const
{
    for (unsigned int i = 0; i < equipSlots.size(); ++i)
        if (name == equipSlots.at(i).first)
            return i;
    LOG_WARN("Item Manager: attempt to find equip id from name \"" <<
             name << "\" not found, defaulting to 0!");
    return 0;
}

unsigned int ItemManager::getMaxSlotsFromId(unsigned int id) const
{
    return equipSlots.at(id).second;
}

unsigned int ItemManager::getVisibleSlotCount() const
{
    if (!mVisibleEquipSlotCount)
        for (VisibleEquipSlots::const_iterator it = visibleEquipSlots.begin(),
                                               it_end = visibleEquipSlots.end();
             it != it_end;
             ++it)
            mVisibleEquipSlotCount += equipSlots.at(*it).second;
    return mVisibleEquipSlotCount;
}

bool ItemManager::isEquipSlotVisible(unsigned int id) const
{
    for (VisibleEquipSlots::const_iterator it = visibleEquipSlots.begin(),
                                           it_end = visibleEquipSlots.end();
         it != it_end;
         ++it)
        if (*it == id)
            return true;
    return false;
}