1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2008  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#include <map>
23
24
#include "statuseffect.h"
25
26
#include "log.h"
27
28
#include "gui/widgets/chattab.h"
29
30
#include "utils/xml.h"
31
32
33
#define STATUS_EFFECTS_FILE "status-effects.xml"
34
35
StatusEffect::StatusEffect() :
36
    mPersistentParticleEffect(false)
37
{}
38
39
StatusEffect::~StatusEffect()
40
{}
41
42
void StatusEffect::playSFX()
43
{
44
    if (!mSFXEffect.empty())
45
        sound.playSfx(mSFXEffect);
46
}
47
48
void StatusEffect::deliverMessage()
49
{
50
    if (!mMessage.empty())
51
        localChatTab->chatLog(mMessage, BY_SERVER);
52
}
53
54
Particle *StatusEffect::getParticle()
55
{
56
    if (mParticleEffect.empty())
57
        return NULL;
58
    else
59
        return particleEngine->addEffect(mParticleEffect, 0, 0);
60
}
61
62
AnimatedSprite *StatusEffect::getIcon()
63
{
64
    if (mIcon.empty())
65
        return NULL;
66
    else {
67
        AnimatedSprite *sprite = AnimatedSprite::load(
68
                "graphics/sprites/" + mIcon);
69
        if (false && sprite) {
70
            sprite->play(ACTION_DEFAULT);
71
            sprite->reset();
72
        }
73
        return sprite;
74
    }
75
}
76
77
SpriteAction StatusEffect::getAction()
78
{
79
    if (mAction.empty())
80
        return ACTION_INVALID;
81
    else
82
        return SpriteDef::makeSpriteAction(mAction);
83
}
84
85
86
// -- initialisation and static parts --
87
88
89
typedef std::map<int, StatusEffect *> status_effect_map[2];
90
91
static status_effect_map statusEffects;
92
static status_effect_map stunEffects;
93
static std::map<int, int> blockEffectIndexMap;
94
95
int StatusEffect::blockEffectIndexToEffectIndex(int blockIndex)
96
{
97
    if (blockEffectIndexMap.find(blockIndex) == blockEffectIndexMap.end())
98
        return -1;
99
    return blockEffectIndexMap[blockIndex];
100
}
101
102
StatusEffect *StatusEffect::getStatusEffect(int index, bool enabling)
103
{
104
    return statusEffects[enabling][index];
105
}
106
107
StatusEffect *StatusEffect::getStunEffect(int index, bool enabling)
108
{
109
    return stunEffects[enabling][index];
110
}
111
112
void StatusEffect::load()
113
{
114
    XML::Document doc(STATUS_EFFECTS_FILE);
115
    xmlNodePtr rootNode = doc.rootNode();
116
117
    if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "status-effects"))
118
    {
119
        logger->log("Error loading status effects file: "
120
                    STATUS_EFFECTS_FILE);
121
        return;
122
    }
123
124
    for_each_xml_child_node(node, rootNode)
125
    {
126
        status_effect_map *the_map = NULL;
127
128
        int index = atoi(XML::getProperty(node, "id", "-1").c_str());
129
130
        if (xmlStrEqual(node->name, BAD_CAST "status-effect"))
131
        {
132
            the_map = &statusEffects;
133
            int block_index = atoi(XML::getProperty(node, "block-id", "-1").c_str());
134
135
            if (index >= 0 && block_index >= 0)
136
                blockEffectIndexMap[block_index] = index;
137
138
        } else if (xmlStrEqual(node->name, BAD_CAST "stun-effect"))
139
            the_map = &stunEffects;
140
141
        if (the_map) {
142
            StatusEffect *startEffect = new StatusEffect;
143
            StatusEffect *endEffect = new StatusEffect;
144
145
            startEffect->mMessage = XML::getProperty(node, "start-message", "");
146
            startEffect->mSFXEffect = XML::getProperty(node, "start-audio", "");
147
            startEffect->mParticleEffect = XML::getProperty(node, "start-particle", "");
148
            startEffect->mIcon = XML::getProperty(node, "icon", "");
149
            startEffect->mAction = XML::getProperty(node, "action", "");
150
            startEffect->mPersistentParticleEffect = (XML::getProperty(node, "persistent-particle-effect", "no")) != "no";
151
152
            endEffect->mMessage = XML::getProperty(node, "end-message", "");
153
            endEffect->mSFXEffect = XML::getProperty(node, "end-audio", "");
154
            endEffect->mParticleEffect = XML::getProperty(node, "end-particle", "");
155
156
            (*the_map)[1][index] = startEffect;
157
            (*the_map)[0][index] = endEffect;
158
        }
159
    }
160
}
161
162
void unloadMap(std::map<int, StatusEffect *> map)
163
{
164
    std::map<int, StatusEffect *>::iterator it;
165
166
    for (it = map.begin(); it != map.end(); it++)
167
        delete (*it).second;
168
}
169
170
void StatusEffect::unload()
171
{
172
    unloadMap(statusEffects[0]);
173
    unloadMap(statusEffects[1]);
174
    unloadMap(stunEffects[0]);
175
    unloadMap(stunEffects[1]);
176
}