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