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 { survived, _ := resolveCombatToEnd(hero, enemy, start, opts) return survived } // ResolveCombatToEndWithDuration is like ResolveCombatToEnd but also returns simulated combat // elapsed time (last event time minus start), using the same timeline as the online engine. func ResolveCombatToEndWithDuration(hero *model.Hero, enemy *model.Enemy, start time.Time, opts CombatSimOptions) (survived bool, elapsed time.Duration) { return resolveCombatToEnd(hero, enemy, start, opts) } func resolveCombatToEnd(hero *model.Hero, enemy *model.Enemy, start time.Time, opts CombatSimOptions) (survived bool, elapsed time.Duration) { if hero == nil || enemy == nil { return false, 0 } tickRate := opts.TickRate if tickRate <= 0 { tickRate = 100 * time.Millisecond } now := start heroNext := now.Add(attackInterval(hero.EffectiveSpeed())) enemyNext := now.Add(attackIntervalEnemy(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, ®enRemainder) ProcessSummonDamage(hero, enemy, start, lastTickAt, now) lastTickAt = now if CheckDeath(hero, now) { hero.HP = 0 return false, now.Sub(start) } } nextTick = nextTick.Add(tickRate) continue } if !heroNext.After(enemyNext) && now.Equal(heroNext) { ProcessAttack(hero, enemy, now) if !enemy.IsAlive() { return true, now.Sub(start) } heroNext = now.Add(attackInterval(hero.EffectiveSpeed())) continue } if now.Equal(enemyNext) { ProcessEnemyAttack(hero, enemy, now) if CheckDeath(hero, now) { hero.HP = 0 return false, now.Sub(start) } if opts.AutoUsePotion != nil { _ = opts.AutoUsePotion(hero, now) } enemyNext = now.Add(attackIntervalEnemy(enemy.Speed)) } } win := hero.HP > 0 && enemy.IsAlive() == false return win, now.Sub(start) } // 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 }