package game import ( "aeternalis/server/content" p "aeternalis/server/internal/protocol" "fmt" "hash/fnv" "math" "math/rand/v2" "slices" ) // Read-only arrival preview uses the world's generator and any explored layout. func (w *World) PreviewChunk(x, z int) p.Chunk { c := w.terrain.Chunk(x, z) if layout, ok := w.ResourceLayouts[c.Key]; ok { c.Biome, c.Color, c.Resources = layout.Biome, layout.Color, layout.Resources } return c } func (t *Terrain) biome(x, z int) content.BiomeDefinition { list := t.B.Gathering.Biomes total := int64(0) for _, b := range list { total += b.Weight } // Low-frequency noise creates contiguous biome regions; no per-frame randomness. n := (t.noise.Eval2((float64(x)+0.5)/3, (float64(z)+0.5)/3) + 1) / 2 choice := int64(n * float64(total)) for _, b := range list { choice -= b.Weight if choice < 0 { return b } } return list[len(list)-1] } func (t *Terrain) Chunk(x, z int) p.Chunk { c := t.legacyChunk(x, z) if t.B.Gathering == nil { return c } biome := t.biome(x, z) c.Biome = biome.Name c.Color = &biome.Color if t.Version == 1 { for i := range c.Resources { e := &c.Resources[i] e.ResourceID = stringPtr(e.Kind) } return c // Legacy coordinates and depleted IDs are never regenerated under new rules. } c.Resources = []p.Entity{} for _, r := range t.B.Gathering.Resources { if !slices.Contains(r.Biomes, biome.ID) { continue } hash := fnv.New64a() fmt.Fprintf(hash, "%d:%d:%d:%s", t.Seed, x, z, r.ID) rng := rand.New(rand.NewPCG(hash.Sum64(), uint64(t.Seed))) count := r.Density if biome.ID == "forest" && r.Model == "tree" { count = min(int64(32), count*2) } for i := int64(0); i < count && len(c.Resources) < 128; i++ { px, pz := (float64(x)+.08+rng.Float64()*.84)*c.Size, (float64(z)+.08+rng.Float64()*.84)*c.Size if math.Hypot(px, pz) > t.B.WorldRadius || math.Hypot(px, pz) < 25 { continue } c.Resources = append(c.Resources, p.Entity{ID: fmt.Sprintf("r:%d:%d:%s:%d", x, z, r.ID, i), Kind: r.Model, Name: r.Name, ResourceID: stringPtr(r.ID), SkillID: stringPtr(r.SkillID), RequiredLevel: &r.RequiredLevel, Position: t.Position(px, pz), HP: 1, MaxHP: 1, State: "available"}) } } if x == 0 && z == 0 { for i, id := range []string{"tree", "ore", "herb"} { for _, r := range t.B.Gathering.Resources { if r.ID == id { c.Resources = append(c.Resources, p.Entity{ID: "arrival:" + id, Kind: r.Model, Name: r.Name, ResourceID: stringPtr(id), SkillID: stringPtr(r.SkillID), RequiredLevel: &r.RequiredLevel, Position: t.Position(12+float64(i)*5, 12), HP: 1, MaxHP: 1, State: "available"}) } } } } return c }