aboutsummaryrefslogtreecommitdiff
path: root/internal/world/mob_rekey_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/world/mob_rekey_test.go')
-rw-r--r--internal/world/mob_rekey_test.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/internal/world/mob_rekey_test.go b/internal/world/mob_rekey_test.go
index 8118059..af4dd5c 100644
--- a/internal/world/mob_rekey_test.go
+++ b/internal/world/mob_rekey_test.go
@@ -51,3 +51,51 @@ func TestRekeyRoomsUpdatesMobDespawnRooms(t *testing.T) {
t.Fatalf("despawn_rooms = %v, want [3 7]", got)
}
}
+
+// TestSeedMobsKeysByDefOccurrenceAndPrunes: reordering a room's mobs list
+// must not orphan old instances (spawning duplicates) and removing an entry
+// must delete its seeded instance. Keys use a per-defID occurrence counter, so
+// the same declaration keeps the same key across reorders.
+func TestSeedMobsKeysByDefOccurrenceAndPrunes(t *testing.T) {
+ s := &MobStore{
+ defs: map[string]*MobDef{
+ "rat": {ID: "rat", Name: "rat"},
+ "bat": {ID: "bat", Name: "bat"},
+ },
+ instances: map[string]*MobInstance{
+ "rat_1_0": {InstanceID: "rat_1_0", DefID: "rat", RoomID: 1, HomeRoomID: 1, HP: 1, MaxHP: 1},
+ "bat_1_0": {InstanceID: "bat_1_0", DefID: "bat", RoomID: 1, HomeRoomID: 1, HP: 1, MaxHP: 1},
+ },
+ }
+ // Reorder and drop rat: bat keeps its key, rat is pruned, no duplicates.
+ s.SeedMobs(1, []RoomMob{{ID: "bat"}})
+ if s.instances["bat_1_0"] == nil {
+ t.Fatal("surviving declared mob was dropped")
+ }
+ if _, ok := s.instances["rat_1_0"]; ok {
+ t.Fatal("removed mob was not pruned")
+ }
+ if len(s.instances) != 1 {
+ t.Fatalf("instances = %v, want only bat_1_0", s.instances)
+ }
+}
+
+func TestSeedMobsReorderDoesNotDuplicate(t *testing.T) {
+ s := &MobStore{
+ defs: map[string]*MobDef{
+ "rat": {ID: "rat", Name: "rat"},
+ "bat": {ID: "bat", Name: "bat"},
+ },
+ instances: map[string]*MobInstance{
+ "rat_1_0": {InstanceID: "rat_1_0", DefID: "rat", RoomID: 1, HomeRoomID: 1, HP: 1, MaxHP: 1},
+ "bat_1_0": {InstanceID: "bat_1_0", DefID: "bat", RoomID: 1, HomeRoomID: 1, HP: 1, MaxHP: 1},
+ },
+ }
+ s.SeedMobs(1, []RoomMob{{ID: "bat"}, {ID: "rat"}})
+ if len(s.instances) != 2 {
+ t.Fatalf("reorder changed instance count: %v", s.instances)
+ }
+ if s.instances["rat_1_0"] == nil || s.instances["bat_1_0"] == nil {
+ t.Fatalf("reorder lost a declared mob: %v", s.instances)
+ }
+}