]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
moved menu resources to data files
[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/Hero.h"
6 #include "battle/Monster.h"
7 #include "battle/PartyLayout.h"
8 #include "battle/Resources.h"
9 #include "common/GameConfig.h"
10 #include "common/GameState.h"
11 #include "common/Hero.h"
12 #include "common/Ikari.h"
13 #include "common/Inventory.h"
14 #include "common/Item.h"
15 #include "common/Script.h"
16 #include "common/Spell.h"
17 #include "common/Stats.h"
18 #include "geometry/Vector.h"
19 #include "graphics/ComplexAnimation.h"
20 #include "graphics/Font.h"
21 #include "graphics/Frame.h"
22 #include "graphics/Gauge.h"
23 #include "graphics/Menu.h"
24 #include "graphics/SimpleAnimation.h"
25 #include "graphics/Sprite.h"
26 #include "graphics/Texture.h"
27 #include "loader/Caster.h"
28 #include "loader/Interpreter.h"
29 #include "loader/ParsedSource.h"
30 #include "loader/Parser.h"
31 #include "loader/TypeDescription.h"
32 #include "map/Area.h"
33 #include "map/Entity.h"
34 #include "map/Map.h"
35 #include "map/MapState.h"
36 #include "map/Tile.h"
37 #include "map/Trigger.h"
38 #include "menu/Resources.h"
39 #include "sdl/InitImage.h"
40 #include "sdl/InitScreen.h"
41 #include "sdl/InitSDL.h"
42
43 #include <cstdlib>
44 #include <cstring>
45 #include <ctime>
46 #include <exception>
47 #include <iostream>
48 #include <string>
49 #include <SDL.h>
50 #include <SDL_image.h>
51
52 using app::Application;
53 using app::Arguments;
54 using app::Input;
55 using battle::BattleState;
56 using battle::Monster;
57 using battle::PartyLayout;
58 using common::GameConfig;
59 using common::GameState;
60 using common::Hero;
61 using common::Spell;
62 using geometry::Vector;
63 using graphics::Texture;
64 using loader::Caster;
65 using loader::Interpreter;
66 using loader::ParsedSource;
67 using loader::Parser;
68 using loader::TypeDescription;
69 using map::Entity;
70 using map::MapState;
71 using sdl::InitImage;
72 using sdl::InitScreen;
73 using sdl::InitSDL;
74
75 using std::cerr;
76 using std::cout;
77 using std::endl;
78 using std::exception;
79 using std::string;
80 using std::vector;
81
82 int main(int argc, char **argv) {
83         const int width = 512;
84         const int height = 448;
85
86         const float walkSpeed = 128.0f;
87
88         bool battle(false);
89
90 //      std::srand(std::time(0));
91
92         try {
93                 InitSDL sdl;
94                 InitImage image(IMG_INIT_PNG);
95
96                 Interpreter::CreateTypeDescriptions();
97
98                 battle::Resources::CreateTypeDescription();
99                 battle::Monster::CreateTypeDescription();
100                 battle::PartyLayout::CreateTypeDescription();
101
102                 common::Hero::CreateTypeDescription();
103                 common::Ikari::CreateTypeDescription();
104                 common::Item::CreateTypeDescription();
105                 common::Stats::CreateTypeDescription();
106                 common::Spell::CreateTypeDescription();
107                 common::TargetingMode::CreateTypeDescription();
108
109                 graphics::Animation::CreateTypeDescription();
110                 graphics::ComplexAnimation::CreateTypeDescription();
111                 graphics::Font::CreateTypeDescription();
112                 graphics::Frame::CreateTypeDescription();
113                 graphics::Gauge::CreateTypeDescription();
114                 graphics::MenuProperties::CreateTypeDescription();
115                 graphics::SimpleAnimation::CreateTypeDescription();
116                 graphics::Sprite::CreateTypeDescription();
117                 graphics::Texture::CreateTypeDescription();
118
119                 map::Area::CreateTypeDescription();
120                 map::Entity::CreateTypeDescription();
121                 map::Map::CreateTypeDescription();
122                 map::Tile::CreateTypeDescription();
123                 map::Trigger::CreateTypeDescription();
124
125                 menu::Resources::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                 gameState.partySize = 4;
192
193                 GameConfig gameConfig;
194                 gameConfig.state = &gameState;
195                 gameConfig.heroesLayout = caster.GetPartyLayout("heroesLayout");
196                 gameConfig.battleResources = caster.GetBattleResources("battleResources");
197                 gameConfig.menuResources = caster.GetMenuResources("menuResources");
198
199                 // temporary test data
200                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
201                 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
202
203                 Monster monster(*caster.GetMonster("lizard"));
204
205                 gameState.heroes[0].AddSpell(caster.GetSpell("resetSpell"));
206                 Spell *strongSpell(caster.GetSpell("strongSpell"));
207                 gameState.heroes[0].AddSpell(strongSpell);
208                 gameState.heroes[1].AddSpell(strongSpell);
209                 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
210                 gameState.heroes[0].AddSpell(strongerSpell);
211                 gameState.heroes[1].AddSpell(strongerSpell);
212                 Spell *championSpell(caster.GetSpell("championSpell"));
213                 gameState.heroes[0].AddSpell(championSpell);
214                 gameState.heroes[1].AddSpell(championSpell);
215                 Spell *rallySpell(caster.GetSpell("rallySpell"));
216                 gameState.heroes[0].AddSpell(rallySpell);
217                 gameState.heroes[1].AddSpell(rallySpell);
218                 gameState.heroes[1].AddSpell(caster.GetSpell("escapeSpell"));
219                 Spell *valorSpell(caster.GetSpell("valorSpell"));
220                 gameState.heroes[0].AddSpell(valorSpell);
221                 gameState.heroes[1].AddSpell(valorSpell);
222
223                 gameState.inventory.Add(caster.GetItem("zirconPlateItem"));
224                 gameState.inventory.Add(caster.GetItem("antidoteItem"), 9);
225                 gameState.inventory.Add(caster.GetItem("powerRingItem"));
226                 gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
227                 gameState.inventory.Add(caster.GetItem("sProRingItem"));
228                 gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
229                 gameState.inventory.Add(caster.GetItem("powerRingItem"));
230                 gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
231                 gameState.inventory.Add(caster.GetItem("zircoSwordItem"));
232                 gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
233                 gameState.inventory.Add(caster.GetItem("zircoHelmetItem"));
234                 gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
235                 gameState.inventory.Add(caster.GetItem("zirconPlateItem"));
236
237                 gameState.heroes[0].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoSwordItem"));
238                 gameState.heroes[0].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconArmorItem"));
239                 gameState.heroes[0].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("holyShieldItem"));
240                 gameState.heroes[0].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("legendHelmItem"));
241                 gameState.heroes[0].SetEquipment(Hero::EQUIP_RING, caster.GetItem("sProRingItem"));
242                 gameState.heroes[0].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("evilJewelItem"));
243
244 //              gameState.heroes[1].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoWhipItem"));
245                 gameState.heroes[1].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconPlateItem"));
246                 gameState.heroes[1].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("zircoGlovesItem"));
247                 gameState.heroes[1].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("holyCapItem"));
248                 gameState.heroes[1].SetEquipment(Hero::EQUIP_RING, caster.GetItem("ghostRingItem"));
249                 gameState.heroes[1].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("eagleRockItem"));
250
251 //              gameState.heroes[2].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoAxItem"));
252                 gameState.heroes[2].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconArmorItem"));
253                 gameState.heroes[2].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("megaShieldItem"));
254                 gameState.heroes[2].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("zircoHelmetItem"));
255                 gameState.heroes[2].SetEquipment(Hero::EQUIP_RING, caster.GetItem("powerRingItem"));
256                 gameState.heroes[2].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("evilJewelItem"));
257
258                 // NOTE: this is actually Artea equipment
259 //              gameState.heroes[3].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("lizardBlowItem"));
260                 gameState.heroes[3].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("holyRobeItem"));
261                 gameState.heroes[3].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("zircoGlovesItem"));
262                 gameState.heroes[3].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("holyCapItem"));
263                 gameState.heroes[3].SetEquipment(Hero::EQUIP_RING, caster.GetItem("rocketRingItem"));
264                 gameState.heroes[3].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("krakenRockItem"));
265
266                 gameState.heroes[0].MapEntity().Position() = Vector<float>(64, 128);
267
268                 gameState.heroes[1].MapEntity().Position() = Vector<float>(64, 128);
269                 gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
270                 gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity());
271
272                 gameState.heroes[2].MapEntity().Position() = Vector<float>(64, 128);
273                 gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
274                 gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity());
275
276                 gameState.heroes[3].MapEntity().Position() = Vector<float>(64, 128);
277                 gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
278                 gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity());
279
280                 InitScreen screen(width, height);
281
282                 app::State *state(0);
283
284                 if (battle) {
285                         BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
286                         battleState->AddMonster(monster);
287                         battleState->AddMonster(monster);
288                         battleState->AddMonster(monster);
289                         battleState->AddMonster(monster);
290                         battleState->AddHero(gameState.heroes[0]);
291                         battleState->AddHero(gameState.heroes[1]);
292                         battleState->AddHero(gameState.heroes[2]);
293                         battleState->AddHero(gameState.heroes[3]);
294                         state = battleState;
295                 } else {
296                         MapState *mapState(new MapState(&gameConfig, caster.GetMap("map1")));
297
298                         mapState->ControlEntity(&gameState.heroes[0].MapEntity());
299                         mapState->SetWalkingSpeed(walkSpeed);
300
301                         state = mapState;
302                 }
303
304                 Application app(screen, state);
305                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
306                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
307                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
308                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
309                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
310                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
311                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
312                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
313                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
314                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
315                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
316                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
317                 app.Buttons().MapKey(SDLK_1, Input::DEBUG_1);
318                 app.Buttons().MapKey(SDLK_2, Input::DEBUG_2);
319                 app.Buttons().MapKey(SDLK_3, Input::DEBUG_3);
320                 app.Buttons().MapKey(SDLK_4, Input::DEBUG_4);
321                 app.Run();
322
323                 return 0;
324         } catch (Parser::Error &e) {
325                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
326                 return 2;
327         } catch (exception &e) {
328                 cerr << "exception in main(): " << e.what() << endl;
329                 return 1;
330         }
331 }