X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=8b54fc177913de18a1266f885cc8e959b6eb4eef;hb=eb2ad5ffd08128d31af32f3929a3295fcfa251e9;hp=0ff877688d1231c8794b371319444547234f629c;hpb=4634bd26d24a163b7128df7dd5183fcb2cdc8031;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 0ff8776..8b54fc1 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -1,32 +1,48 @@ -/* - * Interpreter.cpp - * - * Created on: Aug 26, 2012 - * Author: holy - */ - #include "Interpreter.h" #include "ParsedSource.h" +#include "TypeDescription.h" #include "../battle/Hero.h" #include "../battle/Monster.h" +#include "../battle/PartyLayout.h" +#include "../battle/Resources.h" +#include "../common/Ikari.h" +#include "../common/Item.h" +#include "../common/Script.h" +#include "../common/Spell.h" +#include "../common/Stats.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" #include +#include #include using battle::Hero; using battle::Monster; -using battle::Stats; +using battle::PartyLayout; +using common::Ikari; +using common::Item; +using common::Script; +using common::Spell; +using common::Stats; +using common::TargetingMode; using graphics::Animation; +using graphics::Color; +using graphics::Font; +using graphics::Frame; +using graphics::Gauge; using graphics::ComplexAnimation; using graphics::SimpleAnimation; using graphics::Sprite; -using geometry::Vector; +using math::Vector; using std::make_pair; -using std::map; using std::set; using std::string; using std::vector; @@ -34,84 +50,41 @@ using std::vector; namespace loader { Interpreter::~Interpreter() { - for (vector::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) { - delete *i; - } - for (vector::const_iterator i(images.begin()), end(images.end()); i != end; ++i) { - SDL_FreeSurface(*i); + for (std::map::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) { + SDL_FreeSurface(i->second); } } -bool Interpreter::ParsedDefinition::IsCompatible(DynamicType with) const { - return type == with - || (with == ANIMATION - && (type == COMPLEX_ANIMATION || type == SIMPLE_ANIMATION)); -} - - -Animation *Interpreter::GetAnimation(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); +const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) { + std::map::const_iterator i(parsedDefinitions.find(identifier)); if (i != parsedDefinitions.end()) { - if (i->second.IsCompatible(ANIMATION)) { - return animations[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Animation"); - } + return i->second; + } else if (source.IsDefined(identifier)) { + ReadDefinition(source.GetDefinition(identifier)); + return parsedDefinitions.at(identifier); } else { - throw Error("access to undefined Animation " + name); + throw Error("access to undefined object " + identifier); } } -Hero *Interpreter::GetHero(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.IsCompatible(HERO)) { - return &heroes[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Hero"); - } - } else { - throw Error("access to undefined Hero " + name); - } -} -Monster *Interpreter::GetMonster(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); +void *Interpreter::GetObject(int typeId, const std::string &name) { + std::map::const_iterator i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { - if (i->second.IsCompatible(MONSTER)) { - return &monsters[i->second.index]; + const TypeDescription &requested(TypeDescription::Get(typeId)); + const TypeDescription &actual(TypeDescription::Get(i->second.type)); + if (requested.TypeId() == actual.TypeId()) { + return values[actual.TypeId()][i->second.id]; + } else if (actual.IsSubtypeOf(requested)) { + char *sub(reinterpret_cast(values[actual.TypeId()][i->second.id])); + std::ptrdiff_t offset(actual.SupertypeOffset(requested)); + return sub - offset; } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Monster"); + throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName()); } } else { - throw Error("access to undefined Monster " + name); - } -} - -int Interpreter::GetNumber(const std::string &name) const { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.IsCompatible(NUMBER)) { - return numbers[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Number"); - } - } else { - throw Error("access to undefined Number " + name); - } -} - -Sprite *Interpreter::GetSprite(const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); - if (i != parsedDefinitions.end()) { - if (i->second.IsCompatible(SPRITE)) { - return &sprites[i->second.index]; - } else { - throw Error("cannot cast " + i->second.dfn->TypeName() + " to Sprite"); - } - } else { - throw Error("access to undefined Sprite " + name); + throw Error("access to undefined object " + name); } } @@ -134,299 +107,817 @@ void Interpreter::ReadDefinition(const Definition &dfn) { } void Interpreter::ReadLiteral(const Definition &dfn) { - switch (dfn.GetLiteral()->GetType()) { + const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName()); + int typeId(TypeDescription::GetTypeId(typeName)); + int id(values[typeId].size()); + const TypeDescription &td(TypeDescription::Get(typeId)); + int size( + (dfn.GetLiteral()->GetType() == Literal::PATH + || dfn.GetLiteral()->GetType() == Literal::STRING) + ? dfn.GetLiteral()->GetString().size() : td.Size()); + char *object(alloc.Alloc(size)); + values[typeId].push_back(object); + parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id))); + if (dfn.GetLiteral()->GetType() == Literal::OBJECT) { + ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties()); + } else { + ReadLiteral(typeId, id, object, *dfn.GetLiteral()); + } +} + +void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) { + switch (literal.GetType()) { case Literal::ARRAY_VALUES: - throw Error("unhandled literal: array of values"); - break; case Literal::ARRAY_PROPS: - throw Error("unhandled literal: array of properties"); + case Literal::ARRAY_IDENTS: + throw Error("named arrays are not supported, sorry"); break; case Literal::BOOLEAN: - throw Error("unhandled literal: boolean"); + new (object) bool(literal.GetBoolean()); break; case Literal::COLOR: - throw Error("unhandled literal: color"); + new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha()); break; case Literal::NUMBER: - numbers.push_back(dfn.GetLiteral()->GetNumber()); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, NUMBER, numbers.size() - 1))); + new (object) int(literal.GetNumber()); + break; + case Literal::PATH: + std::memcpy(object, literal.GetString().c_str(), literal.GetString().size()); + object[literal.GetString().size()] = '\0'; + break; + case Literal::SCRIPT: + new (object) Script; + ReadScript(literal.GetScript(), reinterpret_cast