-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
2091 lines (1782 loc) · 76.8 KB
/
maze.py
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
#-*- coding:utf-8 -*-
import os
import random
import pickle
from functools import reduce
#from multiprocessing import Process as Thread
from threading import Thread
from setting import Setting, MazeBase, MazeSetting
from cache import Config
from hero import Hero, HeroBase, HeroState
from tools import Tools, LoopException
from story import Story
class TreeNode:
def __init__(self, area, crack, special=False):
self.Area = area #节点包含的区域
self.Crack = crack
self.Cover = self.Area | self.Crack
self.Index = -1 #用于计算
self.Forward = {} #节点之前的节点
self.Backward = {} #节点之后的节点
self.Up = None #上一层的开始节点
self.Down = None #下一层的开始节点
self.UpPos = None
self.DownPos = None
self.Deep = -1 #深度,离楼梯间最短路径的最短距离
self.Special = special #特殊空间
self.Space = len(area) #空间大小
self.Door = 0
self.Key = {
MazeBase.Value.Color.yellow: 0,
MazeBase.Value.Color.blue: 0,
MazeBase.Value.Color.red: 0,
MazeBase.Value.Color.green: 0
}
self.AttackGem = {
MazeBase.Value.Gem.small: 0,
MazeBase.Value.Gem.big: 0,
MazeBase.Value.Gem.large: 0
}
self.DefenceGem = {
MazeBase.Value.Gem.small: 0,
MazeBase.Value.Gem.big: 0,
MazeBase.Value.Gem.large: 0
}
self.Sword = 0
self.Shield = 0
self.Potion = {
MazeBase.Value.Potion.red: 0,
MazeBase.Value.Potion.blue: 0,
MazeBase.Value.Potion.yellow: 0,
MazeBase.Value.Potion.green: 0
}
self.IsDoor = False
self.IsMonster = False
self.IsElite = False
self.IsBoss = False
self.DoorPos = None
self.MonsterPos = None
#英雄到达区域时获得的宝石属性总和,该值用来设置怪物
self.Attack = 0
self.Defence = 0
self.Monster = None
self.Damage = 0
self.Gold = 0
self.Experience = 0
#圣水,开始时放置,用来调整第一个节点无法放置足够药水的问题
self.HolyWater = 0
self.Montecarlo = 0
@property
def floor(self):
return list(self.Area)[0][0]
@property
def space(self):
space = len(self.Area)
if self.Down:
space -= 1
if self.Up:
space -= 1
if self.Door and self.Monster:
space -= 1
space -= sum(self.Key.values())
space -= sum(self.AttackGem.values())
space -= sum(self.DefenceGem.values())
space -= sum(self.Potion.values())
if self.HolyWater:
space -= 1
return space
@property
def start_floor(self):
return self.boss_floor - MazeSetting.base_floor + 1
@property
def boss_floor(self):
return int((self.floor - 1) / MazeSetting.base_floor + 1) * MazeSetting.base_floor
@property
def forbid(self):
return filter(lambda x: Pos.inside(x) and (not (Pos.beside(x) & self.Crack)), reduce(lambda x, y: x ^ y, map(lambda x: Pos.corner(x) - self.Cover, self.Crack)))
#可以给每个类型一个权值,通过计算优化权值
def montecarlo(self, maze, attack, defence, key, total_damage, min_damage):
if self.IsBoss:
return False
if self.IsDoor:
if key[self.Door] == 0:
return False
self.Damage = 0
self.Gold = 0
self.Experience = 0
if self.IsMonster:
self.Damage = maze.get_damage(attack, defence, self.Monster)
if self.Damage == float('inf'):
return False
if total_damage + self.Damage > min_damage:
return False
if self.Damage > MazeSetting.elite_max:
return False
monster = maze.get_monster(self.Monster)
self.Gold += monster['gold']
self.Experience += monster['experience']
montecarlo = 0
montecarlo += self.Damage
if self.Damage == 0:
montecarlo -= 500
elif self.Damage < 100:
montecarlo -= 200
for gem in MazeBase.Value.Gem.total:
if self.AttackGem[gem]:
montecarlo -= 400
montecarlo -= self.AttackGem[gem] * 200
montecarlo -= self.DefenceGem[gem] * 50
montecarlo += random.randint(-100, 100)
self.Montecarlo = montecarlo
return True
class ItemInfo:
path = 'item'
data = {}
@staticmethod
def load():
data = ItemInfo.data
for key, config in Config.config.items():
if 'hero' in key:
continue
if not config['path'].startswith(os.path.join('data', ItemInfo.path)):
continue
key_list = key.split('-', 1)
if len(key_list) != 2:
continue
key1, key2 = key_list
if key1 != 'item':
continue
if key2 not in data:
data[key2] = {}
#需要再添加对属性的改变
if 'name' in config:
data[key2]['name'] = config['name']
class MonsterInfo:
path = 'monster'
data_fluctuate = 10
data_range = 20
base = {'level': 50, 'health': 100, 'ismagic': 0}
data = {}
@staticmethod
def load():
data = MonsterInfo.data
for key, config in Config.config.items():
if 'hero' in key:
continue
if not config['path'].startswith(os.path.join('data', MonsterInfo.path)):
continue
key_list = key.split('-')
if len(key_list) != 2:
continue
key1, key2 = key_list
if key1 not in data:
data[key1] = {}
if key2 not in data[key1]:
data[key1][key2] = {}
if 'name' in config:
data[key1][key2]['name'] = config['name']
for name in MonsterInfo.base:
if name not in data[key1][key2]:
data[key1][key2][name] = MonsterInfo.base[name]
if name in config and config[name].isdigit():
data[key1][key2][name] = int(config[name])
class NpcInfo:
data = {}
@staticmethod
def load():
data = NpcInfo.data
for key, config in Config.config.items():
if 'hero' in key:
continue
key_list = key.split('-')
if len(key_list) != 2:
continue
key1, key2 = key_list
if key1 != 'npc':
continue
if key2 not in data:
data[key2] = {}
if 'name' in config:
data[key2]['name'] = config['name']
class Pos:
@staticmethod
def inside(pos):
_, x, y = pos
if 0 < x < MazeSetting.rows + 1:
if 0 < y < MazeSetting.cols + 1:
return True
return False
@staticmethod
def add(pos, move):
z1, x1, y1 = pos
x2, y2 = move
return (z1, x1 + x2, y1 + y2)
@staticmethod
def sub(pos1, pos2):
_, x1, y1 = pos1
_, x2, y2 = pos2
return (x1 - x2, y1 - y2)
@staticmethod
def beside(pos):
z, x, y = pos
return {(z, x - 1, y), (z, x + 1, y), (z, x, y - 1), (z, x, y + 1)}
@staticmethod
def corner(pos):
z, x, y = pos
return {(z, x - 1, y - 1), (z, x - 1, y + 1), (z, x + 1, y - 1), (z, x + 1, y + 1)}
@staticmethod
def around(pos):
z, x, y = pos
return {(z, x - 1, y - 1), (z, x - 1, y), (z, x - 1, y + 1), (z, x, y - 1), (z, x, y + 1), (z, x + 1, y - 1), (z, x + 1, y), (z, x + 1, y + 1)}
@staticmethod
def inline(pos_list):
z, x, y = map(lambda x: len(set(x)), zip(*pos_list))
if z == 1 and (x == 1 or y == 1):
return True
return False
class Maze:
def __init__(self):
self.maze = {}
self.maze_map = {} #每一层不同点的分类集合
self.maze_info = {} #每一层的信息,node, stair等
self.monster = {}
self.npc = {}
self.hero = Hero(self)
self.herobase = HeroBase()
self.herostate = HeroState(self.herobase)
self.story = Story(self)
self.wall = MazeBase.Value.Wall.earth
self.weapon = MazeBase.Value.Weapon.iron
MazeBase.Value.Gem.large = self.weapon
MazeBase.Value.Gem.total = (MazeBase.Value.Gem.small, MazeBase.Value.Gem.big, MazeBase.Value.Gem.large)
ItemInfo.load()
MonsterInfo.load()
NpcInfo.load()
def init(self, floor):
for key in list(self.maze.keys()):
if key < floor - MazeSetting.save_floor:
del self.maze[key]
del self.maze_map[key]
del self.maze_info[key]
self.maze[floor] = [[[0, 0] for j in range(MazeSetting.cols + 2)] for i in range(MazeSetting.rows + 2)]
self.maze_map[floor] = {MazeBase.Type.Static.ground: set()}
self.maze_info[floor] = {}
for i in range(MazeSetting.rows + 2):
for j in range(MazeSetting.cols + 2):
if i in (0, MazeSetting.rows + 1) or j in (0, MazeSetting.cols + 1):
self.maze[floor][i][j][0] = MazeBase.Type.Static.wall
self.maze[floor][i][j][1] = self.wall
else:
self.maze[floor][i][j][0] = MazeBase.Type.Static.ground
self.maze_map[floor][MazeBase.Type.Static.ground].add((floor, i, j))
if self.is_boss_floor(floor):
self.monster = {}
if self.is_initial_floor(floor):
middle = int((MazeSetting.cols + 1) / 2)
pos = (floor, MazeSetting.rows, middle)
self.set_type(pos, MazeBase.Type.Static.init)
self.set_value(pos, 0)
self.maze_info[floor]['init'] = pos
def outside(self, pos):
_, x, y = pos
if x < 1 or x > MazeSetting.rows:
return True
if y < 1 or y > MazeSetting.cols:
return True
return False
#大小为5时出现错误,可能是由于没有楼梯导致的
def get_type(self, pos):
z, x, y = pos
return self.maze[z][x][y][0]
def get_value(self, pos):
z, x, y = pos
return self.maze[z][x][y][1]
def set_type(self, pos, value):
z, x, y = pos
if self.outside(pos):
return
if z in self.maze_map:
type = self.maze[z][x][y][0]
self.maze_map[z].setdefault(type, set())
self.maze_map[z].setdefault(value, set())
self.maze_map[z][type].remove(pos)
self.maze_map[z][value].add(pos)
self.maze[z][x][y][0] = value
def set_value(self, pos, value):
z, x, y = pos
self.maze[z][x][y][1] = value
def get_data(self, pos):
z, x, y = pos
return self.maze[z][x][y]
def set_data(self, pos, type, value):
self.set_type(pos, type)
self.set_value(pos, value)
def get_beside(self, pos, type):
return {(z, x, y) for z, x, y in Pos.beside(pos) if self.maze[z][x][y][0] == type}
def get_beside_except(self, pos, type):
return {(z, x, y) for z, x, y in Pos.beside(pos) if self.maze[z][x][y][0] != type}
def get_corner(self, pos, type):
return {(z, x, y) for z, x, y in Pos.corner(pos) if self.maze[z][x][y][0] == type}
def get_around(self, pos, type):
return {(z, x, y) for z, x, y in Pos.around(pos) if self.maze[z][x][y][0] == type}
def get_extend(self, pos, type):
extend = set()
for beside in self.get_beside(pos, type):
move = Pos.sub(beside, pos)
next = beside
while self.get_type(next) == type:
beside = next
next = Pos.add(beside, move)
extend.add(beside)
return extend
#在floor层的type类型的区域中寻找符合func要求的点
def find_pos(self, floor, type, func=None):
if type not in self.maze_map[floor]:
return set()
if func:
return {pos for pos in self.maze_map[floor][type] if func(pos)}
else:
return set(self.maze_map[floor][type])
def is_pure(self, pos):
if len(self.get_beside(pos, MazeBase.Type.Static.wall)) != 1:
return False
_, x, y = zip(*self.get_around(pos, MazeBase.Type.Static.wall))
if len(set(x)) != 1 and len(set(y)) != 1:
return False
return True
def get_pure(self, floor):
#如果需要提高速度,每次放置wall时改变该值
ground = self.maze_map[floor][MazeBase.Type.Static.ground] - self.maze_info[floor]['special']
return {pos for pos in ground if self.is_pure(pos)}
def is_wall(self, wall):
for pos in wall[1:-1]:
if self.get_beside(pos, MazeBase.Type.Static.wall):
return False
return True
def get_wall(self, pos):
wall = []
move = Pos.sub(pos, self.get_beside(pos, MazeBase.Type.Static.wall).pop())
while self.get_type(pos) == MazeBase.Type.Static.ground:
wall.append(pos)
pos = Pos.add(pos, move)
return wall
def is_rect(self, pos, rect1, rect2, row, col):
floor, x, y = pos
if x + row > MazeSetting.rows + 2 or y + col > MazeSetting.cols + 2:
return False
for i in range(row):
for j in range(col):
if self.get_type((floor, x + i, y + j)) != rect1[i][j]:
return False
if rect2:
for i in range(row):
for j in range(col):
if rect2[i][j]:
type, value = rect2[i][j]
if type:
self.set_type((floor, x + i, y + j), type)
if value:
self.set_value((floor, x + i, y + j), value)
return True
#查找矩形,设置rect2的话则用rect2替换找到的矩形
def find_rect(self, floor, rect1, rect2=None):
_rect1 = list(zip(*rect1))
_rect2 = list(zip(*rect2) if rect2 else None)
row = len(rect1)
col = len(_rect1)
pos_list = [(floor, i, j) for i in range(0, MazeSetting.rows + 2) for j in range(0, MazeSetting.cols + 2)]
random.shuffle(pos_list)
for pos in pos_list:
self.is_rect(pos, rect1, rect2, row, col)
self.is_rect(pos, _rect1, _rect2, col, row)
#获取pos的类型area, road, end
def pos_type(self, pos):
beside = self.get_beside(pos, MazeBase.Type.Static.ground)
if len(beside) >= 3:
return MazeBase.NodeType.area_normal
if len(beside) == 1:
return MazeBase.NodeType.road_corner
if len(beside) == 2:
(z1, x1, y1), (z2, x2, y2) = beside
if x1 == x2 or y1 == y2:
return MazeBase.NodeType.road_normal
if self.get_type((z2, x2, y1) if pos == (z1, x1, y2) else (z1, x1, y2)) == MazeBase.Type.Static.wall:
return MazeBase.NodeType.road_normal
return MazeBase.NodeType.area_corner
return MazeBase.NodeType.none
#获取一片区域
def get_area(self, pos):
area = set()
beside = set([pos])
while beside:
pos = beside.pop()
area.add(pos)
beside = beside | self.get_beside(pos, MazeBase.Type.Static.ground) - area
return area
def is_crack(self, pos):
beside = self.get_beside(pos, MazeBase.Type.Static.ground)
if len(beside) != 2:
return False
(_, x1, y1), (_, x2, y2) = beside
if x1 != x2 and y1 != y2:
return False
return True
def get_crack(self, floor):
return self.find_pos(floor, MazeBase.Type.Static.wall, self.is_crack)
def node_info(self, floor):
self.maze_info[floor]['node'] = set()
ground = self.find_pos(floor, MazeBase.Type.Static.ground)
crack_list = self.get_crack(floor) #可打通的墙
while ground:
pos = None
around = []
area = self.get_area(ground.pop())
for pos in area:
around.append(self.get_beside(pos, MazeBase.Type.Static.wall))
crack = reduce(lambda x, y: x | y, around) & crack_list
node = TreeNode(area=area, crack=crack, special=pos in self.maze_info[floor]['special'])
self.maze_info[floor]['node'].add(node)
ground -= area
def find_node(self, pos):
z, _, _ = pos
for node in self.maze_info[z]['node']:
if pos in node.Area:
return node
def merge_rect(self, floor):
wall = MazeBase.Type.Static.wall
ground = MazeBase.Type.Static.ground
rect1 = [[wall, wall, wall, wall],
[wall, ground, ground, wall],
[wall, wall, wall, wall],
[wall, ground, ground, wall],
[wall, wall, wall, wall]]
rect2 = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, (ground, 0), (ground, 0), 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
self.find_rect(floor, rect1, rect2)
def is_initial_floor(self, floor):
if floor:
return False
return True
def is_boss_floor(self, floor):
if floor <= 0:
return False
if not floor % MazeSetting.base_floor:
return True
return False
def get_rect(self, pos, width, height):
z, x, y = pos
rect = set()
for i in range(x, x + width):
for j in range(y, y + height):
rect.add((z, i, j))
return rect
def get_rect_crack(self, pos, width, height):
z, x, y = pos
return self.get_rect((z, x - 1, y - 1), width + 2, height + 2) - self.get_rect(pos, width, height)
#特殊区域,一块较大的矩形
def create_special(self, floor):
self.maze_info[floor]['special'] = set()
if self.is_initial_floor(floor):
pass
elif self.is_boss_floor(floor): #boss层只有一个特殊区域
width = MazeSetting.rows // 3
if width < 1:
width = 1
height = MazeSetting.cols // 3
if height < 1:
height = 1
while True:
x = random.randint(1, MazeSetting.rows - width + 1)
if x == 2 or x == MazeSetting.rows - width:
continue
break
while True:
y = random.randint(1, MazeSetting.cols - height + 1)
if y == 2 or y == MazeSetting.cols - height:
continue
break
pos_list = [(floor, x, y)]
for pos in pos_list:
area = self.get_rect(pos, width, height)
crack = self.get_rect_crack(pos, width, height)
for pos in crack:
self.set_type(pos, MazeBase.Type.Static.wall)
self.set_value(pos, self.wall)
self.maze_info[floor]['special'] |= area
def create_wall(self, floor):
pure = True
while pure:
pure = self.get_pure(floor)
while pure:
pos = random.choice(tuple(pure))
wall = self.get_wall(pos)
if self.is_wall(wall):
for pos in wall:
self.set_type(pos, MazeBase.Type.Static.wall)
self.set_value(pos, self.wall)
break
else:
pure -= set(wall)
self.merge_rect(floor)
self.node_info(floor)
def crack_wall(self, floor):
#special区域只开一个口
#crack_list = self.get_crack(floor) #可打通的墙
next_node = list(self.maze_info[floor]['node'])
crack_set = set() #已设置墙和未设置墙区域之间可打通的墙
special_set = set()
while next_node:
if not crack_set:
node = random.choice(list(filter(lambda x: not x.Special, next_node)))
else:
crack_pos = random.choice(list(crack_set - special_set))
#该区域和上一个区域之间的墙
self.set_type(crack_pos, MazeBase.Type.Static.door)
#self.set_value(crack_pos, MazeBase.Value.Color.yellow)
for node in next_node:
if crack_pos in node.Crack:
break
if node.Special:
special_set |= node.Crack
crack_set = (crack_set | node.Crack) - (crack_set & node.Crack)
next_node.remove(node)
def get_stair_pos(self, floor):
pos_set = set()
if self.is_initial_floor(floor):
return pos_set
for node in self.maze_info[floor]['node']:
if node.Special:
continue
for pos in node.Area:
if self.pos_type(pos) == MazeBase.NodeType.road_normal:
continue
if self.get_beside(pos, MazeBase.Type.Static.door):
continue
pos_set.add((pos[1], pos[2]))
#去除已设置的楼梯空间
stair = self.maze_info[floor]['stair']
pos_set -= set([(pos[1], pos[2]) for pos in stair[MazeBase.Value.Stair.down]])
pos_set -= set([(pos[1], pos[2]) for pos in stair[MazeBase.Value.Stair.up]])
return pos_set
def create_stair(self, floor):
self.maze_info[floor]['stair'] = {MazeBase.Value.Stair.up: set(), MazeBase.Value.Stair.down: set()}
pos_set_down = self.get_stair_pos(floor)
pos_set_up = self.get_stair_pos(floor - 1)
pos_set = pos_set_down & pos_set_up #两层相同的位置
if pos_set:
pos_list = list(pos_set)
random.shuffle(pos_list)
pos = pos_list.pop()
pos_down = (floor, pos[0], pos[1])
pos_up = (floor - 1, pos[0], pos[1])
else:
if not self.is_initial_floor(floor - 1):
print('No same pos, use different stair.')
pos_list_down = list(pos_set_down)
random.shuffle(pos_list_down)
if pos_list_down:
pos = pos_list_down.pop()
pos_down = (floor, pos[0], pos[1])
else:
pos_down = None
pos_list_up = list(pos_set_up)
random.shuffle(pos_list_up)
if pos_list_up:
pos = pos_list_up.pop()
pos_up = (floor - 1, pos[0], pos[1])
else:
pos_up = None
if pos_down:
self.maze_info[floor]['stair'][MazeBase.Value.Stair.down].add(pos_down)
if pos_up:
self.maze_info[floor - 1]['stair'][MazeBase.Value.Stair.up].add(pos_up)
def create_tree(self, floor):
self.maze_info[floor]['tree'] = set()
for down in self.maze_info[floor]['stair'][MazeBase.Value.Stair.down]: #只有一个
node = self.find_node(down)
self.maze_info[floor]['tree'].add(node) #每一层起点
node_list = [node]
door_list = self.find_pos(floor, MazeBase.Type.Static.door)
while node_list:
node = node_list.pop()
if self.is_boss_floor(node.floor) and node.Special:
node.IsBoss = True
for door in door_list & node.Crack:
beside_pos = (self.get_beside(door, MazeBase.Type.Static.ground) - node.Area).pop()
beside_node = self.find_node(beside_pos)
node.Forward[door] = beside_node
beside_node.Backward[door] = node
node_list.append(beside_node)
door_list -= node.Crack
if self.is_initial_floor(floor - 1) or self.is_boss_floor(floor - 1):
return
pos_up = set(self.maze_info[floor - 1]['stair'][MazeBase.Value.Stair.up]).pop()
node_up = self.find_node(pos_up)
pos_down = set(self.maze_info[floor]['stair'][MazeBase.Value.Stair.down]).pop()
node_down = self.find_node(pos_down)
node_up.Up = node_down
node_up.UpPos = pos_up
node_down.Down = node_up
node_down.DownPos = pos_down
def adjust_corner(self, floor):
corner = set()
for door in self.find_pos(floor, MazeBase.Type.Static.door):
for beside in self.get_beside(door, MazeBase.Type.Static.ground):
if self.pos_type(beside) == MazeBase.NodeType.road_corner:
corner.add(beside)
#print(len(corner), len(self.find_pos(floor, MazeBase.Type.Static.door)))
def adjust_trap(self, floor):
wall = MazeBase.Type.Static.wall
ground = MazeBase.Type.Static.ground
rect = [[wall, wall, ground, wall, wall],
[wall, ground, ground, ground, wall],
[wall, wall, ground, wall, wall]]
rect = rect
def adjust_crack(self, floor):
for node in self.ergodic(floor, 1):
for _, forward in node.Forward.items():
crack = node.Crack & forward.Crack
if len(crack) <= 1:
continue
if not Pos.inline(crack):
continue
if not len(crack) % 2:
continue
#print(crack)
def adjust(self, floor):
self.adjust_corner(floor)
self.adjust_trap(floor)
self.adjust_crack(floor)
#获取楼梯之间的最短路径
def get_way(self, floor):
node_list = [set(self.maze_info[floor]['tree']).pop()]
index = 0
while node_list:
node_list_next = []
for node in node_list:
node.Index = index
for node in node_list:
if node.Up or node.IsBoss:
break
node_list_next += [n for n in node.Forward.values() if n.Index < 0]
if node.Up or node.IsBoss:
break
node_list = node_list_next
index += 1
node_list = [node]
while True:
backward = node.Backward.values()
index -= 1
for node in backward:
if node.Index == index:
node_list.append(node)
break
else:
break
return node_list[::-1]
#获取深度
def get_deep(self, node_list):
deep = 0
while node_list:
node_list_next = []
for node in node_list:
node.Deep = deep
for node in node_list:
node_list_next += [n for n in node.Forward.values() if n.Deep < 0]
node_list = node_list_next
deep += 1
def process(self, floor):
node_list = self.get_way(floor)
self.get_deep(node_list)
#遍历树,简单区域(没有怪物,门内有相同钥匙)可以合并
def ergodic_yield(self, floor, across=1):
node_list = [set(self.maze_info[floor]['tree']).pop()]
boss_node = None #boss区域放在最后
while node_list:
node = random.choice(node_list)
node_list = list(set(node.Forward.values()) | set(node_list))
if node.Up and floor + across > node.floor + 1:
node_list.append(node.Up)
node_list.remove(node)
if node.IsBoss:
boss_node = node
else:
yield node
if boss_node:
yield boss_node
def ergodic(self, floor, across=1):
ergodic_list = []
for index, node in enumerate(self.ergodic_yield(floor, across)):
node.Index = index
ergodic_list.append(node)
return ergodic_list
def set_stair(self, floor):
for up in self.maze_info[floor]['stair'][MazeBase.Value.Stair.up]:
self.set_type(up, MazeBase.Type.Static.stair)
self.set_value(up, MazeBase.Value.Stair.up)
for down in self.maze_info[floor]['stair'][MazeBase.Value.Stair.down]:
self.set_type(down, MazeBase.Type.Static.stair)
self.set_value(down, MazeBase.Value.Stair.down)
#重新确定space大小,需要排除掉楼梯的空间
def set_space(self, node_list):
for node in node_list:
space = 0
for pos in node.Area:
if self.get_type(pos) == MazeBase.Type.Static.stair:
continue
space += 1
node.Space = space
#空间小于2不放置key,空间越小放置key概率越小
def __set_door(self, node_list):
key_choice = {
MazeBase.Value.Color.yellow: 60,
MazeBase.Value.Color.blue: 20,
MazeBase.Value.Color.red: 5,
MazeBase.Value.Color.green: 1
}
key_choice_normal = {
MazeBase.Value.Color.yellow: 2,
MazeBase.Value.Color.blue: 1
}
key_choice_special = {
MazeBase.Value.Color.red: 2,
MazeBase.Value.Color.green: 1
}
#当前key的数量
key_number = {
MazeBase.Value.Color.yellow: 0,
MazeBase.Value.Color.blue: 0,
MazeBase.Value.Color.red: 0,
MazeBase.Value.Color.green: 0
}
door_number = {
MazeBase.Value.Color.yellow: 0,
MazeBase.Value.Color.blue: 0,
MazeBase.Value.Color.red: 0,
MazeBase.Value.Color.green: 0
}
#前两个节点不设置门,第一个节点可能需要放置圣水,第一个门的钥匙需要放在第二个节点,不然可能导致没有空间放置圣水
#红绿钥匙也可能在大片区域的入口(Deep == 1)
length = len(node_list[-2:1:-1])
door_total = random.randint(int(0.5 * length), int(0.7 * length))
special_node = set()
for node in node_list[-2:1:-1]:
if node.Space < 2:
continue
if node.Deep == 0: #主路径上
continue
if node.Forward: #不是最终节点
continue
keys = []
door = None
is_yellows = False
rand = random.random()
if node.Deep > 3:
total = key_number[MazeBase.Value.Color.red] + key_number[MazeBase.Value.Color.green]
if rand < 1.2 - 0.3 * total: #红绿钥匙
node.IsMonster = True
keys.append(Tools.dict_choice(key_choice_special))
door = Tools.dict_choice(key_choice_normal)
else:
rand = random.random()
if rand < 0.5:
pass
elif rand < 0.8: #多把黄钥匙
door = Tools.dict_choice(key_choice_normal)
node.IsMonster = True
is_yellows = True
else: #关键区域
door = Tools.dict_choice(key_choice_special)
else:
door = Tools.dict_choice(key_choice_normal)
if door == MazeBase.Value.Color.yellow:
if rand < 0.3: #没有黄钥匙(宝石)
pass
elif rand < 0.7: #多把黄钥匙加怪物
node.IsMonster = True
is_yellows = True
else: #一把蓝钥匙(可能有怪物)
if random.random() < 0.3:
node.IsMonster = True
keys.append(MazeBase.Value.Color.blue)
else:
if rand < 0.5:#没有黄钥匙(宝石)
pass
else: #多把黄钥匙(可能有怪物)
if random.random() < 0.3:
node.IsMonster = True
is_yellows = True
if is_yellows == True:
space = node.Space - node.IsMonster
while random.random() < 0.5 and space > 0:
keys.append(MazeBase.Value.Color.yellow)
space -= 1
for key in keys:
key_number[key] += 1
node.Key[key] += 1
node.Space -= 1
if door:
door_number[door] += 1
node.IsDoor = True
node.Door = door
special_node.add(node)
key_set = {
MazeBase.Value.Color.yellow: 0,
MazeBase.Value.Color.blue: 0,
MazeBase.Value.Color.red: 0,
MazeBase.Value.Color.green: 0
}
door_set = {
MazeBase.Value.Color.yellow: 0,
MazeBase.Value.Color.blue: 0,
MazeBase.Value.Color.red: 0,
MazeBase.Value.Color.green: 0
}
#门比钥匙少
for color in MazeBase.Value.Color.total:
num = door_number[color] - key_number[color]
if num < 0:
num = -num
door_set[color] = num
door_total -= num
#设置门的数量分配
for _ in range(door_total - sum(door_number.values())):
door = Tools.dict_choice(key_choice)
door_set[door] += 1
#设置钥匙的数量分配
for color in MazeBase.Value.Color.total:
num = door_number[color] + door_set[color] - key_number[color]
key_set[color] = num