summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-02-15 14:15:27 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-02-15 14:15:34 +0100
commit278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e (patch)
treee1a095b48e34b844405801f7240501fe89579b38 /src/resources
parent2779eb792a04667958feab9b433c1c82392c0bc3 (diff)
downloadmana-278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e.tar.gz
mana-278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e.tar.bz2
mana-278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e.tar.xz
mana-278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e.zip
Fix handling of non-consecutive emote IDs
Previous code was assuming there would be no gaps in the emote IDs. Also cleaned up some confusion where the "emote ID" being passed around in the code was often offset by 1. Now it is only offset in communication with tmwAthena and when saving the shortcuts.
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/emotedb.cpp71
-rw-r--r--src/resources/emotedb.h13
2 files changed, 37 insertions, 47 deletions
diff --git a/src/resources/emotedb.cpp b/src/resources/emotedb.cpp
index 752f7b4f..e1dd8f4a 100644
--- a/src/resources/emotedb.cpp
+++ b/src/resources/emotedb.cpp
@@ -27,12 +27,14 @@
#include "resources/resourcemanager.h"
#include "resources/imageset.h"
+#include <algorithm>
+#include <vector>
+
namespace
{
- Emotes mEmotes;
+ std::vector<Emote> mEmotes;
Emote mUnknown;
bool mLoaded = false;
- int mLastEmote = 0;
}
void EmoteDB::init()
@@ -41,33 +43,30 @@ void EmoteDB::init()
unload();
mUnknown.name = "unknown";
- mUnknown.effect = -1;
+ mUnknown.effectId = -1;
mUnknown.sprite = new ImageSprite(
ResourceManager::getInstance()->getImage("graphics/sprites/error.png"));
-
- mLastEmote = 0;
-
}
void EmoteDB::readEmoteNode(xmlNodePtr node, const std::string &filename)
{
- int id = XML::getProperty(node, "id", -1);
+ const int id = XML::getProperty(node, "id", -1);
if (id == -1)
{
logger->log("Emote Database: Emote with missing ID in %s!", filename.c_str());
return;
}
- auto *currentEmote = new Emote;
+ Emote currentEmote;
- currentEmote->name = XML::getProperty(node, "name", "unknown");
- currentEmote->effect = XML::getProperty(node, "effectid", -1);
+ currentEmote.id = id;
+ currentEmote.name = XML::getProperty(node, "name", "unknown");
+ currentEmote.effectId = XML::getProperty(node, "effectid", -1);
- if (currentEmote->effect == -1)
+ if (currentEmote.effectId == -1)
{
logger->log("Emote Database: Warning: Emote %s has no attached effect in %s!",
- currentEmote->name.c_str(), filename.c_str());
- delete currentEmote;
+ currentEmote.name.c_str(), filename.c_str());
return;
}
@@ -78,31 +77,25 @@ void EmoteDB::readEmoteNode(xmlNodePtr node, const std::string &filename)
if (imageName.empty() || width <= 0 || height <= 0)
{
logger->log("Emote Database: Warning: Emote %s has bad imageset values in %s",
- currentEmote->name.c_str(), filename.c_str());
- delete currentEmote;
+ currentEmote.name.c_str(), filename.c_str());
return;
}
ImageSet *is = ResourceManager::getInstance()->getImageSet(imageName,
width,
height);
- if (!is || !(is->size() > 0))
+ if (!is || is->size() == 0)
{
logger->log("Emote Database: Error loading imageset for emote %s in %s",
- currentEmote->name.c_str(), filename.c_str());
+ currentEmote.name.c_str(), filename.c_str());
delete is;
- delete currentEmote;
return;
}
- else
- {
- // For now we just use the first image in the animation
- currentEmote->sprite = new ImageSprite(is->get(0));
- }
- mEmotes[id] = currentEmote;
- if (id > mLastEmote)
- mLastEmote = id;
+ // For now we just use the first image in the animation
+ currentEmote.sprite = new ImageSprite(is->get(0));
+
+ mEmotes.push_back(std::move(currentEmote));
}
void EmoteDB::checkStatus()
@@ -112,31 +105,31 @@ void EmoteDB::checkStatus()
void EmoteDB::unload()
{
- Emotes::iterator i;
- for (i = mEmotes.begin(); i != mEmotes.end(); i++)
- {
- delete i->second;
- }
-
+ // todo: don't we need to delete the sprites?
mEmotes.clear();
-
mLoaded = false;
}
-const Emote *EmoteDB::get(int id)
+const Emote &EmoteDB::get(int id)
{
- Emotes::const_iterator i = mEmotes.find(id);
+ auto i = std::find_if(mEmotes.begin(), mEmotes.end(),
+ [id](const Emote &e) { return e.id == id; });
if (i == mEmotes.end())
{
logger->log("EmoteDB: Warning, unknown emote ID %d requested", id);
- return &mUnknown;
+ return mUnknown;
}
- return i->second;
+ return *i;
+}
+
+const Emote &EmoteDB::getByIndex(int index)
+{
+ return mEmotes.at(index);
}
-int EmoteDB::getLast()
+int EmoteDB::getEmoteCount()
{
- return mLastEmote;
+ return mEmotes.size();
}
diff --git a/src/resources/emotedb.h b/src/resources/emotedb.h
index cc3b30f7..1ed86a05 100644
--- a/src/resources/emotedb.h
+++ b/src/resources/emotedb.h
@@ -22,7 +22,6 @@
#ifndef EMOTE_DB_H
#define EMOTE_DB_H
-#include <map>
#include <string>
#include "utils/xml.h"
@@ -31,13 +30,12 @@ class ImageSprite;
struct Emote
{
+ int id;
+ int effectId;
std::string name;
ImageSprite *sprite;
- int effect;
};
-using Emotes = std::map<int, Emote *>;
-
/**
* Emote information database.
*/
@@ -51,11 +49,10 @@ namespace EmoteDB
void unload();
- const Emote *get(int id);
-
- int getLast();
+ const Emote &get(int id);
+ const Emote &getByIndex(int index);
- using EmotesIterator = Emotes::iterator;
+ int getEmoteCount();
}
#endif // EMOTE_DB_H