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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
* The Mana World
* Copyright (C) 2004 The Mana World Development Team
*
* This file is part of The Mana World.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <set>
#include "action.h"
#include "animation.h"
#include "dye.h"
#include "image.h"
#include "imageset.h"
#include "resourcemanager.h"
#include "spritedef.h"
#include "../log.h"
#include "../utils/xml.h"
Action *SpriteDef::getAction(SpriteAction action) const
{
Actions::const_iterator i = mActions.find(action);
if (i == mActions.end())
{
logger->log("Warning: no action \"%u\" defined!", action);
return NULL;
}
return i->second;
}
SpriteDef *SpriteDef::load(const std::string &animationFile, int variant)
{
std::string::size_type pos = animationFile.find('|');
std::string palettes;
if (pos != std::string::npos)
palettes = animationFile.substr(pos + 1);
XML::Document doc(animationFile.substr(0, pos));
xmlNodePtr rootNode = doc.rootNode();
if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite"))
{
logger->log("Error, failed to parse %s", animationFile.c_str());
if (animationFile != "graphics/sprites/error.xml") {
return load("graphics/sprites/error.xml", 0);
} else {
return NULL;
}
}
SpriteDef *def = new SpriteDef;
def->loadSprite(rootNode, variant, palettes);
def->substituteActions();
return def;
}
void SpriteDef::substituteActions()
{
substituteAction(ACTION_STAND, ACTION_DEFAULT);
substituteAction(ACTION_WALK, ACTION_STAND);
substituteAction(ACTION_WALK, ACTION_RUN);
substituteAction(ACTION_ATTACK, ACTION_STAND);
substituteAction(ACTION_ATTACK_SWING, ACTION_ATTACK);
substituteAction(ACTION_ATTACK_STAB, ACTION_ATTACK_SWING);
substituteAction(ACTION_ATTACK_BOW, ACTION_ATTACK_STAB);
substituteAction(ACTION_ATTACK_THROW, ACTION_ATTACK_SWING);
substituteAction(ACTION_CAST_MAGIC, ACTION_ATTACK_SWING);
substituteAction(ACTION_USE_ITEM, ACTION_CAST_MAGIC);
substituteAction(ACTION_SIT, ACTION_STAND);
substituteAction(ACTION_SLEEP, ACTION_SIT);
substituteAction(ACTION_HURT, ACTION_STAND);
substituteAction(ACTION_DEAD, ACTION_HURT);
}
void SpriteDef::loadSprite(xmlNodePtr spriteNode, int variant,
const std::string &palettes)
{
// Get the variant
const int variantCount = XML::getProperty(spriteNode, "variants", 0);
int variant_offset = 0;
if (variantCount > 0 && variant < variantCount)
{
variant_offset =
variant * XML::getProperty(spriteNode, "variant_offset", 0);
}
for_each_xml_child_node(node, spriteNode)
{
if (xmlStrEqual(node->name, BAD_CAST "imageset"))
{
loadImageSet(node, palettes);
}
else if (xmlStrEqual(node->name, BAD_CAST "action"))
{
loadAction(node, variant_offset);
}
else if (xmlStrEqual(node->name, BAD_CAST "include"))
{
includeSprite(node);
}
}
}
void SpriteDef::loadImageSet(xmlNodePtr node, const std::string &palettes)
{
const std::string name = XML::getProperty(node, "name", "");
// We don't allow redefining image sets. This way, an included sprite
// definition will use the already loaded image set with the same name.
if (mImageSets.find(name) != mImageSets.end())
return;
const int width = XML::getProperty(node, "width", 0);
const int height = XML::getProperty(node, "height", 0);
std::string imageSrc = XML::getProperty(node, "src", "");
Dye::instantiate(imageSrc, palettes);
ResourceManager *resman = ResourceManager::getInstance();
ImageSet *imageSet = resman->getImageSet(imageSrc, width, height);
if (!imageSet)
{
logger->error("Couldn't load imageset!");
}
mImageSets[name] = imageSet;
}
void SpriteDef::loadAction(xmlNodePtr node, int variant_offset)
{
const std::string actionName = XML::getProperty(node, "name", "");
const std::string imageSetName = XML::getProperty(node, "imageset", "");
ImageSetIterator si = mImageSets.find(imageSetName);
if (si == mImageSets.end())
{
logger->log("Warning: imageset \"%s\" not defined in %s",
imageSetName.c_str(), getIdPath().c_str());
return;
}
ImageSet *imageSet = si->second;
SpriteAction actionType = makeSpriteAction(actionName);
if (actionType == ACTION_INVALID)
{
logger->log("Warning: Unknown action \"%s\" defined in %s",
actionName.c_str(), getIdPath().c_str());
return;
}
Action *action = new Action;
mActions[actionType] = action;
// When first action set it as default direction
if (mActions.empty())
{
mActions[ACTION_DEFAULT] = action;
}
// Load animations
for_each_xml_child_node(animationNode, node)
{
if (xmlStrEqual(animationNode->name, BAD_CAST "animation"))
{
loadAnimation(animationNode, action, imageSet, variant_offset);
}
}
}
void SpriteDef::loadAnimation(xmlNodePtr animationNode,
Action *action, ImageSet *imageSet,
int variant_offset)
{
const std::string directionName =
XML::getProperty(animationNode, "direction", "");
const SpriteDirection directionType = makeSpriteDirection(directionName);
if (directionType == DIRECTION_INVALID)
{
logger->log("Warning: Unknown direction \"%s\" used in %s",
directionName.c_str(), getIdPath().c_str());
return;
}
Animation *animation = new Animation;
action->setAnimation(directionType, animation);
// Get animation frames
for_each_xml_child_node(frameNode, animationNode)
{
const int delay = XML::getProperty(frameNode, "delay", 0);
int offsetX = XML::getProperty(frameNode, "offsetX", 0);
int offsetY = XML::getProperty(frameNode, "offsetY", 0);
offsetY -= imageSet->getHeight() - 32;
offsetX -= imageSet->getWidth() / 2 - 16;
if (xmlStrEqual(frameNode->name, BAD_CAST "frame"))
{
const int index = XML::getProperty(frameNode, "index", -1);
if (index < 0)
{
logger->log("No valid value for 'index'");
continue;
}
Image *img = imageSet->get(index + variant_offset);
if (!img)
{
logger->log("No image at index %d", index + variant_offset);
continue;
}
animation->addFrame(img, delay, offsetX, offsetY);
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "sequence"))
{
int start = XML::getProperty(frameNode, "start", -1);
const int end = XML::getProperty(frameNode, "end", -1);
if (start < 0 || end < 0)
{
logger->log("No valid value for 'start' or 'end'");
continue;
}
while (end >= start)
{
Image *img = imageSet->get(start + variant_offset);
if (!img)
{
logger->log("No image at index %d", start + variant_offset);
continue;
}
animation->addFrame(img, delay, offsetX, offsetY);
start++;
}
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "end"))
{
animation->addTerminator();
}
} // for frameNode
}
void SpriteDef::includeSprite(xmlNodePtr includeNode)
{
// TODO: Perform circular dependency check, since it's easy to crash the
// client this way.
const std::string filename = XML::getProperty(includeNode, "file", "");
if (filename.empty())
return;
XML::Document doc("graphics/sprites/" + filename);
xmlNodePtr rootNode = doc.rootNode();
if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite"))
{
logger->log("Error, no sprite root node in %s", filename.c_str());
return;
}
loadSprite(rootNode, 0);
}
void SpriteDef::substituteAction(SpriteAction complete, SpriteAction with)
{
if (mActions.find(complete) == mActions.end())
{
Actions::iterator i = mActions.find(with);
if (i != mActions.end()) {
mActions[complete] = i->second;
}
}
}
SpriteDef::~SpriteDef()
{
// Actions are shared, so ensure they are deleted only once.
std::set< Action * > actions;
for (Actions::const_iterator i = mActions.begin(),
i_end = mActions.end(); i != i_end; ++i)
{
actions.insert(i->second);
}
for (std::set< Action * >::const_iterator i = actions.begin(),
i_end = actions.end(); i != i_end; ++i)
{
delete *i;
}
for (ImageSetIterator i = mImageSets.begin();
i != mImageSets.end(); ++i)
{
i->second->decRef();
}
}
SpriteAction SpriteDef::makeSpriteAction(const std::string &action)
{
if (action.empty() || action == "default") {
return ACTION_DEFAULT;
}
if (action == "stand") {
return ACTION_STAND;
}
else if (action == "walk") {
return ACTION_WALK;
}
else if (action == "run") {
return ACTION_RUN;
}
else if (action == "attack") {
return ACTION_ATTACK;
}
else if (action == "attack_swing") {
return ACTION_ATTACK_SWING;
}
else if (action == "attack_stab") {
return ACTION_ATTACK_STAB;
}
else if (action == "attack_bow") {
return ACTION_ATTACK_BOW;
}
else if (action == "attack_throw") {
return ACTION_ATTACK_THROW;
}
else if (action == "cast_magic") {
return ACTION_CAST_MAGIC;
}
else if (action == "use_item") {
return ACTION_USE_ITEM;
}
else if (action == "sit") {
return ACTION_SIT;
}
else if (action == "sleep") {
return ACTION_SLEEP;
}
else if (action == "hurt") {
return ACTION_HURT;
}
else if (action == "dead") {
return ACTION_DEAD;
}
else {
return ACTION_INVALID;
}
}
SpriteDirection SpriteDef::makeSpriteDirection(const std::string& direction)
{
if (direction.empty() || direction == "default") {
return DIRECTION_DEFAULT;
}
else if (direction == "up") {
return DIRECTION_UP;
}
else if (direction == "left") {
return DIRECTION_LEFT;
}
else if (direction == "right") {
return DIRECTION_RIGHT;
}
else if (direction == "down") {
return DIRECTION_DOWN;
}
else {
return DIRECTION_INVALID;
}
}
|