This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
forked from paramat/moonrealm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
567 lines (493 loc) · 16 KB
/
init.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
-- Parameters
-- These are given to a player who spawns in a shell
-- To give each player items in their inventory, try the 'give_initial_stuff' mod
local GIVE_INITIAL_STUFF = { "moonrealm:spacesuit 4", "moonrealm:sapling 4",
"moonrealm:airlock 4", "moonrealm:airgen 4",
"moonrealm:hlsource 4", "default:apple 64",
"default:pick_diamond 4", "default:axe_diamond 4",
"default:shovel_diamond 4"
}
-- Set to true to make moonrealm:soil actually default:dirt. This means default:dirt
-- will dry into moonrealm:dust if no moonrealm:hlsource is nearby
SOIL_IS_DIRT = false
-- Allow each player to spawn in their own shell. If false, normal spawn applies.
-- Try setting to false for public servers
local SPAWN_WITH_SHELL = true
local YMIN = -8000 -- Approx lower limit
local GRADCEN = 1 -- Gradient centre / terrain centre average level
local YMAX = 8000 -- Approx upper limit
local XMIN = -8000 -- Approx horizontal limits
local XMAX = 8000
local ZMIN = -8000
local ZMAX = 8000
local CENAMP = 64 -- Grad centre amplitude, terrain centre is varied by this
local HIGRAD = 128 -- Surface generating noise gradient above gradcen, controls depth of upper terrain
local LOGRAD = 128 -- Surface generating noise gradient below gradcen, controls depth of lower terrain
local HEXP = 0.5 -- Noise offset exponent above gradcen, 1 = normal 3D perlin terrain
local LEXP = 2 -- Noise offset exponent below gradcen
local STOT = 0.04 -- Stone density threshold, depth of dust
local ICECHA = 1 / (13*13*13) -- Ice chance per dust node at terrain centre, decreases with altitude
local ICEGRAD = 128 -- Ice gradient, vertical distance for no ice
local ORECHA = 7*7*7 -- Ore 1/x chance per stone node
local TFIS = 0.01 -- Fissure threshold. Controls size of fissures
local FOOT = true -- Footprints in dust
-- 3D noise for terrain
local np_terrain = {
offset = 0,
scale = 1,
spread = {x=512, y=512, z=512},
seed = 58588900033,
octaves = 6,
persist = 0.67
}
-- 3D noise for alt terrain, 414 / 256 = golden ratio
local np_terralt = {
offset = 0,
scale = 1,
spread = {x=414, y=414, z=414},
seed = 13331930910,
octaves = 6,
persist = 0.67
}
-- 3D noise for smooth terrain
local np_smooth = {
offset = 0,
scale = 1,
spread = {x=828, y=828, z=828},
seed = 113,
octaves = 4,
persist = 0.4
}
-- 3D noise for fissures
local np_fissure = {
offset = 0,
scale = 1,
spread = {x=256, y=256, z=256},
seed = 8181112,
octaves = 5,
persist = 0.5
}
-- 3D noise for faults
local np_fault = {
offset = 0,
scale = 1,
spread = {x=414, y=828, z=414},
seed = 14440002,
octaves = 4,
persist = 0.5
}
-- 2D noise for terrain centre
local np_gradcen = {
offset = 0,
scale = 1,
spread = {x=1024, y=1024, z=1024},
seed = 9344,
octaves = 4,
persist = 0.4
}
-- 2D noise for terrain blend
local np_terblen = {
offset = 0,
scale = 1,
spread = {x=2048, y=2048, z=2048},
seed = -13002,
octaves = 3,
persist = 0.4
}
-- Do files
dofile(minetest.get_modpath("moonrealm").."/nodes.lua")
dofile(minetest.get_modpath("moonrealm").."/functions.lua")
-- Set mapgen parameters
minetest.register_on_mapgen_init(function(mgparams)
minetest.set_mapgen_params({mgname="singlenode", water_level=-32000})
end)
-- Player positions
local player_pos = {}
local player_pos_previous = {}
minetest.register_on_joinplayer(function(player)
player_pos_previous[player:get_player_name()] = {x = 0, y = 0, z = 0}
end)
minetest.register_on_leaveplayer(function(player)
player_pos_previous[player:get_player_name()] = nil
end)
-- Globalstep function
minetest.register_globalstep(function(dtime)
for _, player in ipairs(minetest.get_connected_players()) do
if player_pos_previous[player:get_player_name()] ~= nil and -- footprints
FOOT and math.random() < 0.3 then
local pos = player:getpos()
player_pos[player:get_player_name()] = {
x = math.floor(pos.x + 0.5),
y = math.floor(pos.y + 0.2),
z = math.floor(pos.z + 0.5)
}
local p_ground = {
x = math.floor(pos.x + 0.5),
y = math.floor(pos.y + 0.4),
z = math.floor(pos.z + 0.5)
}
local n_ground = minetest.get_node(p_ground).name
local p_groundpl = {
x = math.floor(pos.x + 0.5),
y = math.floor(pos.y - 0.5),
z = math.floor(pos.z + 0.5)
}
if player_pos[player:get_player_name()].x ~=
player_pos_previous[player:get_player_name()].x or
player_pos[player:get_player_name()].y <
player_pos_previous[player:get_player_name()].y or
player_pos[player:get_player_name()].z ~=
player_pos_previous[player:get_player_name()].z then
if n_ground == "moonrealm:dust" then
if math.random() < 0.5 then
minetest.add_node(
p_groundpl,
{name = "moonrealm:dustprint1"}
)
else
minetest.add_node(
p_groundpl,
{name = "moonrealm:dustprint2"}
)
end
end
end
player_pos_previous[player:get_player_name()] = {
x = player_pos[player:get_player_name()].x,
y = player_pos[player:get_player_name()].y,
z = player_pos[player:get_player_name()].z
}
end
if math.random() < 0.1 then -- spacesuit restores breath
if player:get_inventory():contains_item("main", "moonrealm:spacesuit") and
player:get_breath() < 10 then
player:set_breath(10)
end
end
if math.random() > 0.99 then -- set gravity, skybox and override light
local pos = player:getpos()
if pos.y > YMIN and pos.y < YMAX then -- entering realm
player:set_physics_override(1, 0.6, 0.2) -- speed, jump, gravity
local skytextures = {
"moonrealm_posy.png",
"moonrealm_negy.png",
"moonrealm_posz.png",
"moonrealm_negz.png",
"moonrealm_negx.png",
"moonrealm_posx.png",
}
player:set_sky({r=0, g=0, b=0, a=0}, "skybox", skytextures)
player:override_day_night_ratio(1)
else -- on leaving realm
player:set_physics_override(1, 1, 1)
player:set_sky({}, "regular", {})
player:override_day_night_ratio(nil)
end
end
end
end)
-- Initialize noise objects to nil
local nobj_terrain = nil
local nobj_terralt = nil
local nobj_smooth = nil
local nobj_fissure = nil
local nobj_fault = nil
local nobj_terblen = nil
local nobj_gradcen = nil
-- On generated function
minetest.register_on_generated(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
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
local data = vm:get_data()
local c_air = minetest.get_content_id("air")
local c_ignore = minetest.get_content_id("ignore")
local c_mese = minetest.get_content_id("moonrealm:mese")
local c_mrironore = minetest.get_content_id("moonrealm:ironore")
local c_mrcopperore = minetest.get_content_id("moonrealm:copperore")
local c_mrgoldore = minetest.get_content_id("moonrealm:goldore")
local c_mrdiamondore = minetest.get_content_id("moonrealm:diamondore")
local c_mrstone = minetest.get_content_id("moonrealm:stone")
local c_waterice = minetest.get_content_id("moonrealm:waterice")
local c_dust = minetest.get_content_id("moonrealm:dust")
local c_vacuum = minetest.get_content_id("moonrealm:vacuum")
local chulens = x1 - x0 + 1
local pmaplens2d = {x = chulens, y = chulens, z = 1}
local pmaplens3d = {x = chulens, y = chulens, z = chulens}
local minpos2d = {x = x0, y = z0}
local minpos3d = {x = x0, y = y0, z = z0}
nobj_terrain = nobj_terrain or minetest.get_perlin_map(np_terrain, pmaplens3d)
nobj_terralt = nobj_terralt or minetest.get_perlin_map(np_terralt, pmaplens3d)
nobj_smooth = nobj_smooth or minetest.get_perlin_map(np_smooth, pmaplens3d)
nobj_fissure = nobj_fissure or minetest.get_perlin_map(np_fissure, pmaplens3d)
nobj_fault = nobj_fault or minetest.get_perlin_map(np_fault, pmaplens3d)
nobj_terblen = nobj_terblen or minetest.get_perlin_map(np_terblen, pmaplens2d)
nobj_gradcen = nobj_gradcen or minetest.get_perlin_map(np_gradcen, pmaplens2d)
local nvals_terrain = nobj_terrain:get3dMap_flat(minpos3d)
local nvals_terralt = nobj_terralt:get3dMap_flat(minpos3d)
local nvals_smooth = nobj_smooth :get3dMap_flat(minpos3d)
local nvals_fissure = nobj_fissure:get3dMap_flat(minpos3d)
local nvals_fault = nobj_fault :get3dMap_flat(minpos3d)
local nvals_terblen = nobj_terblen:get2dMap_flat(minpos2d)
local nvals_gradcen = nobj_gradcen:get2dMap_flat(minpos2d)
local ni3d = 1
local ni2d = 1
local stable = {}
for z = z0, z1 do
local viu = area:index(x0, y0 - 1, z)
for x = x0, x1 do
local si = x - x0 + 1
local nodid = data[viu]
if nodid == c_vacuum then
stable[si] = false
else
stable[si] = true
end
viu = viu + 1
end
for y = y0, y1 do
local vi = area:index(x0, y, z) -- LVM index for first node in x row
local icecha = ICECHA * (1 + (GRADCEN - y) / ICEGRAD)
for x = x0, x1 do
local nodid = data[vi]
local empty = (nodid == c_air or nodid == c_ignore)
local grad
local density
local si = x - x0 + 1
local terblen = math.max(math.min(math.abs(nvals_terblen[ni2d]) * 4,
1.5), 0.5) - 0.5
local gradcen = GRADCEN + nvals_gradcen[ni2d] * CENAMP
if y > gradcen then
grad = -((y - gradcen) / HIGRAD) ^ HEXP
else
grad = ((gradcen - y) / LOGRAD) ^ LEXP
end
if nvals_fault[ni3d] >= 0 then
density = (nvals_terrain[ni3d] +
nvals_terralt[ni3d]) / 2 * (1 - terblen)
+ nvals_smooth[ni3d] * terblen + grad
else
density = (nvals_terrain[ni3d] -
nvals_terralt[ni3d]) / 2 * (1 - terblen)
- nvals_smooth[ni3d] * terblen + grad
end
if density > 0 and empty then -- if terrain and node empty
local nofis = false
if math.abs(nvals_fissure[ni3d]) > TFIS then
nofis = true
end
if density >= STOT and nofis then -- stone, ores
if math.random(ORECHA) == 2 then
local osel = math.random(25)
if osel == 25 then
data[vi] = c_mese
elseif osel >= 22 then
data[vi] = c_mrdiamondore
elseif osel >= 19 then
data[vi] = c_mrgoldore
elseif osel >= 10 then
data[vi] = c_mrcopperore
else
data[vi] = c_mrironore
end
else
data[vi] = c_mrstone
end
stable[si] = true
elseif density < STOT then -- fine materials
if nofis and stable[si] then
if math.random() < icecha then
data[vi] = c_waterice
else
data[vi] = c_dust
end
else -- fissure
data[vi] = c_vacuum
stable[si] = false
end
else -- fissure or unstable missing node
data[vi] = c_vacuum
stable[si] = false
end
else -- vacuum or spawn egg
if empty then
data[vi] = c_vacuum
end
stable[si] = false
end
ni3d = ni3d + 1
ni2d = ni2d + 1
vi = vi + 1
end
ni2d = ni2d - chulens
end
ni2d = ni2d + chulens
end
vm:set_data(data)
vm:set_lighting({day=0, night=0}) -- not "nolight" because mapgen does not run in all chunks
vm:calc_lighting()
vm:write_to_map(data)
local chugent = math.ceil((os.clock() - t1) * 1000)
print ("[moonrealm] "..chugent.." ms chunk ("..x0.." "..y0.." "..z0..")")
end)
-- Spawn player function, dependant on chunk size of 80 nodes
function moonrealm_spawnplayer(player)
local PSCA = 16
local xsp
local ysp
local zsp
local nobj_terrain = nil
local nobj_terralt = nil
local nobj_smooth = nil
local nobj_fault = nil
local nobj_terblen = nil
local nobj_gradcen = nil
for chunk = 1, 64 do
print ("[moonrealm] searching for spawn "..chunk)
local x0 = 80 * math.random(-PSCA, PSCA) - 32
local z0 = 80 * math.random(-PSCA, PSCA) - 32
local y0 = 80 * math.floor((GRADCEN + 32) / 80) - 32
local x1 = x0 + 79
local z1 = z0 + 79
local y1 = y0 + 79
local chulens = x1 - x0 + 1
local pmaplens2d = {x = chulens, y = chulens, z = 1}
local pmaplens3d = {x = chulens, y = chulens, z = chulens}
local minpos2d = {x = x0, y = z0}
local minpos3d = {x = x0, y = y0, z = z0}
nobj_terrain = nobj_terrain or minetest.get_perlin_map(np_terrain, pmaplens3d)
nobj_terralt = nobj_terralt or minetest.get_perlin_map(np_terralt, pmaplens3d)
nobj_smooth = nobj_smooth or minetest.get_perlin_map(np_smooth, pmaplens3d)
nobj_fault = nobj_fault or minetest.get_perlin_map(np_fault, pmaplens3d)
nobj_terblen = nobj_terblen or minetest.get_perlin_map(np_terblen, pmaplens2d)
nobj_gradcen = nobj_gradcen or minetest.get_perlin_map(np_gradcen, pmaplens2d)
local nvals_terrain = nobj_terrain:get3dMap_flat(minpos3d)
local nvals_terralt = nobj_terralt:get3dMap_flat(minpos3d)
local nvals_smooth = nobj_smooth :get3dMap_flat(minpos3d)
local nvals_fault = nobj_fault :get3dMap_flat(minpos3d)
local nvals_terblen = nobj_terblen:get2dMap_flat(minpos2d)
local nvals_gradcen = nobj_gradcen:get2dMap_flat(minpos2d)
local ni3d = 1
local ni2d = 1
local stable = {}
for z = z0, z1 do
for y = y0, y1 do
for x = x0, x1 do
local si = x - x0 + 1
local grad
local density
local terblen = math.max(math.min(math.abs(nvals_terblen[ni2d]) * 4,
1.5), 0.5) - 0.5
local gradcen = GRADCEN + nvals_gradcen[ni2d] * CENAMP
if y > gradcen then
grad = -((y - gradcen) / HIGRAD) ^ HEXP
else
grad = ((gradcen - y) / LOGRAD) ^ LEXP
end
if nvals_fault[ni3d] >= 0 then
density = (nvals_terrain[ni3d] +
nvals_terralt[ni3d]) / 2 * (1 - terblen)
+ nvals_smooth[ni3d] * terblen + grad
else
density = (nvals_terrain[ni3d] -
nvals_terralt[ni3d]) / 2 * (1 - terblen)
- nvals_smooth[ni3d] * terblen + grad
end
if density >= STOT then
stable[si] = true
elseif stable[si] and density < 0 and terblen == 1 then
ysp = y + 4
xsp = x
zsp = z
break
end
ni3d = ni3d + 1
ni2d = ni2d + 1
end
if ysp then
break
end
ni2d = ni2d - chulens
end
if ysp then
break
end
ni2d = ni2d + chulens
end
if ysp then
break
end
end
print ("[moonrealm] spawn player ("..xsp.." "..ysp.." "..zsp..")")
player:setpos({x = xsp, y = ysp, z = zsp})
for i,v in ipairs(GIVE_INITIAL_STUFF) do
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, v)
end
--[[
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "moonrealm:spacesuit 4")
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "moonrealm:sapling 4")
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "moonrealm:airlock 4")
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "moonrealm:airgen 4")
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "moonrealm:hlsource 4")
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "default:apple 64")
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "default:pick_diamond 4")
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "default:axe_diamond 4")
minetest.add_item({x = xsp, y = ysp + 1, z = zsp}, "default:shovel_diamond 4")
]]
local vm = minetest.get_voxel_manip()
local pos1 = {x = xsp - 3, y = ysp - 3, z = zsp - 3}
local pos2 = {x = xsp + 3, y = ysp + 6, z = zsp + 3}
local emin, emax = vm:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
local data = vm:get_data()
local c_shell = minetest.get_content_id("moonrealm:shell")
local c_light = minetest.get_content_id("moonrealm:light")
local c_lsair = minetest.get_content_id("moonrealm:air")
for i = -3, 3 do
for j = -3, 6 do
for k = -3, 3 do
local vi = area:index(xsp + i, ysp + j, zsp + k)
local rad
if j <= 0 then
rad = math.sqrt(i ^ 2 + j ^ 2 + k ^ 2)
else
rad = math.sqrt(i ^ 2 + j ^ 2 * 0.3 + k ^ 2)
end
if rad <= 3.5 then
if rad >= 2.5 then
data[vi] = c_shell
elseif rad >= 1.5 then
data[vi] = c_light
else
data[vi] = c_lsair
end
end
end
end
end
vm:set_data(data)
vm:write_to_map()
vm:update_map()
end
minetest.register_on_newplayer(function(player)
if SPAWN_WITH_SHELL then
moonrealm_spawnplayer(player)
end
end)
minetest.register_on_respawnplayer(function(player)
if SPAWN_WITH_SHELL then
moonrealm_spawnplayer(player)
end
return true
end)