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->spellMenuPrototype;
84 heroes[i].UpdateSpellMenu();
85 heroes[i].IkariMenu() = res->ikariMenuPrototype;
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->itemMenuPrototype;
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
141 // TODO: push victory state
146 // TODO: push defeat state
150 // TODO: this should not push a state while quitting
151 if (AttackSelectionDone()) {
152 ctrl.PushState(new PerformAttacks(this));
154 ctrl.PushState(new SelectMoveAction(this));
158 bool BattleState::Victory() const {
159 for (int i(0); i < MaxMonsters(); ++i) {
160 if (MonsterAt(i).Health() > 0) return false;
165 bool BattleState::Defeat() const {
166 for (int i(0); i < NumHeroes(); ++i) {
167 if (HeroAt(i).Health() > 0) return false;
172 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
179 OrderCompare(BattleState *battle) : battle(battle) { }
180 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
181 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
182 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
189 void BattleState::CalculateAttackOrder() {
190 attackOrder.reserve(monsters.size() + NumHeroes());
191 for (int i(0); i < NumHeroes(); ++i) {
192 attackOrder.push_back(Order(i, false));
194 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
195 attackOrder.push_back(Order(i, true));
196 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
198 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
201 void BattleState::NextAttack() {
202 if (Victory() || Defeat()) {
203 attackCursor = attackOrder.size();
207 while (attackCursor < int(attackOrder.size())) {
208 if (attackOrder[attackCursor].isMonster) {
209 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
211 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
217 bool BattleState::AttacksFinished() const {
218 return attackCursor >= int(attackOrder.size())
219 || Victory() || Defeat();
222 void BattleState::CalculateDamage() {
223 if (CurrentAttack().isMonster) {
224 DecideMonsterAttack(MonsterAt(CurrentAttack().index));
226 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
227 if (ac.GetType() == AttackChoice::DEFEND) return;
228 TargetSelection &ts(ac.Selection());
230 // TODO: this only evaluates SWORD type attacks
231 if (CurrentAttack().isMonster) {
232 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
233 CalculateDamage(attackerStats, ts);
235 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
236 CalculateDamage(attackerStats, ts);
240 void BattleState::DecideMonsterAttack(Monster &m) const {
241 AttackChoice &ac(m.GetAttackChoice());
242 TargetSelection &ts(ac.Selection());
244 // TODO: run monster's attack script
245 int target(rand() % NumHeroes());
246 while (!HeroPositionOccupied(target)) {
247 target = rand() % NumHeroes();
249 ac.SetType(AttackChoice::SWORD);
255 void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
257 if (ts.TargetsMonsters()) {
258 for (int i(0); i < MaxMonsters(); ++i) {
259 if (ts.IsSelected(i)) {
260 if (MonsterAt(i).Health() > 0) {
261 const Stats &defenderStats(MonsterAt(i).GetStats());
262 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
263 ts.SetBad(i, damage);
271 for (int i(0); i < MaxMonsters(); ++i) {
272 if (MonsterAt(i).Health() > 0) {
273 const Stats &defenderStats(MonsterAt(i).GetStats());
274 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
275 ts.SetBad(i, damage);
280 for (int i(0); i < NumHeroes(); ++i) {
281 if (ts.IsSelected(i)) {
282 if (HeroAt(i).Health() > 0) {
283 const Stats &defenderStats(HeroAt(i).GetStats());
284 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
285 ts.SetBad(i, damage);
293 for (int i(0); i < NumHeroes(); ++i) {
294 if (HeroAt(i).Health() > 0) {
295 const Stats &defenderStats(HeroAt(i).GetStats());
296 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
297 ts.SetBad(i, damage);
304 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
305 // TODO: find out real formula and add some randomness
306 return attacker.Attack() / 2 - defender.Defense() / 4;
309 void BattleState::ApplyDamage() {
310 if (attackCursor < 0) return;
311 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
312 TargetSelection &ts(ac.Selection());
313 if (ts.TargetsMonsters()) {
314 for (int i(0); i < MaxMonsters(); ++i) {
315 Monster &monster(MonsterAt(i));
317 monster.SubtractHealth(ts.GetAmount(i));
318 if (monster.Health() == 0) {
319 expReward += monster.ExpReward();
320 goldReward += monster.GoldReward();
325 for (int i(0); i < NumHeroes(); ++i) {
326 Hero &hero(HeroAt(i));
328 hero.SubtractHealth(ts.GetAmount(i));
334 AttackChoice &BattleState::CurrentAttackAttackChoice() {
335 if (CurrentAttack().isMonster) {
336 return MonsterAt(CurrentAttack().index).GetAttackChoice();
338 return HeroAt(CurrentAttack().index).GetAttackChoice();
342 void BattleState::ClearAllAttacks() {
345 for (int i(0); i < NumHeroes(); ++i) {
346 HeroAt(i).GetAttackChoice() = AttackChoice(this);
348 for (int i(0); i < MaxMonsters(); ++i) {
349 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
355 void BattleState::HandleEvents(const Input &input) {
359 void BattleState::UpdateWorld(float deltaT) {
363 void BattleState::Render(SDL_Surface *screen) {
365 Vector<int> offset(CalculateScreenOffset(screen));
366 RenderBackground(screen, offset);
367 RenderMonsters(screen, offset);
370 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
373 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
375 destRect.x = offset.X();
376 destRect.y = offset.Y();
377 destRect.w = background->w;
378 destRect.h = background->h;
379 SDL_BlitSurface(background, 0, screen, &destRect);
382 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
384 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
385 if (MonsterPositionOccupied(i)) {
386 if (monsters[i].GetAnimation().Running()) {
387 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
389 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
395 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
397 for (int i(0); i < numHeroes; ++i) {
398 if (heroes[i].GetAnimation().Running()) {
399 heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
401 int row(heroes[i].Health() > 0 ? 0 : 2);
402 heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
407 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
409 int tagHeight(attackTypeMenu.Height());
410 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
412 for (int i(0); i < numHeroes; ++i) {
413 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
417 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
419 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
420 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
424 rect.y = offset.Y() + Height() - tagHeight;
427 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
428 rect.y += res->normalFont->CharHeight() / 8;
429 rect.h -= res->normalFont->CharHeight() / 4;
430 SDL_FillRect(screen, &rect, res->heroesBgColor);
432 for (int i(0); i < numHeroes; ++i) {
433 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);