1package world 2 3import ( 4 "testing" 5 6 "tinyadventure/internal/model" 7) 8 9func flat(w, h int) *model.Map { 10 m := &model.Map{Width: w, Height: h} 11 for range w * h { 12 m.Hexes = append(m.Hexes, model.ShapePlain, 3, 1, 0) 13 } 14 return m 15} 16 17func TestNeighboursAreOneHopAway(t *testing.T) { 18 for _, c := range [][2]int{{4, 4}, {5, 5}, {0, 1}} { 19 for _, n := range Neighbours(c[0], c[1]) { 20 if d := Distance(c[0], c[1], n[0], n[1]); d != 1 { 21 t.Fatalf("%v -> %v distance %d", c, n, d) 22 } 23 } 24 } 25} 26 27func TestPathPrefersRoadsAndSkipsBlocked(t *testing.T) { 28 m := flat(9, 5) 29 for x := range 9 { 30 m.Hex(x, 2)[0] = model.ShapeMountain 31 m.Hex(x, 0)[3] = model.FlagRoad 32 } 33 m.Hex(4, 2)[0] = model.ShapePeak 34 got := Path(m, [2]int{0, 1}, [2]int{8, 1}, nil) 35 if got == nil || got[len(got)-1] != [2]int{8, 1} { 36 t.Fatalf("no path: %v", got) 37 } 38 road := 0 39 for _, s := range got { 40 if m.Hex(s[0], s[1])[3]&model.FlagRoad != 0 { 41 road++ 42 } 43 } 44 if road == 0 { 45 t.Fatalf("path ignored the road: %v", got) 46 } 47 if Path(m, [2]int{0, 1}, [2]int{4, 2}, nil) != nil { 48 t.Fatal("path into a peak") 49 } 50 if p := Path(m, [2]int{0, 1}, [2]int{8, 1}, func(x, y int) bool { return y <= 1 && x == 4 }); p == nil { 51 t.Fatal("no detour around a blocked hex") 52 } 53} 54 55func TestClimateDriftsEast(t *testing.T) { 56 hot := New(3, 60, 40, Climate{Temp: 1, Moist: -1}) 57 west, east := 0, 0 58 for row := range 40 { 59 for col := range 10 { 60 if h := hot.Hex(col, row); h[0] >= model.ShapePlain && h[1] == 6 { 61 west++ 62 } 63 if h := hot.Hex(59-col, row); h[0] >= model.ShapePlain && h[1] == 6 { 64 east++ 65 } 66 } 67 } 68 if east <= west*2 { 69 t.Fatalf("badlands west %d east %d", west, east) 70 } 71}