Advertisement
Guest User

Wych's maze implementation

a guest
Apr 12th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.35 KB | None | 0 0
  1. extends Node
  2.  
  3. var floorRect
  4.  
  5. var _rooms = []
  6.  
  7. var windingPercent = 40 #change this
  8.  
  9. var _currentRegion = 0
  10.  
  11. var directionsCardinal = {"n": Vector2(0,-1), "e": Vector2(1,0), "s": Vector2(0,1), "w": Vector2(-1,0) }
  12. var directionsIntercardinal = { "ne": Vector2(1,-1), "se": Vector2(1,1), "sw": Vector2(-1,1), "nw": Vector2(-1,-1)}
  13. var directionsAll = {"none":Vector2(0,0)}
  14.  
  15. var cellSize = 0
  16.  
  17.    
  18.  
  19. func gen(_floorRect, _cellSize):
  20.    
  21.     var _rooms = []
  22.     for direct in directionsCardinal.keys():
  23.         directionsAll[direct] = directionsCardinal[direct]
  24.     for direct in directionsIntercardinal.keys():
  25.         directionsAll[direct] = directionsIntercardinal[direct]
  26.    
  27.     floorRect = _floorRect
  28.    
  29.     cellSize = _cellSize
  30.     tileFill(_floorRect, "wall")
  31.     print("tileFill tile index:",get_node("/root/Main/dungeonLevel").get_cellv(Vector2(0,0)))
  32.    
  33.  
  34.     roomGen(_floorRect)
  35.  
  36.     var y = 1
  37.     while y < floorRect.end.y:
  38.         y +=2
  39.         var x = 1
  40.         while x < floorRect.end.x:
  41.             var pos = Vector2(x,y)
  42.             if get_node("/root/Main/dungeonLevel").get_cellv(pos) != 0:
  43.                     x +=2
  44.                     continue
  45.             elif get_node("/root/Main/dungeonLevel").get_cellv(pos + directionsCardinal["s"]) != 0 or get_node("/root/Main/dungeonLevel").get_cellv(pos + directionsCardinal["n"]) != 0 or get_node("/root/Main/dungeonLevel").get_cellv(pos + directionsCardinal["e"]) != 0 or get_node("/root/Main/dungeonLevel").get_cellv(pos + directionsCardinal["w"]) != 0:
  46.                 x +=2
  47.                 continue
  48.             else:
  49.                 growMaze(pos)
  50.                 x +=2
  51.  
  52.  
  53. func tileFill(areaRect, tileType):
  54.     var areaRectEnd = areaRect.end
  55.    
  56.     var x = areaRect.position.x
  57.     var y = areaRect.position.y
  58.    
  59.     var endX = areaRect.end.x + 1
  60.    
  61.     var endY = areaRect.end.y + 1
  62.    
  63.     var tile = tileIndex(tileType)
  64.     while x < endX :
  65.         while y < endY :
  66.             var currentCell = get_node("/root/Main/dungeonLevel").set_cell(x,y,tile)
  67.             y += 1
  68.         y = areaRect.position.y
  69.         x += 1
  70.        
  71.        
  72. func roomGen(floorRect):
  73.     var numRoomTries = 10
  74.     var roomExtraSize = 2
  75.     var i = 0
  76.     for i in numRoomTries + 1:
  77.         i += 1
  78.     ## might have to change this to round
  79.         var size = int(rand_range(1,3 + roomExtraSize)) * 2 + 1
  80.         var rectangularity = int(rand_range(0,1 + size / 2)) * 2
  81.         var width = size
  82.         var height = size
  83.         var rngPick = int(rand_range(1,2))
  84.         if rngPick == 1:
  85.             width += rectangularity
  86.         else:
  87.             height += rectangularity
  88.            
  89.         var x = int(rand_range(0, floorRect.end.x - width) / 2) * 2 + 1
  90.         var y = int(rand_range(0, floorRect.end.y - height) / 2) * 2 + 1
  91.        
  92.         var room = Rect2(x,y,width,height)
  93.         var overlaps = false
  94.        
  95.         for other in _rooms:
  96.             #print(int(room.position.distance_to(other.position)))
  97.             #print("room end",room.end)
  98.             #print("room start",room.position)
  99.             #if room.end.distance_to(other.end) <= 0 or room.position.distance_to(other.position) <= 0:
  100.             if intersects(room.position, room.end, other.position, other.end):
  101.                 overlaps = true
  102.                 break
  103.  
  104.         if overlaps:
  105.             continue
  106.  
  107.         _rooms.append(room)
  108.  
  109.         _currentRegion +=1
  110.        
  111.         tileFill(room, "floor")
  112.        
  113.         #add code for making regios here
  114.  
  115. func intersects(r1from, r1to, r2from, r2to):
  116.     return !(r2from.x >= r1to.x +2  || r2to.x <= r1from.x -2 || r2from.y >= r1to.y +2 || r2to.y <= r1from.y -2)
  117.  
  118. func tileIndex(tileType):
  119.     var tileDict = {"wall": 0, "floor": 1, "other":2, "water":3}
  120.     if tileType in tileDict:
  121.         return tileDict[tileType]
  122.     else:
  123.         print("ERROR: INVALID TILE")
  124.         get_tree().quit()
  125.  
  126. func _direction(direction): #might need to make this function first
  127.     #WHEN YOU HANDLE THIS LATER YOU JUST HAVE TO ADD THE DIRECTION TO THE POS/VECTOR AND IT MOVES IT IN THAT DIRECTION
  128.     pass
  129.  
  130.  
  131.  
  132. func growMaze(start):
  133.     var cells = [] #these are all vects
  134.     var lastDir
  135.     _currentRegion +=1
  136.     _carve(start, "other")
  137.  
  138.     cells.append(start)
  139.    
  140.     while not cells.empty():
  141.         #print("cells: ", len(cells))
  142.         var cell = cells[-1]
  143.         var unmadeCells = []
  144.  
  145.         for dir in directionsCardinal:
  146.             #print(directionsCardinal[dir])
  147.             if _canCarve(cell, directionsCardinal[dir]):
  148.                 unmadeCells.append(directionsCardinal[dir])
  149.         print("unmade cells len",len(unmadeCells))
  150.         if not unmadeCells.empty():
  151.             var dir
  152.             var rngChoice = int(randi() % 101)
  153.             if unmadeCells.has(lastDir) and rngChoice > windingPercent:
  154.                 dir = lastDir
  155.             else:
  156.                 dir = unmadeCells[int(randi()%len(unmadeCells))] #minus 1? plus 1??
  157.            
  158.            
  159.             _carve(cell+dir, "water")
  160.             _carve(cell+dir*2, "water")
  161.             cells.append(cell+dir*2)
  162.             lastDir = dir
  163.         else:
  164.             cells.pop_back()
  165.             lastDir = null
  166.  
  167.  
  168. func _canCarve(pos, direction):
  169.     var _floorRect = floorRect
  170.     print("just pos ",pos)
  171.     print("just direction ",direction)
  172.     print("pos + direction ",pos + direction)
  173.     print("pos + direction * 3 ",pos + direction * 3)
  174.     print("pos + direction * 3 + north",pos + direction * 3 + directionsCardinal["n"])
  175.     if not 0 < pos.x + direction.x and not pos.x + direction.x < floorRect.end.x and not 0 < pos.y + direction.y and not pos.y + direction.y < floorRect.end.y:
  176.         return false
  177.        
  178.     if get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 2) == 0:
  179.         if direction == directionsCardinal["n"] or direction == directionsCardinal["s"]:
  180.             var directionTile1 = get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 3 + directionsCardinal["e"])
  181.             var directionTile2 = get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 3 + directionsCardinal["w"])
  182.             if directionTile1 != 0:
  183.                 return false
  184.             elif directionTile2 != 0:
  185.                 return false
  186.             else:
  187.                 print("can carve? intersects",intersects(pos + direction * 3,pos + direction * 3, floorRect.position,floorRect.end))
  188.                 print("direction tiles:", directionTile1, directionTile2)
  189.                 return true
  190.                
  191.         elif direction == directionsCardinal["e"] or direction == directionsCardinal["w"]:
  192.             var directionTile1 = get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 3 + directionsCardinal["n"])
  193.             var directionTile2 = get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 3 + directionsCardinal["s"])
  194.             if directionTile1 != 0:
  195.                 return false
  196.             elif directionTile2 != 0:
  197.                 return false
  198.             else:
  199.                 print("can carve? intersects",intersects(pos + direction * 3,pos + direction * 3, floorRect.position,floorRect.end))
  200.                 print("direction tiles:", directionTile1, directionTile2)
  201.                 return true
  202.     else:
  203.         return false
  204.  
  205. func _carve(pos, tileType = "floor"):
  206.     get_node("/root/Main/dungeonLevel").set_cellv(pos,tileIndex(tileType))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement