From a6f0ad79e2e3a5b7c11eef1ffc3233f3a2766f77 Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Sun, 14 Jun 2026 22:38:23 -0400 Subject: feat: fractional ticks and server game_speed implemented. potentially insanely janky. --- internal/engine/tick.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'internal/engine') diff --git a/internal/engine/tick.go b/internal/engine/tick.go index d6d470a..419867d 100644 --- a/internal/engine/tick.go +++ b/internal/engine/tick.go @@ -1,12 +1,11 @@ package engine import ( + "math/rand" "sync" "time" ) -const TickDuration = 600 * time.Millisecond - type Callback func() bool type subscriber struct { @@ -49,14 +48,17 @@ func (e *Engine) Unsubscribe(id uint64) { delete(e.subscribers, id) } -func (e *Engine) Start() { +func (e *Engine) Start(tickLengthMs int) { e.mu.Lock() defer e.mu.Unlock() if e.running { return } e.running = true - e.ticker = time.NewTicker(TickDuration) + if tickLengthMs < 50 { + tickLengthMs = 50 + } + e.ticker = time.NewTicker(time.Duration(tickLengthMs) * time.Millisecond) e.stopCh = make(chan struct{}) go func() { @@ -103,3 +105,18 @@ func (e *Engine) processTick() { } } } + +func FractionalTicks(base, speed float64) int { + if speed <= 0 { + speed = 1 + } + value := base / speed + if value < 1 { + value = 1 + } + floor := int(value) + if rand.Float64() < value-float64(floor) { + return floor + 1 + } + return floor +} -- cgit v1.2.3