+void BattleState::AddMonster(const Monster &m) {
+ if (monsters.size() >= monstersLayout->NumPositions()) {
+ throw std::overflow_error("too many monsters for layout");
+ }
+ monsters.push_back(m);
+}
+
+void BattleState::AddHero(const Hero &h) {
+ if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
+ throw std::overflow_error("too many heroes for layout");
+ }
+ heroes[numHeroes] = h;
+ ++numHeroes;
+}
+
+void BattleState::NextHero() {
+ ++activeHero;
+ while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
+ ++activeHero;
+ }
+}
+
+void BattleState::PreviousHero() {
+ --activeHero;
+ while (activeHero >= 0 && heroes[activeHero].Health() == 0) {
+ --activeHero;
+ }
+}
+
+void BattleState::SwapHeroes(int lhs, int rhs) {
+ if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
+ std::swap(heroes[lhs], heroes[rhs]);
+}
+
+
+void BattleState::OnResize(int w, int h) {
+
+}
+
+
+void BattleState::OnEnterState(SDL_Surface *screen) {
+ for (int i(0); i < 4; ++i) {
+ heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
+ heroes[i].SpellMenu() = *res->spellMenuProperties;
+ heroes[i].UpdateSpellMenu();
+ heroes[i].IkariMenu() = *res->ikariMenuProperties;
+ heroes[i].UpdateIkariMenu(res);
+ heroTags[i] = HeroTag(this, i);
+ smallHeroTags[i] = SmallHeroTag(this, i);
+ }
+
+ for (int i(0); i < int(monsters.size()); ++i) {
+ monsters[i].Position() = monstersLayout->CalculatePosition(i, background->w, background->h);
+ }
+
+ int tagHeight(attackTypeMenu.Height());
+ int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
+ int xOffset((Width() - 2 * tagWidth) / 2);
+ heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
+ heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
+ heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
+ heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
+
+ tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
+ tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
+ xOffset = (Width() - 4 * tagWidth) / 2;
+ int yOffset(Height() - tagHeight);
+ smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
+ smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
+ smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
+ smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
+
+ itemMenu = *res->itemMenuProperties;
+ LoadInventory();
+ ClearAllAttacks();
+}
+
+void BattleState::LoadInventory() {
+ const Inventory &inv(game->state->inventory);
+ itemMenu.Clear();
+ itemMenu.Reserve(inv.MaxItems());
+ for (int i(0); i < inv.MaxItems(); ++i) {
+ const Item *item(inv.ItemAt(i));
+ if (item) {
+ itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
+ } else {
+ itemMenu.AddEmptyEntry();
+ }
+ }
+}
+
+void BattleState::OnExitState(SDL_Surface *screen) {
+
+}
+
+void BattleState::OnResumeState(SDL_Surface *screen) {
+ if (ranAway) {
+ Ctrl().PopState(); // quit the battle scene
+ return;
+ }
+ if (Victory()) {
+ Ctrl().PopState();
+ return;
+ }
+ if (Defeat()) {
+ Ctrl().PopState();
+ return;
+ }
+ // TODO: this should not push a state while quitting
+ if (AttackSelectionDone()) {
+ Ctrl().PushState(new PerformAttacks(this));
+ } else {
+ Ctrl().PushState(new SelectMoveAction(this));
+ }
+}
+
+bool BattleState::Victory() const {
+ for (int i(0); i < MaxMonsters(); ++i) {
+ if (MonsterAt(i).Health() > 0) return false;
+ }
+ return true;
+}
+
+bool BattleState::Defeat() const {
+ for (int i(0); i < NumHeroes(); ++i) {
+ if (HeroAt(i).Health() > 0) return false;
+ }
+ return true;
+}
+
+void BattleState::OnPauseState(SDL_Surface *screen) {
+
+}
+
+
+class OrderCompare {
+ public:
+ OrderCompare(BattleState *battle) : battle(battle) { }
+ bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
+ int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
+ int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
+ return lagl > ragl;
+ }
+ private:
+ BattleState *battle;
+};
+
+void BattleState::CalculateAttackOrder() {
+ attackOrder.reserve(monsters.size() + NumHeroes());
+ for (int i(0); i < NumHeroes(); ++i) {
+ attackOrder.push_back(Order(i, false));
+ }
+ for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
+ attackOrder.push_back(Order(i, true));
+ MonsterAt(i).GetAttackChoice() = AttackChoice(this);
+ }
+ std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
+}
+
+void BattleState::NextAttack() {
+ if (Victory() || Defeat()) {
+ attackCursor = attackOrder.size();
+ return;
+ }
+ ++attackCursor;
+ while (attackCursor < int(attackOrder.size())) {
+ if (attackOrder[attackCursor].isMonster) {
+ if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
+ } else {
+ if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
+ }
+ ++attackCursor;
+ }
+}
+
+bool BattleState::AttacksFinished() const {
+ return attackCursor >= int(attackOrder.size())
+ || Victory() || Defeat();
+}
+
+void BattleState::CalculateDamage() {
+ if (CurrentAttack().isMonster) {
+ DecideMonsterAttack(MonsterAt(CurrentAttack().index));
+ }
+ AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
+ if (ac.GetType() == AttackChoice::DEFEND) return;
+ TargetSelection &ts(ac.Selection());
+
+ if (CurrentAttack().isMonster) {
+ const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
+ CalculateDamage(attackerStats, ts);
+ } else {
+ const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
+ CalculateDamage(attackerStats, ts);
+ }
+}
+
+void BattleState::DecideMonsterAttack(Monster &m) const {
+ AttackChoice &ac(m.GetAttackChoice());
+ TargetSelection &ts(ac.Selection());
+ ac.Reset();
+ int target(rand() % NumHeroes());
+ while (!HeroPositionOccupied(target)) {
+ target = rand() % NumHeroes();
+ }
+ ac.SetType(AttackChoice::SWORD);
+ ts.SelectHeroes();
+ ts.SetSingle();
+ ts.Select(target);
+}
+
+void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
+ bool hitSome(false);
+ if (ts.TargetsMonsters()) {
+ for (int i(0); i < MaxMonsters(); ++i) {
+ if (ts.IsSelected(i)) {
+ if (MonsterAt(i).Health() > 0) {
+ const Stats &defenderStats(MonsterAt(i).GetStats());
+ Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+ ts.SetBad(i, damage);
+ hitSome = true;
+ } else {
+ ts.Unselect(i);
+ }
+ }
+ }
+ if (hitSome) return;
+ for (int i(0); i < MaxMonsters(); ++i) {
+ if (MonsterAt(i).Health() > 0) {
+ const Stats &defenderStats(MonsterAt(i).GetStats());
+ Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+ ts.SetBad(i, damage);
+ break;
+ }
+ }
+ } else {
+ for (int i(0); i < NumHeroes(); ++i) {
+ if (ts.IsSelected(i)) {
+ if (HeroAt(i).Health() > 0) {
+ const Stats &defenderStats(HeroAt(i).GetStats());
+ Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+ ts.SetBad(i, damage);
+ hitSome = true;
+ } else {
+ ts.Unselect(i);
+ }
+ }
+ }
+ if (hitSome) return;
+ for (int i(0); i < NumHeroes(); ++i) {
+ if (HeroAt(i).Health() > 0) {
+ const Stats &defenderStats(HeroAt(i).GetStats());
+ Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+ ts.SetBad(i, damage);
+ break;
+ }
+ }
+ }
+}