Add the fixed-tick Simulation, the EntityRegistry with never-reused IDs, SimRng's named seeded streams, and the 14-byte PlayerCommand used by humans, bots and the network. state_hash is a SHA-256 over a canonical encoding of the state. Movement is a flat placeholder until the v0.2 character controller. Tests replay a 24-player command log over 10,000 ticks and scan src/sim for unseeded randomness and presentation code. Closes #5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
47 lines
1.4 KiB
GDScript
47 lines
1.4 KiB
GDScript
## The match's seeded randomness. Each system draws from its own named stream, so one
|
|
## system's extra rolls can't change what another rolls.
|
|
class_name SimRng
|
|
extends RefCounted
|
|
|
|
const FNV_PRIME: int = 16777619
|
|
const FNV_OFFSET_LOW: int = 2166136261
|
|
const FNV_OFFSET_HIGH: int = 3735928559
|
|
const MASK_32: int = 0xFFFFFFFF
|
|
|
|
var match_seed: int = 0
|
|
|
|
var _streams: Dictionary = {}
|
|
|
|
|
|
func _init(seed_value: int) -> void:
|
|
match_seed = seed_value
|
|
|
|
|
|
## Returns the named stream, creating it on first use.
|
|
func stream(stream_name: String) -> RandomNumberGenerator:
|
|
if not _streams.has(stream_name):
|
|
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
|
rng.seed = match_seed ^ _hash_name(stream_name)
|
|
_streams[stream_name] = rng
|
|
return _streams[stream_name]
|
|
|
|
|
|
## Stream names, sorted, so that state can be hashed in a stable order.
|
|
func stream_names() -> Array[String]:
|
|
var names: Array[String] = []
|
|
for key: String in _streams:
|
|
names.append(key)
|
|
names.sort()
|
|
return names
|
|
|
|
|
|
## 64-bit name hash built from two 32-bit FNV-1a passes. It stays within 64-bit signed
|
|
## arithmetic, and doesn't rely on String.hash(), which can change between engine versions.
|
|
static func _hash_name(text: String) -> int:
|
|
var low: int = FNV_OFFSET_LOW
|
|
var high: int = FNV_OFFSET_HIGH
|
|
for byte: int in text.to_utf8_buffer():
|
|
low = ((low ^ byte) * FNV_PRIME) & MASK_32
|
|
high = ((high ^ (byte + 1)) * FNV_PRIME) & MASK_32
|
|
return (high << 32) | low
|