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"
28 using app::Application;
30 using common::Inventory;
33 using geometry::Point;
34 using geometry::Vector;
42 void BattleState::AddMonster(const Monster &m) {
43 if (monsters.size() >= monstersLayout->NumPositions()) {
44 throw std::overflow_error("too many monsters for layout");
46 monsters.push_back(m);
49 void BattleState::AddHero(const Hero &h) {
50 if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
51 throw std::overflow_error("too many heroes for layout");
53 heroes[numHeroes] = h;
57 void BattleState::NextHero() {
59 while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
64 void BattleState::PreviousHero() {
66 while (activeHero >= 0 && heroes[activeHero].Health() == 0) {
71 void BattleState::SwapHeroes(int lhs, int rhs) {
72 if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
73 std::swap(heroes[lhs], heroes[rhs]);
77 void BattleState::Resize(int w, int h) {
82 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
83 for (int i(0); i < 4; ++i) {
84 heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
85 heroes[i].SpellMenu() = res->spellMenuPrototype;
86 heroes[i].UpdateSpellMenu();
87 heroes[i].IkariMenu() = res->ikariMenuPrototype;
88 heroes[i].UpdateIkariMenu(res);
89 heroTags[i] = HeroTag(this, i);
90 smallHeroTags[i] = SmallHeroTag(this, i);
93 for (int i(0); i < int(monsters.size()); ++i) {
94 monsters[i].Position() = monstersLayout->CalculatePosition(i, background->w, background->h);
97 int tagHeight(attackTypeMenu.Height());
98 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
99 int xOffset((Width() - 2 * tagWidth) / 2);
100 heroTagPositions[0] = Point<int>(xOffset, Height() - 2 * tagHeight);
101 heroTagPositions[1] = Point<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
102 heroTagPositions[2] = Point<int>(xOffset, Height() - tagHeight);
103 heroTagPositions[3] = Point<int>(xOffset + tagWidth, Height() - tagHeight);
105 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
106 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
107 xOffset = (Width() - 4 * tagWidth) / 2;
108 int yOffset(Height() - tagHeight);
109 smallHeroTagPositions[0] = Point<int>(xOffset, yOffset);
110 smallHeroTagPositions[1] = Point<int>(xOffset + 2 * tagWidth, yOffset);
111 smallHeroTagPositions[2] = Point<int>(xOffset + tagWidth, yOffset);
112 smallHeroTagPositions[3] = Point<int>(xOffset + 3 * tagWidth, yOffset);
114 itemMenu = res->itemMenuPrototype;
118 void BattleState::LoadInventory() {
119 const Inventory &inv(*res->inventory);
121 itemMenu.Reserve(inv.MaxItems());
122 for (int i(0); i < inv.MaxItems(); ++i) {
123 const Item *item(inv.ItemAt(i));
125 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
127 itemMenu.AddEmptyEntry();
133 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
137 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
139 ctrl.PopState(); // quit the battle scene
143 // TODO: push victory state
148 // TODO: push defeat state
152 // TODO: this should not push a state while quitting
153 if (AttackSelectionDone()) {
154 ctrl.PushState(new PerformAttacks(this));
156 ctrl.PushState(new SelectMoveAction(this));
160 bool BattleState::Victory() const {
161 for (int i(0); i < MaxMonsters(); ++i) {
162 if (MonsterAt(i).Health() > 0) return false;
167 bool BattleState::Defeat() const {
168 for (int i(0); i < NumHeroes(); ++i) {
169 if (HeroAt(i).Health() > 0) return false;
174 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
181 OrderCompare(BattleState *battle) : battle(battle) { }
182 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
183 int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
184 int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
191 void BattleState::CalculateAttackOrder() {
192 attackOrder.reserve(monsters.size() + NumHeroes());
193 for (int i(0); i < NumHeroes(); ++i) {
194 attackOrder.push_back(Order(i, false));
196 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
197 attackOrder.push_back(Order(i, true));
198 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
200 std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
203 void BattleState::NextAttack() {
204 if (Victory() || Defeat()) {
205 attackCursor = attackOrder.size();
209 while (attackCursor < int(attackOrder.size())) {
210 if (attackOrder[attackCursor].isMonster) {
211 if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
213 if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
219 bool BattleState::AttacksFinished() const {
220 return attackCursor >= int(attackOrder.size())
221 || Victory() || Defeat();
224 void BattleState::CalculateDamage() {
225 if (CurrentAttack().isMonster) {
226 DecideMonsterAttack(MonsterAt(CurrentAttack().index));
228 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
229 if (ac.GetType() == AttackChoice::DEFEND) return;
230 TargetSelection &ts(ac.Selection());
232 // TODO: this only evaluates SWORD type attacks
233 if (CurrentAttack().isMonster) {
234 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
235 CalculateDamage(attackerStats, ts);
237 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
238 CalculateDamage(attackerStats, ts);
242 void BattleState::DecideMonsterAttack(Monster &m) const {
243 AttackChoice &ac(m.GetAttackChoice());
244 TargetSelection &ts(ac.Selection());
246 // TODO: run monster's attack script
247 int target(rand() % NumHeroes());
248 while (!HeroPositionOccupied(target)) {
249 target = rand() % NumHeroes();
251 ac.SetType(AttackChoice::SWORD);
257 void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
259 if (ts.TargetsMonsters()) {
260 for (int i(0); i < MaxMonsters(); ++i) {
261 if (ts.IsSelected(i)) {
262 if (MonsterAt(i).Health() > 0) {
263 const Stats &defenderStats(MonsterAt(i).GetStats());
264 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
265 ts.SetBad(i, damage);
273 for (int i(0); i < MaxMonsters(); ++i) {
274 if (MonsterAt(i).Health() > 0) {
275 const Stats &defenderStats(MonsterAt(i).GetStats());
276 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
277 ts.SetBad(i, damage);
282 for (int i(0); i < NumHeroes(); ++i) {
283 if (ts.IsSelected(i)) {
284 if (HeroAt(i).Health() > 0) {
285 const Stats &defenderStats(HeroAt(i).GetStats());
286 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
287 ts.SetBad(i, damage);
295 for (int i(0); i < NumHeroes(); ++i) {
296 if (HeroAt(i).Health() > 0) {
297 const Stats &defenderStats(HeroAt(i).GetStats());
298 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
299 ts.SetBad(i, damage);
306 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
307 // TODO: find out real formula and add some randomness
308 return attacker.Attack() / 2 - defender.Defense() / 4;
311 void BattleState::ApplyDamage() {
312 if (attackCursor < 0) return;
313 AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
314 TargetSelection &ts(ac.Selection());
315 if (ts.TargetsMonsters()) {
316 for (int i(0); i < MaxMonsters(); ++i) {
317 Monster &monster(MonsterAt(i));
319 monster.SubtractHealth(ts.GetAmount(i));
320 if (monster.Health() == 0) {
321 expReward += monster.ExpReward();
322 goldReward += monster.GoldReward();
327 for (int i(0); i < NumHeroes(); ++i) {
328 Hero &hero(HeroAt(i));
330 hero.SubtractHealth(ts.GetAmount(i));
336 AttackChoice &BattleState::CurrentAttackAttackChoice() {
337 if (CurrentAttack().isMonster) {
338 return MonsterAt(CurrentAttack().index).GetAttackChoice();
340 return HeroAt(CurrentAttack().index).GetAttackChoice();
344 void BattleState::ClearAllAttacks() {
347 for (int i(0); i < NumHeroes(); ++i) {
348 HeroAt(i).GetAttackChoice() = AttackChoice(this);
350 for (int i(0); i < MaxMonsters(); ++i) {
351 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
357 void BattleState::HandleEvents(const Input &input) {
361 void BattleState::UpdateWorld(float deltaT) {
365 void BattleState::Render(SDL_Surface *screen) {
367 Vector<int> offset(CalculateScreenOffset(screen));
368 RenderBackground(screen, offset);
369 RenderMonsters(screen, offset);
372 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
375 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
377 destRect.x = offset.X();
378 destRect.y = offset.Y();
379 destRect.w = background->w;
380 destRect.h = background->h;
381 SDL_BlitSurface(background, 0, screen, &destRect);
384 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
386 for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
387 if (MonsterPositionOccupied(i)) {
388 if (monsters[i].GetAnimation().Running()) {
389 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
391 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
397 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
399 for (int i(0); i < numHeroes; ++i) {
400 if (heroes[i].GetAnimation().Running()) {
401 heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
403 int row(heroes[i].Health() > 0 ? 0 : 2);
404 heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
409 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
411 int tagHeight(attackTypeMenu.Height());
412 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
414 for (int i(0); i < numHeroes; ++i) {
415 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
419 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
421 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
422 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
426 rect.y = offset.Y() + Height() - tagHeight;
429 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
430 rect.y += res->normalFont->CharHeight() / 8;
431 rect.h -= res->normalFont->CharHeight() / 4;
432 SDL_FillRect(screen, &rect, res->heroesBgColor);
434 for (int i(0); i < numHeroes; ++i) {
435 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);