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 pathplayer.go
554 lines (513 loc) · 12.9 KB
/
player.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package main
import (
"errors"
"fmt"
"github.com/anaseto/gruid"
"github.com/anaseto/gruid/rl"
)
type player struct {
HP int
HPbonus int
MP int
Bananas int
Magaras []magara
Dir gruid.Point
//Aptitudes map[aptitude]bool
Statuses map[status]int
Expire map[status]int
P gruid.Point
Target gruid.Point
LOS map[gruid.Point]bool
FOV *rl.FOV
Inventory inventory
}
type inventory struct {
Body item
Neck item
Misc item
}
const DefaultHealth = 5
func (pl *player) HPMax() int {
hpmax := DefaultHealth
if pl.Inventory.Body == CloakVitality {
hpmax += 2
}
if hpmax < 2 {
hpmax = 2
}
return hpmax
}
const DefaultMPmax = 6
func (pl *player) MPMax() int {
mpmax := DefaultMPmax
if pl.Inventory.Body == CloakMagic {
mpmax += 2
}
return mpmax
}
func (pl *player) HasStatus(st status) bool {
return pl.Statuses[st] > 0
}
func (g *game) AutoToDir() bool {
if g.MonsterInLOS() == nil {
p := g.Player.P.Add(g.AutoDir)
nbs := g.dirNeighbors(p, g.AutoDir)
blocked := g.dirBlocked(p, g.AutoDir)
if g.PlayerCanPass(p) && (g.autoDirNeighbors == nbs || nbs != dirFreeLaterals && blocked ||
nbs == dirBlockedLaterals && (g.autoDirChanged || g.dirNeighbors(g.Player.P, g.AutoDir) == dirBlockedLaterals)) {
again, err := g.PlayerBump(p)
if err != nil {
g.Print(err.Error())
g.AutoDir = ZP
return false
}
if again {
g.AutoDir = ZP
return false
}
// player moved in the direction
g.autoDirChanged = false
if blocked && nbs > 0 {
if g.PlayerCanPass(left(p, g.AutoDir)) {
g.AutoDir = left(p, g.AutoDir).Sub(p)
g.autoDirChanged = true
} else if g.PlayerCanPass(right(p, g.AutoDir)) {
g.AutoDir = right(p, g.AutoDir).Sub(p)
g.autoDirChanged = true
}
g.autoDirNeighbors = g.dirNeighbors(p, g.AutoDir)
}
return true
}
}
g.AutoDir = ZP
return false
}
type dirNeighbors int
const (
dirFreeLaterals dirNeighbors = iota
dirBlockedLeft
dirBlockedRight
dirBlockedLaterals
)
func (g *game) dirNeighbors(p, dir gruid.Point) (dn dirNeighbors) {
if !g.PlayerCanPass(left(p, dir)) {
dn += dirBlockedLeft
}
if !g.PlayerCanPass(right(p, dir)) {
dn += dirBlockedRight
}
return dn
}
func (g *game) dirBlocked(p, dir gruid.Point) bool {
return !g.PlayerCanPass(p.Add(dir))
}
func (g *game) GoToDir(dir gruid.Point) (again bool, err error) {
if g.MonsterInLOS() != nil {
g.AutoDir = ZP
return again, errors.New("You cannot travel while there are monsters in view.")
}
p := g.Player.P.Add(dir)
if !g.PlayerCanPass(p) {
return again, errors.New("You cannot move in that direction.")
}
again, err = g.PlayerBump(p)
if err != nil || again {
return again, err
}
g.AutoDir = dir
nbs := g.dirNeighbors(p, dir)
g.autoDirChanged = false
blocked := g.dirBlocked(p, dir)
if blocked && nbs > 0 {
if g.PlayerCanPass(left(p, dir)) {
g.AutoDir = left(p, dir).Sub(p)
g.autoDirChanged = true
} else if g.PlayerCanPass(right(p, dir)) {
g.AutoDir = right(p, dir).Sub(p)
g.autoDirChanged = true
}
g.autoDirNeighbors = g.dirNeighbors(p, g.AutoDir)
} else {
g.autoDirNeighbors = nbs
}
return again, err
}
func (g *game) MoveToTarget() bool {
if !valid(g.AutoTarget) {
return false
}
path := g.PlayerPath(g.Player.P, g.AutoTarget)
if g.MonsterInLOS() != nil {
g.AutoTarget = invalidPos
}
if len(path) < 1 {
g.AutoTarget = invalidPos
return false
}
var err error
var again bool
if len(path) > 1 {
again, err = g.PlayerBump(path[1])
if g.ExclusionsMap[path[1]] {
g.AutoTarget = invalidPos
}
} else {
g.WaitTurn()
}
if err != nil {
g.Print(err.Error())
g.AutoTarget = invalidPos
return false
}
if valid(g.AutoTarget) && g.Player.P == g.AutoTarget {
g.AutoTarget = invalidPos
}
return !again
}
func (g *game) WaitTurn() {
g.Stats.Waits++
}
func (g *game) MonsterCount() (count int) {
for _, mons := range g.Monsters {
if mons.Exists() {
count++
}
}
return count
}
func (g *game) Rest() error {
if terrain(g.Dungeon.Cell(g.Player.P)) != BarrelCell {
return fmt.Errorf("This place is not safe for sleeping.")
}
if cld, ok := g.Clouds[g.Player.P]; ok && cld == CloudFire {
return errors.New("You cannot rest on flames.")
}
if !g.NeedsRegenRest() && !g.StatusRest() {
return errors.New("You do not need to rest.")
}
if g.Player.Bananas <= 0 {
return errors.New("You cannot sleep without eating for dinner a banana first.")
}
// TODO: animation
//g.ui.DrawMessage("Resting...")
g.Resting = true
g.RestingTurns = RandInt(5) // you do not wake up when you want
g.Player.Bananas--
return nil
}
func (g *game) StatusRest() bool {
for st, q := range g.Player.Statuses {
if st.Info() {
continue
}
if q > 0 {
return true
}
}
return false
}
func (g *game) NeedsRegenRest() bool {
return g.Player.HP < g.Player.HPMax() || g.Player.MP < g.Player.MPMax()
}
func (g *game) Teleportation() {
var p gruid.Point
i := 0
count := 0
for {
count++
if count > maxIterations {
panic("Teleportation")
}
p = g.FreePassableCell()
if distance(p, g.Player.P) < 15 && i < maxIterations {
i++
continue
}
break
}
if valid(p) {
// should always happen
opos := g.Player.P
g.Print("You teleport away.")
g.md.TeleportAnimation(opos, p, true)
g.PlacePlayerAt(p)
} else {
// should not happen
g.Print("Something went wrong with the teleportation.")
}
}
const MaxBananas = 4
func (g *game) CollectGround() {
p := g.Player.P
c := g.Dungeon.Cell(p)
if c.IsNotable() {
g.AutoexploreMapRebuild = true
switchcell:
switch terrain(c) {
case BarrelCell:
// TODO: move here message
case BananaCell:
if g.Player.Bananas >= MaxBananas {
g.Print("There is a banana, but your pack is already full.")
} else {
g.Print("You take a banana.")
g.Player.Bananas++
g.StoryPrintf("Found banana (bananas: %d)", g.Player.Bananas)
g.Dungeon.SetCell(p, GroundCell)
delete(g.Objects.Bananas, p)
if g.Player.Bananas == MaxBananas {
AchBananaCollector.Get(g)
}
}
case MagaraCell:
for i, mag := range g.Player.Magaras {
if mag.Kind != NoMagara {
continue
}
g.Player.Magaras[i] = g.Objects.Magaras[p]
delete(g.Objects.Magaras, p)
g.Dungeon.SetCell(p, GroundCell)
g.Printf("You take the %s.", g.Player.Magaras[i])
g.StoryPrintf("Took %s", g.Player.Magaras[i])
break switchcell
}
g.Printf("You stand over %s.", Indefinite(g.Objects.Magaras[p].String(), false))
case FakeStairCell:
g.Dungeon.SetCell(p, GroundCell)
g.PrintStyled("You stand over fake stairs.", logSpecial)
g.PrintStyled("Harmonic illusions!", logSpecial)
g.StoryPrint("Found harmonic fake stairs!")
g.md.FoundFakeStairsAnimation()
case PotionCell:
g.DrinkPotion(p)
default:
g.Printf("You are standing over %s.", c.ShortDesc(g, p))
}
} else if terrain(c) == DoorCell {
g.Print("You stand at the door.")
}
if terrain(c).ReachNotable() {
g.Reach(p)
}
}
func (g *game) FallAbyss(style descendstyle) {
if g.Player.HasStatus(StatusLevitation) {
return
}
g.Player.HP -= 2
if g.Player.HP <= 0 {
g.Player.HP = 1
}
g.Player.MP -= 2
if g.Player.MP < 0 {
g.Player.MP = 0
}
if g.Player.Bananas > 0 {
g.Player.Bananas--
}
if style == DescendFall && g.Depth == MaxDepth || g.Depth == WinDepth {
g.Player.HP = 0
return
}
g.Descend(style)
}
func (g *game) AbyssJumpConfirmation() {
g.PrintStyled("Do you really want to jump into the abyss? (DANGEROUS) [y/N]", logConfirm)
g.md.mode = modeJumpConfirmation
}
func (g *game) DeepChasmDepth() bool {
return g.Depth == WinDepth || g.Depth == MaxDepth
}
func (g *game) AbyssJump() error {
if g.DeepChasmDepth() {
return errors.New("You cannot jump into deep chasm.")
}
g.AbyssJumpConfirmation()
return nil
}
func (g *game) PlayerBump(p gruid.Point) (again bool, err error) {
if !valid(p) {
return again, errors.New("You cannot move there.")
}
c := g.Dungeon.Cell(p)
switch {
case terrain(c) == BarrierCell && !g.Player.HasStatus(StatusLevitation):
return again, errors.New("You cannot move into a magical barrier.")
case terrain(c) == WindowCell && !g.Player.HasStatus(StatusDig):
return again, errors.New("You cannot pass through the closed window.")
case terrain(c) == BarrelCell && g.MonsterLOS[g.Player.P]:
return again, errors.New("You cannot enter a barrel while seen.")
}
mons := g.MonsterAt(p)
if c.IsJumpPropulsion() && !g.Player.HasStatus(StatusDig) {
err := g.WallJump(p)
if err != nil {
return again, err
}
} else if !mons.Exists() {
if g.Player.HasStatus(StatusLignification) {
return again, errors.New("You cannot move while lignified.")
}
if terrain(c) == ChasmCell && !g.Player.HasStatus(StatusLevitation) {
again = true
return again, g.AbyssJump()
}
if terrain(c) == BarrelCell {
g.Print("You hide yourself inside the barrel.")
} else if terrain(c) == TableCell {
g.Print("You hide yourself under the table.")
} else if terrain(c) == TreeCell {
g.Print("You climb to the top.")
} else if terrain(c) == HoledWallCell {
g.Print("You crawl under the wall.")
}
if c.IsDiggable() && terrain(c) != HoledWallCell {
g.Dungeon.SetCell(p, RubbleCell)
g.MakeNoise(WallNoise, p)
g.Print(g.CrackSound())
g.Fog(p, 1)
g.Stats.Digs++
g.Stats.DestructionUse++
if g.Stats.DestructionUse == 20 {
AchDestructorNovice.Get(g)
}
if g.Stats.DestructionUse == 40 {
AchDestructorInitiate.Get(g)
}
if g.Stats.DestructionUse == 60 {
AchDestructorMaster.Get(g)
}
}
if g.Player.Inventory.Body == CloakSmoke {
_, ok := g.Clouds[g.Player.P]
if !ok && g.Dungeon.Cell(g.Player.P).AllowsFog() {
g.Clouds[g.Player.P] = CloudFog
g.PushEventD(&posEvent{P: g.Player.P, Action: CloudEnd}, DurationSmokingCloakFog)
}
}
//}
g.Stats.Moves++
g.PlacePlayerAt(p)
} else if again, err = g.Jump(mons); err != nil {
return again, err
}
if g.Player.HasStatus(StatusSwift) {
again = true
g.Player.Statuses[StatusSwift]--
if !g.Player.HasStatus(StatusSwift) {
g.Print("You no longer feel swift.")
}
g.md.updateMapInfo()
return again, nil
}
return again, nil
}
func (g *game) SwiftFog() {
dij := &noisePath{g: g}
nodes := g.PR.DijkstraMap(dij, []gruid.Point{g.Player.P}, 2)
for _, n := range nodes {
_, ok := g.Clouds[n.P]
if !ok && g.Dungeon.Cell(n.P).AllowsFog() {
g.Clouds[n.P] = CloudFog
g.PushEvent(&posEvent{P: n.P, Action: CloudEnd}, g.Turn+DurationFog+RandInt(DurationFog/2))
}
}
g.PutStatus(StatusSwift, DurationShortSwiftness)
g.ComputeLOS()
g.Print("You feel an energy burst and smoke comes out from you.")
}
func (g *game) Confusion() {
if g.PutStatus(StatusConfusion, DurationConfusionPlayer) {
g.Print("You feel confused.")
}
}
// PlacePlayerAt moves the player to a given position, swapping positions with
// a monster if necessary, and handles LOS and monsters awareness update,
// ground collecting, and footsteps noise.
func (g *game) PlacePlayerAt(p gruid.Point) {
if p == g.Player.P {
return
}
g.Player.Dir = dirnorm(g.Player.P, p)
m := g.MonsterAt(p)
ppos := g.Player.P
g.Player.P = p
if m.Exists() {
m.MoveTo(g, ppos)
m.Swapped = true
}
if terrain(g.Dungeon.Cell(g.Player.P)) == QueenRockCell && !g.Player.HasStatus(StatusLevitation) {
g.MakeNoise(QueenRockFootstepNoise, g.Player.P)
g.Print("Tap-tap.")
}
g.CollectGround()
g.ComputeLOS()
g.MakeMonstersAware()
}
const LignificationHPbonus = 4
func (g *game) EnterLignification() {
if g.PutStatus(StatusLignification, DurationLignificationPlayer) {
g.Print("You feel rooted to the ground.")
g.Player.HPbonus += LignificationHPbonus
}
}
func (g *game) ExtinguishFire() error {
g.Dungeon.SetCell(g.Player.P, ExtinguishedLightCell)
g.Objects.Lights[g.Player.P] = false
g.Stats.Extinguishments++
if g.Stats.Extinguishments >= 15 {
AchExtinguisher.Get(g)
}
g.Print("You extinguish the fire.")
return nil
}
func (g *game) PutStatus(st status, duration int) bool {
if g.Player.Statuses[st] != 0 {
return false
}
g.Player.Statuses[st] += duration
g.PushEventD(&statusEvent{Status: st}, DurationStatusStep)
g.Stats.Statuses[st]++
if st.Good() {
g.Player.Expire[st] = g.Turn + duration
}
return true
}
func (g *game) PutFakeStatus(st status, duration int) bool {
if g.Player.Statuses[st] != 0 {
return false
}
g.Player.Statuses[st] += duration
g.Stats.Statuses[st]++
if st.Good() {
g.Player.Expire[st] = g.Turn + duration
}
return true
}
func (g *game) UpdateKnowledge(p gruid.Point, c cell) {
if g.Player.Sees(p) {
return
}
_, ok := g.TerrainKnowledge[p]
if !ok {
g.TerrainKnowledge[p] = c
}
}
func (g *game) PlayerCanPass(p gruid.Point) bool {
if !valid(p) {
return false
}
c := g.Dungeon.Cell(p)
return c.IsPlayerPassable() ||
g.Player.HasStatus(StatusLevitation) && (terrain(c) == BarrierCell || c.IsLevitatePassable()) ||
g.Player.HasStatus(StatusDig) && c.IsDiggable()
}
func (g *game) PlayerCanJumpPass(p gruid.Point) bool {
if !valid(p) {
return false
}
c := g.Dungeon.Cell(p)
return c.IsJumpPassable() ||
g.Player.HasStatus(StatusLevitation) && terrain(c) == BarrierCell
}