summaryrefslogtreecommitdiff
path: root/src/game-server/skillmanager.cpp
blob: 69a875e6898c6f40137b3daf2081f02da6600103 (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
/*
 *  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/skillmanager.h"

#include "utils/logger.h"

#include <map>

void SkillManager::clear()
{
    for (SkillsInfo::iterator it = mSkillsInfo.begin(),
         it_end = mSkillsInfo.end(); it != it_end; ++it)
    {
        delete it->second;
    }

    mSkillsInfo.clear();
    mNamedSkillsInfo.clear();
}

void SkillManager::initialize()
{
    clear();

    XML::Document doc(mSkillFile);
    xmlNodePtr rootNode = doc.rootNode();

    if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "skills"))
    {
        LOG_ERROR("Skill Manager: " << mSkillFile
                  << " is not a valid database file!");
        return;
    }

    LOG_INFO("Loading skill reference: " << mSkillFile);

    for_each_xml_child_node(setNode, rootNode)
    {
        // The server will prefix the core name with the set, so we need one.
        if (!xmlStrEqual(setNode->name, BAD_CAST "set"))
            continue;

        std::string setName = XML::getProperty(setNode, "name", std::string());
        if (setName.empty())
        {
            LOG_WARN("The " << mSkillFile << " file is containing unamed <set> "
                     "tags and will be ignored.");
            continue;
        }

        setName = utils::toLower(setName);

        for_each_xml_child_node(skillNode, setNode)
            readSkillNode(skillNode, setName);
    }

    printDebugSkillTable();

    if (!mDefaultSkillId)
        LOG_WARN("SkillManager: No default weapon-type id was given during "
                 "Skill map loading. "
                 "Players won't be able to earn XP when unarmed.");

    LOG_INFO("Loaded " << mSkillsInfo.size() << " skills from "
             << mSkillFile);
}

void SkillManager::readSkillNode(xmlNodePtr skillNode,
                                 const std::string& setName)
{
    if (!xmlStrEqual(skillNode->name, BAD_CAST "skill"))
        return;

    SkillInfo *skillInfo = new SkillInfo;
    skillInfo->setName = setName;
    skillInfo->skillName = utils::toLower(
        XML::getProperty(skillNode, "name", std::string()));
    int id = XML::getProperty(skillNode, "id", 0);

    if (id <= 0 || skillInfo->skillName.empty())
    {
        LOG_WARN("Invalid skill (empty name or id <= 0) in set: " << setName);
        return;
    }
    skillInfo->id = (unsigned)id;

    SkillsInfo::iterator it = mSkillsInfo.find(skillInfo->id);
    if (it != mSkillsInfo.end())
    {
        LOG_WARN("SkillManager: The same id: " << skillInfo->id
        << " is given for skill names: " << it->first
        << " and " << skillInfo->skillName);
        LOG_WARN("The skill reference: " << skillInfo->id
        << ": '" << skillInfo->skillName << "' will be ignored.");
        return;
    }

    if (XML::getBoolProperty(skillNode, "default", false))
    {
        if (mDefaultSkillId)
        {
            LOG_WARN("SkillManager: "
            "Default Skill id already defined as "
            << mDefaultSkillId
            << ". Redefinit it as: " << skillInfo->id);
        }
        else
        {
            LOG_INFO("SkillManager: Defining skill id: " << skillInfo->id
            << " as default weapon-type id.");
        }
        mDefaultSkillId = skillInfo->id;
    }

    mSkillsInfo.insert(std::make_pair(skillInfo->id, skillInfo));

    std::string keyName = setName + "_" + skillInfo->skillName;
    mNamedSkillsInfo.insert(keyName, skillInfo);
}

void SkillManager::printDebugSkillTable()
{
    if (::utils::Logger::mVerbosity >= ::utils::Logger::Debug)
    {
        std::string lastSet;
        LOG_DEBUG("Skill map in " << mSkillFile << ":"
                  << std::endl << "-----");
        for (SkillsInfo::iterator it = mSkillsInfo.begin();
            it != mSkillsInfo.end(); ++it)
        {
            if (!lastSet.compare(it->second->setName))
            {
                lastSet = it->second->setName;
                LOG_DEBUG("Skill set: " << lastSet);
            }

            if (it->first == mDefaultSkillId)
            {
                LOG_DEBUG("'" << it->first << "': " << it->second->skillName
                          << " (Default)");
            }
            else
            {
                LOG_DEBUG("'" << it->first << "': " << it->second->skillName);
            }
        }
        LOG_DEBUG("-----");
    }
}

unsigned SkillManager::getId(const std::string &set,
                             const std::string &name) const
{
    std::string key = utils::toLower(set) + "_" + utils::toLower(name);
    return getId(key);
}

unsigned SkillManager::getId(const std::string &skillName) const
{
    SkillInfo *skillInfo = mNamedSkillsInfo.value(skillName);
    return skillInfo ? skillInfo->id : 0;
}

const std::string SkillManager::getSkillName(unsigned id) const
{
    SkillsInfo::const_iterator it = mSkillsInfo.find(id);
    return it != mSkillsInfo.end() ? it->second->skillName : "";
}

const std::string SkillManager::getSetName(unsigned id) const
{
    SkillsInfo::const_iterator it = mSkillsInfo.find(id);
    return it != mSkillsInfo.end() ? it->second->setName : "";
}

bool SkillManager::exists(unsigned id) const
{
    SkillsInfo::const_iterator it = mSkillsInfo.find(id);
    return it != mSkillsInfo.end();
}