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