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