This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdungeon.go
1571 lines (1495 loc) · 32.3 KB
/
dungeon.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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// many ideas here from articles found at http://www.roguebasin.com/
package main
import (
"sort"
)
type dungeon struct {
Gen dungen
Cells []cell
}
type cell struct {
T terrain
Explored bool
}
type terrain int
const (
WallCell terrain = iota
FreeCell
)
type dungen int
const (
GenCaveMap dungen = iota
GenRoomMap
GenCellularAutomataCaveMap
GenCaveMapTree
GenRuinsMap
GenBSPMap
)
func (dg dungen) Use(g *game) {
switch dg {
case GenCaveMap:
g.GenCaveMap(DungeonHeight, DungeonWidth)
case GenRoomMap:
g.GenRoomMap(DungeonHeight, DungeonWidth)
case GenCellularAutomataCaveMap:
g.GenCellularAutomataCaveMap(DungeonHeight, DungeonWidth)
case GenCaveMapTree:
g.GenCaveMapTree(DungeonHeight, DungeonWidth)
case GenRuinsMap:
g.GenRuinsMap(DungeonHeight, DungeonWidth)
case GenBSPMap:
g.GenBSPMap(DungeonHeight, DungeonWidth)
}
g.Dungeon.Gen = dg
g.Stats.DLayout[g.Depth] = dg.String()
}
func (dg dungen) String() (text string) {
switch dg {
case GenCaveMap:
text = "OC"
case GenRoomMap:
text = "BR"
case GenCellularAutomataCaveMap:
text = "EC"
case GenCaveMapTree:
text = "TC"
case GenRuinsMap:
text = "RR"
case GenBSPMap:
text = "DT"
}
return text
}
func (dg dungen) Description() (text string) {
switch dg {
case GenCaveMap:
text = "open cave"
case GenRoomMap:
text = "big rooms"
case GenCellularAutomataCaveMap:
text = "eight cave"
case GenCaveMapTree:
text = "tree-like cave"
case GenRuinsMap:
text = "ruined rooms"
case GenBSPMap:
text = "deserted town"
}
return text
}
type room struct {
pos position
w int
h int
}
func (d *dungeon) Cell(pos position) cell {
return d.Cells[pos.idx()]
}
func (d *dungeon) Border(pos position) bool {
return pos.X == DungeonWidth-1 || pos.Y == DungeonHeight-1 || pos.X == 0 || pos.Y == 0
}
func (d *dungeon) SetCell(pos position, t terrain) {
d.Cells[pos.idx()].T = t
}
func (d *dungeon) SetExplored(pos position) {
d.Cells[pos.idx()].Explored = true
}
func roomDistance(r1, r2 room) int {
return Abs(r1.pos.X-r2.pos.X) + Abs(r1.pos.Y-r2.pos.Y)
}
func nearRoom(rooms []room, r room) room {
closest := rooms[0]
d := roomDistance(r, closest)
for _, nextRoom := range rooms {
nd := roomDistance(r, nextRoom)
if nd < d {
n := RandInt(10)
if n > 3 {
d = nd
closest = nextRoom
}
}
}
return closest
}
func nearestRoom(rooms []room, r room) room {
closest := rooms[0]
d := roomDistance(r, closest)
for _, nextRoom := range rooms {
nd := roomDistance(r, nextRoom)
if nd < d {
n := RandInt(10)
if n > 0 {
d = nd
closest = nextRoom
}
}
}
return closest
}
func intersectsRoom(rooms []room, r room) bool {
for _, rr := range rooms {
if (r.pos.X+r.w-1 >= rr.pos.X && rr.pos.X+rr.w-1 >= r.pos.X) &&
(r.pos.Y+r.h-1 >= rr.pos.Y && rr.pos.Y+rr.h-1 >= r.pos.Y) {
return true
}
}
return false
}
func (d *dungeon) connectRooms(r1, r2 room) {
x := r1.pos.X
if x < r2.pos.X {
x += r1.w - 1
}
y := r1.pos.Y
if y < r2.pos.Y {
y += r1.h - 1
}
d.SetCell(position{x, y}, FreeCell)
count := 0
for {
count++
if count > 1000 {
panic("ConnectRooms")
}
if x < r2.pos.X {
x++
d.SetCell(position{x, y}, FreeCell)
continue
}
if x > r2.pos.X {
x--
d.SetCell(position{x, y}, FreeCell)
continue
}
if y < r2.pos.Y {
y++
d.SetCell(position{x, y}, FreeCell)
continue
}
if y > r2.pos.Y {
y--
d.SetCell(position{x, y}, FreeCell)
continue
}
break
}
d.SetCell(r2.pos, FreeCell)
}
func (d *dungeon) connectRoomsDiagonally(r1, r2 room) {
x := r1.pos.X
if x < r2.pos.X {
x += r1.w - 1
}
y := r1.pos.Y
if y < r2.pos.Y {
y += r1.h - 1
}
d.SetCell(position{x, y}, FreeCell)
count := 0
for {
count++
if count > 1000 {
panic("ConnectRooms")
}
if x < r2.pos.X && y < r2.pos.Y {
x++
d.SetCell(position{x, y}, FreeCell)
y++
d.SetCell(position{x, y}, FreeCell)
continue
}
if x > r2.pos.X && y < r2.pos.Y {
x--
d.SetCell(position{x, y}, FreeCell)
y++
d.SetCell(position{x, y}, FreeCell)
continue
}
if x > r2.pos.X && y > r2.pos.Y {
x--
d.SetCell(position{x, y}, FreeCell)
y--
d.SetCell(position{x, y}, FreeCell)
continue
}
if x < r2.pos.X && y > r2.pos.Y {
x++
d.SetCell(position{x, y}, FreeCell)
y--
d.SetCell(position{x, y}, FreeCell)
continue
}
if x < r2.pos.X {
x++
d.SetCell(position{x, y}, FreeCell)
continue
}
if x > r2.pos.X {
x--
d.SetCell(position{x, y}, FreeCell)
continue
}
if y < r2.pos.Y {
y++
d.SetCell(position{x, y}, FreeCell)
continue
}
if y > r2.pos.Y {
y--
d.SetCell(position{x, y}, FreeCell)
continue
}
break
}
d.SetCell(r2.pos, FreeCell)
}
func (d *dungeon) Area(area []position, pos position, radius int) []position {
area = area[:0]
for x := pos.X - radius; x <= pos.X+radius; x++ {
for y := pos.Y - radius; y <= pos.Y+radius; y++ {
pos := position{x, y}
if pos.valid() {
area = append(area, pos)
}
}
}
return area
}
func (d *dungeon) ConnectRoomsShortestPath(r1, r2 room) {
var r1pos, r2pos position
r1pos.X = r1.pos.X + RandInt(r1.w)
if r1pos.X < r2.pos.X {
r1pos.X = r1.pos.X + r1.w - 1
}
r1pos.Y = r1.pos.Y + RandInt(r1.h)
if r1pos.Y < r2.pos.Y {
r1pos.Y = r1.pos.Y + r1.h - 1
}
r2pos.X = r2.pos.X + RandInt(r2.w)
if r2pos.X < r1.pos.X {
r2pos.X = r2.pos.X + r2.w - 1
}
r2pos.Y = r2.pos.Y + RandInt(r2.h)
if r2pos.Y < r1.pos.Y {
r2pos.Y = r2.pos.Y + r2.h - 1
}
mp := &dungeonPath{dungeon: d}
path, _, _ := AstarPath(mp, r1pos, r2pos)
for _, pos := range path {
d.SetCell(pos, FreeCell)
}
}
func (d *dungeon) ConnectIsolatedRoom(doorpos position) {
for i := 0; i < 200; i++ {
pos := d.FreeCell()
dp := &dungeonPath{dungeon: d, wcost: unreachable}
path, _, _ := AstarPath(dp, pos, doorpos)
wall := false
for _, pos := range path {
if d.Cell(pos).T == WallCell {
wall = true
break
}
}
if !wall {
continue
}
for _, pos := range path {
d.SetCell(pos, FreeCell)
}
break
}
}
func (d *dungeon) DigRoom(r room) {
for i := r.pos.X; i < r.pos.X+r.w; i++ {
for j := r.pos.Y; j < r.pos.Y+r.h; j++ {
rpos := position{i, j}
if rpos.valid() {
d.SetCell(rpos, FreeCell)
}
}
}
}
func (d *dungeon) PutCols(r room) {
for i := r.pos.X + 1; i < r.pos.X+r.w-1; i += 2 {
for j := r.pos.Y + 1; j < r.pos.Y+r.h-1; j += 2 {
rpos := position{i, j}
if rpos.valid() {
d.SetCell(rpos, WallCell)
}
}
}
}
func (d *dungeon) PutDiagCols(r room) {
n := RandInt(2)
for i := r.pos.X + 1; i < r.pos.X+r.w-1; i++ {
m := n
for j := r.pos.Y + 1; j < r.pos.Y+r.h-1; j++ {
rpos := position{i, j}
if rpos.valid() && m%2 == 0 {
d.SetCell(rpos, WallCell)
}
m++
}
n++
}
}
func (d *dungeon) IsAreaFree(pos position, h, w int) bool {
for i := pos.X; i < pos.X+w; i++ {
for j := pos.Y; j < pos.Y+h; j++ {
rpos := position{i, j}
if !rpos.valid() || d.Cell(rpos).T != FreeCell {
return false
}
}
}
return true
}
func (d *dungeon) RoomDigCanditate(pos position, h, w int) (ret bool) {
for i := pos.X; i < pos.X+w; i++ {
for j := pos.Y; j < pos.Y+h; j++ {
rpos := position{i, j}
if !rpos.valid() {
return false
}
if d.Cell(rpos).T == FreeCell {
ret = true
}
}
}
return ret
}
func (d *dungeon) IsolatedRoomDigCanditate(pos position, h, w int) (ret bool) {
for i := pos.X; i < pos.X+w; i++ {
for j := pos.Y; j < pos.Y+h; j++ {
rpos := position{i, j}
if !rpos.valid() {
return false
}
if d.Cell(rpos).T == FreeCell {
return false
}
}
}
return true
}
func (d *dungeon) DigArea(pos position, h, w int) {
for i := pos.X; i < pos.X+w; i++ {
for j := pos.Y; j < pos.Y+h; j++ {
rpos := position{i, j}
if !rpos.valid() {
continue
}
d.SetCell(rpos, FreeCell)
}
}
}
func (d *dungeon) BlockArea(pos position, h, w int) {
// not used now
for i := pos.X; i < pos.X+w; i++ {
for j := pos.Y; j < pos.Y+h; j++ {
rpos := position{i, j}
if !rpos.valid() {
continue
}
d.SetCell(rpos, WallCell)
}
}
}
func (d *dungeon) BuildRoom(pos position, w, h int, outside bool) map[position]bool {
spos := position{pos.X - 1, pos.Y - 1}
if outside && !d.IsAreaFree(spos, h+2, w+2) {
return nil
}
for i := pos.X; i < pos.X+w; i++ {
d.SetCell(position{i, pos.Y}, WallCell)
d.SetCell(position{i, pos.Y + h - 1}, WallCell)
}
for i := pos.Y; i < pos.Y+h; i++ {
d.SetCell(position{pos.X, i}, WallCell)
d.SetCell(position{pos.X + w - 1, i}, WallCell)
}
if RandInt(2) == 0 || !outside {
n := RandInt(2)
for x := pos.X + 1; x < pos.X+w-1; x++ {
m := n
for y := pos.Y + 1; y < pos.Y+h-1; y++ {
if m%2 == 0 {
d.SetCell(position{x, y}, WallCell)
}
m++
}
n++
}
} else {
n := RandInt(2)
m := RandInt(2)
//if n == 0 && m == 0 {
//// round room
//d.SetCell(pos, FreeCell)
//d.SetCell(position{pos.X, pos.Y + h - 1}, FreeCell)
//d.SetCell(position{pos.X + w - 1, pos.Y}, FreeCell)
//d.SetCell(position{pos.X + w - 1, pos.Y + h - 1}, FreeCell)
//}
for x := pos.X + 1 + m; x < pos.X+w-1; x += 2 {
for y := pos.Y + 1 + n; y < pos.Y+h-1; y += 2 {
d.SetCell(position{x, y}, WallCell)
}
}
}
area := make([]position, 9)
if outside {
for _, p := range [4]position{pos, {pos.X, pos.Y + h - 1}, {pos.X + w - 1, pos.Y}, {pos.X + w - 1, pos.Y + h - 1}} {
if d.WallAreaCount(area, p, 1) == 4 {
d.SetCell(p, FreeCell)
}
}
}
doorsc := [4]position{
position{pos.X + w/2, pos.Y},
position{pos.X + w/2, pos.Y + h - 1},
position{pos.X, pos.Y + h/2},
position{pos.X + w - 1, pos.Y + h/2},
}
doors := make(map[position]bool)
for i := 0; i < 3+RandInt(2); i++ {
dpos := doorsc[RandInt(4)]
doors[dpos] = true
d.SetCell(dpos, FreeCell)
}
return doors
}
func (d *dungeon) BuildSomeRoom(w, h int) map[position]bool {
for i := 0; i < 200; i++ {
pos := d.FreeCell()
doors := d.BuildRoom(pos, w, h, true)
if doors != nil {
return doors
}
}
return nil
}
func (d *dungeon) DigSomeRoom(w, h int) map[position]bool {
for i := 0; i < 200; i++ {
pos := d.FreeCell()
dpos := position{pos.X - 1, pos.Y - 1}
if !d.RoomDigCanditate(dpos, h+2, w+2) {
continue
}
d.DigArea(dpos, h+2, w+2)
doors := d.BuildRoom(pos, w, h, true)
if doors != nil {
return doors
}
}
return nil
}
func (d *dungeon) DigIsolatedRoom(w, h int) map[position]bool {
i := RandInt(DungeonNCells)
for j := 0; j < DungeonNCells; j++ {
i = (i + 1) % DungeonNCells
pos := idxtopos(i)
if d.Cells[i].T == FreeCell {
continue
}
dpos := position{pos.X - 1, pos.Y - 1}
if !d.IsolatedRoomDigCanditate(dpos, h+2, w+2) {
continue
}
d.DigArea(pos, h, w)
doors := d.BuildRoom(pos, w, h, false)
if doors != nil {
return doors
}
}
return nil
}
func (d *dungeon) ResizeRoom(r room) room {
if DungeonWidth-r.pos.X < r.w {
r.w = DungeonWidth - r.pos.X
}
if DungeonHeight-r.pos.Y < r.h {
r.h = DungeonHeight - r.pos.Y
}
return r
}
func (g *game) GenRuinsMap(h, w int) {
d := &dungeon{}
d.Cells = make([]cell, h*w)
rooms := []room{}
for i := 0; i < 43; i++ {
var ro room
count := 100
for count > 0 {
count--
ro = room{
pos: position{RandInt(w - 1), RandInt(h - 1)},
w: 3 + RandInt(5),
h: 2 + RandInt(3)}
ro = d.ResizeRoom(ro)
if !intersectsRoom(rooms, ro) {
break
}
}
d.DigRoom(ro)
if RandInt(60) == 0 {
if RandInt(2) == 0 {
d.PutCols(ro)
} else {
d.PutDiagCols(ro)
}
}
if len(rooms) > 0 {
r := RandInt(100)
if r > 75 {
d.connectRooms(nearRoom(rooms, ro), ro)
} else if r > 25 {
d.ConnectRoomsShortestPath(nearRoom(rooms, ro), ro)
} else {
d.connectRoomsDiagonally(nearRoom(rooms, ro), ro)
}
}
rooms = append(rooms, ro)
}
doors := d.DigSomeRooms(5)
g.Dungeon = d
g.Fungus = make(map[position]vegetation)
g.DigFungus(1 + RandInt(2))
g.PutDoors(30)
g.PutDoorsList(doors, 20)
}
func (g *game) DigFungus(n int) {
d := g.Dungeon
count := 0
fungus := g.Foliage(DungeonHeight, DungeonWidth)
loop:
for i := 0; i < 100; i++ {
if count > 100 {
break loop
}
if n <= 0 {
break
}
pos := d.FreeCell()
if _, ok := fungus[pos]; ok {
continue
}
conn, count := d.Connected(pos, func(npos position) bool {
_, ok := fungus[npos]
//return ok && d.IsFreeCell(npos)
return ok
})
if count < 3 {
continue
}
if len(conn) > 150 {
continue
}
for cpos := range conn {
d.SetCell(cpos, FreeCell)
g.Fungus[cpos] = foliage
count++
}
n--
}
}
type roomSlice []room
func (rs roomSlice) Len() int { return len(rs) }
func (rs roomSlice) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
func (rs roomSlice) Less(i, j int) bool {
return rs[i].pos.Y < rs[j].pos.Y || rs[i].pos.Y == rs[j].pos.Y && rs[i].pos.X < rs[j].pos.X
}
func (g *game) GenRoomMap(h, w int) {
d := &dungeon{}
d.Cells = make([]cell, h*w)
rooms := []room{}
cols := 0
for i := 0; i < 35; i++ {
var ro room
count := 100
for count > 0 {
count--
ro = room{
pos: position{RandInt(w - 1), RandInt(h - 1)},
w: 5 + RandInt(4),
h: 3 + RandInt(3)}
ro = d.ResizeRoom(ro)
if !intersectsRoom(rooms, ro) {
break
}
}
d.DigRoom(ro)
if RandInt(10+15*cols) == 0 {
if RandInt(2) == 0 {
d.PutCols(ro)
} else {
d.PutDiagCols(ro)
}
cols++
}
rooms = append(rooms, ro)
}
sort.Sort(roomSlice(rooms))
for i, ro := range rooms {
if i == 0 {
continue
}
r := RandInt(100)
if r > 50 {
d.connectRooms(nearestRoom(rooms[:i], ro), ro)
} else if r > 25 {
d.ConnectRoomsShortestPath(nearRoom(rooms[:i], ro), ro)
} else {
d.connectRoomsDiagonally(nearestRoom(rooms[:i], ro), ro)
}
}
g.Dungeon = d
doors := d.DigSomeRooms(5)
g.PutDoors(90)
g.PutDoorsList(doors, 10)
}
func (g *game) PutDoorsList(doors map[position]bool, threshold int) {
for pos := range doors {
if g.DoorCandidate(pos) && RandInt(100) > threshold {
g.Doors[pos] = true
if _, ok := g.Fungus[pos]; ok {
delete(g.Fungus, pos)
}
}
}
}
func (d *dungeon) FreeCell() position {
count := 0
for {
count++
if count > 1000 {
panic("FreeCell")
}
x := RandInt(DungeonWidth)
y := RandInt(DungeonHeight)
pos := position{x, y}
c := d.Cell(pos)
if c.T == FreeCell {
return pos
}
}
}
func (d *dungeon) WallCell() position {
count := 0
for {
count++
if count > 1000 {
panic("WallCell")
}
x := RandInt(DungeonWidth)
y := RandInt(DungeonHeight)
pos := position{x, y}
c := d.Cell(pos)
if c.T == WallCell {
return pos
}
}
}
func (g *game) GenCaveMap(h, w int) {
d := &dungeon{}
d.Cells = make([]cell, h*w)
pos := position{40, 10}
max := 21 * 42
d.SetCell(pos, FreeCell)
cells := 1
notValid := 0
lastValid := pos
diag := RandInt(4) == 0
for cells < max {
npos := pos.RandomNeighbor(diag)
if !pos.valid() && npos.valid() && d.Cell(npos).T == WallCell {
pos = lastValid
continue
}
pos = npos
if pos.valid() {
if d.Cell(pos).T != FreeCell {
d.SetCell(pos, FreeCell)
cells++
}
lastValid = pos
} else {
notValid++
}
if notValid > 200 {
notValid = 0
pos = lastValid
}
}
cells = 1
max = DungeonHeight * 1
digs := 0
i := 0
block := make([]position, 0, 64)
loop:
for cells < max {
i++
if digs > 3 {
break
}
if i > 1000 {
break
}
diag = RandInt(2) == 0
block = d.DigBlock(block, diag)
if len(block) == 0 {
continue loop
}
if len(block) < 4 || len(block) > 10 {
continue loop
}
for _, pos := range block {
d.SetCell(pos, FreeCell)
cells++
}
digs++
}
doors := make(map[position]bool)
rooms := 0
if RandInt(4) > 0 {
w, h := GenCaveRoomSize()
rooms++
for pos := range d.BuildSomeRoom(w, h) {
doors[pos] = true
}
if RandInt(7) == 0 {
rooms++
w, h := GenCaveRoomSize()
for pos := range d.BuildSomeRoom(w, h) {
doors[pos] = true
}
}
}
if RandInt(1+rooms) == 0 {
w, h := GenLittleRoomSize()
i := 0
for pos := range d.DigIsolatedRoom(w, h) {
doors[pos] = true
if i == 0 {
d.ConnectIsolatedRoom(pos)
}
i++
}
}
g.Dungeon = d
g.Fungus = g.Foliage(DungeonHeight, DungeonWidth)
g.PutDoors(5)
for pos := range doors {
if g.DoorCandidate(pos) && RandInt(100) > 20 {
g.Doors[pos] = true
if _, ok := g.Fungus[pos]; ok {
delete(g.Fungus, pos)
}
}
}
}
func GenCaveRoomSize() (int, int) {
return 7 + 2*RandInt(2), 5 + 2*RandInt(2)
}
func GenLittleRoomSize() (int, int) {
return 7, 5
}
func (d *dungeon) HasFreeNeighbor(pos position) bool {
neighbors := pos.ValidNeighbors()
for _, pos := range neighbors {
if d.Cell(pos).T == FreeCell {
return true
}
}
return false
}
func (g *game) HasFreeExploredNeighbor(pos position) bool {
d := g.Dungeon
neighbors := pos.ValidNeighbors()
for _, pos := range neighbors {
c := d.Cell(pos)
if c.T == FreeCell && c.Explored && !g.WrongWall[pos] {
return true
}
}
return false
}
func (d *dungeon) DigBlock(block []position, diag bool) []position {
pos := d.WallCell()
block = block[:0]
for {
block = append(block, pos)
if d.HasFreeNeighbor(pos) {
break
}
pos = pos.RandomNeighbor(diag)
if !pos.valid() {
block = block[:0]
pos = d.WallCell()
continue
}
if !pos.valid() {
return nil
}
}
return block
}
func (g *game) GenCaveMapTree(h, w int) {
d := &dungeon{}
d.Cells = make([]cell, h*w)
center := position{40, 10}
d.SetCell(center, FreeCell)
d.SetCell(center.E(), FreeCell)
d.SetCell(center.NE(), FreeCell)
d.SetCell(center.S(), FreeCell)
d.SetCell(center.SE(), FreeCell)
d.SetCell(center.N(), FreeCell)
d.SetCell(center.NW(), FreeCell)
d.SetCell(center.W(), FreeCell)
d.SetCell(center.SW(), FreeCell)
max := 21 * 23
cells := 1
diag := RandInt(2) == 0
block := make([]position, 0, 64)
loop:
for cells < max {
block = d.DigBlock(block, diag)
if len(block) == 0 {
continue loop
}
for _, pos := range block {
if d.Cell(pos).T != FreeCell {
d.SetCell(pos, FreeCell)
cells++
}
}
}
doors := d.DigSomeRooms(5)
g.Dungeon = d
g.Fungus = make(map[position]vegetation)
g.DigFungus(1 + RandInt(2))
g.PutDoors(5)
g.PutDoorsList(doors, 20)
}
func (d *dungeon) DigSomeRooms(chances int) map[position]bool {
doors := make(map[position]bool)
if RandInt(chances) > 0 {
w, h := GenCaveRoomSize()
for pos := range d.DigSomeRoom(w, h) {
doors[pos] = true
}
if RandInt(3) == 0 {
w, h := GenCaveRoomSize()
for pos := range d.DigSomeRoom(w, h) {
doors[pos] = true
}
}
}
return doors
}
func (d *dungeon) WallAreaCount(area []position, pos position, radius int) int {
area = d.Area(area, pos, radius)
count := 0
for _, npos := range area {
if d.Cell(npos).T == WallCell {
count++
}
}
switch radius {
case 1:
count += 9 - len(area)
case 2:
count += 25 - len(area)
}
return count
}
func (d *dungeon) Connected(pos position, nf func(position) bool) (map[position]bool, int) {
conn := map[position]bool{}
stack := []position{pos}
count := 0
conn[pos] = true
nb := make([]position, 0, 8)
for len(stack) > 0 {
pos = stack[len(stack)-1]
stack = stack[:len(stack)-1]
count++
nb = pos.Neighbors(nb, nf)
for _, npos := range nb {
if !conn[npos] {
conn[npos] = true
stack = append(stack, npos)
}
}
}
return conn, count
}
func (d *dungeon) connex() bool {
pos := d.FreeCell()
conn, _ := d.Connected(pos, d.IsFreeCell)
for i, c := range d.Cells {
if c.T == FreeCell && !conn[idxtopos(i)] {
return false
}
}
return true
}