package game import ( "fmt" "math" "math/rand/v2" "aeternalis/server/content" p "aeternalis/server/internal/protocol" "github.com/ojrac/opensimplex-go" ) const GeneratorVersion = 2 type Terrain struct { Version int Seed int64 B content.Balance noise opensimplex.Noise } func NewTerrain(seed int64, b content.Balance) *Terrain { return &Terrain{Version: GeneratorVersion, Seed: seed, B: b, noise: opensimplex.New(seed)} } func (t *Terrain) rawHeight(x, z float64) float64 { // A low, flat arrival clearing blends into broad hills and fine detail. d := math.Min(1, math.Hypot(x, z)/55) if t.Version >= 2 { return (t.noise.Eval2(x/220, z/220)*t.B.GatheringRules.HillHeight + t.noise.Eval2(x/55, z/55)*t.B.GatheringRules.DetailHeight) * d } return (t.noise.Eval2(x/220, z/220)*17 + t.noise.Eval2(x/55, z/55)*3) * d } func ChunkCoord(v, size float64) int { return int(math.Floor(v / size)) } func Key(x, z int) string { return fmt.Sprintf("%d:%d", x, z) } // Height uses the same triangle split as the transmitted terrain mesh. func (t *Terrain) Height(x, z float64) float64 { step := t.B.ChunkSize / float64(t.B.TerrainResolution) ix, iz := math.Floor(x/step), math.Floor(z/step) fx, fz := x/step-ix, z/step-iz a, b := t.rawHeight(ix*step, iz*step), t.rawHeight((ix+1)*step, iz*step) c, d := t.rawHeight(ix*step, (iz+1)*step), t.rawHeight((ix+1)*step, (iz+1)*step) if fx+fz <= 1 { return a + (b-a)*fx + (c-a)*fz } return d + (c-d)*(1-fx) + (b-d)*(1-fz) } func (t *Terrain) Position(x, z float64) p.Vec3 { return p.Vec3{X: x, Y: t.Height(x, z), Z: z} } func (t *Terrain) legacyChunk(x, z int) p.Chunk { c := p.Chunk{Key: Key(x, z), X: int64(x), Z: int64(z), Size: t.B.ChunkSize, Resolution: int64(t.B.TerrainResolution), Biome: "highlands", Heights: []float64{}, Resources: []p.Entity{}} for iz := 0; iz <= t.B.TerrainResolution; iz++ { for ix := 0; ix <= t.B.TerrainResolution; ix++ { c.Heights = append(c.Heights, t.rawHeight(float64(x)*c.Size+float64(ix)*c.Size/float64(c.Resolution), float64(z)*c.Size+float64(iz)*c.Size/float64(c.Resolution))) } } rng := rand.New(rand.NewPCG(uint64(t.Seed)^uint64(int64(x)*73856093), uint64(int64(z)*19349663)^0xa341316c)) kinds := []string{"tree", "ore", "herb"} for i := 0; i < t.B.ResourcesPerChunk; i++ { px, pz := (float64(x)+0.08+rng.Float64()*0.84)*c.Size, (float64(z)+0.08+rng.Float64()*0.84)*c.Size if math.Hypot(px, pz) > t.B.WorldRadius || math.Hypot(px, pz) < 10 { continue } kind := kinds[i%len(kinds)] c.Resources = append(c.Resources, p.Entity{ID: fmt.Sprintf("r:%d:%d:%d", x, z, i), Kind: kind, Name: kind, Position: t.Position(px, pz), HP: 1, MaxHP: 1, State: "available"}) } // Arrival resources make the first crafting loop reachable without aimless travel. if x == 0 && z == 0 { for i, k := range kinds { c.Resources = append(c.Resources, p.Entity{ID: "arrival:" + k, Kind: k, Name: k, Position: t.Position(12+float64(i)*5, 12), HP: 1, MaxHP: 1, State: "available"}) } } return c }