forked from DFHack/stonesense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemConfiguration.cpp
121 lines (103 loc) · 3.35 KB
/
ItemConfiguration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "common.h"
#include "ItemConfiguration.h"
#include "tinyxml.h"
#include "GUI.h"
#include "ContentLoader.h"
ItemConfiguration::ItemConfiguration()
{
configured=0;
}
ItemConfiguration::~ItemConfiguration()
{
subItems.clear();
}
bool addSingleItemConfig( TiXmlElement* elemRoot)
{
int basefile = INVALID_INDEX;
const char* filename = elemRoot->Attribute("file");
if (filename != NULL && filename[0] != 0) {
basefile = loadConfigImgFile((char*)filename,elemRoot);
if(basefile == -1) {
return false;
}
}
TiXmlElement* elemFloor = elemRoot->FirstChildElement("item");
while( elemFloor ) {
parseItemElement( elemFloor, basefile);
elemFloor = elemFloor->NextSiblingElement("item");
}
return true;
}
bool parseItemElement( TiXmlElement* elemRoot, int basefile)
{
const char* strGameID = elemRoot->Attribute("game_type");
const char* strGameSub = elemRoot->Attribute("game_subtype");
if (strGameID == NULL || strGameID[0] == 0) {
contentError("<item> node must game_type attribute",elemRoot);
return false;
}
item_type::item_type main_type = (item_type::item_type) INVALID_INDEX;
int subtype = INVALID_INDEX;
string game_type_s;
FOR_ENUM_ITEMS(item_type,i) {
game_type_s = strGameID;
if (game_type_s == ENUM_KEY_STR(item_type,i)) {
main_type = i;
break;
}
}
if(main_type == (item_type::item_type) INVALID_INDEX) {
contentError("<item> unknown game_type value",elemRoot);
return false;
}
if(strGameSub && strGameSub[0] != 0) {
// get subtype string, if available
string sub;
sub += strGameID;
sub += ":";
sub += strGameSub;
//process subtypes
ItemTypeInfo itemdef;
if(!itemdef.find(sub)) {
contentError("<item> unknown game_subtype value",elemRoot);
return false;
} else {
subtype = itemdef.subtype;
}
}
c_sprite sprite;
sprite.set_by_xml(elemRoot, basefile);
if(contentLoader->itemConfigs[main_type] == NULL) {
contentLoader->itemConfigs[main_type] = new ItemConfiguration;
}
//check for an existing item there.
if(subtype == INVALID_INDEX) {
if(!contentLoader->itemConfigs[main_type]->configured) {
contentLoader->itemConfigs[main_type]->configured = true;
contentLoader->itemConfigs[main_type]->default_sprite = sprite;
}
} else {
if(contentLoader->itemConfigs[main_type]->subItems.size() <= subtype) {
contentLoader->itemConfigs[main_type]->subItems.resize(subtype+1, NULL);
}
if(!contentLoader->itemConfigs[main_type]->subItems[subtype]) {
contentLoader->itemConfigs[main_type]->subItems[subtype] = new ItemSubConfiguration;
contentLoader->itemConfigs[main_type]->subItems[subtype]->sprite = sprite;
}
}
return true;
}
void flushItemConfig(vector<ItemConfiguration *> &config)
{
uint32_t currentsize = (uint32_t)config.size();
for (uint32_t i=0; i<currentsize; i++) {
if (config[i] != NULL) {
delete(config[i]);
}
}
config.clear();
if (currentsize < ENUM_LAST_ITEM(item_type)) {
currentsize = ENUM_LAST_ITEM(item_type);
}
config.resize(currentsize,NULL);
}