]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
added argument interpreter
[l2e.git] / src / main.cpp
1 /*
2  * main.cpp
3  *
4  *  Created on: Aug 1, 2012
5  *      Author: holy
6  */
7
8 #include "app/Application.h"
9 #include "app/Arguments.h"
10 #include "app/Input.h"
11 #include "battle/BattleState.h"
12 #include "battle/Hero.h"
13 #include "battle/Monster.h"
14 #include "battle/PartyLayout.h"
15 #include "battle/Resources.h"
16 #include "battle/Stats.h"
17 #include "common/Ikari.h"
18 #include "common/Inventory.h"
19 #include "common/Item.h"
20 #include "common/Spell.h"
21 #include "geometry/Vector.h"
22 #include "graphics/ComplexAnimation.h"
23 #include "graphics/Font.h"
24 #include "graphics/Frame.h"
25 #include "graphics/Gauge.h"
26 #include "graphics/Menu.h"
27 #include "graphics/SimpleAnimation.h"
28 #include "graphics/Sprite.h"
29 #include "loader/Compiler.h"
30 #include "loader/Interpreter.h"
31 #include "loader/ParsedSource.h"
32 #include "loader/Parser.h"
33 #include "loader/TypeDescription.h"
34 #include "sdl/InitImage.h"
35 #include "sdl/InitScreen.h"
36 #include "sdl/InitSDL.h"
37
38 #include <cstdlib>
39 #include <ctime>
40 #include <exception>
41 #include <iostream>
42 #include <string>
43 #include <SDL.h>
44 #include <SDL_image.h>
45
46 using app::Application;
47 using app::Arguments;
48 using app::Input;
49 using battle::BattleState;
50 using battle::Hero;
51 using battle::Monster;
52 using battle::PartyLayout;
53 using battle::Stats;
54 using common::Ikari;
55 using common::Inventory;
56 using common::Item;
57 using common::Spell;
58 using geometry::Vector;
59 using graphics::ComplexAnimation;
60 using graphics::Font;
61 using graphics::Frame;
62 using graphics::Gauge;
63 using graphics::Menu;
64 using graphics::SimpleAnimation;
65 using graphics::Sprite;
66 using loader::Compiler;
67 using loader::Interpreter;
68 using loader::ParsedSource;
69 using loader::Parser;
70 using loader::TypeDescription;
71 using sdl::InitImage;
72 using sdl::InitScreen;
73 using sdl::InitSDL;
74
75 using std::cerr;
76 using std::cout;
77 using std::endl;
78 using std::exception;
79 using std::string;
80 using std::vector;
81
82 int main(int argc, char **argv) {
83         const int width = 800;
84         const int height = 480;
85
86 //      std::srand(std::time(0));
87
88         try {
89                 InitSDL sdl;
90                 InitImage image(IMG_INIT_PNG);
91
92                 battle::Resources::CreateTypeDescription();
93                 ComplexAnimation::CreateTypeDescription();
94                 Font::CreateTypeDescription();
95                 Frame::CreateTypeDescription();
96                 Gauge::CreateTypeDescription();
97                 Hero::CreateTypeDescription();
98                 Ikari::CreateTypeDescription();
99                 Interpreter::CreateTypeDescriptions();
100                 Item::CreateTypeDescription();
101                 graphics::MenuProperties::CreateTypeDescription();
102                 Monster::CreateTypeDescription();
103                 PartyLayout::CreateTypeDescription();
104                 SimpleAnimation::CreateTypeDescription();
105                 Spell::CreateTypeDescription();
106                 Sprite::CreateTypeDescription();
107                 Stats::CreateTypeDescription();
108                 common::TargetingMode::CreateTypeDescription();
109
110                 Arguments args;
111                 args.Read(argc, argv);
112
113                 ParsedSource source;
114
115                 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
116                         string filePath(*i);
117                         switch (filePath[filePath.size() - 1]) {
118                                 case 'o':
119                                         // TODO: load object file
120                                         break;
121                                 case 's':
122                                         Parser(filePath, source).Parse();
123                                         break;
124                                 default:
125                                         throw std::runtime_error("don't know what to do with " + filePath);
126                         }
127                 }
128
129 //              Parser("test-data/test.l2s", source).Parse();
130 //              Parser("test-data/ikaris.l2s", source).Parse();
131 //              Parser("test-data/items.l2s", source).Parse();
132 //              Parser("test-data/spells.l2s", source).Parse();
133 //              Parser("test-data/constants.l2s", source).Parse();
134
135                 Interpreter intp(source);
136                 intp.ReadSource();
137
138                 if (args.OutfileSet()) {
139                         std::ofstream testOut(args.OutfilePath());
140                         Compiler(intp).Write(testOut);
141                         return 0;
142                 }
143
144                 int battleResId(TypeDescription::GetTypeId("BattleResources"));
145                 int heroId(TypeDescription::GetTypeId("Hero"));
146                 int itemId(TypeDescription::GetTypeId("Item"));
147                 int monsterId(TypeDescription::GetTypeId("Monster"));
148                 int partyLayoutId(TypeDescription::GetTypeId("PartyLayout"));
149                 int spellId(TypeDescription::GetTypeId("Spell"));
150
151                 // temporary test data
152                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
153                 PartyLayout monstersLayout(*reinterpret_cast<PartyLayout *>(intp.GetObject(partyLayoutId, "monstersLayout")));
154                 PartyLayout heroesLayout(*reinterpret_cast<PartyLayout *>(intp.GetObject(partyLayoutId, "heroesLayout")));
155
156                 Monster monster(*reinterpret_cast<Monster *>(intp.GetObject(monsterId, "lizard")));
157                 Hero maxim(*reinterpret_cast<Hero *>(intp.GetObject(heroId, "maxim")));
158                 Hero selan(*reinterpret_cast<Hero *>(intp.GetObject(heroId, "selan")));
159                 Hero guy(*reinterpret_cast<Hero *>(intp.GetObject(heroId, "guy")));
160                 Hero dekar(*reinterpret_cast<Hero *>(intp.GetObject(heroId, "dekar")));
161
162                 battle::Resources *battleRes(reinterpret_cast<battle::Resources *>(intp.GetObject(battleResId, "battleResources")));
163
164                 maxim.AddSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "resetSpell")));
165                 Spell *strongSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "strongSpell")));
166                 maxim.AddSpell(strongSpell);
167                 selan.AddSpell(strongSpell);
168                 Spell *strongerSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "strongerSpell")));
169                 maxim.AddSpell(strongerSpell);
170                 selan.AddSpell(strongerSpell);
171                 Spell *championSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "championSpell")));
172                 maxim.AddSpell(championSpell);
173                 selan.AddSpell(championSpell);
174                 Spell *rallySpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "rallySpell")));
175                 maxim.AddSpell(rallySpell);
176                 selan.AddSpell(rallySpell);
177                 selan.AddSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "escapeSpell")));
178                 Spell *valorSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "valorSpell")));
179                 maxim.AddSpell(valorSpell);
180                 selan.AddSpell(valorSpell);
181
182                 Inventory inventory;
183                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "antidoteItem")), 9);
184                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "magicJarItem")), 4);
185                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "hiPotionItem")), 4);
186                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "powerPotionItem")), 4);
187                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "escapeItem")), 2);
188                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "sleepBallItem")), 1);
189                 battleRes->inventory = &inventory;
190
191                 maxim.SetWeapon(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoSwordItem")));
192                 maxim.SetArmor(reinterpret_cast<Item *>(intp.GetObject(itemId, "zirconArmorItem")));
193                 maxim.SetShield(reinterpret_cast<Item *>(intp.GetObject(itemId, "holyShieldItem")));
194                 maxim.SetHelmet(reinterpret_cast<Item *>(intp.GetObject(itemId, "legendHelmItem")));
195                 maxim.SetRing(reinterpret_cast<Item *>(intp.GetObject(itemId, "sProRingItem")));
196                 maxim.SetJewel(reinterpret_cast<Item *>(intp.GetObject(itemId, "evilJewelItem")));
197
198 //              selan.SetWeapon(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoWhipItem")));
199                 selan.SetArmor(reinterpret_cast<Item *>(intp.GetObject(itemId, "zirconPlateItem")));
200                 selan.SetShield(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoGlovesItem")));
201                 selan.SetHelmet(reinterpret_cast<Item *>(intp.GetObject(itemId, "holyCapItem")));
202                 selan.SetRing(reinterpret_cast<Item *>(intp.GetObject(itemId, "ghostRingItem")));
203                 selan.SetJewel(reinterpret_cast<Item *>(intp.GetObject(itemId, "eagleRockItem")));
204
205 //              guy.SetWeapon(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoAxItem")));
206                 guy.SetArmor(reinterpret_cast<Item *>(intp.GetObject(itemId, "zirconArmorItem")));
207                 guy.SetShield(reinterpret_cast<Item *>(intp.GetObject(itemId, "megaShieldItem")));
208                 guy.SetHelmet(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoHelmetItem")));
209                 guy.SetRing(reinterpret_cast<Item *>(intp.GetObject(itemId, "powerRingItem")));
210                 guy.SetJewel(reinterpret_cast<Item *>(intp.GetObject(itemId, "evilJewelItem")));
211
212                 // NOTE: this is actually Artea equipment
213 //              dekar.SetWeapon(reinterpret_cast<Item *>(intp.GetObject(itemId, "lizardBlowItem")));
214                 dekar.SetArmor(reinterpret_cast<Item *>(intp.GetObject(itemId, "holyRobeItem")));
215                 dekar.SetShield(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoGlovesItem")));
216                 dekar.SetHelmet(reinterpret_cast<Item *>(intp.GetObject(itemId, "holyCapItem")));
217                 dekar.SetRing(reinterpret_cast<Item *>(intp.GetObject(itemId, "rocketRingItem")));
218                 dekar.SetJewel(reinterpret_cast<Item *>(intp.GetObject(itemId, "krakenRockItem")));
219
220                 InitScreen screen(width, height);
221
222                 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
223                 battleState->AddMonster(monster);
224                 battleState->AddMonster(monster);
225                 battleState->AddMonster(monster);
226                 battleState->AddMonster(monster);
227                 battleState->AddHero(maxim);
228                 battleState->AddHero(selan);
229                 battleState->AddHero(guy);
230                 battleState->AddHero(dekar);
231                 Application app(&screen, battleState);
232                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
233                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
234                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
235                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
236                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
237                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
238                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
239                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
240                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
241                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
242                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
243                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
244                 app.Run();
245
246                 return 0;
247         } catch (Parser::Error &e) {
248                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
249                 return 1;
250         } catch (exception &e) {
251                 cerr << "exception in main(): " << e.what() << endl;
252                 return 1;
253         }
254 }