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