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 "sdl/InitImage.h"
40 #include "sdl/InitScreen.h"
41 #include "sdl/InitSDL.h"
50 #include <SDL_image.h>
52 using app::Application;
55 using battle::BattleState;
57 using battle::Monster;
58 using battle::PartyLayout;
61 using common::Inventory;
64 using geometry::Vector;
65 using graphics::ComplexAnimation;
67 using graphics::Frame;
68 using graphics::Gauge;
70 using graphics::SimpleAnimation;
71 using graphics::Sprite;
73 using loader::Interpreter;
74 using loader::ParsedSource;
76 using loader::TypeDescription;
83 using sdl::InitScreen;
93 int main(int argc, char **argv) {
94 const int width = 800;
95 const int height = 480;
97 const int tileSize = 32;
98 const float walkSpeed = 128.0f;
100 const bool battle(false);
102 // std::srand(std::time(0));
106 InitImage image(IMG_INIT_PNG);
108 battle::Resources::CreateTypeDescription();
109 ComplexAnimation::CreateTypeDescription();
110 Font::CreateTypeDescription();
111 Frame::CreateTypeDescription();
112 Gauge::CreateTypeDescription();
113 Hero::CreateTypeDescription();
114 Ikari::CreateTypeDescription();
115 Interpreter::CreateTypeDescriptions();
116 Item::CreateTypeDescription();
117 graphics::MenuProperties::CreateTypeDescription();
118 Monster::CreateTypeDescription();
119 PartyLayout::CreateTypeDescription();
120 SimpleAnimation::CreateTypeDescription();
121 Spell::CreateTypeDescription();
122 Sprite::CreateTypeDescription();
123 Stats::CreateTypeDescription();
124 common::TargetingMode::CreateTypeDescription();
127 args.Read(argc, argv);
131 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
132 Parser(*i, source).Parse();
135 switch (args.DetectRunLevel()) {
136 case Arguments::WRITE:
138 int length(std::strlen(args.OutfilePath()));
139 switch (args.OutfilePath()[length - 1]) {
141 std::ofstream outstream(args.OutfilePath());
142 source.WriteHeader(outstream);
146 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
151 case Arguments::DUMP: {
152 std::cout << source << std::endl;
155 case Arguments::PLAY:
159 Interpreter intp(source);
162 if (intp.PostponedDefinitions().size() > 0) {
163 for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
164 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
171 // temporary test data
172 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
173 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
174 PartyLayout heroesLayout(*caster.GetPartyLayout("heroesLayout"));
176 Monster monster(*caster.GetMonster("lizard"));
177 Hero maxim(*caster.GetHero("maxim"));
178 Hero selan(*caster.GetHero("selan"));
179 Hero guy(*caster.GetHero("guy"));
180 Hero dekar(*caster.GetHero("dekar"));
182 battle::Resources *battleRes(caster.GetBattleResources("battleResources"));
184 maxim.AddSpell(caster.GetSpell("resetSpell"));
185 Spell *strongSpell(caster.GetSpell("strongSpell"));
186 maxim.AddSpell(strongSpell);
187 selan.AddSpell(strongSpell);
188 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
189 maxim.AddSpell(strongerSpell);
190 selan.AddSpell(strongerSpell);
191 Spell *championSpell(caster.GetSpell("championSpell"));
192 maxim.AddSpell(championSpell);
193 selan.AddSpell(championSpell);
194 Spell *rallySpell(caster.GetSpell("rallySpell"));
195 maxim.AddSpell(rallySpell);
196 selan.AddSpell(rallySpell);
197 selan.AddSpell(caster.GetSpell("escapeSpell"));
198 Spell *valorSpell(caster.GetSpell("valorSpell"));
199 maxim.AddSpell(valorSpell);
200 selan.AddSpell(valorSpell);
203 inventory.Add(caster.GetItem("antidoteItem"), 9);
204 inventory.Add(caster.GetItem("magicJarItem"), 4);
205 inventory.Add(caster.GetItem("hiPotionItem"), 4);
206 inventory.Add(caster.GetItem("powerPotionItem"), 4);
207 inventory.Add(caster.GetItem("escapeItem"), 2);
208 inventory.Add(caster.GetItem("sleepBallItem"), 1);
209 battleRes->inventory = &inventory;
211 maxim.SetWeapon(caster.GetItem("zircoSwordItem"));
212 maxim.SetArmor(caster.GetItem("zirconArmorItem"));
213 maxim.SetShield(caster.GetItem("holyShieldItem"));
214 maxim.SetHelmet(caster.GetItem("legendHelmItem"));
215 maxim.SetRing(caster.GetItem("sProRingItem"));
216 maxim.SetJewel(caster.GetItem("evilJewelItem"));
218 // selan.SetWeapon(cst.GetItem("zircoWhipItem"));
219 selan.SetArmor(caster.GetItem("zirconPlateItem"));
220 selan.SetShield(caster.GetItem("zircoGlovesItem"));
221 selan.SetHelmet(caster.GetItem("holyCapItem"));
222 selan.SetRing(caster.GetItem("ghostRingItem"));
223 selan.SetJewel(caster.GetItem("eagleRockItem"));
225 // guy.SetWeapon(cst.GetItem("zircoAxItem"));
226 guy.SetArmor(caster.GetItem("zirconArmorItem"));
227 guy.SetShield(caster.GetItem("megaShieldItem"));
228 guy.SetHelmet(caster.GetItem("zircoHelmetItem"));
229 guy.SetRing(caster.GetItem("powerRingItem"));
230 guy.SetJewel(caster.GetItem("evilJewelItem"));
232 // NOTE: this is actually Artea equipment
233 // dekar.SetWeapon(cst.GetItem("lizardBlowItem"));
234 dekar.SetArmor(caster.GetItem("holyRobeItem"));
235 dekar.SetShield(caster.GetItem("zircoGlovesItem"));
236 dekar.SetHelmet(caster.GetItem("holyCapItem"));
237 dekar.SetRing(caster.GetItem("rocketRingItem"));
238 dekar.SetJewel(caster.GetItem("krakenRockItem"));
242 tiles[ 0].SetOffset(Vector<int>(2, 1));
243 tiles[ 1].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_WEST);
244 tiles[ 2].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
245 tiles[ 3].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
246 tiles[ 4].SetOffset(Vector<int>(0, 1));
247 tiles[ 5].SetOffset(Vector<int>(2, 0));
248 tiles[ 6].SetOffset(Vector<int>(2, 0));
249 tiles[ 7].SetOffset(Vector<int>(2, 0));
251 tiles[ 8].SetOffset(Vector<int>(2, 1));
252 tiles[ 9].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
253 tiles[10].SetOffset(Vector<int>(3, 0));
254 tiles[11].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
255 tiles[12].SetOffset(Vector<int>(0, 2));
256 tiles[13].SetOffset(Vector<int>(1, 2));
257 tiles[14].SetOffset(Vector<int>(1, 2));
258 tiles[15].SetOffset(Vector<int>(1, 2));
260 tiles[16].SetOffset(Vector<int>(2, 1));
261 tiles[17].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
262 tiles[18].SetOffset(Vector<int>(3, 0));
263 tiles[19].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
264 tiles[20].SetOffset(Vector<int>(0, 3));
265 tiles[21].SetOffset(Vector<int>(1, 3));
266 tiles[22].SetOffset(Vector<int>(1, 3));
267 tiles[23].SetOffset(Vector<int>(2, 3));
269 tiles[24].SetOffset(Vector<int>(2, 1));
270 tiles[25].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
271 tiles[26].SetOffset(Vector<int>(3, 0));
272 tiles[27].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
273 tiles[28].SetOffset(Vector<int>(0, 4));
274 tiles[29].SetOffset(Vector<int>(1, 4));
275 tiles[30].SetOffset(Vector<int>(1, 4));
276 tiles[31].SetOffset(Vector<int>(2, 4));
278 tiles[32].SetOffset(Vector<int>(2, 1));
279 tiles[33].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
280 tiles[34].SetOffset(Vector<int>(3, 0));
281 tiles[35].SetOffset(Vector<int>(3, 0));
282 tiles[36].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
283 tiles[37].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
284 tiles[38].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
285 tiles[39].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
287 tiles[40].SetOffset(Vector<int>(2, 1));
288 tiles[41].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
289 tiles[42].SetOffset(Vector<int>(3, 0));
290 tiles[43].SetOffset(Vector<int>(3, 0));
291 tiles[44].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
292 tiles[45].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
293 tiles[46].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
294 tiles[47].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
296 tiles[48].SetOffset(Vector<int>(2, 1));
297 tiles[49].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
298 tiles[50].SetOffset(Vector<int>(3, 0));
299 tiles[51].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
300 tiles[52].SetOffset(Vector<int>(0, 0));
301 tiles[53].SetOffset(Vector<int>(1, 0));
302 tiles[54].SetOffset(Vector<int>(1, 0));
303 tiles[55].SetOffset(Vector<int>(1, 0));
305 tiles[56].SetOffset(Vector<int>(2, 1));
306 tiles[57].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_WEST);
307 tiles[58].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
308 tiles[59].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
309 tiles[60].SetOffset(Vector<int>(0, 1));
310 tiles[61].SetOffset(Vector<int>(1, 1));
311 tiles[62].SetOffset(Vector<int>(1, 1));
312 tiles[63].SetOffset(Vector<int>(1, 1));
315 area.SetTiles(tiles, 64);
318 SDL_Surface *tilesetImg(IMG_Load("test-data/tileset.png"));
319 Sprite tileset(tilesetImg, tileSize, tileSize);
322 map.SetAreas(&area, 1);
323 map.SetTileset(&tileset);
326 SDL_Surface *mapMaximImg(IMG_Load("test-data/maxim-map.png"));
327 Sprite mapMaximSprite(mapMaximImg, 32, 64);
328 SimpleAnimation mapMaximAnimation(&mapMaximSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
330 mapMaxim.SetAnimation(&mapMaximAnimation);
331 mapMaxim.Position() = Vector<float>(80, 128);
333 InitScreen screen(width, height);
335 app::State *state(0);
338 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
339 battleState->AddMonster(monster);
340 battleState->AddMonster(monster);
341 battleState->AddMonster(monster);
342 battleState->AddMonster(monster);
343 battleState->AddHero(maxim);
344 battleState->AddHero(selan);
345 battleState->AddHero(guy);
346 battleState->AddHero(dekar);
349 MapState *mapState(new MapState(&map));
350 mapState->AddEntity(&mapMaxim);
351 mapState->ControlEntity(&mapMaxim);
352 mapState->SetWalkingSpeed(walkSpeed);
356 Application app(&screen, state);
357 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
358 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
359 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
360 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
361 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
362 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
363 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
364 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
365 app.Buttons().MapKey(SDLK_RETURN, Input::START);
366 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
367 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
368 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
372 } catch (Parser::Error &e) {
373 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
375 } catch (exception &e) {
376 cerr << "exception in main(): " << e.what() << endl;