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