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::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 monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
82 heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
83 for (int i(0); i < 4; ++i) {
84 heroes[i].SpellMenu() = res->spellMenuPrototype;
85 heroes[i].UpdateSpellMenu();
86 heroes[i].IkariMenu() = res->ikariMenuPrototype;
87 heroes[i].UpdateIkariMenu(res);
88 heroTags[i] = HeroTag(this, i);
89 smallHeroTags[i] = SmallHeroTag(this, i);
92 int tagHeight(attackTypeMenu.Height());
93 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
94 int xOffset((Width() - 2 * tagWidth) / 2);
95 heroTagPositions[0] = Point<int>(xOffset, Height() - 2 * tagHeight);
96 heroTagPositions[1] = Point<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
97 heroTagPositions[2] = Point<int>(xOffset, Height() - tagHeight);
98 heroTagPositions[3] = Point<int>(xOffset + tagWidth, Height() - tagHeight);
100 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
101 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
102 xOffset = (Width() - 4 * tagWidth) / 2;
103 int yOffset(Height() - tagHeight);
104 smallHeroTagPositions[0] = Point<int>(xOffset, yOffset);
105 smallHeroTagPositions[1] = Point<int>(xOffset + 2 * tagWidth, yOffset);
106 smallHeroTagPositions[2] = Point<int>(xOffset + tagWidth, yOffset);
107 smallHeroTagPositions[3] = Point<int>(xOffset + 3 * tagWidth, yOffset);
109 itemMenu = res->itemMenuPrototype;
113 void BattleState::LoadInventory() {
114 const Inventory &inv(*res->inventory);
116 itemMenu.Reserve(inv.MaxItems());
117 for (int i(0); i < inv.MaxItems(); ++i) {
118 const Item *item(inv.ItemAt(i));
120 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
122 itemMenu.AddEmptyEntry();
128 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
132 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
134 ctrl.PopState(); // quit the battle scene
138 // TODO: push victory state
143 // TODO: push defeat state
147 // TODO: this should not push a state while quitting
148 if (AttackSelectionDone()) {
149 ctrl.PushState(new PerformAttacks(this));
151 ctrl.PushState(new SelectMoveAction(this));
155 bool BattleState::Victory() const {
156 for (int i(0); i < MaxMonsters(); ++i) {
157 if (MonsterAt(i).Health() > 0) return false;
162 bool BattleState::Defeat() const {
163 for (int i(0); i < NumHeroes(); ++i) {
164 if (HeroAt(i).Health() > 0) return false;
169 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
176 OrderCompare(BattleState *battle) : battle(battle) { }
177 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
178 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
179 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
186 void BattleState::CalculateAttackOrder() {
187 attackOrder.reserve(monsters.size() + NumHeroes());
188 for (int i(0); i < NumHeroes(); ++i) {
189 attackOrder.push_back(Order(i, false));
191 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
192 attackOrder.push_back(Order(i, true));
194 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
196 monsterAttacks.resize(monsters.size(), AttackChoice(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 AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
222 if (ac.GetType() == AttackChoice::DEFEND) return;
224 if (CurrentAttack().isMonster) {
225 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
226 // TODO: run monster's attack script
227 ac.SetType(AttackChoice::SWORD);
228 ac.Selection().SelectSingle();
229 ac.Selection().SelectHeroes();
230 for (int i(0); i < NumHeroes(); ++i) {
231 if (HeroAt(i).Health() > 0) {
232 const Stats &defenderStats(HeroAt(i).GetStats());
233 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
234 ac.Selection().SetBad(0, damage);
239 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
240 TargetSelection &ts(ac.Selection());
242 if (ts.TargetsEnemies()) {
243 for (int i(0); i < MaxMonsters(); ++i) {
244 if (ts.IsSelected(i)) {
245 if (MonsterAt(i).Health() > 0) {
246 const Stats &defenderStats(MonsterAt(i).GetStats());
247 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
248 ts.SetBad(i, damage);
256 for (int i(0); i < MaxMonsters(); ++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);
265 for (int i(0); i < NumHeroes(); ++i) {
266 if (ts.IsSelected(i)) {
267 if (HeroAt(i).Health() > 0) {
268 const Stats &defenderStats(HeroAt(i).GetStats());
269 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
270 ts.SetBad(i, damage);
278 for (int i(0); i < NumHeroes(); ++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 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
291 // TODO: find out real formula and add some randomness
292 return attacker.Attack() / 2 - defender.Defense() / 4;
295 void BattleState::ApplyDamage() {
296 if (attackCursor < 0) return;
297 AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
298 TargetSelection &ts(ac.Selection());
299 if (ts.TargetsEnemies()) {
300 for (int i(0); i < MaxMonsters(); ++i) {
301 Monster &monster(MonsterAt(i));
303 monster.SubtractHealth(ts.GetAmount(i));
304 if (monster.Health() == 0) {
305 expReward += monster.ExpReward();
306 goldReward += monster.GoldReward();
311 for (int i(0); i < NumHeroes(); ++i) {
312 Hero &hero(HeroAt(i));
314 hero.SubtractHealth(ts.GetAmount(i));
320 AttackChoice &BattleState::CurrentAttackAttackChoice() {
321 if (CurrentAttack().isMonster) {
322 return monsterAttacks[CurrentAttack().index];
324 return AttackChoiceAt(CurrentAttack().index);
328 void BattleState::ClearAllAttacks() {
331 for (int i(0); i < numHeroes; ++i) {
332 heroes[i].GetAttackChoice() = AttackChoice(this);
335 monsterAttacks.clear();
339 void BattleState::HandleEvents(const Input &input) {
343 void BattleState::UpdateWorld(float deltaT) {
347 void BattleState::Render(SDL_Surface *screen) {
349 Vector<int> offset(CalculateScreenOffset(screen));
350 RenderBackground(screen, offset);
351 RenderMonsters(screen, offset);
354 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
357 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
359 destRect.x = offset.X();
360 destRect.y = offset.Y();
361 destRect.w = background->w;
362 destRect.h = background->h;
363 SDL_BlitSurface(background, 0, screen, &destRect);
366 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
368 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
369 if (MonsterPositionOccupied(i)) {
370 if (monsters[i].GetAnimation().Running()) {
371 monsters[i].GetAnimation().DrawCenter(screen, monsterPositions[i] + offset);
373 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
379 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
381 for (int i(0); i < numHeroes; ++i) {
382 if (heroes[i].GetAnimation().Running()) {
383 heroes[i].GetAnimation().DrawCenter(screen, heroesPositions[i] + offset);
385 int row(heroes[i].Health() > 0 ? 0 : 2);
386 heroes[i].Sprite()->DrawCenter(screen, heroesPositions[i] + offset, 1, row);
391 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
393 int tagHeight(attackTypeMenu.Height());
394 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
396 for (int i(0); i < numHeroes; ++i) {
397 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
401 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
403 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
404 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
408 rect.y = offset.Y() + Height() - tagHeight;
411 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
412 rect.y += res->normalFont->CharHeight() / 8;
413 rect.h -= res->normalFont->CharHeight() / 4;
414 SDL_FillRect(screen, &rect, res->heroesBgColor);
416 for (int i(0); i < numHeroes; ++i) {
417 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);