package fluvial import ( "testing" "salty/terrain/internal/world" ) // The whole point of the move from an index hash to a position hash: a planet is solved one landmass at a // time, so the same physical cell turns up in grids of different widths at different offsets. If the jitter // disagreed between them, every place two frames met would show a line. func TestJitterFollowsThePositionNotTheIndex(t *testing.T) { p, err := world.New(512, 8, 100, 50, 2, 512) if err != nil { t.Fatal(err) } // Two frames of different widths, both covering planet column 5, row 7. a := Grid{W: 16, H: 16} a.SetSeed(11) a.SetFrame(world.Frame{P: p, X0: 0, Y0: 0, W: 16, H: 16}) b := Grid{W: 9, H: 12} b.SetSeed(11) b.SetFrame(world.Frame{P: p, X0: 3, Y0: 4, W: 9, H: 12}) for k := int32(0); k < 9; k++ { ja := hashXY(a.seed, a.worldX(5), a.worldY(7), k) jb := hashXY(b.seed, b.worldX(2), b.worldY(3), k) if ja != jb { t.Fatalf("k=%d: frame a gives %v, frame b gives %v for the same planet cell", k, ja, jb) } } // And it must still be a hash: the neighbouring cell gets an unrelated value. if hashXY(a.seed, a.worldX(5), a.worldY(7), 0) == hashXY(a.seed, a.worldX(6), a.worldY(7), 0) { t.Error("neighbouring cells hash the same") } } // A frame that straddles the seam sees the same positions as one that does not. func TestJitterWrapsAtTheSeam(t *testing.T) { p, err := world.New(512, 8, 100, 50, 0, 512) if err != nil { t.Fatal(err) } at := Grid{W: 8, H: 8} at.SetSeed(3) at.SetFrame(world.Frame{P: p, X0: 60, Y0: 0, W: 8, H: 8}) origin := Grid{W: 8, H: 8} origin.SetSeed(3) origin.SetFrame(world.Frame{P: p, X0: 0, Y0: 0, W: 8, H: 8}) // The seam frame's column 4 is planet column 0, which is the origin frame's column 0. if got, want := at.worldX(4), origin.worldX(0); got != want { t.Fatalf("world column = %d, want %d", got, want) } if hashXY(at.seed, at.worldX(4), at.worldY(2), 0) != hashXY(origin.seed, origin.worldX(0), origin.worldY(2), 0) { t.Error("the same planet cell jitters differently on either side of the seam") } } // Without a frame a grid is its own world at the origin, which is what the square canvas is and what every // existing test relies on. func TestNoFrameMeansTheGridIsTheWorld(t *testing.T) { g := Grid{W: 8, H: 8} g.SetSeed(1) if got := g.worldX(7); got != 7 { t.Errorf("worldX(7) = %d, want 7", got) } if got := g.worldY(3); got != 3 { t.Errorf("worldY(3) = %d, want 3", got) } }