1package gen 2 3import ( 4 "reflect" 5 "slices" 6 "strings" 7 "testing" 8 9 "tinyadventure/internal/model" 10 "tinyadventure/internal/rng" 11 "tinyadventure/internal/sim" 12 "tinyadventure/internal/world" 13) 14 15func TestDeterministic(t *testing.T) { 16 for _, s := range []model.Seed{1, 42, 99999} { 17 if !reflect.DeepEqual(Generate(s), Generate(s)) { 18 t.Fatalf("seed %d not deterministic", s) 19 } 20 } 21} 22 23func bfs(m model.Map, sx, sy int) map[[2]int]bool { 24 return world.Reach(&m, [2]int{sx, sy}) 25} 26 27func TestSolvable(t *testing.T) { 28 for i := 0; i < 200; i++ { 29 seed := rng.NewSeed() + model.Seed(i) 30 w := Generate(seed) 31 c := w.Campaign 32 if n := len(c.Main.Steps); n < 6 || n > 10 { 33 t.Fatalf("seed %d: %d main steps", seed, n) 34 } 35 if n := len(w.Party.Units); n < 3 || n > 4 { 36 t.Fatalf("seed %d: party %d", seed, n) 37 } 38 sp := w.Map.Spawns[0] 39 reach := bfs(w.Map, sp.X, sp.Y) 40 for _, s := range w.Map.Spawns { 41 if !reach[[2]int{s.X, s.Y}] { 42 t.Fatalf("seed %d: spawn unreachable", seed) 43 } 44 } 45 pois := map[string]model.POI{} 46 for _, p := range w.Map.POIs { 47 pois[p.ID] = p 48 if !reach[[2]int{p.X, p.Y}] { 49 t.Fatalf("seed %d: poi %s unreachable", seed, p.ID) 50 } 51 } 52 loot := map[string]model.LootNode{} 53 for _, l := range w.Map.Loot { 54 loot[l.Item.ID] = l 55 if !reach[[2]int{l.X, l.Y}] { 56 t.Fatalf("seed %d: loot %s unreachable", seed, l.ID) 57 } 58 } 59 missions := append([]model.Mission{c.Main}, c.SideQuests...) 60 for _, ms := range missions { 61 for _, s := range ms.Steps { 62 if _, ok := pois[s.POI]; !ok { 63 t.Fatalf("seed %d: step %s poi missing", seed, s.ID) 64 } 65 if s.Slots.Item != "" { 66 l, ok := loot[s.Slots.Item] 67 if !ok || !l.Item.Key || l.X != pois[s.POI].X || l.Y != pois[s.POI].Y { 68 t.Fatalf("seed %d: key item %s missing", seed, s.Slots.Item) 69 } 70 } 71 } 72 } 73 last := c.Main.Steps[len(c.Main.Steps)-1] 74 if last.Kind != "boss" { 75 t.Fatalf("seed %d: no final boss step", seed) 76 } 77 boss := false 78 for _, e := range w.Encounters { 79 if e.Boss && e.POI == last.POI { 80 boss = true 81 } 82 } 83 if !boss { 84 t.Fatalf("seed %d: boss encounter missing", seed) 85 } 86 if !strings.Contains(sp.Hint, pois[c.Main.Steps[0].POI].Name) { 87 t.Fatalf("seed %d: hint does not name first step", seed) 88 } 89 } 90} 91 92func TestDifficultyScales(t *testing.T) { 93 for seed := model.Seed(1); seed <= 50; seed++ { 94 w := Generate(seed) 95 first := w.Encounters[0].Enemies[len(w.Encounters[0].Enemies)-1].Level 96 b := w.Encounters[len(w.Campaign.Main.Steps)-1] 97 if b.Enemies[0].Stats.MaxHP <= w.Encounters[0].Enemies[0].Stats.MaxHP || b.Enemies[0].Level <= first { 98 t.Fatalf("seed %d: boss not stronger", seed) 99 } 100 } 101} 102 103func TestSeedsVary(t *testing.T) { 104 systems, factions, names := map[string]bool{}, map[string]bool{}, map[string]bool{} 105 for seed := model.Seed(1); seed <= 60; seed++ { 106 c := Generate(seed).Campaign 107 systems[strings.Join(c.Systems, ",")] = true 108 var fs []string 109 for _, f := range c.Factions { 110 fs = append(fs, f.Name) 111 } 112 factions[strings.Join(fs, ",")] = true 113 names[c.Main.Name] = true 114 } 115 if len(systems) < 10 || len(factions) < 10 || len(names) < 3 { 116 t.Fatalf("too little variety: %d %d %d", len(systems), len(factions), len(names)) 117 } 118} 119 120func TestEveryCampaignIsAKeyHuntOverPlacesOnTheMap(t *testing.T) { 121 for seed := model.Seed(1); seed <= 200; seed++ { 122 w := Generate(seed) 123 names := map[string]bool{} 124 for i, s := range w.Campaign.Main.Steps[1 : len(w.Campaign.Main.Steps)-1] { 125 if s.Kind != "retrieve" || s.Slots.Item == "" || names[s.Text[:strings.Index(s.Text, " from ")]] { 126 t.Fatalf("seed %d: main step %d is %q, %q, with item %q", seed, i+2, s.Kind, s.Text, s.Slots.Item) 127 } 128 names[s.Text[:strings.Index(s.Text, " from ")]] = true 129 } 130 for _, sys := range w.Campaign.Systems { 131 kind, ok := strings.CutPrefix(sys, "poi:") 132 if ok && !slices.ContainsFunc(w.Map.POIs, func(p model.POI) bool { return p.Kind == kind }) { 133 t.Fatalf("seed %d lists %s with no %s on the map", seed, sys, kind) 134 } 135 } 136 } 137} 138 139func TestEveryHeroDrawsThreeAbilitiesWithOneThatHits(t *testing.T) { 140 for seed := model.Seed(1); seed <= 60; seed++ { 141 for _, u := range Generate(seed).Party.Units { 142 hits := slices.ContainsFunc(u.Abilities, func(a model.Ability) bool { return a.Kind == sim.KindDamage || a.Kind == sim.KindAoE }) 143 if len(u.Abilities) != sim.Drawn || !hits { 144 t.Fatalf("seed %d %s %s: %+v", seed, u.Class, u.Name, u.Abilities) 145 } 146 } 147 } 148}