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;
33 using geometry::Vector;
41 void BattleState::AddMonster(const Monster &m) {
42 if (monsters.size() >= monstersLayout->NumPositions()) {
43 throw std::overflow_error("too many monsters for layout");
45 monsters.push_back(m);
48 void BattleState::AddHero(const Hero &h) {
49 if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
50 throw std::overflow_error("too many heroes for layout");
52 heroes[numHeroes] = h;
56 void BattleState::NextHero() {
58 while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
63 void BattleState::PreviousHero() {
65 while (activeHero >= 0 && heroes[activeHero].Health() == 0) {
70 void BattleState::SwapHeroes(int lhs, int rhs) {
71 if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
72 std::swap(heroes[lhs], heroes[rhs]);
76 void BattleState::Resize(int w, int h) {
81 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
82 for (int i(0); i < 4; ++i) {
83 heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
84 heroes[i].SpellMenu() = *res->spellMenuProperties;
85 heroes[i].UpdateSpellMenu();
86 heroes[i].IkariMenu() = *res->ikariMenuProperties;
87 heroes[i].UpdateIkariMenu(res);
88 heroTags[i] = HeroTag(this, i);
89 smallHeroTags[i] = SmallHeroTag(this, i);
92 for (int i(0); i < int(monsters.size()); ++i) {
93 monsters[i].Position() = monstersLayout->CalculatePosition(i, background->w, background->h);
96 int tagHeight(attackTypeMenu.Height());
97 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
98 int xOffset((Width() - 2 * tagWidth) / 2);
99 heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
100 heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
101 heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
102 heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
104 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
105 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
106 xOffset = (Width() - 4 * tagWidth) / 2;
107 int yOffset(Height() - tagHeight);
108 smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
109 smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
110 smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
111 smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
113 itemMenu = *res->itemMenuProperties;
117 void BattleState::LoadInventory() {
118 const Inventory &inv(*res->inventory);
120 itemMenu.Reserve(inv.MaxItems());
121 for (int i(0); i < inv.MaxItems(); ++i) {
122 const Item *item(inv.ItemAt(i));
124 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
126 itemMenu.AddEmptyEntry();
132 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
136 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
138 ctrl.PopState(); // quit the battle scene
149 // TODO: this should not push a state while quitting
150 if (AttackSelectionDone()) {
151 ctrl.PushState(new PerformAttacks(this));
153 ctrl.PushState(new SelectMoveAction(this));
157 bool BattleState::Victory() const {
158 for (int i(0); i < MaxMonsters(); ++i) {
159 if (MonsterAt(i).Health() > 0) return false;
164 bool BattleState::Defeat() const {
165 for (int i(0); i < NumHeroes(); ++i) {
166 if (HeroAt(i).Health() > 0) return false;
171 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
178 OrderCompare(BattleState *battle) : battle(battle) { }
179 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
180 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
181 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
188 void BattleState::CalculateAttackOrder() {
189 attackOrder.reserve(monsters.size() + NumHeroes());
190 for (int i(0); i < NumHeroes(); ++i) {
191 attackOrder.push_back(Order(i, false));
193 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
194 attackOrder.push_back(Order(i, true));
195 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
197 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
200 void BattleState::NextAttack() {
201 if (Victory() || Defeat()) {
202 attackCursor = attackOrder.size();
206 while (attackCursor < int(attackOrder.size())) {
207 if (attackOrder[attackCursor].isMonster) {
208 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
210 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
216 bool BattleState::AttacksFinished() const {
217 return attackCursor >= int(attackOrder.size())
218 || Victory() || Defeat();
221 void BattleState::CalculateDamage() {
222 if (CurrentAttack().isMonster) {
223 DecideMonsterAttack(MonsterAt(CurrentAttack().index));
225 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
226 if (ac.GetType() == AttackChoice::DEFEND) return;
227 TargetSelection &ts(ac.Selection());
229 if (CurrentAttack().isMonster) {
230 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
231 CalculateDamage(attackerStats, ts);
233 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
234 CalculateDamage(attackerStats, ts);
238 void BattleState::DecideMonsterAttack(Monster &m) const {
239 AttackChoice &ac(m.GetAttackChoice());
240 TargetSelection &ts(ac.Selection());
242 int target(rand() % NumHeroes());
243 while (!HeroPositionOccupied(target)) {
244 target = rand() % NumHeroes();
246 ac.SetType(AttackChoice::SWORD);
252 void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
254 if (ts.TargetsMonsters()) {
255 for (int i(0); i < MaxMonsters(); ++i) {
256 if (ts.IsSelected(i)) {
257 if (MonsterAt(i).Health() > 0) {
258 const Stats &defenderStats(MonsterAt(i).GetStats());
259 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
260 ts.SetBad(i, damage);
268 for (int i(0); i < MaxMonsters(); ++i) {
269 if (MonsterAt(i).Health() > 0) {
270 const Stats &defenderStats(MonsterAt(i).GetStats());
271 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
272 ts.SetBad(i, damage);
277 for (int i(0); i < NumHeroes(); ++i) {
278 if (ts.IsSelected(i)) {
279 if (HeroAt(i).Health() > 0) {
280 const Stats &defenderStats(HeroAt(i).GetStats());
281 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
282 ts.SetBad(i, damage);
290 for (int i(0); i < NumHeroes(); ++i) {
291 if (HeroAt(i).Health() > 0) {
292 const Stats &defenderStats(HeroAt(i).GetStats());
293 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
294 ts.SetBad(i, damage);
301 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
302 return attacker.Attack() / 2 - defender.Defense() / 4;
305 void BattleState::ApplyDamage() {
306 if (attackCursor < 0) return;
307 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
308 TargetSelection &ts(ac.Selection());
309 if (ts.TargetsMonsters()) {
310 for (int i(0); i < MaxMonsters(); ++i) {
311 Monster &monster(MonsterAt(i));
313 monster.SubtractHealth(ts.GetAmount(i));
314 if (monster.Health() == 0) {
315 expReward += monster.ExpReward();
316 goldReward += monster.GoldReward();
321 for (int i(0); i < NumHeroes(); ++i) {
322 Hero &hero(HeroAt(i));
324 hero.SubtractHealth(ts.GetAmount(i));
330 AttackChoice &BattleState::CurrentAttackAttackChoice() {
331 if (CurrentAttack().isMonster) {
332 return MonsterAt(CurrentAttack().index).GetAttackChoice();
334 return HeroAt(CurrentAttack().index).GetAttackChoice();
338 void BattleState::ClearAllAttacks() {
341 for (int i(0); i < NumHeroes(); ++i) {
342 HeroAt(i).GetAttackChoice() = AttackChoice(this);
344 for (int i(0); i < MaxMonsters(); ++i) {
345 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
351 void BattleState::HandleEvents(const Input &input) {
355 void BattleState::UpdateWorld(float deltaT) {
359 void BattleState::Render(SDL_Surface *screen) {
361 Vector<int> offset(CalculateScreenOffset(screen));
362 RenderBackground(screen, offset);
363 RenderMonsters(screen, offset);
366 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
369 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
371 destRect.x = offset.X();
372 destRect.y = offset.Y();
373 destRect.w = background->w;
374 destRect.h = background->h;
375 SDL_BlitSurface(background, 0, screen, &destRect);
378 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
380 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
381 if (MonsterPositionOccupied(i)) {
382 if (monsters[i].GetAnimation().Running()) {
383 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
385 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
391 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
393 for (int i(0); i < numHeroes; ++i) {
394 if (heroes[i].GetAnimation().Running()) {
395 heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
397 int row(heroes[i].Health() > 0 ? 0 : 2);
398 heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
403 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
405 int tagHeight(attackTypeMenu.Height());
406 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
408 for (int i(0); i < numHeroes; ++i) {
409 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
413 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
415 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
416 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
420 rect.y = offset.Y() + Height() - tagHeight;
423 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
424 rect.y += res->normalFont->CharHeight() / 8;
425 rect.h -= res->normalFont->CharHeight() / 4;
426 SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
428 for (int i(0); i < numHeroes; ++i) {
429 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);