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