00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <map>
00023
00024 #include "statuseffect.h"
00025
00026 #include "log.h"
00027
00028 #include "gui/widgets/chattab.h"
00029
00030 #include "utils/xml.h"
00031
00032
00033 #define STATUS_EFFECTS_FILE "status-effects.xml"
00034
00035 StatusEffect::StatusEffect() :
00036 mPersistentParticleEffect(false)
00037 {}
00038
00039 StatusEffect::~StatusEffect()
00040 {}
00041
00042 void StatusEffect::playSFX()
00043 {
00044 if (!mSFXEffect.empty())
00045 sound.playSfx(mSFXEffect);
00046 }
00047
00048 void StatusEffect::deliverMessage()
00049 {
00050 if (!mMessage.empty())
00051 localChatTab->chatLog(mMessage, BY_SERVER);
00052 }
00053
00054 Particle *StatusEffect::getParticle()
00055 {
00056 if (mParticleEffect.empty())
00057 return NULL;
00058 else
00059 return particleEngine->addEffect(mParticleEffect, 0, 0);
00060 }
00061
00062 AnimatedSprite *StatusEffect::getIcon()
00063 {
00064 if (mIcon.empty())
00065 return NULL;
00066 else {
00067 AnimatedSprite *sprite = AnimatedSprite::load(
00068 "graphics/sprites/" + mIcon);
00069 if (false && sprite) {
00070 sprite->play(ACTION_DEFAULT);
00071 sprite->reset();
00072 }
00073 return sprite;
00074 }
00075 }
00076
00077 SpriteAction StatusEffect::getAction()
00078 {
00079 if (mAction.empty())
00080 return ACTION_INVALID;
00081 else
00082 return SpriteDef::makeSpriteAction(mAction);
00083 }
00084
00085
00086
00087
00088
00089 typedef std::map<int, StatusEffect *> status_effect_map[2];
00090
00091 static status_effect_map statusEffects;
00092 static status_effect_map stunEffects;
00093 static std::map<int, int> blockEffectIndexMap;
00094
00095 int StatusEffect::blockEffectIndexToEffectIndex(int blockIndex)
00096 {
00097 if (blockEffectIndexMap.find(blockIndex) == blockEffectIndexMap.end())
00098 return -1;
00099 return blockEffectIndexMap[blockIndex];
00100 }
00101
00102 StatusEffect *StatusEffect::getStatusEffect(int index, bool enabling)
00103 {
00104 return statusEffects[enabling][index];
00105 }
00106
00107 StatusEffect *StatusEffect::getStunEffect(int index, bool enabling)
00108 {
00109 return stunEffects[enabling][index];
00110 }
00111
00112 void StatusEffect::load()
00113 {
00114 XML::Document doc(STATUS_EFFECTS_FILE);
00115 xmlNodePtr rootNode = doc.rootNode();
00116
00117 if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "status-effects"))
00118 {
00119 logger->log("Error loading status effects file: "
00120 STATUS_EFFECTS_FILE);
00121 return;
00122 }
00123
00124 for_each_xml_child_node(node, rootNode)
00125 {
00126 status_effect_map *the_map = NULL;
00127
00128 int index = atoi(XML::getProperty(node, "id", "-1").c_str());
00129
00130 if (xmlStrEqual(node->name, BAD_CAST "status-effect"))
00131 {
00132 the_map = &statusEffects;
00133 int block_index = atoi(XML::getProperty(node, "block-id", "-1").c_str());
00134
00135 if (index >= 0 && block_index >= 0)
00136 blockEffectIndexMap[block_index] = index;
00137
00138 } else if (xmlStrEqual(node->name, BAD_CAST "stun-effect"))
00139 the_map = &stunEffects;
00140
00141 if (the_map) {
00142 StatusEffect *startEffect = new StatusEffect;
00143 StatusEffect *endEffect = new StatusEffect;
00144
00145 startEffect->mMessage = XML::getProperty(node, "start-message", "");
00146 startEffect->mSFXEffect = XML::getProperty(node, "start-audio", "");
00147 startEffect->mParticleEffect = XML::getProperty(node, "start-particle", "");
00148 startEffect->mIcon = XML::getProperty(node, "icon", "");
00149 startEffect->mAction = XML::getProperty(node, "action", "");
00150 startEffect->mPersistentParticleEffect = (XML::getProperty(node, "persistent-particle-effect", "no")) != "no";
00151
00152 endEffect->mMessage = XML::getProperty(node, "end-message", "");
00153 endEffect->mSFXEffect = XML::getProperty(node, "end-audio", "");
00154 endEffect->mParticleEffect = XML::getProperty(node, "end-particle", "");
00155
00156 (*the_map)[1][index] = startEffect;
00157 (*the_map)[0][index] = endEffect;
00158 }
00159 }
00160 }
00161
00162 void unloadMap(std::map<int, StatusEffect *> map)
00163 {
00164 std::map<int, StatusEffect *>::iterator it;
00165
00166 for (it = map.begin(); it != map.end(); it++)
00167 delete (*it).second;
00168 }
00169
00170 void StatusEffect::unload()
00171 {
00172 unloadMap(statusEffects[0]);
00173 unloadMap(statusEffects[1]);
00174 unloadMap(stunEffects[0]);
00175 unloadMap(stunEffects[1]);
00176 }