]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/Interpreter.cpp
moved menu prototypes to test.l2s
[l2e.git] / src / loader / Interpreter.cpp
index 4416f9b267ccd91ea922b48b883d88e08f978af6..372a114b8fb26fd83d63493b26099bbc6eef0c85 100644 (file)
 #include "../battle/Hero.h"
 #include "../battle/Monster.h"
 #include "../battle/PartyLayout.h"
+#include "../common/Ikari.h"
+#include "../common/Item.h"
 #include "../common/Spell.h"
 #include "../common/TargetingMode.h"
 #include "../graphics/ComplexAnimation.h"
 #include "../graphics/Font.h"
 #include "../graphics/Frame.h"
 #include "../graphics/Gauge.h"
+#include "../graphics/Menu.h"
 #include "../graphics/SimpleAnimation.h"
 #include "../graphics/Sprite.h"
 
@@ -28,9 +31,12 @@ using battle::Hero;
 using battle::Monster;
 using battle::PartyLayout;
 using battle::Stats;
+using common::Ikari;
+using common::Item;
 using common::Spell;
 using common::TargetingMode;
 using graphics::Animation;
+using graphics::Color;
 using graphics::Font;
 using graphics::Frame;
 using graphics::Gauge;
@@ -50,9 +56,6 @@ Interpreter::~Interpreter() {
        for (vector<ComplexAnimation *>::const_iterator i(complexAnimations.begin()), end(complexAnimations.end()); i != end; ++i) {
                delete *i;
        }
-       for (vector<Hero *>::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) {
-               delete *i;
-       }
        for (vector<Font *>::const_iterator i(fonts.begin()), end(fonts.end()); i != end; ++i) {
                delete *i;
        }
@@ -62,9 +65,21 @@ Interpreter::~Interpreter() {
        for (vector<Gauge *>::const_iterator i(gauges.begin()), end(gauges.end()); i != end; ++i) {
                delete *i;
        }
+       for (vector<Hero *>::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) {
+               delete *i;
+       }
+       for (vector<Ikari *>::const_iterator i(ikaris.begin()), end(ikaris.end()); i != end; ++i) {
+               delete *i;
+       }
        for (vector<SDL_Surface *>::const_iterator i(images.begin()), end(images.end()); i != end; ++i) {
                SDL_FreeSurface(*i);
        }
+       for (vector<Item *>::const_iterator i(items.begin()), end(items.end()); i != end; ++i) {
+               delete *i;
+       }
+       for (vector<graphics::MenuProperties *>::const_iterator i(menuProperties.begin()), end(menuProperties.end()); i != end; ++i) {
+               delete *i;
+       }
        for (vector<Monster *>::const_iterator i(monsters.begin()), end(monsters.end()); i != end; ++i) {
                delete *i;
        }
@@ -117,6 +132,19 @@ bool Interpreter::GetBoolean(const std::string &name) const {
        }
 }
 
+const Color &Interpreter::GetColor(const std::string &name) const {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == COLOR) {
+                       return colors[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Color");
+               }
+       } else {
+               throw Error("access to undefined Color " + name);
+       }
+}
+
 Font *Interpreter::GetFont(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
@@ -169,6 +197,45 @@ Hero *Interpreter::GetHero(const std::string &name) {
        }
 }
 
+Ikari *Interpreter::GetIkari(const std::string &name) {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == IKARI) {
+                       return ikaris[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Ikari");
+               }
+       } else {
+               throw Error("access to undefined Ikari " + name);
+       }
+}
+
+Item *Interpreter::GetItem(const std::string &name) {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == ITEM) {
+                       return items[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Item");
+               }
+       } else {
+               throw Error("access to undefined Item " + name);
+       }
+}
+
+graphics::MenuProperties *Interpreter::GetMenuProperties(const std::string &name) {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == MENU_PROPERTIES) {
+                       return menuProperties[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to MenuProperties");
+               }
+       } else {
+               throw Error("access to undefined MenuProperties " + name);
+       }
+}
+
 Monster *Interpreter::GetMonster(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
@@ -208,6 +275,19 @@ PartyLayout *Interpreter::GetPartyLayout(const std::string &name) {
        }
 }
 
