This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtarget.go
484 lines (454 loc) · 10.8 KB
/
target.go
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package main
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/anaseto/gruid"
"github.com/anaseto/gruid/ui"
)
var invalidPos = gruid.Point{-1, -1}
type examination struct {
p gruid.Point
nmonster int
objects []gruid.Point
nobject int
sortedStairs []gruid.Point
stairIndex int
info posInfo
scroll bool
}
// HideCursor hides the target cursor.
func (md *model) HideCursor() {
md.targ.ex.p = invalidPos
}
// SetCursor sets the target cursor.
func (md *model) SetCursor(p gruid.Point) {
md.targ.ex.p = p
}
// CancelExamine cancels current targeting.
func (md *model) CancelExamine() {
md.g.Highlight = nil
md.g.MonsterTargLOS = nil
md.HideCursor()
md.targ.kbTargeting = false
md.targ.ex.scroll = false
}
// Examine targets a given position with the cursor.
func (md *model) Examine(p gruid.Point) {
if md.targ.ex.p == p {
return
}
md.examine(p)
}
func (md *model) examine(p gruid.Point) {
if !valid(p) {
return
}
md.SetCursor(p)
md.computeHighlight()
m := md.g.MonsterAt(p)
if m.Exists() && md.g.Player.Sees(p) {
md.g.ComputeMonsterCone(m)
} else {
md.g.MonsterTargLOS = nil
}
md.updatePosInfo()
md.targ.ex.scroll = false
}
func (md *model) targetMonsters() []*monster {
tms := []*monster{}
for _, m := range md.g.Monsters {
if !m.Exists() || !md.g.Player.Sees(m.P) {
continue
}
tms = append(tms, m)
}
for p, i := range md.g.LastMonsterKnownAt {
m := md.g.Monsters[i]
if !m.Exists() || md.g.Player.Sees(m.P) {
continue
}
mons := &monster{}
*mons = *m
mons.P = p
tms = append(tms, mons)
}
sort.Slice(tms, func(i, j int) bool {
mi := tms[i]
mj := tms[j]
if md.g.Player.Sees(mi.P) && !md.g.Player.Sees(mj.P) {
return true
}
if !md.g.Player.Sees(mi.P) && md.g.Player.Sees(mj.P) {
return false
}
return distance(md.g.Player.P, mi.P) < distance(md.g.Player.P, mj.P)
})
return tms
}
// KeyboardExamine starts keyboard examination mode, with a sensible default
// target.
func (md *model) KeyboardExamine() {
md.targ.kbTargeting = true
g := md.g
p := g.Player.P
for _, mons := range md.targetMonsters() {
p = mons.P
break
}
md.targ.ex = &examination{
p: p,
objects: []gruid.Point{},
}
if p == g.Player.P {
md.nextObject(invalidPos, md.targ.ex)
if !valid(md.targ.ex.p) {
md.nextStair(md.targ.ex)
}
if valid(md.targ.ex.p) && distance(p, md.targ.ex.p) < DefaultLOSRange+5 {
p = md.targ.ex.p
}
}
md.examine(p)
}
type posInfo struct {
P gruid.Point
Unknown bool
Noise bool
Unreachable bool
Sees bool
Player bool
Monster *monster
Cell cell
Cloud string
Lighted bool
}
func (md *model) drawPosInfo() {
g := md.g
p := gruid.Point{}
if md.targ.ex.p.X < DungeonWidth/2 {
p.X += DungeonWidth/2 + 1
}
info := md.targ.ex.info
y := 2
stt := ui.StyledText{}.WithMarkups(map[rune]gruid.Style{
'h': gruid.Style{}.WithFg(ColorCyan),
's': gruid.Style{}.WithFg(ColorGreen),
})
formatBox := func(title, s string, fg gruid.Color) {
md.description.Content = stt.WithText(s).Format(DungeonWidth/2 - 3)
if md.description.Content.Size().Y+2 > 2+DungeonHeight {
md.description.Box = &ui.Box{Title: ui.NewStyledText(title, gruid.Style{}.WithFg(fg)),
Footer: ui.Text("scroll/page down for more...")}
} else {
md.description.Box = &ui.Box{Title: ui.NewStyledText(title, gruid.Style{}.WithFg(fg))}
}
y += md.description.Draw(md.gd.Slice(gruid.NewRange(0, y, DungeonWidth/2-1, 2+DungeonHeight).Add(p))).Size().Y
}
features := []string{}
if !info.Unknown {
features = append(features, info.Cell.ShortString(g, info.P))
if info.Cloud != "" && info.Sees {
features = append(features, info.Cloud)
}
if info.Lighted && info.Sees {
features = append(features, "lighted")
}
} else {
features = append(features, "unknown")
}
if info.Noise {
features = append(features, "noise")
}
if info.Unreachable {
features = append(features, "unreachable")
}
if !info.Sees && !info.Unknown {
features = append(features, "seen")
} else if info.Unknown {
features = append(features, "unexplored")
}
t := features[0]
if len(features) > 1 {
t += " (" + strings.Join(features[1:], ", ") + ")"
}
fg := ColorFg
if info.Unreachable {
fg = ColorOrange
}
desc := ""
if info.Unknown {
desc = "You do not know what is in there."
} else {
desc = info.Cell.Desc(g, info.P)
}
if info.Player {
if !md.targ.ex.scroll {
formatBox(t, desc, fg)
}
formatBox("Syu", "This is you, the monkey named Syu.", ColorBlue)
return
}
mons := info.Monster
if !mons.Exists() {
formatBox(t, desc, fg)
return
}
title := fmt.Sprintf("%s (%s %s)", mons.Kind, mons.State, dirString(mons.Dir))
if !info.Sees {
title = fmt.Sprintf("%s (seen)", mons.Kind)
}
var mfg gruid.Color
if g.Player.Sees(mons.P) {
mfg = mons.color(g)
} else {
_, mfg = mons.StyleKnowledge()
}
var mdesc []string
mdesc = append(mdesc, mons.Kind.Desc())
if info.Sees {
statuses := mons.statusesText()
if statuses != "" {
mdesc = append(mdesc, fmt.Sprintf("@hStatuses:@N %s", statuses))
}
}
mdesc = append(mdesc, "@hTraits:@N "+mons.traits())
if !md.targ.ex.scroll {
formatBox(t, desc, fg)
}
if !mons.Seen {
formatBox("unknown monster", "You sensed a monster there.", mfg)
} else {
formatBox(title, strings.Join(mdesc, "\n"), mfg)
}
}
func (m *monster) color(g *game) gruid.Color {
var fg gruid.Color
if m.Status(MonsLignified) {
fg = ColorFgLignifiedMonster
} else if m.Status(MonsConfused) {
fg = ColorFgConfusedMonster
} else if m.Status(MonsParalysed) {
fg = ColorFgParalysedMonster
} else if m.State == Resting {
fg = ColorFgSleepingMonster
} else if m.State == Hunting {
fg = ColorFgMonster
} else if m.Peaceful(g) {
fg = ColorFgPlayer
} else {
fg = ColorFgWanderingMonster
}
return fg
}
func (m *monster) statusesText() string {
infos := []string{}
for st, i := range m.Statuses {
if i > 0 {
infos = append(infos, fmt.Sprintf("%s(@s%d@N)", monsterStatus(st), m.Statuses[monsterStatus(st)]))
}
}
return strings.Join(infos, ", ")
}
func (m *monster) traits() string {
var info string
info += fmt.Sprintf("Their size is %s.", m.Kind.Size())
if m.Kind.Peaceful() {
info += "They are peaceful."
}
if m.Kind.CanOpenDoors() {
info += " " + "They can open doors."
}
if m.Kind.CanFly() {
info += " " + "They can fly."
}
if m.Kind.CanSwim() {
info += " " + "They can swim."
}
if m.Kind.ShallowSleep() {
info += " " + "They have very shallow sleep."
}
if m.Kind.ResistsLignification() {
info += " " + "They are unaffected by lignification."
}
if m.Kind.ReflectsTeleport() {
info += " " + "They partially reflect back oric teleport magic."
}
if m.Kind.GoodSmell() {
info += " " + "They have good sense of smell."
}
return info
}
func (md *model) updatePosInfo() {
g := md.g
pi := posInfo{}
p := md.targ.ex.p
pi.P = p
switch {
case !explored(g.Dungeon.Cell(p)):
pi.Unknown = true
if g.Noise[p] || g.NoiseIllusion[p] {
pi.Noise = true
}
if mons := g.lastMonsterKnownAt(p); mons.Exists() {
pi.Monster = mons
}
md.targ.ex.info = pi
return
//case !targ.Reachable(g, pos):
//pi.Unreachable = true
//return
}
if p == g.Player.P {
pi.Player = true
}
if g.Player.Sees(p) {
pi.Sees = true
}
c := g.Dungeon.Cell(p)
if t, ok := g.TerrainKnowledge[p]; ok {
c = t | c&Explored
}
if mons := g.MonsterAt(p); mons.Exists() && g.Player.Sees(p) {
pi.Monster = mons
} else if mons := g.lastMonsterKnownAt(p); mons.Exists() {
pi.Monster = mons
}
if cld, ok := g.Clouds[p]; ok && g.Player.Sees(p) {
pi.Cloud = cld.String()
}
pi.Cell = c
if g.Illuminated(p) && c.IsIlluminable() && g.Player.Sees(p) {
pi.Lighted = true
}
if g.Noise[p] || g.NoiseIllusion[p] {
pi.Noise = true
}
md.targ.ex.info = pi
}
func (md *model) computeHighlight() {
md.g.computePathHighlight(md.targ.ex.p)
}
func (g *game) computePathHighlight(p gruid.Point) {
path := g.PlayerPath(g.Player.P, p)
g.Highlight = map[gruid.Point]bool{}
for _, p := range path {
g.Highlight[p] = true
}
}
func (md *model) target() error {
g := md.g
p := md.targ.ex.p
if !explored(g.Dungeon.Cell(p)) {
return errors.New("You do not know this place.")
}
if terrain(g.Dungeon.Cell(p)) == WallCell && !g.Player.HasStatus(StatusDig) {
return errors.New("You cannot travel into a wall.")
}
path := g.PlayerPath(g.Player.P, p)
if len(path) == 0 {
return errors.New("There is no safe path to this place.")
}
if c := g.Dungeon.Cell(p); explored(c) && terrain(c) != WallCell {
g.AutoTarget = p
return nil
}
return errors.New("Invalid destination.")
}
func (md *model) nextMonster(key gruid.Key, p gruid.Point, data *examination) {
nmonster := data.nmonster
ms := md.targetMonsters()
if len(ms) == 0 {
return
}
if key == "+" {
nmonster++
} else {
nmonster--
}
if nmonster > len(ms)-1 {
nmonster = 0
} else if nmonster < 0 {
nmonster = len(ms) - 1
}
mons := ms[nmonster]
p = mons.P
data.nmonster = nmonster
md.Examine(p)
}
func (md *model) nextStair(data *examination) {
g := md.g
if data.sortedStairs == nil {
stairs := g.StairsSlice()
data.sortedStairs = g.SortedNearestTo(stairs, g.Player.P)
}
if data.stairIndex >= len(data.sortedStairs) {
data.stairIndex = 0
}
if len(data.sortedStairs) > 0 {
md.Examine(data.sortedStairs[data.stairIndex])
data.stairIndex++
}
}
func (md *model) nextObject(np gruid.Point, data *examination) {
g := md.g
nobject := data.nobject
if len(data.objects) == 0 {
for p := range g.Objects.Stairs {
data.objects = append(data.objects, p)
}
for p := range g.Objects.FakeStairs {
data.objects = append(data.objects, p)
}
for p := range g.Objects.Stones {
data.objects = append(data.objects, p)
}
for p := range g.Objects.Barrels {
data.objects = append(data.objects, p)
}
for p := range g.Objects.Magaras {
data.objects = append(data.objects, p)
}
for p := range g.Objects.Bananas {
data.objects = append(data.objects, p)
}
for p := range g.Objects.Items {
data.objects = append(data.objects, p)
}
for p := range g.Objects.Scrolls {
data.objects = append(data.objects, p)
}
for p := range g.Objects.Potions {
data.objects = append(data.objects, p)
}
data.objects = g.SortedNearestTo(data.objects, g.Player.P)
}
for i := 0; i < len(data.objects); i++ {
p := data.objects[nobject]
nobject++
if nobject > len(data.objects)-1 {
nobject = 0
}
if explored(g.Dungeon.Cell(p)) {
np = p
break
}
}
data.nobject = nobject
md.Examine(np)
}
func (md *model) excludeZone(p gruid.Point) {
g := md.g
if !explored(g.Dungeon.Cell(p)) {
g.Print("You cannot choose an unexplored cell for exclusion.")
} else {
g.ComputeExclusion(p)
}
}
func (md *model) clearExcludeZone(p gruid.Point) {
rg := visionRange(p, DefaultMonsterLOSRange)
rg.Iter(func(p gruid.Point) {
delete(md.g.ExclusionsMap, p)
})
}