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.5KB

7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 = 3 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 <= d_lmt:
  33. new_line.append(False)
  34. else:
  35. new_line.append(True)
  36. else:
  37. if n_count >= a_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):
  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. my_map = createDungeon(x,y,seed)
  72. if not out:
  73. printDungeon(my_map)
  74. for _ in range(reps):
  75. my_map = refineDungeon(my_map, d_lmt, d_lmt)
  76. if not out:
  77. printDungeon(my_map)
  78. if out:
  79. img = Image.new("RGB",(x,y),(0,0,0))
  80. lst = []
  81. c_wall = [r.randint(0,255), r.randint(0,255), r.randint(0,255)] if color else [0,0,0]
  82. c_space = [255-x for x in c_wall]
  83. for line in my_map:
  84. for val in line:
  85. lst.append(tuple(c_space) if val else tuple(c_wall))
  86. img.putdata(lst)
  87. hexes = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
  88. filename = []
  89. for _ in range(16):
  90. filename.append(r.choice(hexes))
  91. if not os.path.exists("maps"):
  92. os.makedirs("maps")
  93. img.save('maps/{}.png'.format("".join(filename)))
  94. print("Saved maps/{}.png".format("".join(filename)))
  95. def parseArgs(args):
  96. flags = {
  97. "--height" : 20,
  98. "--width" : 20,
  99. "--seed" : 45,
  100. "--death" : 4,
  101. "--birth" : 4,
  102. "--reps" : 2,
  103. "--out" : False,
  104. "--color" : False,
  105. }
  106. for flag, default in flags.items():
  107. if flag in args:
  108. if flag == "--out":
  109. flags["--out"] = True
  110. elif flag == "--color":
  111. flags["--color"] = True
  112. else:
  113. flags[flag] = args[args.index(flag) + 1]
  114. return flags
  115. if __name__ == "__main__":
  116. flags = parseArgs(sys.argv)
  117. main(flags["--width"],
  118. flags["--height"],
  119. flags["--seed"],
  120. flags["--death"],
  121. flags["--birth"],
  122. flags["--reps"],
  123. flags["--out"],
  124. flags["--color"])