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