]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.cpp
interpret Monster's meleeAnimation property
[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 "../graphics/ComplexAnimation.h"
14 #include "../graphics/SimpleAnimation.h"
15 #include "../graphics/Sprite.h"
16
17 #include <algorithm>
18 #include <SDL_image.h>
19
20 using battle::Hero;
21 using battle::Monster;
22 using battle::Stats;
23 using graphics::Animation;
24 using graphics::ComplexAnimation;
25 using graphics::SimpleAnimation;
26 using graphics::Sprite;
27 using geometry::Vector;
28 using std::make_pair;
29 using std::map;
30 using std::set;
31 using std::string;
32 using std::vector;
33
34 namespace loader {
35
36 Interpreter::~Interpreter() {
37         for (vector<Animation *>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
38                 delete *i;
39         }
40         for (vector<SDL_Surface *>::const_iterator i(images.begin()), end(images.end()); i != end; ++i) {
41                 SDL_FreeSurface(*i);
42         }
43 }
44
45
46 bool Interpreter::ParsedDefinition::IsCompatible(DynamicType with) const {
47         return type == with
48                         || (with == ANIMATION
49                                         && (type == COMPLEX_ANIMATION || type == SIMPLE_ANIMATION));
50 }
51
52
53 Animation *Interpreter::GetAnimation(const std::string &name) {
54         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
55         if (i != parsedDefinitions.end()) {
56                 if (i->second.IsCompatible(ANIMATION)) {
57                         return animations[i->second.index];
58                 } else {
59                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Animation");
60                 }
61         } else {
62                 throw Error("access to undefined Animation " + name);
63         }
64 }
65
66 Hero *Interpreter::GetHero(const std::string &name) {
67         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
68         if (i != parsedDefinitions.end()) {
69                 if (i->second.IsCompatible(HERO)) {
70                         return &heroes[i->second.index];
71                 } else {
72                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Hero");
73                 }
74         } else {
75                 throw Error("access to undefined Hero " + name);
76         }
77 }
78
79 Monster *Interpreter::GetMonster(const std::string &name) {
80         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
81         if (i != parsedDefinitions.end()) {
82                 if (i->second.IsCompatible(MONSTER)) {
83                         return &monsters[i->second.index];
84                 } else {
85                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Monster");
86                 }
87         } else {
88                 throw Error("access to undefined Monster " + name);
89         }
90 }
91
92 int Interpreter::GetNumber(const std::string &name) const {
93         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
94         if (i != parsedDefinitions.end()) {
95                 if (i->second.IsCompatible(NUMBER)) {
96                         return numbers[i->second.index];
97                 } else {
98                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Number");
99                 }
100         } else {
101                 throw Error("access to undefined Number " + name);
102         }
103 }
104
105 Sprite *Interpreter::GetSprite(const std::string &name) {
106         map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
107         if (i != parsedDefinitions.end()) {
108                 if (i->second.IsCompatible(SPRITE)) {
109                         return &sprites[i->second.index];
110                 } else {
111                         throw Error("cannot cast " + i->second.dfn->TypeName() + " to Sprite");
112                 }
113         } else {
114                 throw Error("access to undefined Sprite " + name);
115         }
116 }
117
118
119 void Interpreter::ReadSource() {
120         for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
121                 ReadDefinition(source.GetDefinition(*i));
122         }
123 }
124
125 void Interpreter::ReadDefinition(const Definition &dfn) {
126         if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
127                 return;
128         }
129         if (dfn.HasLiteralValue()) {
130                 ReadLiteral(dfn);
131         } else {
132                 ReadObject(dfn);
133         }
134 }
135
136 void Interpreter::ReadLiteral(const Definition &dfn) {
137         switch (dfn.GetLiteral()->GetType()) {
138                 case Literal::ARRAY_VALUES:
139                         throw Error("unhandled literal: array of values");
140                         break;
141                 case Literal::ARRAY_PROPS:
142                         throw Error("unhandled literal: array of properties");
143                         break;
144                 case Literal::BOOLEAN:
145                         throw Error("unhandled literal: boolean");
146                         break;
147                 case Literal::COLOR:
148                         throw Error("unhandled literal: color");
149                         break;
150                 case Literal::NUMBER:
151                         numbers.push_back(dfn.GetLiteral()->GetNumber());
152                         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, NUMBER, numbers.size() - 1)));
153                         break;
154                 case Literal::STRING:
155                         throw Error("unhandled literal: string");
156                         break;
157                 case Literal::VECTOR:
158                         throw Error("unhandled literal: vector");
159                         break;
160                 case Literal::OBJECT:
161                         throw Error("unhandled literal: object");
162                         break;
163         }
164 }
165
166 Animation *Interpreter::GetAnimation(const Value &v) {
167         if (v.IsLiteral()) {
168                 if (v.GetLiteral().GetTypeName() == "ComplexAnimation") {
169                         ComplexAnimation *a(new ComplexAnimation);
170                         ReadComplexAnimation(*a, *v.GetLiteral().GetProperties());
171                         animations.push_back(a);
172                         return a;
173                 } else {
174                         SimpleAnimation *a(new SimpleAnimation);
175                         ReadSimpleAnimation(*a, *v.GetLiteral().GetProperties());
176                         animations.push_back(a);
177                         return a;
178                 }
179         } else {
180                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
181                 return GetAnimation(v.GetIdentifier());
182         }
183 }
184
185 const vector<Value *> &Interpreter::GetValueArray(const Value &v) {
186         if (v.IsLiteral()) {
187                 return v.GetLiteral().GetValues();
188         } else {
189                 throw Error("identifier resolution not implemented for arrays of values");
190         }
191 }
192
193 const vector<PropertyList *> &Interpreter::GetPropertyListArray(const Value &v) {
194         if (v.IsLiteral()) {
195                 return v.GetLiteral().GetPropertyLists();
196         } else {
197                 throw Error("identifier resolution not implemented for arrays of property lists");
198         }
199 }
200
201 bool Interpreter::GetBoolean(const Value &v) {
202         if (v.IsLiteral()) {
203                 return v.GetLiteral().GetBoolean();
204         } else {
205                 throw Error("identifier resolution not implemented for booleans");
206         }
207 }
208
209 SDL_Surface *Interpreter::GetImage(const Value &v) {
210         const char *file(GetString(v));
211         SDL_Surface *image(IMG_Load(file));
212         images.push_back(image);
213         return image;
214 }
215
216 int Interpreter::GetNumber(const Value &v) {
217         if (v.IsLiteral()) {
218                 return v.GetLiteral().GetNumber();
219         } else {
220                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
221                 return GetNumber(v.GetIdentifier());
222         }
223 }
224
225 const PropertyList *Interpreter::GetPropertyList(const Value &v) {
226         if (v.IsLiteral()) {
227                 return v.GetLiteral().GetProperties();
228         } else {
229                 throw Error("identifier resolution not implemented for property lists");
230         }
231 }
232
233 Sprite *Interpreter::GetSprite(const Value &v) {
234         if (v.IsLiteral()) {
235                 Sprite *s(new Sprite);
236                 ReadSprite(*s, *v.GetLiteral().GetProperties());
237                 return s;
238         } else {
239                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
240                 return GetSprite(v.GetIdentifier());
241         }
242 }
243
244 const char *Interpreter::GetString(const Value &v) {
245         if (v.IsLiteral()) {
246                 return v.GetLiteral().GetString().c_str();
247         } else {
248                 throw Error("identifier resolution not implemented for strings");
249         }
250 }
251
252 Vector<int> Interpreter::GetVector(const Value &v) {
253         if (v.IsLiteral()) {
254                 return Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY());
255         } else {
256                 throw Error("identifier resolution not implemented for vectors");
257         }
258 }
259
260
261 void Interpreter::ReadObject(const Definition &dfn) {
262         if (dfn.TypeName() == "Hero") {
263                 heroes.push_back(Hero());
264                 ReadHero(heroes.back(), *dfn.GetProperties());
265                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, HERO, heroes.size() - 1)));
266         } else if (dfn.TypeName() == "Monster") {
267                 monsters.push_back(Monster());
268                 ReadMonster(monsters.back(), *dfn.GetProperties());
269                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, MONSTER, monsters.size() - 1)));
270         } else if (dfn.TypeName() == "Sprite") {
271                 int index(sprites.size());
272                 sprites.push_back(Sprite());
273                 ReadSprite(sprites.back(), *dfn.GetProperties());
274                 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SPRITE, index)));
275         } else {
276                 throw Error("unhandled object type: " + dfn.TypeName());
277         }
278 }
279
280
281 void Interpreter::ReadComplexAnimation(ComplexAnimation &a, const PropertyList &props) {
282         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
283                 if (i->first == "sprite") {
284                         a.SetSprite(GetSprite(*i->second));
285                 } else if (i->first == "frametime") {
286                         a.SetFrameTime(GetNumber(*i->second));
287                 } else if (i->first == "repeat") {
288                         a.SetRepeat(GetBoolean(*i->second));
289                 } else if (i->first == "frames") {
290                         const vector<PropertyList *> &values(GetPropertyListArray(*i->second));
291                         for (vector<PropertyList *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
292                                 ComplexAnimation::FrameProp frame;
293                                 ReadComplexAnimationFrame(frame, **i);
294                                 a.AddFrame(frame);
295                         }
296                 } else {
297                         throw Error("unknown ComplexAnimation property: " + i->first);
298                 }
299         }
300 }
301
302 void Interpreter::ReadComplexAnimationFrame(ComplexAnimation::FrameProp &f, const PropertyList &props) {
303         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
304                 if (i->first == "column") {
305                         f.col = GetNumber(*i->second);
306                 } else if (i->first == "row") {
307                         f.row = GetNumber(*i->second);
308                 } else if (i->first == "disposition") {
309                         f.disposition = GetVector(*i->second);
310                 } else {
311                         throw Error("unknown ComplexAnimationFrame property: " + i->first);
312                 }
313         }
314 }
315
316 void Interpreter::ReadHero(Hero &h, const PropertyList &props) {
317         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
318                 if (i->first == "name") {
319                         h.SetName(GetString(*i->second));
320                 } else if (i->first == "sprite") {
321                         h.SetSprite(GetSprite(*i->second));
322                 } else if (i->first == "level") {
323                         h.SetLevel(GetNumber(*i->second));
324                 } else if (i->first == "maxHealth") {
325                         h.SetMaxHealth(GetNumber(*i->second));
326                 } else if (i->first == "health") {
327                         h.SetHealth(GetNumber(*i->second));
328                 } else if (i->first == "maxMana") {
329                         h.SetMaxMana(GetNumber(*i->second));
330                 } else if (i->first == "mana") {
331                         h.SetMana(GetNumber(*i->second));
332                 } else if (i->first == "ip") {
333                         h.SetIP(GetNumber(*i->second));
334                 } else if (i->first == "stats") {
335                         battle::Stats stats;
336                         ReadStats(stats, *GetPropertyList(*i->second));
337                         h.SetStats(stats);
338                 } else if (i->first == "attackAnimation") {
339                         h.SetAttackAnimation(GetAnimation(*i->second));
340                 } else if (i->first == "spellAnimation") {
341                         h.SetSpellAnimation(GetAnimation(*i->second));
342                 } else if (i->first == "meleeAnimation") {
343                         h.SetMeleeAnimation(GetAnimation(*i->second));
344                 } else {
345                         throw Error("unknown Hero property: " + i->first);
346                 }
347         }
348 }
349
350 void Interpreter::ReadMonster(Monster &m, const PropertyList &props) {
351         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
352                 if (i->first == "name") {
353                         m.SetName(GetString(*i->second));
354                 } else if (i->first == "sprite") {
355                         m.SetSprite(GetSprite(*i->second));
356                 } else if (i->first == "level") {
357                         m.SetLevel(GetNumber(*i->second));
358                 } else if (i->first == "maxHealth") {
359                         m.SetMaxHealth(GetNumber(*i->second));
360                 } else if (i->first == "health") {
361                         m.SetHealth(GetNumber(*i->second));
362                 } else if (i->first == "maxMana") {
363                         m.SetMaxMana(GetNumber(*i->second));
364                 } else if (i->first == "mana") {
365                         m.SetMana(GetNumber(*i->second));
366                 } else if (i->first == "stats") {
367                         battle::Stats stats;
368                         ReadStats(stats, *GetPropertyList(*i->second));
369                         m.SetStats(stats);
370                 } else if (i->first == "attackAnimation") {
371                         m.SetAttackAnimation(GetAnimation(*i->second));
372                 } else if (i->first == "meleeAnimation") {
373                         m.SetMeleeAnimation(GetAnimation(*i->second));
374                 } else {
375                         throw Error("unknown Monster property: " + i->first);
376                 }
377         }
378 }
379
380 void Interpreter::ReadSimpleAnimation(SimpleAnimation &a, const PropertyList &props) {
381         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
382                 if (i->first == "sprite") {
383                         a.SetSprite(GetSprite(*i->second));
384                 } else if (i->first == "frametime") {
385                         a.SetFrameTime(GetNumber(*i->second));
386                 } else if (i->first == "repeat") {
387                         a.SetRepeat(GetBoolean(*i->second));
388                 } else if (i->first == "framecount") {
389                         a.SetNumFrames(GetNumber(*i->second));
390                 } else if (i->first == "col") {
391                         a.SetCol(GetNumber(*i->second));
392                 } else if (i->first == "row") {
393                         a.SetRow(GetNumber(*i->second));
394                 } else {
395                         throw Error("unknown SimpleAnimation property: " + i->first);
396                 }
397         }
398 }
399
400 void Interpreter::ReadSprite(Sprite &s, const PropertyList &props) {
401         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
402                 if (i->first == "image") {
403                         s.SetSurface(GetImage(*i->second));
404                 } else if (i->first == "size") {
405                         s.SetSize(GetVector(*i->second));
406                 } else if (i->first == "offset") {
407                         s.SetOffset(GetVector(*i->second));
408                 } else {
409                         throw Error("unknown Sprite property: " + i->first);
410                 }
411         }
412 }
413
414 void Interpreter::ReadStats(Stats &s, const PropertyList &props) {
415         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
416                 if (i->first == "atp") {
417                         s.SetAttack(GetNumber(*i->second));
418                 } else if (i->first == "dfp") {
419                         s.SetDefense(GetNumber(*i->second));
420                 } else if (i->first == "str") {
421                         s.SetStrength(GetNumber(*i->second));
422                 } else if (i->first == "agl") {
423                         s.SetAgility(GetNumber(*i->second));
424                 } else if (i->first == "int") {
425                         s.SetIntelligence(GetNumber(*i->second));
426                 } else if (i->first == "gut") {
427                         s.SetGut(GetNumber(*i->second));
428                 } else if (i->first == "mgr") {
429                         s.SetMagicResistance(GetNumber(*i->second));
430                 } else {
431                         throw Error("unknown Stats property: " + i->first);
432                 }
433         }
434 }
435
436 }