Python script to generate simple "dungeon maps"
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

procgen.py 3.2KB

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