aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_look.go
blob: e6ed5d3eb5d4cfcf00ceeaf64dd6d8e6caf9bde7 (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
215
216
217
218
219
220
package game

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

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

func (g *Game) doLook(sess *net.Session) {
	p := sess.Player
	room, err := g.World.LoadRoom(p.RoomID)
	if err != nil {
		sess.WriteLine("You are in a void.")
		return
	}

	g.showRoomName(sess, p, room)

	wrapWidth := p.OptionInt("wrap_width")
	if wrapWidth < 80 {
		wrapWidth = 80
	}
	if wrapWidth > maxMapDimension {
		// Do not trust the persisted option: writeLookSideBySide pads every
		// content line to the wrap width, so an unbounded value is a
		// one-command OOM (the same clamp map_width already has).
		wrapWidth = maxMapDimension
	}

	var mapLines []string
	mapOn := p.OptionString("tiny_map") != "off"
	if mapOn {
		mapLines = buildTinyMap(g, sess, p.RoomID, mapGlyphsForPlayer(p.OptionBool("unicode")))
		if len(mapLines) != 7 {
			mapLines = nil
			mapOn = false
		}
	}

	descWidth := wrapWidth
	if mapOn {
		descWidth = wrapWidth - 9
	}

	var content []string
	content = append(content, g.showRoomDescription(sess, room, descWidth)...)
	content = append(content, g.showRoomObjects(sess, p, room)...)
	content = append(content, g.showRoomMobs(sess, p, room)...)
	content = append(content, g.showGroundItems(sess, p, room)...)

	if mapOn {
		g.writeLookSideBySide(sess, p, content, mapLines, descWidth)
	} else {
		for _, line := range content {
			sess.WriteLine(line)
		}
	}

	// When the minimap is taller than the room content it already renders
	// empty content rows below the last line, so the exit section must not
	// add another gap on top of them: that is what read as two blank lines.
	addExitGap := !(mapOn && len(mapLines) > len(content))
	g.showRoomExits(sess, p, room, addExitGap)
	g.showRoomPlayers(sess, p, room)
	// Seated at a table: look keeps its room-first order, then appends the
	// live game state and the current hint menu.
	g.showCasinoGameLook(sess, p, room)
	if sess.CryptoActive && room.CryptoExchange {
		sess.CryptoShowHints = true
		if sess.CryptoBusy {
			sess.WriteLine("Updating crypto quotes...")
			g.cryptoHints(sess)
		} else {
			g.executeCrypto(sess, "refresh", nil)
		}
	}
	if sess.PredictionActive && room.PredictionExchange {
		sess.WriteLine("")
		if sess.PredictionBusy {
			sess.WriteLine("Updating markets...")
			sess.PredictionShowHints = true
			g.predictionHints(sess)
		} else {
			g.executePredictions(sess, []string{"refresh"}, "look")
		}
	}
}

func (g *Game) doExits(sess *net.Session) {
	p := sess.Player
	room, err := g.World.LoadRoom(p.RoomID)
	if err != nil || len(room.Exits) == 0 {
		sess.WriteLine("There are no exits here.")
		return
	}
	lines := g.formatExitLines(sess, room)
	if len(lines) == 0 {
		sess.WriteLine("There are no exits here.")
		return
	}
	for _, l := range lines {
		sess.WriteLine(l)
	}
}

func joinInts(nums []int) string {
	var parts []string
	for _, n := range nums {
		parts = append(parts, strconv.Itoa(n))
	}
	return strings.Join(parts, ",")
}

func mobInstanceIdx(mob *world.MobInstance, roomMobs []*world.MobInstance) int {
	var same []*world.MobInstance
	for _, m := range roomMobs {
		if m.DefID == mob.DefID {
			same = append(same, m)
		}
	}
	if len(same) <= 1 {
		return 0
	}
	sort.Slice(same, func(i, j int) bool {
		return same[i].InstanceID < same[j].InstanceID
	})
	for i, m := range same {
		if m.InstanceID == mob.InstanceID {
			return i + 1
		}
	}
	return 0
}

func visibleLen(s string) int {
	return color.VisibleLen(s)
}

func wrapText(text string, width int) []string {
	if width <= 0 {
		return []string{text}
	}
	words := strings.Fields(text)
	if len(words) == 0 {
		return nil
	}
	var lines []string
	current := words[0]
	for _, word := range words[1:] {
		if visibleLen(current)+1+visibleLen(word) <= width {
			current += " " + word
		} else {
			lines = append(lines, current)
			current = word
		}
	}
	lines = append(lines, current)
	return lines
}

func (g *Game) writeLookSideBySide(sess *net.Session, p *player.Player, contentLines []string, mapLines []string, descWidth int) {
	mapWidth := descWidth
	if mapWidth <= 0 {
		mapWidth = 70
	}
	// Wrap any content line that would overrun the map column. Without this a
	// long table status writes through the minimap; with it the status wraps
	// and the map keeps its own column.
	content := make([]string, 0, len(contentLines))
	for _, line := range contentLines {
		if visibleLen(line) <= mapWidth {
			content = append(content, line)
			continue
		}
		content = append(content, strings.Split(color.WrapANSI(line, mapWidth), "\r\n")...)
	}
	total := len(content)
	if len(mapLines) > total {
		total = len(mapLines)
	}
	leftMap := p.OptionString("tiny_map") == "left"
	for i := 0; i < total; i++ {
		piece := ""
		if i < len(content) {
			piece = content[i]
		}
		if i < len(mapLines) {
			mapLine := mapLines[i]
			if leftMap {
				sess.WriteLine(fmt.Sprintf("%s  %s", mapLine, piece))
			} else {
				pad := mapWidth - visibleLen(piece)
				if pad < 0 {
					pad = 0
				}
				sess.WriteLine(piece + strings.Repeat(" ", pad) + "  " + mapLine)
			}
		} else {
			sess.WriteLine(piece)
		}
	}
}

func (g *Game) executeLook(sess *net.Session, args []string, rawInput string) {
	if len(args) == 0 {
		g.doLook(sess)
	} else {
		g.doLookTarget(sess, strings.Join(args, " "))
	}
}

func (g *Game) executeExits(sess *net.Session, args []string, rawInput string) {
	g.doExits(sess)
}