-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
300 lines (255 loc) · 8.41 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
local Map = require("Map")
local Camera = require("camera")
local SIZE = 600
local islandType = 'Radial'
-- local islandType = 'Square'
-- local islandType = 'ROT'
local pointType = 'Square'
local numPoints = 5000
local mapMode = 'Polygons'
local map
local maploader = require("maploader")
local colors = {
OCEAN = {68, 68, 122},
COAST = {51, 51, 90},
LAKESHORE = {34, 85, 136},
LAKE = {51, 102, 153},
MARSH = {47, 102, 102},
ICE = {153, 255, 255},
BEACH = {160, 144, 119},
SNOW = {255, 255, 255},
TUNDRA = {187, 187, 170},
BARE = {136, 136, 136},
SCORCHED = {85, 85, 85},
TAIGA = {153, 170, 119},
SHRUBLAND = {136, 153, 119},
TEMPERATE_DESERT = {201, 210, 155},
TEMPERATE_RAIN_FOREST = {68, 136, 85},
TEMPERATE_DECIDUOUS_FOREST = {103, 148, 89},
GRASSLAND = {136, 170, 85},
SUBTROPICAL_DESERT = {210, 185, 139},
TROPICAL_RAIN_FOREST = {51, 119, 85},
TROPICAL_SEASONAL_FOREST = {85, 153, 68}
}
local generated = false
local nm = {}
local process = 0
local nextProcess = 1
local grid = {}
local circle = {}
function love.load()
circle.x = 100
circle.y = 100
camera = Camera(circle.x, circle.y, nil, nil, Camera.smooth.damped(2))
camera:lockPosition(circle.x, circle.y)
local seed = math.floor(love.math.random() * 10000000)
local variant = 1+math.floor(9*love.math.random())
maploader.newMap(nm, SIZE, islandType, pointType, numPoints, seed, variant)
local function allDone()
print("#nm.data", #nm.data)
-- for _, v in ipairs(nm.data) do
-- print(v[1], v[2])
-- end
local numGrid = math.sqrt(#nm.data)
for i = 1, numGrid do
grid[i] = {}
for j = 1, numGrid do
local p = table.remove(nm.data)
grid[i][j] = p[3]
end
end
generated = true
end
local function oneDone(process_)
if not generated then
process = process_
nextProcess = process_ + 1
end
end
maploader.start(allDone, oneDone)
print(seed)
print(variant)
end
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
local n = SIZE / math.sqrt(numPoints) * 0.5
-- local n2 = n * 2
local n2 = 128 * 2
local line = false
local full = false
local screenWidth, screenHeight = love.graphics.getWidth(), love.graphics.getHeight()
function love.draw()
if not generated then
love.graphics.setColor(255, 255, 255)
love.graphics.print("Loading...", 100, 100)
local separation = 30
local w = screenWidth - 2*separation
local h = 50;
local x,y = separation, screenHeight - separation - h;
love.graphics.rectangle("line", x, y, w, h)
x, y = x + 3, y + 3
w, h = w - 6, h - 7
w = w * (process / 8)
love.graphics.rectangle("fill", x, y, w, h)
return
end
camera:attach()
-- for _, p in pairs(nm.data) do
-- local color = colors[p[3]]
-- if not color then
-- color = {255, 255, 255}
-- end
-- love.graphics.setColor(color)
-- love.graphics.rectangle('fill', p[1] - n, p[2]-n, n*2, n*2)
-- love.graphics.setColor(255, 255, 255)
-- love.graphics.print(tostring(round(p[1], 1))..";;"..tostring(round(p[2], 1)), p[1] - 10, p[2])
-- if line then
-- love.graphics.setColor(255, 255, 255, 60)
-- love.graphics.rectangle('line', p[1]-n, p[2]-n, n*2, n*2)
-- end
-- end
-- for x, v in ipairs(grid) do
-- for y, biome in ipairs(v) do
-- local r_x = (x - 1) * n2
-- local r_y = (y - 1) * n2
-- local color = colors[biome]
-- if not color then
-- color = {255, 255, 255}
-- end
-- love.graphics.setColor(color)
-- love.graphics.rectangle('fill', r_x, r_y, n2, n2)
-- love.graphics.setColor(255, 255, 255)
-- love.graphics.print(tostring(x)..";"..tostring(y), r_x, r_y)
-- if line then
-- love.graphics.setColor(255, 255, 255, 60)
-- love.graphics.rectangle('line', r_x, r_y, n2, n2)
-- end
-- end
-- end
local g_x = math.floor(circle.x / n2) + 1
local g_y = math.floor(circle.y / n2) + 1
-- local bound = math.floor(#grid / 3)
local bound = math.floor(#grid / 5)
local l_x = g_x - bound
local l_y = g_y - bound
local r_x = g_x + bound
local r_y = g_y + bound
if l_x < 1 then l_x = 1 end
if l_y < 1 then l_y = 1 end
local num_grid = #grid
if r_x > num_grid then r_x = num_grid end
if r_y > num_grid then r_y = num_grid end
local function f(a, b)
return (a - b) < bound
end
if f(g_x, l_x) then
r_x = r_x + bound - (g_x - l_x)
end
if f(g_y, l_y) then
r_y = r_y + bound - (g_y - l_y)
end
if f(r_x, g_x) then
l_x = l_x - (bound - (r_x - g_x))
end
if f(r_y, g_y) then
l_y = l_y - (bound - (r_y - g_y))
end
for x = l_x, r_x do
for y = l_y, r_y do
local biome = grid[x][y]
local rr_x = (x - 1) * n2
local rr_y = (y - 1) * n2
local color = colors[biome]
if not color then
color = {255, 255, 255}
end
love.graphics.setColor(color)
love.graphics.rectangle('fill', rr_x, rr_y, n2, n2)
love.graphics.setColor(255, 255, 255)
-- love.graphics.print(tostring(x)..";"..tostring(y), rr_x, rr_y)
if line then
love.graphics.setColor(255, 255, 255, 60)
love.graphics.rectangle('line', rr_x, rr_y, n2, n2)
end
end
end
local color = {155, 155, 155, 100}
love.graphics.setColor(color)
love.graphics.rectangle('fill', (g_x - 1) * n2, (g_y - 1) * n2, n2, n2)
love.graphics.setColor(255, 255, 255, 60)
love.graphics.circle("fill", circle.x, circle.y, 10)
love.graphics.print(tostring(circle.x)..";"..tostring(circle.y), circle.x, circle.y)
camera:detach()
love.graphics.setColor(255, 255, 255)
love.graphics.print("Press 'K' to generate new map", 600, 100)
love.graphics.print("Press 'L' to draw edges", 600, 150)
love.graphics.print("Press 'J' to draw full minimap", 600, 170)
love.graphics.push()
local n3 = 3
love.graphics.setColor(0, 0, 0, 255)
love.graphics.rectangle('fill', 0, 0, n3 * #grid, n3 * #grid)
if full then
l_x = 1
l_y = 1
r_x = #grid
r_y = #grid
end
for x = l_x, r_x do
for y = l_y, r_y do
local biome = grid[x][y]
local rr_x = (x - 1) * n3
local rr_y = (y - 1) * n3
local color = colors[biome]
if not color then
color = {255, 255, 255}
end
love.graphics.setColor(color)
love.graphics.rectangle('fill', rr_x, rr_y, n3, n3)
love.graphics.setColor(255, 255, 255, 100)
-- love.graphics.print(tostring(x)..";"..tostring(y), rr_x, rr_y)
if line then
love.graphics.setColor(255, 255, 255, 60)
love.graphics.rectangle('line', rr_x, rr_y, n3, n3)
end
end
end
love.graphics.setColor(255, 255, 255)
love.graphics.circle('fill', g_x * n3, g_y * n3, n3)
love.graphics.pop()
end
function love.update(dt)
maploader.update()
if process < nextProcess then
process = process + 200.0 / numPoints
end
if love.keyboard.isDown("left") then
circle.x = circle.x - 10
elseif love.keyboard.isDown("right") then
circle.x = circle.x + 10
end
if love.keyboard.isDown("up") then
circle.y = circle.y - 10
elseif love.keyboard.isDown("down") then
circle.y = circle.y + 10
end
camera:lockPosition(circle.x, circle.y)
end
function love.keypressed(k)
if k == "k" then
process = 0
nextProcess = 1
generated = false
nm.data = {}
local seed = math.floor(love.math.random() * 10000000)
local variant = 1+math.floor(9*love.math.random())
maploader.newMap(nm, SIZE, islandType, pointType, numPoints, seed, variant)
print(seed)
print(variant)
elseif k == "l" then
line = not line
elseif k == "j" then
full = not full
end
end