]> git.localhorst.tv Git - l2e.git/blobdiff - src/main.cpp
new language, new compiler
[l2e.git] / src / main.cpp
index bd8ff4a48c0850c58232857a6b439d23bdff3853..8fa276ce73f07566b182b465b2457aea7af4580c 100644 (file)
@@ -1,35 +1,85 @@
-/*
- * main.cpp
- *
- *  Created on: Aug 1, 2012
- *      Author: holy
- */
-
+#include "keys.h"
 #include "app/Application.h"
+#include "app/Arguments.h"
 #include "app/Input.h"
 #include "battle/BattleState.h"
+#include "battle/Capsule.h"
 #include "battle/Hero.h"
 #include "battle/Monster.h"
 #include "battle/PartyLayout.h"
-#include "geometry/Point.h"
+#include "battle/Resources.h"
+#include "common/Capsule.h"
+#include "common/GameConfig.h"
+#include "common/GameState.h"
+#include "common/Hero.h"
+#include "common/Ikari.h"
+#include "common/Inventory.h"
+#include "common/Item.h"
+#include "common/LevelUp.h"
+#include "common/Script.h"
+#include "common/Spell.h"
+#include "common/Stats.h"
+#include "graphics/CharSelect.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 "graphics/Texture.h"
+#include "loader/Caster.h"
+#include "loader/Compiler.h"
+#include "loader/Interpreter.h"
+#include "loader/Loader.h"
+#include "loader/ParsedSource.h"
+#include "loader/Parser.h"
+#include "loader/TypeDescription.h"
+#include "map/Area.h"
+#include "map/Entity.h"
+#include "map/Map.h"
+#include "map/MapState.h"
+#include "map/Tile.h"
+#include "map/Trigger.h"
+#include "math/Fixed.h"
+#include "math/Vector.h"
+#include "menu/Resources.h"
 #include "sdl/InitImage.h"
 #include "sdl/InitScreen.h"
 #include "sdl/InitSDL.h"
 
+#include <cstdlib>
+#include <cstring>
+#include <ctime>
 #include <exception>
 #include <iostream>
+#include <string>
 #include <SDL.h>
 #include <SDL_image.h>
 
 using app::Application;
+using app::Arguments;
 using app::Input;
 using battle::BattleState;
-using battle::Hero;
 using battle::Monster;
 using battle::PartyLayout;
-using geometry::Point;
-using graphics::Sprite;
+using common::Capsule;
+using common::GameConfig;
+using common::GameState;
+using common::Hero;
+using common::Spell;
+using graphics::Texture;
+using loader::Caster;
+using loader::Compiler;
+using loader::Interpreter;
+using loader::Loader;
+using loader::ParsedSource;
+using loader::Parser;
+using loader::TypeDescription;
+using map::Entity;
+using map::MapState;
+using math::Fixed;
+using math::Vector;
 using sdl::InitImage;
 using sdl::InitScreen;
 using sdl::InitSDL;
@@ -38,80 +88,273 @@ using std::cerr;
 using std::cout;
 using std::endl;
 using std::exception;
