1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package world
import (
"thehouseoficarus/internal/behavior"
)
// RewriteRoomIDs remaps every genuine room-ID reference in this room per idMap
// (oldID -> newID): exit targets; every Step.Teleport and Step.SpawnMob
// .DespawnRooms inside on_enter/on_exit/
// on_traverse/on_use/on_look/on_kill blocks; every Condition.Room anywhere in
// the room (exits, descriptions, trigger conditions, per-step conditions,
// including nested all_of/any_of); and mob WanderRooms. It does NOT touch
// author-chosen set_flags/set_player_flags values or condition `value:` fields
// — those are not structural room references.
//
// idMap is assumed to be a bijection (each old ID maps to a distinct new ID);
// this holds for both swap (A<->B) and rename-to-unused (A->B). Returns
// whether any field was changed.
func (r *Room) RewriteRoomIDs(idMap map[int]int) bool {
if r == nil || len(idMap) == 0 {
return false
}
changed := false
// Exit targets + their on_traverse steps + exit/desc conditions.
for dir, exit := range r.Exits {
var exitChanged bool
if remapInt(idMap, &exit.Room) {
exitChanged = true
}
if remapTriggers(idMap, exit.OnTraverse) {
exitChanged = true
}
if exit.Condition != nil && remapCondition(idMap, exit.Condition) {
exitChanged = true
}
if exitChanged {
r.Exits[dir] = exit
changed = true
}
}
// Every trigger-shaped block on the room shares the Step list + Condition
// structure. Remap their steps and conditions uniformly.
for i := range r.OnEnter {
c := false
if remapStepList(idMap, r.OnEnter[i].Steps) {
c = true
}
if remapCondition(idMap, r.OnEnter[i].Condition) {
c = true
}
changed = changed || c
}
for i := range r.OnExit {
c := false
if remapStepList(idMap, r.OnExit[i].Steps) {
c = true
}
if remapCondition(idMap, r.OnExit[i].Condition) {
c = true
}
changed = changed || c
}
// Conditional room descriptions carry a Condition.
for i := range r.Description {
if remapCondition(idMap, r.Description[i].Condition) {
changed = true
}
}
// Per-room local objects: on_use/on_look steps + object desc conditions.
for i := range r.Objects {
if r.Objects[i].Local != nil {
loc := r.Objects[i].Local
if remapTriggers(idMap, loc.OnUse) {
changed = true
}
if remapTriggers(idMap, loc.OnLook) {
changed = true
}
for j := range loc.Description {
if remapCondition(idMap, loc.Description[j].Condition) {
changed = true
}
}
}
}
for i := range r.Mobs {
if remapIntSlice(idMap, r.Mobs[i].WanderRooms) {
changed = true
}
}
return changed
}
// remapTriggers remaps step fields across a []behavior.Trigger (each trigger's
// Steps + per-step Condition + trigger-level Condition).
func remapTriggers(idMap map[int]int, list []behavior.Trigger) bool {
changed := false
for i := range list {
c := false
if remapStepList(idMap, list[i].Steps) {
c = true
}
if remapCondition(idMap, list[i].Condition) {
c = true
}
changed = changed || c
}
return changed
}
// remapStepList remaps Step.Teleport and Step.SpawnMob.DespawnRooms plus every
// step's per-step Condition.Room across a single Trigger's Steps.
func remapStepList(idMap map[int]int, steps []behavior.Step) bool {
changed := false
for i := range steps {
s := &steps[i]
if remapInt(idMap, &s.Teleport) {
changed = true
}
if s.SpawnMob != nil && remapIntSlice(idMap, s.SpawnMob.DespawnRooms) {
changed = true
}
if remapCondition(idMap, s.Condition) {
changed = true
}
}
return changed
}
// remapCondition walks a Condition tree and remaps any Room field (a
// structural room reference) it finds, recursing into AllOf/AnyOf.
func remapCondition(idMap map[int]int, c *behavior.Condition) bool {
if c == nil {
return false
}
changed := false
if c.Room != 0 && remapInt(idMap, &c.Room) {
changed = true
}
for i := range c.AllOf {
if remapCondition(idMap, &c.AllOf[i]) {
changed = true
}
}
for i := range c.AnyOf {
if remapCondition(idMap, &c.AnyOf[i]) {
changed = true
}
}
return changed
}
func remapInt(idMap map[int]int, n *int) bool {
if newID, ok := idMap[*n]; ok && newID != *n {
*n = newID
return true
}
return false
}
func remapIntSlice(idMap map[int]int, slice []int) bool {
changed := false
for i := range slice {
if remapInt(idMap, &slice[i]) {
changed = true
}
}
return changed
}
|