Guest User

Untitled

a guest
Jul 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. MAX_RECURSION = 3
  2. TL = 1
  3. BL = 2
  4. BR = 3
  5. TR = 4
  6.  
  7. grid = {}
  8. grid.w = 60;
  9. grid.h = 12;
  10. grid.max = grid.w * grid.h
  11. grid.pos = function (self, x, y) return (x * self.w + y)+1 end
  12. grid.get = function (self, x, y) return self.data[self:pos(x, y)] end
  13. grid.set = function (self, x, y, d) self.data[self:pos(x,y)] = d end
  14. grid.data = {}
  15. for i = 1, grid.max do grid.data[i] = " " end
  16. rooms = 1
  17.  
  18. function node_c(parent, rect)
  19. n = {}
  20. n.child1 = nil
  21. n.child2 = nil
  22. n.parent = parent
  23. n.rect = rect
  24. n.w = rect[TR].x-rect[TL].x
  25. n.h = rect[BL].y-rect[TL].y if n.h < 0 then n.h = 0 end
  26. n.level = parent.level + 1
  27. n.id = 0
  28. n.vertical = direction_get(n)
  29. node_draw(n)
  30. return n
  31. end
  32.  
  33. function rootnode(rect)
  34. n = {}
  35. n.child1 = nil
  36. n.child2 = nil
  37. n.rect = rect
  38. n.parent = nil
  39. n.w = rect[TR].x-rect[TL].x
  40. n.h = rect[BL].y-rect[TL].y
  41. n.level = 0
  42. n.id = 0
  43. n.vertical = direction_get(n)
  44. node_draw(n)
  45. return n
  46. end
  47.  
  48. function node_draw(node)
  49. local x, y
  50. local rect = node.rect
  51. for y = rect[TL].y, rect[BL].y do
  52. for x = rect[TL].x, rect[TR].x do
  53. if x==rect[TL].x or x==rect[TR].x or y==rect[TL].y or y==rect[BR].y then
  54. grid:set(x, y, "#")
  55. else
  56. grid:set(x, y, ".")
  57. end
  58. end
  59. end
  60. end
  61.  
  62. function node_split(node)
  63. if not node or not (node.level < MAX_RECURSION) then
  64. node.id = rooms
  65. rooms = rooms + 1
  66. return
  67. else
  68. local p
  69. if node.vertical then p = "X" else p = "Y" end
  70. local split = node_splitfrom(node)
  71. if split == nil then return end
  72. local c1, c2, r1, r2
  73. r1,r2 = node_rects(node, split)
  74. node.child1 = node_c(node, r1)
  75. node.child2 = node_c(node, r2)
  76. --draw_it(grid)
  77. node_split(node.child1)
  78. node_split(node.child2)
  79. end
  80. end
  81.  
  82. function direction_get(node)
  83. if math.random(0, 1) == 0 then return true else return false end
  84. end
  85.  
  86. function node_splitfrom(node)
  87. local result
  88. if node.vertical then
  89. local r = math.floor(((math.random(20, 80)/100) * node.w))
  90. if r < 2 or r > node.w - 2 then return nil else return node.rect[TL].x + r end
  91. else
  92. local r = math.floor(((math.random(20, 80)/100) * node.h))
  93. if r < 2 or r > node.h - 2 then return nil else return node.rect[TL].y + r end
  94. end
  95. end
  96.  
  97. function print_rect(rect)
  98. return "TL:"..rect[TL].x..","..rect[TL].y.." BL:"..rect[BL].x..","..rect[BL].y.." BR:"..rect[BR].x..","..rect[BR].y.." TR:"..rect[TR].x..","..rect[TR].y
  99. end
  100.  
  101. function node_rects(node, split)
  102. local rect1, rect2
  103. local t
  104.  
  105. t = node.rect
  106. if node.vertical then
  107. rect1 = {[TL] = t[TL], [BL] = t[BL], [TR] = {x=split,y=t[TR].y}, [BR] = {x=split,y=t[BR].y}}
  108. rect2 = {[TL] = {x=split,y=t[TL].y}, [BL] = {x=split,y=t[BL].y}, [TR] = t[TR], [BR] = t[BR]}
  109. return rect1,rect2
  110. else
  111. rect1 = {[TL]=t[TL],[TR] = t[TR],[BL]={x=t[BL].x,y=split}, [BR]={x=t[BR].x,y=split}}
  112. rect2 = {[TL]={x=t[TL].x,y=split},[TR]={x=t[TR].x,y=split},[BL]=t[BL],[BR] = t[BR]}
  113. return rect1,rect2
  114. end
  115. end
  116.  
  117. function bsp_it(g)
  118. local rect = { --Counter clockwise
  119. [TL] = {x=0,y=0},
  120. [BL] = {x=0,y=g.h},
  121. [BR] = {x=g.w,y=g.h},
  122. [TR] = {x=g.w,y=0}
  123. }
  124. local root = rootnode(rect)
  125. node_split(root)
  126. return root
  127. end
  128.  
  129. function print_it(t)
  130. if(t) then
  131. local spaces = (t.level * 2)
  132. local i
  133. local space = ""
  134. for i = 0, spaces do space = space.." " end
  135. if not t.parent then print(space.."#"..("%X"):format(t.id)) else print(space.."\"..("%X"):format(t.id)) end
  136. print_it(t.child1) print_it(t.child2)
  137. end
  138. end
  139.  
  140. function draw_it(grid)
  141. local line = ""
  142. local x,y
  143. for y = 0, grid.h do
  144. for x = 0, grid.w do
  145. line = ""..line..(grid.data[grid:pos(x,y)] or 0)
  146. end
  147. print(line)
  148. line = ""
  149. end
  150. end
  151.  
  152. math.randomseed(os.time())
  153. test = bsp_it(grid, 3)
  154. print_it(test)
  155. draw_it(grid)
Add Comment
Please, Sign In to add comment