]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
added dump option
[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                 switch (args.DetectRunLevel()) {
139                         case Arguments::COMPILE:
140                         {
141                                 std::ofstream testOut(args.OutfilePath());
142                                 Compiler(intp).Write(testOut);
143                                 return 0;
144                         }
145                         case Arguments::DUMP:
146                         {
147                                 std::cout << source << std::endl;
148                                 return 0;
149                         }
150                         case Arguments::PLAY:
151                                 break;
152                 }
153
154                 int battleResId(TypeDescription::GetTypeId("BattleResources"));
155                 int heroId(TypeDescription::GetTypeId("Hero"));
156                 int itemId(TypeDescription::GetTypeId("Item"));
157                 int monsterId(TypeDescription::GetTypeId("Monster"));
158                 int partyLayoutId(TypeDescription::GetTypeId("PartyLayout"));
159                 int spellId(TypeDescription::GetTypeId("Spell"));
160
161                 // temporary test data
162                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
163                 PartyLayout monstersLayout(*reinterpret_cast<PartyLayout *>(intp.GetObject(partyLayoutId, "monstersLayout")));
164                 PartyLayout heroesLayout(*reinterpret_cast<PartyLayout *>(intp.GetObject(partyLayoutId, "heroesLayout")));
165
166                 Monster monster(*reinterpret_cast<Monster *>(intp.GetObject(monsterId, "lizard")));
167                 Hero maxim(*reinterpret_cast<Hero *>(intp.GetObject(heroId, "maxim")));
168                 Hero selan(*reinterpret_cast<Hero *>(intp.GetObject(heroId, "selan")));
169                 Hero guy(*reinterpret_cast<Hero *>(intp.GetObject(heroId, "guy")));
170                 Hero dekar(*reinterpret_cast<Hero *>(intp.GetObject(heroId, "dekar")));
171
172                 battle::Resources *battleRes(reinterpret_cast<battle::Resources *>(intp.GetObject(battleResId, "battleResources")));
173
174                 maxim.AddSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "resetSpell")));
175                 Spell *strongSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "strongSpell")));
176                 maxim.AddSpell(strongSpell);
177                 selan.AddSpell(strongSpell);
178                 Spell *strongerSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "strongerSpell")));
179                 maxim.AddSpell(strongerSpell);
180                 selan.AddSpell(strongerSpell);
181                 Spell *championSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "championSpell")));
182                 maxim.AddSpell(championSpell);
183                 selan.AddSpell(championSpell);
184                 Spell *rallySpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "rallySpell")));
185                 maxim.AddSpell(rallySpell);
186                 selan.AddSpell(rallySpell);
187                 selan.AddSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "escapeSpell")));
188                 Spell *valorSpell(reinterpret_cast<Spell *>(intp.GetObject(spellId, "valorSpell")));
189                 maxim.AddSpell(valorSpell);
190                 selan.AddSpell(valorSpell);
191
192                 Inventory inventory;
193                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "antidoteItem")), 9);
194                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "magicJarItem")), 4);
195                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "hiPotionItem")), 4);
196                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "powerPotionItem")), 4);
197                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "escapeItem")), 2);
198                 inventory.Add(reinterpret_cast<Item *>(intp.GetObject(itemId, "sleepBallItem")), 1);
199                 battleRes->inventory = &inventory;
200
201                 maxim.SetWeapon(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoSwordItem")));
202                 maxim.SetArmor(reinterpret_cast<Item *>(intp.GetObject(itemId, "zirconArmorItem")));
203                 maxim.SetShield(reinterpret_cast<Item *>(intp.GetObject(itemId, "holyShieldItem")));
204                 maxim.SetHelmet(reinterpret_cast<Item *>(intp.GetObject(itemId, "legendHelmItem")));
205                 maxim.SetRing(reinterpret_cast<Item *>(intp.GetObject(itemId, "sProRingItem")));
206                 maxim.SetJewel(reinterpret_cast<Item *>(intp.GetObject(itemId, "evilJewelItem")));
207
208 //              selan.SetWeapon(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoWhipItem")));
209                 selan.SetArmor(reinterpret_cast<Item *>(intp.GetObject(itemId, "zirconPlateItem")));
210                 selan.SetShield(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoGlovesItem")));
211                 selan.SetHelmet(reinterpret_cast<Item *>(intp.GetObject(itemId, "holyCapItem")));
212                 selan.SetRing(reinterpret_cast<Item *>(intp.GetObject(itemId, "ghostRingItem")));
213                 selan.SetJewel(reinterpret_cast<Item *>(intp.GetObject(itemId, "eagleRockItem")));
214
215 //              guy.SetWeapon(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoAxItem")));
216                 guy.SetArmor(reinterpret_cast<Item *>(intp.GetObject(itemId, "zirconArmorItem")));
217                 guy.SetShield(reinterpret_cast<Item *>(intp.GetObject(itemId, "megaShieldItem")));
218                 guy.SetHelmet(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoHelmetItem")));
219                 guy.SetRing(reinterpret_cast<Item *>(intp.GetObject(itemId, "powerRingItem")));
220                 guy.SetJewel(reinterpret_cast<Item *>(intp.GetObject(itemId, "evilJewelItem")));
221
222                 // NOTE: this is actually Artea equipment
223 //              dekar.SetWeapon(reinterpret_cast<Item *>(intp.GetObject(itemId, "lizardBlowItem")));
224                 dekar.SetArmor(reinterpret_cast<Item *>(intp.GetObject(itemId, "holyRobeItem")));
225                 dekar.SetShield(reinterpret_cast<Item *>(intp.GetObject(itemId, "zircoGlovesItem")));
226                 dekar.SetHelmet(reinterpret_cast<Item *>(intp.GetObject(itemId, "holyCapItem")));
227                 dekar.SetRing(reinterpret_cast<Item *>(intp.GetObject(itemId, "rocketRingItem")));
228                 dekar.SetJewel(reinterpret_cast<Item *>(intp.GetObject(itemId, "krakenRockItem")));
229
230                 InitScreen screen(width, height);
231
232                 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
233                 battleState->AddMonster(monster);
234                 battleState->AddMonster(monster);
235                 battleState->AddMonster(monster);
236                 battleState->AddMonster(monster);
237                 battleState->AddHero(maxim);
238                 battleState->AddHero(selan);
239                 battleState->AddHero(guy);
240                 battleState->AddHero(dekar);
241                 Application app(&screen, battleState);
242                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
243                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
244                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
245                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
246                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
247                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
248                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
249                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
250                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
251                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
252                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
253                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
254                 app.Run();
255
256                 return 0;
257         } catch (Parser::Error &e) {
258                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
259                 return 1;
260         } catch (exception &e) {
261                 cerr << "exception in main(): " << e.what() << endl;
262                 return 1;
263         }
264 }