forked from DFHack/stonesense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreatureConfiguration.cpp
203 lines (186 loc) · 6.85 KB
/
CreatureConfiguration.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "common.h"
#include "CreatureConfiguration.h"
#include "Creatures.h"
#include "MapLoading.h"
#include "GUI.h"
#include "ContentLoader.h"
#include "tinyxml.h"
CreatureConfiguration::CreatureConfiguration(int professionID, const char* professionStr, uint8_t sex, int incaste, enumCreatureSpecialCases special, c_sprite &sprite, int shadow)
{
memset(this, 0, sizeof(CreatureConfiguration) );
this->sprite = sprite;
this->professionID = professionID;
this->sex = sex;
this->shadow = shadow;
this->special = special;
this->caste = incaste;
if(professionStr) {
int len = (int) strlen(professionStr);
if(len > CREATURESTRLENGTH) {
len = CREATURESTRLENGTH;
}
memcpy(this->professionstr, professionStr, len);
this->professionstr[CREATURESTRLENGTH-1]=0;
}
}
CreatureConfiguration::~CreatureConfiguration(void) {}
int translateProfession(const char* currentProf)
{
uint32_t j, dfNumJobs;
string proffStr;
if (currentProf == NULL || currentProf[0]==0) {
return INVALID_INDEX;
}
dfNumJobs = contentLoader->professionStrings.size();
for(j=0; j < dfNumJobs; j++) {
proffStr = contentLoader->professionStrings[j];
if( proffStr.compare( currentProf ) == 0) {
//assign ID
return j;
}
}
LogVerbose("Unable to match profession '%s' to anything in-game\n", currentProf);
return INT_MAX; //if it is left at INVALID_INDEX, the condition is ignored entierly.
}
void pushCreatureConfig( vector<vector<CreatureConfiguration>*>& knownCreatures, unsigned int gameID, CreatureConfiguration& cre)
{
if(!ssConfig.skipCreatureTypes) {
vector<CreatureConfiguration>* creatureList;
if (knownCreatures.size() <= gameID) {
//resize using hint from creature name list
unsigned int newsize = gameID +1;
if (newsize <= contentLoader->Mats->race.size()) {
newsize = contentLoader->Mats->race.size() + 1;
}
knownCreatures.resize(newsize);
}
creatureList = knownCreatures[gameID];
if (creatureList == NULL) {
creatureList = new vector<CreatureConfiguration>();
knownCreatures[gameID]=creatureList;
}
creatureList->push_back(cre);
}
}
bool addSingleCreatureConfig( TiXmlElement* elemCreature, vector<vector<CreatureConfiguration>*>& knownCreatures, int basefile )
{
if(ssConfig.skipCreatureTypes) {
return false;
}
int gameID = lookupIndexedType(elemCreature->Attribute("gameID"),contentLoader->Mats->race);
if (gameID == INVALID_INDEX) {
return false;
}
const char* sheetIndexStr;
int defaultFile = basefile;
c_sprite sprite;
sprite.set_fileindex(basefile);
uint8_t baseShadow = DEFAULT_SHADOW;
const char* shadowStr = elemCreature->Attribute("shadow");
if (shadowStr != NULL && shadowStr[0] != 0) {
baseShadow = atoi( shadowStr );
}
if (baseShadow < 0 || baseShadow > MAX_SHADOW) {
baseShadow = DEFAULT_SHADOW;
}
const char* filename = elemCreature->Attribute("file");
if (filename != NULL && filename[0] != 0) {
defaultFile = loadConfigImgFile((char*)filename,elemCreature);
if(defaultFile == -1) {
return false;
}
}
TiXmlElement* elemVariant = elemCreature->FirstChildElement("variant");
while( elemVariant ) {
int professionID = INVALID_INDEX;
const char* profStr = elemVariant->Attribute("prof");
if (profStr == NULL || profStr[0] == 0) {
profStr = elemVariant->Attribute("profession");
}
professionID = translateProfession(profStr);
const char* customStr = elemVariant->Attribute("custom");
if (customStr != NULL && customStr[0] == 0) {
customStr = NULL;
}
if (customStr != NULL) {
LogError("custom: %s\n",customStr);
}
const char* sexstr = elemVariant->Attribute("sex");
uint8_t cresex = 0;
if(sexstr) {
if(strcmp( sexstr, "M" ) == 0) {
cresex = 2;
}
if(strcmp( sexstr, "F" ) == 0) {
cresex = 1;
}
}
int caste = -1;
const char* caststr = elemVariant->Attribute("caste");
if (caststr != NULL && caststr[0] != 0) {
caste = lookupIndexedType(caststr,contentLoader->Mats->raceEx[gameID].castes);
}
const char* specstr = elemVariant->Attribute("special");
enumCreatureSpecialCases crespec = eCSC_Any;
if (specstr) {
if(strcmp( specstr, "Normal" ) == 0) {
crespec = eCSC_Normal;
}
if(strcmp( specstr, "Zombie" ) == 0) {
crespec = eCSC_Zombie;
}
if(strcmp( specstr, "Skeleton" ) == 0) {
crespec = eCSC_Skeleton;
}
if(strcmp( specstr, "Ghost" ) == 0) {
crespec = eCSC_Ghost;
}
if(strcmp( specstr, "Military" ) == 0) {
crespec = eCSC_Military;
}
}
int shadow = baseShadow;
const char* shadowStr = elemVariant->Attribute("shadow");
if (shadowStr != NULL && shadowStr[0] != 0) {
shadow = atoi( shadowStr );
}
if (shadow < 0 || shadow > MAX_SHADOW) {
shadow = baseShadow;
}
sprite.set_by_xml(elemVariant, defaultFile, gameID, caste);
sprite.animate = 0;
CreatureConfiguration cre( professionID, customStr , cresex, caste, crespec, sprite, shadow);
//add a copy to known creatures
pushCreatureConfig(knownCreatures, gameID, cre);
elemVariant = elemVariant->NextSiblingElement("variant");
}
sheetIndexStr = elemCreature->Attribute("sheetIndex");
if (sheetIndexStr) {
sprite.set_by_xml(elemCreature, basefile);
CreatureConfiguration cre( INVALID_INDEX, NULL, eCreatureSex_NA, INVALID_INDEX, eCSC_Any, sprite, baseShadow);
//add a copy to known creatures
pushCreatureConfig(knownCreatures, gameID, cre);
}
return true;
}
bool addCreaturesConfig( TiXmlElement* elemRoot, vector<vector<CreatureConfiguration>*>& knownCreatures )
{
int basefile = -1;
const char* filename = elemRoot->Attribute("file");
if (filename != NULL && filename[0] != 0) {
basefile = loadConfigImgFile((char*)filename,elemRoot);
if(basefile == -1) {
return false;
}
}
TiXmlElement* elemCreature = elemRoot->FirstChildElement("creature");
if (elemCreature == NULL) {
contentError("No creatures found",elemRoot);
return false;
}
while( elemCreature ) {
addSingleCreatureConfig(elemCreature,knownCreatures,basefile );
elemCreature = elemCreature->NextSiblingElement("creature");
}
return true;
}