4 * Created on: Aug 1, 2012
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 "battle/Stats.h"
17 #include "common/Ikari.h"
18 #include "common/Inventory.h"
19 #include "common/Item.h"
20 #include "common/Spell.h"
21 #include "geometry/Vector.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 "loader/Caster.h"
30 #include "loader/Interpreter.h"
31 #include "loader/ParsedSource.h"
32 #include "loader/Parser.h"
33 #include "loader/TypeDescription.h"
35 #include "map/Entity.h"
37 #include "map/MapState.h"
39 #include "map/Trigger.h"
40 #include "sdl/InitImage.h"
41 #include "sdl/InitScreen.h"
42 #include "sdl/InitSDL.h"
51 #include <SDL_image.h>
53 using app::Application;
56 using battle::BattleState;
58 using battle::Monster;
59 using battle::PartyLayout;
62 using common::Inventory;
65 using geometry::Vector;
66 using graphics::ComplexAnimation;
68 using graphics::Frame;
69 using graphics::Gauge;
71 using graphics::SimpleAnimation;
72 using graphics::Sprite;
74 using loader::Interpreter;
75 using loader::ParsedSource;
77 using loader::TypeDescription;
85 using sdl::InitScreen;
95 int main(int argc, char **argv) {
96 const int width = 800;
97 const int height = 480;
99 const int tileSize = 32;
100 const float walkSpeed = 128.0f;
102 const bool battle(false);
104 // std::srand(std::time(0));
108 InitImage image(IMG_INIT_PNG);
110 battle::Resources::CreateTypeDescription();
111 ComplexAnimation::CreateTypeDescription();
112 Font::CreateTypeDescription();
113 Frame::CreateTypeDescription();
114 Gauge::CreateTypeDescription();
115 Hero::CreateTypeDescription();
116 Ikari::CreateTypeDescription();
117 Interpreter::CreateTypeDescriptions();
118 Item::CreateTypeDescription();
119 graphics::MenuProperties::CreateTypeDescription();
120 Monster::CreateTypeDescription();
121 PartyLayout::CreateTypeDescription();
122 SimpleAnimation::CreateTypeDescription();
123 Spell::CreateTypeDescription();
124 Sprite::CreateTypeDescription();
125 Stats::CreateTypeDescription();
126 common::TargetingMode::CreateTypeDescription();
129 args.Read(argc, argv);
133 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
134 Parser(*i, source).Parse();
137 switch (args.DetectRunLevel()) {
138 case Arguments::WRITE:
140 int length(std::strlen(args.OutfilePath()));
141 switch (args.OutfilePath()[length - 1]) {
143 std::ofstream outstream(args.OutfilePath());
144 source.WriteHeader(outstream);
148 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
153 case Arguments::DUMP: {
154 std::cout << source << std::endl;
157 case Arguments::PLAY:
161 Interpreter intp(source);
164 if (intp.PostponedDefinitions().size() > 0) {
165 for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
166 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
173 // temporary test data
174 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
175 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
176 PartyLayout heroesLayout(*caster.GetPartyLayout("heroesLayout"));
178 Monster monster(*caster.GetMonster("lizard"));
179 Hero maxim(*caster.GetHero("maxim"));
180 Hero selan(*caster.GetHero("selan"));
181 Hero guy(*caster.GetHero("guy"));
182 Hero dekar(*caster.GetHero("dekar"));
184 battle::Resources *battleRes(caster.GetBattleResources("battleResources"));
186 maxim.AddSpell(caster.GetSpell("resetSpell"));
187 Spell *strongSpell(caster.GetSpell("strongSpell"));
188 maxim.AddSpell(strongSpell);
189 selan.AddSpell(strongSpell);
190 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
191 maxim.AddSpell(strongerSpell);
192 selan.AddSpell(strongerSpell);
193 Spell *championSpell(caster.GetSpell("championSpell"));
194 maxim.AddSpell(championSpell);
195 selan.AddSpell(championSpell);
196 Spell *rallySpell(caster.GetSpell("rallySpell"));
197 maxim.AddSpell(rallySpell);
198 selan.AddSpell(rallySpell);
199 selan.AddSpell(caster.GetSpell("escapeSpell"));
200 Spell *valorSpell(caster.GetSpell("valorSpell"));
201 maxim.AddSpell(valorSpell);
202 selan.AddSpell(valorSpell);
205 inventory.Add(caster.GetItem("antidoteItem"), 9);
206 inventory.Add(caster.GetItem("magicJarItem"), 4);
207 inventory.Add(caster.GetItem("hiPotionItem"), 4);
208 inventory.Add(caster.GetItem("powerPotionItem"), 4);
209 inventory.Add(caster.GetItem("escapeItem"), 2);
210 inventory.Add(caster.GetItem("sleepBallItem"), 1);
211 battleRes->inventory = &inventory;
213 maxim.SetWeapon(caster.GetItem("zircoSwordItem"));
214 maxim.SetArmor(caster.GetItem("zirconArmorItem"));
215 maxim.SetShield(caster.GetItem("holyShieldItem"));
216 maxim.SetHelmet(caster.GetItem("legendHelmItem"));
217 maxim.SetRing(caster.GetItem("sProRingItem"));
218 maxim.SetJewel(caster.GetItem("evilJewelItem"));
220 // selan.SetWeapon(cst.GetItem("zircoWhipItem"));
221 selan.SetArmor(caster.GetItem("zirconPlateItem"));
222 selan.SetShield(caster.GetItem("zircoGlovesItem"));
223 selan.SetHelmet(caster.GetItem("holyCapItem"));
224 selan.SetRing(caster.GetItem("ghostRingItem"));
225 selan.SetJewel(caster.GetItem("eagleRockItem"));
227 // guy.SetWeapon(cst.GetItem("zircoAxItem"));
228 guy.SetArmor(caster.GetItem("zirconArmorItem"));
229 guy.SetShield(caster.GetItem("megaShieldItem"));
230 guy.SetHelmet(caster.GetItem("zircoHelmetItem"));
231 guy.SetRing(caster.GetItem("powerRingItem"));
232 guy.SetJewel(caster.GetItem("evilJewelItem"));
234 // NOTE: this is actually Artea equipment
235 // dekar.SetWeapon(cst.GetItem("lizardBlowItem"));
236 dekar.SetArmor(caster.GetItem("holyRobeItem"));
237 dekar.SetShield(caster.GetItem("zircoGlovesItem"));
238 dekar.SetHelmet(caster.GetItem("holyCapItem"));
239 dekar.SetRing(caster.GetItem("rocketRingItem"));
240 dekar.SetJewel(caster.GetItem("krakenRockItem"));
244 tiles1[ 0].SetOffset(Vector<int>(2, 1));
245 tiles1[ 1].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_WEST);
246 tiles1[ 2].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
247 tiles1[ 3].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
248 tiles1[ 4].SetOffset(Vector<int>(0, 1));
249 tiles1[ 5].SetOffset(Vector<int>(2, 0));
250 tiles1[ 6].SetOffset(Vector<int>(2, 0));
251 tiles1[ 7].SetOffset(Vector<int>(2, 0));
253 tiles1[ 8].SetOffset(Vector<int>(2, 1));
254 tiles1[ 9].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
255 tiles1[10].SetOffset(Vector<int>(3, 0));
256 tiles1[11].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
257 tiles1[12].SetOffset(Vector<int>(0, 2));
258 tiles1[13].SetOffset(Vector<int>(1, 2));
259 tiles1[14].SetOffset(Vector<int>(1, 2));
260 tiles1[15].SetOffset(Vector<int>(1, 2));
262 tiles1[16].SetOffset(Vector<int>(2, 1));
263 tiles1[17].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
264 tiles1[18].SetOffset(Vector<int>(3, 0));
265 tiles1[19].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
266 tiles1[20].SetOffset(Vector<int>(0, 3));
267 tiles1[21].SetOffset(Vector<int>(1, 3));
268 tiles1[22].SetOffset(Vector<int>(1, 3));
269 tiles1[23].SetOffset(Vector<int>(2, 3));
271 tiles1[24].SetOffset(Vector<int>(2, 1));
272 tiles1[25].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
273 tiles1[26].SetOffset(Vector<int>(3, 0));
274 tiles1[27].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
275 tiles1[28].SetOffset(Vector<int>(0, 4));
276 tiles1[29].SetOffset(Vector<int>(1, 4));
277 tiles1[30].SetOffset(Vector<int>(1, 4));
278 tiles1[31].SetOffset(Vector<int>(2, 4));
280 tiles1[32].SetOffset(Vector<int>(2, 1));
281 tiles1[33].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
282 tiles1[34].SetOffset(Vector<int>(3, 0));
283 tiles1[35].SetOffset(Vector<int>(3, 0));
284 tiles1[36].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
285 tiles1[37].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
286 tiles1[38].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
287 tiles1[39].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
289 tiles1[40].SetOffset(Vector<int>(2, 1));
290 tiles1[41].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
291 tiles1[42].SetOffset(Vector<int>(3, 0));
292 tiles1[43].SetOffset(Vector<int>(3, 0));
293 tiles1[44].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
294 tiles1[45].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
295 tiles1[46].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
296 tiles1[47].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
298 tiles1[48].SetOffset(Vector<int>(2, 1));
299 tiles1[49].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
300 tiles1[50].SetOffset(Vector<int>(3, 0));
301 tiles1[51].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
302 tiles1[52].SetOffset(Vector<int>(0, 0));
303 tiles1[53].SetOffset(Vector<int>(1, 0));
304 tiles1[54].SetOffset(Vector<int>(1, 0));
305 tiles1[55].SetOffset(Vector<int>(1, 0));
307 tiles1[56].SetOffset(Vector<int>(2, 1));
308 tiles1[57].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_WEST);
309 tiles1[58].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
310 tiles1[59].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
311 tiles1[60].SetOffset(Vector<int>(0, 1));
312 tiles1[61].SetOffset(Vector<int>(1, 1));
313 tiles1[62].SetOffset(Vector<int>(1, 1));
314 tiles1[63].SetOffset(Vector<int>(1, 1));
318 tiles2[ 0].SetOffset(Vector<int>(2, 0));
319 tiles2[ 1].SetOffset(Vector<int>(2, 0));
320 tiles2[ 2].SetOffset(Vector<int>(2, 0));
321 tiles2[ 3].SetOffset(Vector<int>(2, 0));
322 tiles2[ 4].SetOffset(Vector<int>(2, 0));
323 tiles2[ 5].SetOffset(Vector<int>(2, 0));
324 tiles2[ 6].SetOffset(Vector<int>(2, 0));
325 tiles2[ 7].SetOffset(Vector<int>(2, 0));
327 tiles2[ 8].SetOffset(Vector<int>(1, 2));
328 tiles2[ 9].SetOffset(Vector<int>(1, 2));
329 tiles2[10].SetOffset(Vector<int>(5, 3));
330 tiles2[11].SetOffset(Vector<int>(2, 0));
331 tiles2[12].SetOffset(Vector<int>(2, 0));
332 tiles2[13].SetOffset(Vector<int>(2, 0));
333 tiles2[14].SetOffset(Vector<int>(2, 0));
334 tiles2[15].SetOffset(Vector<int>(2, 0));
336 tiles2[16].SetOffset(Vector<int>(3, 3));
337 tiles2[17].SetOffset(Vector<int>(0, 3));
338 tiles2[18].SetOffset(Vector<int>(0, 1));
339 tiles2[19].SetOffset(Vector<int>(2, 0));
340 tiles2[20].SetOffset(Vector<int>(2, 0));
341 tiles2[21].SetOffset(Vector<int>(2, 0));
342 tiles2[22].SetOffset(Vector<int>(2, 0));
343 tiles2[23].SetOffset(Vector<int>(2, 0));
345 tiles2[24].SetOffset(Vector<int>(3, 4)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST | Tile::BLOCK_WEST);
346 tiles2[25].SetOffset(Vector<int>(0, 4));
347 tiles2[26].SetOffset(Vector<int>(0, 1));
348 tiles2[27].SetOffset(Vector<int>(2, 0));
349 tiles2[28].SetOffset(Vector<int>(2, 0));
350 tiles2[29].SetOffset(Vector<int>(2, 0));
351 tiles2[30].SetOffset(Vector<int>(2, 0));
352 tiles2[31].SetOffset(Vector<int>(2, 0));
354 tiles2[32].SetOffset(Vector<int>(5, 0));
355 tiles2[33].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
356 tiles2[34].SetOffset(Vector<int>(0, 1));
357 tiles2[35].SetOffset(Vector<int>(2, 0));
358 tiles2[36].SetOffset(Vector<int>(2, 0));
359 tiles2[37].SetOffset(Vector<int>(2, 0));
360 tiles2[38].SetOffset(Vector<int>(2, 0));
361 tiles2[39].SetOffset(Vector<int>(2, 0));
363 tiles2[40].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
364 tiles2[41].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
365 tiles2[42].SetOffset(Vector<int>(0, 1));
366 tiles2[43].SetOffset(Vector<int>(2, 0));
367 tiles2[44].SetOffset(Vector<int>(2, 0));
368 tiles2[45].SetOffset(Vector<int>(2, 0));
369 tiles2[46].SetOffset(Vector<int>(2, 0));
370 tiles2[47].SetOffset(Vector<int>(2, 0));
372 tiles2[48].SetOffset(Vector<int>(1, 0));
373 tiles2[49].SetOffset(Vector<int>(1, 0));
374 tiles2[50].SetOffset(Vector<int>(5, 4));
375 tiles2[51].SetOffset(Vector<int>(2, 0));
376 tiles2[52].SetOffset(Vector<int>(2, 0));
377 tiles2[53].SetOffset(Vector<int>(2, 0));
378 tiles2[54].SetOffset(Vector<int>(2, 0));
379 tiles2[55].SetOffset(Vector<int>(2, 0));
381 tiles2[56].SetOffset(Vector<int>(2, 0));
382 tiles2[57].SetOffset(Vector<int>(2, 0));
383 tiles2[58].SetOffset(Vector<int>(2, 0));
384 tiles2[59].SetOffset(Vector<int>(2, 0));
385 tiles2[60].SetOffset(Vector<int>(2, 0));
386 tiles2[61].SetOffset(Vector<int>(2, 0));
387 tiles2[62].SetOffset(Vector<int>(2, 0));
388 tiles2[63].SetOffset(Vector<int>(2, 0));
391 areas[0].SetTiles(tiles1, 64);
392 areas[0].SetWidth(8);
393 areas[1].SetTiles(tiles2, 64);
394 areas[1].SetWidth(8);
397 triggers[0].SetTilePosition(Vector<int>(2, 0));
399 SDL_Surface *tilesetImg(IMG_Load("test-data/tileset.png"));
400 Sprite tileset(tilesetImg, tileSize, tileSize);
403 map.SetAreas(areas, 2);
404 map.SetTileset(&tileset);
405 map.SetTriggers(triggers, 1);
408 SDL_Surface *mapMaximImg(IMG_Load("test-data/maxim-map.png"));
409 Sprite mapMaximSprite(mapMaximImg, 32, 64);
410 SimpleAnimation mapMaximAnimation(&mapMaximSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
412 mapMaxim.SetAnimation(&mapMaximAnimation);
413 mapMaxim.Position() = Vector<float>(64, 128);
414 mapMaxim.SpriteOffset() = Vector<float>(0, -32);
416 SDL_Surface *mapSelanImg(IMG_Load("test-data/selan-map.png"));
417 Sprite mapSelanSprite(mapSelanImg, 32, 64);
418 SimpleAnimation mapSelanAnimation(&mapSelanSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
420 mapSelan.SetAnimation(&mapSelanAnimation);
421 mapSelan.Position() = Vector<float>(64, 128);
422 mapSelan.SpriteOffset() = Vector<float>(0, -32);
423 mapSelan.SetFlags(Entity::FLAG_NONBLOCKING);
424 mapMaxim.AddFollower(&mapSelan);
426 SDL_Surface *mapGuyImg(IMG_Load("test-data/guy-map.png"));
427 Sprite mapGuySprite(mapGuyImg, 32, 64);
428 SimpleAnimation mapGuyAnimation(&mapGuySprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
430 mapGuy.SetAnimation(&mapGuyAnimation);
431 mapGuy.Position() = Vector<float>(64, 128);
432 mapGuy.SpriteOffset() = Vector<float>(0, -32);
433 mapGuy.SetFlags(Entity::FLAG_NONBLOCKING);
434 mapSelan.AddFollower(&mapGuy);
436 SDL_Surface *mapDekarImg(IMG_Load("test-data/dekar-map.png"));
437 Sprite mapDekarSprite(mapDekarImg, 32, 64);
438 SimpleAnimation mapDekarAnimation(&mapDekarSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
440 mapDekar.SetAnimation(&mapDekarAnimation);
441 mapDekar.Position() = Vector<float>(64, 128);
442 mapDekar.SpriteOffset() = Vector<float>(0, -32);
443 mapDekar.SetFlags(Entity::FLAG_NONBLOCKING);
444 mapGuy.AddFollower(&mapDekar);
446 SDL_Surface *mapMonsterImg(IMG_Load("test-data/monster-map.png"));
447 Sprite mapMonsterSprite(mapMonsterImg, 32, 32);
448 SimpleAnimation mapMonsterAnimation(&mapMonsterSprite, 500, 2, 0, 0, true);
450 mapMonster.SetAnimation(&mapMonsterAnimation);
451 mapMonster.Position() = Vector<float>(64, 32);
452 mapMonster.SetOrientation(Entity::ORIENTATION_SOUTH);
454 InitScreen screen(width, height);
456 app::State *state(0);
459 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
460 battleState->AddMonster(monster);
461 battleState->AddMonster(monster);
462 battleState->AddMonster(monster);
463 battleState->AddMonster(monster);
464 battleState->AddHero(maxim);
465 battleState->AddHero(selan);
466 battleState->AddHero(guy);
467 battleState->AddHero(dekar);
470 MapState *mapState(new MapState(&map));
472 mapState->AddEntity(&mapMaxim);
473 mapState->AddEntity(&mapSelan);
474 mapState->AddEntity(&mapGuy);
475 mapState->AddEntity(&mapDekar);
477 mapState->ControlEntity(&mapMaxim);
478 mapState->SetWalkingSpeed(walkSpeed);
480 mapState->AddEntity(&mapMonster);
481 mapMonster.StartAnimation(*mapState);
486 Application app(&screen, state);
487 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
488 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
489 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
490 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
491 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
492 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
493 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
494 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
495 app.Buttons().MapKey(SDLK_RETURN, Input::START);
496 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
497 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
498 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
499 app.Buttons().MapKey(SDLK_1, Input::DEBUG_1);
500 app.Buttons().MapKey(SDLK_2, Input::DEBUG_2);
501 app.Buttons().MapKey(SDLK_3, Input::DEBUG_3);
502 app.Buttons().MapKey(SDLK_4, Input::DEBUG_4);
506 } catch (Parser::Error &e) {
507 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
509 } catch (exception &e) {
510 cerr << "exception in main(): " << e.what() << endl;