4 * Created on: Aug 5, 2012
8 #include "BattleState.h"
10 #include "PartyLayout.h"
11 #include "states/SelectMoveAction.h"
12 #include "states/PerformAttacks.h"
13 #include "../app/Application.h"
14 #include "../app/Input.h"
15 #include "../common/Ikari.h"
16 #include "../common/Inventory.h"
17 #include "../common/Item.h"
18 #include "../common/Spell.h"
19 #include "../geometry/operators.h"
20 #include "../graphics/Frame.h"
21 #include "../graphics/Sprite.h"
26 using app::Application;
28 using common::Inventory;
31 using geometry::Point;
32 using geometry::Vector;
39 void BattleState::AddMonster(const Monster &m) {
40 if (monsters.size() >= monstersLayout->NumPositions()) {
41 throw std::overflow_error("too many monsters for layout");
43 monsters.push_back(m);
46 void BattleState::AddHero(const Hero &h) {
47 if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
48 throw std::overflow_error("too many heroes for layout");
50 heroes[numHeroes] = h;
54 void BattleState::NextHero() {
56 while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
61 void BattleState::SwapHeroes(int lhs, int rhs) {
62 if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
63 std::swap(heroes[lhs], heroes[rhs]);
67 void BattleState::Resize(int w, int h) {
72 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
73 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
74 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
75 for (int i(0); i < 4; ++i) {
76 spellMenus[i] = res->spellMenuPrototype;
78 ikariMenus[i] = res->ikariMenuPrototype;
80 heroTags[i] = HeroTag(this, i);
81 smallHeroTags[i] = SmallHeroTag(this, i);
84 int tagHeight(attackTypeMenu.Height());
85 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
86 int xOffset((Width() - 2 * tagWidth) / 2);
87 heroTagPositions[0] = Point<int>(xOffset, Height() - 2 * tagHeight);
88 heroTagPositions[1] = Point<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
89 heroTagPositions[2] = Point<int>(xOffset, Height() - tagHeight);
90 heroTagPositions[3] = Point<int>(xOffset + tagWidth, Height() - tagHeight);
92 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
93 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
94 xOffset = (Width() - 4 * tagWidth) / 2;
95 int yOffset(Height() - tagHeight);
96 smallHeroTagPositions[0] = Point<int>(xOffset, yOffset);
97 smallHeroTagPositions[1] = Point<int>(xOffset + 2 * tagWidth, yOffset);
98 smallHeroTagPositions[2] = Point<int>(xOffset + tagWidth, yOffset);
99 smallHeroTagPositions[3] = Point<int>(xOffset + 3 * tagWidth, yOffset);
101 itemMenu = res->itemMenuPrototype;
105 void BattleState::LoadSpellMenu(vector<Hero>::size_type index) {
106 spellMenus[index].Clear();
107 spellMenus[index].Reserve(HeroAt(index).Spells().size());
108 for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
109 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
110 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
114 void BattleState::LoadIkariMenu(vector<Hero>::size_type index) {
115 ikariMenus[index].Clear();
116 ikariMenus[index].Reserve(6);
118 if (HeroAt(index).HasWeapon()) {
119 ikariMenus[index].Add(
120 HeroAt(index).Weapon()->Name(),
121 HeroAt(index).Weapon(),
122 HeroAt(index).Weapon()->HasIkari() && HeroAt(index).Weapon()->GetIkari()->Cost() <= HeroAt(index).IP(),
125 HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : "");
127 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon);
130 if (HeroAt(index).HasArmor()) {
131 ikariMenus[index].Add(
132 HeroAt(index).Armor()->Name(),
133 HeroAt(index).Armor(),
134 HeroAt(index).Armor()->HasIkari() && HeroAt(index).Armor()->GetIkari()->Cost() <= HeroAt(index).IP(),
137 HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : "");
139 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon);
142 if (HeroAt(index).HasShield()) {
143 ikariMenus[index].Add(
144 HeroAt(index).Shield()->Name(),
145 HeroAt(index).Shield(),
146 HeroAt(index).Shield()->HasIkari() && HeroAt(index).Shield()->GetIkari()->Cost() <= HeroAt(index).IP(),
149 HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : "");
151 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon);
154 if (HeroAt(index).HasHelmet()) {
155 ikariMenus[index].Add(
156 HeroAt(index).Helmet()->Name(),
157 HeroAt(index).Helmet(),
158 HeroAt(index).Helmet()->HasIkari() && HeroAt(index).Helmet()->GetIkari()->Cost() <= HeroAt(index).IP(),
161 HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : "");
163 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon);
166 if (HeroAt(index).HasRing()) {
167 ikariMenus[index].Add(
168 HeroAt(index).Ring()->Name(),
169 HeroAt(index).Ring(),
170 HeroAt(index).Ring()->HasIkari() && HeroAt(index).Ring()->GetIkari()->Cost() <= HeroAt(index).IP(),
173 HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : "");
175 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon);
178 if (HeroAt(index).HasJewel()) {
179 ikariMenus[index].Add(
180 HeroAt(index).Jewel()->Name(),
181 HeroAt(index).Jewel(),
182 HeroAt(index).Jewel()->HasIkari() && HeroAt(index).Jewel()->GetIkari()->Cost() <= HeroAt(index).IP(),
185 HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : "");
187 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon);
191 void BattleState::LoadInventory() {
192 const Inventory &inv(*res->inventory);
194 itemMenu.Reserve(inv.MaxItems());
195 for (int i(0); i < inv.MaxItems(); ++i) {
196 const Item *item(inv.ItemAt(i));
198 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
200 itemMenu.AddEmptyEntry();
206 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
210 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
211 // TODO: check for victory or defeat
213 ctrl.PopState(); // quit the battle scene
217 // TODO: push victory state
222 // TODO: push defeat state
226 // TODO: this should not push a state while quitting
227 if (AttackSelectionDone()) {
228 ctrl.PushState(new PerformAttacks(this));
230 ctrl.PushState(new SelectMoveAction(this));
234 bool BattleState::Victory() const {
235 for (int i(0); i < MaxMonsters(); ++i) {
236 if (MonsterAt(i).Health() > 0) return false;
241 bool BattleState::Defeat() const {
242 for (int i(0); i < NumHeroes(); ++i) {
243 if (HeroAt(i).Health() > 0) return false;
248 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
255 OrderCompare(BattleState *battle) : battle(battle) { }
256 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
257 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
258 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
265 void BattleState::CalculateAttackOrder() {
266 attackOrder.reserve(monsters.size() + NumHeroes());
267 for (int i(0); i < numHeroes; ++i) {
268 attackOrder.push_back(Order(i, false));
270 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
271 attackOrder.push_back(Order(i, true));
273 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
275 monsterAttacks.resize(monsters.size(), AttackChoice(this));
278 void BattleState::NextAttack() {
280 while (attackCursor < int(attackOrder.size())) {
281 if (attackOrder[attackCursor].isMonster) {
282 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
284 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
290 void BattleState::CalculateDamage() {
291 AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
292 if (ac.GetType() == AttackChoice::DEFEND) return;
294 if (CurrentAttack().isMonster) {
295 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
296 // TODO: run monster's attack script
297 ac.SetType(AttackChoice::SWORD);
298 ac.Selection().SelectSingle();
299 ac.Selection().SelectHeroes();
300 for (int i(0); i < NumHeroes(); ++i) {
301 if (HeroAt(i).Health() > 0) {
302 const Stats &defenderStats(HeroAt(i).GetStats());
303 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
304 ac.Selection().SetBad(0, damage);
309 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
310 TargetSelection &ts(ac.Selection());
312 if (ts.TargetsEnemies()) {
313 for (int i(0); i < MaxMonsters(); ++i) {
314 if (ts.IsSelected(i)) {
315 if (MonsterAt(i).Health() > 0) {
316 const Stats &defenderStats(MonsterAt(i).GetStats());
317 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
318 ts.SetBad(i, damage);
326 for (int i(0); i < MaxMonsters(); ++i) {
327 if (MonsterAt(i).Health() > 0) {
328 const Stats &defenderStats(MonsterAt(i).GetStats());
329 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
330 ts.SetBad(i, damage);
335 for (int i(0); i < NumHeroes(); ++i) {
336 if (ts.IsSelected(i)) {
337 if (HeroAt(i).Health() > 0) {
338 const Stats &defenderStats(HeroAt(i).GetStats());
339 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
340 ts.SetBad(i, damage);
348 for (int i(0); i < NumHeroes(); ++i) {
349 if (HeroAt(i).Health() > 0) {
350 const Stats &defenderStats(HeroAt(i).GetStats());
351 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
352 ts.SetBad(i, damage);
360 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
361 // TODO: find out real formula and add some randomness
362 return attacker.Attack() / 2 - defender.Defense() / 4;
365 void BattleState::ApplyDamage() {
366 if (attackCursor < 0) return;
367 AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
368 TargetSelection &ts(ac.Selection());
369 if (ts.TargetsEnemies()) {
370 for (int i(0); i < MaxMonsters(); ++i) {
372 MonsterAt(i).SubtractHealth(ts.GetAmount(i));
376 for (int i(0); i < NumHeroes(); ++i) {
378 HeroAt(i).SubtractHealth(ts.GetAmount(i));
384 void BattleState::ClearAllAttacks() {
387 for (int i(0); i < numHeroes; ++i) {
388 attackChoices[i] = AttackChoice(this);
391 monsterAttacks.clear();
395 void BattleState::HandleEvents(const Input &input) {
399 void BattleState::UpdateWorld(float deltaT) {
403 void BattleState::Render(SDL_Surface *screen) {
404 Vector<int> offset(CalculateScreenOffset(screen));
405 RenderBackground(screen, offset);
406 RenderMonsters(screen, offset);
409 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
411 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
413 destRect.x = offset.X();
414 destRect.y = offset.Y();
415 destRect.w = background->w;
416 destRect.h = background->h;
417 SDL_BlitSurface(background, 0, screen, &destRect);
420 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
421 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
422 if (MonsterPositionOccupied(i)) {
423 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
428 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
429 for (int i(0); i < numHeroes; ++i) {
430 if (heroes[i].AttackAnimation() && heroes[i].AttackAnimation()->Running()) {
431 heroes[i].AttackAnimation()->DrawCenter(screen, heroesPositions[i] + offset);
432 } else if (heroes[i].SpellAnimation() && heroes[i].SpellAnimation()->Running()) {
433 heroes[i].SpellAnimation()->DrawCenter(screen, heroesPositions[i] + offset);
435 int row(heroes[i].Health() > 0 ? 0 : 2);
436 heroes[i].Sprite()->DrawCenter(screen, heroesPositions[i] + offset, 1, row);
441 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
442 int tagHeight(attackTypeMenu.Height());
443 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
445 for (int i(0); i < numHeroes; ++i) {
446 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
450 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
451 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
452 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
456 rect.y = offset.Y() + Height() - tagHeight;
459 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
460 rect.y += res->normalFont->CharHeight() / 8;
461 rect.h -= res->normalFont->CharHeight() / 4;
462 SDL_FillRect(screen, &rect, res->heroesBgColor);
464 for (int i(0); i < numHeroes; ++i) {
465 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);