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