go: port foundation — types, RNG, tables, daemon scheduler
Module sneak.berlin/go/rogue, package game: - types.go: all rogue.h constants, flag types, Coord/Stats/Room/ObjInfo - creature.go/object.go: the THING union split into Creature (Player/ Monster) and Object; slices replace the C linked lists - level.go: Place map with the C column-major layout and chat/flat/moat/ winat access methods - tables.go: extern.c + init.c data (bestiary, item tables, name pools) - rng.go: the exact C LCG (seed*11109+13849), golden-tested against a compiled C reference for seed compatibility - init.go: per-game item appearance randomization (init.c) - daemon.go/daemons.go: scheduler with DaemonID replacing function pointers (ids match the C save format's mapping) - game.go: RogueGame holds all former globals; NewGame constructor
This commit is contained in:
64
game/rng_test.go
Normal file
64
game/rng_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package game
|
||||
|
||||
import "testing"
|
||||
|
||||
// The golden values below were produced by the original C generator
|
||||
// (seed = seed*11109+13849; rnd(range) = abs(RN) % range) compiled and run
|
||||
// on this machine. They lock in seed compatibility with the C game.
|
||||
func TestRndMatchesCImplementation(t *testing.T) {
|
||||
golden := map[int32][]int{
|
||||
1: {0, 30, 79, 7, 87, 1, 23, 7, 57, 98},
|
||||
12345: {92, 92, 45, 98, 24, 39, 92, 67, 3, 7},
|
||||
2026: {43, 90, 6, 2, 43, 34, 20, 5, 67, 71},
|
||||
1751808000: {61, 51, 56, 99, 68, 45, 88, 50, 46, 88},
|
||||
}
|
||||
for seed, want := range golden {
|
||||
r := &Rng{Seed: seed}
|
||||
for i, w := range want {
|
||||
if got := r.Rnd(100); got != w {
|
||||
t.Errorf("seed %d: rnd(100) call %d = %d, want %d", seed, i, got, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRollMatchesCImplementation(t *testing.T) {
|
||||
golden := map[int32][]int{
|
||||
1: {6, 8, 10},
|
||||
12345: {10, 10, 15},
|
||||
2026: {10, 10, 13},
|
||||
1751808000: {7, 9, 9},
|
||||
}
|
||||
for seed, want := range golden {
|
||||
r := &Rng{Seed: seed}
|
||||
for i, w := range want {
|
||||
if got := r.Roll(3, 6); got != w {
|
||||
t.Errorf("seed %d: roll(3,6) call %d = %d, want %d", seed, i, got, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// rnd(0) must return 0 without stepping the generator: the C macro
|
||||
// short-circuits before evaluating RN, and BEFORE/AFTER depend on it.
|
||||
func TestRndZeroDoesNotStep(t *testing.T) {
|
||||
r := &Rng{Seed: 42}
|
||||
if got := r.Rnd(0); got != 0 {
|
||||
t.Fatalf("rnd(0) = %d, want 0", got)
|
||||
}
|
||||
if r.Seed != 42 {
|
||||
t.Fatalf("rnd(0) stepped the generator: seed = %d, want 42", r.Seed)
|
||||
}
|
||||
}
|
||||
|
||||
// spread(1)==1 and spread(2)==2 deterministically; the C BEFORE/AFTER
|
||||
// constants rely on this.
|
||||
func TestSpreadSmallValues(t *testing.T) {
|
||||
g := &RogueGame{Rng: &Rng{Seed: 7}}
|
||||
if got := g.spread(1); got != 1 {
|
||||
t.Errorf("spread(1) = %d, want 1", got)
|
||||
}
|
||||
if got := g.spread(2); got != 2 {
|
||||
t.Errorf("spread(2) = %d, want 2", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user