forked from Ezhh/other_worlds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
asteroid_layer_helpers.lua
272 lines (253 loc) · 8.31 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
-- 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 = 3*3*3 -- 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.
-- 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'.
-- 3D Perlin noise 1 for large structures
local np_large = {
offset = 0,
scale = 1,
spread = {x=256, y=128, z=256},
seed = -83928935,
octaves = 5,
persist = 0.6
}
-- 3D Perlin noise 3 for fissures
local np_fissure = {
offset = 0,
scale = 1,
spread = {x=64, y=64, z=64},
seed = -188881,
octaves = 4,
persist = 0.5
}
-- 3D Perlin noise 4 for small structures
local np_small = {
offset = 0,
scale = 1,
spread = {x=128, y=64, z=128},
seed = 1000760700090,
octaves = 4,
persist = 0.6
}
-- 3D Perlin noise 5 for ore selection
local np_ores = {
offset = 0,
scale = 1,
spread = {x=128, y=128, z=128},
seed = -70242,
octaves = 1,
persist = 0.5
}
-- 3D Perlin noise 6 for comet atmosphere
local np_latmos = {
offset = 0,
scale = 1,
spread = {x=256, y=128, z=256},
seed = -83928935,
octaves = 3,
persist = 0.6
}
-- 3D Perlin noise 7 for small comet atmosphere
local np_satmos = {
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"}) 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 YMIN = ymin
local YMAX = ymax
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 t1 = os.clock()
local x1 = maxp.x
local y1 = maxp.y
local z1 = maxp.z
local x0 = minp.x
local y0 = minp.y
local z0 = minp.z
--print ("[asteroid] chunk ("..x0.." "..y0.." "..z0..")")
local sidelen = x1 - x0 + 1 -- chunk side length
--local vplanarea = sidelen ^ 2 -- vertical plane area, used if calculating noise index from x y z
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):get3dMap_flat(minpos)
local nvals3 = minetest.get_perlin_map(np_fissure, chulens):get3dMap_flat(minpos)
local nvals4 = minetest.get_perlin_map(np_small, chulens):get3dMap_flat(minpos)
local nvals5 = minetest.get_perlin_map(np_ores, chulens):get3dMap_flat(minpos)
local nvals6 = minetest.get_perlin_map(np_latmos, chulens):get3dMap_flat(minpos)
local nvals7 = minetest.get_perlin_map(np_satmos, chulens):get3dMap_flat(minpos)
local ni = 1
for z = z0, z1 do -- for each vertical plane do
for y = y0, y1 do -- for each horizontal row do
local vi = area:index(x0, y, z) -- LVM index for first node in x row
for x = x0, x1 do -- for each node do
local noise1abs = math.abs(nvals1[ni])
local noise4abs = math.abs(nvals4[ni])
local comet = false
if nvals6[ni] < -(ASCOT + ATMOT) or (nvals7[ni] < -(SASCOT + ATMOT) and nvals1[ni] < ASCOT) then
comet = true -- comet biome
end
if noise1abs > ASCOT or noise4abs > SASCOT then -- if below surface then
local noise1dep = noise1abs - ASCOT -- noise1dep zero at surface, positive beneath
if math.abs(nvals3[ni]) > FISTS + noise1dep * FISEXP then -- if no fissure then
local noise4dep = noise4abs - SASCOT -- noise4dep zero at surface, positive beneath
if not comet or (comet and (noise1dep > math.random() + ICET or noise4dep > math.random() + ICET)) then
-- asteroid or asteroid materials in comet
if noise1dep >= STOT or noise4dep >= STOT then
-- stone/ores
if math.random(ORECHA) == 2 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 comet 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
-- craters
for ci = 1, CPCHU do -- iterate
local cr = CRMIN + math.floor(math.random() ^ 2 * CRRAN) -- exponential radius
local cx = math.random(minp.x + cr, maxp.x - cr) -- centre x
local cz = math.random(minp.z + cr, maxp.z - cr) -- centre z
local comet = false
local surfy = false
for y = y1, y0 + cr, -1 do
local vi = area:index(cx, y, cz) -- LVM index for node
local 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 surfy and y1 - surfy > 8 then -- if surface found and 8 node space above impact node 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
local vi = area:index(x, y, z) -- LVM index for node
local 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
local 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
local 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