You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.9 KiB
Go

package game
import (
"math/rand"
"time"
"github.com/denisovdennis/autohero/internal/model"
"github.com/denisovdennis/autohero/internal/tuning"
)
const (
offlineAutoPotionChance = 0.02
offlineAutoPotionHPThresh = 0.40
)
// CombatSimOptions configures the shared combat resolution loop.
type CombatSimOptions struct {
// TickRate matches the engine combat tick cadence (used for periodic effects).
TickRate time.Duration
// AutoUsePotion decides whether to consume a potion after damage ticks/attacks.
// It should return true when a potion was used.
AutoUsePotion func(hero *model.Hero, now time.Time) bool
}
// ResolveCombatToEnd runs a combat loop using the same mechanics as the online engine.
// It mutates hero and enemy until one side dies, returning whether the hero survived.
func ResolveCombatToEnd(hero *model.Hero, enemy *model.Enemy, start time.Time, opts CombatSimOptions) bool {
if hero == nil || enemy == nil {
return false
}
tickRate := opts.TickRate
if tickRate <= 0 {
tickRate = 100 * time.Millisecond
}
now := start
heroNext := now.Add(attackInterval(hero.EffectiveSpeed()))
enemyNext := now.Add(attackInterval(enemy.Speed))
nextTick := now.Add(tickRate)
lastTickAt := now
var regenRemainder float64
step := 0
const maxSteps = 200000
for step < maxSteps {
step++
next := heroNext
if enemyNext.Before(next) {
next = enemyNext
}
if nextTick.Before(next) {
next = nextTick
}
now = next
if now.Equal(nextTick) {
tickDur := now.Sub(lastTickAt)
if tickDur > 0 {
ProcessDebuffDamage(hero, tickDur, now)
ProcessEnemyRegen(enemy, tickDur, &regenRemainder)
ProcessSummonDamage(hero, enemy, start, lastTickAt, now)
lastTickAt = now
if CheckDeath(hero, now) {
hero.HP = 0
return false
}
}
nextTick = nextTick.Add(tickRate)
continue
}
if !heroNext.After(enemyNext) && now.Equal(heroNext) {
ProcessAttack(hero, enemy, now)
if !enemy.IsAlive() {
return true
}
heroNext = now.Add(attackInterval(hero.EffectiveSpeed()))
continue
}
if now.Equal(enemyNext) {
ProcessEnemyAttack(hero, enemy, now)
if CheckDeath(hero, now) {
hero.HP = 0
return false
}
if opts.AutoUsePotion != nil {
_ = opts.AutoUsePotion(hero, now)
}
enemyNext = now.Add(attackInterval(enemy.Speed))
}
}
return hero.HP > 0 && enemy.IsAlive() == false
}
// OfflineAutoPotionHook is a low-probability offline-only potion usage policy.
func OfflineAutoPotionHook(hero *model.Hero, now time.Time) bool {
if hero == nil || hero.Potions <= 0 || hero.HP <= 0 {
return false
}
hpThresh := int(float64(hero.MaxHP) * offlineAutoPotionHPThresh)
if hero.HP >= hpThresh {
return false
}
if rand.Float64() >= offlineAutoPotionChance {
return false
}
hero.Potions--
healAmount := int(float64(hero.MaxHP) * tuning.Get().PotionHealPercent)
if healAmount < 1 {
healAmount = 1
}
hero.HP += healAmount
if hero.HP > hero.MaxHP {
hero.HP = hero.MaxHP
}
return true
}