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 "../graphics/Frame.h"
20 #include "../graphics/Sprite.h"
27 using app::Application;
29 using common::Inventory;
32 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::PreviousHero() {
64 while (activeHero >= 0 && heroes[activeHero].Health() == 0) {
69 void BattleState::SwapHeroes(int lhs, int rhs) {
70 if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
71 std::swap(heroes[lhs], heroes[rhs]);
75 void BattleState::Resize(int w, int h) {
80 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
81 for (int i(0); i < 4; ++i) {
82 heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
83 heroes[i].SpellMenu() = *res->spellMenuProperties;
84 heroes[i].UpdateSpellMenu();
85 heroes[i].IkariMenu() = *res->ikariMenuProperties;
86 heroes[i].UpdateIkariMenu(res);
87 heroTags[i] = HeroTag(this, i);
88 smallHeroTags[i] = SmallHeroTag(this, i);
91 for (int i(0); i < int(monsters.size()); ++i) {
92 monsters[i].Position() = monstersLayout->CalculatePosition(i, background->w, background->h);
95 int tagHeight(attackTypeMenu.Height());
96 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
97 int xOffset((Width() - 2 * tagWidth) / 2);
98 heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
99 heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
100 heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
101 heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
103 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
104 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
105 xOffset = (Width() - 4 * tagWidth) / 2;
106 int yOffset(Height() - tagHeight);
107 smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
108 smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
109 smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
110 smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
112 itemMenu = *res->itemMenuProperties;
116 void BattleState::LoadInventory() {
117 const Inventory &inv(*res->inventory);
119 itemMenu.Reserve(inv.MaxItems());
120 for (int i(0); i < inv.MaxItems(); ++i) {
121 const Item *item(inv.ItemAt(i));
123 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
125 itemMenu.AddEmptyEntry();
131 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
135 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
137 ctrl.PopState(); // quit the battle scene
148 // TODO: this should not push a state while quitting
149 if (AttackSelectionDone()) {
150 ctrl.PushState(new PerformAttacks(this));
152 ctrl.PushState(new SelectMoveAction(this));
156 bool BattleState::Victory() const {
157 for (int i(0); i < MaxMonsters(); ++i) {
158 if (MonsterAt(i).Health() > 0) return false;
163 bool BattleState::Defeat() const {
164 for (int i(0); i < NumHeroes(); ++i) {
165 if (HeroAt(i).Health() > 0) return false;
170 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
177 OrderCompare(BattleState *battle) : battle(battle) { }
178 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
179 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
180 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
187 void BattleState::CalculateAttackOrder() {
188 attackOrder.reserve(monsters.size() + NumHeroes());
189 for (int i(0); i < NumHeroes(); ++i) {
190 attackOrder.push_back(Order(i, false));
192 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
193 attackOrder.push_back(Order(i, true));
194 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
196 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
199 void BattleState::NextAttack() {
200 if (Victory() || Defeat()) {
201 attackCursor = attackOrder.size();
205 while (attackCursor < int(attackOrder.size())) {
206 if (attackOrder[attackCursor].isMonster) {
207 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
209 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
215 bool BattleState::AttacksFinished() const {
216 return attackCursor >= int(attackOrder.size())
217 || Victory() || Defeat();
220 void BattleState::CalculateDamage() {
221 if (CurrentAttack().isMonster) {
222 DecideMonsterAttack(MonsterAt(CurrentAttack().index));
224 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
225 if (ac.GetType() == AttackChoice::DEFEND) return;
226 TargetSelection &ts(ac.Selection());
228 if (CurrentAttack().isMonster) {
229 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
230 CalculateDamage(attackerStats, ts);
232 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
233 CalculateDamage(attackerStats, ts);
237 void BattleState::DecideMonsterAttack(Monster &m) const {
238 AttackChoice &ac(m.GetAttackChoice());
239 TargetSelection &ts(ac.Selection());
241 int target(rand() % NumHeroes());
242 while (!HeroPositionOccupied(target)) {
243 target = rand() % NumHeroes();
245 ac.SetType(AttackChoice::SWORD);
251 void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
253 if (ts.TargetsMonsters()) {
254 for (int i(0); i < MaxMonsters(); ++i) {
255 if (ts.IsSelected(i)) {
256 if (MonsterAt(i).Health() > 0) {
257 const Stats &defenderStats(MonsterAt(i).GetStats());
258 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
259 ts.SetBad(i, damage);
267 for (int i(0); i < MaxMonsters(); ++i) {
268 if (MonsterAt(i).Health() > 0) {
269 const Stats &defenderStats(MonsterAt(i).GetStats());
270 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
271 ts.SetBad(i, damage);
276 for (int i(0); i < NumHeroes(); ++i) {
277 if (ts.IsSelected(i)) {
278 if (HeroAt(i).Health() > 0) {
279 const Stats &defenderStats(HeroAt(i).GetStats());
280 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
281 ts.SetBad(i, damage);
289 for (int i(0); i < NumHeroes(); ++i) {
290 if (HeroAt(i).Health() > 0) {
291 const Stats &defenderStats(HeroAt(i).GetStats());
292 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
293 ts.SetBad(i, damage);
300 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
301 return attacker.Attack() / 2 - defender.Defense() / 4;
304 void BattleState::ApplyDamage() {
305 if (attackCursor < 0) return;
306 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
307 TargetSelection &ts(ac.Selection());
308 if (ts.TargetsMonsters()) {
309 for (int i(0); i < MaxMonsters(); ++i) {
310 Monster &monster(MonsterAt(i));
312 monster.SubtractHealth(ts.GetAmount(i));
313 if (monster.Health() == 0) {
314 expReward += monster.ExpReward();
315 goldReward += monster.GoldReward();
320 for (int i(0); i < NumHeroes(); ++i) {
321 Hero &hero(HeroAt(i));
323 hero.SubtractHealth(ts.GetAmount(i));
329 AttackChoice &BattleState::CurrentAttackAttackChoice() {
330 if (CurrentAttack().isMonster) {
331 return MonsterAt(CurrentAttack().index).GetAttackChoice();
333 return HeroAt(CurrentAttack().index).GetAttackChoice();
337 void BattleState::ClearAllAttacks() {
340 for (int i(0); i < NumHeroes(); ++i) {
341 HeroAt(i).GetAttackChoice() = AttackChoice(this);
343 for (int i(0); i < MaxMonsters(); ++i) {
344 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
350 void BattleState::HandleEvents(const Input &input) {
354 void BattleState::UpdateWorld(float deltaT) {
358 void BattleState::Render(SDL_Surface *screen) {
360 Vector<int> offset(CalculateScreenOffset(screen));
361 RenderBackground(screen, offset);
362 RenderMonsters(screen, offset);
365 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
368 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
370 destRect.x = offset.X();
371 destRect.y = offset.Y();
372 destRect.w = background->w;
373 destRect.h = background->h;
374 SDL_BlitSurface(background, 0, screen, &destRect);
377 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
379 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
380 if (MonsterPositionOccupied(i)) {
381 if (monsters[i].GetAnimation().Running()) {
382 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
384 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
390 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
392 for (int i(0); i < numHeroes; ++i) {
393 if (heroes[i].GetAnimation().Running()) {
394 heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
396 int row(heroes[i].Health() > 0 ? 0 : 2);
397 heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
402 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
404 int tagHeight(attackTypeMenu.Height());
405 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
407 for (int i(0); i < numHeroes; ++i) {
408 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
412 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
414 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
415 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
419 rect.y = offset.Y() + Height() - tagHeight;
422 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
423 rect.y += res->normalFont->CharHeight() / 8;
424 rect.h -= res->normalFont->CharHeight() / 4;
425 SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
427 for (int i(0); i < numHeroes; ++i) {
428 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);