]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
added Caster class to simplify the code in main()
[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/Caster.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::Caster;
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                         Parser(*i, source).Parse();
118                 }
119
120                 switch (args.DetectRunLevel()) {
121                         case Arguments::WRITE:
122                         {
123                                 int length(std::strlen(args.OutfilePath()));
124                                 switch (args.OutfilePath()[length - 1]) {
125                                         case 'h': {
126                                                 std::ofstream outstream(args.OutfilePath());
127                                                 source.WriteHeader(outstream);
128                                                 break;
129                                         }
130                                         default: {
131                                                 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
132                                         }
133                                 }
134                                 return 0;
135                         }
136                         case Arguments::DUMP: {
137                                 std::cout << source << std::endl;
138                                 return 0;
139                         }
140                         case Arguments::PLAY:
141                                 break;
142                 }
143
144                 Interpreter intp(source);
145                 intp.ReadSource();
146
147                 if (intp.PostponedDefinitions().size() > 0) {
148                         for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
149                                 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
150                         }
151                         return 3;
152                 }
153
154                 Caster caster(intp);
155
156                 // temporary test data
157                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
158                 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
159                 PartyLayout heroesLayout(*caster.GetPartyLayout("heroesLayout"));
160
161                 Monster monster(*caster.GetMonster("lizard"));
162                 Hero maxim(*caster.GetHero("maxim"));
163                 Hero selan(*caster.GetHero("selan"));
164                 Hero guy(*caster.GetHero("guy"));
165                 Hero dekar(*caster.GetHero("dekar"));
166
167                 battle::Resources *battleRes(caster.GetBattleResources("battleResources"));
168
169                 maxim.AddSpell(caster.GetSpell("resetSpell"));
170                 Spell *strongSpell(caster.GetSpell("strongSpell"));
171                 maxim.AddSpell(strongSpell);
172                 selan.AddSpell(strongSpell);
173                 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
174                 maxim.AddSpell(strongerSpell);
175                 selan.AddSpell(strongerSpell);
176                 Spell *championSpell(caster.GetSpell("championSpell"));
177                 maxim.AddSpell(championSpell);
178                 selan.AddSpell(championSpell);
179                 Spell *rallySpell(caster.GetSpell("rallySpell"));
180                 maxim.AddSpell(rallySpell);
181                 selan.AddSpell(rallySpell);
182                 selan.AddSpell(caster.GetSpell("escapeSpell"));
183                 Spell *valorSpell(caster.GetSpell("valorSpell"));
184                 maxim.AddSpell(valorSpell);
185                 selan.AddSpell(valorSpell);
186
187                 Inventory inventory;
188                 inventory.Add(caster.GetItem("antidoteItem"), 9);
189                 inventory.Add(caster.GetItem("magicJarItem"), 4);
190                 inventory.Add(caster.GetItem("hiPotionItem"), 4);
191                 inventory.Add(caster.GetItem("powerPotionItem"), 4);
192                 inventory.Add(caster.GetItem("escapeItem"), 2);
193                 inventory.Add(caster.GetItem("sleepBallItem"), 1);
194                 battleRes->inventory = &inventory;
195
196                 maxim.SetWeapon(caster.GetItem("zircoSwordItem"));
197                 maxim.SetArmor(caster.GetItem("zirconArmorItem"));
198                 maxim.SetShield(caster.GetItem("holyShieldItem"));
199                 maxim.SetHelmet(caster.GetItem("legendHelmItem"));
200                 maxim.SetRing(caster.GetItem("sProRingItem"));
201                 maxim.SetJewel(caster.GetItem("evilJewelItem"));
202
203 //              selan.SetWeapon(cst.GetItem("zircoWhipItem"));
204                 selan.SetArmor(caster.GetItem("zirconPlateItem"));
205                 selan.SetShield(caster.GetItem("zircoGlovesItem"));
206                 selan.SetHelmet(caster.GetItem("holyCapItem"));
207                 selan.SetRing(caster.GetItem("ghostRingItem"));
208                 selan.SetJewel(caster.GetItem("eagleRockItem"));
209
210 //              guy.SetWeapon(cst.GetItem("zircoAxItem"));
211                 guy.SetArmor(caster.GetItem("zirconArmorItem"));
212                 guy.SetShield(caster.GetItem("megaShieldItem"));
213                 guy.SetHelmet(caster.GetItem("zircoHelmetItem"));
214                 guy.SetRing(caster.GetItem("powerRingItem"));
215                 guy.SetJewel(caster.GetItem("evilJewelItem"));
216
217                 // NOTE: this is actually Artea equipment
218 //              dekar.SetWeapon(cst.GetItem("lizardBlowItem"));
219                 dekar.SetArmor(caster.GetItem("holyRobeItem"));
220                 dekar.SetShield(caster.GetItem("zircoGlovesItem"));
221                 dekar.SetHelmet(caster.GetItem("holyCapItem"));
222                 dekar.SetRing(caster.GetItem("rocketRingItem"));
223                 dekar.SetJewel(caster.GetItem("krakenRockItem"));
224
225                 InitScreen screen(width, height);
226
227                 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
228                 battleState->AddMonster(monster);
229                 battleState->AddMonster(monster);
230                 battleState->AddMonster(monster);
231                 battleState->AddMonster(monster);
232                 battleState->AddHero(maxim);
233                 battleState->AddHero(selan);
234                 battleState->AddHero(guy);
235                 battleState->AddHero(dekar);
236                 Application app(&screen, battleState);
237                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
238                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
239                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
240                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
241                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
242                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
243                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
244                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
245                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
246                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
247                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
248                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
249                 app.Run();
250
251                 return 0;
252         } catch (Parser::Error &e) {
253                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
254                 return 2;
255         } catch (exception &e) {
256                 cerr << "exception in main(): " << e.what() << endl;
257                 return 1;
258         }
259 }