]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
2f1c12ab655f4c2e92208bff89b37825f90780a2
[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/Input.h"
10 #include "battle/BattleState.h"
11 #include "battle/Hero.h"
12 #include "battle/Monster.h"
13 #include "battle/PartyLayout.h"
14 #include "battle/Resources.h"
15 #include "battle/Stats.h"
16 #include "common/Ikari.h"
17 #include "common/Inventory.h"
18 #include "common/Item.h"
19 #include "common/Spell.h"
20 #include "geometry/Vector.h"
21 #include "graphics/ComplexAnimation.h"
22 #include "graphics/Font.h"
23 #include "graphics/Frame.h"
24 #include "graphics/Gauge.h"
25 #include "graphics/Menu.h"
26 #include "graphics/SimpleAnimation.h"
27 #include "graphics/Sprite.h"
28 #include "loader/Interpreter.h"
29 #include "loader/ParsedSource.h"
30 #include "loader/Parser.h"
31 #include "sdl/InitImage.h"
32 #include "sdl/InitScreen.h"
33 #include "sdl/InitSDL.h"
34
35 #include <cstdlib>
36 #include <ctime>
37 #include <exception>
38 #include <iostream>
39 #include <SDL.h>
40 #include <SDL_image.h>
41
42 using app::Application;
43 using app::Input;
44 using battle::BattleState;
45 using battle::Hero;
46 using battle::Monster;
47 using battle::PartyLayout;
48 using battle::Stats;
49 using common::Ikari;
50 using common::Inventory;
51 using common::Item;
52 using common::Spell;
53 using geometry::Vector;
54 using graphics::ComplexAnimation;
55 using graphics::Font;
56 using graphics::Frame;
57 using graphics::Gauge;
58 using graphics::Menu;
59 using graphics::SimpleAnimation;
60 using graphics::Sprite;
61 using loader::Interpreter;
62 using loader::ParsedSource;
63 using loader::Parser;
64 using sdl::InitImage;
65 using sdl::InitScreen;
66 using sdl::InitSDL;
67
68 using std::cerr;
69 using std::cout;
70 using std::endl;
71 using std::exception;
72
73 int main(int argc, char **argv) {
74         const int width = 800;
75         const int height = 480;
76
77         const int framerate = 33;
78
79 //      std::srand(std::time(0));
80
81         try {
82                 InitSDL sdl;
83                 InitImage image(IMG_INIT_PNG);
84
85                 ParsedSource source;
86                 Parser parser("test-data/test.l2s", source);
87                 parser.Parse();
88                 Interpreter intp(source);
89                 intp.ReadSource();
90
91                 InitScreen screen(width, height);
92
93                 // temporary test data
94                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
95                 PartyLayout monstersLayout(*intp.GetPartyLayout("monstersLayout"));
96                 PartyLayout heroesLayout(*intp.GetPartyLayout("heroesLayout"));
97
98                 Monster monster(*intp.GetMonster("lizard"));
99                 Hero maxim(*intp.GetHero("maxim"));
100                 Hero selan(*intp.GetHero("selan"));
101                 Hero guy(*intp.GetHero("guy"));
102                 Hero dekar(*intp.GetHero("dekar"));
103
104                 battle::Resources battleRes;
105
106                 battleRes.swapCursor = intp.GetSprite("swapCursor");
107                 battleRes.attackIcons = intp.GetSprite("attackIcons");
108                 battleRes.attackChoiceIcons = intp.GetSprite("attackChoiceIcons");
109                 battleRes.moveIcons = intp.GetSprite("moveIcons");
110                 battleRes.titleFrame = intp.GetFrame("titleFrame");
111                 battleRes.titleFont = intp.GetFont("largeFont");
112                 battleRes.numberAnimationPrototype = intp.GetAnimation("numberAnimationPrototype");
113                 battleRes.bigNumberSprite = intp.GetSprite("bigNumbers");
114                 battleRes.greenNumberSprite = intp.GetSprite("bigGreenNumbers");
115
116                 battleRes.heroTagLabels = intp.GetSprite("heroTagLabels");
117                 battleRes.levelLabelCol = 0;
118                 battleRes.levelLabelRow = 0;
119                 battleRes.healthLabelCol = 0;
120                 battleRes.healthLabelRow = 1;
121                 battleRes.manaLabelCol = 0;
122                 battleRes.manaLabelRow = 2;
123                 battleRes.moveLabelCol = 0;
124                 battleRes.moveLabelRow = 3;
125                 battleRes.ikariLabelCol = 0;
126                 battleRes.ikariLabelRow = 4;
127
128                 battleRes.heroTagFont = intp.GetFont("heroTagFont");
129                 battleRes.heroTagFrame = intp.GetFrame("heroTagFrame");
130                 battleRes.activeHeroTagFrame = intp.GetFrame("activeHeroTagFrame");
131                 battleRes.smallHeroTagFrame = intp.GetFrame("smallHeroTagFrame");
132                 battleRes.lastSmallHeroTagFrame = intp.GetFrame("lastSmallHeroTagFrame");
133                 battleRes.heroesBgColor = SDL_MapRGB(screen.Screen()->format, 0x18, 0x28, 0x31);
134
135                 battleRes.healthGauge = intp.GetGauge("healthGauge");
136                 battleRes.manaGauge = intp.GetGauge("manaGauge");
137                 battleRes.ikariGauge = intp.GetGauge("ikariGauge");
138
139                 battleRes.selectFrame = intp.GetFrame("selectFrame");
140                 battleRes.normalFont = intp.GetFont("normalFont");
141                 battleRes.disabledFont = intp.GetFont("disabledFont");
142                 battleRes.menuCursor = intp.GetSprite("handCursor");
143
144                 battleRes.weaponTargetCursor = intp.GetSprite("weaponTargetCursor");
145                 battleRes.magicTargetCursor = intp.GetSprite("magicTargetCursor");
146                 battleRes.itemTargetCursor = intp.GetSprite("itemTargetCursor");
147
148                 maxim.AddSpell(intp.GetSpell("resetSpell"));
149                 Spell *strongSpell(intp.GetSpell("strongSpell"));
150                 maxim.AddSpell(strongSpell);
151                 selan.AddSpell(strongSpell);
152                 Spell *strongerSpell(intp.GetSpell("strongerSpell"));
153                 maxim.AddSpell(strongerSpell);
154                 selan.AddSpell(strongerSpell);
155                 Spell *championSpell(intp.GetSpell("championSpell"));
156                 maxim.AddSpell(championSpell);
157                 selan.AddSpell(championSpell);
158                 Spell *rallySpell(intp.GetSpell("rallySpell"));
159                 maxim.AddSpell(rallySpell);
160                 selan.AddSpell(rallySpell);
161                 selan.AddSpell(intp.GetSpell("escapeSpell"));
162                 Spell *valorSpell(intp.GetSpell("valorSpell"));
163                 maxim.AddSpell(valorSpell);
164                 selan.AddSpell(valorSpell);
165
166                 battleRes.spellMenuHeadline = "Please choose a spell.";
167                 battleRes.spellMenuPrototype = Menu<const Spell *>(intp.GetFont("normalFont"), intp.GetFont("disabledFont"), intp.GetSprite("handCursor"), 9, 6, 8, 0, 2, 32, 2, ':');
168
169                 SDL_Surface *itemIcons(IMG_Load("test-data/item-icons.png"));
170                 Sprite potionIcon(itemIcons, 16, 16);
171                 Sprite ballIcon(itemIcons, 16, 16, 0, 16);
172                 Sprite crankIcon(itemIcons, 16, 16, 0, 32);
173                 Sprite spearIcon(itemIcons, 16, 16, 0, 48);
174                 Sprite swordIcon(itemIcons, 16, 16, 0, 64);
175                 Sprite axIcon(itemIcons, 16, 16, 0, 80);
176                 Sprite rodIcon(itemIcons, 16, 16, 0, 96);
177                 Sprite armorIcon(itemIcons, 16, 16, 0, 112);
178                 Sprite shieldIcon(itemIcons, 16, 16, 0, 128);
179                 Sprite helmetIcon(itemIcons, 16, 16, 0, 144);
180                 Sprite ringIcon(itemIcons, 16, 16, 0, 160);
181                 Sprite jewelIcon(itemIcons, 16, 16, 0, 176);
182
183                 battleRes.weaponMenuIcon = &swordIcon;
184                 battleRes.armorMenuIcon = &armorIcon;
185                 battleRes.shieldMenuIcon = &shieldIcon;
186                 battleRes.helmetMenuIcon = &helmetIcon;
187                 battleRes.ringMenuIcon = &ringIcon;
188                 battleRes.jewelMenuIcon = &jewelIcon;
189
190                 Inventory inventory;
191                 Item antidote;
192                 antidote.SetName("Antidote");
193                 antidote.SetMenuIcon(&potionIcon);
194                 antidote.SetUsableInBattle();
195                 antidote.GetTargetingMode().TargetSingleAlly();
196                 inventory.Add(&antidote, 9);
197                 Item magicJar;
198                 magicJar.SetName("Magic jar");
199                 magicJar.SetMenuIcon(&potionIcon);
200                 magicJar.SetUsableInBattle();
201                 magicJar.GetTargetingMode().TargetSingleAlly();
202                 inventory.Add(&magicJar, 4);
203                 Item hiPotion;
204                 hiPotion.SetName("Hi-Potion");
205                 hiPotion.SetMenuIcon(&potionIcon);
206                 hiPotion.SetUsableInBattle();
207                 hiPotion.GetTargetingMode().TargetSingleAlly();
208                 inventory.Add(&hiPotion, 4);
209                 Item powerPotion;
210                 powerPotion.SetName("Power potion");
211                 powerPotion.SetMenuIcon(&potionIcon);
212                 inventory.Add(&powerPotion, 4);
213                 Item escape;
214                 escape.SetName("Escape");
215                 inventory.Add(&escape, 2);
216                 Item sleepBall;
217                 sleepBall.SetName("Sleep ball");
218                 sleepBall.SetMenuIcon(&ballIcon);
219                 sleepBall.SetUsableInBattle();
220                 sleepBall.GetTargetingMode().TargetSingleEnemy();
221                 inventory.Add(&sleepBall, 1);
222                 Item multiBall;
223                 multiBall.SetName("Multi-ball!");
224                 multiBall.SetMenuIcon(&ballIcon);
225                 multiBall.SetUsableInBattle();
226                 multiBall.GetTargetingMode().TargetMultipleEnemies();
227                 inventory.Add(&multiBall, 1);
228                 Item figgoru;
229                 figgoru.SetName("Figgoru");
230                 figgoru.SetMenuIcon(&crankIcon);
231                 figgoru.GetTargetingMode().TargetAllEnemies();
232                 inventory.Add(&figgoru, 1);
233                 battleRes.inventory = &inventory;
234
235                 battleRes.itemMenuHeadline = "Please choose an item.";
236                 battleRes.itemMenuPrototype = Menu<const common::Item *>(intp.GetFont("normalFont"), intp.GetFont("disabledFont"), intp.GetSprite("handCursor"), 15, 6, 8, 16, 1, 32, 2, ':');
237
238                 SDL_Surface *swordAttackImg(IMG_Load("test-data/attack-sword.png"));
239                 Sprite swordAttackSprite(swordAttackImg, 96, 96);
240                 SimpleAnimation swordAttackAnimation(&swordAttackSprite, 2 * framerate, 4);
241
242                 Item zircoSword;
243                 zircoSword.SetName("Zirco sword");
244                 zircoSword.SetMenuIcon(&swordIcon);
245                 zircoSword.GetTargetingMode().TargetSingleEnemy();
246                 Ikari firestorm;
247                 firestorm.SetName("Firestorm");
248                 firestorm.SetCost(224);
249                 firestorm.GetTargetingMode().TargetAllEnemies();
250                 firestorm.SetPhysical();
251                 zircoSword.SetIkari(&firestorm);
252                 zircoSword.SetAttackAnimation(&swordAttackAnimation);
253                 maxim.SetWeapon(&zircoSword);
254                 Item zirconArmor;
255                 zirconArmor.SetName("Zircon armor");
256                 zirconArmor.SetMenuIcon(&armorIcon);
257                 Ikari magicCure;
258                 magicCure.SetName("Magic cure");
259                 magicCure.SetCost(128);
260                 magicCure.GetTargetingMode().TargetSingleAlly();
261                 magicCure.SetMagical();
262                 zirconArmor.SetIkari(&magicCure);
263                 maxim.SetArmor(&zirconArmor);
264                 Item holyShield;
265                 holyShield.SetName("Holy shield");
266                 holyShield.SetMenuIcon(&shieldIcon);
267                 Ikari lightGuard;
268                 lightGuard.SetName("Light guard");
269                 lightGuard.SetCost(128);
270                 lightGuard.GetTargetingMode().TargetAllAllies(); // actually only targets self
271                 lightGuard.SetMagical();
272                 holyShield.SetIkari(&lightGuard);
273                 maxim.SetShield(&holyShield);
274                 Item legendHelm;
275                 legendHelm.SetName("Legend helm");
276                 legendHelm.SetMenuIcon(&helmetIcon);
277                 Ikari boomerang;
278                 boomerang.SetName("Boomerang");
279                 boomerang.SetCost(164);
280                 boomerang.GetTargetingMode().TargetAllAllies(); // actually only targets self
281                 boomerang.SetMagical();
282                 legendHelm.SetIkari(&boomerang);
283                 maxim.SetHelmet(&legendHelm);
284                 Item sProRing;
285                 sProRing.SetName("S-pro ring");
286                 sProRing.SetMenuIcon(&ringIcon);
287                 Ikari courage;
288                 courage.SetName("Courage");
289                 courage.SetCost(64);
290                 courage.GetTargetingMode().TargetMultipleAllies();
291                 courage.SetMagical();
292                 sProRing.SetIkari(&courage);
293                 maxim.SetRing(&sProRing);
294                 Item evilJewel;
295                 evilJewel.SetName("Evil jewel");
296                 evilJewel.SetMenuIcon(&jewelIcon);
297                 Ikari gloomy;
298                 gloomy.SetName("Gloomy");
299                 gloomy.SetCost(164);
300                 gloomy.GetTargetingMode().TargetAllEnemies();
301                 gloomy.SetMagical();
302                 evilJewel.SetIkari(&gloomy);
303                 maxim.SetJewel(&evilJewel);
304
305                 Item zircoWhip;
306                 zircoWhip.SetName("Zirco whip");
307                 zircoWhip.SetMenuIcon(&rodIcon);
308                 zircoWhip.GetTargetingMode().TargetSingleEnemy();
309                 Ikari thundershriek;
310                 thundershriek.SetName("Thundershriek");
311                 thundershriek.SetCost(224);
312                 thundershriek.GetTargetingMode().TargetAllEnemies();
313                 thundershriek.SetPhysical();
314                 zircoWhip.SetIkari(&thundershriek);
315 //              selan.SetWeapon(&zircoWhip);
316                 Item zirconPlate;
317                 zirconPlate.SetName("Zircon plate");
318                 zirconPlate.SetMenuIcon(&armorIcon);
319                 Ikari suddenCure;
320                 suddenCure.SetName("Sudden cure");
321                 suddenCure.SetCost(96);
322                 suddenCure.GetTargetingMode().TargetAllAllies();
323                 suddenCure.SetMagical();
324                 zirconPlate.SetIkari(&suddenCure);
325                 selan.SetArmor(&zirconPlate);
326                 Item zircoGloves;
327                 zircoGloves.SetName("Zirco gloves");
328                 zircoGloves.SetMenuIcon(&shieldIcon);
329                 Ikari forcefield;
330                 forcefield.SetName("Forcefield");
331                 forcefield.SetCost(64);
332                 forcefield.GetTargetingMode().TargetAllAllies();
333                 forcefield.SetMagical();
334                 zircoGloves.SetIkari(&forcefield);
335                 selan.SetShield(&zircoGloves);
336                 Item holyCap;
337                 holyCap.SetName("Holy cap");
338                 holyCap.SetMenuIcon(&helmetIcon);
339                 Ikari vulnerable;
340                 vulnerable.SetName("Vulnerable");
341                 vulnerable.SetCost(196);
342                 vulnerable.GetTargetingMode().TargetAllEnemies();
343                 vulnerable.SetPhysical();
344                 holyCap.SetIkari(&vulnerable);
345                 selan.SetHelmet(&holyCap);
346                 Item ghostRing;
347                 ghostRing.SetName("Ghost ring");
348                 ghostRing.SetMenuIcon(&ringIcon);
349                 Ikari destroy;
350                 destroy.SetName("Destroy");
351                 destroy.SetCost(128);
352                 destroy.GetTargetingMode().TargetMultipleEnemies();
353                 destroy.SetMagical();
354                 ghostRing.SetIkari(&destroy);
355                 selan.SetRing(&ghostRing);
356                 Item eagleRock;
357                 eagleRock.SetName("Eagle rock");
358                 eagleRock.SetMenuIcon(&jewelIcon);
359                 Ikari dive;
360                 dive.SetName("Dive");
361                 dive.SetCost(128);
362                 dive.GetTargetingMode().TargetSingleEnemy();
363                 dive.SetPhysical();
364                 eagleRock.SetIkari(&dive);
365                 selan.SetJewel(&eagleRock);
366
367                 Item zircoAx;
368                 zircoAx.SetName("Zirco ax");
369                 zircoAx.SetMenuIcon(&axIcon);
370                 zircoAx.GetTargetingMode().TargetSingleEnemy();
371                 Ikari torrent;
372                 torrent.SetName("Torrent");
373                 torrent.SetCost(224);
374                 torrent.GetTargetingMode().TargetAllEnemies();
375                 torrent.SetPhysical();
376                 zircoAx.SetIkari(&torrent);
377 //              guy.SetWeapon(&zircoAx);
378                 guy.SetArmor(&zirconArmor);
379                 Item megaShield;
380                 megaShield.SetName("Mega shield");
381                 megaShield.SetMenuIcon(&shieldIcon);
382                 Ikari ironBarrier;
383                 ironBarrier.SetName("Iron barrier");
384                 ironBarrier.SetCost(255);
385                 ironBarrier.GetTargetingMode().TargetAllAllies(); // actually only targets self
386                 ironBarrier.SetMagical();
387                 megaShield.SetIkari(&ironBarrier);
388                 guy.SetShield(&megaShield);
389                 Item zircoHelmet;
390                 zircoHelmet.SetName("Zirco helmet");
391                 zircoHelmet.SetMenuIcon(&helmetIcon);
392                 Ikari slow;
393                 slow.SetName("Slow");
394                 slow.SetCost(196);
395                 slow.GetTargetingMode().TargetAllEnemies();
396                 slow.SetPhysical();
397                 zircoHelmet.SetIkari(&slow);
398                 guy.SetHelmet(&zircoHelmet);
399                 Item powerRing;
400                 powerRing.SetName("Power ring");
401                 powerRing.SetMenuIcon(&ringIcon);
402                 Ikari trick;
403                 trick.SetName("Trick");
404                 trick.SetCost(32);
405                 trick.GetTargetingMode().TargetAllEnemies();
406                 trick.SetMagical();
407                 zircoHelmet.SetIkari(&trick);
408                 guy.SetRing(&powerRing);
409                 guy.SetJewel(&evilJewel);
410
411                 // NOTE: this is actually Artea equipment
412                 Item lizardBlow;
413                 lizardBlow.SetName("Lizard blow");
414                 lizardBlow.SetMenuIcon(&swordIcon);
415                 lizardBlow.GetTargetingMode().TargetSingleEnemy();
416                 Ikari dragonRush;
417                 dragonRush.SetName("Dragon rush");
418                 dragonRush.SetCost(164);
419                 dragonRush.GetTargetingMode().TargetSingleEnemy();
420                 dragonRush.SetPhysical();
421                 lizardBlow.SetIkari(&dragonRush);
422 //              dekar.SetWeapon(&lizardBlow);
423                 Item holyRobe;
424                 holyRobe.SetName("Holy robe");
425                 holyRobe.SetMenuIcon(&armorIcon);
426                 Ikari crisisCure;
427                 crisisCure.SetName("Crisis cure");
428                 crisisCure.SetCost(164);
429                 crisisCure.GetTargetingMode().TargetAllAllies();
430                 crisisCure.SetMagical();
431                 holyRobe.SetIkari(&crisisCure);
432                 dekar.SetArmor(&holyRobe);
433                 dekar.SetShield(&zircoGloves);
434                 dekar.SetHelmet(&holyCap);
435                 Item rocketRing;
436                 rocketRing.SetName("Rocket ring");
437                 rocketRing.SetMenuIcon(&ringIcon);
438                 Ikari fake;
439                 fake.SetName("Fake");
440                 fake.SetCost(32);
441                 fake.GetTargetingMode().TargetSingleAlly();
442                 fake.SetMagical();
443                 rocketRing.SetIkari(&fake);
444                 dekar.SetRing(&rocketRing);
445                 Item krakenRock;
446                 krakenRock.SetName("Kraken rock");
447                 krakenRock.SetMenuIcon(&jewelIcon);
448                 Ikari tenLegger;
449                 tenLegger.SetName("Ten-legger");
450                 tenLegger.SetCost(164);
451                 tenLegger.GetTargetingMode().TargetAllEnemies();
452                 tenLegger.SetPhysical();
453                 rocketRing.SetIkari(&tenLegger);
454                 dekar.SetJewel(&krakenRock);
455
456                 battleRes.ikariMenuHeadline = "Please choose equipment.";
457                 battleRes.noEquipmentText = "No equip";
458                 battleRes.ikariMenuPrototype = Menu<const Item *>(intp.GetFont("normalFont"), intp.GetFont("disabledFont"), intp.GetSprite("handCursor"), 12, 6, intp.GetFont("normalFont")->CharHeight() / 2, intp.GetFont("normalFont")->CharWidth(), 1, intp.GetFont("normalFont")->CharWidth() * 2, 0, ':', 12, intp.GetFont("normalFont")->CharWidth());
459
460                 battleRes.escapeText = "Escapes.";
461
462                 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &battleRes));
463                 battleState->AddMonster(monster);
464                 battleState->AddMonster(monster);
465                 battleState->AddMonster(monster);
466                 battleState->AddMonster(monster);
467                 battleState->AddHero(maxim);
468                 battleState->AddHero(selan);
469                 battleState->AddHero(guy);
470                 battleState->AddHero(dekar);
471                 Application app(&screen, battleState);
472                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
473                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
474                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
475                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
476                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
477                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
478                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
479                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
480                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
481                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
482                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
483                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
484                 app.Run();
485
486                 return 0;
487         } catch (Parser::Error &e) {
488                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
489                 return 1;
490         } catch (exception &e) {
491                 cerr << "exception in main(): " << e.what() << endl;
492                 return 1;
493         }
494 }