summaryrefslogtreecommitdiff
path: root/src/spellmanager.cpp
blob: 83fb746a7c893ded270045aa1b4a38a176b71263 (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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 *  The ManaPlus Client
 *  Copyright (C) 2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  Andrei Karas
 *  Copyright (C) 2011-2013  The ManaPlus developers
 *
 *  This file is part of The ManaPlus 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 "spellmanager.h"

#include "configuration.h"
#include "localplayer.h"
#include "playerinfo.h"

#include "gui/chatwindow.h"

#include "net/net.h"
#include "net/playerhandler.h"

#include "utils/dtor.h"

#include "debug.h"

SpellManager::SpellManager() :
    mSpells(),
    mSpellsVector()
{
    load();
}

SpellManager::~SpellManager()
{
    delete_all(mSpells);
    mSpells.clear();
    mSpellsVector.clear();
}

TextCommand* SpellManager::getSpell(const int spellId) const
{
    if (spellId < 0 || static_cast<unsigned int>(spellId) >= mSpells.size())
        return nullptr;

    std::map<unsigned int, TextCommand*>::const_iterator
        it = mSpells.find(spellId);

    return it != mSpells.end() ? (*it).second : nullptr;
}

TextCommand* SpellManager::getSpellByItem(const int itemId) const
{
    return getSpell(itemId - SPELL_MIN_ID);
}

void SpellManager::fillSpells()
{
// id, std::string name, std::string symbol, ST type, int basicLvl,
//    MagicSchool school, int schoolLvl, int mana)

    addSpell(new TextCommand(0, "lum", "#lum", "heal with lifestones",
             ALLOWTARGET, "", 1, SKILL_MAGIC_LIFE, 0, 6));
    addSpell(new TextCommand(1, "inm", "#inma", "heal", NEEDTARGET,
             "", 2, SKILL_MAGIC_LIFE, 2, 10));
    addSpell(new TextCommand(2, "fla", "#flar", "", NOTARGET,
             "", 1, SKILL_MAGIC_WAR, 0, 10));
    addSpell(new TextCommand(3, "chi", "#chiza", "", NOTARGET,
             "", 1, SKILL_MAGIC_WAR, 0, 9));
    addSpell(new TextCommand(4, "ing", "#ingrav", "", NOTARGET,
             "", 2, SKILL_MAGIC_WAR, 2, 20));
    addSpell(new TextCommand(5, "fri", "#frillyar", "", NOTARGET,
             "", 2, SKILL_MAGIC_WAR, 2, 25));
    addSpell(new TextCommand(6, "upm", "#upmarmu", "", NOTARGET,
             "", 2, SKILL_MAGIC_WAR, 2, 20));
    addSpell(new TextCommand(7, "ite", "#itenplz", "", NOTARGET,
             "", 1, SKILL_MAGIC_NATURE, 0, 3));
    addSpell(new TextCommand(8, "bet", "#betsanc", "", ALLOWTARGET,
             "", 2, SKILL_MAGIC_NATURE, 2, 14));
    addSpell(new TextCommand(9, "abi", "#abizit", "", NOTARGET,
             "", 1, SKILL_MAGIC, 0, 1));
    addSpell(new TextCommand(10, "inw", "#inwilt", "", NOTARGET,
             "", 2, SKILL_MAGIC, 2, 7));
    addSpell(new TextCommand(11, "hi", "hi", "", NOTARGET, ""));
    addSpell(new TextCommand(12, "hea", "heal", "", NOTARGET, ""));
    addSpell(new TextCommand(13, "@sp", "@spawn maggot 10", "", NOTARGET, ""));
    for (unsigned f = 12; f < SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS; f++)
        addSpell(new TextCommand(f));
}

bool SpellManager::addSpell(TextCommand *const spell)
{
    if (!spell)
        return false;

    const std::map<unsigned int, TextCommand*>::const_iterator
        i = mSpells.find(spell->getId());
    if (i == mSpells.end())
    {
        mSpells[spell->getId()] = spell;
        mSpellsVector.push_back(spell);
        return true;
    }
    return false;
}

std::vector<TextCommand*> SpellManager::getAll() const
{
    return mSpellsVector;
}

void SpellManager::useItem(const int itemId) const
{
    invoke(itemId - SPELL_MIN_ID);
}

void SpellManager::invoke(const int spellId) const
{
    if (!player_node)
        return;

    const TextCommand *const spell = getSpell(spellId);
    if (!spell)
        return;

    if (spell->getCommand() == "")
        return;

    if (spell->getCommandType() == TEXT_COMMAND_TEXT
        || (Net::getPlayerHandler()->canUseMagic()
        && PlayerInfo::getSkillLevel(SKILL_MAGIC)
        >= static_cast<signed>(spell->getBaseLvl())
        && PlayerInfo::getSkillLevel(
        spell->getSchool()) >= static_cast<signed>(spell->getSchoolLvl())
        && PlayerInfo::getAttribute(PlayerInfo::MP) >= spell->getMana()))
    {
        const Being *const target = player_node->getTarget();
        if (spell->getTargetType() == NOTARGET)
        {
            invokeSpell(spell);
        }
        if ((target && (target->getType() != Being::MONSTER
            || spell->getCommandType() == TEXT_COMMAND_TEXT))
            && (spell->getTargetType() == ALLOWTARGET
            || spell->getTargetType() == NEEDTARGET))
        {
            invokeSpell(spell, target);
        }
        else if (spell->getTargetType() == ALLOWTARGET)
        {
            invokeSpell(spell);
        }
    }
}

void SpellManager::invokeSpell(const TextCommand *const spell) const
{
    if (!chatWindow || !spell)
        return;
    chatWindow->localChatInput(parseCommand(spell->getCommand(), nullptr));
}

void SpellManager::invokeSpell(const TextCommand *const spell,
                               const Being *const target) const
{
    if (!chatWindow || !spell || !target)
        return;
    chatWindow->localChatInput(parseCommand(spell->getCommand(), target));
}

std::string SpellManager::parseCommand(std::string command,
                                       const Being *const target) const
{
    if (!player_node)
        return command;

    std::string name;
    std::string id;
    std::string name2;

    if (target)
    {
        name = target->getName();
        name2 = target->getName();
        id = toString(target->getId());
    }
    else
    {
        name2 = player_node->getName();
    }

    bool found = false;

    int idx = static_cast<int>(command.find("<TARGET>"));
    if (idx >= 0)
    {
        found = true;
        command = replaceAll(command, "<TARGET>", name);
    }
    idx = static_cast<int>(command.find("<TARGETID>"));
    if (idx >= 0)
    {
        found = true;
        command = replaceAll(command, "<TARGETID>", id);
    }
    idx = static_cast<int>(command.find("<TARGETORSELF>"));
    if (idx >= 0)
    {
        found = true;
        command = replaceAll(command, "<TARGETORSELF>", name2);
    }

    if (!found && !name.empty())
        command.append(" ").append(name);

    return command;
}

TextCommand *SpellManager::createNewSpell() const
{
    return new TextCommand(static_cast<unsigned>(mSpellsVector.size()));
}

void SpellManager::load(const bool oldConfig)
{
    const Configuration *cfg;
    if (oldConfig)
        cfg = &config;
    else
        cfg = &serverConfig;

    delete_all(mSpells);
    mSpells.clear();
    mSpellsVector.clear();

    if (cfg->getValue("commandShortcutFlags0", "") == "")
    {
        fillSpells();
        save();
        return;
    }

    unsigned int targetType;
    unsigned int basicLvl;
    unsigned int school;
    unsigned int schoolLvl;
    unsigned int mana;
    unsigned int commandType;

    for (unsigned i = 0; i < SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS; i++)
    {
        std::string flags =
            cfg->getValue("commandShortcutFlags" + toString(i), "");
        std::stringstream ss(flags);
        ss >> commandType;
        ss >> targetType;
        ss >> basicLvl;
        ss >> school;
        ss >> schoolLvl;
        ss >> mana;

        std::string cmd = cfg->getValue("commandShortcutCmd"
                                        + toString(i), "");
        std::string comment = cfg->getValue("commandShortcutComment"
                                        + toString(i), "");
        std::string symbol = cfg->getValue("commandShortcutSymbol"
                                           + toString(i), "");
        std::string icon = cfg->getValue("commandShortcutIcon"
                                         + toString(i), "");

        if (static_cast<TextCommandType>(commandType) == TEXT_COMMAND_MAGIC)
        {
            addSpell(new TextCommand(i, symbol, cmd, comment,
                static_cast<SpellTarget>(targetType), icon, basicLvl,
                static_cast<MagicSchool>(school), schoolLvl, mana));
        }
        else
        {
            addSpell(new TextCommand(i, symbol, cmd, comment,
                static_cast<SpellTarget>(targetType), icon));
        }
    }
}

void SpellManager::save() const
{
    for (unsigned i = 0; i < SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS; i++)
    {
        const TextCommand *const spell = mSpellsVector[i];
        if (spell)
        {
            if (spell->getCommand() != "")
            {
                serverConfig.setValue("commandShortcutCmd" + toString(i),
                                      spell->getCommand());
            }
            else
            {
                serverConfig.deleteKey("commandShortcutCmd" + toString(i));
            }
            if (spell->getComment() != "")
            {
                serverConfig.setValue("commandShortcutComment" + toString(i),
                                      spell->getComment());
            }
            else
            {
                serverConfig.deleteKey("commandShortcutComment" + toString(i));
            }
            if (spell->getSymbol() != "")
            {
                serverConfig.setValue("commandShortcutSymbol" + toString(i),
                                      spell->getSymbol());
            }
            else
            {
                serverConfig.deleteKey("commandShortcutSymbol" + toString(i));
            }
            if (spell->getIcon() != "")
            {
                serverConfig.setValue("commandShortcutIcon" + toString(i),
                                      spell->getIcon());
            }
            else
            {
                serverConfig.deleteKey("commandShortcutIcon" + toString(i));
            }
            if (spell->getCommand() != "" && spell->getSymbol() != "")
            {
                serverConfig.setValue("commandShortcutFlags" + toString(i),
                    strprintf("%u %u %u %u %u %u", static_cast<unsigned>(
                    spell->getCommandType()), static_cast<unsigned>(
                    spell->getTargetType()), spell->getBaseLvl(),
                    static_cast<unsigned>(spell->getSchool()),
                    spell->getSchoolLvl(), static_cast<unsigned>(
                    spell->getMana())));
            }
            else
            {
                serverConfig.deleteKey("commandShortcutFlags" + toString(i));
            }
        }
    }
}

std::string SpellManager::autoComplete(const std::string &partName) const
{
    std::vector<TextCommand*>::const_iterator i = mSpellsVector.begin();
    const std::vector<TextCommand*>::const_iterator
        i_end = mSpellsVector.end();
    std::string newName;
    const TextCommand *newCommand = nullptr;

    while (i != i_end)
    {
        const TextCommand *const cmd = *i;
        const std::string line = cmd->getCommand();

        if (!line.empty())
        {
            const size_t pos = line.find(partName, 0);
            if (pos == 0)
            {
                if (!newName.empty())
                {
                    newName = findSameSubstring(line, newName);
                    newCommand = nullptr;
                }
                else
                {
                    newName = line;
                    newCommand = cmd;
                }
            }
        }
        ++i;
    }
    if (!newName.empty() && newCommand
        && newCommand->getTargetType() == NEEDTARGET)
    {
        return newName.append(" ");
    }
    return newName;
}

void SpellManager::swap(const int id1, const int id2)
{
    TextCommand *const spell1 = mSpells[id1];
    TextCommand *const spell2 = mSpells[id2];
    // swap in map
    mSpells[id1] = spell2;
    mSpells[id2] = spell1;

    // swap id
    int tmp = spell1->getId();
    spell1->setId(spell2->getId());
    spell2->setId(tmp);

    // swap in vector
    const int sz = SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS;
    for (unsigned f = 0; f < sz; f++)
    {
        const TextCommand *const spellA = mSpellsVector[f];
        if (spellA == spell1)
        {
            for (unsigned d = 0; d < sz; d++)
            {
                const TextCommand *const spellB = mSpellsVector[d];
                if (spellB == spell2)
                {
                    mSpellsVector[f] = spell2;
                    mSpellsVector[d] = spell1;
                    return;
                }
            }
        }
    }
}