-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1105 lines (817 loc) · 36.5 KB
/
main.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
from math import sqrt, tan, sin, cos, pi, ceil, floor, acos, atan, asin, degrees, radians, log, atan2, acos, asin
from copy import copy, deepcopy
import pygame
import sys, os, time
from generator import Generator
from drawn_map import DrawnMap, load_tile_table
#PYGAME-BASED VISUAL INTERFACE
#the generator can be used without it (see e.g. ascii_level_example.py)
#parameters for drawing graphs
minNodeHeight = 29
maxHeight = 250
nodeRadius = 10
spaceX = 90
xOffset = 96
yOffset = 96
#file paths
settingsPath = "resources/settings.ini"
iconPath = "resources/chest_icon.png"
tilesetPath = "resources/tileset.png"
#SETUP
class DrawnTree:
def __init__(self):
self.drawnNodes = []
self.drawnNodeDc = {}
self.selectedNode = None
def updatePathConnections(self):
drawnNodeDcKeys = set(self.drawnNodeDc.keys())
for drawnNode in self.drawnNodes:
drawnNode.pathConnections = []
for drawnNode in self.drawnNodes:
currentNode = drawnNode.node
parentNode = drawnNode.parentNode
if parentNode != None:
for childNode in parentNode.childNodes:
if childNode.label == currentNode.label:
break
for pathSource, pathTarget, pathColor, pathSpecifiedMaterial in parentNode.childPaths:
sourceLabel, sourceDirection = pathSource
targetLabel, targetDirection = pathTarget
if sourceLabel == currentNode.label:
if targetLabel == None: #connects to parent
drawnParent = self.drawnNodeDc[parentNode]
drawnNode.pathConnections.append(drawnParent)
drawnParent.pathConnections.append(drawnNode)
else: #connects to sibling
node2 = None
for childNode in parentNode.childNodes:
if childNode.label == targetLabel:
node2 = childNode
break
if not node2 in drawnNodeDcKeys: #wrong label in path
continue
drawnNode2 = self.drawnNodeDc[node2]
if drawnNode != None and drawnNode2 != None:
drawnNode.pathConnections.append(drawnNode2)
drawnNode2.pathConnections.append(drawnNode)
elif targetLabel != None:
if targetLabel == currentNode.label:
node2 = None
for childNode in parentNode.childNodes:
if childNode.label == sourceLabel:
node2 = childNode
break
if not node2 in drawnNodeDcKeys: #wrong label in path
continue
drawnNode2 = self.drawnNodeDc[node2]
if drawnNode != None and drawnNode2 != None:
drawnNode.pathConnections.append(drawnNode2)
drawnNode2.pathConnections.append(drawnNode)
class DrawnNode:
def __init__(self, label, node, x, y, h, w, parentNode, parentOffset, parentSize, prototypeDc):
self.label = label
self.node = node
self.x, self.y, self.w, self.h = x, y, w, h
self.parentNode = parentNode
self.parentOffset = parentOffset
self.parentSize = parentSize
self.prototypeDc = prototypeDc
self.pathConnections = []
def drawPaths(self, screen, nodeRadius, xOffset, yOffset, tree, color):
for connected in self.pathConnections:
if self.x == connected.x:
midpoint = min(self.x, connected.x)-48
else:
midpoint = min(self.x, connected.x)
pygame.draw.line(screen, color, (self.x+xOffset,self.y+yOffset), (midpoint+xOffset,self.y+yOffset), 3)
pygame.draw.line(screen, color, (connected.x+xOffset,connected.y+yOffset), (midpoint+xOffset,connected.y+yOffset), 3)
pygame.draw.line(screen, color, (midpoint+xOffset,self.y+yOffset), (midpoint+xOffset,connected.y+yOffset), 3)
def drawLinks(self, screen, nodeRadius, xOffset, yOffset, tree, color):
global screenH
if self.parentNode:
sourceY = self.parentOffset + self.parentSize/2
if sourceY+yOffset < 0 and self.y+yOffset < 0:
return
if sourceY+yOffset > screenH and self.y+yOffset > screenH:
return
parts = 160
yPrev = -999
for i in range(parts):
progress = float(i) / float(parts)
increment = 1. / float(parts)
x1 = self.x - (progress*90) -nodeRadius/2
x2 = x1 + (increment*90)
progress = progress**2
y1 = (self.y + (sourceY-self.y) * progress)
y2 = (self.y + (sourceY-self.y) * progress) + (sourceY-self.y) * increment
if yPrev != -999:
y1 = yPrev
pygame.draw.line(screen, color, (x2+xOffset,y1+yOffset), (x1+xOffset,y2+yOffset), 2)
yPrev = y2
def draw(self, screen, nodeRadius, xOffset, yOffset, tree):
# after drawing a map, nodes are assigned structures for arrangements which they previously don't have
global screenH
if self.y+yOffset-self.h/2 > -32 and self.y+yOffset+self.h/2 < screenH+32:
if self.node.structure != None and self.node.structure.__class__.__name__ == "Structure":
nodeColor = self.node.structure.color
elif self.label in self.prototypeDc.keys():
nodeColor = self.prototypeDc[self.label].color
else:
nodeColor = (100, 96, 96)
nodeColor = (min(230,nodeColor[0]+64),min(230,nodeColor[1]+64),min(230,nodeColor[2]+64))
nodeColorDark = nodeColor
nodeColorDot = (255,255,255)
if self == tree.selectedNode:
nodeColorDark = (0,160,224)
pygame.draw.rect(screen, nodeColorDark, (xOffset+self.x-(self.w/2)-self.h/2, self.y+yOffset-self.h/2, self.w+self.h, self.h), 0, border_radius=999)
pygame.draw.rect(screen, nodeColor, (xOffset+self.x-(self.w/2)+2-self.h/2, self.y+yOffset-(self.h-4)/2, self.w-4+self.h, self.h-4), 0, border_radius=999)
if '!' in self.node.properties:
if self.h > 32:
pygame.draw.circle(screen, nodeColorDot, (xOffset+self.x-36, self.y+yOffset), 4, 0)
else:
pygame.draw.circle(screen, nodeColorDot, (xOffset+self.x-28, self.y+yOffset), 4, 0)
textsurface = FONT.render(str(self.label)[:10], True, (0, 0, 0))
screen.blit(textsurface, (self.x+xOffset-self.w/2, self.y+yOffset-8))
if self.h > 32:
textsurface = FONT4.render(str(self.node.category)[:30], True, (0, 0, 0))
screen.blit(textsurface, (self.x+xOffset-self.w/2-2, self.y+yOffset+5))
def saveNode(screen, nodeRadius, node, parentNode, layer, layer_dc, nodeNumber, spaceX, xOffset, yOffset, parentOffset, parentSize, treeNodes, prototypeDc, drawnNodeDc):
minHeight = getMinHeight(node)
x = 0
layerHeight = 0
previousNodesHeight = 0
if parentNode != None:
for i, anyNode in enumerate(parentNode.childNodes):
currentHeight = getMinHeight(anyNode)
layerHeight += currentHeight
if i < nodeNumber:
previousNodesHeight += currentHeight
else:
layerHeight = minHeight
previousArea = float(previousNodesHeight) / float(layerHeight)
areaStart = parentOffset + int( previousArea * parentSize )
areaSize = minHeight
y = areaStart + areaSize / 2
for childNodeNumber, childNode in enumerate(node.childNodes):
heightDiff = 0
childHeightsCollected = max(minNodeHeight, sum([getMinHeight(childNode2) for childNode2 in node.childNodes]))
childHeights = max(minNodeHeight, childHeightsCollected)
heightDiff = childHeights - minHeight
saveNode(screen, nodeRadius, childNode, node, layer+1, layer_dc, childNodeNumber, spaceX, xOffset, yOffset, int(areaStart-heightDiff/2), int(areaSize+heightDiff), treeNodes, prototypeDc, drawnNodeDc)
rectHeight = minNodeHeight-4
if getMinHeightAltered(node, 0) > minNodeHeight:
rectHeight = minNodeHeight*2-16
rectWidth = 44
x = layer*spaceX
drawnNode = DrawnNode(node.label, node, x, y, rectHeight, rectWidth, parentNode, parentOffset, parentSize, prototypeDc)
drawnNodeDc[node] = drawnNode
treeNodes.append(drawnNode)
#create arrangement
def collectNodes(node, depth, layer_dc):
if depth in layer_dc.keys():
layer_dc[depth].append(node)
else:
layer_dc[depth] = [node]
for childNode in node.childNodes:
collectNodes(childNode, depth+1, layer_dc)
def getMinHeight(node):
if node.childNodes:
childHeights = sum([getMinHeight(childNode) for childNode in node.childNodes])
return max(minNodeHeight, childHeights)
else:
return minNodeHeight
def getMinHeightAltered(node, margin):
if node.childNodes:
childHeights = sum([getMinHeightAltered(childNode, margin) for childNode in node.childNodes])
return max(minNodeHeight, childHeights-margin)
else:
return minNodeHeight
def generateDrawnTree(root, prototypeDc):
tree = DrawnTree()
tree.drawnNodes = []
tree.drawnNodeDc = {}
layer_dc = {}
collectNodes(root, 0, layer_dc)
saveNode(screen, nodeRadius, root, None, 0, layer_dc, 0, spaceX, xOffset, yOffset, 0, maxHeight, tree.drawnNodes, prototypeDc, tree.drawnNodeDc)
tree.updatePathConnections()
return tree
class Path:
def __init__(self, pathData):
self.data = pathData
self.targetIsParent = False
self.reverseDirection = False
self.itemId = 0
self.source = None
self.sourceLabel = ''
self.sourceDirection = 's'
self.target = None
self.targetLabel = ''
self.targetDirection = 's'
def getSettings(settingsPath):
setting_flags = {}
if os.path.isfile(settingsPath):
with open(settingsPath, 'r') as file:
for line in file.readlines():
if "=" in line:
key, value = line.split("=", 1)
setting_flags[key.strip()] = value.strip()
return setting_flags
def updateSettings(settingsPath, setting_flags, setting_keys, autosave, key, value):
if autosave and os.path.isfile(settingsPath):
setting_keys.add(key)
setting_flags[key] = value
with open(settingsPath, 'w') as file:
setting_tileSize = int(setting_flags['tileSize']) if 'tileSize' in setting_keys else 8
setting_seedSelected = int(setting_flags['seedSelected']) if 'seedSelected' in setting_keys else 122
setting_mapSelected = setting_flags['mapSelected'] if 'mapSelected' in setting_keys else ""
contents = ["autosave=%i\n" % 1,
"tileSize=%i\n" % setting_tileSize,
"seedSelected=%i\n" % setting_seedSelected,
"mapSelected=%s" % setting_mapSelected
]
file.writelines(contents)
class Interface:
def __init__(self, screen, surface, screenW, screenH, nodeRadius, xOffset, yOffset):
self.screen, self.surface, self.screenW, self.screenH = screen, surface, screenW, screenH
self.nodeRadius = nodeRadius
self.xOffset, self.yOffset = xOffset, yOffset
self.xOffsetOrigin, self.yOffsetOrigin = self.xOffset, self.yOffset
self.mapDirectory = "maps"
self.currentMapDirectory = ""
self.currentMapName = ""
self.ms_names = []
self.map_selection = False
self.button_text = ["LOAD MAP", "GENERATE", "ADD DETAILS", "EXPORT TXT", "EXPORT PNG"]
self.button_active = [True, False, False, False, False]
self.setupUI()
self.setupCurrentZoom()
self.setupCurrentSeed()
self.setupMapSelection()
self.overlay_alpha = 32 #darkening the screen slightly while something is loading
self.overlay = pygame.Surface((screenW,screenH))
self.overlay.fill((0,0,0))
self.overlay.set_alpha(self.overlay_alpha)
self.tree = None
self.currentPaths = []
self.generator = Generator()
self.drawnMap = None
self.displayTiles = False
#read settings from settings.ini, if it exists
self.setting_flags = getSettings(settingsPath)
self.setting_keys = set(self.setting_flags.keys())
self.autosaveSettings = 'autosave' in self.setting_flags and self.setting_flags['autosave'] == '1'
#tile size
self.tileSizes = [1,2,3,4,6,8,12,16]
self.tileSizeSelected = 5
if 'tileSize' in self.setting_keys:
tileSizeSetting = int(self.setting_flags['tileSize']) if self.setting_flags['tileSize'].isdigit() else 8
if tileSizeSetting in self.tileSizes:
self.tileSizeSelected = self.tileSizes.index(tileSizeSetting)
self.tileSize = self.tileSizes[self.tileSizeSelected]
#seed
self.SEED = 122
if 'seedSelected' in self.setting_keys:
self.SEED = int(self.setting_flags['seedSelected']) if self.setting_flags['seedSelected'].isdigit() else 122
#load map (if map name is saved in settings)
mapSelected = ""
if 'mapSelected' in self.setting_keys:
mapSelected = self.setting_flags['mapSelected']
if mapSelected:
self.currentMapName = mapSelected
self.currentMapDirectory = self.mapDirectory + "/" + self.currentMapName
setupSuccessful = self.setup(self.currentMapDirectory, self.SEED)
if setupSuccessful:
self.generateDrawnTree()
self.button_active[1] = True
self.button_active[2] = False
else:
self.generator.reset()
self.tree = None
def loadMap(self, mapNumber):
global popup
#close level selection and current map when a new map is loaded
self.map_selection = False
self.drawnMap = None
self.generator.reset()
self.currentMapName = self.ms_names[mapNumber]
self.currentMapDirectory = self.mapDirectory + "/" + self.currentMapName
setupSuccessful = self.setup(self.currentMapDirectory, self.SEED)
if setupSuccessful:
self.generateDrawnTree()
popup = False
self.xOffset, self.yOffset = self.xOffsetOrigin, self.yOffsetOrigin
self.button_active[1] = True
self.button_active[2] = False
updateSettings(settingsPath, self.setting_flags, self.setting_keys, self.autosaveSettings, 'mapSelected', self.currentMapName)
print ("\nLOADED MAP: %s" % self.currentMapName)
else:
self.generator.reset()
self.tree = None
def setup(self, directoryName, selectionSeed):
global popup, popupTimer, popupsurface
try:
#load map rules, create graph for given seed
textFiles = [os.path.join(directoryName, filePath) for filePath in os.listdir(directoryName) if os.path.isfile(os.path.join(directoryName, filePath)) and filePath[-4:] == ".txt"]
self.generator.loadRules(textFiles)
self.generator.loadGraph(selectionSeed)
# self.generator.printCurrentGraph()
# self.generator.printCurrentStructures()
return True
#interface error messages
except IndexError as missingDefinition:
popup, popupTimer = True, 100
popupsurface = FONT3.render("ERROR - map doesn't contain any structures for: %s" % str(missingDefinition), True, (0, 0, 0))
print ("ERROR - map doesn't contain any structures for: %s" % str(missingDefinition))
return False
except KeyError as missingKey:
missingLabel, missingCategory = str(missingKey)[1:-1].rsplit("#", 1)
popup, popupTimer = True, 100
if missingCategory == "":
popupsurface = FONT3.render("ERROR - label doesn't have any category: %s (???)" % missingLabel, True, (0, 0, 0))
print ("ERROR - structure label doesn't have a category: %s" % missingLabel)
else:
popupsurface = FONT3.render("ERROR - category '%s' is not defined: %s (%s)" % (missingCategory, missingLabel, missingCategory), True, (0, 0, 0))
print ("ERROR - map doesn't define structure: %s" % missingCategory)
return False
except FileNotFoundError:
popup, popupTimer = True, 100
popupsurface = FONT3.render("ERROR - couldn't find the map folder", True, (0, 0, 0))
print ("ERROR - couldn't find the map folder")
return False
except:
popup, popupTimer = True, 100
popupsurface = FONT3.render("UNKNOWN ERROR - possibly missing root or wrong path formatting?", True, (0, 0, 0))
print ("UNKNOWN ERROR - possibly missing root or wrong path formatting?")
return False
def generateDrawnTree(self):
self.tree = generateDrawnTree(self.generator.root, self.generator.prototypeDc)
self.tree.selectedNode = None
def getCurrentPaths(self):
self.currentPaths = []
if self.tree.selectedNode != None and self.tree.selectedNode.label != "root":
parentNode = self.tree.selectedNode.parentNode
currentNode = self.tree.selectedNode.node
for itemId, pathData in enumerate(parentNode.childPaths):
pathSource, pathTarget, pathColor, pathSpecifiedMaterial = pathData
sourceLabel, sourceDirection = pathSource
targetLabel, targetDirection = pathTarget
if targetLabel == None:
targetLabel = parentNode.label
if self.tree.selectedNode.label == sourceLabel:
#source -> target
path = Path(pathData)
path.reverseDirection = False
path.targetIsParent = targetLabel == None
path.itemId = itemId
path.source, path.sourceLabel, path.sourceDirection = None, sourceLabel, sourceDirection
path.target, path.targetLabel, path.targetDirection = None, targetLabel, targetDirection
if path.targetIsParent:
path.targetLabel = "PARENT"
self.currentPaths.append(path)
elif self.tree.selectedNode.label == targetLabel:
#target -> source (flipped)
path = Path(pathData)
path.reverseDirection = True
path.targetIsParent = False
path.itemId = itemId
path.source, path.sourceLabel, path.sourceDirection = None, sourceLabel, sourceDirection
path.target, path.targetLabel, path.targetDirection = None, targetLabel, targetDirection
self.currentPaths.append(path)
def centerText(self, text, font, color, x, y):
message = font.render(text, True, color)
rect = message.get_rect(center=(x, y))
self.screen.blit(message, (rect[0], y))
def setupMapSelection(self):
if os.path.isdir(self.mapDirectory):
self.ms_names = [directoryName for directoryName in os.listdir(self.mapDirectory) if os.path.isdir(os.path.join(self.mapDirectory, directoryName))]
else:
self.ms_names = []
self.ms_bounds = []
self.ms_text_positions = []
button_h = 64
button_spacing = 16
button_padding = 40
for i in range(len(self.ms_names)):
selection_y = button_padding + (button_h+button_spacing) * i
ms_bounds = (self.ms_button_x-self.ms_button_width/2, selection_y, self.ms_button_width, button_h)
ms_text_position = self.ms_button_x, selection_y+20
self.ms_bounds.append(ms_bounds)
self.ms_text_positions.append(ms_text_position)
def setupCurrentSeed(self):
if self.button_bounds:
self.seed_bounds = (self.screenW-113, self.button_bounds[0][1]-53, 100, 35)
self.seed_text_position = (self.screenW-66, self.seed_bounds[1]+9)
else:
self.seed_bounds = (self.screenW-113, 75, 100, 35)
self.seed_text_position = (self.screenW-66, self.seed_bounds[1]+9)
def setupCurrentZoom(self):
if self.button_bounds:
self.zoom_bounds = (self.screenW-113, self.button_bounds[0][1]-53-60, 100, 35)
self.zoom_text_position = (self.screenW-66, self.zoom_bounds[1]+9)
else:
self.zoom_bounds = (self.screenW-113, 75-60, 100, 35)
self.zoom_text_position = (self.screenW-66, self.zoom_bounds[1]+9)
def drawCurrentSeed(self):
pygame.draw.rect(self.screen, (255, 255, 255), self.seed_bounds, 0)
self.centerText("seed", FONT2, (0,0,0), self.seed_text_position[0], self.seed_text_position[1]-28)
self.centerText(str(self.SEED), FONT2, (0,0,0), self.seed_text_position[0], self.seed_text_position[1])
pygame.draw.polygon(self.screen, (0, 0, 0), [(self.seed_text_position[0]-28-12, self.seed_text_position[1]+8), (self.seed_text_position[0]-28, self.seed_text_position[1]+8-8), (self.seed_text_position[0]-28, self.seed_text_position[1]+8+8)])
pygame.draw.polygon(self.screen, (0, 0, 0), [(self.seed_text_position[0]+28+12, self.seed_text_position[1]+8), (self.seed_text_position[0]+28, self.seed_text_position[1]+8-8), (self.seed_text_position[0]+28, self.seed_text_position[1]+8+8)])
def drawCurrentZoom(self):
pygame.draw.rect(self.screen, (255, 255, 255), self.zoom_bounds, 0)
self.centerText("tile size", FONT2, (0,0,0), self.zoom_text_position[0], self.zoom_text_position[1]-28)
self.centerText(str(self.tileSize), FONT2, (0,0,0), self.zoom_text_position[0], self.zoom_text_position[1])
pygame.draw.polygon(self.screen, (0, 0, 0), [(self.zoom_text_position[0]-28-12, self.zoom_text_position[1]+8), (self.zoom_text_position[0]-28, self.zoom_text_position[1]+8-8), (self.zoom_text_position[0]-28, self.zoom_text_position[1]+8+8)])
pygame.draw.polygon(self.screen, (0, 0, 0), [(self.zoom_text_position[0]+28+12, self.zoom_text_position[1]+8), (self.zoom_text_position[0]+28, self.zoom_text_position[1]+8-8), (self.zoom_text_position[0]+28, self.zoom_text_position[1]+8+8)])
def setupUI(self):
#UI background
x1, y1 = self.screenW-128, 0
x2, y2 = self.screenW, self.screenH
self.ui_bounds = (x1, y1, x2-x1, y2-y1)
#buttons
button_w = 160
button_h = 80
button_offset, button_text_offset = 0, 0
button_spacing = 12
button_padding = 32 +96 +24
self.button_bounds = []
self.button_text_positions = []
self.button_selected = -1
for i in range(len(self.button_text)):
button_x = self.ui_bounds[0]+self.ui_bounds[2]/2 - button_w/2
button_y = button_padding + (button_h+button_spacing)*i
#hacky change: shrink size of export buttons
if i >= 3:
button_h_multiplier = .5
else:
button_h_multiplier = 1
if i >= 4:
button_y -= button_h/2
self.button_bounds.append( (button_x+button_offset, button_y, button_w, button_h*button_h_multiplier) )
self.button_text_positions.append( (button_x+button_text_offset+button_w/2-4, button_y+button_h*button_h_multiplier/2-10) )
#map selection
self.ms_button_width = 400
self.ms_button_selected = -1
ms_x1, ms_y1 = 0, 0
ms_x2, ms_y2 = self.screenW, self.screenH
self.map_selection_bounds = (ms_x1, ms_y1, ms_x2-ms_x1, ms_y2-ms_y1)
self.ms_button_x = self.map_selection_bounds[0]+self.map_selection_bounds[2]/2
def drawUI(self):
#colors
column_color = (224, 224, 224)
outline_color = (64, 64, 64)
bg_color = (192, 192, 192)
#map selection
if self.map_selection:
pygame.draw.rect(self.screen, bg_color, self.map_selection_bounds, 0)
for i in range(len(self.ms_names)):
if i == self.ms_button_selected:
button_color = (192, 224, 255)
button_text_color = (0, 0, 128)
else:
button_color = (240, 240, 240)
button_text_color = (0, 0, 0)
pygame.draw.rect(self.screen, button_color, self.ms_bounds[i], 0)
pygame.draw.rect(self.screen, outline_color, self.ms_bounds[i], 1)
self.centerText(self.ms_names[i], FONT3, button_text_color, self.ms_text_positions[i][0], self.ms_text_positions[i][1])
#UI background
pygame.draw.rect(self.screen, column_color, self.ui_bounds, 0)
pygame.draw.rect(self.screen, outline_color, (self.ui_bounds[0]-1, self.ui_bounds[1], 1, self.ui_bounds[3]), 0)
#buttons
for i in range(len(self.button_text)):
if i == self.button_selected:
button_color = (192, 224, 255)
button_text_color = (0, 0, 128)
elif self.button_active[i]:
button_color = (240, 240, 240)
button_text_color = (0, 0, 0)
else:
button_color = (192, 192, 192)
button_text_color = (144, 144, 144)
if self.button_text[i] == "LOAD MAP" and self.drawnMap:
button_text = "RETURN"
else:
button_text = self.button_text[i]
pygame.draw.rect(self.screen, button_color, self.button_bounds[i], 0, border_radius=8)
pygame.draw.rect(self.screen, outline_color, self.button_bounds[i], 1, border_radius=8)
self.centerText(button_text, FONT3, button_text_color, self.button_text_positions[i][0], self.button_text_positions[i][1])
def updateButtonHover(self, mousePos):
self.button_selected = -1
self.ms_button_selected = -1
mouseX, mouseY = mousePos
for i in range(len(self.button_text)):
if self.button_active[i] and self.button_bounds[i][0] < mouseX < self.button_bounds[i][0]+self.button_bounds[i][2] and self.button_bounds[i][1] < mouseY < self.button_bounds[i][1]+self.button_bounds[i][3]:
self.button_selected = i
if self.map_selection:
for i in range(len(self.ms_names)):
if self.ms_bounds[i][0]-8 < mouseX < self.ms_bounds[i][0]+self.ms_bounds[i][2]+8 and self.ms_bounds[i][1]-8 < mouseY < self.ms_bounds[i][1]+self.ms_bounds[i][3]+8:
self.ms_button_selected = i
def backToGraph(self):
self.generator.root.clearStructures()
self.button_active[1] = True
self.button_active[2] = False
self.button_active[3] = False
self.button_active[4] = False
def leftClick(self, mousePos):
global popup, popupTimer, popupsurface
# otherKeys=pygame.key.get_pressed()
mouseX, mouseY = mousePos
clickedMap = False
if self.map_selection:
clickedMap = True
if self.ms_button_selected != -1:
self.loadMap(self.ms_button_selected)
else:
self.map_selection = not self.map_selection
if self.button_selected != -1:
if self.button_selected == 0:
if self.drawnMap:
self.backToGraph()
self.drawnMap = None
else:
if self.ms_names:
self.map_selection = not self.map_selection
else:
popup, popupTimer = True, 50
popupsurface = FONT3.render("ERROR - directory named 'maps' not found!", True, (0, 0, 0))
print ("ERROR - directory named 'maps' not found!")
elif self.button_selected == 1:
self.map_selection = False
self.button_active[1] = False
self.button_active[2] = True
self.drawnMap = self.generateCurrentMap()
elif self.button_selected == 2:
self.map_selection = False
self.button_active[1] = False
self.button_active[2] = False
self.drawnMap = self.addDetailsToCurrentMap(self.drawnMap)
elif self.button_selected == 3:
self.save_level_tilemap()
self.button_active[3] = False
elif self.button_selected == 4:
self.save_level_image()
self.button_active[4] = False
if not clickedMap and not self.drawnMap and mouseX < self.screenW-120 and self.tree != None: #node graph #update this section
self.tree.selectedNode = None
self.currentPaths = []
for drawnNode in reversed(self.tree.drawnNodes):
xOffset, yOffset = self.xOffset, self.yOffset
if drawnNode.y+yOffset-(drawnNode.h+8)/2 <= mouseY and drawnNode.y+yOffset+(drawnNode.h+8)/2 >= mouseY and drawnNode.x+xOffset-(drawnNode.w+24)/2 <= mouseX and drawnNode.x+xOffset+(drawnNode.w+24)/2 >= mouseX:
self.tree.selectedNode = drawnNode
self.getCurrentPaths()
break
else:
#change seed
if not self.drawnMap or (not self.displayTiles and (self.tree.selectedNode == None or self.tree.selectedNode.label == "root")):
seedLeftRect = pygame.Rect(self.seed_bounds[0], self.seed_bounds[1], 50, self.seed_bounds[3])
seedRightRect = pygame.Rect(self.seed_bounds[0]+50, self.seed_bounds[1], 50, self.seed_bounds[3])
if seedLeftRect.collidepoint(mousePos): #seed -1
self.SEED -= 1
updateSettings(settingsPath, self.setting_flags, self.setting_keys, self.autosaveSettings, 'seedSelected', str(self.SEED))
if self.currentMapDirectory != "":
self.setup(self.currentMapDirectory, self.SEED)
self.generateDrawnTree()
if self.drawnMap:
self.drawnMap = self.generateCurrentMap()
elif seedRightRect.collidepoint(mousePos): #seed +1
self.SEED += 1
updateSettings(settingsPath, self.setting_flags, self.setting_keys, self.autosaveSettings, 'seedSelected', str(self.SEED))
if self.currentMapDirectory != "":
self.setup(self.currentMapDirectory, self.SEED)
self.generateDrawnTree()
if self.drawnMap:
self.drawnMap = self.generateCurrentMap()
#change tile size
zoomLeftRect = pygame.Rect(self.zoom_bounds[0], self.zoom_bounds[1], 50, self.zoom_bounds[3])
zoomRightRect = pygame.Rect(self.zoom_bounds[0]+50, self.zoom_bounds[1], 50, self.zoom_bounds[3])
if zoomLeftRect.collidepoint(mousePos) and self.tileSizeSelected > 0: #tileSize +1
self.tileSizeSelected -= 1
self.tileSize = self.tileSizes[self.tileSizeSelected]
updateSettings(settingsPath, self.setting_flags, self.setting_keys, self.autosaveSettings, 'tileSize', str(self.tileSize))
if self.currentMapDirectory != "":
if self.drawnMap:
self.drawnMap.mapDrawn = False
elif zoomRightRect.collidepoint(mousePos) and self.tileSizeSelected < len(self.tileSizes)-1: #tileSize -1
self.tileSizeSelected += 1
self.tileSize = self.tileSizes[self.tileSizeSelected]
updateSettings(settingsPath, self.setting_flags, self.setting_keys, self.autosaveSettings, 'tileSize', str(self.tileSize))
if self.currentMapDirectory != "":
if self.drawnMap:
self.drawnMap.mapDrawn = False
#update screen
if self.drawnMap:
self.drawnMap.drawScreen(screen, surface, screenW, screenH, self.tileSize, FONT, tileset, self.displayTiles)
self.drawUIOverMap()
else:
self.drawScreen()
def drawUIOverMap(self):
self.updateButtonHover(pygame.mouse.get_pos())
self.drawUI()
self.drawCurrentZoom()
if not self.displayTiles and (self.tree.selectedNode == None or self.tree.selectedNode.label == "root"):
self.drawCurrentSeed()
pygame.display.update()
def generateCurrentMap(self):
global popup, popupTimer, popupsurface
if not self.tree or not self.generator.root:
return None
#if structure isn't a leaf node, place loading overlay
if self.tree.selectedNode == None or not self.tree.selectedNode.node.category in self.generator.structureDc.keys():
self.screen.blit(self.overlay, (0,0))
pygame.display.update()
start_time = time.time()
try:
if self.tree.selectedNode == None or self.tree.selectedNode.label == "root":
self.generator.generateMapFromCurrentGraph(self.SEED)
else:
rootCopy = deepcopy(self.generator.root)
rootCopy.childNodes = [ self.tree.selectedNode.node ]
self.generator.generateMapFromCurrentGraph(self.SEED, rootCopy)
except Exception as errorMessage:
self.button_active[1] = True
self.button_active[2] = False
popup, popupTimer = True, 50
popupsurface = FONT3.render("Error - " + str(errorMessage), True, (0, 0, 0))
print ("Error - " + str(errorMessage))
return None
print ("\nGenerated in %.3f seconds" % ((time.time() - start_time)))
self.displayTiles = False
#draw update is done in click step
drawnMap = DrawnMap(self.generator.generatedMap)
return drawnMap
def addDetailsToCurrentMap(self, drawnMap):
if not self.tree or not self.generator.generatedMap or not drawnMap:
return None
#place loading overlay (hacky solution)
self.screen.blit(self.overlay, (0,0))
pygame.display.update()
start_time = time.time()
self.generator.addDetails()
print ("\nAdded details in %.3f seconds" % ((time.time() - start_time)))
self.displayTiles = True
drawnMap.setStructure(self.generator.generatedMap)
drawnMap.clearMap()
start_time = time.time()
drawnMap.drawScreen(screen, surface, screenW, screenH, self.tileSize, FONT, tileset, self.displayTiles)
print ("\nDrawn map in %.3f seconds" % ((time.time() - start_time)))
self.button_active[3] = True
self.button_active[4] = True
return drawnMap
def drawScreen(self):
bg_color = (192, 192, 192)
self.screen.fill(bg_color)
link_color = (172, 172, 172)
path_color = (220, 220, 220)
if self.tree != None:
for drawnNode in self.tree.drawnNodes:
drawnNode.drawLinks(self.screen, self.nodeRadius, self.xOffset, self.yOffset, self.tree, link_color)
# for drawnNode in self.tree.drawnNodes:
# drawnNode.drawPaths(self.screen, self.nodeRadius, self.xOffset, self.yOffset, self.tree, path_color)
for drawnNode in self.tree.drawnNodes:
drawnNode.draw(self.screen, self.nodeRadius, self.xOffset, self.yOffset, self.tree)
#interface
interface.drawUI()
interface.drawCurrentZoom()
interface.drawCurrentSeed()
pygame.display.update()
def save_level_tilemap(self):
global popup, popupTimer, popupsurface
file_name = "export_txt_%s.txt" % time.strftime('%Y-%m-%d_%H%M%S', time.gmtime(time.time()))
# tilemap_text = self.generator.generatedMap.getTilemapText(simplify=False)
tilemap_text = self.generator.generatedMap.getTilemapText(simplify=True)
with open(file_name, 'w') as file:
file.write(tilemap_text)
print ("\nSaved tile map to file: %s" % file_name)
popup, popupTimer = True, 30
popupsurface = FONT3.render("Saved tile map to file: %s" % file_name, True, (0, 0, 0))
def save_level_image(self):
global popup, popupTimer, popupsurface
if self.drawnMap == None or self.drawnMap.displayedStructure == None:
return
file_name = "export_png_%s.png" % time.strftime('%Y-%m-%d_%H%M%S', time.gmtime(time.time()))
imageSurface = pygame.Surface((self.drawnMap.displayedStructure.w * self.tileSize, self.drawnMap.displayedStructure.h * self.tileSize))
imageSurface.blit(self.drawnMap.mapSurface, (0, 0), (0, 0, imageSurface.get_width(), imageSurface.get_height()))
pygame.image.save(imageSurface, file_name)
print ("\nSaved map image to file: %s" % file_name)
popup, popupTimer = True, 30
popupsurface = FONT3.render("Saved map image to file: %s" % file_name, True, (0, 0, 0))
#LAUNCH
pygame.font.init()
FONT = pygame.font.SysFont('verdana', 11)
FONT2 = pygame.font.SysFont('verdana', 13)
FONT3 = pygame.font.SysFont('verdana', 16)
FONT4 = pygame.font.SysFont('verdana', 7)
screenW, screenH = 1280, 720
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (300, 150)
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((screenW,screenH), pygame.RESIZABLE)
surface = pygame.Surface((110*32, 72*32))
pygame.display.set_caption("Node-Oriented Procedural Engine")
if os.path.isfile(iconPath):
pygame.display.set_icon(pygame.image.load(iconPath))
tileset = []
if os.path.isfile(tilesetPath):
tileset = load_tile_table(tilesetPath, 16, 16)
interface = Interface(screen, surface, screenW, screenH, nodeRadius, xOffset, yOffset)
interface.drawScreen()
popup = False
popupTimer = 0
#MAIN LOOP
running = True
while running:
clock.tick(30)
if interface.drawnMap:
heldKeys=pygame.key.get_pressed()
#do not re-draw the map unless position changes
if heldKeys[pygame.K_s] or heldKeys[pygame.K_DOWN]:
interface.drawnMap.yOffset -= 24
interface.drawnMap.drawScreen(screen, surface, screenW, screenH, interface.tileSize, FONT, tileset, interface.displayTiles, updateDisplay=False)
elif heldKeys[pygame.K_w] or heldKeys[pygame.K_UP]:
interface.drawnMap.yOffset += 24
interface.drawnMap.drawScreen(screen, surface, screenW, screenH, interface.tileSize, FONT, tileset, interface.displayTiles, updateDisplay=False)
if heldKeys[pygame.K_d] or heldKeys[pygame.K_RIGHT]: