-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
721 lines (678 loc) · 25.9 KB
/
path.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
* path.go
* Rescape
*
* Created by Chad Pierce on 1/16/2022.
* Copyright 2022. All rights reserved.
*
* This file is part of Rescape.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"math/rand"
"time"
"fmt"
"github.com/gdamore/tcell/v2"
)
// NOTES
// basic pathfinding is a bit wonky and needs work
// mobs will align diagonally when chasing to the east
// the following mit course was used to develop the bfs algorithm:
// https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-13-breadth-first-search-bfs/
// this was ported from rust and needs some additional changes to make more go-like
type PathPoint struct {
i, x, y int
}
func isAdjacent(sx, sy, tx, ty int) bool {
if tx == sx - 1 && ty == sy - 1 { return true
} else if tx == sx && ty == sy - 1 { return true
} else if tx == sx + 1 && ty == sy - 1 { return true
} else if tx == sx + 1 && ty == sy { return true
} else if tx == sx + 1 && ty == sy + 1 { return true
} else if tx == sx && ty == sy + 1 { return true
} else if tx == sx - 1 && ty == sy + 1 { return true
} else if tx == sx - 1 && ty == sy { return true
} else { return false }
}
func getAdjacentBlockers(x, y int, actors []Actor, items []Item, grid *[][]bool) {
var moves []Point
moves = append(moves,
Point { x-1, y-1 },
Point { x, y-1 },
Point { x+1, y-1 },
Point { x+1, y },
Point { x+1, y+1 },
Point { x, y+1 },
Point { x-1, y+1 },
Point { x-1, y } )
for _, m := range moves {
for _, a := range actors {
if a.pos.x == m.x && a.pos.y == m.y && a.blocks == true {
(*grid)[m.x][m.y] = true
}
}
for _, i := range items {
if i.pos.x == m.x && i.pos.y == m.y && i.blocks == true {
(*grid)[m.x][m.y] = true
}
}
}
}
func getNeighbors(source_x, source_y int, grid [][]bool, level [][]int) []Point {
var moves []Point
if grid[source_x - 1][source_y - 1] == false && level[source_x - 1][source_y - 1] == -1 {
moves = append(moves, Point{source_x - 1, source_y - 1})
}
if grid[source_x][source_y - 1] == false && level[source_x][source_y - 1] == -1 {
moves = append(moves, Point{source_x, source_y - 1})
}
if grid[source_x + 1][source_y - 1] == false && level[source_x + 1][source_y - 1] == -1 {
moves = append(moves, Point{source_x + 1, source_y - 1})
}
if grid[source_x + 1][source_y] == false && level[source_x + 1][source_y] == -1 {
moves = append(moves, Point{source_x + 1, source_y})
}
if grid[source_x + 1][source_y + 1] == false && level[source_x + 1][source_y + 1] == -1 {
moves = append(moves, Point{source_x + 1, source_y + 1})
}
if grid[source_x][source_y + 1] == false && level[source_x][source_y + 1] == -1 {
moves = append(moves, Point{source_x, source_y + 1})
}
if grid[source_x - 1][source_y + 1] == false && level[source_x - 1][source_y + 1] == -1 {
moves = append(moves, Point{source_x - 1, source_y + 1})
}
if grid[source_x - 1][source_y] == false && level[source_x - 1][source_y] == -1 {
moves = append(moves, Point{source_x - 1, source_y})
}
return moves
}
func getPNeighbors(source_x, source_y int, grid [][]bool) []Point {
var moves []Point
if grid[source_x - 1][source_y - 1] == false {
moves = append(moves, Point{source_x - 1, source_y - 1})
}
if grid[source_x][source_y - 1] == false {
moves = append(moves, Point{source_x, source_y - 1})
}
if grid[source_x + 1][source_y - 1] == false {
moves = append(moves, Point{source_x + 1, source_y - 1})
}
if grid[source_x + 1][source_y] == false {
moves = append(moves, Point{source_x + 1, source_y})
}
if grid[source_x + 1][source_y + 1] == false {
moves = append(moves, Point{source_x + 1, source_y + 1})
}
if grid[source_x][source_y + 1] == false {
moves = append(moves, Point{source_x, source_y + 1})
}
if grid[source_x - 1][source_y + 1] == false {
moves = append(moves, Point{source_x - 1, source_y + 1})
}
if grid[source_x - 1][source_y] == false {
moves = append(moves, Point{source_x - 1, source_y})
}
return moves
}
// TODO pass only cur floor
func getBFSGrid(g *Game) [][]bool {
grid := make([][]bool, MaxWidth)
for i, _ := range grid {
grid[i] = make([]bool, MaxHeight)
}
for h:= 0; h < MaxHeight; h++ {
for w := 0; w < MaxWidth; w++ {
grid[w][h] = false
}
}
for h := 0; h < MaxHeight; h++ {
for w := 0; w < MaxWidth; w++ {
for _, t := range g.floors[g.cur].tiles {
if t.blocks == true &&
t.pos.x == w && t.pos.y == h {
grid[w][h] = true
}
}
}
}
return grid
}
//this is not being used - slow and buggy - bfsTest2 is better
func (g *Game) bfsTest(id, sx, sy, tx, ty int) {
grid := getBFSGrid(g)
getAdjacentBlockers(sx, sy, g.floors[g.cur].actors, g.floors[g.cur].items, &grid)
levels := make([][]int, MaxWidth)
for i, _ := range levels {
levels[i] = make([]int, MaxHeight)
}
for h := 0; h < MaxHeight; h++ {
for w := 0; w < MaxWidth; w++ {
levels[w][h] = -1
}
}
lvl := 1
levels[sx][sy] = lvl - 1
frontier := getNeighbors(sx, sy, grid, levels)
if len(frontier) <= 0 { return } // if no moves
for _, f := range frontier {
levels[f.x][f.y] = lvl
}
lvl++
for len(frontier) > 0 {
var next []Point
for _, f := range frontier {
neighbors := getNeighbors(f.x, f.y, grid, levels)
for _, n := range neighbors {
levels[n.x][n.y] = lvl
next = append(next, Point { n.x, n.y })
// for id, tile := range g.floors[g.cur].tiles {
// //fmt.Printf("n: %v, %v", n.y, n.y)
// if tile.pos.x == n.x && tile.pos.y == n.y {
// //fmt.Printf("m:%v %v l: %v", n.x, n.y, level[n.x][n.y])
// switch levels[n.x][n.y] {
// case -1: g.floors[g.cur].tiles[id].glyph = 'w'
// g.floors[g.cur].tiles[id].bg = tcell.Color20
// case 0: g.floors[g.cur].tiles[id].glyph = '0'
// g.floors[g.cur].tiles[id].bg = tcell.Color21
// case 1: g.floors[g.cur].tiles[id].glyph = '1'
// g.floors[g.cur].tiles[id].bg = tcell.Color22
// case 2: g.floors[g.cur].tiles[id].glyph = '2'
// g.floors[g.cur].tiles[id].bg = tcell.Color23
// case 3: g.floors[g.cur].tiles[id].glyph = '3'
// g.floors[g.cur].tiles[id].bg = tcell.Color24
// case 4: g.floors[g.cur].tiles[id].glyph = '4'
// g.floors[g.cur].tiles[id].bg = tcell.Color25
// case 5: g.floors[g.cur].tiles[id].glyph = '5'
// g.floors[g.cur].tiles[id].bg = tcell.Color26
// case 6: g.floors[g.cur].tiles[id].glyph = '6'
// g.floors[g.cur].tiles[id].bg = tcell.Color27
// case 7: g.floors[g.cur].tiles[id].glyph = '7'
// g.floors[g.cur].tiles[id].bg = tcell.Color28
// case 8: g.floors[g.cur].tiles[id].glyph = '8'
// g.floors[g.cur].tiles[id].bg = tcell.Color29
// case 9: g.floors[g.cur].tiles[id].glyph = '9'
// g.floors[g.cur].tiles[id].bg = tcell.Color30
// case 10: g.floors[g.cur].tiles[id].glyph = 'A'
// g.floors[g.cur].tiles[id].bg = tcell.Color31
// case 11: g.floors[g.cur].tiles[id].glyph = 'B'
// g.floors[g.cur].tiles[id].bg = tcell.Color32
// case 12: g.floors[g.cur].tiles[id].glyph = 'C'
// g.floors[g.cur].tiles[id].bg = tcell.Color33
// case 13: g.floors[g.cur].tiles[id].glyph = 'D'
// g.floors[g.cur].tiles[id].bg = tcell.Color34
// case 14: g.floors[g.cur].tiles[id].glyph = 'E'
// g.floors[g.cur].tiles[id].bg = tcell.Color35
// case 15: g.floors[g.cur].tiles[id].glyph = 'F'
// default:
// g.floors[g.cur].tiles[id].glyph = 'x'
// g.floors[g.cur].tiles[id].bg = tcell.Color37
// }
// }
// }
//fmt.Printf("next: %v", next)
// fmt.Printf("lnext: %v", len(next))
// fmt.Printf("lh: %v", level[tx][ty])
//}
}
}
frontier = next
next = nil
lvl++
}
//get neighbors of target
px := tx
py := ty
cnt := 0
for isAdjacent(px, py, sx, sy) == false {
var shortPath []Point
var short int
pNeighbors := getPNeighbors(px, py, grid)
var neighbors []Point
for _, pN := range pNeighbors {
pNPos := Point { pN.x, pN.y }
if g.floors[g.cur].actors[id].pos != pNPos {
neighbors = append(neighbors, pN)
}
}
for i, n := range neighbors {
if i==0 || levels[n.x][n.y] < short {
short = levels[n.x][n.y]
}
}
for _, sh := range neighbors {
if levels[sh.x][sh.y] == short { //&& !isB {
shortPath = append(shortPath, sh)
}
}
if len(shortPath) < 1 {
g.dbg("no moves?!?")
return
} else {
rand.Seed(time.Now().UnixNano())
theMove := shortPath[rand.Intn(len(shortPath))]
px = theMove.x
py = theMove.y
}
cnt++
if cnt > 1000 {
g.dbg("break - hack to fix infinite loop!")
break
}
}
dx := px - sx
dy := py - sy
g.floors[g.cur].aiTakeTurnPathTest(id, dx, dy)
}
func moveLine (id int, sPos, tPos Point, g *Game) {
line := getLine(sPos.x, sPos.y, tPos.x, tPos.y)
dx := line[1].x - sPos.x
dy := line[1].y - sPos.y
if isAdjacent(sPos.x, sPos.y, tPos.x, tPos.y) {
g.dbg("attack! moveLine")
} else {
isB, _ := isBlocked(sPos.x + dx, sPos.y + dy, g.floors[g.cur].actors, g.floors[g.cur].tiles)
if !isB {
g.floors[g.cur].aiTakeTurnPathTest(id, dx, dy)
}
}
}
func confusedMove(id int, sPos, tPos Point, g *Game) {
if isAdjacent(sPos.x, sPos.y, tPos.x, tPos.y) {
dmg := g.floors[g.cur].actors[id].meleeAttack(&g.hero)
g.addMessage(fmt.Sprintf("%v attacks for %v damage!", g.floors[g.cur].actors[id].name, dmg), tcell.ColorRed)
} else {
grid := getBFSGrid(g)
n := getPNeighbors(sPos.x, sPos.y, grid)
n = append(n, Point { sPos.x, sPos.y })
rand.Seed(time.Now().UnixNano())
theMove := n[rand.Intn(len(n))]
dx := theMove.x - sPos.x
dy := theMove.y - sPos.y
g.floors[g.cur].aiTakeTurnPathTest(id, dx, dy)
}
}
func getRandomFloorTile(g [][]bool) Point {
var emptyTiles []Point
for h:= 0; h < MaxHeight; h++ {
for w := 0; w < MaxWidth; w++ {
if g[w][h] == false {
emptyTiles = append(emptyTiles, Point { w, h })
}
}
}
rand.Seed(time.Now().UnixNano())
randTile := emptyTiles[rand.Intn(len(emptyTiles))]
return randTile
}
func wanderMove(id int, g *Game) {
//mobs with no target choose a random target,
//if they reach their target they choose a new one
a := g.floors[g.cur].actors[id]
curPos := a.pos
if a.target.x == -1 || a.pos == a.target {
g.floors[g.cur].actors[id].target = getRandomFloorTile(getBFSGrid(g))
}
g.bfsTest2(id, a.pos.x, a.pos.y, g.floors[g.cur].actors[id].target.x, g.floors[g.cur].actors[id].target.y)
//after moving, if they are in the same pos they choose a new target
if g.floors[g.cur].actors[id].pos == curPos {
g.floors[g.cur].actors[id].target = getRandomFloorTile(getBFSGrid(g))
}
}
func dumbMove(id int, sPos, tPos Point, g *Game) {
// TODO WORKING check for los and move straight at target if not blocked
if isAdjacent(sPos.x, sPos.y, tPos.x, tPos.y) {
dmg := g.floors[g.cur].actors[id].meleeAttack(&g.hero)
g.addMessage(fmt.Sprintf("%v attacks for %v damage!", g.floors[g.cur].actors[id].name, dmg), tcell.ColorRed)
} else {
g.bfsTest2(id, sPos.x, sPos.y, tPos.x, tPos.y)
}
}
func (g *Game) heroDumbMove() {
sPos := Point { g.hero.pos.x, g.hero.pos.y }
tPos := Point { g.hero.target.x, g.hero.target.y }
isBlockedPath := false
if isAdjacent(sPos.x, sPos.y, tPos.x, tPos.y) {
g.dbg("hero hits it")
} else {
line := getLine(sPos.x, sPos.y, tPos.x, tPos.y)
for _, l := range line {
// if actor cant make straigt line, do bfs
isB, _ := isBlocked(l.x, l.y, g.floors[g.cur].actors, g.floors[g.cur].tiles)
if isB {
isBlockedPath = true
}
if isBlockedPath {
g.heroBfsTest()
} else {
g.dbg("move that dir")
dx := line[1].x - sPos.x
dy := line[1].y - sPos.y
heroMove(g, dx, dy)
sawSomething, sawWhat := g.floors[g.cur].isActorsInFOV(g.hero)
if sawSomething {
g.addMessage(fmt.Sprintf("You see %v.", sawWhat), tcell.ColorDefault)
g.state = Playing
} else if g.hero.target == g.hero.pos {
g.state = Playing
}
}
}
}
}
func (f *Floor) isExitCorridor(x, y, dir int) bool {
var s1 bool
var s2 bool
var f1 bool
var f2 bool
switch dir {
case 1:
for _, tile := range f.tiles {
if tile.pos == (Point { x-1, y }) { s1 = tile.blocks }
if tile.pos == (Point { x+1, y }) { s2 = tile.blocks }
if tile.pos == (Point { x-1, y-1 }) { f1 = tile.blocks }
if tile.pos == (Point { x+1, y-1 }) { f2 = tile.blocks }
}
case 3:
for _, tile := range f.tiles {
if tile.pos == (Point { x, y-1 }) { s1 = tile.blocks }
if tile.pos == (Point { x, y+1 }) { s2 = tile.blocks }
if tile.pos == (Point { x+1, y-1 }) { f1 = tile.blocks }
if tile.pos == (Point { x+1, y+1 }) { f2 = tile.blocks }
}
case 5:
for _, tile := range f.tiles {
if tile.pos == (Point { x-1, y }) { s1 = tile.blocks }
if tile.pos == (Point { x+1, y }) { s2 = tile.blocks }
if tile.pos == (Point { x-1, y+1 }) { f1 = tile.blocks }
if tile.pos == (Point { x+1, y+1 }) { f2 = tile.blocks }
}
case 7:
for _, tile := range f.tiles {
if tile.pos == (Point { x-1, y-1 }) { s1 = tile.blocks }
if tile.pos == (Point { x-1, y+1 }) { s2 = tile.blocks }
if tile.pos == (Point { x-2, y-1 }) { f1 = tile.blocks }
if tile.pos == (Point { x-2, y+1 }) { f2 = tile.blocks }
}
default: return false
}
if (s1 == true && f1 == false) || (s2 == true && f2 == false) {
return true
} else {
return false
}
}
func (f *Floor) setRunTarget(hero *Actor, dir int, g *Game) {
var tx int
var ty int
switch dir {
case 0: tx = -1; ty = -1
case 1: tx = 0; ty = -1
case 2: tx = +1; ty = -1
case 3: tx = +1; ty = 0
case 4: tx = 1; ty = 1
case 5: tx = 0; ty = 1
case 6: tx = -1; ty = 1
case 7: tx = -1; ty = 0
default: tx = hero.pos.x; ty = hero.pos.y // TODO handle this better
}
curx := hero.pos.x
cury := hero.pos.y
out:
for {
curx += tx
cury += ty
for i, tile := range f.tiles {
if tile.pos == (Point{curx, cury}) {
if tile.blocks == true || f.isExitCorridor(curx, cury, dir) {
if dir == 1 || dir == 5 {
hero.target = Point { curx, f.tiles[i-1].pos.y }
} else if dir == 3 {
hero.target = Point { f.tiles[i].pos.x, cury }
// 3 and 7 are split up as a hack
// because running east was sticking
} else if dir == 7 {
hero.target = Point { f.tiles[i-1].pos.x, cury }
} else {
hero.target = Point { f.tiles[i-1].pos.x, f.tiles[i-1].pos.y }
}
break out
}
}
}
}
}
func (g *Game) heroRun() {
sawSomething, sawWhat := g.floors[g.cur].isActorsInFOV(g.hero)
if sawSomething {
g.addMessage(fmt.Sprintf("You see %v.", sawWhat), tcell.ColorDefault)
g.state = Playing
}
if g.hero.pos != g.hero.target && !sawSomething {
line := getLine(g.hero.pos.x, g.hero.pos.y, g.hero.target.x, g.hero.target.y)
isB, _ := isBlocked(line[1].x, line[1].y, g.floors[g.cur].actors, g.floors[g.cur].tiles)
if isB {
g.state = Playing
} else {
heroMove(g, line[1].x - g.hero.pos.x, line[1].y - g.hero.pos.y)
if g.hero.target == g.hero.pos {
g.state = Playing
}
}
} else {
g.state = Playing
}
}
func (g *Game) heroConfused() Point {
grid := getBFSGrid(g)
n := getPNeighbors(g.hero.pos.x, g.hero.pos.y, grid)
n = append(n, Point { g.hero.pos.x, g.hero.pos.y })
rand.Seed(time.Now().UnixNano())
theMove := n[rand.Intn(len(n))]
dx := theMove.x - g.hero.pos.x
dy := theMove.y - g.hero.pos.y
heroMove(g, dx, dy)
g.dbg(fmt.Sprintf("heromove: %v, %v ", dx, dy))
return g.hero.pos
}
// TODO combine this with normal AI mob bfs move func
func (g *Game) heroBfsTest() {
tx := g.hero.target.x
ty := g.hero.target.y
sx := g.hero.pos.x
sy := g.hero.pos.y
grid := getBFSGrid(g)
getAdjacentBlockers(sx, sy, g.floors[g.cur].actors, g.floors[g.cur].items, &grid)
levels := make([][]int, MaxWidth)
for i, _ := range levels {
levels[i] = make([]int, MaxHeight)
}
for h := 0; h < MaxHeight; h++ {
for w := 0; w < MaxWidth; w++ {
levels[w][h] = -1
}
}
lvl := 1
levels[sx][sy] = lvl - 1
frontier := getNeighbors(sx, sy, grid, levels)
if len(frontier) <= 0 { return } // if no moves
for _, f := range frontier {
levels[f.x][f.y] = lvl
}
lvl++
for len(frontier) > 0 {
var next []Point
for _, f := range frontier {
neighbors := getNeighbors(f.x, f.y, grid, levels)
for _, n := range neighbors {
levels[n.x][n.y] = lvl
next = append(next, Point { n.x, n.y })
}
}
frontier = next
next = nil
lvl++
}
px := tx
py := ty
cnt := 0
for isAdjacent(px, py, sx, sy) == false {
var shortPath []Point
var short int
pNeighbors := getPNeighbors(px, py, grid)
var neighbors []Point
for _, pN := range pNeighbors {
pNPos := Point { pN.x, pN.y }
if g.hero.pos != pNPos {
neighbors = append(neighbors, pN)
}
}
for i, n := range neighbors {
if i==0 || levels[n.x][n.y] < short {
short = levels[n.x][n.y]
}
}
for _, sh := range neighbors {
if levels[sh.x][sh.y] == short { //&& !isB {
shortPath = append(shortPath, sh)
}
}
if len(shortPath) < 1 {
g.dbg("no moves?!?")
return
} else {
rand.Seed(time.Now().UnixNano())
theMove := shortPath[rand.Intn(len(shortPath))]
px = theMove.x
py = theMove.y
}
cnt++
if cnt > 2000 {
g.dbg("break - hack to fix infinite loop!")
break
}
}
dx := px - sx
dy := py - sy
heroMove(g, dx, dy)
sawSomething, sawWhat := g.floors[g.cur].isActorsInFOV(g.hero)
if sawSomething {
g.addMessage(fmt.Sprintf("You see %v.", sawWhat), tcell.ColorDefault)
g.state = Playing
} else if g.hero.target == g.hero.pos {
g.state = Playing
}
}
// this one does bfs from destination to source and is faster
func (g *Game) bfsTest2(id, sx, sy, tx, ty int) {
grid := getBFSGrid(g)
getAdjacentBlockers(sx, sy, g.floors[g.cur].actors, g.floors[g.cur].items, &grid)
levels := make([][]int, MaxWidth)
for i, _ := range levels {
levels[i] = make([]int, MaxHeight)
}
for h := 0; h < MaxHeight; h++ {
for w := 0; w < MaxWidth; w++ {
levels[w][h] = -1
}
}
lvl := 1
levels[tx][ty] = lvl - 1
frontier := getNeighbors(tx, ty, grid, levels)
if len(frontier) <= 0 { return } // if no moves
for _, f := range frontier {
levels[f.x][f.y] = lvl
}
lvl++
isSourceReached := false
for len(frontier) > 0 {
var next []Point
for _, f := range frontier {
neighbors := getNeighbors(f.x, f.y, grid, levels)
for _, n := range neighbors {
levels[n.x][n.y] = lvl
next = append(next, Point { n.x, n.y })
if n.x == sx && n.y == sy { isSourceReached = true }
// for id, tile := range g.floors[g.cur].tiles {
// if tile.pos.x == n.x && tile.pos.y == n.y {
// switch levels[n.x][n.y] {
// case -1: g.floors[g.cur].tiles[id].glyph = 'w'
// g.floors[g.cur].tiles[id].bg = tcell.Color20
// case 0: g.floors[g.cur].tiles[id].glyph = '0'
// g.floors[g.cur].tiles[id].bg = tcell.Color21
// case 1: g.floors[g.cur].tiles[id].glyph = '1'
// g.floors[g.cur].tiles[id].bg = tcell.Color22
// case 2: g.floors[g.cur].tiles[id].glyph = '2'
// g.floors[g.cur].tiles[id].bg = tcell.Color23
// case 3: g.floors[g.cur].tiles[id].glyph = '3'
// g.floors[g.cur].tiles[id].bg = tcell.Color24
// case 4: g.floors[g.cur].tiles[id].glyph = '4'
// g.floors[g.cur].tiles[id].bg = tcell.Color25
// case 5: g.floors[g.cur].tiles[id].glyph = '5'
// g.floors[g.cur].tiles[id].bg = tcell.Color26
// case 6: g.floors[g.cur].tiles[id].glyph = '6'
// g.floors[g.cur].tiles[id].bg = tcell.Color27
// case 7: g.floors[g.cur].tiles[id].glyph = '7'
// g.floors[g.cur].tiles[id].bg = tcell.Color28
// case 8: g.floors[g.cur].tiles[id].glyph = '8'
// g.floors[g.cur].tiles[id].bg = tcell.Color29
// case 9: g.floors[g.cur].tiles[id].glyph = '9'
// g.floors[g.cur].tiles[id].bg = tcell.Color30
// case 10: g.floors[g.cur].tiles[id].glyph = 'A'
// g.floors[g.cur].tiles[id].bg = tcell.Color31
// case 11: g.floors[g.cur].tiles[id].glyph = 'B'
// g.floors[g.cur].tiles[id].bg = tcell.Color32
// case 12: g.floors[g.cur].tiles[id].glyph = 'C'
// g.floors[g.cur].tiles[id].bg = tcell.Color33
// case 13: g.floors[g.cur].tiles[id].glyph = 'D'
// g.floors[g.cur].tiles[id].bg = tcell.Color34
// case 14: g.floors[g.cur].tiles[id].glyph = 'E'
// g.floors[g.cur].tiles[id].bg = tcell.Color35
// case 15: g.floors[g.cur].tiles[id].glyph = 'F'
// default:
// g.floors[g.cur].tiles[id].glyph = 'x'
// g.floors[g.cur].tiles[id].bg = tcell.Color37
// }
// }
// }
}
}
if isSourceReached { break }
frontier = next
next = nil
lvl++
}
moveLevel := levels[sx][sy] - 1
pNeighbors := getPNeighbors(sx, sy, grid)
if pNeighbors == nil { g.dbg("nil"); return }
var theMoves []Point
for _, p := range pNeighbors {
if levels[p.x][p.y] == moveLevel {
theMoves = append(theMoves, p)
}
}
var theMove Point
rand.Seed(time.Now().UnixNano())
if theMoves == nil {
theMove = Point { sx, sy }
} else if len(theMoves) == 1 {
theMove = theMoves[0]
} else {
theMove = theMoves[1]
}
dx := theMove.x - sx
dy := theMove.y - sy
g.floors[g.cur].aiTakeTurnPathTest(id, dx, dy)
}