RegionIO/internal/server/spawner.go
Master290 57214fbd76 Run the world clock so day and night happen
set_time was declared nowhere and sent never, so the client's sky was frozen
wherever it started: the sun did not move, night did not fall, and nothing in
the world had a time.

26.1.2 replaced the old (gameTime, dayTime, doDaylightCycle) triple with a
registry of named clocks. The packet now carries a fixed-width game time plus a
map from world clock to (totalTicks, partialTick, rate); the client advances
each clock locally at its rate and drives the minecraft:day timeline -- a
keyframe track over a 24000-tick period -- from the overworld clock. The clock
registry is already among the 28 we sync, so the id is looked up rather than
hardcoded and the server refuses to start if it is missing.

The counter rides the entity tick loop because that is the only loop already
running at 20 TPS. It belongs on the single authoritative tick the engine still
needs; putting a sixth ticker beside the five that exist would make that worse.
Broadcast every second, which is what vanilla does -- the client interpolates in
between, so the resend only corrects drift.

The clock also persists now. The world metadata file was written once and never
touched again; it is atomically rewritable, carries gameTime and dayTime, and a
file written before those fields existed still opens and resumes at dawn as it
did. Saved every 30 seconds alongside the chunk autosave, and once more on
shutdown after the final flush.
2026-07-27 02:45:28 +03:00

142 lines
3.9 KiB
Go

package server
import (
"context"
"log/slog"
"math"
"math/rand"
"time"
"regionio/internal/protocol"
"regionio/internal/registry"
"regionio/internal/world"
)
// StartSpawning begins the entity tick and spawn loops. log matches
// Cache.StartAutosave's convention: these are background loops that have to be
// able to report a failure nobody is waiting on.
func (s *Server) StartSpawning(ctx context.Context, log *slog.Logger) {
go s.entityTickLoop(ctx, log)
go s.mobSpawnLoop(ctx)
}
func (s *Server) entityTickLoop(ctx context.Context, log *slog.Logger) {
ticker := time.NewTicker(50 * time.Millisecond) // 20 TPS
defer ticker.Stop()
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
// The world clock rides this loop because it is the only one
// already running at 20 TPS. It belongs on the single authoritative
// tick the engine still needs.
if gameTime, dayTime := s.advanceWorldTime(); gameTime%timeSyncTicks == 0 {
s.Broadcast(protocol.PlaySetTime, encodeSetTime(gameTime, dayTime))
if gameTime%timePersistTicks == 0 {
if err := s.SaveWorldTime(); err != nil && log != nil {
log.Warn("saving world clock", "err", err)
}
}
}
all := s.entities.All()
for i := range all {
snapshot := all[i]
yBelow := int(math.Floor(snapshot.Y - 0.1))
blockBelow := s.chunks.GetBlock(int(math.Floor(snapshot.X)), yBelow, int(math.Floor(snapshot.Z)))
nearest, hasPlayer := s.NearestPlayer(snapshot.X, snapshot.Y, snapshot.Z)
s.entities.Update(all[i].ID, func(e *world.Entity) {
// Apply gravity
if blockBelow == world.StateAir || blockBelow == world.StateWater { // Air or Water
e.VelocityY -= 80 // gravity acceleration
if e.VelocityY < -3000 {
e.VelocityY = -3000 // terminal velocity
}
} else {
e.VelocityY = 0
e.Y = float64(yBelow + 1)
// Basic random wandering or player tracking when on ground
if hasPlayer && e.TypeName == "minecraft:zombie" {
// Zombies move towards the player
dx := nearest[0] - e.X
dz := nearest[2] - e.Z
dist := math.Sqrt(dx*dx + dz*dz)
if dist > 1.0 && dist < 32.0 {
e.X += (dx / dist) * 0.15
e.Z += (dz / dist) * 0.15
// Simple yaw calculation
e.Yaw = float32(math.Atan2(-dx, dz) * (180 / math.Pi))
}
} else {
// Random wander
e.X += (rng.Float64() - 0.5) * 0.2
e.Z += (rng.Float64() - 0.5) * 0.2
e.Yaw += float32((rng.Float64() - 0.5) * 10.0)
}
}
if e.VelocityY != 0 {
e.Y += float64(e.VelocityY) / 8000.0
}
})
}
}
}
}
func (s *Server) mobSpawnLoop(ctx context.Context) {
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
pigType := registry.EntityTypeIndex("minecraft:pig")
zombieType := registry.EntityTypeIndex("minecraft:zombie")
if pigType < 0 || zombieType < 0 {
return
}
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if s.PlayerCount() == 0 || s.entities.Count() >= 20 {
continue
}
s.spawnMobNearPlayer(rng, pigType, zombieType)
}
}
}
func (s *Server) spawnMobNearPlayer(rng *rand.Rand, pigType, zombieType int) bool {
players := s.PlayerSnapshots()
if len(players) == 0 {
return false
}
player := players[rng.Intn(len(players))]
angle := rng.Float64() * 2 * math.Pi
distance := 16.0 + rng.Float64()*16.0
x := int(math.Floor(player.X + math.Cos(angle)*distance))
z := int(math.Floor(player.Z + math.Sin(angle)*distance))
y, ok := s.chunks.SafeSpawnY(x, z)
if !ok {
return false
}
typeID := pigType
typeName := "minecraft:pig"
if rng.Float32() < 0.5 {
typeID = zombieType
typeName = "minecraft:zombie"
}
s.entities.Add(&world.Entity{
TypeID: typeID,
TypeName: typeName,
X: float64(x) + 0.5,
Y: float64(y),
Z: float64(z) + 0.5,
})
return true
}