Pure rename, no behavior change; the full suite (RNG goldens,
generation invariants, scripted sessions, save round trip) is
unchanged and green.
- Creature flags: IsHuh→Confused, IsHalu→Hallucinating,
CanHuh→CanConfuse, CanSee→CanSeeInvisible, IsRun→Awake,
SeeMonst→SenseMonsters, IsCanc→Cancelled, IsLevit→Levitating,
IsBlind/IsGreed/IsHaste/IsTarget/IsHeld/IsInvis/IsMean/IsRegen/
IsFly/IsSlow → Blind/Greedy/Hasted/Targeted/Held/Invisible/Mean/
Regenerates/Flying/Slowed, IsFound→Found
- Object flags: IsCursed→Cursed, IsKnow→Known, IsMissl→Missile,
IsMany→Stackable, ObjIsFound→WasFound, IsProt→Protected
- Room flags: IsDark/IsGone/IsMaze → Dark/Gone/Maze; place flags:
FPass→FPassage, FPNum→FPassNum, FTMask→FTrapMask
- Trap types: TDoor→TrapDoor ... TMyst→TrapMystery
- Item subtypes: P*→Potion* (PLSD→PotionLSD, PMFind→
PotionDetectMonsters, ...), S*→Scroll* (SIDRorS→
ScrollIdentifyRingOrStick, ...), R*→Ring* (RAddHit→RingDexterity,
RNop→RingAdornment, ...), Ws*→Wand* (WsElect→WandLightning, ...),
weapons (TwoSword→WeaponTwoHandedSword, Shiraken→WeaponShuriken,
...), armor (RingMail→ArmorRingMail, ...)
- Counts: Max{Potions,Scrolls,Rings,Sticks,Weapons,Armors} →
Num{Potion,Scroll,Ring,Wand,Weapon,Armor}Types; NTraps→NumTrapTypes
- Level.NTraps field → TrapCount (also in the save snapshot)
- Original C constant names preserved as comment breadcrumbs in
types.go so the lineage stays greppable
TODO.md: step 1 moved to Completed, step 2 (typed kinds) promoted to
Next Step.
113 lines
2.9 KiB
Go
113 lines
2.9 KiB
Go
package game
|
|
|
|
import (
|
|
"encoding/gob"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestSaveRestoreRoundTrip(t *testing.T) {
|
|
g := mkGame(t, 4242)
|
|
// Dirty up some state so the round trip is meaningful.
|
|
g.Player.Purse = 123
|
|
g.Player.FoodLeft = 777
|
|
g.HasAmulet = true
|
|
g.Items.Potions[PotionHealing].Know = true
|
|
g.Items.Scrolls[ScrollMagicMapping].Guess = "map???"
|
|
g.Monsters['F'-'A'].Stats.Dmg = "3x1" // mutated bestiary must survive
|
|
if len(g.Level.Monsters) > 0 {
|
|
g.Level.Monsters[0].Flags.Set(Awake)
|
|
g.Level.Monsters[0].Dest = &g.Player.Pos
|
|
}
|
|
|
|
path := filepath.Join(t.TempDir(), "rogue.save")
|
|
if err := g.saveFile(path); err != nil {
|
|
t.Fatalf("saveFile: %v", err)
|
|
}
|
|
|
|
h, err := Restore(path, Config{Term: &testTerm{}})
|
|
if err != nil {
|
|
t.Fatalf("Restore: %v", err)
|
|
}
|
|
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Error("save file not deleted on restore (C anti-restart rule)")
|
|
}
|
|
|
|
if h.Player.Purse != 123 || h.Player.FoodLeft != 777 {
|
|
t.Errorf("player state lost: purse=%d food=%d",
|
|
h.Player.Purse, h.Player.FoodLeft)
|
|
}
|
|
if !h.HasAmulet {
|
|
t.Error("amulet flag lost")
|
|
}
|
|
if !h.Items.Potions[PotionHealing].Know {
|
|
t.Error("potion identification lost")
|
|
}
|
|
if h.Items.Scrolls[ScrollMagicMapping].Guess != "map???" {
|
|
t.Error("scroll guess lost")
|
|
}
|
|
if h.Monsters['F'-'A'].Stats.Dmg != "3x1" {
|
|
t.Error("mutated bestiary lost")
|
|
}
|
|
if h.Rng.Seed != g.Rng.Seed {
|
|
t.Error("RNG state lost")
|
|
}
|
|
if renderMap(h) != renderMap(g) {
|
|
t.Error("restored level map differs")
|
|
}
|
|
if len(h.Level.Monsters) != len(g.Level.Monsters) {
|
|
t.Fatalf("monster count %d != %d",
|
|
len(h.Level.Monsters), len(g.Level.Monsters))
|
|
}
|
|
if len(g.Level.Monsters) > 0 {
|
|
m := h.Level.Monsters[0]
|
|
if m.Dest != &h.Player.Pos {
|
|
t.Error("monster chase target not re-aliased to the hero")
|
|
}
|
|
if h.Level.MonsterAt(m.Pos.Y, m.Pos.X) != m {
|
|
t.Error("map monster index not rebuilt")
|
|
}
|
|
if m.Room == nil {
|
|
t.Error("monster room pointer not rebuilt")
|
|
}
|
|
}
|
|
// Equipment aliasing: the wielded mace must be the same *Object as the
|
|
// one in the pack.
|
|
st := g.snapshot()
|
|
t.Logf("snapshot indices: weapon=%d armor=%d rings=%v packlen=%d",
|
|
st.Player.CurWeapon, st.Player.CurArmor, st.Player.CurRing,
|
|
len(st.Player.Body.Pack))
|
|
t.Logf("restored: CurWeapon=%p pack has %d items", h.Player.CurWeapon,
|
|
len(h.Player.Pack))
|
|
found := false
|
|
for i, o := range h.Player.Pack {
|
|
t.Logf(" pack[%d]=%p type=%c which=%d", i, o, o.Type, o.Which)
|
|
if o == h.Player.CurWeapon {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Error("restored CurWeapon is not aliased into the pack")
|
|
}
|
|
}
|
|
|
|
func TestRestoreRejectsWrongVersion(t *testing.T) {
|
|
g := mkGame(t, 1)
|
|
path := filepath.Join(t.TempDir(), "rogue.save")
|
|
st := g.snapshot()
|
|
st.Version = "0.0.0"
|
|
f, err := os.Create(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := gob.NewEncoder(f).Encode(st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.Close()
|
|
if _, err := Restore(path, Config{}); err == nil {
|
|
t.Error("restore accepted an out-of-date save")
|
|
}
|
|
}
|