-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroid_layer_helpers.lua
309 lines (243 loc) · 8.26 KB
/
asteroid_layer_helpers.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
301
302
303
304
305
306
307
308
309
-- submodule
otherworlds.asteroids = {}
-- Approximate realm limits
local XMIN = -33000
local XMAX = 33000
local ZMIN = -33000
local ZMAX = 33000
local ASCOT = 1.0 -- Large asteroid / comet nucleus noise threshold
local SASCOT = 1.0 -- Small asteroid / comet nucleus noise threshold
local STOT = 0.125 -- Asteroid stone threshold
local COBT = 0.05 -- Asteroid cobble threshold
local GRAT = 0.02 -- Asteroid gravel threshold
local ICET = 0.05 -- Comet ice threshold
local ATMOT = -0.2 -- Comet atmosphere threshold
local FISTS = 0.01 -- Fissure noise threshold at surface. Controls size of fissures and amount / size of fissure entrances
local FISEXP = 0.3 -- Fissure expansion rate under surface
local ORECHA = otherworlds.settings.ore_chance.value -- Ore 1/x chance per stone node
local CPCHU = 0 -- Maximum craters per chunk
local CRMIN = 5 -- Crater radius minimum, radius includes dust and obsidian layers
local CRRAN = 8 -- Crater radius range
local random = math.random
local floor = math.floor
local abs = math.abs
-- Note: for fewer large objects: increase the 'spread' numbers in 'np_large' noise parameters. For fewer small objects do the same in 'np_small'. Then tune size with 'ASCOT'
local np_large = { -- 3D Perlin noise 1 for large structures
offset = 0,
scale = 1,
spread = {x = 256, y = 128, z = 256},
seed = -83928935,
octaves = 5,
persist = 0.6}
local np_fissure = { -- 3D Perlin noise 3 for fissures
offset = 0,
scale = 1,
spread = {x = 64, y = 64, z = 64},
seed = -188881,
octaves = 4,
persist = 0.5}
local np_small = { -- 3D Perlin noise 4 for small structures
offset = 0,
scale = 1,
spread = {x = 128, y = 64, z = 128},
seed = 1000760700090,
octaves = 4,
persist = 0.6}
local np_ores = { -- 3D Perlin noise 5 for ore selection
offset = 0,
scale = 1,
spread = {x = 128, y = 128, z = 128},
seed = -70242,
octaves = 1,
persist = 0.5}
local np_latmos = { -- 3D Perlin noise 6 for comet atmosphere
offset = 0,
scale = 1,
spread = {x = 256, y = 128, z = 256},
seed = -83928935,
octaves = 3,
persist = 0.6}
local np_satmos = { -- 3D Perlin noise 7 for small comet atmosphere
offset = 0,
scale = 1,
spread = {x = 128, y = 64, z = 128},
seed = 1000760700090,
octaves = 2,
persist = 0.6}
-- On dignode function. Atmosphere flows into a dug hole.
minetest.register_on_dignode(function(pos, oldnode, digger)
if minetest.find_node_near(pos, 1, {"asteroid:atmos"})
and minetest.get_node(pos).name == "air" then
minetest.set_node(pos, {name = "asteroid:atmos"})
end
end)
-- Generate on_generated function based on parameters
function otherworlds.asteroids.create_on_generated(YMIN, YMAX, content_ids)
local c_air = content_ids.c_air
local c_stone = content_ids.c_stone
local c_cobble = content_ids.c_cobble
local c_gravel = content_ids.c_gravel
local c_dust = content_ids.c_dust
local c_ironore = content_ids.c_ironore
local c_copperore = content_ids.c_copperore
local c_goldore = content_ids.c_goldore
local c_diamondore = content_ids.c_diamondore
local c_meseore = content_ids.c_meseore
local c_waterice = content_ids.c_waterice
local c_atmos = content_ids.c_atmos
local c_snowblock = content_ids.c_snowblock
local c_obsidian = content_ids.c_obsidian
-- return the function closed over the upvalues we want
return function(minp, maxp, seed)
if minp.x < XMIN or maxp.x > XMAX
or minp.y < YMIN or maxp.y > YMAX
or minp.z < ZMIN or maxp.z > ZMAX then
return
end
local x0, y0, z0 = minp.x, minp.y, minp.z
local x1, y1, z1 = maxp.x, maxp.y, maxp.z
-- local t1 = os.clock()
--print ("[asteroid] chunk ("..x0.." "..y0.." "..z0..")")
local sidelen = x1 - x0 + 1 -- chunk side length
local chulens = {x = sidelen, y = sidelen, z = sidelen}
local minpos = {x = x0, y = y0, z = z0}
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax}
local data = vm:get_data()
local nvals1 = minetest.get_perlin_map(np_large, chulens):get_3d_map_flat(minpos)
local nvals3 = minetest.get_perlin_map(np_fissure, chulens):get_3d_map_flat(minpos)
local nvals4 = minetest.get_perlin_map(np_small, chulens):get_3d_map_flat(minpos)
local nvals5 = minetest.get_perlin_map(np_ores, chulens):get_3d_map_flat(minpos)
local nvals6 = minetest.get_perlin_map(np_latmos, chulens):get_3d_map_flat(minpos)
local nvals7 = minetest.get_perlin_map(np_satmos, chulens):get_3d_map_flat(minpos)
local ni = 1
local noise1abs, noise4abs, comet, noise1dep, noise4dep, vi
for z = z0, z1 do -- for each vertical plane do
for y = y0, y1 do -- for each horizontal row do
vi = area:index(x0, y, z) -- LVM index for first node in x row
for x = x0, x1 do -- for each node do
noise1abs = abs(nvals1[ni])
noise4abs = abs(nvals4[ni])
comet = false
-- comet biome
if nvals6[ni] < -(ASCOT + ATMOT)
or (nvals7[ni] < -(SASCOT + ATMOT) and nvals1[ni] < ASCOT) then
comet = true
end
-- if below surface
if noise1abs > ASCOT or noise4abs > SASCOT then
-- noise1dep zero at surface, positive beneath
noise1dep = noise1abs - ASCOT
-- if no fissure
if abs(nvals3[ni]) > FISTS + noise1dep * FISEXP then
-- noise4dep zero at surface, positive beneath
noise4dep = noise4abs - SASCOT
if not comet
or (comet and (noise1dep > random() + ICET
or noise4dep > random() + ICET)) then
-- asteroid or asteroid materials in comet
if noise1dep >= STOT or noise4dep >= STOT then
-- stone/ores
if random(ORECHA) == 1 then
if nvals5[ni] > 0.6 then
data[vi] = c_goldore
elseif nvals5[ni] < -0.6 then
data[vi] = c_diamondore
elseif nvals5[ni] > 0.2 then
data[vi] = c_meseore
elseif nvals5[ni] < -0.2 then
data[vi] = c_copperore
else
data[vi] = c_ironore
end
else
data[vi] = c_stone
end
elseif noise1dep >= COBT or noise4dep >= COBT then
data[vi] = c_cobble
elseif noise1dep >= GRAT or noise4dep >= GRAT then
data[vi] = c_gravel
else
data[vi] = c_dust
end
else -- comet materials
if noise1dep >= ICET or noise4dep >= ICET then
data[vi] = c_waterice
else
data[vi] = c_snowblock
end
end
elseif comet then -- fissures, if comet then add atmosphere
data[vi] = c_atmos
end
elseif comet then -- if comet atmosphere then
data[vi] = c_atmos
end
ni = ni + 1
vi = vi + 1
end
end
end
local cr, cx, cz, comet, surfy, vi, nr, nodeid
-- craters
for ci = 1, CPCHU do -- iterate
-- exponential radius
cr = CRMIN + floor(random() ^ 2 * CRRAN)
cx = random(minp.x + cr, maxp.x - cr) -- centre x
cz = random(minp.z + cr, maxp.z - cr) -- centre z
comet = false
surfy = false
for y = y1, y0 + cr, -1 do
vi = area:index(cx, y, cz) -- LVM index for node
nodeid = data[vi]
if nodeid == c_dust or nodeid == c_gravel or nodeid == c_cobble then
surfy = y
break
elseif nodename == c_snowblock or nodename == c_waterice then
comet = true
surfy = y
break
end
end
-- if surface found and 8 node space above impact node then
if surfy and y1 - surfy > 8 then
for x = cx - cr, cx + cr do -- for each plane do
for z = cz - cr, cz + cr do -- for each column do
for y = surfy - cr, surfy + cr do -- for each node do
-- LVM index for node
vi = area:index(x, y, z)
nr = ((x - cx) ^ 2 + (y - surfy) ^ 2 + (z - cz) ^ 2) ^ 0.5
if nr <= cr - 2 then
if comet then
data[vi] = c_atmos
else
data[vi] = c_air
end
elseif nr <= cr - 1 then
nodeid = data[vi]
if nodeid == c_gravel or nodeid == c_cobble
or nodeid == c_stone or nodeid == c_diamondore
or nodeid == c_goldore or nodeid == c_meseore
or nodeid == c_copperore or nodeid == c_ironore then
data[vi] = c_dust
end
elseif nr <= cr then
nodeid = data[vi]
if nodeid == c_cobble or nodeid == c_stone then
data[vi] = c_obsidian -- obsidian buried under dust
end
end
end
end
end
end
end
vm:set_data(data)
vm:set_lighting({day = 0, night = 0})
vm:calc_lighting()
vm:write_to_map(data)
-- local chugent = math.ceil((os.clock() - t1) * 1000)
--print ("[asteroid] time "..chugent.." ms")
data = nil
end
end