summaryrefslogtreecommitdiff
path: root/init.lua
blob: 9a38338ba3f7ce304bd87aa3722e0a476f4f78ee (plain)
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
-- Parameters

local YMAXMINP = -32 -- Maximum minp.y of generated chunks
local HSAMP = 0.03 -- Height select amplitude. Maximum steepness of paths

-- Mapgen v7 noises

-- 2D noise for base terrain

local np_base = {
	offset = 4,
	scale = 70,
	spread = {x = 600, y = 600, z = 600},
	seed = 82341,
	octaves = 5,
	persist = 0.6
}

-- 2D noise for alt terrain

local np_alt = {
	offset = 4,
	scale = 25,
	spread = {x = 600, y = 600, z = 600},
	seed = 5934,
	octaves = 5,
	persist = 0.6
}

-- 2D noise for height select

local np_select = {
	offset = -8,
	scale = 16,
	spread = {x = 500, y = 500, z = 500},
	seed = 4213,
	octaves = 4, -- default 6
	persist = 0.3 -- default 0.7
}

-- Mod noises

-- 2D noise for patha

local np_patha = {
	offset = 0,
	scale = 1,
	spread = {x = 1024, y = 1024, z = 1024},
	seed = 11711,
	octaves = 3,
	persist = 0.4
}

-- 2D noise for pathb

local np_pathb = {
	offset = 0,
	scale = 1,
	spread = {x = 2048, y = 2048, z = 2048},
	seed = -8017,
	octaves = 4,
	persist = 0.4
}

-- 2D noise for pathc

local np_pathc = {
	offset = 0,
	scale = 1,
	spread = {x = 4096, y = 4096, z = 4096},
	seed = 300707,
	octaves = 5,
	persist = 0.4
}

-- 2D noise for pathd

local np_pathd = {
	offset = 0,
	scale = 1,
	spread = {x = 8192, y = 8192, z = 8192},
	seed = -80033,
	octaves = 6,
	persist = 0.4
}

-- 2D noise for columns

local np_column = {
	offset = 0,
	scale = 1,
	spread = {x = 8, y = 8, z = 8},
	seed = 1728833,
	octaves = 3,
	persist = 2
}


-- Do files

dofile(minetest.get_modpath("pathv7") .. "/nodes.lua")


-- Initialise noise objects to nil

local nobj_base = nil
local nobj_alt = nil
local nobj_select = nil
local nobj_patha = nil
local nobj_pathb = nil
local nobj_pathc = nil
local nobj_pathd = nil
local nobj_column = nil

-- Localise noise buffers

local nbuf_base
local nbuf_alt
local nbuf_select
local nbuf_patha
local nbuf_pathb
local nbuf_pathc
local nbuf_pathd
local nbuf_column


-- On generated function

