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