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 2.5KB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import random as r
  2. import sys
  3. def createDungeon(x=None, y=None, seed=None):
  4. """ Initializes an x by y grid.
  5. x is width, y is height
  6. seed is the chance that a given cell will be "live" and should be an integer between 1-99.
  7. If True is equivalent to "walkable", then lower seeds make more walls.
  8. """
  9. x = 10 if x == None else int(x)
  10. y = 10 if y == None else int(y)
  11. seed = 45 if seed == None else int(seed)
  12. new_map = []
  13. for j in range(y):
  14. new_row = []
  15. for i in range(x):
  16. new_row.append(False if r.randint(1,99) > seed else True)
  17. new_map.append(new_row)
  18. return new_map
  19. def refineDungeon(d_map, d_lmt=None, a_lmt=None):
  20. """ Refines the grid.
  21. """
  22. d_lmt = 3 if d_lmt == None else int(d_lmt)
  23. a_lmt = 4 if a_lmt == None else int(a_lmt)
  24. new_map = []
  25. for j in range(len(d_map)):
  26. new_line = []
  27. for i in range(len(d_map[j])):
  28. x, y = i, j
  29. n_count = countAliveNeighbors(d_map, x, y)
  30. if d_map[y][x]:
  31. if n_count <= d_lmt:
  32. new_line.append(False)
  33. else:
  34. new_line.append(True)
  35. else:
  36. if n_count >= a_lmt:
  37. new_line.append(True)
  38. else:
  39. new_line.append(False)
  40. new_map.append(new_line)
  41. return new_map
  42. def countAliveNeighbors(d_map, x, y):
  43. count = 0
  44. for j in range(-1,2):
  45. for i in range(-1,2):
  46. n_x, n_y = x+i, y+j
  47. if i == 0 and j == 0:
  48. continue
  49. if n_x < 0 or n_x >= len(d_map[j]) or n_y == 0 or n_y >= len(d_map):
  50. count += 1
  51. elif d_map[n_y][n_x]:
  52. count += 1
  53. return count
  54. def printDungeon(d_map, wall=None, path=None):
  55. wall = "II" if wall == None else wall
  56. path = " " if path == None else path
  57. for line in d_map:
  58. print("".join([wall if x == True else path for x in line]))
  59. print()
  60. def main(x=None, y=None, seed=None, d_lmt=None, a_lmt=None, reps=None):
  61. # Initialize
  62. x = 20 if x == None else int(x)
  63. y = 20 if y == None else int(y)
  64. seed = 45 if seed == None else int(seed)
  65. d_lmt = 4 if d_lmt == None else int(d_lmt)
  66. a_lmt = 4 if a_lmt == None else int(a_lmt)
  67. reps = 2 if reps == None else int(reps)
  68. my_map = createDungeon(x,y,seed)
  69. printDungeon(my_map)
  70. for _ in range(reps):
  71. my_map = refineDungeon(my_map, d_lmt, d_lmt)
  72. printDungeon(my_map)
  73. def parseArgs(args):
  74. flags = {
  75. "--height" : 20,
  76. "--width" : 20,
  77. "--seed" : 45,
  78. "--death" : 4,
  79. "--birth" : 4,
  80. "--reps" : 2
  81. }
  82. for flag, default in flags.items():
  83. if flag in args:
  84. flags[flag] = args[args.index(flag) + 1]
  85. return flags
  86. if __name__ == "__main__":
  87. flags = parseArgs(sys.argv)
  88. main(flags["--width"],
  89. flags["--height"],
  90. flags["--seed"],
  91. flags["--death"],
  92. flags["--birth"],
  93. flags["--reps"])