-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.lua
209 lines (181 loc) · 6.61 KB
/
generator.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require('xy_map')
require('nodes')
require('player')
-------------------------------------------------------------------------------
-- World generator
-------------------------------------------------------------------------------
generator = {}
generator.storage = newXYMap()
generator.toDoLater = {}
GENERATOR_DISTANCE = 50
STONE_FILL_DISTANCE = 20
function generator:add(x, y, data)
table.insert(generator.toDoLater, function() generator.storage:safeAdd(x, y, data) end)
end
function generator:remove(x, y)
table.insert(generator.toDoLater, function() generator.storage:remove(x, y) end)
end
function generator:generate()
generator.storage:forEach(function(x,y,node)
if (node.ignore_player or math.abs(x - player.x) < GENERATOR_DISTANCE and math.abs(y - player.y) < GENERATOR_DISTANCE) then
node.action(x,y,node)
end
end)
for _, func in ipairs(generator.toDoLater) do
func()
end
generator.toDoLater = {}
end
function generator:placeWall(x, y)
nodes:safeAddNode(x, y, "stone_wall")
end
local function numOpenNeighbours(x, y)
local count = 0
for dx = -1, 1 do
for dy = -1, 1 do
if (nodes:isWalkable(x + dx, y + dy)) then
count = count + 1
end
end
end
return count
end
function generator:placeFloor(x, y)
if (math.random() < 0.9 and numOpenNeighbours(x, y) >= 6) then
nodes:addNode(x, y, "spikes")
else
nodes:addNode(x, y, "stone_floor")
if (math.random() > 0.999) then
candles:add(x, y)
elseif (math.random() > 0.98) then
coins:add(x, y)
end
if (math.random() > 0.992) then
if (math.random() > 0.8) then
ghosts:addGreyGhost(x, y)
else
ghosts:addPinkGhost(x, y)
end
end
end
end
-- Begin generating a maze at (x, y)
function generator:addMaze(x, y)
generator:add(x, y, {
action = function()
generator:remove(x, y)
for dx = -1,1 do
for dy = -1,1 do
generator:placeFloor(x + dx,y + dy)
end
end
generator:placeWall(x+2,y+2)
generator:placeWall(x+2,y-2)
generator:placeWall(x-2,y+2)
generator:placeWall(x-2,y-2)
local function generateNextNode(generate, dx, dy)
local function generateBorders(wall)
local func = function(x,y)
if (wall) then
generator:placeWall(x,y)
else
generator:placeFloor(x,y)
end
end
if (math.random() > 0.4) then
if (dx == 0) then
func(x, y + dy / 2)
func(x - 1, y + dy / 2)
func(x + 1, y + dy / 2)
else
func(x + dx / 2, y)
func(x + dx / 2, y - 1)
func(x + dx / 2, y + 1)
end
else
if (dx == 0) then
func(x, y + dy / 2)
generator:placeWall(x - 1, y + dy / 2)
generator:placeWall(x + 1, y + dy / 2)
else
func(x + dx / 2, y)
generator:placeWall(x + dx / 2, y - 1)
generator:placeWall(x + dx / 2, y + 1)
end
end
end
if (generate and not nodes:contains(x + dx, y + dy)) then
generateBorders(false)
generator:addMaze(x + dx, y + dy)
else
generateBorders(true)
end
return generate
end
local generated_nodes = 1
local function generateNextNodeProbabilityDone(dx, dy)
local generate
if (generator.storage.size < 20) then
generate = math.random() < 1 / generated_nodes
elseif (generator.storage.size < 10) then
generate = true
else
generate = math.random() < 0.6 / generated_nodes
end
if (generate) then
if (generator.storage.size < 20) then
generated_nodes = generated_nodes + 1
else
generated_nodes = generated_nodes * 3
end
end
return generateNextNode(
generate,
4 * dx,
4 * dy
)
end
local generationQueue = {
function() generateNextNodeProbabilityDone(1, 0) end,
function() generateNextNodeProbabilityDone(-1,0) end,
function() generateNextNodeProbabilityDone(0, 1) end,
function() generateNextNodeProbabilityDone(0,-1) end
}
while (#generationQueue ~= 0) do
local index = math.random(#generationQueue)
generationQueue[index]()
table.remove(generationQueue, index)
end
end
})
end
function generator:addCave(x, y, points)
generator:add(x, y, {
ignore_player = false,
points = points or math.pow(2, 50),
action = function(x, y, node)
if (nodes:get(x,y)) then
generator:remove(x, y)
end
local points = node.points
if (points == 1) then
generator:remove(x, y)
if (x % 4 == 0 and y % 4 == 0) then
generator:addMaze(x,y)
elseif (math.random() < 1 * math.exp(-generator.size/20)) then
generator:addCave(x, y, math.pow(2, math.random(60) + 10))
end
else
nodes:safeAddNode(x, y, "stone_floor")
node.points = points / 2
local generatorOptions = {
function() generator:addCave(x+1, y, points / 2) end,
function() generator:addCave(x-1, y, points / 2) end,
function() generator:addCave(x, y+1, points / 2) end,
function() generator:addCave(x, y-1, points / 2) end
}
generatorOptions[math.random(#generatorOptions)]()
end
end
})
end