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