This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
init.lua
2547 lines (2198 loc) · 76.8 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---============================================================================
--MG TECTONIC
--By Dokimi
--A naturalistic mapgen.
--This is the main mapgen code.
--=============================================================================
--PRELIMINARIES
mgtec = {}
local mod_storage = minetest.get_mod_storage()
mgtec.mod_storage = mod_storage
local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
dofile(modpath .. "/cartography.lua")
local timer = dofile(modpath .. "/timer.lua")
-----------------
--PLANTS API
dofile(minetest.get_modpath("mg_tectonic").."/trees.lua")
dofile(minetest.get_modpath("mg_tectonic").."/plants_api.lua")
dofile(minetest.get_modpath("mg_tectonic").."/plants.lua")
--may cause bugs when used???? -- this is an experiment for adding custom plants
if minetest.get_modpath("mgt_flora") ~= nil then
dofile(minetest.get_modpath("mgt_flora").."/plants_reg.lua")
end
if mgtec.registered_on_first_mapgen then -- Run callbacks
for _, f in ipairs(mgtec.registered_on_first_mapgen) do
f()
end
mgtec.registered_on_first_mapgen = nil
mgtec.register_on_first_mapgen = nil
end
--=============================================================================
-- PARAMETERS
----------------------
--MISC
-- the edge of the map
local YMAX = 31000
local YMIN = -31000
--sealevel
local SEA = 0
--height of lava
local MAXMAG = -15000
-------------------
--BASE LAYER:
-- Wave Roll Size: i.e Period
--Controls distance between ranges, and thickness.
--This is the period at the map centre. Grows to double at map edges
--will effect gradient and number of major mt ranges
local XRS = 537--617
--Where does the continental shelf end?
local SHELFX = 25000
local SHELFZ = 28000
--How deep are the oceans?
local SEABED = -15000
--Strength of noise on continental shelf boundaries lines
local CONOI = 2500
--Cave size.
--Base cave threshold for fissures
local BCAVTF = 0.0079
--Base cave threshold for caves
local BCAVT = 0.996
--Ore threshold
local ORET = 0.975
--==================================================================
--FUNCTIONS
-- base waves + noise height estimation
-- actuall base terrain can be up to 30 nodes lower then this, depending on the 3d noises
local function estimate_base_terrain_height(x, z, n_terr, n_terr2)
local xab = math.abs(x) + (n_terr * 1466)
local xtgrad = (xab/YMAX)
local whs = (1-xtgrad) + (n_terr * 0.06)
local mup = whs - xtgrad - (n_terr * 0.25)
local x_roll = XRS + (XRS * xtgrad) + (234 - (n_terr * 234))
local dwav = ((whs*math.cos(xab/(x_roll/6.89))) ^ 3)*1.67 + ((whs*math.cos(xab/x_roll)) ^ 3)*6.89 + mup*10.81
local dnoi = 2.8 * n_terr * (0.5 + n_terr^2 + n_terr2*0.8) * (whs + 0.02)
local d_base = dwav + dnoi
local d_soft = d_base * 0.4 + 1.3 + (1 - n_terr) * (2.22 - xtgrad * 2)
local d_allu = d_soft * 0.95 + 0.1
local d_sedi = d_allu + 0.03
local d_max = math.max(d_base, d_soft, d_allu, d_sedi) / 0.00969
return d_max
end
mgtec.estimate_base_terrain_height = estimate_base_terrain_height
-- basisns don't use 3d noises so they return acurate terrain height
-- hieght limit imposed by ocean basin
local function get_ocan_basin_height(x, z, n_terr, n_terr2)
local xab = math.abs(x) + (n_terr * 1466)
local zab = math.abs(z)
local shelfnoi_xz = (n_terr^3 + n_terr2 * 0.08) * CONOI
local x_slope_y = (SHELFX + shelfnoi_xz * 2 - xab) / (3 - n_terr^3)
local z_slope_y = (SHELFZ + shelfnoi_xz - zab) / (3 - n_terr^3)
local ocean_basin_y = math.max(SEABED, math.min(x_slope_y, z_slope_y))
return ocean_basin_y
end
mgtec.get_ocan_basin_height = get_ocan_basin_height
-- gets combined height or river and lake beds
local function get_river_bed_heigh(x, z, n_terr, n_terr2)
local laker_0 = 160 + (100*n_terr) + (25*n_terr2)
local laker_1 = laker_0/(160 + 20*n_terr)
local laked = -25 + (10 * n_terr2)
local xab = math.abs(x) + (n_terr * 1466)
local wp = 8 + 0.0003*xab
local w_0 = wp - 3*n_terr2^3
local w_1 = wp/(8 + 2*n_terr)
local per_ch = (3.5*n_terr + 22) * wp --period of channel
local interr2 = 1-n_terr2
local am_ch = (interr2 + 4.6) * wp --amplitude
local c1 = am_ch*math.sin(x/per_ch) --wave for channel
local c2 = (12 + (3*interr2))*math.sin(x/(56 + (8*n_terr)))
local channel = c1 + c2
local lar_y = YMAX --lowest lake and river height
for _, lake in pairs(mgtec.lakes) do
-- lake basin
local lake_xy = (math.abs(x - lake.x) / 1.6 - laker_0) / laker_1
local lake_zy = (math.abs(z - lake.z) - laker_0) / laker_1
lar_y = math.min(lar_y, math.max(lake_xy, lake_zy, laked))
-- river basin
if lake.r <= 4 then
local river_z = lake.z + channel
local river_dist = math.abs(z - river_z)
local river_y = (river_dist - w_0) / w_1
local river_end_y = nil
if lake.x < 0 then
river_end_y = (x - lake.x - w_0) / w_1
else
river_end_y = -(x - lake.x + w_0) / w_1
end
lar_y = math.min(lar_y, math.max(river_y, river_end_y))
end
end
return lar_y
end
mgtec.get_river_bed_heigh = get_river_bed_heigh
-- returns aproximate height at given cooridinates
local function estimate_height(x, z, n_terr, n_terr2)
local e_terrain_y = mgtec.estimate_base_terrain_height(x, z, n_terr, n_terr2)
local ocean_basin_y = mgtec.get_ocan_basin_height(x, z, n_terr, n_terr2)
local river_basin_y = mgtec.get_river_bed_heigh(x, z, n_terr, n_terr2)
local estimate_y = math.min(e_terrain_y, ocean_basin_y, river_basin_y)
return estimate_y
end
mgtec.estimate_height = estimate_height
--Checks if this is a place for ore deposits.
local function ore(nocave, ab_stra, ab_cave, ab_cave2, y, ORET, ybig, n_strata, data, vi, OREID)
--c_coal, c_iron, c_copp, c_tin, c_gold, c_diam, c_mese
-- timer.start("ore")
--strata thickness
local thick = 100 + (50 * ab_stra)
--strata splits for ore types..
local t1 = 0.25
local t2 = 0.5
local t3 = 0.83
local ystrata = math.sin(y/thick)
--a fair sine wave split, with low value ores dominant
--[[for reference... if the above gets confusing
local ysmin_coal = 0 * TSTRA
local ysmax_coal = 0.25 * TSTRA
local ysmin_cop = 0.25 * TSTRA
local ysmax_cop = 0.5 * TSTRA
local ysmin_gol = 0.5 * TSTRA
local ysmax_gol = 0.83 * TSTRA
local ys_mese1 = 0.83 * TSTRA --mese is on the ends of the range
local ysmin_iron = -0.25 * TSTRA
local ysmax_iron = 0 * TSTRA
local ysmax_tin = -0.25 * TSTRA
local ysmin_tin = -0.5 * TSTRA
local ysmax_dia = -0.5 * TSTRA
local ysmin_dia = -0.83 * TSTRA
local ys_mese2 = -0.83 * TSTRA
--]]
--threshold adjusted with depth to get bigger down deep.
local ore_t = ORET + ybig
--harder if actually in the cave.
if not nocave then
ore_t = ore_t + 0.05
end
--height limits
local blend = n_strata * 50
local orehmin_c = -10000 + (n_strata * 500) --min height for coal (a shallow ore)
local orehmax_g = -30 + blend --dig a little for gold
local orehmax_d = -600 + blend --diamonds are deep
local orehmax_m = -400 + blend --mese is deep
--above their threshold
--add some of the cave noise to increase chance of finding ores near caves
--working with caves: should be less inside the actual cave (less floaters), but..
--..reduced threshold should boost approaching cave
if ab_stra >= ore_t - (ab_cave2 * 0.02) then
--split them by height and strata
--coal.
if y > orehmin_c
and ystrata >= 0 --strata splits
and ystrata < t1
then
data[vi] = OREID.c_coal
-- timer.stop("ore")
return true
--iron
elseif ystrata > -t1 --strata splits
and ystrata < 0 then
data[vi] = OREID.c_iron
-- timer.stop("ore")
return true
--copper
elseif ystrata > t1 --strata splits
and ystrata < t2 then
data[vi] = OREID.c_copp
-- timer.stop("ore")
return true
--tin
elseif ystrata > -t2 --strata splits
and ystrata < -t1 then
data[vi] = OREID.c_tin
-- timer.stop("ore")
return true
--Gold
elseif y < orehmax_g
and ystrata > t2 --strata splits
and ystrata < t3 then
data[vi] = OREID.c_gold
-- timer.stop("ore")
return true
--Diamonds
elseif y < orehmax_d
and ystrata > -t3 --strata splits
and ystrata < -t2 then
data[vi] = OREID.c_diam
-- timer.stop("ore")
return true
--Mese
elseif y < orehmax_m
and (ystrata > t3 --strata splits, end of range
or ystrata < -t3) then
data[vi] = OREID.c_mese
-- timer.stop("ore")
return true
end
--end of ores
else
--have to let it know so it can set void etc
-- timer.stop("ore")
return false
end
--end of all ores
end
--End of Ore function
------------------------------
--Climate Calculations.
--this function can be called by other mods instead of default biome climate checks
mgtec.climate = function(x, z, y, n_terr, n_terr2)
--east = + x, west = - x, south = -z, n = + z
--Climate is decided by:
-- -Ranges: rains come from the west (-x), rise over the ranges dumping cooling rain, descending hot and dry (east +x)
-- - Altitude: it's cold up there.
-- - Latitude: hot north, cold south
--blending
local blend = math.random(-4, 4)
--to help climate be accessed by other mods (mainly weather)
--we will ignore noise (thorn0906's solution crashes when actually used,
-- accessing noise repeatedly likely very heavy for little gain
-- reducing noiseyness better? Randomness can make things throw a fit)
if n_terr == nil or n_terr2 == nil then
blend = 0
n_terr = 0
n_terr2 = 0
end
--adjust x to match with the adjusted centreline of symmetry
local x_adj = (n_terr * 1466)
x = x + x_adj
--We are Southern Hemisphererers here!
--decreasing temp from max z to min z (latitude) (from 100 to 0 i.e north desert to south ice)
-- linear increase, intercept at 50
local temp_z = (0.00163*z) + 50 - blend
-- no Fohn? Westies are controlled by z only
local temp_x = 0
--offset east-west border with some noise
-- centreline from noise with more noise
local lon_blend = x_adj + ((blend*3) - (n_terr2 * 200))
-- Easterners?
if x > lon_blend then
--Fohn Winds! They are hot! The East Coast gets a temp boost
-- adds + 20 at centre of map, declines to zero
temp_x = (-0.00065*x) + 20 - blend
end
--Mountain tops ought to be cold!
--decreasing temp with hieght...and combine previous two as baseline
local temp = temp_z + temp_x - blend
--only apply height adjustment above sea level, otherwise cooking oceans
--large parts of "lowlands" are very high too, so start cooling high.
--i.e. -0.05 = -50 at 1000m
if y >= 200 + (n_terr *75) then
temp = (-0.05*y) + temp_z + temp_x - blend
end
---------------
--what's the humidity? Rainshadow!
--decreasing humid from far x to x= 0,(rain shadow)
--tip a little past 50 for greater variety
--i.e. 0-60, so have wet islands on the dry side, dry islands on wet side
--if in doubt ...
local hum = 50
----positive, east coast. Dry inland
--linear increase,
if x > lon_blend then
hum = (0.00194*x) + blend
--increasing humid from far x to x= 0,(rain shadow)
else --negative , west coast. Wet inland
--linear increase,
--starts a little lower than max (i.e. + <100)
--so that altitude boosts it to max
hum = (0.00145*x) + 85 + blend
end
--Transition humidity
--this is to avoid a sharp line East West
-- not perfect but better than nothing?
if x > lon_blend and x < lon_blend + 400 then
--east must be wetter
if x < lon_blend + 100 then
hum = hum + 35
elseif x < lon_blend + 200 then
hum = hum + 20
else
hum = hum + 15
end
elseif x < lon_blend and x > lon_blend - 300 then
--west must be drier
if x > lon_blend - 100 then
hum = hum - 20
elseif x > lon_blend - 200 then
hum = hum - 10
else
hum = hum - 5
end
end
--disturbance regime
--i.e. ecological succession
local distu = math.abs(n_terr * 100 + blend)
--altitude effects..
--coast is wet, but disturbed
-- hill tops catch rain but are more disturbed
if y > 490 or (y < 10 and y > -3) then
--alpine (force snowy)
if y > 1200 + math.random(-30, 5) then
hum = hum + 48
temp = temp - 32
distu = distu + 12
--subalpine
elseif y > 1100 + blend then
hum = hum + 24
temp = temp - 16
distu = distu + 6
elseif y > 1000 + blend then
hum = hum + 12
temp = temp - 8
distu = distu + 3
--montane
elseif y > 900 + blend then
temp = temp - 4
hum = hum + 6
elseif y > 700 + blend then
temp = temp - 2
hum = hum + 3
elseif y > 500 + blend then
hum = hum + 2
--coast
elseif (y <= 3 and y >= -1) then
-- generally wetter, but sometimes drier
--..coasts are more variable
hum = hum + 3 + blend
distu = distu + 5
end
end
--disturbance Feedsback on micro-climate
--calm areas hold water.
--exposed areas are hotter
--sheltered a little colder
if distu <15 or distu >80 then
if distu < 10 then
if distu <5 then
hum = hum + 8
temp = temp - 4
elseif distu <15 then
hum = hum + 4
temp = temp - 2
end
elseif distu > 80 then
if distu > 95 then
hum = hum - 12
temp = temp + 12
elseif distu > 90 then
hum = hum - 6
temp = temp + 6
elseif distu > 80 then
hum = hum - 3
temp = temp + 3
end
end
end
return temp, hum, distu
--done climate calculations
end
-------------------------------------
--Create Swamp
local function swamp(data, vi, chance, mud, water)
--let's place a messy swampy mire
local roll = math.random(1, chance)
if roll == 1 then
data[vi] = water
else
data[vi] = mud
end
--end of swamp
end
--End of functions
--=============================================================================
--IDs
-- Get the content IDs for the nodes used.
--rocks
local c_stone = minetest.get_content_id("default:stone")
local c_stone2 = minetest.get_content_id("default:desert_stone")
local c_sandstone = minetest.get_content_id("default:desert_sandstone")
local c_sandstone2 = minetest.get_content_id("default:sandstone")
local c_sandstone3 = minetest.get_content_id("default:silver_sandstone")
local c_obsid = minetest.get_content_id("default:obsidian")
local c_coral = minetest.get_content_id("default:coral_skeleton")
local c_coralb = minetest.get_content_id("default:coral_brown")
local c_coralo = minetest.get_content_id("default:coral_orange")
--sediments
local SEDID = {
c_gravel = minetest.get_content_id("default:gravel"),
c_clay = minetest.get_content_id("default:clay"),
c_sand = minetest.get_content_id("default:sand"),
c_sand2 = minetest.get_content_id("default:silver_sand"),
c_dirt = minetest.get_content_id("default:dirt"),
c_dry_dirt = minetest.get_content_id("default:dry_dirt"),
c_perma = minetest.get_content_id("default:permafrost"),
}
--surfaces
local c_dirtgr = minetest.get_content_id("default:dirt_with_grass")
local c_dirtdgr = minetest.get_content_id("default:dirt_with_dry_grass")
local c_dry_dirtdgr = minetest.get_content_id("default:dry_dirt_with_dry_grass")
local c_dirtsno = minetest.get_content_id("default:dirt_with_snow")
local c_dirtlit = minetest.get_content_id("default:dirt_with_rainforest_litter")
local c_dirtconlit = minetest.get_content_id("default:dirt_with_coniferous_litter")
local c_snowbl = minetest.get_content_id("default:snowblock")
local c_snow = minetest.get_content_id("default:snow")
local c_ice = minetest.get_content_id("default:ice")
local c_dsand = minetest.get_content_id("default:desert_sand")
local c_permamoss = minetest.get_content_id("default:permafrost_with_moss")
local c_permastone = minetest.get_content_id("default:permafrost_with_stones")
--Miscellaneous
local MISCID = {
c_water = minetest.get_content_id("default:water_source"),
c_river = minetest.get_content_id("default:river_water_source"),
c_air = minetest.get_content_id("air"),
c_ignore = minetest.get_content_id("ignore"),
c_lava = minetest.get_content_id("default:lava_source"),
c_kelpsand = minetest.get_content_id("default:sand_with_kelp"),
c_coral_cyan = minetest.get_content_id("default:coral_cyan"),
c_coral_green = minetest.get_content_id("default:coral_green"),
c_coral_pink = minetest.get_content_id("default:coral_pink"),
}
--ores
local OREID = {
c_diam = minetest.get_content_id("default:stone_with_diamond"),
c_mese = minetest.get_content_id("default:stone_with_mese"),
c_gold = minetest.get_content_id("default:stone_with_gold"),
c_copp = minetest.get_content_id("default:stone_with_copper"),
c_tin = minetest.get_content_id("default:stone_with_tin"),
c_iron = minetest.get_content_id("default:stone_with_iron"),
c_coal = minetest.get_content_id("default:stone_with_coal")
}
--=============================================================================
--NOISES
-- 2D Mountain Terrain.
--controls: large rounded mountains.
local np_terrain = {
offset = 0,
scale = 1,
spread = {x = 1944, y = 3078, z = 1944},
seed = 110013,
octaves = 5,
persist = 0.3,
lacunarity = 3,
--flags = 'noeased',
}
mgtec.np_terrain = np_terrain
-- 2D Soft rock Terrain.
--controls: sharp peaks
local np_terrain2 = {
offset = 0,
scale = 1,
spread = {x = 405, y = 243, z = 405},
seed = 5938033,
octaves = 3,
persist = 0.6,
lacunarity = 3,
flags = 'noeased',
}
mgtec.np_terrain2 = np_terrain2
-- 3D Caves 1
--used by: fissures in basement rock,
local np_cave = {
offset = 0,
scale = 1,
spread = {x = 108, y = 243, z = 324},
seed = -9103323,
octaves = 3,
persist = 0.2,
lacunarity = 3,
--flags = 'noeased',
}
mgtec.np_cave = np_cave
-- 3D Caves 2
--used by: breaks in fissures, caves in basement rock
local np_cave2 = {
offset = 0,
scale = 1,
spread = {x = 81, y = 27, z = 81},
seed = 205301,
octaves = 3,
persist = 0.5,
lacunarity = 3,
flags = 'noeased',
}
mgtec.np_cave2 = np_cave2
--3D Strata
-- used by: ore thresholds.
local np_strata = {
offset = 0,
scale = 1,
spread = {x = 81, y = 81, z = 81},
seed = 51055033,
octaves = 3,
persist = 0.7, --high or get few ore
lacunarity = 3,
}
mgtec.np_strata = np_strata
--======================================================================================================================
--NOISE MEMORY
-- Initialize noise object to nil. They will be created after the world starts
-- and 'minetest.get_perlin' becomes available.
local nobj_terrain = nil
local nobj_terrain2 = nil
local nobj_cave = nil
local nobj_cave2 = nil
local nobj_strata = nil
minetest.after(0, function()
-- Side length of mapchunk.
local blocks_per_chunk = tonumber(minetest.settings:get("chunksize")) or 5
local side_lenght = blocks_per_chunk * 16
-- Dimensions for perlin maps
local chunk_size = {x = side_lenght, y = side_lenght, z = side_lenght}
-- Creates perlin map noise objects
-- runs before map generation starts
nobj_terrain = minetest.get_perlin_map(np_terrain, chunk_size)
nobj_terrain2 = minetest.get_perlin_map(np_terrain2, chunk_size)
nobj_cave = minetest.get_perlin_map(np_cave, chunk_size)
nobj_cave2 = minetest.get_perlin_map(np_cave2, chunk_size)
nobj_strata = minetest.get_perlin_map(np_strata, chunk_size)
end)
-- Localise noise buffer table outside the loop, to be re-used for all
-- mapchunks, therefore minimising memory use.
local nvals_terrain = {}
local nvals_terrain2 = {}
local nvals_cave = {}
local nvals_cave2 = {}
local nvals_strata = {}
-- Localise data buffer table outside the loop, to be re-used for all
-- mapchunks, therefore minimising memory use.
local data = {}
-- param2 data
local data2 = {}
--============================================================================================================ --***
----ON--------------------------------------------------------------------
logg = function(sss) --for debug
minetest.log(sss)
--minetest.chat_send_all(sss) --Causes silent crash if called to soon
minetest.after(0 , minetest.chat_send_all , sss) --delay until global step loop
end
----OFF-------------------------------------------------------------------
--logg = function(sss) end
--------------------------------------------------------------------------
--========== OVERRIDE minetest.get_heat(pos) , minetest.get_humidity(pos) ============================================== --***
--Some mod could query heat/hum too frequently, return cached data to save CPU if pos "enough" near to last calculated pos.
--(heat and humidity vary smoothly)
-- h_th (horizontal) and v_th(vertical) are the threshold distances that decide wether return cached or recalculate
-- Different h_th and v_th due to heat, hum being more altitude dependent than x,z dependent
-- This naive cache system is only good for singleplayer.Multiplayer would need kind of per player cache.
-- (Cached pos of player 1 is useless if player 2 queries hum/heat)
local clima_cache = {50, 50, x=100000, y=0, z=0 }
local v_th = 15 --PLEASE ADJUST THIS VALUE!
local h_th = 30 --PLEASE ADJUST THIS VALUE!
local h_th2 = h_th^2
local get_heat_or_humidity = function(pos,what)
if math.abs(pos.y - clima_cache.y) > v_th
or (pos.x - clima_cache.x)^2 + (pos.z - clima_cache.z)^2 > h_th2
then
--local n_terr = nvals_terrain[nixz] ---doesn't actually give it noise?
--local n_terr2 = nvals_terrain2[nixz]
local temp, hum, distu = mgtec.climate(pos.x, pos.z, pos.y)--, n_terr, n_terr2)
clima_cache.x = pos.x
clima_cache.y = pos.y
clima_cache.z = pos.z
clima_cache[1] = temp
clima_cache[2] = hum
logg("get_heat_or_humidity -> recalculated T,H = ".. temp .." , ".. hum)
else
logg("get_heat_or_humidity -> cached T,H = ".. clima_cache[1] .." , ".. clima_cache[2])
end
return clima_cache[what]
end
minetest.get_heat = function(pos) return get_heat_or_humidity(pos,1) end
minetest.get_humidity = function(pos) return get_heat_or_humidity(pos,2) end
--=============================================================================
-- GENERATION
local num_lakes = nil
local lakes = nil
local spawnpoint = {x = 0, z = 0}
minetest.register_on_mapgen_init(function(mapgen_params)
minetest.log("on_mapgen_init ") --MIO
math.randomseed(mapgen_params.seed)
spawnpoint = {x = math.random(-23000, 23000), z = math.random(-27000, 27000)}
minetest.set_mapgen_setting("mg_name", "singlenode", true) --***
minetest.set_mapgen_setting("mg_flags", "nolight", true) --***
-- some things need to be random, but stay constant throughout the loop
-- number of lakes
if lakes == nil then
num_lakes = math.random(15,18)
lakes = {}
for i = 0, num_lakes do
--keep back from "shelf" as the coastline is actually much further back
--need to keep them out of main ranges...too much erosion.
--choose east or west.
if math.random(1,3) <=2 then
--west lake
lakes[i] = {x = math.random(-13000,-5500), z = math.random(-25000,25000), r = math.random(1,5)}
else
--East lake.
lakes[i] = {x = math.random(5500,13000), z = math.random(-25000,25000), r = math.random(1,4)}
end
--save location for bug checking
mod_storage:set_int("Lake"..i.."x", lakes[i].x)
mod_storage:set_int("Lake"..i.."z", lakes[i].z)
mod_storage:set_int("Lake"..i.."river", lakes[i].r)
end
end
mgtec.lakes = lakes
end)
table.insert(minetest.registered_on_generateds, 1, (function(minp, maxp, seed)
-- if minp.z < 1000 then return end
timer.new_sample()
timer.start("total")
math.randomseed(seed)
--------------------------------
--don't do out of bounds!
--world is a square, ymin will do for z and x too.
if minp.x < YMIN
or maxp.x > YMAX
or minp.y < YMIN
or maxp.y > YMAX
or minp.z < YMIN
or maxp.z > YMAX
then
return
end
-----------------------------
-- Start time of mapchunk generation.
local t0 = minetest.get_us_time()
--------------------------------
-- NOISE
-- Side length of mapchunk.
local sidelen = maxp.x - minp.x + 1
local minposxyz = {x = minp.x, y = minp.y - 1, z = minp.z}
-- offset is to align noise better around x = 0
local minposxz = {x = minp.x + 970, y = minp.z}
-- strides for voxelmanip
local ystridevm = sidelen + 32
local zstridevm = ystridevm ^ 2
-- Create a flat array of noise values from the perlin map, with the
-- minimum point being 'minp'.
-- Set the buffer parameter to use and reuse 'nvals_X' for this.
timer.start("perlin")
nobj_terrain:get_2d_map_flat(minposxz, nvals_terrain)
nobj_terrain2:get_2d_map_flat(minposxz, nvals_terrain2)
timer.stop("perlin")
----------------------------------------------------------------------------
-- AREAS
-- For conversion between coordinates and table indices
-- emerged area - for the data and data2 tables
-- local area_em = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
-- chunk area - for 3d noise tables
local area_ch = VoxelArea(minp, maxp)
-- chunk area 2d - for 2d noise tables
local area_ch_2d = VoxelArea(vector.new(minp.x, 0, minp.z), vector.new(maxp.x, 0, maxp.z))
----------------------------------------------------------------------------
-- HEIGHT MAP
-- create an estimated height map
-- this will hopefull allow an early exit from empty chunks
timer.start("height_map")
local height_map = {}
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
local noise_index = area_ch_2d:index(x, 0, z)
local height = estimate_height(x, z, nvals_terrain[noise_index], nvals_terrain2[noise_index])
height_map[noise_index] = height
end
end
local heighest = height_map[1]
for _, height in pairs(height_map) do
heighest = math.max(heighest, height)
end
if heighest < minp.y + 3 then
-- print("empty chunk - aborting")
-- map gets lighting errors when chunkgen is directly aborted
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
vm:calc_lighting()
vm:write_to_map()
return
end
timer.stop("height_map")
----------------------------------------------------------------------------
-- 3d noise generation delayed untill we are sure that we actually need it
timer.start("perlin")
nobj_cave:get_3d_map_flat(minposxyz, nvals_cave)
nobj_cave2:get_3d_map_flat(minposxyz, nvals_cave2)
nobj_strata:get_3d_map_flat(minposxyz, nvals_strata)
timer.stop("perlin")
-----------------------------------------------------------
timer.start("load_data")
-- VOXELMANIP
-- Load the voxelmanip with the result of engine mapgen. Since 'singlenode'
-- mapgen is used this will be a mapchunk of air nodes.
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
--minetest.log("+++++++++ on_generated called with seed = " .. seed .. " minp= " .. minetest.pos_to_string(minp) .. " maxp= " .. minetest.pos_to_string(maxp) .. " emin= " .. minetest.pos_to_string(emin) .. " emax= " .. minetest.pos_to_string(emax)) --MIO
--minetest.log("+++++++++ on_generated called with seed = " .. seed .. "," .. minetest.pos_to_string(minp) .. "," .. minetest.pos_to_string(maxp) .. "," .. minetest.pos_to_string(emin) .. "," .. minetest.pos_to_string(emax)) --MIO
-- emerged area - for the data and data2 tables
local area_em = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
-- Get the content ID data from the voxelmanip in the form of a flat array.
-- Set the buffer parameter to use and reuse 'data' for this.
vm:get_data(data)
vm:get_param2_data(data2)
---------------------------------------------
-- GENERATION LOOP
----------------------------
--Begin the Loop
timer.stop("load_data")
timer.start("terrain")
-- Process the content IDs in 'data'.
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
local nixz = area_ch_2d:index(x, 0, z) -- 2d noise index
-- 2D Noises
local n_terr = nvals_terrain[nixz]
local n_terr2 = nvals_terrain2[nixz]
local ab_t = math.abs(n_terr)
local ab_t2 = math.abs(n_terr2)
timer.start("river_basins")
local ocean_basin_y = mgtec.get_ocan_basin_height(x, z, n_terr, n_terr2)
local river_basin_y = mgtec.get_river_bed_heigh(x, z, n_terr, n_terr2)
timer.stop("river_basins")
for y = minp.y, maxp.y do
-- Voxelmanip index for the flat array of content IDs.
local vi = area_em:index(x, y, z) -- voxelmanip index
local nixyz = area_ch:index(x, y, z) -- 3d noise index
----------------------------------------------------
--Welcome to the Loop
-- if we are above estimated terrain height and above ocean level abort column
if y > height_map[nixz] + 3 and y > SEA + 3 then
break
end
-----------
--3D Noises
local n_cave = nvals_cave[nixyz]
local n_cave2 = nvals_cave2[nixyz]
local ab_cave = math.abs(n_cave)
local ab_cave2 = math.abs(n_cave2)
local n_strata = nvals_strata[nixyz]
local ab_stra = math.abs(n_strata)
--Get the node underneath
local nodu = data[(vi - ystridevm)]
-----------
-- Math
--[[note on noise:
Without noise the waves form straight lines down the entire map.
This is only noticeable after much exploring,
but makes the map less interesting.
Noise added to make waves more varied along the map.
-xab: shifts symmetry
-whs: creates high and low points (must be small or crazy effects!).
-xroll: number and size of ranges.
-mup: low and high points
]]
--absolute for x (for symmetry on both sides of map)
local xab = math.abs(x) + (n_terr * 1466)
--x axis terrain gradient. 0 at centre. 1 at edges.
--Used by equations to adjust along x axis
local xtgrad = (xab/YMAX)
--amplitude... going from 1 to zero at map edge
local whs = (1-xtgrad) + (n_terr * 0.06)
--Move up/down along x axis. Goes from +1 to -1
--mup raises and lowers along x axis. the two terms cancel out in middle of x range (15k).
--aimed for equations that need to lift map centre, sink edges
local mup = whs + (-1 * xtgrad) + (n_terr * -0.25)
------------------
--Base Layer Waves
--creates the underlying, undulating and mountainous terrain
--Wave period. "Roll". i.e. how wide/steep and they are.
--Gradient widens the ranges towards the edges.
local x_roll = XRS + (XRS * xtgrad) + (234 - (n_terr * 234))
--The Wave!
-- (while cos doesn't need an absolute value for x, it does need the noise adjusted one, hence xab)
local xwav = (whs*math.cos(xab/x_roll)) -- north south wave (main ranges)
local xwav2 = (whs*math.cos(xab/(x_roll/6.89))) --smaller more detailed wave
--Base Wave Density.
--This is checked against the threshold to decide where the base layer can go.
--the wave controls the landscape folding i.e. the bunching up caused by the plates hitting eachother.
--variation along this fold is caused by erosion, so is much more random...
--cubing/squaring flattens out the middle range, squaring eliminates negatives
--for the wave that gives it a flat shelf at sea level.