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