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