+using std::string;
+using std::vector;
 
 int main(int argc, char **argv) {
-       const int width = 800;
-       const int height = 480;
+       const int width = 512;
+       const int height = 448;
+
+       const int walkSpeed = 8;
+
+       bool battle(false);
+
+//     std::srand(std::time(0));
 
        try {
                InitSDL sdl;
                InitImage image(IMG_INIT_PNG);
-               InitScreen screen(width, height);
+
+               Interpreter::CreateTypeDescriptions();
+
+               battle::Resources::CreateTypeDescription();
+               battle::Monster::CreateTypeDescription();
+               battle::PartyLayout::CreateTypeDescription();
+
+               common::Capsule::CreateTypeDescription();
+               common::Hero::CreateTypeDescription();
+               common::Ikari::CreateTypeDescription();
+               common::Item::CreateTypeDescription();
+               common::LevelUp::CreateTypeDescription();
+               common::Stats::CreateTypeDescription();
+               common::Spell::CreateTypeDescription();
+               common::TargetingMode::CreateTypeDescription();
+
+               graphics::Animation::CreateTypeDescription();
+               graphics::CharSelect::CreateTypeDescription();
+               graphics::ComplexAnimation::CreateTypeDescription();
+               graphics::Font::CreateTypeDescription();
+               graphics::Frame::CreateTypeDescription();
+               graphics::Gauge::CreateTypeDescription();
+               graphics::MenuProperties::CreateTypeDescription();
+               graphics::SimpleAnimation::CreateTypeDescription();
+               graphics::Sprite::CreateTypeDescription();
+               graphics::Texture::CreateTypeDescription();
+
+               map::Area::CreateTypeDescription();
+               map::Entity::CreateTypeDescription();
+               map::Map::CreateTypeDescription();
+               map::Tile::CreateTypeDescription();
+               map::Trigger::CreateTypeDescription();
+
+               menu::Resources::CreateTypeDescription();
+
+               Arguments args;
+               args.Read(argc, argv);
+
+               ParsedSource source;
+
+               Loader ld;
+
+               for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
+                       string filePath(*i);
+                       switch (filePath[filePath.size() - 1]) {
+                               case 'o':
+                                       ld.Load(filePath);
+                                       break;
+                               case 's':
+                                       Parser(filePath, source).Parse();
+                                       break;
+                               default:
+                                       throw std::runtime_error("don't know what to do with " + filePath);
+                       }
+               }
+
+               Interpreter intp(source);
+               intp.ReadSource();
+
+               switch (args.GetRunLevel()) {
+                       case Arguments::WRITE:
+                       {
+                               int length(std::strlen(args.OutfilePath()));
+                               switch (args.OutfilePath()[length - 1]) {
+                                       case 'h': {
+                                               std::ofstream outstream(args.OutfilePath());
+                                               source.WriteHeader(outstream);
+                                               break;
+                                       }
+                                       case 'o': {
+                                               std::fstream outstream(args.OutfilePath(), std::ios_base::out|std::ios_base::trunc);
+                                               outstream.flush();
+                                               outstream.close();
+                                               outstream.open(args.OutfilePath());
+                                               outstream.exceptions(std::ios_base::badbit|std::ios_base::failbit);
+                                               Compiler(intp).Write(outstream);
+                                               break;
+                                       }
+                                       default: {
+                                               throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
+                                       }
+                               }
+                               return 0;
+                       }
+                       case Arguments::DUMP: {
+                               std::cout << source << ld << std::endl;
+                               return 0;
+                       }
+                       case Arguments::SOURCE_WIKI: {
+                               TypeDescription::WriteSourceWiki(std::cout);
+                               return 0;
+                       }
+                       case Arguments::BATTLE:
+                               battle = true;
+                               break;
+                       case Arguments::PLAY:
+                       case Arguments::MAP:
+                               break;
+               }
+
+               if (intp.PostponedDefinitions().size() > 0) {
+                       for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
+                               std::cerr << "missing definition of " << TypeDescription::Get(i->type).TypeName() << " " << i->identifier << std::endl;
+                       }
+                       return 3;
+               }
+
+               Caster caster(ld, intp);
+
+               GameState gameState;
+
+               gameState.heroes[0] = *caster.GetHero("maxim");
+               gameState.heroes[1] = *caster.GetHero("selan");
+               gameState.heroes[2] = *caster.GetHero("guy");
+               gameState.heroes[3] = *caster.GetHero("dekar");
+
+               gameState.party[0] = &gameState.heroes[0];
+               gameState.party[1] = &gameState.heroes[1];
+               gameState.party[2] = &gameState.heroes[2];
+               gameState.party[3] = &gameState.heroes[3];
+               gameState.partySize = 4;
+
+               gameState.capsules[1] = *caster.GetCapsule("flash");
+               gameState.capsules[1].UpgradeClass();
+               gameState.capsules[1].UpgradeClass();
+               gameState.capsules[1].UpgradeClass();
+               gameState.capsules[1].UpgradeClass();
+               gameState.capsule = 1;
+
+               GameConfig gameConfig;
+               gameConfig.state = &gameState;
+               gameConfig.heroesLayout = caster.GetPartyLayout("heroesLayout");
+               gameConfig.battleResources = caster.GetBattleResources("battleResources");
+               gameConfig.menuResources = caster.GetMenuResources("menuResources");
 
                // temporary test data
