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
|
package game
import (
"fmt"
"strings"
"gopkg.in/yaml.v3"
"thehouseoficarus/internal/behavior"
"thehouseoficarus/internal/color"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/player"
"thehouseoficarus/internal/world"
)
func (g *Game) executeRoom(sess *net.Session, args []string, rawInput string) {
if !g.checkAdmin(sess) {
sess.WriteLine("Unknown command.")
return
}
p := sess.Player
if p == nil {
return
}
if len(args) == 0 {
g.showRoomHelp(sess)
return
}
action := strings.ToLower(args[0])
rest := args[1:]
switch action {
case "name":
g.roomSetName(sess, rest)
case "desc":
g.roomSetDesc(sess, rest)
case "addobj", "remobj", "hide", "unhide":
g.roomObject(sess, action, rest)
case "addlocalobj":
name, aliases, desc := parseLocalObjArgs(strings.Join(rawArgsFrom(rawInput, "room")[1:], " "))
g.roomAddLocalObj(sess, name, aliases, desc)
case "remlocalobj":
name, _, _ := parseLocalObjArgs(strings.Join(rawArgsFrom(rawInput, "room")[1:], " "))
g.roomRemLocalObj(sess, name)
case "addmob", "remmob":
g.roomMob(sess, action, rest)
case "addspawn", "remspawn":
g.roomSpawn(sess, action, rest)
case "color":
g.roomSetColor(sess, rest)
case "insert":
g.roomInsert(sess, rest)
case "remove":
g.roomRemove(sess, rest)
default:
sess.WriteLine(fmt.Sprintf("Unknown room action: %s", action))
g.showRoomHelp(sess)
}
}
func (g *Game) showRoomHelp(sess *net.Session) {
sess.WriteLine("room actions:")
sess.WriteLine(" room name <text> — set room name")
sess.WriteLine(" room desc <text> — set room description")
sess.WriteLine(" room addobj <object_id> — add a referenced object")
sess.WriteLine(" room remobj <object_id> — remove a referenced object")
sess.WriteLine(" room addlocalobj [name, alias...] <description> — add a local object")
sess.WriteLine(" room remlocalobj [name]|<name> — remove a local object")
sess.WriteLine(" room hide <name_or_id> — hide all matching objects")
sess.WriteLine(" room unhide <name_or_id> — unhide all matching objects")
sess.WriteLine(" room addmob <mob_id> — add a mob spawn")
sess.WriteLine(" room remmob <mob_id> — remove a mob spawn")
sess.WriteLine(" room addspawn <item_id> [qty] [respawn_ticks] — add an item spawn")
sess.WriteLine(" room remspawn <item_id> — remove an item spawn")
sess.WriteLine(" room color <spec|off> — set or clear room map color")
sess.WriteLine(" room insert <direction> [name] — insert a new room into the exit, pushing rooms beyond")
sess.WriteLine(" room remove <direction> — delete the inserted room and pull the far end back")
}
func (g *Game) roomSetName(sess *net.Session, args []string) {
if len(args) == 0 {
sess.WriteLine("Usage: room name <text>")
return
}
text := strings.Join(args, " ")
g.mutateRoom(sess, func(room *world.Room) {
room.Name = text
})
sess.WriteLine(fmt.Sprintf("Room name set to: %s", text))
}
func (g *Game) roomSetDesc(sess *net.Session, args []string) {
if len(args) == 0 {
sess.WriteLine("Usage: room desc <text>")
return
}
room, _, err := g.loadRoomWrite(sess.Player)
if err != nil {
sess.WriteLine(fmt.Sprintf("Error loading room: %v", err))
return
}
// Replacing the list wholesale would destroy alternates; require a direct
// YAML edit when the room already has conditional or multiple variants.
if len(room.Description) > 1 {
sess.WriteLine("This room has multiple or conditional descriptions; edit the room YAML directly to keep them.")
return
}
for _, v := range room.Description {
if v.Condition != nil {
sess.WriteLine("This room has conditional descriptions; edit the room YAML directly to keep them.")
return
}
}
text := strings.Join(args, " ")
if !g.mutateRoom(sess, func(room *world.Room) {
room.Description = behavior.DescList{{Text: text}}
}) {
return
}
sess.WriteLine("Room description updated.")
}
func (g *Game) roomSetColor(sess *net.Session, args []string) {
if len(args) == 0 {
sess.WriteLine("Usage: room color <spec|off>")
sess.WriteLine("Format: <00-FF> [bg:<00-FF>] [bold] [dim] [underline]")
sess.WriteLine("Example: D0 bold")
return
}
value := strings.Join(args, " ")
if value == "off" {
value = ""
}
if value != "" {
spec := color.Parse(value)
if spec.Empty() {
sess.WriteLine(fmt.Sprintf("Invalid color string: %s", value))
sess.WriteLine("Format: <00-FF> [bg:<00-FF>] [bold] [dim] [underline]")
sess.WriteLine("Example: D0 bold")
return
}
}
g.mutateRoom(sess, func(room *world.Room) {
room.Color = value
})
if value == "" {
sess.WriteLine("Room color cleared.")
} else {
sess.WriteLine(fmt.Sprintf("Room color set to: %s", value))
}
}
func (g *Game) loadRoomWrite(p *player.Player) (*world.Room, string, error) {
path, ok := g.World.GetRoomPath(p.RoomID)
if !ok {
return nil, "", fmt.Errorf("room file not found")
}
room, err := g.World.LoadRoom(p.RoomID)
if err != nil {
return nil, "", err
}
return room, path, nil
}
func parseLocalObjArgs(rest string) (name string, aliases []string, desc string) {
rest = strings.TrimSpace(rest)
if rest == "" {
return "", nil, ""
}
if rest[0] == '[' {
closeB := strings.Index(rest, "]")
if closeB < 0 {
return rest, nil, ""
}
desc = strings.TrimSpace(rest[closeB+1:])
for _, p := range strings.Split(rest[1:closeB], ",") {
p = strings.TrimSpace(p)
if name == "" {
name = p
} else if p != "" {
aliases = append(aliases, p)
}
}
return name, aliases, desc
}
fields := strings.Fields(rest)
name = fields[0]
if len(fields) > 1 {
desc = strings.Join(fields[1:], " ")
}
return name, nil, desc
}
// mutateRoom loads the player's current room, applies fn, writes it back, and
// reconciles the live runtime state from the new file. Every successful
// in-game room edit must go through it: a plain file write left the live room
// unchanged (a removed mob kept standing, an added spawn never appeared) until
// the next room entry or restart.
//
// It runs on the owner goroutine and returns false after reporting an error.
func (g *Game) mutateRoom(sess *net.Session, fn func(*world.Room)) bool {
p := sess.Player
room, path, err := g.loadRoomWrite(p)
if err != nil {
sess.WriteLine(fmt.Sprintf("Error loading room: %v", err))
return false
}
fn(room)
data, err := yaml.Marshal(room)
if err != nil {
sess.WriteLine(fmt.Sprintf("Error marshaling room YAML: %v", err))
return false
}
if err := writeRoomFile(path, data); err != nil {
sess.WriteLine(fmt.Sprintf("Error writing room file: %v", err))
return false
}
g.ReconcileRoom(p.RoomID)
return true
}
|