1package sim 2 3import ( 4 "fmt" 5 "slices" 6 7 "tinyadventure/internal/model" 8) 9 10type effect struct { 11 id string 12 owner *combatant 13 power float64 14 due bool 15} 16 17func (c *combatant) under(id string) bool { 18 return slices.ContainsFunc(c.effects, func(e effect) bool { return e.id == id }) 19} 20 21func (c *combatant) drop(id string) { 22 c.effects = slices.DeleteFunc(c.effects, func(e effect) bool { return e.id == id }) 23} 24 25func (c *combatant) sum(id string) float64 { 26 n := 0.0 27 for _, e := range c.effects { 28 if e.id == id { 29 n += e.power 30 } 31 } 32 return n 33} 34 35func (c *combatant) boost() float64 { return 1 + c.sum("rage") + c.sum("rally") } 36 37func (c *combatant) tauntedBy(foes []*combatant) *combatant { 38 for i := len(c.effects) - 1; i >= 0; i-- { 39 if e := c.effects[i]; e.id == "taunt" && slices.Contains(foes, e.owner) { 40 return e.owner 41 } 42 } 43 return nil 44} 45 46func (c *combatant) exposure() float64 { return 1 + c.sum("mark") } 47 48func (c *combatant) hidden() bool { return c.under("sneak") } 49 50func (c *combatant) startTurn() { 51 for i := range c.effects { 52 c.effects[i].due = true 53 } 54} 55 56func (c *combatant) endTurn() { 57 c.effects = slices.DeleteFunc(c.effects, func(e effect) bool { return e.due }) 58} 59 60var effectText = map[string]string{ 61 "taunt": "must attack %s next", 62 "rage": "rages: %d%% more damage on the next attack", 63 "sneak": "slips out of sight", 64 "dodge": "gets ready to dodge", 65 "ward": "is warded against every type", 66 "rally": "deals %d%% more damage", 67 "mark": "is marked to take %d%% more damage", 68 "haste": "acts at once", 69} 70 71func (b *battle) cast(actor *combatant, a model.Ability) { 72 var targets []*combatant 73 switch a.Target { 74 case TargetSelf: 75 targets = []*combatant{actor} 76 case TargetAllAllies: 77 targets = b.side(actor.side) 78 case TargetAllEnemies: 79 targets = b.side(1 - actor.side) 80 case TargetEnemy: 81 targets = []*combatant{b.pickTarget(actor, b.side(1-actor.side))} 82 case TargetAlly: 83 targets = []*combatant{b.hasteTarget(actor)} 84 } 85 for _, t := range targets { 86 if a.ID != "haste" { 87 t.effects = append(t.effects, effect{id: a.ID, owner: actor, power: a.Power}) 88 } 89 text := effectText[a.ID] 90 if a.ID == "taunt" { 91 text = fmt.Sprintf(text, actor.unit.Name) 92 } else if a.Power > 0 { 93 text = fmt.Sprintf(text, int(a.Power*100)) 94 } 95 b.log = append(b.log, model.CombatEvent{ 96 Tick: b.tick, Actor: actor.unit.ID, Target: t.unit.ID, Ability: a.ID, Kind: a.Kind, Amount: int(a.Power * 100), 97 Text: fmt.Sprintf("%s uses %s: %s %s", actor.unit.Name, a.Name, t.unit.Name, text), 98 }) 99 } 100 if a.ID == "sneak" { 101 actor.ambush = true 102 } 103 if a.ID == "haste" && targets[0] != actor { 104 b.act(targets[0], true) 105 } 106} 107 108func (b *battle) hasteTarget(actor *combatant) *combatant { 109 best := actor 110 for _, al := range b.side(actor.side) { 111 if al != actor && (best == actor || max(al.stats.Attack, al.stats.Magic) > max(best.stats.Attack, best.stats.Magic)) { 112 best = al 113 } 114 } 115 return best 116}