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