+const char *Interpreter::GetPath(const std::string &name) const {
+       map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
+       if (i != parsedDefinitions.end()) {
+               if (i->second.type == PATH) {
+                       return strings[i->second.index];
+               } else {
+                       throw Error("cannot cast " + i->second.dfn->TypeName() + " to Path");
+               }
+       } else {
+               throw Error("access to undefined Path " + name);
+       }
+}
+
 Spell *Interpreter::GetSpell(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
@@ -237,7 +317,8 @@ Sprite *Interpreter::GetSprite(const std::string &name) {
 const char *Interpreter::GetString(const std::string &name) const {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
-               if (i->second.type == STRING) {
+               // TODO: enable path to string casting some time
+               if (i->second.type == STRING /* || i->second.type == PATH */) {
                        return strings[i->second.index];
                } else {
                        throw Error("cannot cast " + i->second.dfn->TypeName() + " to String");
@@ -306,12 +387,22 @@ void Interpreter::ReadLiteral(const Definition &dfn) {
                        parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, BOOLEAN, booleans.size() - 1)));
                        break;
                case Literal::COLOR:
-                       throw Error("unhandled literal: color");
+                       colors.push_back(Color(dfn.GetLiteral()->GetRed(), dfn.GetLiteral()->GetGreen(), dfn.GetLiteral()->GetBlue(), dfn.GetLiteral()->GetAlpha()));
+                       parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, COLOR, colors.size() - 1)));
                        break;
                case Literal::NUMBER:
                        numbers.push_back(dfn.GetLiteral()->GetNumber());
                        parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, NUMBER, numbers.size() - 1)));
                        break;
+               case Literal::PATH:
+                       {
+                               char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
+                               std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size());
+                               str[dfn.GetLiteral()->GetString().size()] = '\0';
+                               strings.push_back(str);
+                       }
+                       parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, PATH, strings.size() - 1)));
+                       break;
                case Literal::STRING:
                        {
                                char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
@@ -359,6 +450,15 @@ bool Interpreter::GetBoolean(const Value &v) {
        }
 }
 
+Color Interpreter::GetColor(const Value &v) {
+       if (v.IsLiteral()) {
+               return Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha());
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetColor(v.GetIdentifier());
+       }
+}
+
 Font *Interpreter::GetFont(const Value &v) {
        if (v.IsLiteral()) {
                Font *f(new Font);
@@ -392,11 +492,72 @@ Gauge *Interpreter::GetGauge(const Value &v) {
        }
 }
 
+Hero *Interpreter::GetHero(const Value &v) {
+       if (v.IsLiteral()) {
+               Hero *h(new Hero);
+               ReadHero(*h, *v.GetLiteral().GetProperties());
+               return h;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetHero(v.GetIdentifier());
+       }
+}
+
+Ikari *Interpreter::GetIkari(const Value &v) {
+       if (v.IsLiteral()) {
+               Ikari *i(new Ikari);
+               ReadIkari(*i, *v.GetLiteral().GetProperties());
+               return i;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetIkari(v.GetIdentifier());
+       }
+}
+
 SDL_Surface *Interpreter::GetImage(const Value &v) {
-       const char *file(GetString(v));
-       SDL_Surface *image(IMG_Load(file));
-       images.push_back(image);
-       return image;
+       string path(GetPath(v));
+       map<string, SDL_Surface *>::const_iterator i(imageCache.find(path));
+       if (i == imageCache.end()) {
+               SDL_Surface *image(IMG_Load(path.c_str()));
+               images.push_back(image);
+               imageCache.insert(make_pair(path, image));
+               return image;
+       } else {
+               return i->second;
+       }
+}
+
+Item *Interpreter::GetItem(const Value &v) {
+       if (v.IsLiteral()) {
+               Item *i(new Item);
+               ReadItem(*i, *v.GetLiteral().GetProperties());
+               return i;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetItem(v.GetIdentifier());
+       }
+}
+
+graphics::MenuProperties *Interpreter::GetMenuProperties(const Value &v) {
+       if (v.IsLiteral()) {
+               graphics::MenuProperties *m(new graphics::MenuProperties);
+               ReadMenuProperties(*m, *v.GetLiteral().GetProperties());
+               return m;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetMenuProperties(v.GetIdentifier());
+       }
+}
+
+Monster *Interpreter::GetMonster(const Value &v) {
+       if (v.IsLiteral()) {
+               Monster *m(new Monster);
+               ReadMonster(*m, *v.GetLiteral().GetProperties());
+               return m;
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetMonster(v.GetIdentifier());
+       }
 }
 
 int Interpreter::GetNumber(const Value &v) {
@@ -419,6 +580,15 @@ PartyLayout *Interpreter::GetPartyLayout(const Value &v) {
        }
 }
 
+const char *Interpreter::GetPath(const Value &v) {
+       if (v.IsLiteral()) {
+               return v.GetLiteral().GetString().c_str();
+       } else {
+               ReadDefinition(source.GetDefinition(v.GetIdentifier()));
+               return GetPath(v.GetIdentifier());
+       }
+}
+
 const PropertyList *Interpreter::GetPropertyList(const Value &v) {
        if (v.IsLiteral()) {
                return v.GetLiteral().GetProperties();
@@ -526,6 +696,24 @@ void Interpreter::ReadObject(const Definition &dfn) {
                heroes.push_back(hero);
                ReadHero(*hero, *dfn.GetProperties());
                parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, HERO, index)));
+       } else if (dfn.TypeName() == "Ikari") {
+               Ikari *ikari(new Ikari);
+               int index(ikaris.size());
+               ikaris.push_back(ikari);
+               ReadIkari(*ikari, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, IKARI, index)));
+       } else if (dfn.TypeName() == "Item") {
+               Item *item(new Item);
+               int index(items.size());
+               items.push_back(item);
+               ReadItem(*item, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, ITEM, index)));
+       } else if (dfn.TypeName() == "MenuProperties") {
+               graphics::MenuProperties *mprops(new graphics::MenuProperties);
+               int index(menuProperties.size());
+               menuProperties.push_back(mprops);
+               ReadMenuProperties(*mprops, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, MENU_PROPERTIES, index)));
        } else if (dfn.TypeName() == "Monster") {
                Monster *monster(new Monster);
                int index(monsters.size());
@@ -655,6 +843,50 @@ void Interpreter::ReadGauge(Gauge &g, const PropertyList &props) {
        }
 }
 
