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 for (int i(0); i < 4; ++i) {
83 heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
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));
193 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
195 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
198 void BattleState::NextAttack() {
199 if (Victory() || Defeat()) {
200 attackCursor = attackOrder.size();
204 while (attackCursor < int(attackOrder.size())) {
205 if (attackOrder[attackCursor].isMonster) {
206 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
208 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
214 bool BattleState::AttacksFinished() const {
215 return attackCursor >= int(attackOrder.size())
216 || Victory() || Defeat();
219 void BattleState::CalculateDamage() {
220 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
221 if (ac.GetType() == AttackChoice::DEFEND) return;
223 if (CurrentAttack().isMonster) {
224 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
225 // TODO: run monster's attack script
226 ac.SetType(AttackChoice::SWORD);
227 ac.Selection().SelectSingle();
228 ac.Selection().SelectHeroes();
229 for (int i(0); i < NumHeroes(); ++i) {
230 if (HeroAt(i).Health() > 0) {
231 const Stats &defenderStats(HeroAt(i).GetStats());
232 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
233 ac.Selection().SetBad(0, damage);
238 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
239 TargetSelection &ts(ac.Selection());
241 if (ts.TargetsEnemies()) {
242 for (int i(0); i < MaxMonsters(); ++i) {
243 if (ts.IsSelected(i)) {
244 if (MonsterAt(i).Health() > 0) {
245 const Stats &defenderStats(MonsterAt(i).GetStats());
246 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
247 ts.SetBad(i, damage);
255 for (int i(0); i < MaxMonsters(); ++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);
264 for (int i(0); i < NumHeroes(); ++i) {
265 if (ts.IsSelected(i)) {
266 if (HeroAt(i).Health() > 0) {
267 const Stats &defenderStats(HeroAt(i).GetStats());
268 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
269 ts.SetBad(i, damage);
277 for (int i(0); i < NumHeroes(); ++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 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
290 // TODO: find out real formula and add some randomness
291 return attacker.Attack() / 2 - defender.Defense() / 4;
294 void BattleState::ApplyDamage() {
295 if (attackCursor < 0) return;
296 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
297 TargetSelection &ts(ac.Selection());
298 if (ts.TargetsEnemies()) {
299 for (int i(0); i < MaxMonsters(); ++i) {
300 Monster &monster(MonsterAt(i));
302 monster.SubtractHealth(ts.GetAmount(i));
303 if (monster.Health() == 0) {
304 expReward += monster.ExpReward();
305 goldReward += monster.GoldReward();
310 for (int i(0); i < NumHeroes(); ++i) {
311 Hero &hero(HeroAt(i));
313 hero.SubtractHealth(ts.GetAmount(i));
319 AttackChoice &BattleState::CurrentAttackAttackChoice() {
320 if (CurrentAttack().isMonster) {
321 return MonsterAt(CurrentAttack().index).GetAttackChoice();
323 return HeroAt(CurrentAttack().index).GetAttackChoice();
327 void BattleState::ClearAllAttacks() {
330 for (int i(0); i < NumHeroes(); ++i) {
331 HeroAt(i).GetAttackChoice() = AttackChoice(this);
333 for (int i(0); i < MaxMonsters(); ++i) {
334 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
340 void BattleState::HandleEvents(const Input &input) {
344 void BattleState::UpdateWorld(float deltaT) {
348 void BattleState::Render(SDL_Surface *screen) {
350 Vector<int> offset(CalculateScreenOffset(screen));
351 RenderBackground(screen, offset);
352 RenderMonsters(screen, offset);
355 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
358 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
360 destRect.x = offset.X();
361 destRect.y = offset.Y();
362 destRect.w = background->w;
363 destRect.h = background->h;
364 SDL_BlitSurface(background, 0, screen, &destRect);
367 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
369 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
370 if (MonsterPositionOccupied(i)) {
371 if (monsters[i].GetAnimation().Running()) {
372 monsters[i].GetAnimation().DrawCenter(screen, monsterPositions[i] + offset);
374 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
380 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
382 for (int i(0); i < numHeroes; ++i) {
383 if (heroes[i].GetAnimation().Running()) {
384 heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
386 int row(heroes[i].Health() > 0 ? 0 : 2);
387 heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
392 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
394 int tagHeight(attackTypeMenu.Height());
395 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
397 for (int i(0); i < numHeroes; ++i) {
398 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
402 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
404 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
405 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
409 rect.y = offset.Y() + Height() - tagHeight;
412 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
413 rect.y += res->normalFont->CharHeight() / 8;
414 rect.h -= res->normalFont->CharHeight() / 4;
415 SDL_FillRect(screen, &rect, res->heroesBgColor);
417 for (int i(0); i < numHeroes; ++i) {
418 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);