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