Mana
Loading...
Searching...
No Matches
hairdb.cpp
Go to the documentation of this file.
1/*
2 * Hair database
3 * Copyright (C) 2008 Aethyra Development Team
4 * Copyright (C) 2009-2013 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "resources/hairdb.h"
23
24#include "log.h"
25
26#define COLOR_WHITE "#ffffff"
27
28
30{
31 if (mLoaded)
32 unload();
33
34 // Default entry
36}
37
38void HairDB::readHairColorNode(XML::Node node, const std::string &filename)
39{
40 int id = node.getProperty("id", 0);
41
42 if (mHairColors.find(id) != mHairColors.end())
43 Log::info("HairDb: Redefinition of color Id %d in %s", id, filename.c_str());
44
45 mHairColors[id] = node.getProperty("value", COLOR_WHITE);
46}
47
49{
50 mLoaded = true;
51}
52
54{
55 if (!mLoaded)
56 return;
57
58 Log::info("Unloading hair style and color database...");
59
60 mHairColors.clear();
61 mHairStyles.clear();
62
63 mLoaded = false;
64}
65
67{
68 // TODO: Adapt the sprite handling with hair styles separated from items.
69 // And remove that hack for negative ids.
70 if (id < 0)
71 id = -id;
72
73 if (mHairStyles.find(id) != mHairStyles.end())
74 Log::warn("Redefinition of hairstyle id %i:", id);
75
76 mHairStyles.insert(id);
77}
78
79const std::string &HairDB::getHairColor(int id) const
80{
81 if (!mLoaded)
82 {
83 // no idea if this can happen, but that check existed before
84 Log::warn("HairDB::getHairColor() called before settings were loaded!");
85 }
86 auto it = mHairColors.find(id);
87 if (it != mHairColors.end())
88 return it->second;
89
90 Log::info("HairDb: Error, unknown color Id# %d", id);
91 return mHairColors.at(0);
92}
93
94std::vector<int> HairDB::getHairStyleIds(int maxId) const
95{
96 std::vector<int> hairStylesIds;
97 for (int hairStyle : mHairStyles)
98 {
99 // Don't give ids higher than the requested maximum.
100 if (maxId > 0 && hairStyle > maxId)
101 continue;
102 hairStylesIds.push_back(hairStyle);
103 }
104 return hairStylesIds;
105}
106
107std::vector<int> HairDB::getHairColorIds(int minId, int maxId) const
108{
109 std::vector<int> hairColorsIds;
110 for (const auto &[id, _] : mHairColors)
111 {
112 // Don't give ids higher than the requested maximum.
113 if ((maxId > 0 && id > maxId) || id < minId)
114 continue;
115 hairColorsIds.push_back(id);
116 }
117 return hairColorsIds;
118}
std::vector< int > getHairStyleIds(int maxId) const
Returns the available hair style ids.
Definition hairdb.cpp:94
void checkStatus()
Definition hairdb.cpp:48
void init()
Definition hairdb.cpp:29
const std::string & getHairColor(int id) const
Definition hairdb.cpp:79
std::set< int > mHairStyles
Definition hairdb.h:89
std::map< int, std::string > mHairColors
Definition hairdb.h:88
void addHairStyle(int id)
Add a hair style to the database.
Definition hairdb.cpp:66
void readHairColorNode(XML::Node node, const std::string &filename)
Definition hairdb.cpp:38
void unload()
Clear the color data.
Definition hairdb.cpp:53
std::vector< int > getHairColorIds(int minId, int maxId) const
Returns the available hair color ids.
Definition hairdb.cpp:107
bool mLoaded
Definition hairdb.h:91
int getProperty(const char *name, int def) const
Definition xml.h:144
#define _(s)
Definition gettext.h:38
#define COLOR_WHITE
Definition hairdb.cpp:26
void warn(const char *log_text,...) LOG_PRINTF_ATTR
void info(const char *log_text,...) LOG_PRINTF_ATTR