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