]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
moved icon config to test.l2s
[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 = intp.GetString("spellMenuHeadline");
167                 battleRes.spellMenuPrototype = Menu<const Spell *>(intp.GetFont("normalFont"), intp.GetFont("disabledFont"), intp.GetSprite("handCursor"), 9, 6, 8, 0, 2, 32, 2, ':');
168
169                 battleRes.weaponMenuIcon = intp.GetSprite("swordIcon");
170                 battleRes.armorMenuIcon = intp.GetSprite("armorIcon");
171                 battleRes.shieldMenuIcon = intp.GetSprite("shieldIcon");
172                 battleRes.helmetMenuIcon = intp.GetSprite("helmetIcon");
173                 battleRes.ringMenuIcon = intp.GetSprite("ringIcon");
174                 battleRes.jewelMenuIcon = intp.GetSprite("jewelIcon");
175
176                 Inventory inventory;
177                 Item antidote;
178                 antidote.SetName("Antidote");
179                 antidote.SetMenuIcon(intp.GetSprite("potionIcon"));
180                 antidote.SetUsableInBattle();
181                 antidote.GetTargetingMode().TargetSingleAlly();
182                 inventory.Add(&antidote, 9);
183                 Item magicJar;
184                 magicJar.SetName("Magic jar");
185                 magicJar.SetMenuIcon(intp.GetSprite("potionIcon"));
186                 magicJar.SetUsableInBattle();
187                 magicJar.GetTargetingMode().TargetSingleAlly();
188                 inventory.Add(&magicJar, 4);
189                 Item hiPotion;
190                 hiPotion.SetName("Hi-Potion");
191                 hiPotion.SetMenuIcon(intp.GetSprite("potionIcon"));
192                 hiPotion.SetUsableInBattle();
193                 hiPotion.GetTargetingMode().TargetSingleAlly();
194                 inventory.Add(&hiPotion, 4);
195                 Item powerPotion;
196                 powerPotion.SetName("Power potion");
197                 powerPotion.SetMenuIcon(intp.GetSprite("potionIcon"));
198                 inventory.Add(&powerPotion, 4);
199                 Item escape;
200                 escape.SetName("Escape");
201                 inventory.Add(&escape, 2);
202                 Item sleepBall;
203                 sleepBall.SetName("Sleep ball");
204                 sleepBall.SetMenuIcon(intp.GetSprite("ballIcon"));
205                 sleepBall.SetUsableInBattle();
206                 sleepBall.GetTargetingMode().TargetSingleEnemy();
207                 inventory.Add(&sleepBall, 1);
208                 Item multiBall;
209                 multiBall.SetName("Multi-ball!");
210                 multiBall.SetMenuIcon(intp.GetSprite("ballIcon"));
211                 multiBall.SetUsableInBattle();
212                 multiBall.GetTargetingMode().TargetMultipleEnemies();
213                 inventory.Add(&multiBall, 1);
214                 Item figgoru;
215                 figgoru.SetName("Figgoru");
216                 figgoru.SetMenuIcon(intp.GetSprite("crankIcon"));
217                 figgoru.GetTargetingMode().TargetAllEnemies();
218                 inventory.Add(&figgoru, 1);
219                 battleRes.inventory = &inventory;
220
221                 battleRes.itemMenuHeadline = "Please choose an item.";
222                 battleRes.itemMenuPrototype = Menu<const common::Item *>(intp.GetFont("normalFont"), intp.GetFont("disabledFont"), intp.GetSprite("handCursor"), 15, 6, 8, 16, 1, 32, 2, ':');
223
224                 SDL_Surface *swordAttackImg(IMG_Load("test-data/attack-sword.png"));
225                 Sprite swordAttackSprite(swordAttackImg, 96, 96);
226                 SimpleAnimation swordAttackAnimation(&swordAttackSprite, 2 * framerate, 4);
227
228                 Item zircoSword;
229                 zircoSword.SetName("Zirco sword");
230                 zircoSword.SetMenuIcon(intp.GetSprite("swordIcon"));
231                 zircoSword.GetTargetingMode().TargetSingleEnemy();
232                 Ikari firestorm;
233                 firestorm.SetName("Firestorm");
234                 firestorm.SetCost(224);
235                 firestorm.GetTargetingMode().TargetAllEnemies();
236                 firestorm.SetPhysical();
237                 zircoSword.SetIkari(&firestorm);
238                 zircoSword.SetAttackAnimation(&swordAttackAnimation);
239                 maxim.SetWeapon(&zircoSword);
240                 Item zirconArmor;
241                 zirconArmor.SetName("Zircon armor");
242                 zirconArmor.SetMenuIcon(intp.GetSprite("armorIcon"));
243                 Ikari magicCure;
244                 magicCure.SetName("Magic cure");
245                 magicCure.SetCost(128);
246                 magicCure.GetTargetingMode().TargetSingleAlly();
247                 magicCure.SetMagical();
248                 zirconArmor.SetIkari(&magicCure);
249                 maxim.SetArmor(&zirconArmor);
250                 Item holyShield;
251                 holyShield.SetName("Holy shield");
252                 holyShield.SetMenuIcon(intp.GetSprite("shieldIcon"));
253                 Ikari lightGuard;
254                 lightGuard.SetName("Light guard");
255                 lightGuard.SetCost(128);
256                 lightGuard.GetTargetingMode().TargetAllAllies(); // actually only targets self
257                 lightGuard.SetMagical();
258                 holyShield.SetIkari(&lightGuard);
259                 maxim.SetShield(&holyShield);
260                 Item legendHelm;
261                 legendHelm.SetName("Legend helm");
262                 legendHelm.SetMenuIcon(intp.GetSprite("helmetIcon"));
263                 Ikari boomerang;
264                 boomerang.SetName("Boomerang");
265                 boomerang.SetCost(164);
266                 boomerang.GetTargetingMode().TargetAllAllies(); // actually only targets self
267                 boomerang.SetMagical();
268                 legendHelm.SetIkari(&boomerang);
269                 maxim.SetHelmet(&legendHelm);
270                 Item sProRing;
271                 sProRing.SetName("S-pro ring");
272                 sProRing.SetMenuIcon(intp.GetSprite("ringIcon"));
273                 Ikari courage;
274                 courage.SetName("Courage");
275                 courage.SetCost(64);
276                 courage.GetTargetingMode().TargetMultipleAllies();
277                 courage.SetMagical();
278                 sProRing.SetIkari(&courage);
279                 maxim.SetRing(&sProRing);
280                 Item evilJewel;
281                 evilJewel.SetName("Evil jewel");
282                 evilJewel.SetMenuIcon(intp.GetSprite("jewelIcon"));
283                 Ikari gloomy;
284                 gloomy.SetName("Gloomy");
285                 gloomy.SetCost(164);
286                 gloomy.GetTargetingMode().TargetAllEnemies();
287                 gloomy.SetMagical();
288                 evilJewel.SetIkari(&gloomy);
289                 maxim.SetJewel(&evilJewel);
290
291                 Item zircoWhip;
292                 zircoWhip.SetName("Zirco whip");
293                 zircoWhip.SetMenuIcon(intp.GetSprite("rodIcon"));
294                 zircoWhip.GetTargetingMode().TargetSingleEnemy();
295                 Ikari thundershriek;
296                 thundershriek.SetName("Thundershriek");
297                 thundershriek.SetCost(224);
298                 thundershriek.GetTargetingMode().TargetAllEnemies();
299                 thundershriek.SetPhysical();
300                 zircoWhip.SetIkari(&thundershriek);
301 //              selan.SetWeapon(&zircoWhip);
302                 Item zirconPlate;
303                 zirconPlate.SetName("Zircon plate");
304                 zirconPlate.SetMenuIcon(intp.GetSprite("armorIcon"));
305                 Ikari suddenCure;
306                 suddenCure.SetName("Sudden cure");
307                 suddenCure.SetCost(96);
308                 suddenCure.GetTargetingMode().TargetAllAllies();
309                 suddenCure.SetMagical();
310                 zirconPlate.SetIkari(&suddenCure);
311                 selan.SetArmor(&zirconPlate);
312                 Item zircoGloves;
313                 zircoGloves.SetName("Zirco gloves");
314                 zircoGloves.SetMenuIcon(intp.GetSprite("shieldIcon"));
315                 Ikari forcefield;
316                 forcefield.SetName("Forcefield");
317                 forcefield.SetCost(64);
318                 forcefield.GetTargetingMode().TargetAllAllies();
319                 forcefield.SetMagical();
320                 zircoGloves.SetIkari(&forcefield);
321                 selan.SetShield(&zircoGloves);
322                 Item holyCap;
323                 holyCap.SetName("Holy cap");
324                 holyCap.SetMenuIcon(intp.GetSprite("helmetIcon"));
325                 Ikari vulnerable;
326                 vulnerable.SetName("Vulnerable");
327                 vulnerable.SetCost(196);
328                 vulnerable.GetTargetingMode().TargetAllEnemies();
329                 vulnerable.SetPhysical();
330                 holyCap.SetIkari(&vulnerable);
331                 selan.SetHelmet(&holyCap);
332                 Item ghostRing;
333                 ghostRing.SetName("Ghost ring");
334                 ghostRing.SetMenuIcon(intp.GetSprite("ringIcon"));
335                 Ikari destroy;
336                 destroy.SetName("Destroy");
337                 destroy.SetCost(128);
338                 destroy.GetTargetingMode().TargetMultipleEnemies();
339                 destroy.SetMagical();
340                 ghostRing.SetIkari(&destroy);
341                 selan.SetRing(&ghostRing);
342                 Item eagleRock;
343                 eagleRock.SetName("Eagle rock");
344                 eagleRock.SetMenuIcon(intp.GetSprite("jewelIcon"));
345                 Ikari dive;
346                 dive.SetName("Dive");
347                 dive.SetCost(128);
348                 dive.GetTargetingMode().TargetSingleEnemy();
349                 dive.SetPhysical();
350                 eagleRock.SetIkari(&dive);
351                 selan.SetJewel(&eagleRock);
352
353                 Item zircoAx;
354                 zircoAx.SetName("Zirco ax");
355                 zircoAx.SetMenuIcon(intp.GetSprite("axIcon"));
356                 zircoAx.GetTargetingMode().TargetSingleEnemy();
357                 Ikari torrent;
358                 torrent.SetName("Torrent");
359                 torrent.SetCost(224);
360                 torrent.GetTargetingMode().TargetAllEnemies();
361                 torrent.SetPhysical();
362                 zircoAx.SetIkari(&torrent);
363 //              guy.SetWeapon(&zircoAx);
364                 guy.SetArmor(&zirconArmor);
365                 Item megaShield;
366                 megaShield.SetName("Mega shield");
367                 megaShield.SetMenuIcon(intp.GetSprite("shieldIcon"));
368                 Ikari ironBarrier;
369                 ironBarrier.SetName("Iron barrier");
370                 ironBarrier.SetCost(255);
371                 ironBarrier.GetTargetingMode().TargetAllAllies(); // actually only targets self
372                 ironBarrier.SetMagical();
373                 megaShield.SetIkari(&ironBarrier);
374                 guy.SetShield(&megaShield);
375                 Item zircoHelmet;
376                 zircoHelmet.SetName("Zirco helmet");
377                 zircoHelmet.SetMenuIcon(intp.GetSprite("helmetIcon"));
378                 Ikari slow;
379                 slow.SetName("Slow");
380                 slow.SetCost(196);
381                 slow.GetTargetingMode().TargetAllEnemies();
382                 slow.SetPhysical();
383                 zircoHelmet.SetIkari(&slow);
384                 guy.SetHelmet(&zircoHelmet);
385                 Item powerRing;
386                 powerRing.SetName("Power ring");
387                 powerRing.SetMenuIcon(intp.GetSprite("ringIcon"));
388                 Ikari trick;
389                 trick.SetName("Trick");
390                 trick.SetCost(32);
391                 trick.GetTargetingMode().TargetAllEnemies();
392                 trick.SetMagical();
393                 zircoHelmet.SetIkari(&trick);
394                 guy.SetRing(&powerRing);
395                 guy.SetJewel(&evilJewel);
396
397                 // NOTE: this is actually Artea equipment
398                 Item lizardBlow;
399                 lizardBlow.SetName("Lizard blow");
400                 lizardBlow.SetMenuIcon(intp.GetSprite("swordIcon"));
401                 lizardBlow.GetTargetingMode().TargetSingleEnemy();
402                 Ikari dragonRush;
403                 dragonRush.SetName("Dragon rush");
404                 dragonRush.SetCost(164);
405                 dragonRush.GetTargetingMode().TargetSingleEnemy();
406                 dragonRush.SetPhysical();
407                 lizardBlow.SetIkari(&dragonRush);
408 //              dekar.SetWeapon(&lizardBlow);
409                 Item holyRobe;
410                 holyRobe.SetName("Holy robe");
411                 holyRobe.SetMenuIcon(intp.GetSprite("armorIcon"));
412                 Ikari crisisCure;
413                 crisisCure.SetName("Crisis cure");
414                 crisisCure.SetCost(164);
415                 crisisCure.GetTargetingMode().TargetAllAllies();
416                 crisisCure.SetMagical();
417                 holyRobe.SetIkari(&crisisCure);
418                 dekar.SetArmor(&holyRobe);
419                 dekar.SetShield(&zircoGloves);
420                 dekar.SetHelmet(&holyCap);
421                 Item rocketRing;
422                 rocketRing.SetName("Rocket ring");
423                 rocketRing.SetMenuIcon(intp.GetSprite("ringIcon"));
424                 Ikari fake;
425                 fake.SetName("Fake");
426                 fake.SetCost(32);
427                 fake.GetTargetingMode().TargetSingleAlly();
428                 fake.SetMagical();
429                 rocketRing.SetIkari(&fake);
430                 dekar.SetRing(&rocketRing);
431                 Item krakenRock;
432                 krakenRock.SetName("Kraken rock");
433                 krakenRock.SetMenuIcon(intp.GetSprite("jewelIcon"));
434                 Ikari tenLegger;
435                 tenLegger.SetName("Ten-legger");
436                 tenLegger.SetCost(164);
437                 tenLegger.GetTargetingMode().TargetAllEnemies();
438                 tenLegger.SetPhysical();
439                 rocketRing.SetIkari(&tenLegger);
440                 dekar.SetJewel(&krakenRock);
441
442                 battleRes.ikariMenuHeadline = "Please choose equipment.";
443                 battleRes.noEquipmentText = "No equip";
444                 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());
445
446                 battleRes.escapeText = "Escapes.";
447
448                 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &battleRes));
449                 battleState->AddMonster(monster);
450                 battleState->AddMonster(monster);
451                 battleState->AddMonster(monster);
452                 battleState->AddMonster(monster);
453                 battleState->AddHero(maxim);
454                 battleState->AddHero(selan);
455                 battleState->AddHero(guy);
456                 battleState->AddHero(dekar);
457                 Application app(&screen, battleState);
458                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
459                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
460                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
461                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
462                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
463                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
464                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
465                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
466                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
467                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
468                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
469                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
470                 app.Run();
471
472                 return 0;
473         } catch (Parser::Error &e) {
474                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
475                 return 1;
476         } catch (exception &e) {
477                 cerr << "exception in main(): " << e.what() << endl;
478                 return 1;
479         }
480 }