]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.cpp
added hard support for path names in source files
[l2e.git] / src / loader / Interpreter.cpp
1 /*
2  * Interpreter.cpp
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #include "Interpreter.h"
9
10 #include "ParsedSource.h"
11 #include "../battle/Hero.h"
12 #include "../battle/Monster.h"
13 #include "../battle/PartyLayout.h"
14 #include "../common/Ikari.h"
15 #include "../common/Item.h"
16 #include "../common/Spell.h"
17 #include "../common/TargetingMode.h"
18 #include "../graphics/ComplexAnimation.h"
19 #include "../graphics/Font.h"
20 #include "../graphics/Frame.h"
21 #include "../graphics/Gauge.h"
22 #include "../graphics/SimpleAnimation.h"
23 #include "../graphics/Sprite.h"
24
25 #include <algorithm>
26 #include <cstring>
27 #include <SDL_image.h>
28
29 using battle::Hero;
30 using battle::Monster;
31 using battle::PartyLayout;
32 using battle::Stats;
33 using common::Ikari;
34 using common::Item;
35 using common::Spell;
36 using common::TargetingMode;
37 using graphics::Animation;
38 using graphics::Font;
39 using graphics::Frame;
40 using graphics::Gauge;
41 using graphics::ComplexAnimation;
42 using graphics::SimpleAnimation;
43 using graphics::Sprite;
44 using geometry::Vector;
45 using std::make_pair;
46 using std::map;
47 using std::set;
48 using std::string;
49 using std::vector;
50
51 namespace loader {
52
53 Interpreter::~Interpreter() {
54         for (vector<ComplexAnimation *>::const_iterator i(complexAnimations.begin()), end(complexAnimations.end()); i != end; ++i) {
55                 delete *i;
56         }
57         for (vector<Font *>::const_iterator i(fonts.begin()), end(fonts.end()); i != end; ++i) {
58                 delete *i;
59         }
60         for (vector<Frame *>::const_iterator i(frames.begin()), end(frames.end()); i != end; ++i) {
61                 delete *i;
62         }
63         for (vector<Gauge *>::const_iterator i(gauges.begin()), end(gauges.end()); i != end; ++i) {
64                 delete *i;
65         }
66         for (vector<Hero *>::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) {
67                 delete *i;
68         }
69         for (vector<Ikari *>::const_iterator i(ikaris.begin()), end(ikaris.end()); i != end; ++i) {
70                 delete *i;
71         }
72         for (vector<SDL_Surface *>::const_iterator i(images.begin()), end(images.end()); i != end; ++i) {
73                 SDL_FreeSurface(*i);
74         }
75         for (vector<Item *>::const_iterator i(items.begin()), end(items.end()); i != end; ++i) {
76                 delete *i;
77         }
78         for (vector<Monster *>::const_iterator i(monsters.begin()), end(monsters.end()); i != end; ++i) {
79                 delete *i;
80         }
81         for (vector<PartyLayout *>::const_iterator i(partyLayouts.begin()), end(partyLayouts.end()); i != end; ++i) {
82                 delete *i;
83         }
84         for (vector<SimpleAnimation *>::const_iterator i(simpleAnimations.begin()), end(simpleAnimations.end()); i != end; ++i) {
85                 delete *i;
86         }
87         for (vector<Spell *>::const_iterator i(spells.begin()), end(spells.end()); i != end; ++i) {
88                 delete *i;
89         }
90         for (vector<Sprite *>::const_iterator i(sprites.begin()), end(sprites.end()); i != end; ++i) {
91                 delete *i;
92         }
93         for (vector<const char *>::const_iterator i(strings.begin()), end(strings.end()); i != end; ++i) {
94                 delete *i;
95         }
96         for (vector<TargetingMode *>::const_iterator i(targetingModes.begin()), end(targetingModes.end()); i != end; ++i) {
97                 delete *i;
98         }
99 }
100
101
102 Animation *Interpreter::GetAnimation(const std::string &name) {
103         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
104         if (i != parsedDefinitions.end()) {
105                 if (i->second.type == COMPLEX_ANIMATION) {
106                         return complexAnimations[i->second.index];
107                 } else if (i->second.type == SIMPLE_ANIMATION) {
108                         return simpleAnimations[i->second.index];
109                 } else {
110                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Animation");
111                 }
112         } else {
113                 throw Error("access to undefined Animation " + name);
114         }
115 }
116
117 bool Interpreter::GetBoolean(const std::string &name) const {
118         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
119         if (i != parsedDefinitions.end()) {
120                 if (i->second.type == BOOLEAN) {
121                         return booleans[i->second.index];
122                 } else {
123                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Boolean");
124                 }
125         } else {
126                 throw Error("access to undefined Boolean " + name);
127         }
128 }
129
130 Font *Interpreter::GetFont(const std::string &name) {
131         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
132         if (i != parsedDefinitions.end()) {
133                 if (i->second.type == FONT) {
134                         return fonts[i->second.index];
135                 } else {
136                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Font");
137                 }
138         } else {
139                 throw Error("access to undefined Font " + name);
140         }
141 }
142
143 Frame *Interpreter::GetFrame(const std::string &name) {
144         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
145         if (i != parsedDefinitions.end()) {
146                 if (i->second.type == FRAME) {
147                         return frames[i->second.index];
148                 } else {
149                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Frame");
150                 }
151         } else {
152                 throw Error("access to undefined Frame " + name);
153         }
154 }
155
156 Gauge *Interpreter::GetGauge(const std::string &name) {
157         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
158         if (i != parsedDefinitions.end()) {
159                 if (i->second.type == GAUGE) {
160                         return gauges[i->second.index];
161                 } else {
162                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Gauge");
163                 }
164         } else {
165                 throw Error("access to undefined Gauge " + name);
166         }
167 }
168
169 Hero *Interpreter::GetHero(const std::string &name) {
170         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
171         if (i != parsedDefinitions.end()) {
172                 if (i->second.type == HERO) {
173                         return heroes[i->second.index];
174                 } else {
175                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Hero");
176                 }
177         } else {
178                 throw Error("access to undefined Hero " + name);
179         }
180 }
181
182 Ikari *Interpreter::GetIkari(const std::string &name) {
183         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
184         if (i != parsedDefinitions.end()) {
185                 if (i->second.type == IKARI) {
186                         return ikaris[i->second.index];
187                 } else {
188                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Ikari");
189                 }
190         } else {
191                 throw Error("access to undefined Ikari " + name);
192         }
193 }
194
195 Item *Interpreter::GetItem(const std::string &name) {
196         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
197         if (i != parsedDefinitions.end()) {
198                 if (i->second.type == ITEM) {
199                         return items[i->second.index];
200                 } else {
201                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Item");
202                 }
203         } else {
204                 throw Error("access to undefined Item " + name);
205         }
206 }
207
208 Monster *Interpreter::GetMonster(const std::string &name) {
209         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
210         if (i != parsedDefinitions.end()) {
211                 if (i->second.type == MONSTER) {
212                         return monsters[i->second.index];
213                 } else {
214                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Monster");
215                 }
216         } else {
217                 throw Error("access to undefined Monster " + name);
218         }
219 }
220
221 int Interpreter::GetNumber(const std::string &name) const {
222         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
223         if (i != parsedDefinitions.end()) {
224                 if (i->second.type == NUMBER) {
225                         return numbers[i->second.index];
226                 } else {
227                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Number");
228                 }
229         } else {
230                 throw Error("access to undefined Number " + name);
231         }
232 }
233
234 PartyLayout *Interpreter::GetPartyLayout(const std::string &name) {
235         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
236         if (i != parsedDefinitions.end()) {
237                 if (i->second.type == PARTY_LAYOUT) {
238                         return partyLayouts[i->second.index];
239                 } else {
240                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to PartyLayout");
241                 }
242         } else {
243                 throw Error("access to undefined PartyLayout " + name);
244         }
245 }
246
247 const char *Interpreter::GetPath(const std::string &name) const {
248         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
249         if (i != parsedDefinitions.end()) {
250                 if (i->second.type == PATH) {
251                         return strings[i->second.index];
252                 } else {
253                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Path");
254                 }
255         } else {
256                 throw Error("access to undefined Path " + name);
257         }
258 }
259
260 Spell *Interpreter::GetSpell(const std::string &name) {
261         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
262         if (i != parsedDefinitions.end()) {
263                 if (i->second.type == SPELL) {
264                         return spells[i->second.index];
265                 } else {
266                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Spell");
267                 }
268         } else {
269                 throw Error("access to undefined Spell " + name);
270         }
271 }
272
273 Sprite *Interpreter::GetSprite(const std::string &name) {
274         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
275         if (i != parsedDefinitions.end()) {
276                 if (i->second.type == SPRITE) {
277                         return sprites[i->second.index];
278                 } else {
279                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Sprite");
280                 }
281         } else {
282                 throw Error("access to undefined Sprite " + name);
283         }
284 }
285
286 const char *Interpreter::GetString(const std::string &name) const {
287         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
288         if (i != parsedDefinitions.end()) {
289                 // TODO: enable path to string casting some time
290                 if (i->second.type == STRING /* || i->second.type == PATH */) {
291                         return strings[i->second.index];
292                 } else {
293                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to String");
294                 }
295         } else {
296                 throw Error("access to undefined String " + name);
297         }
298 }
299
300 TargetingMode *Interpreter::GetTargetingMode(const std::string &name) {
301         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
302         if (i != parsedDefinitions.end()) {
303                 if (i->second.type == TARGETING_MODE) {
304                         return targetingModes[i->second.index];
305                 } else {
306                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to TargetingMode");
307                 }
308         } else {
309                 throw Error("access to undefined TargetingMode " + name);
310         }
311 }
312
313 Vector<int> Interpreter::GetVector(const std::string &name) const {
314         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
315         if (i != parsedDefinitions.end()) {
316                 if (i->second.type == VECTOR) {
317                         return vectors[i->second.index];
318                 } else {
319                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Vector");
320                 }
321         } else {
322                 throw Error("access to undefined Vector " + name);
323         }
324 }
325
326
327 void Interpreter::ReadSource() {
328         for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
329                 ReadDefinition(source.GetDefinition(*i));
330         }
331 }
332
333 void Interpreter::ReadDefinition(const Definition &dfn) {
334         if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
335                 return;
336         }
337         if (dfn.HasLiteralValue()) {
338                 ReadLiteral(dfn);
339         } else {
340                 ReadObject(dfn);
341         }
342 }
343
344 void Interpreter::ReadLiteral(const Definition &dfn) {
345         switch (dfn.GetLiteral()->GetType()) {
346                 case Literal::ARRAY_VALUES:
347                         valueArrays.push_back(dfn.GetLiteral()->GetValues());
348                         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, VALUE_ARRAY, valueArrays.size() - 1)));
349                         break;
350                 case Literal::ARRAY_PROPS:
351                         propertyListArrays.push_back(dfn.GetLiteral()->GetPropertyLists());
352                         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, PROPERTY_LIST_ARRAY, propertyListArrays.size() - 1)));
353                         break;
354                 case Literal::BOOLEAN:
355                         booleans.push_back(dfn.GetLiteral()->GetBoolean());
356                         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, BOOLEAN, booleans.size() - 1)));
357                         break;
358                 case Literal::COLOR:
359                         throw Error("unhandled literal: color");
360                         break;
361                 case Literal::NUMBER:
362                         numbers.push_back(dfn.GetLiteral()->GetNumber());
363                         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, NUMBER, numbers.size() - 1)));
364                         break;
365                 case Literal::PATH:
366                         {
367                                 char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
368                                 std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size());
369                                 str[dfn.GetLiteral()->GetString().size()] = '\0';
370                                 strings.push_back(str);
371                         }
372                         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, PATH, strings.size() - 1)));
373                         break;
374                 case Literal::STRING:
375                         {
376                                 char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
377                                 std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size());
378                                 str[dfn.GetLiteral()->GetString().size()] = '\0';
379                                 strings.push_back(str);
380                         }
381                         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, STRING, strings.size() - 1)));
382                         break;
383                 case Literal::VECTOR:
384                         vectors.push_back(Vector<int>(dfn.GetLiteral()->GetX(), dfn.GetLiteral()->GetY()));
385                         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, VECTOR, vectors.size() - 1)));
386                         break;
387                 case Literal::OBJECT:
388                         ReadObject(dfn);
389                         break;
390         }
391 }
392
393 Animation *Interpreter::GetAnimation(const Value &v) {
394         if (v.IsLiteral()) {
395                 if (v.GetLiteral().GetTypeName() == "ComplexAnimation") {
396                         ComplexAnimation *a(new ComplexAnimation);
397                         ReadComplexAnimation(*a, *v.GetLiteral().GetProperties());
398                         complexAnimations.push_back(a);
399                         return a;
400                 } else {
401                         SimpleAnimation *a(new SimpleAnimation);
402                         ReadSimpleAnimation(*a, *v.GetLiteral().GetProperties());
403                         simpleAnimations.push_back(a);
404                         return a;
405                 }
406         } else {
407                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
408                 return GetAnimation(v.GetIdentifier());
409         }
410 }
411
412 bool Interpreter::GetBoolean(const Value &v) {
413         if (v.IsLiteral()) {
414                 return v.GetLiteral().GetBoolean();
415         } else {
416                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
417                 return GetBoolean(v.GetIdentifier());
418         }
419 }
420
421 Font *Interpreter::GetFont(const Value &v) {
422         if (v.IsLiteral()) {
423                 Font *f(new Font);
424                 ReadFont(*f, *v.GetLiteral().GetProperties());
425                 return f;
426         } else {
427                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
428                 return GetFont(v.GetIdentifier());
429         }
430 }
431
432 Frame *Interpreter::GetFrame(const Value &v) {
433         if (v.IsLiteral()) {
434                 Frame *f(new Frame);
435                 ReadFrame(*f, *v.GetLiteral().GetProperties());
436                 return f;
437         } else {
438                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
439                 return GetFrame(v.GetIdentifier());
440         }
441 }
442
443 Gauge *Interpreter::GetGauge(const Value &v) {
444         if (v.IsLiteral()) {
445                 Gauge *g(new Gauge);
446                 ReadGauge(*g, *v.GetLiteral().GetProperties());
447                 return g;
448         } else {
449                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
450                 return GetGauge(v.GetIdentifier());
451         }
452 }
453
454 Hero *Interpreter::GetHero(const Value &v) {
455         if (v.IsLiteral()) {
456                 Hero *h(new Hero);
457                 ReadHero(*h, *v.GetLiteral().GetProperties());
458                 return h;
459         } else {
460                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
461                 return GetHero(v.GetIdentifier());
462         }
463 }
464
465 Ikari *Interpreter::GetIkari(const Value &v) {
466         if (v.IsLiteral()) {
467                 Ikari *i(new Ikari);
468                 ReadIkari(*i, *v.GetLiteral().GetProperties());
469                 return i;
470         } else {
471                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
472                 return GetIkari(v.GetIdentifier());
473         }
474 }
475
476 SDL_Surface *Interpreter::GetImage(const Value &v) {
477         string path(GetPath(v));
478         map<string, SDL_Surface *>::const_iterator i(imageCache.find(path));
479         if (i == imageCache.end()) {
480                 SDL_Surface *image(IMG_Load(path.c_str()));
481                 images.push_back(image);
482                 imageCache.insert(make_pair(path, image));
483                 return image;
484         } else {
485                 return i->second;
486         }
487 }
488
489 Item *Interpreter::GetItem(const Value &v) {
490         if (v.IsLiteral()) {
491                 Item *i(new Item);
492                 ReadItem(*i, *v.GetLiteral().GetProperties());
493                 return i;
494         } else {
495                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
496                 return GetItem(v.GetIdentifier());
497         }
498 }
499
500 int Interpreter::GetNumber(const Value &v) {
501         if (v.IsLiteral()) {
502                 return v.GetLiteral().GetNumber();
503         } else {
504                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
505                 return GetNumber(v.GetIdentifier());
506         }
507 }
508
509 PartyLayout *Interpreter::GetPartyLayout(const Value &v) {
510         if (v.IsLiteral()) {
511                 PartyLayout *l(new PartyLayout);
512                 ReadPartyLayout(*l, *v.GetLiteral().GetProperties());
513                 return l;
514         } else {
515                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
516                 return GetPartyLayout(v.GetIdentifier());
517         }
518 }
519
520 const char *Interpreter::GetPath(const Value &v) {
521         if (v.IsLiteral()) {
522                 return v.GetLiteral().GetString().c_str();
523         } else {
524                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
525                 return GetPath(v.GetIdentifier());
526         }
527 }
528
529 const PropertyList *Interpreter::GetPropertyList(const Value &v) {
530         if (v.IsLiteral()) {
531                 return v.GetLiteral().GetProperties();
532         } else {
533                 throw Error("cannot reference property lists");
534         }
535 }
536
537 const vector<PropertyList *> &Interpreter::GetPropertyListArray(const Value &v) {
538         if (v.IsLiteral()) {
539                 return v.GetLiteral().GetPropertyLists();
540         } else {
541                 throw Error("cannot reference property list arrays");
542         }
543 }
544
545 Spell *Interpreter::GetSpell(const Value &v) {
546         if (v.IsLiteral()) {
547                 Spell *s(new Spell);
548                 ReadSpell(*s, *v.GetLiteral().GetProperties());
549                 return s;
550         } else {
551                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
552                 return GetSpell(v.GetIdentifier());
553         }
554 }
555
556 Sprite *Interpreter::GetSprite(const Value &v) {
557         if (v.IsLiteral()) {
558                 Sprite *s(new Sprite);
559                 ReadSprite(*s, *v.GetLiteral().GetProperties());
560                 return s;
561         } else {
562                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
563                 return GetSprite(v.GetIdentifier());
564         }
565 }
566
567 const char *Interpreter::GetString(const Value &v) {
568         if (v.IsLiteral()) {
569                 return v.GetLiteral().GetString().c_str();
570         } else {
571                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
572                 return GetString(v.GetIdentifier());
573         }
574 }
575
576 TargetingMode *Interpreter::GetTargetingMode(const Value &v) {
577         if (v.IsLiteral()) {
578                 TargetingMode *t(new TargetingMode);
579                 ReadTargetingMode(*t, *v.GetLiteral().GetProperties());
580                 return t;
581         } else {
582                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
583                 return GetTargetingMode(v.GetIdentifier());
584         }
585 }
586
587 Vector<int> Interpreter::GetVector(const Value &v) {
588         if (v.IsLiteral()) {
589                 return Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY());
590         } else {
591                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
592                 return GetVector(v.GetIdentifier());
593         }
594 }
595
596 const vector<Value *> &Interpreter::GetValueArray(const Value &v) {
597         if (v.IsLiteral()) {
598                 return v.GetLiteral().GetValues();
599         } else {
600                 throw Error("cannot reference value arrays");
601         }
602 }
603
604
605 void Interpreter::ReadObject(const Definition &dfn) {
606         if (dfn.TypeName() == "ComplexAnimation") {
607                 ComplexAnimation *animation(new ComplexAnimation);
608                 int index(complexAnimations.size());
609                 complexAnimations.push_back(animation);
610                 ReadComplexAnimation(*animation, *dfn.GetProperties());
611                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, COMPLEX_ANIMATION, index)));
612         } else if (dfn.TypeName() == "Font") {
613                 Font *font(new Font);
614                 int index(fonts.size());
615                 fonts.push_back(font);
616                 ReadFont(*font, *dfn.GetProperties());
617                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, FONT, index)));
618         } else if (dfn.TypeName() == "Frame") {
619                 Frame *frame(new Frame);
620                 int index(frames.size());
621                 frames.push_back(frame);
622                 ReadFrame(*frame, *dfn.GetProperties());
623                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, FRAME, index)));
624         } else if (dfn.TypeName() == "Gauge") {
625                 Gauge *gauge(new Gauge);
626                 int index(gauges.size());
627                 gauges.push_back(gauge);
628                 ReadGauge(*gauge, *dfn.GetProperties());
629                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, GAUGE, index)));
630         } else if (dfn.TypeName() == "Hero") {
631                 Hero *hero(new Hero);
632                 int index(heroes.size());
633                 heroes.push_back(hero);
634                 ReadHero(*hero, *dfn.GetProperties());
635                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, HERO, index)));
636         } else if (dfn.TypeName() == "Ikari") {
637                 Ikari *ikari(new Ikari);
638                 int index(ikaris.size());
639                 ikaris.push_back(ikari);
640                 ReadIkari(*ikari, *dfn.GetProperties());
641                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, IKARI, index)));
642         } else if (dfn.TypeName() == "Item") {
643                 Item *item(new Item);
644                 int index(items.size());
645                 items.push_back(item);
646                 ReadItem(*item, *dfn.GetProperties());
647                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, ITEM, index)));
648         } else if (dfn.TypeName() == "Monster") {
649                 Monster *monster(new Monster);
650                 int index(monsters.size());
651                 monsters.push_back(monster);
652                 ReadMonster(*monster, *dfn.GetProperties());
653                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, MONSTER, index)));
654         } else if (dfn.TypeName() == "PartyLayout") {
655                 PartyLayout *layout(new PartyLayout);
656                 int index(partyLayouts.size());
657                 partyLayouts.push_back(layout);
658                 ReadPartyLayout(*layout, *dfn.GetProperties());
659                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, PARTY_LAYOUT, index)));
660         } else if (dfn.TypeName() == "SimpleAnimation") {
661                 SimpleAnimation *animation(new SimpleAnimation);
662                 int index(simpleAnimations.size());
663                 simpleAnimations.push_back(animation);
664                 ReadSimpleAnimation(*animation, *dfn.GetProperties());
665                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SIMPLE_ANIMATION, index)));
666         } else if (dfn.TypeName() == "Spell") {
667                 Spell *spell(new Spell);
668                 int index(spells.size());
669                 spells.push_back(spell);
670                 ReadSpell(*spell, *dfn.GetProperties());
671                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SPELL, index)));
672         } else if (dfn.TypeName() == "Sprite") {
673                 Sprite *sprite(new Sprite);
674                 int index(sprites.size());
675                 sprites.push_back(sprite);
676                 ReadSprite(*sprite, *dfn.GetProperties());
677                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SPRITE, index)));
678         } else if (dfn.TypeName() == "TargetingMode") {
679                 TargetingMode *mode(new TargetingMode);
680                 int index(targetingModes.size());
681                 targetingModes.push_back(mode);
682                 ReadTargetingMode(*mode, *dfn.GetProperties());
683                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, TARGETING_MODE, index)));
684         } else {
685                 throw Error("unhandled object type: " + dfn.TypeName());
686         }
687 }
688
689
690 void Interpreter::ReadComplexAnimation(ComplexAnimation &a, const PropertyList &props) {
691         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
692                 if (i->first == "sprite") {
693                         a.SetSprite(GetSprite(*i->second));
694                 } else if (i->first == "frametime") {
695                         a.SetFrameTime(GetNumber(*i->second));
696                 } else if (i->first == "repeat") {
697                         a.SetRepeat(GetBoolean(*i->second));
698                 } else if (i->first == "frames") {
699                         const vector<PropertyList *> &values(GetPropertyListArray(*i->second));
700                         for (vector<PropertyList *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
701                                 ComplexAnimation::FrameProp frame;
702                                 ReadComplexAnimationFrame(frame, **i);
703                                 a.AddFrame(frame);
704                         }
705                 } else {
706                         throw Error("unknown ComplexAnimation property: " + i->first);
707                 }
708         }
709 }
710
711 void Interpreter::ReadComplexAnimationFrame(ComplexAnimation::FrameProp &f, const PropertyList &props) {
712         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
713                 if (i->first == "column") {
714                         f.col = GetNumber(*i->second);
715                 } else if (i->first == "row") {
716                         f.row = GetNumber(*i->second);
717                 } else if (i->first == "disposition") {
718                         f.disposition = GetVector(*i->second);
719                 } else {
720                         throw Error("unknown ComplexAnimationFrame property: " + i->first);
721                 }
722         }
723 }
724
725 void Interpreter::ReadFont(Font &f, const PropertyList &props) {
726         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
727                 if (i->first == "sprite") {
728                         f.SetSprite(GetSprite(*i->second));
729                 } else if (i->first == "columnoffset") {
730                         f.SetColOffset(GetNumber(*i->second));
731                 } else if (i->first == "rowoffset") {
732                         f.SetRowOffset(GetNumber(*i->second));
733                 } else {
734                         throw Error("unknown Font property: " + i->first);
735                 }
736         }
737 }
738
739 void Interpreter::ReadFrame(Frame &f, const PropertyList &props) {
740         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
741                 if (i->first == "image") {
742                         f.SetSurface(GetImage(*i->second));
743                 } else if (i->first == "border") {
744                         f.SetBorderSize(GetVector(*i->second));
745                 } else if (i->first == "repeat") {
746                         f.SetRepeatSize(GetVector(*i->second));
747                 } else if (i->first == "offset") {
748                         f.SetOffset(GetVector(*i->second));
749                 } else {
750                         throw Error("unknown Frame property: " + i->first);
751                 }
752         }
753 }
754
755 void Interpreter::ReadGauge(Gauge &g, const PropertyList &props) {
756         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
757                 if (i->first == "image") {
758                         g.SetSurface(GetImage(*i->second));
759                 } else if (i->first == "full") {
760                         g.SetFullOffset(GetVector(*i->second));
761                 } else if (i->first == "empty") {
762                         g.SetEmptyOffset(GetVector(*i->second));
763                 } else if (i->first == "height") {
764                         g.SetHeight(GetNumber(*i->second));
765                 } else if (i->first == "start") {
766                         g.SetStartWidth(GetNumber(*i->second));
767                 } else if (i->first == "repeat") {
768                         g.SetRepeatWidth(GetNumber(*i->second));
769                 } else if (i->first == "end") {
770                         g.SetEndWidth(GetNumber(*i->second));
771                 } else {
772                         throw Error("unknown Gauge property: " + i->first);
773                 }
774         }
775 }
776
777 void Interpreter::ReadIkari(Ikari &ikari, const PropertyList &props) {
778         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
779                 if (i->first == "name") {
780                         ikari.SetName(GetString(*i->second));
781                 } else if (i->first == "cost") {
782                         ikari.SetCost(GetNumber(*i->second));
783                 } else if (i->first == "targets") {
784                         ikari.GetTargetingMode() = *GetTargetingMode(*i->second);
785                 } else if (i->first == "magical") {
786                         if (GetBoolean(*i->second)) {
787                                 ikari.SetMagical();
788                         }
789                 } else if (i->first == "physical") {
790                         if (GetBoolean(*i->second)) {
791                                 ikari.SetPhysical();
792                         }
793                 } else {
794                         throw Error("unknown Ikari property: " + i->first);
795                 }
796         }
797 }
798
799 void Interpreter::ReadItem(Item &item, const PropertyList &props) {
800         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
801                 if (i->first == "name") {
802                         item.SetName(GetString(*i->second));
803                 } else if (i->first == "menuicon") {
804                         item.SetMenuIcon(GetSprite(*i->second));
805                 } else if (i->first == "battle") {
806                         if (GetBoolean(*i->second)) {
807                                 item.SetUsableInBattle();
808                         }
809                 } else if (i->first == "targets") {
810                         item.GetTargetingMode() = *GetTargetingMode(*i->second);
811                 } else if (i->first == "ikari") {
812                         item.SetIkari(GetIkari(*i->second));
813                 } else if (i->first == "attackanimation") {
814                         item.SetAttackAnimation(GetAnimation(*i->second));
815                 } else {
816                         throw Error("unknown Item property: " + i->first);
817                 }
818         }
819 }
820
821 void Interpreter::ReadHero(Hero &h, const PropertyList &props) {
822         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
823                 if (i->first == "name") {
824                         h.SetName(GetString(*i->second));
825                 } else if (i->first == "sprite") {
826                         h.SetSprite(GetSprite(*i->second));
827                 } else if (i->first == "level") {
828                         h.SetLevel(GetNumber(*i->second));
829                 } else if (i->first == "maxHealth") {
830                         h.SetMaxHealth(GetNumber(*i->second));
831                 } else if (i->first == "health") {
832                         h.SetHealth(GetNumber(*i->second));
833                 } else if (i->first == "maxMana") {
834                         h.SetMaxMana(GetNumber(*i->second));
835                 } else if (i->first == "mana") {
836                         h.SetMana(GetNumber(*i->second));
837                 } else if (i->first == "ip") {
838                         h.SetIP(GetNumber(*i->second));
839                 } else if (i->first == "stats") {
840                         battle::Stats stats;
841                         ReadStats(stats, *GetPropertyList(*i->second));
842                         h.SetStats(stats);
843                 } else if (i->first == "attackAnimation") {
844                         h.SetAttackAnimation(GetAnimation(*i->second));
845                 } else if (i->first == "spellAnimation") {
846                         h.SetSpellAnimation(GetAnimation(*i->second));
847                 } else if (i->first == "meleeAnimation") {
848                         h.SetMeleeAnimation(GetAnimation(*i->second));
849                 } else {
850                         throw Error("unknown Hero property: " + i->first);
851                 }
852         }
853 }
854
855 void Interpreter::ReadMonster(Monster &m, const PropertyList &props) {
856         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
857                 if (i->first == "name") {
858                         m.SetName(GetString(*i->second));
859                 } else if (i->first == "sprite") {
860                         m.SetSprite(GetSprite(*i->second));
861                 } else if (i->first == "level") {
862                         m.SetLevel(GetNumber(*i->second));
863                 } else if (i->first == "maxHealth") {
864                         m.SetMaxHealth(GetNumber(*i->second));
865                 } else if (i->first == "health") {
866                         m.SetHealth(GetNumber(*i->second));
867                 } else if (i->first == "maxMana") {
868                         m.SetMaxMana(GetNumber(*i->second));
869                 } else if (i->first == "mana") {
870                         m.SetMana(GetNumber(*i->second));
871                 } else if (i->first == "stats") {
872                         battle::Stats stats;
873                         ReadStats(stats, *GetPropertyList(*i->second));
874                         m.SetStats(stats);
875                 } else if (i->first == "attackAnimation") {
876                         m.SetAttackAnimation(GetAnimation(*i->second));
877                 } else if (i->first == "meleeAnimation") {
878                         m.SetMeleeAnimation(GetAnimation(*i->second));
879                 } else {
880                         throw Error("unknown Monster property: " + i->first);
881                 }
882         }
883 }
884
885 void Interpreter::ReadPartyLayout(PartyLayout &p, const PropertyList &props) {
886         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
887                 if (i->first == "positions") {
888                         const vector<Value *> &positions(GetValueArray(*i->second));
889                         for (vector<Value *>::const_iterator j(positions.begin()), end(positions.end()); j != end; ++j) {
890                                 p.AddPosition(GetVector(**j));
891                         }
892                 } else {
893                         throw Error("unknown PartyLayout property: " + i->first);
894                 }
895         }
896 }
897
898 void Interpreter::ReadSimpleAnimation(SimpleAnimation &a, const PropertyList &props) {
899         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
900                 if (i->first == "sprite") {
901                         a.SetSprite(GetSprite(*i->second));
902                 } else if (i->first == "frametime") {
903                         a.SetFrameTime(GetNumber(*i->second));
904                 } else if (i->first == "repeat") {
905                         a.SetRepeat(GetBoolean(*i->second));
906                 } else if (i->first == "framecount") {
907                         a.SetNumFrames(GetNumber(*i->second));
908                 } else if (i->first == "col") {
909                         a.SetCol(GetNumber(*i->second));
910                 } else if (i->first == "row") {
911                         a.SetRow(GetNumber(*i->second));
912                 } else {
913                         throw Error("unknown SimpleAnimation property: " + i->first);
914                 }
915         }
916 }
917
918 void Interpreter::ReadSpell(Spell &s, const PropertyList &props) {
919         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
920                 if (i->first == "name") {
921                         s.SetName(GetString(*i->second));
922                 } else if (i->first == "cost") {
923                         s.SetCost(GetNumber(*i->second));
924                 } else if (i->first == "battle") {
925                         if (GetBoolean(*i->second)) {
926                                 s.SetUsableInBattle();
927                         }
928                 } else if (i->first == "targets") {
929                         s.GetTargetingMode() = *GetTargetingMode(*i->second);
930                 } else {
931                         throw Error("unknown Spell property: " + i->first);
932                 }
933         }
934 }
935
936 void Interpreter::ReadSprite(Sprite &s, const PropertyList &props) {
937         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
938                 if (i->first == "image") {
939                         s.SetSurface(GetImage(*i->second));
940                 } else if (i->first == "size") {
941                         s.SetSize(GetVector(*i->second));
942                 } else if (i->first == "offset") {
943                         s.SetOffset(GetVector(*i->second));
944                 } else {
945                         throw Error("unknown Sprite property: " + i->first);
946                 }
947         }
948 }
949
950 void Interpreter::ReadStats(Stats &s, const PropertyList &props) {
951         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
952                 if (i->first == "atp") {
953                         s.SetAttack(GetNumber(*i->second));
954                 } else if (i->first == "dfp") {
955                         s.SetDefense(GetNumber(*i->second));
956                 } else if (i->first == "str") {
957                         s.SetStrength(GetNumber(*i->second));
958                 } else if (i->first == "agl") {
959                         s.SetAgility(GetNumber(*i->second));
960                 } else if (i->first == "int") {
961                         s.SetIntelligence(GetNumber(*i->second));
962                 } else if (i->first == "gut") {
963                         s.SetGut(GetNumber(*i->second));
964                 } else if (i->first == "mgr") {
965                         s.SetMagicResistance(GetNumber(*i->second));
966                 } else {
967                         throw Error("unknown Stats property: " + i->first);
968                 }
969         }
970 }
971
972 void Interpreter::ReadTargetingMode(TargetingMode &t, const PropertyList &props) {
973         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
974                 if (i->first == "ally") {
975                         if (GetBoolean(*i->second)) {
976                                 t.TargetAlly();
977                         } else {
978                                 t.TargetEnemy();
979                         }
980                 } else if (i->first == "enemy") {
981                         if (GetBoolean(*i->second)) {
982                                 t.TargetEnemy();
983                         } else {
984                                 t.TargetAlly();
985                         }
986                 } else if (i->first == "all") {
987                         if (GetBoolean(*i->second)) {
988                                 t.TargetAll();
989                         }
990                 } else if (i->first == "multiple") {
991                         if (GetBoolean(*i->second)) {
992                                 t.TargetMultiple();
993                         }
994                 } else if (i->first == "single") {
995                         if (GetBoolean(*i->second)) {
996                                 t.TargetSingle();
997                         }
998                 } else {
999                         throw Error("unknown TargetingMode property: " + i->first);
1000                 }
1001         }
1002 }
1003
1004 }