]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
added capsule wheel and table
[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.capsule = &gameState.capsules[1];
202
203                 GameConfig gameConfig;
204                 gameConfig.state = &gameState;
205                 gameConfig.heroesLayout = caster.GetPartyLayout("heroesLayout");
206                 gameConfig.battleResources = caster.GetBattleResources("battleResources");
207                 gameConfig.menuResources = caster.GetMenuResources("menuResources");
208
209                 // temporary test data
210                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
211                 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
212
213                 Monster monster(*caster.GetMonster("lizard"));
214
215                 gameState.heroes[0].AddSpell(caster.GetSpell("resetSpell"));
216                 Spell *strongSpell(caster.GetSpell("strongSpell"));
217                 gameState.heroes[0].AddSpell(strongSpell);
218                 gameState.heroes[1].AddSpell(strongSpell);
219                 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
220                 gameState.heroes[0].AddSpell(strongerSpell);
221                 gameState.heroes[1].AddSpell(strongerSpell);
222                 Spell *championSpell(caster.GetSpell("championSpell"));
223                 gameState.heroes[0].AddSpell(championSpell);
224                 gameState.heroes[1].AddSpell(championSpell);
225                 Spell *rallySpell(caster.GetSpell("rallySpell"));
226                 gameState.heroes[0].AddSpell(rallySpell);
227                 gameState.heroes[1].AddSpell(rallySpell);
228                 gameState.heroes[1].AddSpell(caster.GetSpell("escapeSpell"));
229                 Spell *valorSpell(caster.GetSpell("valorSpell"));
230                 gameState.heroes[0].AddSpell(valorSpell);
231                 gameState.heroes[1].AddSpell(valorSpell);
232
233                 gameState.inventory.Add(caster.GetItem("zirconPlateItem"));
234                 gameState.inventory.Add(caster.GetItem("antidoteItem"), 9);
235                 gameState.inventory.Add(caster.GetItem("powerRingItem"));
236                 gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
237                 gameState.inventory.Add(caster.GetItem("sProRingItem"));
238                 gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
239                 gameState.inventory.Add(caster.GetItem("powerRingItem"));
240                 gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
241                 gameState.inventory.Add(caster.GetItem("zircoSwordItem"));
242                 gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
243                 gameState.inventory.Add(caster.GetItem("zircoHelmetItem"));
244                 gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
245                 gameState.inventory.Add(caster.GetItem("zirconPlateItem"));
246
247                 gameState.heroes[0].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoSwordItem"));
248                 gameState.heroes[0].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconArmorItem"));
249                 gameState.heroes[0].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("holyShieldItem"));
250                 gameState.heroes[0].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("legendHelmItem"));
251                 gameState.heroes[0].SetEquipment(Hero::EQUIP_RING, caster.GetItem("sProRingItem"));
252                 gameState.heroes[0].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("evilJewelItem"));
253
254 //              gameState.heroes[1].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoWhipItem"));
255                 gameState.heroes[1].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconPlateItem"));
256                 gameState.heroes[1].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("zircoGlovesItem"));
257                 gameState.heroes[1].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("holyCapItem"));
258                 gameState.heroes[1].SetEquipment(Hero::EQUIP_RING, caster.GetItem("ghostRingItem"));
259                 gameState.heroes[1].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("eagleRockItem"));
260
261 //              gameState.heroes[2].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoAxItem"));
262                 gameState.heroes[2].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconArmorItem"));
263                 gameState.heroes[2].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("megaShieldItem"));
264                 gameState.heroes[2].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("zircoHelmetItem"));
265                 gameState.heroes[2].SetEquipment(Hero::EQUIP_RING, caster.GetItem("powerRingItem"));
266                 gameState.heroes[2].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("evilJewelItem"));
267
268                 // NOTE: this is actually Artea equipment
269 //              gameState.heroes[3].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("lizardBlowItem"));
270                 gameState.heroes[3].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("holyRobeItem"));
271                 gameState.heroes[3].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("zircoGlovesItem"));
272                 gameState.heroes[3].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("holyCapItem"));
273                 gameState.heroes[3].SetEquipment(Hero::EQUIP_RING, caster.GetItem("rocketRingItem"));
274                 gameState.heroes[3].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("krakenRockItem"));
275
276                 gameState.heroes[0].MapEntity().Position() = Vector<float>(64, 128);
277
278                 gameState.heroes[1].MapEntity().Position() = Vector<float>(64, 128);
279                 gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
280                 gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity());
281
282                 gameState.heroes[2].MapEntity().Position() = Vector<float>(64, 128);
283                 gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
284                 gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity());
285
286                 gameState.heroes[3].MapEntity().Position() = Vector<float>(64, 128);
287                 gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
288                 gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity());
289
290                 InitScreen screen(width, height);
291
292                 app::State *state(0);
293
294                 if (battle) {
295                         BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
296                         battleState->AddMonster(monster);
297                         battleState->AddMonster(monster);
298                         battleState->AddMonster(monster);
299                         battleState->AddMonster(monster);
300                         battleState->SetCapsule(caster.GetCapsule("flash"));
301                         battleState->AddHero(gameState.heroes[0]);
302                         battleState->AddHero(gameState.heroes[1]);
303                         battleState->AddHero(gameState.heroes[2]);
304                         battleState->AddHero(gameState.heroes[3]);
305                         state = battleState;
306                 } else {
307                         MapState *mapState(new MapState(&gameConfig, caster.GetMap("map1")));
308
309                         mapState->ControlEntity(&gameState.heroes[0].MapEntity());
310                         mapState->SetWalkingSpeed(walkSpeed);
311
312                         state = mapState;
313                 }
314
315                 Application app(screen, state);
316                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
317                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
318                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
319                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
320                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
321                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
322                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
323                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
324                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
325                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
326                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
327                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
328                 app.Buttons().MapKey(SDLK_1, Input::DEBUG_1);
329                 app.Buttons().MapKey(SDLK_2, Input::DEBUG_2);
330                 app.Buttons().MapKey(SDLK_3, Input::DEBUG_3);
331                 app.Buttons().MapKey(SDLK_4, Input::DEBUG_4);
332                 app.Run();
333
334                 return 0;
335         } catch (Parser::Error &e) {
336                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
337                 return 2;
338         } catch (exception &e) {
339                 cerr << "exception in main(): " << e.what() << endl;
340                 return 1;
341         }
342 }