]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
new language, new compiler
[l2e.git] / src / main.cpp
1 #include "keys.h"
2 #include "app/Application.h"
3 #include "app/Arguments.h"
4 #include "app/Input.h"
5 #include "battle/BattleState.h"
6 #include "battle/Capsule.h"
7 #include "battle/Hero.h"
8 #include "battle/Monster.h"
9 #include "battle/PartyLayout.h"
10 #include "battle/Resources.h"
11 #include "common/Capsule.h"
12 #include "common/GameConfig.h"
13 #include "common/GameState.h"
14 #include "common/Hero.h"
15 #include "common/Ikari.h"
16 #include "common/Inventory.h"
17 #include "common/Item.h"
18 #include "common/LevelUp.h"
19 #include "common/Script.h"
20 #include "common/Spell.h"
21 #include "common/Stats.h"
22 #include "graphics/CharSelect.h"
23 #include "graphics/ComplexAnimation.h"
24 #include "graphics/Font.h"
25 #include "graphics/Frame.h"
26 #include "graphics/Gauge.h"
27 #include "graphics/Menu.h"
28 #include "graphics/SimpleAnimation.h"
29 #include "graphics/Sprite.h"
30 #include "graphics/Texture.h"
31 #include "loader/Caster.h"
32 #include "loader/Compiler.h"
33 #include "loader/Interpreter.h"
34 #include "loader/Loader.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 "math/Fixed.h"
45 #include "math/Vector.h"
46 #include "menu/Resources.h"
47 #include "sdl/InitImage.h"
48 #include "sdl/InitScreen.h"
49 #include "sdl/InitSDL.h"
50
51 #include <cstdlib>
52 #include <cstring>
53 #include <ctime>
54 #include <exception>
55 #include <iostream>
56 #include <string>
57 #include <SDL.h>
58 #include <SDL_image.h>
59
60 using app::Application;
61 using app::Arguments;
62 using app::Input;
63 using battle::BattleState;
64 using battle::Monster;
65 using battle::PartyLayout;
66 using common::Capsule;
67 using common::GameConfig;
68 using common::GameState;
69 using common::Hero;
70 using common::Spell;
71 using graphics::Texture;
72 using loader::Caster;
73 using loader::Compiler;
74 using loader::Interpreter;
75 using loader::Loader;
76 using loader::ParsedSource;
77 using loader::Parser;
78 using loader::TypeDescription;
79 using map::Entity;
80 using map::MapState;
81 using math::Fixed;
82 using math::Vector;
83 using sdl::InitImage;
84 using sdl::InitScreen;
85 using sdl::InitSDL;
86
87 using std::cerr;
88 using std::cout;
89 using std::endl;
90 using std::exception;
91 using std::string;
92 using std::vector;
93
94 int main(int argc, char **argv) {
95         const int width = 512;
96         const int height = 448;
97
98         const int walkSpeed = 8;
99
100         bool battle(false);
101
102 //      std::srand(std::time(0));
103
104         try {
105                 InitSDL sdl;
106                 InitImage image(IMG_INIT_PNG);
107
108                 Interpreter::CreateTypeDescriptions();
109
110                 battle::Resources::CreateTypeDescription();
111                 battle::Monster::CreateTypeDescription();
112                 battle::PartyLayout::CreateTypeDescription();
113
114                 common::Capsule::CreateTypeDescription();
115                 common::Hero::CreateTypeDescription();
116                 common::Ikari::CreateTypeDescription();
117                 common::Item::CreateTypeDescription();
118                 common::LevelUp::CreateTypeDescription();
119                 common::Stats::CreateTypeDescription();
120                 common::Spell::CreateTypeDescription();
121                 common::TargetingMode::CreateTypeDescription();
122
123                 graphics::Animation::CreateTypeDescription();
124                 graphics::CharSelect::CreateTypeDescription();
125                 graphics::ComplexAnimation::CreateTypeDescription();
126                 graphics::Font::CreateTypeDescription();
127                 graphics::Frame::CreateTypeDescription();
128                 graphics::Gauge::CreateTypeDescription();
129                 graphics::MenuProperties::CreateTypeDescription();
130                 graphics::SimpleAnimation::CreateTypeDescription();
131                 graphics::Sprite::CreateTypeDescription();
132                 graphics::Texture::CreateTypeDescription();
133
134                 map::Area::CreateTypeDescription();
135                 map::Entity::CreateTypeDescription();
136                 map::Map::CreateTypeDescription();
137                 map::Tile::CreateTypeDescription();
138                 map::Trigger::CreateTypeDescription();
139
140                 menu::Resources::CreateTypeDescription();
141
142                 Arguments args;
143                 args.Read(argc, argv);
144
145                 ParsedSource source;
146
147                 Loader ld;
148
149                 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
150                         string filePath(*i);
151                         switch (filePath[filePath.size() - 1]) {
152                                 case 'o':
153                                         ld.Load(filePath);
154                                         break;
155                                 case 's':
156                                         Parser(filePath, source).Parse();
157                                         break;
158                                 default:
159                                         throw std::runtime_error("don't know what to do with " + filePath);
160                         }
161                 }
162
163                 Interpreter intp(source);
164                 intp.ReadSource();
165
166                 switch (args.GetRunLevel()) {
167                         case Arguments::WRITE:
168                         {
169                                 int length(std::strlen(args.OutfilePath()));
170                                 switch (args.OutfilePath()[length - 1]) {
171                                         case 'h': {
172                                                 std::ofstream outstream(args.OutfilePath());
173                                                 source.WriteHeader(outstream);
174                                                 break;
175                                         }
176                                         case 'o': {
177                                                 std::fstream outstream(args.OutfilePath(), std::ios_base::out|std::ios_base::trunc);
178                                                 outstream.flush();
179                                                 outstream.close();
180                                                 outstream.open(args.OutfilePath());
181                                                 outstream.exceptions(std::ios_base::badbit|std::ios_base::failbit);
182                                                 Compiler(intp).Write(outstream);
183                                                 break;
184                                         }
185                                         default: {
186                                                 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
187                                         }
188                                 }
189                                 return 0;
190                         }
191                         case Arguments::DUMP: {
192                                 std::cout << source << ld << std::endl;
193                                 return 0;
194                         }
195                         case Arguments::SOURCE_WIKI: {
196                                 TypeDescription::WriteSourceWiki(std::cout);
197                                 return 0;
198                         }
199                         case Arguments::BATTLE:
200                                 battle = true;
201                                 break;
202                         case Arguments::PLAY:
203                         case Arguments::MAP:
204                                 break;
205                 }
206
207                 if (intp.PostponedDefinitions().size() > 0) {
208                         for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
209                                 std::cerr << "missing definition of " << TypeDescription::Get(i->type).TypeName() << " " << i->identifier << std::endl;
210                         }
211                         return 3;
212                 }
213
214                 Caster caster(ld, intp);
215
216                 GameState gameState;
217
218                 gameState.heroes[0] = *caster.GetHero("maxim");
219                 gameState.heroes[1] = *caster.GetHero("selan");
220                 gameState.heroes[2] = *caster.GetHero("guy");
221                 gameState.heroes[3] = *caster.GetHero("dekar");
222
223                 gameState.party[0] = &gameState.heroes[0];
224                 gameState.party[1] = &gameState.heroes[1];
225                 gameState.party[2] = &gameState.heroes[2];
226                 gameState.party[3] = &gameState.heroes[3];
227                 gameState.partySize = 4;
228
229                 gameState.capsules[1] = *caster.GetCapsule("flash");
230                 gameState.capsules[1].UpgradeClass();
231                 gameState.capsules[1].UpgradeClass();
232                 gameState.capsules[1].UpgradeClass();
233                 gameState.capsules[1].UpgradeClass();
234                 gameState.capsule = 1;
235
236                 GameConfig gameConfig;
237                 gameConfig.state = &gameState;
238                 gameConfig.heroesLayout = caster.GetPartyLayout("heroesLayout");
239                 gameConfig.battleResources = caster.GetBattleResources("battleResources");
240                 gameConfig.menuResources = caster.GetMenuResources("menuResources");
241
242                 // temporary test data
243                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
244                 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
245
246                 Monster monster(*caster.GetMonster("lizard"));
247
248                 gameState.heroes[0].AddSpell(caster.GetSpell("resetSpell"));
249                 Spell *strongSpell(caster.GetSpell("strongSpell"));
250                 gameState.heroes[0].AddSpell(strongSpell);
251                 gameState.heroes[1].AddSpell(strongSpell);
252                 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
253                 gameState.heroes[0].AddSpell(strongerSpell);
254                 gameState.heroes[1].AddSpell(strongerSpell);
255                 Spell *championSpell(caster.GetSpell("championSpell"));
256                 gameState.heroes[0].AddSpell(championSpell);
257                 gameState.heroes[1].AddSpell(championSpell);
258                 Spell *rallySpell(caster.GetSpell("rallySpell"));
259                 gameState.heroes[0].AddSpell(rallySpell);
260                 gameState.heroes[1].AddSpell(rallySpell);
261                 gameState.heroes[1].AddSpell(caster.GetSpell("escapeSpell"));
262                 Spell *valorSpell(caster.GetSpell("valorSpell"));
263                 gameState.heroes[0].AddSpell(valorSpell);
264                 gameState.heroes[1].AddSpell(valorSpell);
265
266                 gameState.inventory.Add(caster.GetItem("zirconPlateItem"), 32);
267                 gameState.inventory.Add(caster.GetItem("holyFruitItem"));
268                 gameState.inventory.Add(caster.GetItem("darkFruitItem"));
269                 gameState.inventory.Add(caster.GetItem("antidoteItem"), 9);
270                 gameState.inventory.Add(caster.GetItem("powerRingItem"));
271                 gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
272                 gameState.inventory.Add(caster.GetItem("sProRingItem"));
273                 gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
274                 gameState.inventory.Add(caster.GetItem("powerRingItem"));
275                 gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
276                 gameState.inventory.Add(caster.GetItem("zircoSwordItem"));
277                 gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
278                 gameState.inventory.Add(caster.GetItem("zircoHelmetItem"));
279                 gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
280                 gameState.inventory.Add(caster.GetItem("zirconPlateItem"));
281
282                 gameState.heroes[0].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoSwordItem"));
283                 gameState.heroes[0].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconArmorItem"));
284                 gameState.heroes[0].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("holyShieldItem"));
285                 gameState.heroes[0].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("legendHelmItem"));
286                 gameState.heroes[0].SetEquipment(Hero::EQUIP_RING, caster.GetItem("sProRingItem"));
287                 gameState.heroes[0].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("evilJewelItem"));
288
289 //              gameState.heroes[1].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoWhipItem"));
290                 gameState.heroes[1].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconPlateItem"));
291                 gameState.heroes[1].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("zircoGlovesItem"));
292                 gameState.heroes[1].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("holyCapItem"));
293                 gameState.heroes[1].SetEquipment(Hero::EQUIP_RING, caster.GetItem("ghostRingItem"));
294                 gameState.heroes[1].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("eagleRockItem"));
295
296 //              gameState.heroes[2].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("zircoAxItem"));
297                 gameState.heroes[2].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("zirconArmorItem"));
298                 gameState.heroes[2].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("megaShieldItem"));
299                 gameState.heroes[2].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("zircoHelmetItem"));
300                 gameState.heroes[2].SetEquipment(Hero::EQUIP_RING, caster.GetItem("powerRingItem"));
301                 gameState.heroes[2].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("evilJewelItem"));
302
303                 // NOTE: this is actually Artea equipment
304 //              gameState.heroes[3].SetEquipment(Hero::EQUIP_WEAPON, caster.GetItem("lizardBlowItem"));
305                 gameState.heroes[3].SetEquipment(Hero::EQUIP_ARMOR, caster.GetItem("holyRobeItem"));
306                 gameState.heroes[3].SetEquipment(Hero::EQUIP_SHIELD, caster.GetItem("zircoGlovesItem"));
307                 gameState.heroes[3].SetEquipment(Hero::EQUIP_HELMET, caster.GetItem("holyCapItem"));
308                 gameState.heroes[3].SetEquipment(Hero::EQUIP_RING, caster.GetItem("rocketRingItem"));
309                 gameState.heroes[3].SetEquipment(Hero::EQUIP_JEWEL, caster.GetItem("krakenRockItem"));
310
311                 gameState.heroes[0].MapEntity().Position() = Vector<Fixed<8> >(64, 128);
312
313                 gameState.heroes[1].MapEntity().Position() = Vector<Fixed<8> >(64, 128);
314                 gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
315                 gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity());
316
317                 gameState.heroes[2].MapEntity().Position() = Vector<Fixed<8> >(64, 128);
318                 gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
319                 gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity());
320
321                 gameState.heroes[3].MapEntity().Position() = Vector<Fixed<8> >(64, 128);
322                 gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
323                 gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity());
324
325                 InitScreen screen(width, height);
326
327                 app::State *state(0);
328
329                 if (battle) {
330                         BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
331                         battleState->AddMonster(monster);
332                         battleState->AddMonster(monster);
333                         battleState->AddMonster(monster);
334                         battleState->AddMonster(monster);
335                         battleState->SetCapsule(caster.GetCapsule("flash"));
336                         battleState->AddHero(gameState.heroes[0]);
337                         battleState->AddHero(gameState.heroes[1]);
338                         battleState->AddHero(gameState.heroes[2]);
339                         battleState->AddHero(gameState.heroes[3]);
340                         state = battleState;
341                 } else {
342                         MapState *mapState(new MapState(&gameConfig, caster.GetMap("map1")));
343
344                         mapState->ControlEntity(&gameState.heroes[0].MapEntity());
345                         mapState->SetWalkingSpeed(walkSpeed);
346
347                         state = mapState;
348                 }
349
350                 Application app(screen, state);
351                 MapKeys(app.Buttons());
352                 app.Run();
353
354                 return 0;
355         } catch (Parser::Error &e) {
356                 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
357                 return 2;
358         } catch (exception &e) {
359                 cerr << "exception in main(): " << e.what() << endl;
360                 return 1;
361         }
362 }