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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
package game
import (
"fmt"
"gopkg.in/yaml.v3"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/world"
)
// roomRemove is the inverse of roomInsert: it deletes the room reached via the
// current room's dir exit (the "inserted" room B) and pulls the far room C (B's
// dir exit's target) back to the current room, rewiring A→dir→C and
// C→oppositeDir→A. The beyond-rooms shift one step toward A. It fails when:
// - B does not lead back to the current room via oppositeDir (not an insert
// chain),
// - B has no forward dir exit (a dead end — use `undig` for that),
// - B has any exit other than oppositeDir and dir (other exits in the deleted
// room),
// - C's opposite exit does not point back at B (broken reciprocity),
// - some other room points into B (extra inbound edge — B was modified since
// insertion),
// - the pull would cause a grid overlap or twist (RemoveGridConflicts).
//
// Admin gating is inherited from executeRoom.
func (g *Game) roomRemove(sess *net.Session, args []string) {
p := sess.Player
if p == nil {
return
}
if len(args) == 0 {
sess.WriteLine("Usage: room remove <direction>")
return
}
dir := g.World.ResolveExit(args[0])
if dir == "" {
sess.WriteLine("Invalid direction. Use n/s/e/w/ne/nw/se/sw/u/d.")
return
}
oppositeDir := world.OppositeExit[dir]
aID := p.RoomID
aPath, ok := g.World.GetRoomPath(aID)
if !ok {
sess.WriteLine("Error: can't find current room file.")
return
}
aRoom, err := g.World.LoadRoom(aID)
if err != nil {
sess.WriteLine("Error loading current room.")
return
}
aExit, hasExit := aRoom.Exits[dir]
if !hasExit {
sess.WriteLine(fmt.Sprintf("There is no exit to the %s from here.", dir))
return
}
bID := aExit.Room
if bID == aID {
sess.WriteLine("That exit loops back to this room; nothing to remove.")
return
}
bRoom, err := g.World.LoadRoom(bID)
if err != nil {
sess.WriteLine(fmt.Sprintf("Target room %d not found.", bID))
return
}
bName := fmt.Sprintf("#%d (%s)", bID, bRoom.Name)
// Guard 1: B must lead back to A via oppositeDir.
backExit, hasBack := bRoom.Exits[oppositeDir]
if !hasBack || backExit.Room != aID {
sess.WriteLine(fmt.Sprintf(
"Cannot remove: %s does not lead back here via %s (not an insert chain).",
bName, oppositeDir))
return
}
// Guard 2: B must have a forward dir exit (a far room C to pull).
fwdExit, hasFwd := bRoom.Exits[dir]
if !hasFwd {
sess.WriteLine(fmt.Sprintf(
"Cannot remove: %s has no room beyond to pull (use `undig %s` to delete a dead-end room).",
bName, dir))
return
}
cID := fwdExit.Room
if cID == bID || cID == aID {
sess.WriteLine(fmt.Sprintf("Cannot remove: the far room in %s is not a distinct room.", dir))
return
}
// Guard 3: B must have exactly two exits (oppositeDir back to A and dir
// forward to C). Any other exit means B is not a clean insert chain.
if len(bRoom.Exits) != 2 {
sess.WriteLine(fmt.Sprintf(
"Cannot remove: %s has other exits besides %s and %s.",
bName, oppositeDir, dir))
return
}
// Guard 4: C's opposite exit must point back at B.
cRoom, err := g.World.LoadRoom(cID)
if err != nil {
sess.WriteLine(fmt.Sprintf("Far room %d not found.", cID))
return
}
cOppExit, hasCOpp := cRoom.Exits[oppositeDir]
if !hasCOpp || cOppExit.Room != bID {
sess.WriteLine(fmt.Sprintf(
"Cannot remove: the far room #%d does not lead back to %s via %s.",
cID, bName, oppositeDir))
return
}
// Guard 5: no other room points into B. The only allowed inbound edges are
// A→dir→B and C→oppositeDir→B.
if extra := world.CountExtraInbound(g.DataDir, bID, aID, cID, dir, oppositeDir); extra > 0 {
sess.WriteLine(fmt.Sprintf(
"Cannot remove: %d other exit(s) point into %s (it was modified since insertion).",
extra, bName))
return
}
// Guard 6: the pull must not cause a grid overlap or twist.
conflicts := world.RemoveGridConflicts(aID, dir, func(id int) (*world.Room, bool) {
r, err := g.World.LoadRoom(id)
if err != nil {
return nil, false
}
return r, true
})
if len(conflicts) > 0 {
for _, c := range conflicts {
switch c.Kind {
case "twist":
room, _ := g.World.LoadRoom(c.Target)
name := fmt.Sprintf("#%d", c.Target)
if room != nil {
name = fmt.Sprintf("#%d (%s)", c.Target, room.Name)
}
sess.WriteLine(fmt.Sprintf(
"Cannot remove: pulling the far end would give room %s an ambiguous grid position.",
name))
case "overlap":
occRoom, _ := g.World.LoadRoom(c.Occupier)
occName := fmt.Sprintf("#%d", c.Occupier)
if occRoom != nil {
occName = fmt.Sprintf("#%d (%s)", c.Occupier, occRoom.Name)
}
sess.WriteLine(fmt.Sprintf(
"Cannot remove: pulling the far end would cause grid collision with %s.",
occName))
}
}
return
}
// Stage A, C, and B's deletion into ONE journal transaction: the previous
// sequential writes could delete B after rewriting A but before C, leaving
// the map half-pulled with no recovery record. The transaction restores
// every before-image on any failure or crash.
txn := g.newTxn()
// Rewire A's dir exit to C, preserving A's current dir-exit props.
aRoom.Exits[dir] = world.RewirePreserving(aExit, cID)
aData, err := yaml.Marshal(aRoom)
if err != nil {
sess.WriteLine("Error updating current room YAML.")
return
}
if err := txn.Replace(aPath, aData, 0644); err != nil {
sess.WriteLine(fmt.Sprintf("Error writing current room file: %v", err))
return
}
// Rewire C's opposite exit to A, preserving C's current opposite-exit props.
cRoom.Exits[oppositeDir] = world.RewirePreserving(cOppExit, aID)
cPath, cPathOK := g.World.GetRoomPath(cID)
if cPathOK {
cData, cerr := yaml.Marshal(cRoom)
if cerr != nil {
sess.WriteLine(fmt.Sprintf("Error updating room %d YAML: %v", cID, cerr))
return
}
if err := txn.Replace(cPath, cData, 0644); err != nil {
sess.WriteLine(fmt.Sprintf("Error linking the opposite exit in room %d: %v", cID, err))
return
}
}
// Delete B's file in the same transaction.
bPath, bPathOK := g.World.GetRoomPath(bID)
if !bPathOK {
sess.WriteLine(fmt.Sprintf("Room file for %d not found.", bID))
return
}
if err := txn.Delete(bPath); err != nil {
sess.WriteLine(fmt.Sprintf("Error deleting room file: %v", err))
return
}
if err := txn.Commit(); err != nil {
sess.WriteLine(fmt.Sprintf("Error committing room remove: %v", err))
return
}
g.World.RebuildRoomIndex(g.DataDir)
g.MobStore.RemoveMobsInRoom(bID)
g.World.ClearRoomState(bID)
g.clearGuardWatchRoom(bID)
// Drop any cached in-room games and refund their outstanding wagers
// before the room stops existing (same eviction semantics as the
// admin editor's room deletion).
g.EvictCasinoRoom(bID)
// Relocate any other players standing in B to A (the current player is in
// A and unaffected). Mirrors performUndig's relocate tail.
for _, other := range g.Hub.AllSessions() {
if other == sess || other.Player == nil {
continue
}
if other.Player.RoomID == bID {
other.Player.RoomID = aID
if g.Hub != nil {
g.Hub.EnterRoom(other, aID)
}
other.WriteLine("The room around you collapses. You find yourself elsewhere.")
g.doLook(other)
}
}
cName := fmt.Sprintf("#%d (%s)", cID, cRoom.Name)
sess.WriteLine(fmt.Sprintf("You remove %s and pull %s back to the %s.", bName, cName, dir))
g.doLook(sess)
}
// countExtraInbound was moved to world.CountExtraInbound so the in-game and
// admin removal paths share one implementation.
|