1package sim 2 3import ( 4 "slices" 5 "testing" 6 7 "tinyadventure/internal/model" 8 "tinyadventure/internal/rng" 9) 10 11func duel(attacker model.Unit, target model.Unit, a model.Ability) (*battle, *combatant, *combatant) { 12 b := &battle{r: rng.New(1)} 13 x, y := newCombatant(attacker, 0), newCombatant(target, 1) 14 b.all = []*combatant{x, y} 15 b.hit(x, y, a, 1) 16 return b, x, y 17} 18 19func dummy(t ...string) model.Unit { 20 return model.Unit{ID: "d", Name: "Dummy", Traits: t, Stats: model.Stats{HP: 1000, MaxHP: 1000, Attack: 20, Magic: 20, Defense: 10}} 21} 22 23func TestWeaknessAndResistScaleDamage(t *testing.T) { 24 bolt := mustAbility("firebolt") 25 _, _, plain := duel(dummy(), dummy(), bolt) 26 weak := dummy() 27 weak.Weak = []string{"fire"} 28 _, _, w := duel(dummy(), weak, bolt) 29 res := dummy() 30 res.Resist = []string{"fire"} 31 _, _, r := duel(dummy(), res, bolt) 32 if !(1000-w.hp > 1000-plain.hp && 1000-plain.hp > 1000-r.hp) { 33 t.Fatalf("weak %d plain %d resist %d", 1000-w.hp, 1000-plain.hp, 1000-r.hp) 34 } 35} 36 37func TestPierceBeatsArmorAndThornsHurtBack(t *testing.T) { 38 strike := mustAbility(StrikeID) 39 _, _, armored := duel(dummy(), dummy("armored"), strike) 40 _, _, pierced := duel(dummy("pierce"), dummy("armored"), strike) 41 if 1000-pierced.hp <= 1000-armored.hp { 42 t.Fatalf("pierce %d vs armored %d", 1000-pierced.hp, 1000-armored.hp) 43 } 44 b, x, _ := duel(dummy(), dummy("thorns"), strike) 45 if x.hp >= 1000 || len(b.log) != 2 { 46 t.Fatalf("thorns: attacker hp %d log %d", x.hp, len(b.log)) 47 } 48} 49 50func TestTauntDecidesEachFoesNextAttackOnly(t *testing.T) { 51 b := &battle{r: rng.New(1)} 52 foe := newCombatant(dummy(), 1) 53 soft, tank := newCombatant(dummy(), 0), newCombatant(dummy(), 0) 54 soft.hp = 1 55 b.all = []*combatant{soft, tank, foe} 56 b.cast(tank, mustAbility("taunt")) 57 foe.startTurn() 58 if got := b.pickTarget(foe, b.side(0)); got != tank { 59 t.Fatal("taunt did not draw the attack") 60 } 61 foe.endTurn() 62 if foe.tauntedBy(b.side(0)) != nil { 63 t.Fatal("taunt outlived the foe's turn") 64 } 65} 66 67func TestRageBoostsTheCastersNextAttack(t *testing.T) { 68 b := &battle{r: rng.New(1)} 69 hero, foe := newCombatant(dummy(), 0), newCombatant(dummy(), 1) 70 b.all = []*combatant{hero, foe} 71 hero.startTurn() 72 b.cast(hero, mustAbility("rage")) 73 hero.endTurn() 74 hero.startTurn() 75 if hero.boost() != 2 { 76 t.Fatalf("boost on the next turn is %v", hero.boost()) 77 } 78 hero.endTurn() 79 if hero.boost() != 1 { 80 t.Fatalf("rage outlived the next turn: %v", hero.boost()) 81 } 82} 83 84func TestImbueAddsTraitWithCost(t *testing.T) { 85 it := model.Item{Slot: model.SlotWeapon, Rarity: "epic", Bonus: model.Stats{Attack: 10, Defense: 5, MaxHP: 20, Magic: 5, Crit: 10}} 86 Imbue(func(n int) int { return 0 }, &it) 87 if it.Trait != "lifesteal" || it.Bonus.Defense != 3 { 88 t.Fatalf("%+v", it) 89 } 90} 91 92func TestEdgeNamesHeroesWhoHitWeakness(t *testing.T) { 93 mage := model.Unit{Name: "Isolde", Abilities: Abilities("firebolt")} 94 knight := model.Unit{Name: "Brom", Gear: model.Gear{model.SlotWeapon: &model.Item{Trait: "pierce"}}} 95 foes := []model.Unit{{Weak: []string{"fire"}, Traits: []string{"armored"}}} 96 e := EdgeOf([]model.Unit{mage, knight}, foes) 97 if !slices.Equal(e.Hits, []string{"fire (Isolde)", "pierce (Brom)"}) || len(e.Blunted) != 0 { 98 t.Fatalf("%+v", e) 99 } 100 e = EdgeOf([]model.Unit{mage}, []model.Unit{{Resist: []string{"fire"}}}) 101 if !slices.Equal(e.Blunted, []string{"fire (Isolde)"}) { 102 t.Fatalf("%+v", e) 103 } 104}