aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_map.go
blob: e84d1ce42a50a32edb53d8dc9758bb0d25bd7a3d (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
package game

import (
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

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

// maxMapDimension bounds the grid a player may request. buildFullMap
// allocates mapHeight x mapWidth cells, so an unbounded option value would let
// any authenticated player trigger a fatal (unrecoverable) out-of-memory
// allocation that takes the whole server down. The option parser accepts any
// int and the value persists in the account file, so the clamp must live here.
const maxMapDimension = 200

// mapDimensions returns the clamped map grid size requested by the player.
func mapDimensions(p *player.Player) (int, int) {
	mapWidth := p.OptionInt("map_width")
	mapHeight := p.OptionInt("map_height")
	if mapWidth < 5 {
		mapWidth = 5
	}
	if mapHeight < 5 {
		mapHeight = 5
	}
	if mapWidth > maxMapDimension {
		mapWidth = maxMapDimension
	}
	if mapHeight > maxMapDimension {
		mapHeight = maxMapDimension
	}
	return mapWidth, mapHeight
}

func (g *Game) doMap(sess *net.Session) {
	p := sess.Player
	mapWidth, mapHeight := mapDimensions(p)

	lines := buildFullMap(g, sess, p.RoomID, mapWidth, mapHeight, mapGlyphsForPlayer(p.OptionBool("unicode")))

	mode := p.OptionString("map_padding")
	if mode == "none" || mode == "x" {
		lines = stripBlankRows(lines)
	}
	if mode == "none" || mode == "y" {
		lines = leftTrimCommon(lines)
	}

	if len(lines) == 0 {
		sess.WriteLine("No map data to display.")
		return
	}
	sess.WriteLine("")
	for _, line := range lines {
		sess.WriteLine(line)
	}
}