minetest.register_on_generated(function(minp, maxp, seed)
	if minp.y < -32 or minp.y > YMAXMINP 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 c_air        = minetest.CONTENT_AIR
	local c_ignore     = minetest.CONTENT_IGNORE
	local c_tree       = minetest.get_content_id("default:tree")
	local c_sand       = minetest.get_content_id("default:sand")
	local c_dirt       = minetest.get_content_id("default:dirt")
	local c_grass      = minetest.get_content_id("default:dirt_with_grass")
	local c_drygrass   = minetest.get_content_id("default:dirt_with_dry_grass")
	local c_dirtsnow   = minetest.get_content_id("default:dirt_with_snow")
	local c_desand     = minetest.get_content_id("default:desert_sand")
	local c_stone      = minetest.get_content_id("default:stone")
	local c_sastone    = minetest.get_content_id("default:sandstone")
	local c_destone    = minetest.get_content_id("default:desert_stone")
	local c_ice        = minetest.get_content_id("default:ice")
	local c_meselamp   = minetest.get_content_id("default:meselamp")
	local c_gravel     = minetest.get_content_id("default:gravel")
	local c_tree       = minetest.get_content_id("default:tree")
	local c_jungletree = minetest.get_content_id("default:jungletree")
	local c_pinetree   = minetest.get_content_id("default:pine_tree")
	local c_acaciatree = minetest.get_content_id("default:acacia_tree")
	local c_aspentree  = minetest.get_content_id("default:aspen_tree")

	local c_wood   = minetest.get_content_id("pathv7:junglewood")
	local c_column = minetest.get_content_id("pathv7:bridgewood")

	local c_stairn  = minetest.get_content_id("pathv7:stairn")
	local c_stairs  = minetest.get_content_id("pathv7:stairs")
	local c_staire  = minetest.get_content_id("pathv7:staire")
	local c_stairw  = minetest.get_content_id("pathv7:stairw")
	local c_stairne = minetest.get_content_id("pathv7:stairne")
	local c_stairnw = minetest.get_content_id("pathv7:stairnw")
	local c_stairse = minetest.get_content_id("pathv7:stairse")
	local c_stairsw = minetest.get_content_id("pathv7:stairsw")

	local sidelen = x1 - x0 + 1
	local emerlen = sidelen + 32
	local overlen = sidelen + 5
	local chulens = {x = overlen, y = overlen, z = 1}
	local minpos = {x = x0 - 3, y = z0 - 3}

	nobj_base   = nobj_base   or minetest.get_perlin_map(np_base,   chulens)
	nobj_alt    = nobj_alt    or minetest.get_perlin_map(np_alt,    chulens)
	nobj_select = nobj_select or minetest.get_perlin_map(np_select, chulens)

	nobj_patha  = nobj_patha  or minetest.get_perlin_map(np_patha,  chulens)
	nobj_pathb  = nobj_pathb  or minetest.get_perlin_map(np_pathb,  chulens)
	nobj_pathc  = nobj_pathc  or minetest.get_perlin_map(np_pathc,  chulens)
	nobj_pathd  = nobj_pathd  or minetest.get_perlin_map(np_pathd,  chulens)
	nobj_column = nobj_column or minetest.get_perlin_map(np_column, chulens)
	
	local nvals_base   = nobj_base  :get2dMap_flat({x = x0 - 3, y = z0 - 3}, nbuf_base)
	local nvals_alt    = nobj_alt   :get2dMap_flat({x = x0 - 3, y = z0 - 3}, nbuf_alt)
	local nvals_select = nobj_select:get2dMap_flat({x = x0 - 3, y = z0 - 3}, nbuf_select)

	local nvals_patha  = nobj_patha :get2dMap_flat(minpos, nbuf_patha)
	local nvals_pathb  = nobj_pathb :get2dMap_flat(minpos, nbuf_pathb)
	local nvals_pathc  = nobj_pathc :get2dMap_flat(minpos, nbuf_pathc)
	local nvals_pathd  = nobj_pathd :get2dMap_flat(minpos, nbuf_pathd)
	local nvals_column = nobj_column:get2dMap_flat(minpos, nbuf_column)
	
	local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
	local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
	local data = vm:get_data()

	local ni = 1
	local stable = {}
	for z = z0 - 3, z1 + 2 do
		local n_xprepatha = false
		local n_xprepathb = false
		local n_xprepathc = false
		local n_xprepathd = false
		-- x0 - 3, z0 - 3 is to setup initial values of 'xprepath_', 'zprepath_'
		for x = x0 - 3, x1 + 2 do
			local n_patha = nvals_patha[ni]
			local n_zprepatha = nvals_patha[(ni - overlen)]

			local n_pathb = nvals_pathb[ni]
			local n_zprepathb = nvals_pathb[(ni - overlen)]
			
			local n_pathc = nvals_pathc[ni]
			local n_zprepathc = nvals_pathc[(ni - overlen)]

			local n_pathd = nvals_pathd[ni]
			local n_zprepathd = nvals_pathd[(ni - overlen)]

			if x >= x0 - 2 and z >= z0 - 2 then
				local abscol = math.abs(nvals_column[ni])
				local base = nvals_base[ni]
				local alt = nvals_alt[ni]
				local select = nvals_select[ni]
				if base < alt then
					base = alt
				end
				local tblend = 0.5 + HSAMP * (select - 0.5)
				tblend = math.min(math.max(tblend, 0), 1)
				local tlevel = base * tblend + alt * (1 - tblend)
				-- TODO allow path above
				local pathy = math.floor(math.min(math.max(tlevel, 7), 42))

				if (n_patha >= 0 and n_xprepatha < 0) -- detect sign change of noise
						or (n_patha < 0 and n_xprepatha >= 0)
						or (n_patha >= 0 and n_zprepatha < 0)
						or (n_patha < 0 and n_zprepatha >= 0)

						or (n_pathb >= 0 and n_xprepathb < 0)
						or (n_pathb < 0 and n_xprepathb >= 0)
						or (n_pathb >= 0 and n_zprepathb < 0)
						or (n_pathb < 0 and n_zprepathb >= 0)

						or (n_pathc >= 0 and n_xprepathc < 0)
						or (n_pathc < 0 and n_xprepathc >= 0)
						or (n_pathc >= 0 and n_zprepathc < 0)
						or (n_pathc < 0 and n_zprepathc >= 0)

						or (n_pathd >= 0 and n_xprepathd < 0)
						or (n_pathd < 0 and n_xprepathd >= 0)
						or (n_pathd >= 0 and n_zprepathd < 0)
						or (n_pathd < 0 and n_zprepathd >= 0) then
					if pathy > y1 then
						-- build columns through this chunk
						if abscol < 0.3 then
							for xx = x - 1, x + 1, 2 do
							for zz = z - 1, z + 1, 2 do
								local vi = area:index(xx, y1, zz)
								for y = 1, sidelen do
									local nodid = data[vi]
									if nodid == c_stone
											or nodid == c_destone
											or nodid == c_sastone
											or nodid == c_ice then
										break
									else
										data[vi] = c_column
									end
									vi = vi - emerlen
								end
							end
							end
						end
					elseif pathy >= y0 then
						-- path in chunk
						-- scan disk 5 nodes above path
						local tunnel = false
						local excatop
						for zz = z - 2, z + 2 do
							local vi = area:index(x - 2, pathy + 5, zz)
							for xx = x - 2, x + 2 do
								local nodid = data[vi]
								if nodid == c_stone
										or nodid == c_destone
										or nodid == c_sastone
										or nodid == c_ice then
									tunnel = true
								end
								vi = vi + 1
							end
						end
						if tunnel then
							excatop = pathy + 5 -- tunnel
						else
							excatop = y1 -- excavate to chunk top
						end

						-- place path node brush
						local vi = area:index(x - 2, pathy, z - 2)
						if data[vi] ~= c_wood then
							data[vi] = c_stairne
						end
						for iter = 1, 3 do
							vi = vi + 1
							if data[vi] ~= c_wood then
								data[vi] = c_stairn
							end
						end
						vi = vi + 1
						if data[vi] ~= c_wood then
							data[vi] = c_stairnw
						end
						for zz = z - 1, z + 1 do
							local vi = area:index(x - 2, pathy, zz)
							if data[vi] ~= c_wood then
								data[vi] = c_staire
							end
							for iter = 1, 3 do
								vi = vi + 1
								data[vi] = c_wood
							end
							vi = vi + 1
							if data[vi] ~= c_wood then
								data[vi] = c_stairw
							end
						end
						local vi = area:index(x - 2, pathy, z + 2)
						if data[vi] ~= c_wood then
							data[vi] = c_stairse
						end
						for iter = 1, 3 do
							vi = vi + 1
							if data[vi] ~= c_wood then
								data[vi] = c_stairs
							end
						end
						vi = vi + 1
						if data[vi] ~= c_wood then
							data[vi] = c_stairsw
						end

						-- excavate above path
						local det_destone = false
						local det_sastone = false
						local det_ice = false
						for y = pathy + 1, excatop do
							for zz = z - 2, z + 2 do
								local vi = area:index(x - 2, y, zz)
								for xx = x - 2, x + 2 do
									local nodid = data[vi]
									if nodid == c_destone then
										det_destone = true
									elseif nodid == c_sastone then
										det_sastone = true
									elseif nodid == c_ice then
										det_ice = true
									end
									if tunnel and y == excatop then -- tunnel ceiling
										if nodid ~= c_air
												and nodid ~= c_ignore
												and nodid ~= c_meselamp then
											if math.random() < 0.1 then
												data[vi] = c_meselamp
											elseif det_destone then
												data[vi] = c_destone
											elseif det_sastone then
												data[vi] = c_sastone
											elseif det_ice then
												data[vi] = c_ice
											else
												data[vi] = c_stone
											end
										end
									elseif y <= pathy + 5 then
										if nodid ~= c_wood
												and nodid ~= c_stairn
												and nodid ~= c_stairs
												and nodid ~= c_staire
												and nodid ~= c_stairw
												and nodid ~= c_stairne
												and nodid ~= c_stairnw
												and nodid ~= c_stairse
												and nodid ~= c_stairsw then
											data[vi] = c_air
										end
									else
										data[vi] = c_air
									end
									vi = vi + 1
								end
							end
						end

						-- bridge understructure
						for zz = z - 1, z + 1 do
							local vi = area:index(x - 1, pathy - 1, zz)
							for xx = x - 1, x + 1 do		
								data[vi] = c_column
								vi = vi + 1
							end
						end
						local vi = area:index(x, pathy - 2, z)
						data[vi] = c_column

						-- bridge columns
						if abscol < 0.3 then
							for xx = x - 1, x + 1, 2 do
							for zz = z - 1, z + 1, 2 do
								local vi = area:index(xx, pathy - 2, zz)
								for y = pathy - 2, y0, -1 do
									local nodid = data[vi]
									if nodid == c_stone
											or nodid == c_destone
											or nodid == c_sastone
											or nodid == c_ice then
										break
									else
										data[vi] = c_column
									end
									vi = vi - emerlen
								end
							end
							end
						end
					end
				end
			end

			n_xprepatha = n_patha
			n_xprepathb = n_pathb
			n_xprepathc = n_pathc
			n_xprepathd = n_pathd
			ni = ni + 1
		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 ("[pathv7] Generate chunk " .. chugent .. " ms")
end)