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