Add basic entity tracking, mob spawning and syncing

This commit is contained in:
Master290 2026-06-27 21:13:20 +03:00
parent 7e285e8068
commit 7af0acd3a3
6 changed files with 238 additions and 7 deletions

View file

@ -51,9 +51,10 @@ func DefaultConfig() Config {
// Server is the top-level core shared across all connections.
type Server struct {
cfg Config
chunks *world.Cache
store *world.Store // nil when persistence is disabled
cfg Config
chunks *world.Cache
store *world.Store // nil when persistence is disabled
entities *world.EntityManager
}
// New constructs a Server from cfg. When cfg.WorldDir is set, the world is
@ -61,19 +62,21 @@ type Server struct {
// only. A returned error (e.g. the world dir cannot be created) is fatal.
func New(cfg Config) (*Server, error) {
gen := world.NewVanillaGenerator(cfg.WorldSeed)
em := world.NewEntityManager()
if cfg.WorldDir == "" {
// No persistence; keep eviction off too (flat/test worlds expect full
// presence). Real servers set WorldDir and MaxCachedChunks together.
return &Server{cfg: cfg, chunks: world.NewCache(int32(cfg.CompressionThreshold), gen)}, nil
return &Server{cfg: cfg, chunks: world.NewCache(int32(cfg.CompressionThreshold), gen), entities: em}, nil
}
store, err := world.NewStore(cfg.WorldDir)
if err != nil {
return nil, err
}
return &Server{
cfg: cfg,
chunks: world.NewCacheWithLimit(int32(cfg.CompressionThreshold), gen, store, cfg.MaxCachedChunks),
store: store,
cfg: cfg,
chunks: world.NewCacheWithLimit(int32(cfg.CompressionThreshold), gen, store, cfg.MaxCachedChunks),
store: store,
entities: em,
}, nil
}
@ -83,6 +86,9 @@ func (s *Server) Config() Config { return s.cfg }
// Chunks returns the shared chunk cache.
func (s *Server) Chunks() *world.Cache { return s.chunks }
// Entities returns the shared entity manager.
func (s *Server) Entities() *world.EntityManager { return s.entities }
// Store returns the on-disk world store, or nil if persistence is disabled.
func (s *Server) Store() *world.Store { return s.store }

View file

@ -0,0 +1,66 @@
package server
import (
"math/rand"
"time"
"regionio/internal/registry"
"regionio/internal/world"
)
// StartSpawning begins the entity tick and spawn loops.
func (s *Server) StartSpawning() {
go s.entityTickLoop()
go s.mobSpawnLoop()
}
func (s *Server) entityTickLoop() {
ticker := time.NewTicker(50 * time.Millisecond) // 20 TPS
defer ticker.Stop()
for range ticker.C {
all := s.entities.All()
for _, e := range all {
// Basic random wandering
e.X += (rand.Float64() - 0.5) * 0.2
e.Z += (rand.Float64() - 0.5) * 0.2
e.Yaw += float32((rand.Float64() - 0.5) * 10.0)
}
}
}
func (s *Server) mobSpawnLoop() {
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
pigType := registry.Index("minecraft:entity_type", "minecraft:pig")
zombieType := registry.Index("minecraft:entity_type", "minecraft:zombie")
if pigType < 0 || zombieType < 0 {
return
}
for range ticker.C {
all := s.entities.All()
if len(all) > 50 {
continue // limit to 50 entities
}
// Spawn near the spawn point (8.5, 200, 8.5)
x := (rand.Float64() - 0.5) * 30.0
z := (rand.Float64() - 0.5) * 30.0
t := pigType
name := "minecraft:pig"
if rand.Float32() < 0.5 {
t = zombieType
name = "minecraft:zombie"
}
s.entities.Add(&world.Entity{
TypeID: t,
TypeName: name,
X: x + 8.5,
Y: 200.0, // They float for now since there's no gravity
Z: z + 8.5,
})
}
}