]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
removed debug output from 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 "common/GameConfig.h"
17 #include "common/GameState.h"
18 #include "common/Hero.h"
19 #include "common/Ikari.h"
20 #include "common/Inventory.h"
21 #include "common/Item.h"
22 #include "common/Script.h"
23 #include "common/Spell.h"
24 #include "common/Stats.h"
25 #include "geometry/Vector.h"
26 #include "graphics/ComplexAnimation.h"
27 #include "graphics/Font.h"
28 #include "graphics/Frame.h"
29 #include "graphics/Gauge.h"
30 #include "graphics/Menu.h"
31 #include "graphics/SimpleAnimation.h"
32 #include "graphics/Sprite.h"
33 #include "loader/Caster.h"
34 #include "loader/Interpreter.h"
35 #include "loader/ParsedSource.h"
36 #include "loader/Parser.h"
37 #include "loader/TypeDescription.h"
38 #include "map/Area.h"
39 #include "map/Entity.h"
40 #include "map/Map.h"
41 #include "map/MapState.h"
42 #include "map/Tile.h"
43 #include "map/Trigger.h"
44 #include "sdl/InitImage.h"
45 #include "sdl/InitScreen.h"
46 #include "sdl/InitSDL.h"
47
48 #include <cstdlib>
49 #include <cstring>
50 #include <ctime>
51 #include <exception>
52 #include <iostream>
53 #include <string>
54 #include <SDL.h>
55 #include <SDL_image.h>
56
57 using app::Application;
58 using app::Arguments;
59 using app::Input;
60 using battle::BattleState;
61 using battle::Monster;
62 using battle::PartyLayout;
63 using common::GameConfig;
64 using common::GameState;
65 using common::Hero;
66 using common::Ikari;
67 using common::Inventory;
68 using common::Item;
69 using common::Script;
70 using common::Spell;
71 using common::Stats;
72 using geometry::Vector;
73 using graphics::ComplexAnimation;
74 using graphics::Font;
75 using graphics::Frame;
76 using graphics::Gauge;
77 using graphics::Menu;
78 using graphics::SimpleAnimation;
79 using graphics::Sprite;
80 using loader::Caster;
81 using loader::Interpreter;
82 using loader::ParsedSource;
83 using loader::Parser;
84 using loader::TypeDescription;
85 using map::Area;
86 using map::Entity;
87 using map::Map;
88 using map::MapState;
89 using map::Tile;
90 using map::Trigger;
91 using sdl::InitImage;
92 using sdl::InitScreen;
93 using sdl::InitSDL;
94
95 using std::cerr;
96 using std::cout;
97 using std::endl;
98 using std::exception;
99 using std::string;
100 using std::vector;
101
102 int main(int argc, char **argv) {
103         const int width = 800;
104         const int height = 480;
105
106         const float walkSpeed = 128.0f;
107
108         bool battle(false);
109
110 //      std::srand(std::time(0));
111
112         try {
113                 InitSDL sdl;
114                 InitImage image(IMG_INIT_PNG);
115
116                 Area::CreateTypeDescription();
117                 battle::Resources::CreateTypeDescription();
118                 ComplexAnimation::CreateTypeDescription();
119                 Font::CreateTypeDescription();
120                 Frame::CreateTypeDescription();
121                 Gauge::CreateTypeDescription();
122                 Hero::CreateTypeDescription();
123                 Ikari::CreateTypeDescription();
124                 Interpreter::CreateTypeDescriptions();
125                 Item::CreateTypeDescription();
126                 Map::CreateTypeDescription();
127                 graphics::MenuProperties::CreateTypeDescription();
128                 Monster::CreateTypeDescription();
129                 PartyLayout::CreateTypeDescription();
130                 SimpleAnimation::CreateTypeDescription();
131                 Spell::CreateTypeDescription();
132                 Sprite::CreateTypeDescription();
133                 Stats::CreateTypeDescription();
134                 common::TargetingMode::CreateTypeDescription();
135                 Tile::CreateTypeDescription();
136                 Trigger::CreateTypeDescription();
137                 Entity::CreateTypeDescription();
138
139                 Arguments args;
140                 args.Read(argc, argv);
141
142                 ParsedSource source;
143
144                 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
145                         Parser(*i, source).Parse();
146                 }
147
148                 switch (args.GetRunLevel()) {
149                         case Arguments::WRITE:
150                         {
151                                 int length(std::strlen(args.OutfilePath()));
152                                 switch (args.OutfilePath()[length - 1]) {
153                                         case 'h': {
154                                                 std::ofstream outstream(args.OutfilePath());
155                                                 source.WriteHeader(outstream);
156                                                 break;
157                                         }
158                                         default: {
159                                                 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
160                                         }
161                                 }
162                                 return 0;
163                         }
164                         case Arguments::DUMP: {
165                                 std::cout << source << std::endl;
166                                 return 0;
167                         }
168                         case Arguments::SOURCE_WIKI: {
169                                 TypeDescription::WriteSourceWiki(std::cout);
170                                 return 0;
171                         }
172                         case Arguments::BATTLE:
173                                 battle = true;
174                                 break;
175                         case Arguments::PLAY:
176                         case Arguments::MAP:
177                                 break;
178                 }
179
180                 Interpreter intp(source);
181                 intp.ReadSource();
182
183                 if (intp.PostponedDefinitions().size() > 0) {
184                         for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
185                                 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
186                         }
187                         return 3;
188                 }
189
190                 Caster caster(intp);
191
192                 GameState gameState;
193
194                 gameState.heroes[0] = *caster.GetHero("maxim");
195                 gameState.heroes[1] = *caster.GetHero("selan");
196                 gameState.heroes[2] = *caster.GetHero("guy");
197                 gameState.heroes[3] = *caster.GetHero("dekar");
198
199                 gameState.party[0] = &gameState.heroes[0];
200                 gameState.party[1] = &gameState.heroes[1];
201                 gameState.party[2] = &gameState.heroes[2];
202                 gameState.party[3] = &gameState.heroes[3];
203
204                 GameConfig gameConfig;
205                 gameConfig.state = &gameState;
206                 gameConfig.heroesLayout = caster.GetPartyLayout("heroesLayout");
207                 gameConfig.battleResources = caster.GetBattleResources("battleResources");
208
209                 // temporary test data
210                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
211                 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
212
213                 Monster monster(*caster.GetMonster("lizard"));
214
215                 gameState.heroes[0].AddSpell(caster.GetSpell("resetSpell"));
216                 Spell *strongSpell(caster.GetSpell("strongSpell"));
217                 gameState.heroes[0].AddSpell(strongSpell);
218                 gameState.heroes[1].AddSpell(strongSpell);
219                 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
220                 gameState.heroes[0].AddSpell(strongerSpell);
221                 gameState.heroes[1].AddSpell(strongerSpell);
222                 Spell *championSpell(caster.GetSpell("championSpell"));
223                 gameState.heroes[0].AddSpell(championSpell);
224                 gameState.heroes[1].AddSpell(championSpell);
225                 Spell *rallySpell(caster.GetSpell("rallySpell"));
226                 gameState.heroes[0].AddSpell(rallySpell);
227                 gameState.heroes[1].AddSpell(rallySpell);
228                 gameState.heroes[1].AddSpell(caster.GetSpell("escapeSpell"));
229                 Spell *valorSpell(caster.GetSpell("valorSpell"));
230                 gameState.heroes[0].AddSpell(valorSpell);
231                 gameState.heroes[1].AddSpell(valorSpell);
232
233                 gameState.inventory.Add(caster.GetItem("antidoteItem"), 9);
234                 gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
235                 gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
236                 gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
237                 gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
238                 gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
239
240                 gameState.heroes[0].SetWeapon(caster.GetItem("zircoSwordItem"));
241                 gameState.heroes[0].SetArmor(caster.GetItem("zirconArmorItem"));
242                 gameState.heroes[0].SetShield(caster.GetItem("holyShieldItem"));
243                 gameState.heroes[0].SetHelmet(caster.GetItem("legendHelmItem"));
244                 gameState.heroes[0].SetRing(caster.GetItem("sProRingItem"));
245                 gameState.heroes[0].SetJewel(caster.GetItem("evilJewelItem"));
246
247 //              gameState.heroes[1].SetWeapon(cst.GetItem("zircoWhipItem"));
248                 gameState.heroes[1].SetArmor(caster.GetItem("zirconPlateItem"));
249                 gameState.heroes[1].SetShield(caster.GetItem("zircoGlovesItem"));
250                 gameState.heroes[1].SetHelmet(caster.GetItem("holyCapItem"));
251                 gameState.heroes[1].SetRing(caster.GetItem("ghostRingItem"));
252                 gameState.heroes[1].SetJewel(caster.GetItem("eagleRockItem"));
253
254 //              gameState.heroes[2].SetWeapon(cst.GetItem("zircoAxItem"));
255                 gameState.heroes[2].SetArmor(caster.GetItem("zirconArmorItem"));
256                 gameState.heroes[2].SetShield(caster.GetItem("megaShieldItem"));
257                 gameState.heroes[2].SetHelmet(caster.GetItem("zircoHelmetItem"));
258                 gameState.heroes[2].SetRing(caster.GetItem("powerRingItem"));
259                 gameState.heroes[2].SetJewel(caster.GetItem("evilJewelItem"));
260
261                 // NOTE: this is actually Artea equipment
262 //              gameState.heroes[3].SetWeapon(cst.GetItem("lizardBlowItem"));
263                 gameState.heroes[3].SetArmor(caster.GetItem("holyRobeItem"));
264                 gameState.heroes[3].SetShield(caster.GetItem("zircoGlovesItem"));
265                 gameState.heroes[3].SetHelmet(caster.GetItem("holyCapItem"));
266                 gameState.heroes[3].SetRing(caster.GetItem("rocketRingItem"));
267                 gameState.heroes[3].SetJewel(caster.GetItem("krakenRockItem"));
268
269                 gameState.heroes[0].MapEntity().Position() = Vector<float>(64, 128);
270
271                 gameState.heroes[1].MapEntity().Position() = Vector<float>(64, 128);
272                 gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
273                 gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity());
274
275                 gameState.heroes[2].MapEntity().Position() = Vector<float>(64, 128);
276                 gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
277                 gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity());
278
279                 gameState.heroes[3].MapEntity().Position() = Vector<float>(64, 128);
280                 gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
281                 gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity());
282
283                 InitScreen screen(width, height);
284
285                 app::State *state(0);
286
287                 if (battle) {
288                         BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
289                         battleState->AddMonster(monster);
290                         battleState->AddMonster(monster);
291                         battleState->AddMonster(monster);
292                         battleState->AddMonster(monster);
293                         battleState->AddHero(gameState.heroes[0]);
294                         battleState->AddHero(gameState.heroes[1]);
295                         battleState->AddHero(gameState.heroes[2]);
296                         battleState->AddHero(gameState.heroes[3]);
297                         state = battleState;
298                 } else {
299                         MapState *mapState(new MapState(&gameConfig, caster.GetMap("map1")));
300
301                         mapState->ControlEntity(&gameState.heroes[0].MapEntity());
302                         mapState->SetWalkingSpeed(walkSpeed);
303
304                         state = mapState;
305                 }
306
307                 Application app(screen, state);
308                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
309                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
310                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
311                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
312                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
313                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
314                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
315                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
316                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
317                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
318                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
319                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
320                 app.Buttons().MapKey(SDLK_1, Input::DEBUG_1);
321                 app.Buttons().MapKey(SDLK_2, Input::DEBUG_2);
322                 app.Buttons().MapKey(SDLK_3, Input::DEBUG_3);
323                 app.Buttons().MapKey(SDLK_4, Input::DEBUG_4);
324                 app.Run();
325
326                 return 0;
327         } catch (Parser::Error &e) {
328                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
329                 return 2;
330         } catch (exception &e) {
331                 cerr << "exception in main(): " << e.what() << endl;
332                 return 1;
333         }
334 }