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