summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2012-01-18 19:20:34 +0100
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2012-02-02 15:31:41 +0100
commitf5de9ae444f1bca1f6ba6969214e9a8cacb15f68 (patch)
treef9b9c699c52d279cdc4d93b09f48dd3b24403f5b /src/resources
parent7cc504d993fa948ae2e10848993f4552b2d6daaa (diff)
downloadmana-f5de9ae444f1bca1f6ba6969214e9a8cacb15f68.tar.gz
mana-f5de9ae444f1bca1f6ba6969214e9a8cacb15f68.tar.bz2
mana-f5de9ae444f1bca1f6ba6969214e9a8cacb15f68.tar.xz
mana-f5de9ae444f1bca1f6ba6969214e9a8cacb15f68.zip
Fix to the hair colors and styles handling.
- I made the charCreatedialog handle a possible max permitted color Id and a minimum hair style id for tA. - Added a foundation to later load the styles and colors from the same file, to handle the Mana-issue #224 for manaserv. - Support for non-contiguous hair color and style ids has also been added. - I also replaced the < and > arrow signs with images. Reviewed-by: Ben Longbons, Thorbjørn Lindeijer
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/hairdb.cpp146
-rw-r--r--src/resources/hairdb.h66
-rw-r--r--src/resources/itemdb.cpp11
3 files changed, 167 insertions, 56 deletions
diff --git a/src/resources/hairdb.cpp b/src/resources/hairdb.cpp
index abf64bd5..26ad966e 100644
--- a/src/resources/hairdb.cpp
+++ b/src/resources/hairdb.cpp
@@ -1,5 +1,5 @@
/*
- * Color database
+ * Hair database
* Copyright (C) 2008 Aethyra Development Team
* Copyright (C) 2009-2012 The Mana Developers
*
@@ -25,58 +25,51 @@
#include "utils/xml.h"
-#include <libxml/tree.h>
+#include <assert.h>
-namespace
-{
- HairDB::Colors mColors;
- bool mLoaded = false;
- std::string mFail = "#ffffff";
-}
+#define COLOR_WHITE "#ffffff"
+#define HAIR_XML_FILE "hair.xml"
void HairDB::load()
{
if (mLoaded)
unload();
+ // Default entry
+ mHairColors[0] = COLOR_WHITE;
+
XML::Document *doc = new XML::Document(HAIR_XML_FILE);
xmlNodePtr root = doc->rootNode();
- bool hairXml = true;
- if (!root || !xmlStrEqual(root->name, BAD_CAST "colors"))
+ if (!root || (!xmlStrEqual(root->name, BAD_CAST "colors")
+ && !xmlStrEqual(root->name, BAD_CAST "hair")))
{
- logger->log("Trying to fall back on " COLORS_XML_FILE);
-
- hairXml = false;
-
+ logger->log("HairDb: Failed to find any old <colors> or new "
+ "<hair> nodes.");
delete doc;
- doc = new XML::Document(COLORS_XML_FILE);
- root = doc->rootNode();
-
- if (!root || !xmlStrEqual(root->name, BAD_CAST "colors"))
- {
- logger->log("ColorDB: Failed to find any color files.");
- mColors[0] = mFail;
- mLoaded = true;
-
- delete doc;
+ mLoaded = true;
+ return;
+ }
- return;
- }
+ // Old colors.xml file style. The hair style will be declared
+ // in the items.xml file.
+ if (xmlStrEqual(root->name, BAD_CAST "colors"))
+ {
+ loadHairColorsNode(root);
}
- for_each_xml_child_node(node, root)
+ else if (xmlStrEqual(root->name, BAD_CAST "hair"))
{
- if (xmlStrEqual(node->name, BAD_CAST "color"))
+ // Loading new format: hair styles + colors.
+ for_each_xml_child_node(node, root)
{
- int id = XML::getProperty(node, "id", 0);
-
- if (mColors.find(id) != mColors.end())
+ if (xmlStrEqual(node->name, BAD_CAST "styles"))
{
- logger->log("ColorDB: Redefinition of dye ID %d", id);
+ loadHairStylesNode(root);
+ }
+ else if (xmlStrEqual(node->name, BAD_CAST "colors"))
+ {
+ loadHairColorsNode(node);
}
-
- mColors[id] = hairXml ? XML::getProperty(node, "value", "#FFFFFF") :
- XML::getProperty(node, "dye", "#FFFFFF");
}
}
@@ -87,31 +80,88 @@ void HairDB::load()
void HairDB::unload()
{
- logger->log("Unloading color database...");
+ if (!mLoaded)
+ return;
+
+ logger->log("Unloading hair style and color database...");
+
+ mHairColors.clear();
+ mHairStyles.clear();
- mColors.clear();
mLoaded = false;
}
-std::string &HairDB::get(int id)
+void HairDB::loadHairColorsNode(xmlNodePtr colorsNode)
+{
+ for_each_xml_child_node(node, colorsNode)
+ {
+ if (xmlStrEqual(node->name, BAD_CAST "color"))
+ {
+ int id = XML::getProperty(node, "id", 0);
+
+ if (mHairColors.find(id) != mHairColors.end())
+ logger->log("HairDb: Redefinition of color Id %d", id);
+
+ mHairColors[id] = XML::getProperty(node, "value", COLOR_WHITE);
+ }
+ }
+}
+
+void HairDB::loadHairStylesNode(xmlNodePtr stylesNode)
+{
+ // TODO: Add support of the races.xml file.
+}
+
+void HairDB::addHairStyle(int id)
+{
+ // TODO: Adapt the sprite handling with hair styles separated from items.
+ // And remove that hack for negative ids.
+ if (id < 0)
+ id = -id;
+
+ if (mHairStyles.find(id) != mHairStyles.end())
+ logger->log("Warning: Redefinition of hairstyle id %i:", id);
+
+ mHairStyles.insert(id);
+}
+
+const std::string &HairDB::getHairColor(int id)
{
if (!mLoaded)
load();
- ColorIterator i = mColors.find(id);
+ ColorConstIterator it = mHairColors.find(id);
+ if (it != mHairColors.end())
+ return it->second;
- if (i == mColors.end())
- {
- logger->log("ColorDB: Error, unknown dye Id# %d", id);
- return mFail;
- }
- else
+ logger->log("HairDb: Error, unknown color Id# %d", id);
+ return mHairColors[0];
+}
+
+std::vector<int> HairDB::getHairStyleIds(int maxId) const
+{
+ std::vector<int> hairStylesIds;
+ for (HairStylesConstIterator it = mHairStyles.begin(),
+ it_end = mHairStyles.end(); it != it_end; ++it)
{
- return i->second;
+ // Don't give ids higher than the requested maximum.
+ if (maxId > 0 && (*it) > maxId)
+ continue;
+ hairStylesIds.push_back(*it);
}
+ return hairStylesIds;
}
-int HairDB::size()
+std::vector<int> HairDB::getHairColorIds(int maxId) const
{
- return mColors.size();
+ std::vector<int> hairColorsIds;
+ for (ColorConstIterator it = mHairColors.begin(),
+ it_end = mHairColors.end(); it != it_end; ++it)
+ {
+ // Don't give ids higher than the requested maximum.
+ if (maxId > 0 && it->first > maxId)
+ continue;
+ hairColorsIds.push_back(it->first);
+ }
+ return hairColorsIds;
}
diff --git a/src/resources/hairdb.h b/src/resources/hairdb.h
index c29dbb83..700bd8b7 100644
--- a/src/resources/hairdb.h
+++ b/src/resources/hairdb.h
@@ -1,5 +1,5 @@
/*
- * Color database
+ * Hair database
* Copyright (C) 2008 Aethyra Development Team
* Copyright (C) 2009-2012 The Mana Developers
*
@@ -22,16 +22,26 @@
#ifndef HAIR_MANAGER_H
#define HAIR_MANAGER_H
+#include "iteminfo.h"
+
#include <map>
#include <string>
/**
* Hair information database.
*/
-namespace HairDB
+class HairDB
{
+ public:
+ HairDB():
+ mLoaded(false)
+ {}
+
+ ~HairDB()
+ { unload(); }
+
/**
- * Loads the color data from <code>colors.xml</code>.
+ * Loads the color data from <code>hair.xml</code>.
*/
void load();
@@ -40,13 +50,55 @@ namespace HairDB
*/
void unload();
- std::string &get(int id);
+ const std::string &getHairColor(int id);
+
+ /**
+ * Returns the available hair style ids
+ * @param maxId the max permited id. If not 0, the hairDb won't
+ * return ids > to the parameter.
+ */
+ std::vector<int> getHairStyleIds(int maxId = 0) const;
+
+ /**
+ * Returns the available hair color ids
+ * @param maxId the max permited id. If not 0, the hairDb won't
+ * return ids > to the parameter.
+ */
+ std::vector<int> getHairColorIds(int maxId = 0) const;
+
+ /**
+ * Add a hair style to the database.
+ * @see ItemDB for the itemInfo.
+ */
+ void addHairStyle(int id);
+
+ private:
+ /**
+ * Load the hair colors, contained in a <colors> node.
+ */
+ void loadHairColorsNode(xmlNodePtr colorsNode);
- int size();
+ /**
+ * Load the hair styles, contained in a <styles> node.
+ * Used only by Manaserv. TMW-Athena is considering hairstyles as items.
+ * @see ItemDB
+ */
+ void loadHairStylesNode(xmlNodePtr stylesNode);
- // Hair Color DB
+ // Hair colors Db
typedef std::map<int, std::string> Colors;
typedef Colors::iterator ColorIterator;
-}
+ typedef Colors::const_iterator ColorConstIterator;
+ Colors mHairColors;
+
+ typedef std::set<int> HairStyles;
+ typedef HairStyles::iterator HairStylesIterator;
+ typedef HairStyles::const_iterator HairStylesConstIterator;
+ HairStyles mHairStyles;
+
+ bool mLoaded;
+};
+
+extern HairDB hairDB;
#endif
diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp
index 977fd56f..95fdae2f 100644
--- a/src/resources/itemdb.cpp
+++ b/src/resources/itemdb.cpp
@@ -25,7 +25,7 @@
#include "net/net.h"
-#include "resources/iteminfo.h"
+#include "resources/hairdb.h"
#include "resources/resourcemanager.h"
#include "utils/dtor.h"
@@ -382,6 +382,10 @@ void TaItemDB::load()
checkItemInfo(itemInfo);
addItem(itemInfo);
+
+ // Insert hairstyle id while letting the info as an item.
+ if (itemInfo->mType == ITEM_SPRITE_HAIR)
+ hairDB.addHairStyle(itemInfo->mId);
}
checkHairWeaponsRacesSpecialIds();
@@ -528,6 +532,11 @@ void ManaServItemDB::load()
(const char*)effectChild->xmlChildrenNode->content);
}
}
+
+ // FIXME: Load hair styles through the races.xml file
+ if (itemInfo->mType == ITEM_SPRITE_HAIR)
+ hairDB.addHairStyle(itemInfo->mId);
+
// Set Item Type based on subnodes info
// TODO: Improve it once the itemTypes are loaded through xml
itemInfo->mType = ITEM_UNUSABLE;