-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk2d.lua
225 lines (187 loc) · 5.48 KB
/
chunk2d.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
--[[
MIT License
Copyright (c) 2019 Guthen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
module( "Chunk2D", package.seeall )
author = "Guthen"
version = "1.0.0"
local seed = 0
local cellSize = 8
local chunkW, chunkH = 600, 800
local surfaceY = 30
local minSurfaceY = chunkH/cellSize - 15
local maxSurfaceY = 5
local fillIDs =
{
[1] = 1,
[2] = 2,
}
math.randomseed( seed )
--[[-------------------------------------------------------------------------
-- > get config
---------------------------------------------------------------------------]]
function getSeed()
return seed
end
function getCellSize()
return cellSize
end
function getChunkSize()
return chunkW, chunkH
end
function getSurfaceValues()
return surfaceY, minSurfaceY, maxSurfaceY
end
function getFillSurfaces()
return fillIDs
end
--[[-------------------------------------------------------------------------
-- > config
---------------------------------------------------------------------------]]
function set( _seed, _cellSize, _chunkW, _chunkH, _startY, _minY, _maxY )
seed = _seed or seed
cellSize = _cellSize or cellSize
chunkW = _chunkW or chunkW
chunkH = _chunkH or chunkH
surfaceY = _startY or surfaceY
minSurfaceY = _minY or minSurfaceY
maxSurfaceY = _maxY or maxSurfaceY
if _seed then
math.randomseed( seed )
end
end
function setFillID( _IDs )
fillIDs = _IDs or fillIDs
end
--[[-------------------------------------------------------------------------
-- > chunks & world
---------------------------------------------------------------------------]]
-- > Create Empty Chunk
function createEmptyChunk()
local _chunk = {}
for y = 0, chunkH/cellSize do
_chunk[y] = {}
for x = 0, chunkW/cellSize do
_chunk[y][x] = 0
end
end
return _chunk
end
-- > Get SurfaceY Position of the '_chunk' by given '_x'
function getSurfaceY( _chunk, _x )
if _x < 0 then return end
for y, yv in pairs( _chunk ) do
for x, xv in pairs( yv ) do
if _x == x then
if xv > 0 then return y end
end
end
end
end
-- > Create & Generate a chunk by using the '_lastChunk'
function generateChunk( _chunk, _lastChunk )
local function fillChunk()
for x = 0, chunkW/cellSize do
local y = getSurfaceY( _chunk, x )
if y then
local b = 0
local surface = fillIDs[2]
for _y = y+1, H/cellSize do
_chunk[_y][x] = surface
b = b + 1
if b > 1 and fillIDs[b] then
surface = fillIDs[b]
end
end
end
end
end
for x = 0, chunkW/cellSize do -- generate the surface
local _y
if _lastChunk and x == 1 then
_y = getSurfaceY( _lastChunk, chunkW/cellSize )
else
_y = getSurfaceY( _chunk, x-1 )
end
if _y then
local offset = math.random( -1, 1 ) -- get the Y offset
if _chunk[_y + offset] and _chunk[_y + offset][x] then -- if it's exists
if _y + offset < maxSurfaceY or _y + offset > minSurfaceY then
offset = 0
end
_chunk[_y + offset][x] = fillIDs[1]
end
elseif not _lastChunk then
_chunk[surfaceY][x] = fillIDs[1]
end
end
smoothChunk( _chunk, _lastChunk )
fillChunk()
end
-- > Create & Generate a world of 'nChunks' chunks
function generateWorld( nChunks )
local world = {}
for i = 1, nChunks do
world[i] = createEmptyChunk()
generateChunk( world[i], world[i-1] )
end
return world
end
-- > Smooth the chunk
function smoothChunk( _chunk, _lastChunk )
for x = 0, chunkW/cellSize do -- smoothness
local activeChunk = _chunk
local y, _y, __y, ___y
y = getSurfaceY( _chunk, x )
if _lastChunk and not _chunk[x-1] then
_y = getSurfaceY( _lastChunk, chunkW/cellSize-1 )
activeChunk = _lastChunk
else
_y = getSurfaceY( _chunk, x-1 )
end
if _lastChunk and not _chunk[x-2] then
__y = getSurfaceY( _lastChunk, chunkW/cellSize-2 )
activeChunk = _lastChunk
else
__y = getSurfaceY( _chunk, x-2 )
end
if _lastChunk and not _chunk[x-3] then
___y = getSurfaceY( _lastChunk, chunkW/cellSize-3 )
activeChunk = _lastChunk
else
___y = getSurfaceY( _chunk, x-3 )
end
if y and _y and __y then
if y == __y and not (_y == y) and math.abs( y - _y ) <= 1 and activeChunk[y] then -- if : '0 - 1 - 0' or : '1 - 0 - 1'
if _y and y and _y < y then activeChunk[_y][x-1] = 0 end
activeChunk[y][x-1] = 1
elseif y == ___y and _y == __y and not y == _y then
activeChunk[y][x-1] = 2
activeChunk[y][x-2] = 2
end
end
end
end
-- Smooth all of the world chunks
function smoothWorld( world )
for id = 1, #world do -- smoothness
local curChunk = world[id]
local lastChunk = world[id-1]
smoothChunk( curChunk, lastChunk )
end
end