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 heroes[i].SpellMenu() = res->spellMenuPrototype;
78 heroes[i].UpdateSpellMenu();
79 heroes[i].IkariMenu() = res->ikariMenuPrototype;
80 heroes[i].UpdateIkariMenu(res);
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::LoadInventory() {
107 const Inventory &inv(*res->inventory);
109 itemMenu.Reserve(inv.MaxItems());
110 for (int i(0); i < inv.MaxItems(); ++i) {
111 const Item *item(inv.ItemAt(i));
113 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
115 itemMenu.AddEmptyEntry();
121 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
125 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
127 ctrl.PopState(); // quit the battle scene
131 // TODO: push victory state
136 // TODO: push defeat state
140 // TODO: this should not push a state while quitting
141 if (AttackSelectionDone()) {
142 ctrl.PushState(new PerformAttacks(this));
144 ctrl.PushState(new SelectMoveAction(this));
148 bool BattleState::Victory() const {
149 for (int i(0); i < MaxMonsters(); ++i) {
150 if (MonsterAt(i).Health() > 0) return false;
155 bool BattleState::Defeat() const {
156 for (int i(0); i < NumHeroes(); ++i) {
157 if (HeroAt(i).Health() > 0) return false;
162 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
169 OrderCompare(BattleState *battle) : battle(battle) { }
170 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
171 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
172 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
179 void BattleState::CalculateAttackOrder() {
180 attackOrder.reserve(monsters.size() + NumHeroes());
181 for (int i(0); i < NumHeroes(); ++i) {
182 attackOrder.push_back(Order(i, false));
184 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
185 attackOrder.push_back(Order(i, true));
187 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
189 monsterAttacks.resize(monsters.size(), AttackChoice(this));
192 void BattleState::NextAttack() {
193 if (Victory() || Defeat()) {
194 attackCursor = attackOrder.size();
198 while (attackCursor < int(attackOrder.size())) {
199 if (attackOrder[attackCursor].isMonster) {
200 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
202 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
208 bool BattleState::AttacksFinished() const {
209 return attackCursor >= int(attackOrder.size())
210 || Victory() || Defeat();
213 void BattleState::CalculateDamage() {
214 AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
215 if (ac.GetType() == AttackChoice::DEFEND) return;
217 if (CurrentAttack().isMonster) {
218 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
219 // TODO: run monster's attack script
220 ac.SetType(AttackChoice::SWORD);
221 ac.Selection().SelectSingle();
222 ac.Selection().SelectHeroes();
223 for (int i(0); i < NumHeroes(); ++i) {
224 if (HeroAt(i).Health() > 0) {
225 const Stats &defenderStats(HeroAt(i).GetStats());
226 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
227 ac.Selection().SetBad(0, damage);
232 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
233 TargetSelection &ts(ac.Selection());
235 if (ts.TargetsEnemies()) {
236 for (int i(0); i < MaxMonsters(); ++i) {
237 if (ts.IsSelected(i)) {
238 if (MonsterAt(i).Health() > 0) {
239 const Stats &defenderStats(MonsterAt(i).GetStats());
240 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
241 ts.SetBad(i, damage);
249 for (int i(0); i < MaxMonsters(); ++i) {
250 if (MonsterAt(i).Health() > 0) {
251 const Stats &defenderStats(MonsterAt(i).GetStats());
252 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
253 ts.SetBad(i, damage);
258 for (int i(0); i < NumHeroes(); ++i) {
259 if (ts.IsSelected(i)) {
260 if (HeroAt(i).Health() > 0) {
261 const Stats &defenderStats(HeroAt(i).GetStats());
262 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
263 ts.SetBad(i, damage);
271 for (int i(0); i < NumHeroes(); ++i) {
272 if (HeroAt(i).Health() > 0) {
273 const Stats &defenderStats(HeroAt(i).GetStats());
274 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
275 ts.SetBad(i, damage);
283 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
284 // TODO: find out real formula and add some randomness
285 return attacker.Attack() / 2 - defender.Defense() / 4;
288 void BattleState::ApplyDamage() {
289 if (attackCursor < 0) return;
290 AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
291 TargetSelection &ts(ac.Selection());
292 if (ts.TargetsEnemies()) {
293 for (int i(0); i < MaxMonsters(); ++i) {
294 Monster &monster(MonsterAt(i));
296 monster.SubtractHealth(ts.GetAmount(i));
297 if (monster.Health() == 0) {
298 expReward += monster.ExpReward();
299 goldReward += monster.GoldReward();
304 for (int i(0); i < NumHeroes(); ++i) {
305 Hero &hero(HeroAt(i));
307 hero.SubtractHealth(ts.GetAmount(i));
313 AttackChoice &BattleState::CurrentAttackAttackChoice() {
314 if (CurrentAttack().isMonster) {
315 return monsterAttacks[CurrentAttack().index];
317 return AttackChoiceAt(CurrentAttack().index);
321 void BattleState::ClearAllAttacks() {
324 for (int i(0); i < numHeroes; ++i) {
325 heroes[i].GetAttackChoice() = AttackChoice(this);
328 monsterAttacks.clear();
332 void BattleState::HandleEvents(const Input &input) {
336 void BattleState::UpdateWorld(float deltaT) {
340 void BattleState::Render(SDL_Surface *screen) {
342 Vector<int> offset(CalculateScreenOffset(screen));
343 RenderBackground(screen, offset);
344 RenderMonsters(screen, offset);
347 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
350 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
352 destRect.x = offset.X();
353 destRect.y = offset.Y();
354 destRect.w = background->w;
355 destRect.h = background->h;
356 SDL_BlitSurface(background, 0, screen, &destRect);
359 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
361 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
362 if (MonsterPositionOccupied(i)) {
363 if (monsters[i].GetAnimation().Running()) {
364 monsters[i].GetAnimation().DrawCenter(screen, monsterPositions[i] + offset);
366 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
372 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
374 for (int i(0); i < numHeroes; ++i) {
375 if (heroes[i].GetAnimation().Running()) {
376 heroes[i].GetAnimation().DrawCenter(screen, heroesPositions[i] + offset);
378 int row(heroes[i].Health() > 0 ? 0 : 2);
379 heroes[i].Sprite()->DrawCenter(screen, heroesPositions[i] + offset, 1, row);
384 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
386 int tagHeight(attackTypeMenu.Height());
387 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
389 for (int i(0); i < numHeroes; ++i) {
390 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
394 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
396 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
397 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
401 rect.y = offset.Y() + Height() - tagHeight;
404 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
405 rect.y += res->normalFont->CharHeight() / 8;
406 rect.h -= res->normalFont->CharHeight() / 4;
407 SDL_FillRect(screen, &rect, res->heroesBgColor);
409 for (int i(0); i < numHeroes; ++i) {
410 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);