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