From: Daniel Karbach Date: Wed, 22 Aug 2012 19:45:10 +0000 (+0200) Subject: randomized monsters' attack target selection X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=0f5f8a6eaa938f88fd3d1e92dec2462b30840e21;p=l2e.git randomized monsters' attack target selection --- diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index fe1b309..fe821fc 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -22,6 +22,7 @@ #include #include +#include #include using app::Application; @@ -33,6 +34,7 @@ using geometry::Point; using geometry::Vector; using graphics::Menu; +using std::rand; using std::vector; namespace battle { @@ -220,26 +222,43 @@ bool BattleState::AttacksFinished() const { } 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()); + // TODO: this only evaluates SWORD type attacks if (CurrentAttack().isMonster) { const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats()); - // TODO: run monster's attack script - ac.SetType(AttackChoice::SWORD); - ac.Selection().SelectSingle(); - ac.Selection().SelectHeroes(); - for (int i(0); i < NumHeroes(); ++i) { - if (HeroAt(i).Health() > 0) { - const Stats &defenderStats(HeroAt(i).GetStats()); - Uint16 damage(CalculateDamage(attackerStats, defenderStats)); - ac.Selection().SetBad(0, damage); - break; + if (ts.TargetsEnemies()) { + 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); + } else { + ts.Unselect(i); + } + } + } + } 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); + } else { + ts.Unselect(i); + } + } } } } else { const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats()); - TargetSelection &ts(ac.Selection()); bool hitSome(false); if (ts.TargetsEnemies()) { for (int i(0); i < MaxMonsters(); ++i) { @@ -289,6 +308,21 @@ void BattleState::CalculateDamage() { } } +void BattleState::DecideMonsterAttack(Monster &m) { + AttackChoice &ac(m.GetAttackChoice()); + TargetSelection &ts(ac.Selection()); + ac.Reset(); + // TODO: run monster's attack script + int target(rand() % NumHeroes()); + while (!HeroPositionOccupied(target)) { + target = rand() % NumHeroes(); + } + ac.SetType(AttackChoice::SWORD); + ts.SelectHeroes(); + ts.SetSingle(); + ts.Select(target); +} + Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const { // TODO: find out real formula and add some randomness return attacker.Attack() / 2 - defender.Defense() / 4; diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index d22d689..4fd9a15 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -150,6 +150,7 @@ public: private: void LoadInventory(); + void DecideMonsterAttack(Monster &); Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const; private: diff --git a/src/main.cpp b/src/main.cpp index cf6e78b..71a29e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,6 +30,8 @@ #include "sdl/InitScreen.h" #include "sdl/InitSDL.h" +#include +#include #include #include #include @@ -70,6 +72,8 @@ int main(int argc, char **argv) { const int framerate = 33; +// std::srand(std::time(0)); + try { InitSDL sdl; InitImage image(IMG_INIT_PNG);