]> git.localhorst.tv Git - l2e.git/blob - main.cpp
4c350de35f3e48432c40ef98cdb54c4826612745
[l2e.git] / 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 "common/GameConfig.h"
17 #include "common/GameState.h"
18 #include "common/Hero.h"
19 #include "common/Ikari.h"
20 #include "common/Inventory.h"
21 #include "common/Item.h"
22 #include "common/Script.h"
23 #include "common/Spell.h"
24 #include "common/Stats.h"
25 #include "geometry/Vector.h"
26 #include "graphics/ComplexAnimation.h"
27 #include "graphics/Font.h"
28 #include "graphics/Frame.h"
29 #include "graphics/Gauge.h"
30 #include "graphics/Menu.h"
31 #include "graphics/SimpleAnimation.h"
32 #include "graphics/Sprite.h"
33 #include "graphics/Texture.h"
34 #include "loader/Caster.h"
35 #include "loader/Interpreter.h"
36 #include "loader/ParsedSource.h"
37 #include "loader/Parser.h"
38 #include "loader/TypeDescription.h"
39 #include "map/Area.h"
40 #include "map/Entity.h"
41 #include "map/Map.h"
42 #include "map/MapState.h"
43 #include "map/Tile.h"
44 #include "map/Trigger.h"
45 #include "menu/Resources.h"
46 #include "sdl/InitImage.h"
47 #include "sdl/InitScreen.h"
48 #include "sdl/InitSDL.h"
49
50 #include <cstdlib>
51 #include <cstring>
52 #include <ctime>
53 #include <exception>
54 #include <iostream>
55 #include <string>
56 #include <SDL.h>
57 #include <SDL_image.h>
58
59 using app::Application;
60 using app::Arguments;
61 using app::Input;
62 using battle::BattleState;
63 using battle::Monster;
64 using battle::PartyLayout;
65 using common::GameConfig;
66 using common::GameState;
67 using common::Spell;
68 using geometry::Vector;
69 using graphics::Texture;
70 using loader::Caster;
71 using loader::Interpreter;
72 using loader::ParsedSource;
73 using loader::Parser;
74 using loader::TypeDescription;
75 using map::Entity;
76 using map::MapState;
77 using sdl::InitImage;
78 using sdl::InitScreen;
79 using sdl::InitSDL;
80
81 using std::cerr;
82 using std::cout;
83 using std::endl;
84 using std::exception;
85 using std::string;
86 using std::vector;
87
88 int main(int argc, char **argv) {
89         const int width = 512;
90         const int height = 448;
91
92         const float walkSpeed = 128.0f;
93
94         bool battle(false);
95
96 //      std::srand(std::time(0));
97
98         try {
99                 InitSDL sdl;
100                 InitImage image(IMG_INIT_PNG);
101
102                 Interpreter::CreateTypeDescriptions();
103
104                 battle::Resources::CreateTypeDescription();
105                 battle::Monster::CreateTypeDescription();
106                 battle::PartyLayout::CreateTypeDescription();
107
108                 common::Hero::CreateTypeDescription();
109                 common::Ikari::CreateTypeDescription();
110                 common::Item::CreateTypeDescription();
111                 common::Stats::CreateTypeDescription();
112                 common::Spell::CreateTypeDescription();
113                 common::TargetingMode::CreateTypeDescription();
114
115                 graphics::Animation::CreateTypeDescription();
116                 graphics::ComplexAnimation::CreateTypeDescription();
117                 graphics::Font::CreateTypeDescription();
118                 graphics::Frame::CreateTypeDescription();
119                 graphics::Gauge::CreateTypeDescription();
120                 graphics::MenuProperties::CreateTypeDescription();
121                 graphics::SimpleAnimation::CreateTypeDescription();
122                 graphics::Sprite::CreateTypeDescription();
123
124                 map::Area::CreateTypeDescription();
125                 map::Entity::CreateTypeDescription();
126                 map::Map::CreateTypeDescription();
127                 map::Tile::CreateTypeDescription();
128                 map::Trigger::CreateTypeDescription();
129
130                 Arguments args;
131                 args.Read(argc, argv);
132
133                 ParsedSource source;
134
135                 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
136                         Parser(*i, source).Parse();
137                 }
138
139                 switch (args.GetRunLevel()) {
140                         case Arguments::WRITE:
141                         {
142                                 int length(std::strlen(args.OutfilePath()));
143                                 switch (args.OutfilePath()[length - 1]) {
144                                         case 'h': {
145                                                 std::ofstream outstream(args.OutfilePath());
146                                                 source.WriteHeader(outstream);
147                                                 break;
148                                         }
149                                         default: {
150                                                 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
151                                         }
152                                 }
153                                 return 0;
154                         }
155                         case Arguments::DUMP: {
156                                 std::cout << source << std::endl;
157                                 return 0;
158                         }
159                         case Arguments::SOURCE_WIKI: {
160                                 TypeDescription::WriteSourceWiki(std::cout);
161                                 return 0;
162                         }
163                         case Arguments::BATTLE:
164                                 battle = true;
165                                 break;
166                         case Arguments::PLAY:
167                         case Arguments::MAP:
168                                 break;
169                 }
170
171                 Interpreter intp(source);
172                 intp.ReadSource();
173
174                 if (intp.PostponedDefinitions().size() > 0) {
175                         for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
176                                 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
177                         }
178                         return 3;
179                 }
180
181                 Caster caster(intp);
182
183                 GameState gameState;
184
185                 gameState.heroes[0] = *caster.GetHero("maxim");
186                 gameState.heroes[1] = *caster.GetHero("selan");
187                 gameState.heroes[2] = *caster.GetHero("guy");
188                 gameState.heroes[3] = *caster.GetHero("dekar");
189
190                 gameState.party[0] = &gameState.heroes[0];
191                 gameState.party[1] = &gameState.heroes[1];
192                 gameState.party[2] = &gameState.heroes[2];
193                 gameState.party[3] = &gameState.heroes[3];
194                 gameState.partySize = 4;
195
196                 GameConfig gameConfig;
197                 gameConfig.state = &gameState;
198                 gameConfig.heroesLayout = caster.GetPartyLayout("heroesLayout");
199                 gameConfig.battleResources = caster.GetBattleResources("battleResources");
200
201                 // temporary test data
202                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
203                 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
204
205                 Monster monster(*caster.GetMonster("lizard"));
206
207                 gameState.heroes[0].AddSpell(caster.GetSpell("resetSpell"));
208                 Spell *strongSpell(caster.GetSpell("strongSpell"));
209                 gameState.heroes[0].AddSpell(strongSpell);
210                 gameState.heroes[1].AddSpell(strongSpell);
211                 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
212                 gameState.heroes[0].AddSpell(strongerSpell);
213                 gameState.heroes[1].AddSpell(strongerSpell);
214                 Spell *championSpell(caster.GetSpell("championSpell"));
215                 gameState.heroes[0].AddSpell(championSpell);
216                 gameState.heroes[1].AddSpell(championSpell);
217                 Spell *rallySpell(caster.GetSpell("rallySpell"));
218                 gameState.heroes[0].AddSpell(rallySpell);
219                 gameState.heroes[1].AddSpell(rallySpell);
220                 gameState.heroes[1].AddSpell(caster.GetSpell("escapeSpell"));
221                 Spell *valorSpell(caster.GetSpell("valorSpell"));
222                 gameState.heroes[0].AddSpell(valorSpell);
223                 gameState.heroes[1].AddSpell(valorSpell);
224
225                 gameState.inventory.Add(caster.GetItem("antidoteItem"), 9);
226                 gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
227                 gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
228                 gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
229                 gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
230                 gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
231
232                 gameState.heroes[0].SetWeapon(caster.GetItem("zircoSwordItem"));
233                 gameState.heroes[0].SetArmor(caster.GetItem("zirconArmorItem"));
234                 gameState.heroes[0].SetShield(caster.GetItem("holyShieldItem"));
235                 gameState.heroes[0].SetHelmet(caster.GetItem("legendHelmItem"));
236                 gameState.heroes[0].SetRing(caster.GetItem("sProRingItem"));
237                 gameState.heroes[0].SetJewel(caster.GetItem("evilJewelItem"));
238
239 //              gameState.heroes[1].SetWeapon(cst.GetItem("zircoWhipItem"));
240                 gameState.heroes[1].SetArmor(caster.GetItem("zirconPlateItem"));
241                 gameState.heroes[1].SetShield(caster.GetItem("zircoGlovesItem"));
242                 gameState.heroes[1].SetHelmet(caster.GetItem("holyCapItem"));
243                 gameState.heroes[1].SetRing(caster.GetItem("ghostRingItem"));
244                 gameState.heroes[1].SetJewel(caster.GetItem("eagleRockItem"));
245
246 //              gameState.heroes[2].SetWeapon(cst.GetItem("zircoAxItem"));
247                 gameState.heroes[2].SetArmor(caster.GetItem("zirconArmorItem"));
248                 gameState.heroes[2].SetShield(caster.GetItem("megaShieldItem"));
249                 gameState.heroes[2].SetHelmet(caster.GetItem("zircoHelmetItem"));
250                 gameState.heroes[2].SetRing(caster.GetItem("powerRingItem"));
251                 gameState.heroes[2].SetJewel(caster.GetItem("evilJewelItem"));
252
253                 // NOTE: this is actually Artea equipment
254 //              gameState.heroes[3].SetWeapon(cst.GetItem("lizardBlowItem"));
255                 gameState.heroes[3].SetArmor(caster.GetItem("holyRobeItem"));
256                 gameState.heroes[3].SetShield(caster.GetItem("zircoGlovesItem"));
257                 gameState.heroes[3].SetHelmet(caster.GetItem("holyCapItem"));
258                 gameState.heroes[3].SetRing(caster.GetItem("rocketRingItem"));
259                 gameState.heroes[3].SetJewel(caster.GetItem("krakenRockItem"));
260
261                 gameState.heroes[0].MapEntity().Position() = Vector<float>(64, 128);
262
263                 gameState.heroes[1].MapEntity().Position() = Vector<float>(64, 128);
264                 gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
265                 gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity());
266
267                 gameState.heroes[2].MapEntity().Position() = Vector<float>(64, 128);
268                 gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
269                 gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity());
270
271                 gameState.heroes[3].MapEntity().Position() = Vector<float>(64, 128);
272                 gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
273                 gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity());
274
275                 menu::Resources menuResources;
276                 gameConfig.menuResources = &menuResources;
277
278                 Texture menubg;
279                 menubg.SetSurface(IMG_Load("test-data/menubg.png"));
280                 menubg.SetSize(Vector<int>(64, 64));
281                 menuResources.menubg = &menubg;
282
283                 menuResources.statusFont = gameConfig.battleResources->normalFont;
284
285                 graphics::Sprite statusLabels(IMG_Load("test-data/status-labels.png"), 32, 16);
286                 menuResources.statusLabels = &statusLabels;
287
288                 graphics::Frame statusFrame(IMG_Load("test-data/status-frame.png"), 32, 32, 32, 32);
289                 menuResources.statusFrame = &statusFrame;
290
291                 graphics::Sprite menuFontSprite(IMG_Load("test-data/menu-font.png"), 16, 16);
292                 graphics::Font menuFont(&menuFontSprite, 0, -2);
293
294                 menuResources.normalFont = &menuFont;
295
296                 graphics::Sprite menuCursor(IMG_Load("test-data/menu-cursor.png"), 32, 16);
297
298                 graphics::MenuProperties mainMenuProperties;
299                 mainMenuProperties.cols = 2;
300                 mainMenuProperties.rows = 4;
301                 mainMenuProperties.charsPerEntry = 8;
302                 mainMenuProperties.rowGap = 8;
303                 mainMenuProperties.colGap = 32;
304                 mainMenuProperties.cursor = &menuCursor;
305                 mainMenuProperties.font = &menuFont;
306                 mainMenuProperties.wrapX = true;
307                 mainMenuProperties.wrapY = true;
308                 menuResources.mainMenuProperties = &mainMenuProperties;
309
310                 menuResources.mainMenuItemText = "ITEM";
311                 menuResources.mainMenuSpellText = "SPELL";
312                 menuResources.mainMenuCapsuleText = "CAPSULE";
313                 menuResources.mainMenuEquipmentText = "EQUIP";
314                 menuResources.mainMenuStatusText = "STATUS";
315                 menuResources.mainMenuChangeText = "CHANGE";
316                 menuResources.mainMenuConfigText = "CONFIG";
317                 menuResources.mainMenuScenarioText = "SCENARIO";
318
319                 menuResources.mainMenuTimeText = "TIME";
320                 menuResources.mainMenuGoldText = "GOLD";
321
322                 graphics::Sprite heroCursor(IMG_Load("test-data/hero-cursor.png"), 64, 16);
323                 menuResources.heroCursor = &heroCursor;
324                 menuResources.heroCursorBlinkTime = 532;
325
326                 menuResources.noEquipmentText = "No equip";
327
328                 graphics::Sprite shoulderNav(IMG_Load("test-data/shoulder-nav.png"), 160, 16);
329                 menuResources.shoulderNav = &shoulderNav;
330
331                 menuResources.atpLabel = "ATP";
332                 menuResources.dfpLabel = "DFP";
333                 menuResources.strLabel = "STR";
334                 menuResources.aglLabel = "AGL";
335                 menuResources.intLabel = "INT";
336                 menuResources.gutLabel = "GUT";
337                 menuResources.mgrLabel = "MGR";
338
339                 menuResources.ipLabel = "IP";
340                 menuResources.experienceLabel = "NOW EXP";
341                 menuResources.nextLevelLabel = "NEXT LEVEL";
342
343                 graphics::MenuProperties statusMenuProperties;
344                 statusMenuProperties.cols = 2;
345                 statusMenuProperties.rows = 1;
346                 statusMenuProperties.charsPerEntry = 6;
347                 statusMenuProperties.rowGap = 0;
348                 statusMenuProperties.colGap = 16;
349                 statusMenuProperties.cursor = &menuCursor;
350                 statusMenuProperties.font = &menuFont;
351                 statusMenuProperties.wrapX = true;
352                 menuResources.statusMenuProperties = &statusMenuProperties;
353
354                 menuResources.nextLabel = "NEXT";
355                 menuResources.returnLabel = "RETURN";
356
357                 InitScreen screen(width, height);
358
359                 app::State *state(0);
360
361                 if (battle) {
362                         BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
363                         battleState->AddMonster(monster);
364                         battleState->AddMonster(monster);
365                         battleState->AddMonster(monster);
366                         battleState->AddMonster(monster);
367                         battleState->AddHero(gameState.heroes[0]);
368                         battleState->AddHero(gameState.heroes[1]);
369                         battleState->AddHero(gameState.heroes[2]);
370                         battleState->AddHero(gameState.heroes[3]);
371                         state = battleState;
372                 } else {
373                         MapState *mapState(new MapState(&gameConfig, caster.GetMap("map1")));
374
375                         mapState->ControlEntity(&gameState.heroes[0].MapEntity());
376                         mapState->SetWalkingSpeed(walkSpeed);
377
378                         state = mapState;
379                 }
380
381                 Application app(screen, state);
382                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
383                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
384                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
385                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
386                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
387                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
388                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
389                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
390                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
391                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
392                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
393                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
394                 app.Buttons().MapKey(SDLK_1, Input::DEBUG_1);
395                 app.Buttons().MapKey(SDLK_2, Input::DEBUG_2);
396                 app.Buttons().MapKey(SDLK_3, Input::DEBUG_3);
397                 app.Buttons().MapKey(SDLK_4, Input::DEBUG_4);
398                 app.Run();
399
400                 return 0;
401         } catch (Parser::Error &e) {
402                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
403                 return 2;
404         } catch (exception &e) {
405                 cerr << "exception in main(): " << e.what() << endl;
406                 return 1;
407         }
408 }