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

import (
	"thehouseoficarus/internal/color"
	"thehouseoficarus/internal/config"
	"thehouseoficarus/internal/item"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/object"
)

func (g *Game) colorMode(sess *net.Session) string {
	if p := sess.Player; p != nil {
		return p.OptionString("color")
	}
	return "none"
}

func (g *Game) resolveColor(sess *net.Session, category string) color.ColorSpec {
	if sess != nil && sess.Account != nil && sess.Account.Colors != nil {
		if val, ok := sess.Account.Colors[category]; ok {
			if val == "off" {
				return color.NoColor()
			}
			if val != "" {
				return color.Parse(val)
			}
		}
	}
	if g.ColorConfig != nil {
		if val, ok := (*g.ColorConfig)[category]; ok && val != "" {
			return color.Parse(val)
		}
	}
	if val, ok := config.DefaultColors()[category]; ok && val != "" {
		return color.Parse(val)
	}
	return color.NoColor()
}

func (g *Game) colorize(sess *net.Session, category, text string) string {
	spec := g.resolveColor(sess, category)
	return color.Render(g.colorMode(sess), spec, text)
}

// resolveConstant returns the live spec for a constant (player-invisible)
// color category. Unlike resolveColor, it never consults the account's
// per-player override map — constants are admin-tunable only — so players
// have no way to change them. Falls back through the file's constant_colors
// map, then to the built-in DefaultConstantColors.
func (g *Game) resolveConstant(sess *net.Session, category string) color.ColorSpec {
	if g.ConstantColorConfig != nil {
		if val, ok := (*g.ConstantColorConfig)[category]; ok && val != "" {
			return color.Parse(val)
		}
	}
	if val, ok := config.DefaultConstantColors()[category]; ok && val != "" {
		return color.Parse(val)
	}
	return color.NoColor()
}

// colorizeConstant paints text with a constant color spec, bypassing the
// player override tier entirely.
func (g *Game) colorizeConstant(sess *net.Session, category, text string) string {
	spec := g.resolveConstant(sess, category)
	return color.Render(g.colorMode(sess), spec, text)
}

// levelColorSpec returns the color spec for the level-difference band shown
// next to mob/player levels (green/red/white by how far the target is from
// the viewer). Uses admin-tunable constant_colors; falls back to built-ins.
func (g *Game) levelColorSpec(sess *net.Session, myLevel, theirLevel int) color.ColorSpec {
	diff := theirLevel - myLevel
	var name string
	switch {
	case diff == 0:
		name = "level_diff_equal"
	case diff > 0 && diff < 5:
		name = "level_diff_up_small"
	case diff >= 5:
		name = "level_diff_up_big"
	case diff < 0 && diff > -5:
		name = "level_diff_down_small"
	default:
		name = "level_diff_down_big"
	}
	return g.resolveConstant(sess, name)
}

func (g *Game) levelColorize(sess *net.Session, myLevel, theirLevel int, text string) string {
	spec := g.levelColorSpec(sess, myLevel, theirLevel)
	return color.Render(g.colorMode(sess), spec, text)
}

func (g *Game) objColorize(sess *net.Session, objDef *object.ObjectDef, text string) string {
	if objDef != nil && objDef.Color != "" {
		spec := color.Parse(objDef.Color)
		return color.Render(g.colorMode(sess), spec, text)
	}
	return text
}

func (g *Game) itemColorize(sess *net.Session, itemDef *item.ItemDef, text string) string {
	if itemDef != nil && itemDef.Color != "" {
		spec := color.Parse(itemDef.Color)
		return color.Render(g.colorMode(sess), spec, text)
	}
	return g.colorize(sess, "item", text)
}

// namedColorSpecs maps every color category name to its resolved ColorSpec
// for the session, letting inline tags like {command} bind to a color
// category. All player-overridable categories, server defaults, and constant
// colors are included so any color can be tagged in data content.
func (g *Game) namedColorSpecs(sess *net.Session) map[string]color.ColorSpec {
	names := make(map[string]color.ColorSpec)
	for name := range config.DefaultColors() {
		if spec := g.resolveColor(sess, name); !spec.Empty() {
			names[name] = spec
		}
	}
	if g.ColorConfig != nil {
		for name := range *g.ColorConfig {
			if _, ok := names[name]; ok {
				continue
			}
			if spec := g.resolveColor(sess, name); !spec.Empty() {
				names[name] = spec
			}
		}
	}
	for name := range config.DefaultConstantColors() {
		if spec := g.resolveConstant(sess, name); !spec.Empty() {
			names[name] = spec
		}
	}
	if g.ConstantColorConfig != nil {
		for name := range *g.ConstantColorConfig {
			if _, ok := names[name]; ok {
				continue
			}
			if spec := g.resolveConstant(sess, name); !spec.Empty() {
				names[name] = spec
			}
		}
	}
	return names
}

// expandTags expands inline {spec}text{/} tags, including named color
// categories such as {command}, using the session's resolved colors.
func (g *Game) expandTags(sess *net.Session, text string) string {
	return color.ExpandTagsNamed(g.colorMode(sess), g.namedColorSpecs(sess), text)
}

// expandTagsDefault is expandTags with a default spec applied to untagged text.
func (g *Game) expandTagsDefault(sess *net.Session, def color.ColorSpec, text string) string {
	return color.ExpandTagsDefaultNamed(g.colorMode(sess), def, g.namedColorSpecs(sess), text)
}

// expandDialogTags expands dialog text (quoted speech) with named color tags.
func (g *Game) expandDialogTags(sess *net.Session, dialogSpec color.ColorSpec, text string) string {
	return color.ExpandDialogTagsNamed(g.colorMode(sess), dialogSpec, g.namedColorSpecs(sess), text)
}