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
244
245
246
247
248
249
250
251
252
253
254
255
|
package game
import (
"fmt"
"strconv"
"strings"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/player"
"thehouseoficarus/internal/world"
)
func (g *Game) executeWalk(sess *net.Session, args []string, rawInput string) {
g.doWalk(sess, args)
}
func (g *Game) doWalk(sess *net.Session, args []string) {
p := sess.Player
if len(args) == 0 {
sess.WriteLine("Walk where?")
return
}
input := strings.Join(args, "")
if roomID, err := strconv.Atoi(input); err == nil {
path := g.findPathToRoom(sess, p.RoomID, roomID)
if path == nil {
sess.WriteLine(fmt.Sprintf("No path found to room #%d.", roomID))
return
}
g.startWalk(sess, p, path)
return
}
dirs, err := parseWalkDirections(input)
if err != nil {
sess.WriteLine(err.Error())
return
}
g.startWalk(sess, p, dirs)
}
func parseWalkDirections(input string) ([]string, error) {
var dirs []string
total := 0
// maxWalkSteps bounds the slice before it is allocated. Agility caps a
// real walk well below this; the limit exists only so a hostile count
// like "walk 999999999n" cannot allocate gigabytes on the owner goroutine.
const maxWalkSteps = 1000
i := 0
for i < len(input) {
num := 0
for i < len(input) && input[i] >= '0' && input[i] <= '9' {
num = num*10 + int(input[i]-'0')
if num > maxWalkSteps {
return nil, fmt.Errorf("too many steps at once (limit %d)", maxWalkSteps)
}
i++
}
if num == 0 {
num = 1
}
total += num
if total > maxWalkSteps {
return nil, fmt.Errorf("too many steps at once (limit %d)", maxWalkSteps)
}
if i >= len(input) {
return nil, fmt.Errorf("unexpected end of directions")
}
ch := input[i]
i++
var dir string
switch ch {
case 'n':
if i < len(input) && input[i] == 'e' {
i++
dir = string(world.Northeast)
} else if i < len(input) && input[i] == 'w' {
i++
dir = string(world.Northwest)
} else {
dir = string(world.North)
}
case 's':
if i < len(input) && input[i] == 'e' {
i++
dir = string(world.Southeast)
} else if i < len(input) && input[i] == 'w' {
i++
dir = string(world.Southwest)
} else {
dir = string(world.South)
}
case 'e':
dir = string(world.East)
case 'w':
dir = string(world.West)
case 'u':
dir = string(world.Up)
case 'd':
dir = string(world.Down)
default:
return nil, fmt.Errorf("unknown direction: %c", ch)
}
for j := 0; j < num; j++ {
dirs = append(dirs, dir)
}
}
return dirs, nil
}
func (g *Game) startWalk(sess *net.Session, p *player.Player, dirs []string) {
agilityLevel := p.Level(player.Agility)
if agilityLevel < 1 {
agilityLevel = 1
}
if len(dirs) > agilityLevel {
sess.WriteLine(fmt.Sprintf("You can only make %d moves at once with your current agility level!", agilityLevel))
return
}
g.cancelAction(p)
p.WalkSequence = dirs
}
func (g *Game) advanceWalk(sess *net.Session, p *player.Player) {
if p.MoveTicks > 0 {
return
}
if p.EscapeDir != "" {
return
}
if len(p.WalkSequence) == 0 {
return
}
dir := p.WalkSequence[0]
remaining := condensePath(p.WalkSequence[1:])
p.WalkSequence = p.WalkSequence[1:]
oldRoom := p.RoomID
g.doMove(sess, dir, true)
if p.MoveTicks == 0 && p.RoomID == oldRoom {
p.WalkSequence = nil
return
}
if len(p.WalkSequence) > 0 && remaining != "" {
sess.WriteLine(fmt.Sprintf("You're headed: %s.", remaining))
}
}
func condensePath(dirs []string) string {
if len(dirs) == 0 {
return ""
}
var result strings.Builder
i := 0
for i < len(dirs) {
count := 1
for i+count < len(dirs) && dirs[i+count] == dirs[i] {
count++
}
if count > 1 {
result.WriteString(fmt.Sprintf("%d", count))
}
result.WriteString(directionToChar(dirs[i]))
i += count
}
return result.String()
}
func directionToChar(dir string) string {
switch world.ExitDir(dir) {
case world.North:
return "n"
case world.South:
return "s"
case world.East:
return "e"
case world.West:
return "w"
case world.Northeast:
return "ne"
case world.Northwest:
return "nw"
case world.Southeast:
return "se"
case world.Southwest:
return "sw"
case world.Up:
return "u"
case world.Down:
return "d"
}
return "?"
}
var walkSearchDirs = []world.ExitDir{
world.North, world.South, world.East, world.West,
world.Northeast, world.Northwest, world.Southeast, world.Southwest,
world.Up, world.Down,
}
func (g *Game) findPathToRoom(sess *net.Session, fromRoom, toRoom int) []string {
if fromRoom == toRoom {
return nil
}
type bfsNode struct {
roomID int
path []string
}
visited := make(map[int]bool)
queue := []bfsNode{{fromRoom, nil}}
visited[fromRoom] = true
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
room, ok := loadRoom(g, cur.roomID)
if !ok {
continue
}
for _, dir := range walkSearchDirs {
exitDef, exists := room.Exits[dir]
if !exists {
continue
}
if ed := g.exitDisplayState(sess, cur.roomID, dir, exitDef); !ed.Visible || ed.Blocked {
continue
}
if visited[exitDef.Room] {
continue
}
newPath := make([]string, len(cur.path)+1)
copy(newPath, cur.path)
newPath[len(cur.path)] = string(dir)
if exitDef.Room == toRoom {
return newPath
}
visited[exitDef.Room] = true
queue = append(queue, bfsNode{exitDef.Room, newPath})
}
}
return nil
}
|