-               SDL_Surface *bg(SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF));
-               SDL_FillRect(bg, 0, SDL_MapRGB(bg->format, 0xFF, 0xFF, 0xFF));
-               SDL_Rect r;
-               r.x = 1;
-               r.y = 1;
-               r.w = width - 2;
-               r.h = height - 2;
-               SDL_FillRect(bg, &r, SDL_MapRGB(bg->format, 0, 0, 0));
-               PartyLayout monstersLayout;
-               monstersLayout.AddPosition(Point<Uint8>(50, 100));
-               monstersLayout.AddPosition(Point<Uint8>(100, 100));
-               monstersLayout.AddPosition(Point<Uint8>(150, 100));
-               monstersLayout.AddPosition(Point<Uint8>(200, 100));
-               PartyLayout heroesLayout;
-               heroesLayout.AddPosition(Point<Uint8>(27, 219));
-               heroesLayout.AddPosition(Point<Uint8>(104, 227));
-               heroesLayout.AddPosition(Point<Uint8>(66, 238));
-               heroesLayout.AddPosition(Point<Uint8>(143, 246));
-               SDL_Surface *white96(SDL_CreateRGBSurface(0, 96, 96, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF));
-               SDL_FillRect(white96, 0, SDL_MapRGB(bg->format, 0xFF, 0xFF, 0xFF));
-               Sprite dummySprite(white96, 96, 96);
-               Monster monster;
-               monster.SetSprite(&dummySprite);
-               Hero hero;
-               hero.SetName("Name");
-               hero.SetLevel(34);
-               hero.SetSprite(&dummySprite);
-               hero.SetMaxHealth(100);
-               hero.SetHealth(50);
-               hero.SetMaxMana(100);
-               hero.SetMana(66);
-               hero.SetIP(160);
-
-               SDL_Surface *attackIcons(IMG_Load("test-data/attack-type-icons.png"));
-               Sprite attackIconsSprite(attackIcons, 32, 32);
-               SDL_Surface *moveIcons(IMG_Load("test-data/move-icons.png"));
-               Sprite moveIconsSprite(moveIcons, 32, 32);
-
-               BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite, &moveIconsSprite));
-               battleState->AddMonster(monster);
-               battleState->AddMonster(monster);
-               battleState->AddMonster(monster);
-               battleState->AddMonster(monster);
-               battleState->AddHero(hero);
-               battleState->AddHero(hero);
-               battleState->AddHero(hero);
-               battleState->AddHero(hero);
-               Application app(&screen, battleState);
-               app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
-               app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
-               app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
-               app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
-               app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
-               app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
-               app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
-               app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
-               app.Buttons().MapKey(SDLK_RETURN, Input::START);
-               app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
-               app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
-               app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
+               SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
+               PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
+
+               Monster monster(*caster.GetMonster("lizard"));
+
+               gameState.heroes[0].AddSpell(caster.GetSpell("resetSpell"));
+               Spell *strongSpell(caster.GetSpell("strongSpell"));
+               gameState.heroes[0].AddSpell(strongSpell);
+               gameState.heroes[1].AddSpell(strongSpell);
+               Spell *strongerSpell(caster.GetSpell("strongerSpell"));
+               gameState.heroes[0].AddSpell(strongerSpell);
+               gameState.heroes[1].AddSpell(strongerSpell);
+               Spell *championSpell(caster.GetSpell("championSpell"));
+               gameState.heroes[0].AddSpell(championSpell);
+               gameState.heroes[1].AddSpell(championSpell);
+               Spell *rallySpell(caster.GetSpell("rallySpell"));
+               gameState.heroes[0].AddSpell(rallySpell);
+               gameState.heroes[1].AddSpell(rallySpell);
+               gameState.heroes[1].AddSpell(caster.GetSpell("escapeSpell"));
+               Spell *valorSpell(caster.GetSpell("valorSpell"));
+               gameState.heroes[0].AddSpell(valorSpell);
+               gameState.heroes[1].AddSpell(valorSpell);
+
+               gameState.inventory.Add(caster.GetItem("zirconPlateItem"), 32);
+               gameState.inventory.Add(caster.GetItem("holyFruitItem"));
+               gameState.inventory.Add(caster.GetItem("darkFruitItem"));
+               gameState.inventory.Add(caster.GetItem("antidoteItem"), 9);
+               gameState.inventory.Add(caster.GetItem("powerRingItem"));
+               gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
+               gameState.inventory.Add(caster.GetItem("sProRingItem"));
+               gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
+               gameState.inventory.Add(caster.GetItem("powerRingItem"));
+               gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
+               gameState.inventory.Add(caster.GetItem("zircoSwordItem"));
+               gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
+               gameState.inventory.Add(caster.GetItem("zircoHelmetItem"));
+               gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
+               gameState.inventory.Add(caster.GetItem("zirconPlateItem"));
+
+               gameState.heroes[0].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoSwordItem"));
+               gameState.heroes[0].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconArmorItem"));
+               gameState.heroes[0].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("holyShieldItem"));
+               gameState.heroes[0].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("legendHelmItem"));
+               gameState.heroes[0].SetEquipment(Hero::EQUIP_RING, caster.GetItem("sProRingItem"));
+               gameState.heroes[0].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("evilJewelItem"));
+
+//             gameState.heroes[1].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoWhipItem"));
+               gameState.heroes[1].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconPlateItem"));
+               gameState.heroes[1].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("zircoGlovesItem"));
+               gameState.heroes[1].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("holyCapItem"));
+               gameState.heroes[1].SetEquipment(Hero::EQUIP_RING, caster.GetItem("ghostRingItem"));
+               gameState.heroes[1].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("eagleRockItem"));
+
+//             gameState.heroes[2].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoAxItem"));
+               gameState.heroes[2].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconArmorItem"));
+               gameState.heroes[2].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("megaShieldItem"));
+               gameState.heroes[2].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("zircoHelmetItem"));
+               gameState.heroes[2].SetEquipment(Hero::EQUIP_RING, caster.GetItem("powerRingItem"));
+               gameState.heroes[2].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("evilJewelItem"));
+
+               // NOTE: this is actually Artea equipment
+//             gameState.heroes[3].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("lizardBlowItem"));
+               gameState.heroes[3].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("holyRobeItem"));
+               gameState.heroes[3].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("zircoGlovesItem"));
+               gameState.heroes[3].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("holyCapItem"));
+               gameState.heroes[3].SetEquipment(Hero::EQUIP_RING, caster.GetItem("rocketRingItem"));
+               gameState.heroes[3].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("krakenRockItem"));
+
+               gameState.heroes[0].MapEntity().Position() = Vector<Fixed<8> >(64, 128);
+
+               gameState.heroes[1].MapEntity().Position() = Vector<Fixed<8> >(64, 128);
+               gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
+               gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity());
+
+               gameState.heroes[2].MapEntity().Position() = Vector<Fixed<8> >(64, 128);
+               gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
+               gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity());
+
+               gameState.heroes[3].MapEntity().Position() = Vector<Fixed<8> >(64, 128);
+               gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
+               gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity());
+
+               InitScreen screen(width, height);
+
+               app::State *state(0);
+
+               if (battle) {
+                       BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
+                       battleState->AddMonster(monster);
+                       battleState->AddMonster(monster);
+                       battleState->AddMonster(monster);
+                       battleState->AddMonster(monster);
+                       battleState->SetCapsule(caster.GetCapsule("flash"));
+                       battleState->AddHero(gameState.heroes[0]);
+                       battleState->AddHero(gameState.heroes[1]);
+                       battleState->AddHero(gameState.heroes[2]);
+                       battleState->AddHero(gameState.heroes[3]);
+                       state = battleState;
+               } else {
+                       MapState *mapState(new MapState(&gameConfig, caster.GetMap("map1")));
+
+                       mapState->ControlEntity(&gameState.heroes[0].MapEntity());
+                       mapState->SetWalkingSpeed(walkSpeed);
+
+                       state = mapState;
+               }
+
+               Application app(screen, state);
+               MapKeys(app.Buttons());
                app.Run();
 
                return 0;
+       } catch (Parser::Error &e) {
+               cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
+               return 2;
        } catch (exception &e) {
                cerr << "exception in main(): " << e.what() << endl;
                return 1;