]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/Interpreter.cpp
added Spell and TargetingMode interpretation
[l2e.git] / src / loader / Interpreter.cpp
index f616c1a394a0f803d0fee0a828b5f53abf3c32b0..4416f9b267ccd91ea922b48b883d88e08f978af6 100644 (file)
@@ -11,6 +11,8 @@
 #include "../battle/Hero.h"
 #include "../battle/Monster.h"
 #include "../battle/PartyLayout.h"
+#include "../common/Spell.h"
+#include "../common/TargetingMode.h"
 #include "../graphics/ComplexAnimation.h"
 #include "../graphics/Font.h"
 #include "../graphics/Frame.h"
@@ -26,6 +28,8 @@ using battle::Hero;
 using battle::Monster;
 using battle::PartyLayout;
 using battle::Stats;
+using common::Spell;
+using common::TargetingMode;
 using graphics::Animation;
 using graphics::Font;
 using graphics::Frame;
@@ -70,12 +74,18 @@ Interpreter::~Interpreter() {
        for (vector<SimpleAnimation *>::const_iterator i(simpleAnimations.begin()), end(simpleAnimations.end()); i != end; ++i) {
                delete *i;
        }
+       for (vector<Spell *>::const_iterator i(spells.begin()), end(spells.end()); i != end; ++i) {
+               delete *i;
+       }
        for (vector<Sprite *>::const_iterator i(sprites.begin()), end(sprites.end()); i != end; ++i) {
                delete *i;
        }
        for (vector<const char *>::const_iterator i(strings.begin()), end(strings.end()); i != end; ++i) {
                delete *i;
        }
+       for (vector<TargetingMode *>::const_iterator i(targetingModes.begin()), end(targetingModes.end()); i != end; ++i) {
+               delete *i;
+       }
 }
 
 
@@ -198,6 +208,19 @@ PartyLayout *Interpreter::GetPartyLayout(const std::string &name) {
        }
 }
 
+Spell *Interpreter::GetSpell(const std::string &name) {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == SPELL) {
+                       return spells[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Spell");
+               }
+       } else {
+               throw Error("access to undefined Spell " + name);
+       }
+}
+
 Sprite *Interpreter::GetSprite(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
@@ -224,6 +247,19 @@ const char *Interpreter::GetString(const std::string &name) const {
        }
 }
 
+TargetingMode *Interpreter::GetTargetingMode(const std::string &name) {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == TARGETING_MODE) {
+                       return targetingModes[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to TargetingMode");
+               }
+       } else {
+               throw Error("access to undefined TargetingMode " + name);
+       }
+}
+
 Vector<int> Interpreter::GetVector(const std::string &name) const {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
@@ -399,6 +435,17 @@ const vector<PropertyList *> &Interpreter::GetPropertyListArray(const Value &v)
        }
 }
 
+Spell *Interpreter::GetSpell(const Value &v) {
+       if (v.IsLiteral()) {
+               Spell *s(new Spell);
+               ReadSpell(*s, *v.GetLiteral().GetProperties());
+               return s;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetSpell(v.GetIdentifier());
+       }
+}
+
 Sprite *Interpreter::GetSprite(const Value &v) {
        if (v.IsLiteral()) {
                Sprite *s(new Sprite);
@@ -419,6 +466,17 @@ const char *Interpreter::GetString(const Value &v) {
        }
 }
 
+TargetingMode *Interpreter::GetTargetingMode(const Value &v) {
+       if (v.IsLiteral()) {
+               TargetingMode *t(new TargetingMode);
+               ReadTargetingMode(*t, *v.GetLiteral().GetProperties());
+               return t;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetTargetingMode(v.GetIdentifier());
+       }
+}
+
 Vector<int> Interpreter::GetVector(const Value &v) {
        if (v.IsLiteral()) {
                return Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY());
@@ -486,12 +544,24 @@ void Interpreter::ReadObject(const Definition &dfn) {
                simpleAnimations.push_back(animation);
                ReadSimpleAnimation(*animation, *dfn.GetProperties());
                parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SIMPLE_ANIMATION, index)));
+       } else if (dfn.TypeName() == "Spell") {
+               Spell *spell(new Spell);
+               int index(spells.size());
+               spells.push_back(spell);
+               ReadSpell(*spell, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SPELL, index)));
        } else if (dfn.TypeName() == "Sprite") {
                Sprite *sprite(new Sprite);
                int index(sprites.size());
                sprites.push_back(sprite);
                ReadSprite(*sprite, *dfn.GetProperties());
                parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SPRITE, index)));
+       } else if (dfn.TypeName() == "TargetingMode") {
+               TargetingMode *mode(new TargetingMode);
+               int index(targetingModes.size());
+               targetingModes.push_back(mode);
+               ReadTargetingMode(*mode, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, TARGETING_MODE, index)));
        } else {
                throw Error("unhandled object type: " + dfn.TypeName());
        }
@@ -682,6 +752,24 @@ void Interpreter::ReadSimpleAnimation(SimpleAnimation &a, const PropertyList &pr
        }
 }
 
+void Interpreter::ReadSpell(Spell &s, const PropertyList &props) {
+       for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
+               if (i->first == "name") {
+                       s.SetName(GetString(*i->second));
+               } else if (i->first == "cost") {
+                       s.SetCost(GetNumber(*i->second));
+               } else if (i->first == "battle") {
+                       if (GetBoolean(*i->second)) {
+                               s.SetUsableInBattle();
+                       }
+               } else if (i->first == "targets") {
+                       s.GetTargetingMode() = *GetTargetingMode(*i->second);
+               } else {
+                       throw Error("unknown Spell property: " + i->first);
+               }
+       }
+}
+
 void Interpreter::ReadSprite(Sprite &s, const PropertyList &props) {
        for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
                if (i->first == "image") {
@@ -718,4 +806,36 @@ void Interpreter::ReadStats(Stats &s, const PropertyList &props) {
        }
 }
 
+void Interpreter::ReadTargetingMode(TargetingMode &t, const PropertyList &props) {
+       for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
+               if (i->first == "ally") {
+                       if (GetBoolean(*i->second)) {
+                               t.TargetAlly();
+                       } else {
+                               t.TargetEnemy();
+                       }
+               } else if (i->first == "enemy") {
+                       if (GetBoolean(*i->second)) {
+                               t.TargetEnemy();
+                       } else {
+                               t.TargetAlly();
+                       }
+               } else if (i->first == "all") {
+                       if (GetBoolean(*i->second)) {
+                               t.TargetAll();
+                       }
+               } else if (i->first == "multiple") {
+                       if (GetBoolean(*i->second)) {
+                               t.TargetMultiple();
+                       }
+               } else if (i->first == "single") {
+                       if (GetBoolean(*i->second)) {
+                               t.TargetSingle();
+                       }
+               } else {
+                       throw Error("unknown TargetingMode property: " + i->first);
+               }
+       }
+}
+
 }