Python script to generate simple "dungeon maps"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

procgen.py 3.9KB

7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import random as r
  2. import sys, os
  3. from PIL import Image
  4. def createDungeon(x=None, y=None, seed=None):
  5. """ Initializes an x by y grid.
  6. x is width, y is height
  7. seed is the chance that a given cell will be "live" and should be an integer between 1-99.
  8. If True is equivalent to "walkable", then lower seeds make more walls.
  9. """
  10. x = 10 if x == None else int(x)
  11. y = 10 if y == None else int(y)
  12. seed = 45 if seed == None else int(seed)
  13. new_map = []
  14. for j in range(y):
  15. new_row = []
  16. for i in range(x):
  17. new_row.append(False if r.randint(1,99) > seed else True)
  18. new_map.append(new_row)
  19. return new_map
  20. def refineDungeon(d_map, d_lmt=None, a_lmt=None):
  21. """ Refines the grid.
  22. """
  23. d_lmt = 4 if d_lmt == None else int(d_lmt)
  24. a_lmt = 4 if a_lmt == None else int(a_lmt)
  25. new_map = []
  26. for j in range(len(d_map)):
  27. new_line = []
  28. for i in range(len(d_map[j])):
  29. x, y = i, j
  30. n_count = countAliveNeighbors(d_map, x, y)
  31. if d_map[y][x]:
  32. if n_count >= a_lmt:
  33. new_line.append(False)
  34. else:
  35. new_line.append(True)
  36. else:
  37. if n_count <= d_lmt:
  38. new_line.append(True)
  39. else:
  40. new_line.append(False)
  41. new_map.append(new_line)
  42. return new_map
  43. def countAliveNeighbors(d_map, x, y):
  44. count = 0
  45. for j in range(-1,2):
  46. for i in range(-1,2):
  47. n_x, n_y = x+i, y+j
  48. if i == 0 and j == 0:
  49. continue
  50. if n_x < 0 or n_x >= len(d_map[j]) or n_y == 0 or n_y >= len(d_map):
  51. count += 1
  52. elif d_map[n_y][n_x]:
  53. count += 1
  54. return count
  55. def printDungeon(d_map, wall=None, path=None):
  56. wall = "II" if wall == None else wall
  57. path = " " if path == None else path
  58. for line in d_map:
  59. print("".join([wall if x == True else path for x in line]))
  60. print()
  61. def main(x=None, y=None, seed=None, d_lmt=None, a_lmt=None, reps=None, out=None, color=None, chunky=None):
  62. # Initialize
  63. x = 20 if x == None else int(x)
  64. y = 20 if y == None else int(y)
  65. seed = 45 if seed == None else int(seed)
  66. d_lmt = 4 if d_lmt == None else int(d_lmt)
  67. a_lmt = 4 if a_lmt == None else int(a_lmt)
  68. reps = 2 if reps == None else int(reps)
  69. out = False if out == None else bool(out)
  70. color = False if color == None else bool(color)
  71. chunky = False if chunky == None else bool(chunky)
  72. my_map = createDungeon(x,y,seed)
  73. if not out:
  74. printDungeon(my_map)
  75. for _ in range(reps):
  76. my_map = refineDungeon(my_map, d_lmt, d_lmt)
  77. if not out:
  78. printDungeon(my_map)
  79. if out:
  80. if chunky:
  81. true_x, true_y = x*2, y*2
  82. else:
  83. true_x, true_y = x, y
  84. img = Image.new("RGB",(true_x,true_y),(0,0,0))
  85. lst = []
  86. c_wall = [r.randint(0,255), r.randint(0,255), r.randint(0,255)] if color else [0,0,0]
  87. c_space = [255-x for x in c_wall]
  88. if chunky:
  89. for line in my_map:
  90. for _ in range(2):
  91. for val in line:
  92. for _ in range(2):
  93. lst.append(tuple(c_space) if val else tuple(c_wall))
  94. else:
  95. for line in my_map:
  96. for val in line:
  97. lst.append(tuple(c_space) if val else tuple(c_wall))
  98. img.putdata(lst)
  99. hexes = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
  100. filename = []
  101. for _ in range(16):
  102. filename.append(r.choice(hexes))
  103. if not os.path.exists("maps"):
  104. os.makedirs("maps")
  105. img.save('maps/{}.png'.format("".join(filename)))
  106. print("Saved maps/{}.png".format("".join(filename)))
  107. def parseArgs(args):
  108. flags = {
  109. "--height" : 20,
  110. "--width" : 20,
  111. "--seed" : 45,
  112. "--death" : 4,
  113. "--birth" : 4,
  114. "--reps" : 2,
  115. "--out" : False,
  116. "--color" : False,
  117. "--chunky" : False,
  118. }
  119. for flag, default in flags.items():
  120. if flag in args:
  121. if flag == "--out":
  122. flags["--out"] = True
  123. elif flag == "--color":
  124. flags["--color"] = True
  125. elif flag == "--chunky":
  126. flags["--chunky"] = True
  127. else:
  128. flags[flag] = args[args.index(flag) + 1]
  129. return flags
  130. if __name__ == "__main__":
  131. flags = parseArgs(sys.argv)
  132. main(flags["--width"],
  133. flags["--height"],
  134. flags["--seed"],
  135. flags["--death"],
  136. flags["--birth"],
  137. flags["--reps"],
  138. flags["--out"],
  139. flags["--color"],
  140. flags["--chunky"])