aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_walk.go
blob: 9485af51e59de9525e8521ac215fb6b0a7ebeddd (plain)
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
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(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
	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')
			i++
		}
		if num == 0 {
			num = 1
		}
		if i >= len(input) {
			return nil, fmt.Errorf("Unexpected end of directions.")
		}
		ch := input[i]
		i++
		var dir string
		switch ch {
		case 'n':
			dir = string(world.North)
		case 's':
			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 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, 2.0)

	if p.MoveTicks == 0 && p.RoomID == oldRoom {
		p.WalkSequence = nil
		p.ActionState = nil
		return
	}

	if len(p.WalkSequence) > 0 && remaining != "" {
		p.ActionState = &ActionState{Type: ActionWalking}
		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.Up:
		return "u"
	case world.Down:
		return "d"
	}
	return "?"
}

var bfsSearchDirs = []world.ExitDir{world.North, world.South, world.East, world.West, world.Up, world.Down}

func (g *Game) findPathToRoom(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 bfsSearchDirs {
			exitDef, exists := room.Exits[dir]
			if !exists {
				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
}