aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_aps.go
blob: a5cff782e8bf7b3e31e0c66eee811eef5060144c (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
212
213
214
package game

import (
	"fmt"
	"sort"
	"strconv"
	"strings"

	"thehouseoficarus/internal/color"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
	"thehouseoficarus/internal/ui"
)

func (g *Game) executeAps(sess *net.Session, args []string, rawInput string) {
	p := sess.Player

	known := getKnownApsNodes(p)

	if len(args) == 0 {
		if len(known) == 0 {
			sess.WriteLine("You have no APS coordinates stored in your datapad. Use an APS Tower to add some.")
			return
		}
		displayApsNodes(g, sess, p, known)
		return
	}

	if len(known) == 0 {
		sess.WriteLine("You have no APS coordinates stored in your datapad. Use an APS Tower to add some.")
		return
	}

	targetID := resolveApsTarget(g, sess, p, known, args)
	if targetID == 0 {
		return
	}

	if targetID == p.RoomID {
		sess.WriteLine("You are already at that location.")
		return
	}

	path := g.findPathToRoom(p.RoomID, targetID)
	if path == nil {
		room, err := g.World.LoadRoom(targetID)
		roomName := fmt.Sprintf("#%d", targetID)
		if err == nil {
			roomName = room.Name
		}
		sess.WriteLine(fmt.Sprintf("No path found to %s (#%d).", roomName, targetID))
		return
	}

	agilityLevel := p.Level(player.Agility)
	if agilityLevel < 1 {
		agilityLevel = 1
	}
	truncated := path
	if len(path) > agilityLevel {
		truncated = path[:agilityLevel]
	}

	room, _ := g.World.LoadRoom(targetID)
	roomName := fmt.Sprintf("#%d", targetID)
	if room != nil {
		roomName = room.Name
	}

	g.cancelAction(p)
	p.WalkSequence = truncated
	sess.WriteLine(fmt.Sprintf("You set off towards %s (#%d) — %d move%s queued.",
		roomName, targetID, len(truncated), plural(len(truncated))))
}

func getKnownApsNodes(p *player.Player) []int {
	if p.Flags == nil {
		return nil
	}
	var nodes []int
	for k, v := range p.Flags {
		if !strings.HasPrefix(k, "aps_node_") {
			continue
		}
		if b, ok := v.(bool); !ok || !b {
			continue
		}
		idStr := strings.TrimPrefix(k, "aps_node_")
		id, err := strconv.Atoi(idStr)
		if err != nil {
			continue
		}
		nodes = append(nodes, id)
	}
	sort.Ints(nodes)
	return nodes
}

type apsNodeInfo struct {
	ID       int
	Name     string
	Distance int
}

func displayApsNodes(g *Game, sess *net.Session, p *player.Player, known []int) {
	var infos []apsNodeInfo
	for _, id := range known {
		room, err := g.World.LoadRoom(id)
		name := fmt.Sprintf("#%d", id)
		if err == nil {
			name = room.Name
		}
		dist := 0
		if id != p.RoomID {
			path := g.findPathToRoom(p.RoomID, id)
			if path != nil {
				dist = len(path)
			}
		}
		infos = append(infos, apsNodeInfo{ID: id, Name: name, Distance: dist})
	}

	sort.Slice(infos, func(i, j int) bool {
		return infos[i].Distance < infos[j].Distance
	})

	mode := g.colorMode(sess)
	rows := make([][]string, len(infos))
	for i, info := range infos {
		distStr := strconv.Itoa(info.Distance)
		if info.Distance == 0 {
			distStr = color.Render(mode, color.Parse("52"), "here")
		}
		rows[i] = []string{
			color.Render(mode, color.Parse("F5"), fmt.Sprintf("#%d", info.ID)),
			color.Render(mode, color.Parse("4B"), info.Name),
			distStr,
		}
	}

	t := &ui.Table{
		Title: "APS Datapad",
		Columns: []string{
			color.Render(mode, color.Parse("F5"), "Room"),
			color.Render(mode, color.Parse("4B"), "Name"),
			color.Render(mode, color.Parse("E6"), "Distance"),
		},
		Rows: rows,
	}

	for _, line := range t.Render(p.OptionBool("unicode"), p.OptionInt("wrap_width")) {
		sess.WriteLine(line)
	}
}

func resolveApsTarget(g *Game, sess *net.Session, p *player.Player, known []int, args []string) int {
	input := strings.ToLower(strings.Join(args, " "))

	if input == "nearest" {
		bestID := 0
		bestDist := -1
		for _, id := range known {
			if id == p.RoomID {
				return id
			}
			path := g.findPathToRoom(p.RoomID, id)
			if path == nil {
				continue
			}
			if bestDist == -1 || len(path) < bestDist {
				bestDist = len(path)
				bestID = id
			}
		}
		if bestID == 0 {
			sess.WriteLine("No reachable APS nodes found.")
			return 0
		}
		return bestID
	}

	if roomID, err := strconv.Atoi(input); err == nil {
		for _, id := range known {
			if id == roomID {
				return roomID
			}
		}
		sess.WriteLine(fmt.Sprintf("Room #%d is not in your APS datapad.", roomID))
		return 0
	}

	var matches []int
	for _, id := range known {
		room, err := g.World.LoadRoom(id)
		if err != nil {
			continue
		}
		nameLower := strings.ToLower(room.Name)
		if strings.HasPrefix(nameLower, input) || strings.HasPrefix(input, nameLower) {
			matches = append(matches, id)
		}
	}

	if len(matches) == 1 {
		return matches[0]
	}
	if len(matches) > 1 {
		sess.WriteLine("Multiple APS nodes match that name. Be more specific or use the room number.")
		return 0
	}

	sess.WriteLine("That location is not in your APS datapad.")
	return 0
}