]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
d92e2940cc1dcb8b7b97fb477a5c17fccb044339
[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 "map/Area.h"
35 #include "map/Entity.h"
36 #include "map/Map.h"
37 #include "map/MapState.h"
38 #include "map/Tile.h"
39 #include "map/Trigger.h"
40 #include "sdl/InitImage.h"
41 #include "sdl/InitScreen.h"
42 #include "sdl/InitSDL.h"
43
44 #include <cstdlib>
45 #include <cstring>
46 #include <ctime>
47 #include <exception>
48 #include <iostream>
49 #include <string>
50 #include <SDL.h>
51 #include <SDL_image.h>
52
53 using app::Application;
54 using app::Arguments;
55 using app::Input;
56 using battle::BattleState;
57 using battle::Hero;
58 using battle::Monster;
59 using battle::PartyLayout;
60 using battle::Stats;
61 using common::Ikari;
62 using common::Inventory;
63 using common::Item;
64 using common::Spell;
65 using geometry::Vector;
66 using graphics::ComplexAnimation;
67 using graphics::Font;
68 using graphics::Frame;
69 using graphics::Gauge;
70 using graphics::Menu;
71 using graphics::SimpleAnimation;
72 using graphics::Sprite;
73 using loader::Caster;
74 using loader::Interpreter;
75 using loader::ParsedSource;
76 using loader::Parser;
77 using loader::TypeDescription;
78 using map::Area;
79 using map::Entity;
80 using map::Map;
81 using map::MapState;
82 using map::Tile;
83 using map::Trigger;
84 using sdl::InitImage;
85 using sdl::InitScreen;
86 using sdl::InitSDL;
87
88 using std::cerr;
89 using std::cout;
90 using std::endl;
91 using std::exception;
92 using std::string;
93 using std::vector;
94
95 int main(int argc, char **argv) {
96         const int width = 800;
97         const int height = 480;
98
99         const int tileSize = 32;
100         const float walkSpeed = 128.0f;
101
102         const bool battle(false);
103
104 //      std::srand(std::time(0));
105
106         try {
107                 InitSDL sdl;
108                 InitImage image(IMG_INIT_PNG);
109
110                 battle::Resources::CreateTypeDescription();
111                 ComplexAnimation::CreateTypeDescription();
112                 Font::CreateTypeDescription();
113                 Frame::CreateTypeDescription();
114                 Gauge::CreateTypeDescription();
115                 Hero::CreateTypeDescription();
116                 Ikari::CreateTypeDescription();
117                 Interpreter::CreateTypeDescriptions();
118                 Item::CreateTypeDescription();
119                 graphics::MenuProperties::CreateTypeDescription();
120                 Monster::CreateTypeDescription();
121                 PartyLayout::CreateTypeDescription();
122                 SimpleAnimation::CreateTypeDescription();
123                 Spell::CreateTypeDescription();
124                 Sprite::CreateTypeDescription();
125                 Stats::CreateTypeDescription();
126                 common::TargetingMode::CreateTypeDescription();
127
128                 Arguments args;
129                 args.Read(argc, argv);
130
131                 ParsedSource source;
132
133                 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
134                         Parser(*i, source).Parse();
135                 }
136
137                 switch (args.DetectRunLevel()) {
138                         case Arguments::WRITE:
139                         {
140                                 int length(std::strlen(args.OutfilePath()));
141                                 switch (args.OutfilePath()[length - 1]) {
142                                         case 'h': {
143                                                 std::ofstream outstream(args.OutfilePath());
144                                                 source.WriteHeader(outstream);
145                                                 break;
146                                         }
147                                         default: {
148                                                 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
149                                         }
150                                 }
151                                 return 0;
152                         }
153                         case Arguments::DUMP: {
154                                 std::cout << source << std::endl;
155                                 return 0;
156                         }
157                         case Arguments::PLAY:
158                                 break;
159                 }
160
161                 Interpreter intp(source);
162                 intp.ReadSource();
163
164                 if (intp.PostponedDefinitions().size() > 0) {
165                         for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
166                                 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
167                         }
168                         return 3;
169                 }
170
171                 Caster caster(intp);
172
173                 // temporary test data
174                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
175                 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
176                 PartyLayout heroesLayout(*caster.GetPartyLayout("heroesLayout"));
177
178                 Monster monster(*caster.GetMonster("lizard"));
179                 Hero maxim(*caster.GetHero("maxim"));
180                 Hero selan(*caster.GetHero("selan"));
181                 Hero guy(*caster.GetHero("guy"));
182                 Hero dekar(*caster.GetHero("dekar"));
183
184                 battle::Resources *battleRes(caster.GetBattleResources("battleResources"));
185
186                 maxim.AddSpell(caster.GetSpell("resetSpell"));
187                 Spell *strongSpell(caster.GetSpell("strongSpell"));
188                 maxim.AddSpell(strongSpell);
189                 selan.AddSpell(strongSpell);
190                 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
191                 maxim.AddSpell(strongerSpell);
192                 selan.AddSpell(strongerSpell);
193                 Spell *championSpell(caster.GetSpell("championSpell"));
194                 maxim.AddSpell(championSpell);
195                 selan.AddSpell(championSpell);
196                 Spell *rallySpell(caster.GetSpell("rallySpell"));
197                 maxim.AddSpell(rallySpell);
198                 selan.AddSpell(rallySpell);
199                 selan.AddSpell(caster.GetSpell("escapeSpell"));
200                 Spell *valorSpell(caster.GetSpell("valorSpell"));
201                 maxim.AddSpell(valorSpell);
202                 selan.AddSpell(valorSpell);
203
204                 Inventory inventory;
205                 inventory.Add(caster.GetItem("antidoteItem"), 9);
206                 inventory.Add(caster.GetItem("magicJarItem"), 4);
207                 inventory.Add(caster.GetItem("hiPotionItem"), 4);
208                 inventory.Add(caster.GetItem("powerPotionItem"), 4);
209                 inventory.Add(caster.GetItem("escapeItem"), 2);
210                 inventory.Add(caster.GetItem("sleepBallItem"), 1);
211                 battleRes->inventory = &inventory;
212
213                 maxim.SetWeapon(caster.GetItem("zircoSwordItem"));
214                 maxim.SetArmor(caster.GetItem("zirconArmorItem"));
215                 maxim.SetShield(caster.GetItem("holyShieldItem"));
216                 maxim.SetHelmet(caster.GetItem("legendHelmItem"));
217                 maxim.SetRing(caster.GetItem("sProRingItem"));
218                 maxim.SetJewel(caster.GetItem("evilJewelItem"));
219
220 //              selan.SetWeapon(cst.GetItem("zircoWhipItem"));
221                 selan.SetArmor(caster.GetItem("zirconPlateItem"));
222                 selan.SetShield(caster.GetItem("zircoGlovesItem"));
223                 selan.SetHelmet(caster.GetItem("holyCapItem"));
224                 selan.SetRing(caster.GetItem("ghostRingItem"));
225                 selan.SetJewel(caster.GetItem("eagleRockItem"));
226
227 //              guy.SetWeapon(cst.GetItem("zircoAxItem"));
228                 guy.SetArmor(caster.GetItem("zirconArmorItem"));
229                 guy.SetShield(caster.GetItem("megaShieldItem"));
230                 guy.SetHelmet(caster.GetItem("zircoHelmetItem"));
231                 guy.SetRing(caster.GetItem("powerRingItem"));
232                 guy.SetJewel(caster.GetItem("evilJewelItem"));
233
234                 // NOTE: this is actually Artea equipment
235 //              dekar.SetWeapon(cst.GetItem("lizardBlowItem"));
236                 dekar.SetArmor(caster.GetItem("holyRobeItem"));
237                 dekar.SetShield(caster.GetItem("zircoGlovesItem"));
238                 dekar.SetHelmet(caster.GetItem("holyCapItem"));
239                 dekar.SetRing(caster.GetItem("rocketRingItem"));
240                 dekar.SetJewel(caster.GetItem("krakenRockItem"));
241
242                 Tile tiles[64];
243
244                 tiles[ 0].SetOffset(Vector<int>(2, 1));
245                 tiles[ 1].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_WEST);
246                 tiles[ 2].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
247                 tiles[ 3].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
248                 tiles[ 4].SetOffset(Vector<int>(0, 1));
249                 tiles[ 5].SetOffset(Vector<int>(2, 0));
250                 tiles[ 6].SetOffset(Vector<int>(2, 0));
251                 tiles[ 7].SetOffset(Vector<int>(2, 0));
252
253                 tiles[ 8].SetOffset(Vector<int>(2, 1));
254                 tiles[ 9].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
255                 tiles[10].SetOffset(Vector<int>(3, 0));
256                 tiles[11].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
257                 tiles[12].SetOffset(Vector<int>(0, 2));
258                 tiles[13].SetOffset(Vector<int>(1, 2));
259                 tiles[14].SetOffset(Vector<int>(1, 2));
260                 tiles[15].SetOffset(Vector<int>(1, 2));
261
262                 tiles[16].SetOffset(Vector<int>(2, 1));
263                 tiles[17].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
264                 tiles[18].SetOffset(Vector<int>(3, 0));
265                 tiles[19].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
266                 tiles[20].SetOffset(Vector<int>(0, 3));
267                 tiles[21].SetOffset(Vector<int>(1, 3));
268                 tiles[22].SetOffset(Vector<int>(1, 3));
269                 tiles[23].SetOffset(Vector<int>(2, 3));
270
271                 tiles[24].SetOffset(Vector<int>(2, 1));
272                 tiles[25].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
273                 tiles[26].SetOffset(Vector<int>(3, 0));
274                 tiles[27].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
275                 tiles[28].SetOffset(Vector<int>(0, 4));
276                 tiles[29].SetOffset(Vector<int>(1, 4));
277                 tiles[30].SetOffset(Vector<int>(1, 4));
278                 tiles[31].SetOffset(Vector<int>(2, 4));
279
280                 tiles[32].SetOffset(Vector<int>(2, 1));
281                 tiles[33].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
282                 tiles[34].SetOffset(Vector<int>(3, 0));
283                 tiles[35].SetOffset(Vector<int>(3, 0));
284                 tiles[36].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
285                 tiles[37].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
286                 tiles[38].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
287                 tiles[39].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
288
289                 tiles[40].SetOffset(Vector<int>(2, 1));
290                 tiles[41].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
291                 tiles[42].SetOffset(Vector<int>(3, 0));
292                 tiles[43].SetOffset(Vector<int>(3, 0));
293                 tiles[44].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
294                 tiles[45].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
295                 tiles[46].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
296                 tiles[47].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
297
298                 tiles[48].SetOffset(Vector<int>(2, 1));
299                 tiles[49].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
300                 tiles[50].SetOffset(Vector<int>(3, 0));
301                 tiles[51].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
302                 tiles[52].SetOffset(Vector<int>(0, 0));
303                 tiles[53].SetOffset(Vector<int>(1, 0));
304                 tiles[54].SetOffset(Vector<int>(1, 0));
305                 tiles[55].SetOffset(Vector<int>(1, 0));
306
307                 tiles[56].SetOffset(Vector<int>(2, 1));
308                 tiles[57].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_WEST);
309                 tiles[58].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
310                 tiles[59].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
311                 tiles[60].SetOffset(Vector<int>(0, 1));
312                 tiles[61].SetOffset(Vector<int>(1, 1));
313                 tiles[62].SetOffset(Vector<int>(1, 1));
314                 tiles[63].SetOffset(Vector<int>(1, 1));
315
316                 Area areas[1];
317                 areas[0].SetTiles(tiles, 64);
318                 areas[0].SetWidth(8);
319
320                 Trigger triggers[1];
321                 triggers[0].SetTilePosition(Vector<int>(2, 0));
322
323                 SDL_Surface *tilesetImg(IMG_Load("test-data/tileset.png"));
324                 Sprite tileset(tilesetImg, tileSize, tileSize);
325
326                 Map map;
327                 map.SetAreas(areas, 1);
328                 map.SetTileset(&tileset);
329                 map.SetTriggers(triggers, 1);
330                 map.SetWidth(1);
331
332                 SDL_Surface *mapMaximImg(IMG_Load("test-data/maxim-map.png"));
333                 Sprite mapMaximSprite(mapMaximImg, 32, 64);
334                 SimpleAnimation mapMaximAnimation(&mapMaximSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
335                 Entity mapMaxim;
336                 mapMaxim.SetAnimation(&mapMaximAnimation);
337                 mapMaxim.Position() = Vector<float>(64, 128);
338                 mapMaxim.SpriteOffset() = Vector<float>(0, -32);
339
340                 SDL_Surface *mapSelanImg(IMG_Load("test-data/selan-map.png"));
341                 Sprite mapSelanSprite(mapSelanImg, 32, 64);
342                 SimpleAnimation mapSelanAnimation(&mapSelanSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
343                 Entity mapSelan;
344                 mapSelan.SetAnimation(&mapSelanAnimation);
345                 mapSelan.Position() = Vector<float>(64, 128);
346                 mapSelan.SpriteOffset() = Vector<float>(0, -32);
347                 mapMaxim.AddFollower(&mapSelan);
348
349                 SDL_Surface *mapGuyImg(IMG_Load("test-data/guy-map.png"));
350                 Sprite mapGuySprite(mapGuyImg, 32, 64);
351                 SimpleAnimation mapGuyAnimation(&mapGuySprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
352                 Entity mapGuy;
353                 mapGuy.SetAnimation(&mapGuyAnimation);
354                 mapGuy.Position() = Vector<float>(64, 128);
355                 mapGuy.SpriteOffset() = Vector<float>(0, -32);
356                 mapSelan.AddFollower(&mapGuy);
357
358                 SDL_Surface *mapDekarImg(IMG_Load("test-data/dekar-map.png"));
359                 Sprite mapDekarSprite(mapDekarImg, 32, 64);
360                 SimpleAnimation mapDekarAnimation(&mapDekarSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
361                 Entity mapDekar;
362                 mapDekar.SetAnimation(&mapDekarAnimation);
363                 mapDekar.Position() = Vector<float>(64, 128);
364                 mapDekar.SpriteOffset() = Vector<float>(0, -32);
365                 mapGuy.AddFollower(&mapDekar);
366
367                 InitScreen screen(width, height);
368
369                 app::State *state(0);
370
371                 if (battle) {
372                         BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
373                         battleState->AddMonster(monster);
374                         battleState->AddMonster(monster);
375                         battleState->AddMonster(monster);
376                         battleState->AddMonster(monster);
377                         battleState->AddHero(maxim);
378                         battleState->AddHero(selan);
379                         battleState->AddHero(guy);
380                         battleState->AddHero(dekar);
381                         state = battleState;
382                 } else {
383                         MapState *mapState(new MapState(&map));
384                         mapState->AddEntity(&mapMaxim);
385                         mapState->AddEntity(&mapSelan);
386                         mapState->AddEntity(&mapGuy);
387                         mapState->AddEntity(&mapDekar);
388                         mapState->ControlEntity(&mapMaxim);
389                         mapState->SetWalkingSpeed(walkSpeed);
390                         state = mapState;
391                 }
392
393                 Application app(&screen, state);
394                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
395                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
396                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
397                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
398                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
399                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
400                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
401                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
402                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
403                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
404                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
405                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
406                 app.Run();
407
408                 return 0;
409         } catch (Parser::Error &e) {
410                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
411                 return 2;
412         } catch (exception &e) {
413                 cerr << "exception in main(): " << e.what() << endl;
414                 return 1;
415         }
416 }