1 #include "BattleState.h"
3 #include "PartyLayout.h"
4 #include "states/SelectMoveAction.h"
5 #include "states/PerformAttacks.h"
6 #include "../app/Application.h"
7 #include "../app/Input.h"
8 #include "../common/GameState.h"
9 #include "../common/Ikari.h"
10 #include "../common/Inventory.h"
11 #include "../common/Item.h"
12 #include "../common/Spell.h"
13 #include "../graphics/Frame.h"
14 #include "../graphics/Sprite.h"
21 using app::Application;
23 using common::Inventory;
27 using geometry::Vector;
35 void BattleState::AddMonster(const Monster &m) {
36 if (monsters.size() >= monstersLayout->NumPositions()) {
37 throw std::overflow_error("too many monsters for layout");
39 monsters.push_back(m);
42 void BattleState::AddHero(const Hero &h) {
43 if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
44 throw std::overflow_error("too many heroes for layout");
46 heroes[numHeroes] = h;
50 void BattleState::NextHero() {
52 while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
57 void BattleState::PreviousHero() {
59 while (activeHero >= 0 && heroes[activeHero].Health() == 0) {
64 void BattleState::SwapHeroes(int lhs, int rhs) {
65 if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
66 std::swap(heroes[lhs], heroes[rhs]);
70 void BattleState::OnResize(int w, int h) {
75 void BattleState::OnEnterState(SDL_Surface *screen) {
76 for (int i(0); i < 4; ++i) {
77 heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
78 heroes[i].SpellMenu() = *res->spellMenuProperties;
79 heroes[i].UpdateSpellMenu();
80 heroes[i].IkariMenu() = *res->ikariMenuProperties;
81 heroes[i].UpdateIkariMenu(res);
82 heroTags[i] = HeroTag(this, i);
83 smallHeroTags[i] = SmallHeroTag(this, i);
86 for (int i(0); i < int(monsters.size()); ++i) {
87 monsters[i].Position() = monstersLayout->CalculatePosition(i, background->w, background->h);
90 int tagHeight(attackTypeMenu.Height());
91 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
92 int xOffset((Width() - 2 * tagWidth) / 2);
93 heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
94 heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
95 heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
96 heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
98 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
99 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
100 xOffset = (Width() - 4 * tagWidth) / 2;
101 int yOffset(Height() - tagHeight);
102 smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
103 smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
104 smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
105 smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
107 itemMenu = *res->itemMenuProperties;
112 void BattleState::LoadInventory() {
113 const Inventory &inv(game->state->inventory);
115 itemMenu.Reserve(inv.MaxItems());
116 for (int i(0); i < inv.MaxItems(); ++i) {
117 const Item *item(inv.ItemAt(i));
119 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
121 itemMenu.AddEmptyEntry();
126 void BattleState::OnExitState(SDL_Surface *screen) {
130 void BattleState::OnResumeState(SDL_Surface *screen) {
132 Ctrl().PopState(); // quit the battle scene
143 // TODO: this should not push a state while quitting
144 if (AttackSelectionDone()) {
145 Ctrl().PushState(new PerformAttacks(this));
147 Ctrl().PushState(new SelectMoveAction(this));
151 bool BattleState::Victory() const {
152 for (int i(0); i < MaxMonsters(); ++i) {
153 if (MonsterAt(i).Health() > 0) return false;
158 bool BattleState::Defeat() const {
159 for (int i(0); i < NumHeroes(); ++i) {
160 if (HeroAt(i).Health() > 0) return false;
165 void BattleState::OnPauseState(SDL_Surface *screen) {
172 OrderCompare(BattleState *battle) : battle(battle) { }
173 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
174 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
175 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
182 void BattleState::CalculateAttackOrder() {
183 attackOrder.reserve(monsters.size() + NumHeroes());
184 for (int i(0); i < NumHeroes(); ++i) {
185 attackOrder.push_back(Order(i, false));
187 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
188 attackOrder.push_back(Order(i, true));
189 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
191 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
194 void BattleState::NextAttack() {
195 if (Victory() || Defeat()) {
196 attackCursor = attackOrder.size();
200 while (attackCursor < int(attackOrder.size())) {
201 if (attackOrder[attackCursor].isMonster) {
202 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
204 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
210 bool BattleState::AttacksFinished() const {
211 return attackCursor >= int(attackOrder.size())
212 || Victory() || Defeat();
215 void BattleState::CalculateDamage() {
216 if (CurrentAttack().isMonster) {
217 DecideMonsterAttack(MonsterAt(CurrentAttack().index));
219 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
220 if (ac.GetType() == AttackChoice::DEFEND) return;
221 TargetSelection &ts(ac.Selection());
223 if (CurrentAttack().isMonster) {
224 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
225 CalculateDamage(attackerStats, ts);
227 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
228 CalculateDamage(attackerStats, ts);
232 void BattleState::DecideMonsterAttack(Monster &m) const {
233 AttackChoice &ac(m.GetAttackChoice());
234 TargetSelection &ts(ac.Selection());
236 int target(rand() % NumHeroes());
237 while (!HeroPositionOccupied(target)) {
238 target = rand() % NumHeroes();
240 ac.SetType(AttackChoice::SWORD);
246 void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
248 if (ts.TargetsMonsters()) {
249 for (int i(0); i < MaxMonsters(); ++i) {
250 if (ts.IsSelected(i)) {
251 if (MonsterAt(i).Health() > 0) {
252 const Stats &defenderStats(MonsterAt(i).GetStats());
253 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
254 ts.SetBad(i, damage);
262 for (int i(0); i < MaxMonsters(); ++i) {
263 if (MonsterAt(i).Health() > 0) {
264 const Stats &defenderStats(MonsterAt(i).GetStats());
265 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
266 ts.SetBad(i, damage);
271 for (int i(0); i < NumHeroes(); ++i) {
272 if (ts.IsSelected(i)) {
273 if (HeroAt(i).Health() > 0) {
274 const Stats &defenderStats(HeroAt(i).GetStats());
275 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
276 ts.SetBad(i, damage);
284 for (int i(0); i < NumHeroes(); ++i) {
285 if (HeroAt(i).Health() > 0) {
286 const Stats &defenderStats(HeroAt(i).GetStats());
287 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
288 ts.SetBad(i, damage);
295 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
296 return attacker.Attack() / 2 - defender.Defense() / 4;
299 void BattleState::ApplyDamage() {
300 if (attackCursor < 0) return;
301 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
302 TargetSelection &ts(ac.Selection());
303 if (ts.TargetsMonsters()) {
304 for (int i(0); i < MaxMonsters(); ++i) {
305 Monster &monster(MonsterAt(i));
307 monster.SubtractHealth(ts.GetAmount(i));
308 if (monster.Health() == 0) {
309 expReward += monster.ExpReward();
310 goldReward += monster.GoldReward();
315 for (int i(0); i < NumHeroes(); ++i) {
316 Hero &hero(HeroAt(i));
318 hero.SubtractHealth(ts.GetAmount(i));
324 AttackChoice &BattleState::CurrentAttackAttackChoice() {
325 if (CurrentAttack().isMonster) {
326 return MonsterAt(CurrentAttack().index).GetAttackChoice();
328 return HeroAt(CurrentAttack().index).GetAttackChoice();
332 void BattleState::ClearAllAttacks() {
335 for (int i(0); i < NumHeroes(); ++i) {
336 HeroAt(i).GetAttackChoice() = AttackChoice(this);
338 for (int i(0); i < MaxMonsters(); ++i) {
339 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
345 void BattleState::HandleEvents(const Input &input) {
349 void BattleState::UpdateWorld(float deltaT) {
353 void BattleState::Render(SDL_Surface *screen) {
355 Vector<int> offset(CalculateScreenOffset(screen));
356 RenderBackground(screen, offset);
357 RenderMonsters(screen, offset);
360 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
363 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
365 destRect.x = offset.X();
366 destRect.y = offset.Y();
367 destRect.w = background->w;
368 destRect.h = background->h;
369 SDL_BlitSurface(background, 0, screen, &destRect);
372 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
374 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
375 if (MonsterPositionOccupied(i)) {
376 if (monsters[i].GetAnimation().Running()) {
377 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
379 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
385 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
387 for (int i(0); i < numHeroes; ++i) {
388 if (heroes[i].GetAnimation().Running()) {
389 heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
391 int row(heroes[i].Health() > 0 ? 0 : 2);
392 heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
397 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
399 int tagHeight(attackTypeMenu.Height());
400 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
402 for (int i(0); i < numHeroes; ++i) {
403 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
407 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
409 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
410 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
414 rect.y = offset.Y() + Height() - tagHeight;
417 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
418 rect.y += res->normalFont->CharHeight() / 8;
419 rect.h -= res->normalFont->CharHeight() / 4;
420 SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
422 for (int i(0); i < numHeroes; ++i) {
423 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);