-
Notifications
You must be signed in to change notification settings - Fork 3
/
BSPNode.lua
123 lines (90 loc) · 3.01 KB
/
BSPNode.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
BSPNode = {}
BSPNode.__index = BSPNode
BSPNode._allNodes = {}
-- Tune these values to suit your needs. They give fairly proportional blocks
LANE_WIDTH = 2
MIN_LEAF_SIZE = 3 * LANE_WIDTH
MAX_LEAF_SIZE = math.floor(math.min(globalCols,globalRows) / MIN_LEAF_SIZE)
function BSPNode:new(o)
o = o or {}
setmetatable(o, self)
o.leftChild = nil
o.rightChild = nil
table.insert(BSPNode._allNodes, o)
return o
end
function BSPNode:reset()
BSPNode._allNodes = {}
end
function BSPNode:buildTree()
didSplit = true
-- Build the tree of BSPNodes
while didSplit do
didSplit = false
for _,node in pairs(BSPNode._allNodes) do
if not node.leftChild and not node.rightChild then
-- print(l)
if node.cols > MAX_LEAF_SIZE or node.rows > MAX_LEAF_SIZE then
if node:split() then
table.insert(BSPNode._allNodes, node.leftChild)
table.insert(BSPNode._allNodes, node.rightChild)
didSplit = true
end
end
end
end
end
end
function BSPNode:split()
-- return if this room has already ben split
if self.leftChild ~= nil or self.rightChild ~= nil then
return false
end
splitH = rng:random() > 0.5
if self.cols > self.rows and self.rows / self.cols >= 0.05 then
splitH = false
elseif self.rows > self.cols and self.cols / self.rows >= 0.05 then
splitH = true
end
if splitH then splitDir = "H" else splitDir = "V" end
maxSize = 0
if splitH then maxSize = self.rows else maxSize = self.cols end
maxSize = maxSize - MIN_LEAF_SIZE
-- if the max is smaller than the min then this room can't be split
if maxSize <= MIN_LEAF_SIZE then return false end
split = rng:random(MIN_LEAF_SIZE, maxSize)
-- print("Split: " .. split)
if splitH then
self.leftChild = BSPNode:new({x=self.x, y=self.y, cols=self.cols, rows=split})
self.rightChild = BSPNode:new({x=self.x, y=self.y + split, cols=self.cols, rows=self.rows - split})
else
self.leftChild = BSPNode:new({x=self.x, y=self.y, cols=split, rows=self.rows})
self.rightChild = BSPNode:new({x=self.x + split, y=self.y, cols=self.cols - split, rows=self.rows})
end
-- print("Left: " .. tostring(self.leftChild))
-- print("Right: " .. tostring(self.rightChild))
return true
end
function BSPNode:buildBlocks()
if self.leftChild ~= nil or self.rightChild ~= nil then
if self.leftChild ~= nil then
self.leftChild:buildBlocks()
end
if self.rightChild ~= nil then
self.rightChild:buildBlocks()
end
else
-- We've reached a leaf so create a block with this region
self.block = BSPNode:new({x=self.x + LANE_WIDTH, y=self.y + LANE_WIDTH, cols=self.cols - LANE_WIDTH, rows=self.rows - LANE_WIDTH})
end
end
function BSPNode:tileIndexes()
indexes = {}
for row=self.y, self.y + self.rows - 1 do
for col=self.x, self.x + self.cols - 1 do
--print("looking at " .. row .. "/" .. col)
table.insert(indexes, (globalCols * (row - 1)) + col)
end
end
return indexes
end