F5: Simulation core and player command stream
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>
This commit is contained in:
@@ -6,9 +6,11 @@ const FORBIDDEN: Array[String] = [
|
||||
"Node2D", "Node3D", "MeshInstance3D", "Camera3D", "Control", "CanvasItem", "Viewport",
|
||||
"SceneTree", "get_tree", "Input.",
|
||||
]
|
||||
const NONDETERMINISTIC: Array[String] = [
|
||||
"randf(", "randi(", "randf_range(", "randi_range(", "randomize(", "Time.", "OS.get_ticks",
|
||||
]
|
||||
const NONDETERMINISTIC: Array[String] = ["Time.", "OS.get_ticks"]
|
||||
## Global random calls. A method call on a seeded stream (stream.randf()) is allowed.
|
||||
const GLOBAL_RANDOM: String = "(?<![\\w.])(randf|randi|randf_range|randi_range|randfn|randomize|rand_from_seed)\\("
|
||||
## The one script allowed to create a generator, because it seeds them.
|
||||
const RNG_OWNER: String = "res://src/sim/sim_rng.gd"
|
||||
|
||||
|
||||
func test_simulation_has_no_presentation_dependency() -> void:
|
||||
@@ -19,16 +21,51 @@ func test_simulation_has_no_unseeded_randomness() -> void:
|
||||
_scan(NONDETERMINISTIC)
|
||||
|
||||
|
||||
func test_simulation_makes_no_global_random_calls() -> void:
|
||||
for path: String in _scripts_under("res://src/sim"):
|
||||
for line: String in _code_lines(path):
|
||||
if _global_random().search(line) != null:
|
||||
fail("%s makes a global random call: %s" % [path, line.strip_edges()])
|
||||
|
||||
|
||||
func test_only_sim_rng_creates_generators() -> void:
|
||||
for path: String in _scripts_under("res://src/sim"):
|
||||
if path == RNG_OWNER:
|
||||
continue
|
||||
for line: String in _code_lines(path):
|
||||
if line.contains("RandomNumberGenerator.new"):
|
||||
fail("%s creates its own generator: %s" % [path, line.strip_edges()])
|
||||
|
||||
|
||||
func test_scanner_tells_global_calls_from_seeded_ones() -> void:
|
||||
var pattern: RegEx = _global_random()
|
||||
assert_not_null(pattern.search("var x: float = randf()"), "global call must match")
|
||||
assert_not_null(pattern.search("foo(randi_range(1, 2))"), "nested global call must match")
|
||||
assert_null(pattern.search("var x: float = stream.randf()"), "seeded call must not match")
|
||||
assert_null(pattern.search("my_randf(1)"), "other identifiers must not match")
|
||||
|
||||
|
||||
func _global_random() -> RegEx:
|
||||
var pattern: RegEx = RegEx.new()
|
||||
pattern.compile(GLOBAL_RANDOM)
|
||||
return pattern
|
||||
|
||||
|
||||
func _code_lines(path: String) -> Array[String]:
|
||||
var lines: Array[String] = []
|
||||
for line: String in FileAccess.get_file_as_string(path).split("\n"):
|
||||
lines.append(line.get_slice("#", 0))
|
||||
return lines
|
||||
|
||||
|
||||
func _scan(needles: Array[String]) -> void:
|
||||
var paths: Array[String] = _scripts_under("res://src/sim")
|
||||
assert_true(not paths.is_empty(), "expected scripts in src/sim")
|
||||
for path: String in paths:
|
||||
var source: String = FileAccess.get_file_as_string(path)
|
||||
for needle: String in needles:
|
||||
# Comments may mention these; only code counts.
|
||||
for line: String in source.split("\n"):
|
||||
var code: String = line.get_slice("#", 0)
|
||||
if code.contains(needle):
|
||||
for line: String in _code_lines(path):
|
||||
if line.contains(needle):
|
||||
fail("%s uses %s: %s" % [path, needle, line.strip_edges()])
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user