aboutsummaryrefslogtreecommitdiff
path: root/internal/game/look_entities.go
blob: c1befe39303030dc9871cb86b428d05b5cd4e152 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package game

import (
	"fmt"
	"sort"
	"strings"

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

func (g *Game) showRoomMobs(sess *net.Session, p *player.Player, room *world.Room) []string {
	mobs := g.MobStore.MobsInRoom(p.RoomID)
	if len(mobs) == 0 {
		return nil
	}
	sort.Slice(mobs, func(i, j int) bool {
		iDamaged := mobs[i].HP < mobs[i].MaxHP
		jDamaged := mobs[j].HP < mobs[j].MaxHP
		if iDamaged != jDamaged {
			return iDamaged
		}
		return mobs[i].InstanceID < mobs[j].InstanceID
	})
	var lines []string
	lines = append(lines, "")
	playerLevel := p.CombatLevel()
	for _, m := range mobs {
		hp := ""
		if m.HP < m.MaxHP {
			if m.IsTask() {
				pct := 0
				if m.MaxHP > 0 {
					pct = (m.MaxHP - m.HP) * 100 / m.MaxHP
				}
				hp = fmt.Sprintf(" [%d%% complete]", pct)
			} else {
				hp = fmt.Sprintf(" [%s/%dhp]", color.Render(g.colorMode(sess), color.Parse("A7"), fmt.Sprint(m.HP)), m.MaxHP)
			}
		}
		var desc string
		if g.Combat.IsMobInCombat(m.InstanceID) {
			if len(m.CombatDescriptions) > 0 {
				target := g.Combat.MobTarget(m.InstanceID)
				pattern := m.CombatDescriptions[randInt(len(m.CombatDescriptions))]
				desc = " " + fmt.Sprintf(pattern, target)
			}
		} else if m.IdleDescription != "" {
			desc = fmt.Sprintf(" %s", m.IdleDescription)
		}
		displayName := m.Name
		if !m.Unique {
			displayName = "A " + m.Name
		}
		mobColor := "mob"
		if m.Protected {
			mobColor = "protected_mob"
		}
		var levelStr string
		if m.Protected {
			levelStr = ""
		} else {
			mobLevel := mobCombatLevel(m)
			levelStr = " " + g.levelColorize(sess, playerLevel, mobLevel, fmt.Sprintf("(level %d)", mobLevel))
		}
		lines = append(lines, fmt.Sprintf("%s%s%s%s", g.colorize(sess, mobColor, displayName), levelStr, hp, desc))
	}
	return lines
}

func (g *Game) showRoomObjects(sess *net.Session, p *player.Player, room *world.Room) []string {
	objs := g.World.AllObjInstances(p.RoomID)
	if len(objs) == 0 {
		return g.showFarmPatches(sess, p)
	}
	var lines []string
	lines = append(lines, "")
	grouped := make(map[string]int)
	var order []string
	for _, o := range objs {
		if _, ok := grouped[o.DefID]; !ok {
			order = append(order, o.DefID)
		}
		grouped[o.DefID]++
	}
	for _, objID := range order {
		count := grouped[objID]
		def, isLocal, err := g.resolveObjectDef(p.RoomID, objID)
		if err != nil {
			continue
		}

		if def.Hidden && !p.GodMode {
			continue
		}

		if farmPatchDefIDs[objID] {
			continue
		}

		var godPrefix string
		if p.GodMode {
			var tags []string
			if isLocal {
				tags = append(tags, color.Render(g.colorMode(sess), color.ColorSpec{Fg: -1, Bold: true}, "[LOCAL]"))
			} else {
				tags = append(tags, color.Render(g.colorMode(sess), color.ColorSpec{Fg: -1, Bold: true}, "[GLOBAL]"))
			}
			if def.Hidden {
				tags = append(tags, color.Render(g.colorMode(sess), color.ColorSpec{Fg: -1, Bold: true}, "(HIDDEN)"))
			}
			godPrefix = strings.Join(tags, " ") + " "
		}

		type instInfo struct {
			idx       int
			depleted  bool
			sharedMax int
			sharedCur int
			respawnIn int
			quality   int
		}
		var instances []instInfo
		for i := 0; i < count; i++ {
			st := g.World.GetObjState(p.RoomID, objID, i)
			if st == nil {
				continue
			}
			instances = append(instances, instInfo{
				idx:       i + 1,
				depleted:  st.Depleted,
				sharedMax: int(st.SharedMax),
				sharedCur: st.SharedTimer,
				respawnIn: int(st.DepleteTimer),
				quality:   int(st.Quality),
			})
		}
		multi := len(instances) > 1
		showTimers := p.OptionBool("depletion")

		var freshIdxs []int
		var timed, depleted []instInfo
		for _, ins := range instances {
			if ins.depleted {
				depleted = append(depleted, ins)
			} else if ins.sharedMax > 0 && ins.sharedCur < ins.sharedMax {
				timed = append(timed, ins)
			} else {
				freshIdxs = append(freshIdxs, ins.idx)
			}
		}

		roomDesc := def.InRoomDescription
		if roomDesc != "" {
			roomDesc = color.ExpandTags(g.colorMode(sess), roomDesc)
		}
		coloredName := g.objColorize(sess, def, def.Name)
		coloredPlural := g.objColorize(sess, def, def.Name+"s")

		if len(freshIdxs) > 0 {
			var line string
			if roomDesc != "" {
				line = roomDesc
			} else if len(freshIdxs) == 1 {
				line = fmt.Sprintf("A %s is here.", coloredName)
			} else {
				line = fmt.Sprintf("%d %s are here.", len(freshIdxs), coloredPlural)
			}
			var suffix string
			if multi && (len(timed) > 0 || len(depleted) > 0) {
				suffix = fmt.Sprintf(" [%s]", joinInts(freshIdxs))
			}
			qualityTimer := ""
			if showTimers && len(instances) > 0 && instances[0].quality > 0 {
				qualityTimer = fmt.Sprintf(" (Burning for %d more ticks)", instances[0].quality)
			}
			lines = append(lines, fmt.Sprintf("%s%s%s%s", godPrefix, line, suffix, qualityTimer))
		}

		for _, ins := range timed {
			var line string
			if roomDesc != "" {
				line = roomDesc
			} else {
				line = fmt.Sprintf("A %s is here.", coloredName)
			}
			tag := ""
			if multi {
				tag = fmt.Sprintf(" [%d]", ins.idx)
			}
			timer := ""
			if showTimers {
				timer = fmt.Sprintf(" (despawn: %d/%d)", ins.sharedCur, ins.sharedMax)
			}
			lines = append(lines, fmt.Sprintf("%s%s%s%s", godPrefix, line, tag, timer))
		}

		for _, ins := range depleted {
			var line string
			if roomDesc != "" {
				line = roomDesc
			} else {
				line = fmt.Sprintf("A %s is here.", coloredName)
			}
			tag := ""
			if multi {
				tag = fmt.Sprintf(" [%d]", ins.idx)
			}
			timer := ""
			if showTimers {
				timer = fmt.Sprintf(" (respawns in %d ticks)", ins.respawnIn)
			}
			lines = append(lines, fmt.Sprintf("%s%s (depleted)%s%s", godPrefix, line, tag, timer))
		}
	}

	lines = append(lines, g.showFarmPatches(sess, p)...)
	return lines
}

func (g *Game) showGroundItems(sess *net.Session, p *player.Player, room *world.Room) []string {
	ground := g.World.GroundItemsDetailed(p.RoomID)
	if len(ground) == 0 {
		return nil
	}
	showDespawn := p.OptionBool("despawn")
	showReserve := p.OptionBool("reserve")

	type displayLine struct {
		name       string
		quantity   int
		colorName  string
		annotation string
	}

	type groupKey struct {
		itemID       string
		despawnTimer int
	}

	var lines []displayLine
	groups := make(map[groupKey]*displayLine)
	var groupOrder []groupKey

	for _, info := range ground {
		def, err := g.ItemStore.Load(info.ItemID)
		name := info.ItemID
		if err == nil {
			name = def.Name
		}
		coloredName := g.itemColorize(sess, def, name)

		if info.ReservedFor != "" {
			var parts []string
			if showReserve {
				parts = append(parts, fmt.Sprintf("reserved for %s %dt", info.ReservedFor, info.ReserveTimer))
			} else {
				parts = append(parts, "reserved")
			}
			if showDespawn && !info.IsSpawn {
				parts = append(parts, fmt.Sprintf("despawns %dt", info.DespawnTimer))
			}
			annotation := ""
			if len(parts) > 0 {
				annotation = fmt.Sprintf(" (%s)", strings.Join(parts, ", "))
			}
			lines = append(lines, displayLine{
				name:       name,
				quantity:   info.Quantity,
				colorName:  coloredName,
				annotation: annotation,
			})
			continue
		}

		timerKey := -1
		if showDespawn && !info.IsSpawn {
			timerKey = info.DespawnTimer
		}
		key := groupKey{itemID: info.ItemID, despawnTimer: timerKey}

		if existing, ok := groups[key]; ok {
			existing.quantity += info.Quantity
		} else {
			annotation := ""
			if showDespawn && !info.IsSpawn {
				annotation = fmt.Sprintf(" (despawns %dt)", info.DespawnTimer)
			}
			dl := &displayLine{
				name:       name,
				quantity:   info.Quantity,
				colorName:  coloredName,
				annotation: annotation,
			}
			groups[key] = dl
			groupOrder = append(groupOrder, key)
		}
	}

	for _, key := range groupOrder {
		lines = append(lines, *groups[key])
	}

	sort.Slice(lines, func(i, j int) bool {
		return strings.ToLower(lines[i].name) < strings.ToLower(lines[j].name)
	})

	var out []string
	out = append(out, "")
	out = append(out, "On the ground:")

	type fmtLine struct {
		prefix     string
		annotation string
	}
	var fmtLines []fmtLine
	maxPrefix := 0

	for _, dl := range lines {
		prefix := ""
		if dl.quantity > 1 {
			prefix = fmt.Sprintf("  %d x %s", dl.quantity, dl.colorName)
		} else {
			prefix = fmt.Sprintf("  %s", dl.colorName)
		}
		if visibleLen(prefix) > maxPrefix {
			maxPrefix = visibleLen(prefix)
		}
		fmtLines = append(fmtLines, fmtLine{prefix, dl.annotation})
	}

	for _, l := range fmtLines {
		if l.annotation != "" {
			pad := maxPrefix + 1 + (len(l.prefix) - visibleLen(l.prefix))
			out = append(out, fmt.Sprintf("%-*s%s", pad, l.prefix, l.annotation))
		} else {
			out = append(out, l.prefix)
		}
	}
	return out
}

func (g *Game) showRoomPlayers(sess *net.Session, p *player.Player, room *world.Room) {
	others := g.Hub.PlayersInRoom(p.RoomID)
	for _, other := range others {
		if other != sess && other.Player != nil {
			op := other.Player
			line := fmt.Sprintf("%s is here", g.colorize(sess, "player_name", op.Name))
			if cs := g.Combat.Get(op.Name); cs != nil {
				if mob := g.MobStore.GetInstance(cs.MobID); mob != nil && mob.HP > 0 {
					name := mobDisplayName(mob, false)
					if idx := mobInstanceIdx(mob, g.MobStore.MobsInRoom(p.RoomID)); idx > 0 {
						name += fmt.Sprintf(" [%d]", idx)
					}
					line += fmt.Sprintf(" (fighting %s)", name)
				}
			} else if desc := g.playerActionDisplay(op); desc != "" {
				line += ", " + desc
			}
			sess.WriteLine(line + ".")
		}
	}
}