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