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"
27 using app::Application;
29 using common::Inventory;
32 using geometry::Point;
33 using geometry::Vector;
40 void BattleState::AddMonster(const Monster &m) {
41 if (monsters.size() >= monstersLayout->NumPositions()) {
42 throw std::overflow_error("too many monsters for layout");
44 monsters.push_back(m);
47 void BattleState::AddHero(const Hero &h) {
48 if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
49 throw std::overflow_error("too many heroes for layout");
51 heroes[numHeroes] = h;
55 void BattleState::NextHero() {
57 while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
62 void BattleState::SwapHeroes(int lhs, int rhs) {
63 if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
64 std::swap(heroes[lhs], heroes[rhs]);
68 void BattleState::Resize(int w, int h) {
73 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
74 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
75 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
76 for (int i(0); i < 4; ++i) {
77 spellMenus[i] = res->spellMenuPrototype;
79 ikariMenus[i] = res->ikariMenuPrototype;
81 heroTags[i] = HeroTag(this, i);
82 smallHeroTags[i] = SmallHeroTag(this, i);
85 int tagHeight(attackTypeMenu.Height());
86 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
87 int xOffset((Width() - 2 * tagWidth) / 2);
88 heroTagPositions[0] = Point<int>(xOffset, Height() - 2 * tagHeight);
89 heroTagPositions[1] = Point<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
90 heroTagPositions[2] = Point<int>(xOffset, Height() - tagHeight);
91 heroTagPositions[3] = Point<int>(xOffset + tagWidth, Height() - tagHeight);
93 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
94 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
95 xOffset = (Width() - 4 * tagWidth) / 2;
96 int yOffset(Height() - tagHeight);
97 smallHeroTagPositions[0] = Point<int>(xOffset, yOffset);
98 smallHeroTagPositions[1] = Point<int>(xOffset + 2 * tagWidth, yOffset);
99 smallHeroTagPositions[2] = Point<int>(xOffset + tagWidth, yOffset);
100 smallHeroTagPositions[3] = Point<int>(xOffset + 3 * tagWidth, yOffset);
102 itemMenu = res->itemMenuPrototype;
106 void BattleState::LoadSpellMenu(vector<Hero>::size_type index) {
107 assert(index >= 0 && index < 4);
108 spellMenus[index].Clear();
109 spellMenus[index].Reserve(HeroAt(index).Spells().size());
110 for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
111 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
112 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
116 void BattleState::LoadIkariMenu(vector<Hero>::size_type index) {
117 assert(index >= 0 && index < 4);
118 ikariMenus[index].Clear();
119 ikariMenus[index].Reserve(6);
121 if (HeroAt(index).HasWeapon()) {
122 ikariMenus[index].Add(
123 HeroAt(index).Weapon()->Name(),
124 HeroAt(index).Weapon(),
125 HeroAt(index).Weapon()->HasIkari() && HeroAt(index).Weapon()->GetIkari()->Cost() <= HeroAt(index).IP(),
128 HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : "");
130 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon);
133 if (HeroAt(index).HasArmor()) {
134 ikariMenus[index].Add(
135 HeroAt(index).Armor()->Name(),
136 HeroAt(index).Armor(),
137 HeroAt(index).Armor()->HasIkari() && HeroAt(index).Armor()->GetIkari()->Cost() <= HeroAt(index).IP(),
140 HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : "");
142 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon);
145 if (HeroAt(index).HasShield()) {
146 ikariMenus[index].Add(
147 HeroAt(index).Shield()->Name(),
148 HeroAt(index).Shield(),
149 HeroAt(index).Shield()->HasIkari() && HeroAt(index).Shield()->GetIkari()->Cost() <= HeroAt(index).IP(),
152 HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : "");
154 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon);
157 if (HeroAt(index).HasHelmet()) {
158 ikariMenus[index].Add(
159 HeroAt(index).Helmet()->Name(),
160 HeroAt(index).Helmet(),
161 HeroAt(index).Helmet()->HasIkari() && HeroAt(index).Helmet()->GetIkari()->Cost() <= HeroAt(index).IP(),
164 HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : "");
166 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon);
169 if (HeroAt(index).HasRing()) {
170 ikariMenus[index].Add(
171 HeroAt(index).Ring()->Name(),
172 HeroAt(index).Ring(),
173 HeroAt(index).Ring()->HasIkari() && HeroAt(index).Ring()->GetIkari()->Cost() <= HeroAt(index).IP(),
176 HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : "");
178 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon);
181 if (HeroAt(index).HasJewel()) {
182 ikariMenus[index].Add(
183 HeroAt(index).Jewel()->Name(),
184 HeroAt(index).Jewel(),
185 HeroAt(index).Jewel()->HasIkari() && HeroAt(index).Jewel()->GetIkari()->Cost() <= HeroAt(index).IP(),
188 HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : "");
190 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon);
194 void BattleState::LoadInventory() {
195 const Inventory &inv(*res->inventory);
197 itemMenu.Reserve(inv.MaxItems());
198 for (int i(0); i < inv.MaxItems(); ++i) {
199 const Item *item(inv.ItemAt(i));
201 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
203 itemMenu.AddEmptyEntry();
209 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
213 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
215 ctrl.PopState(); // quit the battle scene
219 // TODO: push victory state
224 // TODO: push defeat state
228 // TODO: this should not push a state while quitting
229 if (AttackSelectionDone()) {
230 ctrl.PushState(new PerformAttacks(this));
232 ctrl.PushState(new SelectMoveAction(this));
236 bool BattleState::Victory() const {
237 for (int i(0); i < MaxMonsters(); ++i) {
238 if (MonsterAt(i).Health() > 0) return false;
243 bool BattleState::Defeat() const {
244 for (int i(0); i < NumHeroes(); ++i) {
245 if (HeroAt(i).Health() > 0) return false;
250 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
257 OrderCompare(BattleState *battle) : battle(battle) { }
258 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
259 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
260 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
267 void BattleState::CalculateAttackOrder() {
268 attackOrder.reserve(monsters.size() + NumHeroes());
269 for (int i(0); i < NumHeroes(); ++i) {
270 attackOrder.push_back(Order(i, false));
272 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
273 attackOrder.push_back(Order(i, true));
275 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
277 monsterAttacks.resize(monsters.size(), AttackChoice(this));
280 void BattleState::NextAttack() {
281 if (Victory() || Defeat()) {
282 attackCursor = attackOrder.size();
286 while (attackCursor < int(attackOrder.size())) {
287 if (attackOrder[attackCursor].isMonster) {
288 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
290 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
296 bool BattleState::AttacksFinished() const {
297 return attackCursor >= int(attackOrder.size())
298 || Victory() || Defeat();
301 void BattleState::CalculateDamage() {
302 AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
303 if (ac.GetType() == AttackChoice::DEFEND) return;
305 if (CurrentAttack().isMonster) {
306 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
307 // TODO: run monster's attack script
308 ac.SetType(AttackChoice::SWORD);
309 ac.Selection().SelectSingle();
310 ac.Selection().SelectHeroes();
311 for (int i(0); i < NumHeroes(); ++i) {
312 if (HeroAt(i).Health() > 0) {
313 const Stats &defenderStats(HeroAt(i).GetStats());
314 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
315 ac.Selection().SetBad(0, damage);
320 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
321 TargetSelection &ts(ac.Selection());
323 if (ts.TargetsEnemies()) {
324 for (int i(0); i < MaxMonsters(); ++i) {
325 if (ts.IsSelected(i)) {
326 if (MonsterAt(i).Health() > 0) {
327 const Stats &defenderStats(MonsterAt(i).GetStats());
328 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
329 ts.SetBad(i, damage);
337 for (int i(0); i < MaxMonsters(); ++i) {
338 if (MonsterAt(i).Health() > 0) {
339 const Stats &defenderStats(MonsterAt(i).GetStats());
340 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
341 ts.SetBad(i, damage);
346 for (int i(0); i < NumHeroes(); ++i) {
347 if (ts.IsSelected(i)) {
348 if (HeroAt(i).Health() > 0) {
349 const Stats &defenderStats(HeroAt(i).GetStats());
350 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
351 ts.SetBad(i, damage);
359 for (int i(0); i < NumHeroes(); ++i) {
360 if (HeroAt(i).Health() > 0) {
361 const Stats &defenderStats(HeroAt(i).GetStats());
362 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
363 ts.SetBad(i, damage);
371 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
372 // TODO: find out real formula and add some randomness
373 return attacker.Attack() / 2 - defender.Defense() / 4;
376 void BattleState::ApplyDamage() {
377 if (attackCursor < 0) return;
378 AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
379 TargetSelection &ts(ac.Selection());
380 if (ts.TargetsEnemies()) {
381 for (int i(0); i < MaxMonsters(); ++i) {
382 Monster &monster(MonsterAt(i));
384 monster.SubtractHealth(ts.GetAmount(i));
385 if (monster.Health() == 0) {
386 expReward += monster.ExpReward();
387 goldReward += monster.GoldReward();
392 for (int i(0); i < NumHeroes(); ++i) {
393 Hero &hero(HeroAt(i));
395 hero.SubtractHealth(ts.GetAmount(i));
401 AttackChoice &BattleState::CurrentAttackAttackChoice() {
402 if (CurrentAttack().isMonster) {
403 return monsterAttacks[CurrentAttack().index];
405 return AttackChoiceAt(CurrentAttack().index);
409 void BattleState::ClearAllAttacks() {
412 for (int i(0); i < numHeroes; ++i) {
413 attackChoices[i] = AttackChoice(this);
416 monsterAttacks.clear();
420 void BattleState::HandleEvents(const Input &input) {
424 void BattleState::UpdateWorld(float deltaT) {
428 void BattleState::Render(SDL_Surface *screen) {
430 Vector<int> offset(CalculateScreenOffset(screen));
431 RenderBackground(screen, offset);
432 RenderMonsters(screen, offset);
435 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
438 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
440 destRect.x = offset.X();
441 destRect.y = offset.Y();
442 destRect.w = background->w;
443 destRect.h = background->h;
444 SDL_BlitSurface(background, 0, screen, &destRect);
447 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
449 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
450 if (MonsterPositionOccupied(i)) {
451 // TODO: better solution for running animations
452 if (monsters[i].AttackAnimation() && monsters[i].AttackAnimation()->Running()) {
453 monsters[i].AttackAnimation()->DrawCenter(screen, monsterPositions[i] + offset);
454 } else if (monsters[i].SpellAnimation() && monsters[i].SpellAnimation()->Running()) {
455 monsters[i].SpellAnimation()->DrawCenter(screen, monsterPositions[i] + offset);
457 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
463 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
465 for (int i(0); i < numHeroes; ++i) {
466 if (heroes[i].AttackAnimation() && heroes[i].AttackAnimation()->Running()) {
467 heroes[i].AttackAnimation()->DrawCenter(screen, heroesPositions[i] + offset);
468 } else if (heroes[i].SpellAnimation() && heroes[i].SpellAnimation()->Running()) {
469 heroes[i].SpellAnimation()->DrawCenter(screen, heroesPositions[i] + offset);
471 int row(heroes[i].Health() > 0 ? 0 : 2);
472 heroes[i].Sprite()->DrawCenter(screen, heroesPositions[i] + offset, 1, row);
477 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
479 int tagHeight(attackTypeMenu.Height());
480 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
482 for (int i(0); i < numHeroes; ++i) {
483 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
487 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
489 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
490 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
494 rect.y = offset.Y() + Height() - tagHeight;
497 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
498 rect.y += res->normalFont->CharHeight() / 8;
499 rect.h -= res->normalFont->CharHeight() / 4;
500 SDL_FillRect(screen, &rect, res->heroesBgColor);
502 for (int i(0); i < numHeroes; ++i) {
503 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);