+void Interpreter::ReadIkari(Ikari &ikari, const PropertyList &props) {
+       for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
+               if (i->first == "name") {
+                       ikari.SetName(GetString(*i->second));
+               } else if (i->first == "cost") {
+                       ikari.SetCost(GetNumber(*i->second));
+               } else if (i->first == "targets") {
+                       ikari.GetTargetingMode() = *GetTargetingMode(*i->second);
+               } else if (i->first == "magical") {
+                       if (GetBoolean(*i->second)) {
+                               ikari.SetMagical();
+                       }
+               } else if (i->first == "physical") {
+                       if (GetBoolean(*i->second)) {
+                               ikari.SetPhysical();
+                       }
+               } else {
+                       throw Error("unknown Ikari property: " + i->first);
+               }
+       }
+}
+
+void Interpreter::ReadItem(Item &item, const PropertyList &props) {
+       for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
+               if (i->first == "name") {
+                       item.SetName(GetString(*i->second));
+               } else if (i->first == "menuicon") {
+                       item.SetMenuIcon(GetSprite(*i->second));
+               } else if (i->first == "battle") {
+                       if (GetBoolean(*i->second)) {
+                               item.SetUsableInBattle();
+                       }
+               } else if (i->first == "targets") {
+                       item.GetTargetingMode() = *GetTargetingMode(*i->second);
+               } else if (i->first == "ikari") {
+                       item.SetIkari(GetIkari(*i->second));
+               } else if (i->first == "attackanimation") {
+                       item.SetAttackAnimation(GetAnimation(*i->second));
+               } else {
+                       throw Error("unknown Item property: " + i->first);
+               }
+       }
+}
+
 void Interpreter::ReadHero(Hero &h, const PropertyList &props) {
        for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
                if (i->first == "name") {
@@ -689,6 +921,40 @@ void Interpreter::ReadHero(Hero &h, const PropertyList &props) {
        }
 }
 
+void Interpreter::ReadMenuProperties(graphics::MenuProperties &mprops, const PropertyList &props) {
+       for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
+               if (i->first == "font") {
+                       mprops.font = GetFont(*i->second);
+               } else if (i->first == "disabledFont") {
+                       mprops.disabledFont = GetFont(*i->second);
+               } else if (i->first == "cursor") {
+                       mprops.cursor = GetSprite(*i->second);
+               } else if (i->first == "charsPerEntry") {
+                       mprops.charsPerEntry = GetNumber(*i->second);
+               } else if (i->first == "rows") {
+                       mprops.rows = GetNumber(*i->second);
+               } else if (i->first == "rowGap") {
+                       mprops.rowGap = GetNumber(*i->second);
+               } else if (i->first == "iconSpace") {
+                       mprops.iconSpace = GetNumber(*i->second);
+               } else if (i->first == "cols") {
+                       mprops.cols = GetNumber(*i->second);
+               } else if (i->first == "colGap") {
+                       mprops.colGap = GetNumber(*i->second);
+               } else if (i->first == "delimiter") {
+                       mprops.delimiter = *GetString(*i->second);
+               } else if (i->first == "charsPerNumber") {
+                       mprops.charsPerNumber = GetNumber(*i->second);
+               } else if (i->first == "charsPerAdditionalText") {
+                       mprops.charsPerAdditionalText = GetNumber(*i->second);
+               } else if (i->first == "additionalTextGap") {
+                       mprops.additionalTextGap = GetNumber(*i->second);
+               } else {
+                       throw Error("unknown MenuProperties property: " + i->first);
+               }
+       }
+}
+
 void Interpreter::ReadMonster(Monster &m, const PropertyList &props) {
        for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
                if (i->first == "name") {