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