1package sim 2 3import ( 4 "fmt" 5 "slices" 6 7 "tinyadventure/internal/model" 8) 9 10const ( 11 weakMult = 1.5 12 armorPlus = 3 13 firstMult = 1.5 14 lifestealPC = 30 15 thornsPC = 25 16 sweepPC = 50 17) 18 19var ( 20 thornsAbility = model.Ability{ID: "thorns", Name: "Thorns", Kind: KindDamage, Type: Physical} 21 lifestealAbility = model.Ability{ID: "lifesteal", Name: "Lifesteal", Kind: KindHeal} 22) 23 24func (c *combatant) typeOf(a model.Ability) string { 25 if a.Type == Physical && c.edge != "" { 26 return c.edge 27 } 28 return a.Type 29} 30 31func (c *combatant) statFor(a model.Ability) int { 32 if usesAttack(a.Type) { 33 return c.stats.Attack 34 } 35 return c.stats.Magic 36} 37 38func (c *combatant) guarded(typ string) bool { 39 return typ == Physical || slices.Contains(c.guards, typ) || c.under("ward") 40} 41 42func (c *combatant) defense() int { 43 def := max(c.stats.Defense, 0) 44 if c.has("armored") { 45 def += armorPlus 46 } 47 return def 48} 49 50func (b *battle) hit(actor, target *combatant, a model.Ability, scale float64) { 51 typ := actor.typeOf(a) 52 if target.under("dodge") { 53 target.drop("dodge") 54 b.log = append(b.log, model.CombatEvent{ 55 Tick: b.tick, Actor: actor.unit.ID, Target: target.unit.ID, Ability: a.ID, Kind: a.Kind, Type: typ, Dodged: true, 56 Text: fmt.Sprintf("%s dodges %s's %s", target.unit.Name, actor.unit.Name, a.Name), 57 }) 58 return 59 } 60 raw := float64(max(actor.statFor(a), 1)) * a.Power * scale * (0.85 + 0.3*b.r.Float64()) 61 crit := actor.ambush || b.r.IntN(100) < actor.stats.Crit 62 actor.ambush = false 63 if crit { 64 raw *= 2 65 } 66 if actor.has("first") && !actor.struck { 67 raw *= firstMult 68 } 69 actor.struck = true 70 raw *= actor.boost() * target.exposure() 71 verb := "damage" 72 if typ != Physical { 73 verb = typ + " damage" 74 } 75 if slices.Contains(target.unit.Weak, typ) { 76 raw *= weakMult 77 verb += ", a weakness" 78 } 79 if target.guarded(typ) { 80 def := target.defense() 81 if actor.has("pierce") { 82 def = 0 83 } 84 raw = raw * 10 / float64(10+def) 85 if typ != Physical { 86 verb += ", guarded" 87 } 88 } 89 dmg := max(1, int(raw)) 90 target.hp = max(0, target.hp-dmg) 91 target.hurtBy = actor 92 b.emit(actor, target, a, typ, dmg, crit, !target.alive(), verb) 93 if actor.has("lifesteal") && actor.alive() && actor.hp < actor.stats.MaxHP { 94 drain := min(actor.stats.MaxHP-actor.hp, max(1, dmg*lifestealPC/100)) 95 actor.hp += drain 96 b.emit(actor, actor, lifestealAbility, "", drain, false, false, "healing") 97 } 98 if target.has("thorns") && a.Kind == KindDamage && usesAttack(a.Type) && actor.alive() { 99 back := max(1, dmg*thornsPC/100) 100 actor.hp = max(0, actor.hp-back) 101 b.emit(target, actor, thornsAbility, Physical, back, false, !actor.alive(), "damage") 102 } 103 if scale == 1 && actor.has("sweep") && a.Kind == KindDamage { 104 for _, f := range b.side(target.side) { 105 if f != target { 106 b.hit(actor, f, a, float64(sweepPC)/100) 107 break 108 } 109 } 110 } 111} 112 113func (b *battle) heal(actor *combatant, a model.Ability) { 114 allies := b.side(actor.side) 115 targets := allies[:0:0] 116 if a.Target == TargetAllAllies { 117 for _, al := range allies { 118 if al.hp < al.stats.MaxHP { 119 targets = append(targets, al) 120 } 121 } 122 } else { 123 t := allies[0] 124 for _, al := range allies[1:] { 125 if al.hp*t.stats.MaxHP < t.hp*al.stats.MaxHP { 126 t = al 127 } 128 } 129 targets = append(targets, t) 130 } 131 for _, t := range targets { 132 amt := max(1, int(float64(actor.stats.Magic)*a.Power*(0.85+0.3*b.r.Float64()))) 133 amt = min(amt, t.stats.MaxHP-t.hp) 134 t.hp += amt 135 b.emit(actor, t, a, "", amt, false, false, "healing") 136 } 137} 138 139func (b *battle) emit(actor, target *combatant, a model.Ability, typ string, amount int, crit, killed bool, verb string) { 140 text := fmt.Sprintf("%s uses %s on %s for %d %s", actor.unit.Name, a.Name, target.unit.Name, amount, verb) 141 if crit { 142 text += " (crit)" 143 } 144 if killed { 145 text += ", " + target.unit.Name + " falls" 146 } 147 b.log = append(b.log, model.CombatEvent{ 148 Tick: b.tick, Actor: actor.unit.ID, Target: target.unit.ID, Ability: a.ID, Kind: a.Kind, Type: typ, 149 Amount: amount, Crit: crit, Killed: killed, Text: text, 150 }) 151}