This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.zig
550 lines (414 loc) · 15.1 KB
/
generator.zig
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
const std = @import("std");
const perlin = @import("zig");
var heightMap : [256 * 256]i32 = undefined;
const waterLevel : i32 = 32;
var map_seed: u32 = 0;
var worldData: [*]u8 = undefined;
extern fn onoise(octaves: usize, x: f64, y: f64, seed: u32) f64;
extern fn noise1(x: f32, y: f32) f64;
extern fn noise2(x: f32, y: f32) f64;
extern fn generate_heightmap(hmap: *anyopaque) void;
extern fn smooth_heightmap(hmap: *anyopaque) void;
fn getIdx(x: u32, y: u32, z: u32) usize {
if(x >= 0 and x < 256 and y >= 0 and y < 64 and z >= 0 and z < 256)
return (y * 256 * 256) + (z * 256) + x;
return 0;
}
const RndGen = std.rand.DefaultPrng;
var rnd : RndGen = undefined;
pub export fn generate(seed: u32, data: [*]u8) void {
map_seed = seed;
worldData = data;
rnd = RndGen.init(seed);
//"Raising..."
generate_heightmap(&heightMap);
//"Eroding..."
smooth_heightmap(&heightMap);
//"Soiling..."
create_strata();
//"Carving..."
make_caves();
// create ore veins
make_ores();
//"Watering..."
fill_water();
//"Melting..."
fill_lava();
//"Growing..."
create_surface();
//"Planting..."
create_plants();
}
fn create_strata() void {
var z : usize = 0;
while(z < 256) : (z += 1) {
var x : usize = 0;
while(x < 256) : (x += 1) {
var dirtThickness = onoise(8, @intToFloat(f32, x), @intToFloat(f32, z), map_seed + 1) / 24.0 - 4.0;
var dirtTransition = heightMap[x + z * 256];
var stoneTransition = dirtTransition + @floatToInt(i32, dirtThickness);
var y : usize = 0;
while(y < 64) : (y += 1) {
var bType : u8 = 0;
if(y == 0) {
bType = 10; // LAVA
} else if(y <= stoneTransition) {
bType = 1; // STONE
} else if(y <= dirtTransition) {
bType = 3; // DIRT
}
worldData[getIdx(@intCast(u32, x), @intCast(u32, y), @intCast(u32, z))] = bType;
}
}
}
}
fn fill_water() void {
var z : usize = 0;
while(z < 256) : (z += 1) {
var x : usize = 0;
while(x < 256) : (x += 1) {
var y : usize = @intCast(usize, heightMap[x + z * 256]);
while(y < waterLevel) : (y += 1) {
var idx = getIdx(@intCast(u32, x), @intCast(u32, y), @intCast(u32, z));
var blk = worldData[idx];
if(blk == 0) {
worldData[idx] = 8;
}
}
}
}
}
fn fill_lava() void {
var z : usize = 0;
while(z < 256) : (z += 1) {
var x : usize = 0;
while(x < 256) : (x += 1) {
var y : usize = 0;
while(y < 10) : (y += 1) {
var idx = getIdx(@intCast(u32, x), @intCast(u32, y), @intCast(u32, z));
var blk = worldData[idx];
if(blk == 0) {
worldData[idx] = 8;
}
}
}
}
}
fn create_surface() void {
var z : usize = 0;
while(z < 256) : (z += 1) {
var x : usize = 0;
while(x < 256) : (x += 1) {
var xf = @intToFloat(f32, x);
var zf = @intToFloat(f32, z);
var is_sand = onoise(8, xf, zf, map_seed + 1) > 8.0;
var is_gravel = onoise(8, xf, zf, map_seed + 2) > 12.0;
var y = heightMap[x + z * 256];
var blkA = worldData[getIdx(@intCast(u32, x), @intCast(u32, y + 1), @intCast(u32, z))];
var idx = getIdx(@intCast(u32, x), @intCast(u32, y), @intCast(u32, z));
if(blkA == 8 and is_gravel) {
worldData[idx] = 13;
}
if(blkA == 0) {
if(y <= waterLevel and is_sand) {
worldData[idx] = 12;
} else {
worldData[idx] = 2;
}
}
}
}
}
fn fillOblateSpheroid(cx: i32, cy: i32, cz: i32, radius: f32, blkID: u8) void {
var r = @floatToInt(isize, radius);
var z: isize = cz - r;
while(z < cz + r) : (z += 1) {
var y: isize = cy - r;
while(y < cy + r) : (y += 1) {
var x: isize = cx - r;
while(x < cx + r) : (x += 1) {
var dx = x - cx;
var dy = y - cy;
var dz = z - cz;
if(dx * dx + 2 * dy * dy + dz * dz < r * r) {
if(x >= 0 and x < 256 and y >= 0 and y < 64 and z >= 0 and z < 256) {
var idx = getIdx(@intCast(u32, x), @intCast(u32, y), @intCast(u32, z));
var blk = worldData[idx];
if(blk == 1) {
worldData[idx] = blkID;
}
}
}
}
}
}
}
fn create_vein(abundance: f32, oreBlockID: u8) void {
var vx = @intToFloat(f32, rnd.random().int(u32) % 256);
var vy = @intToFloat(f32, rnd.random().int(u32) % 64);
var vz = @intToFloat(f32, rnd.random().int(u32) % 256);
var vl = rnd.random().float(f32) * rnd.random().float(f32) * 150 * abundance;
var theta = rnd.random().float(f32) * 3.14159 * 2.0;
var deltaTheta : f32 = 0;
var phi = rnd.random().float(f32) * 3.14159 * 2.0;
var deltaPhi : f32 = 0;
var len : usize = 0;
while(len < @floatToInt(usize, vl)) : (len += 1) {
vx += std.math.sin(theta) * std.math.cos(phi);
vy += std.math.cos(theta) * std.math.cos(phi);
vz += std.math.sin(phi);
theta = deltaTheta * 0.2;
deltaTheta *= 0.9;
deltaTheta += rnd.random().float(f32) - rnd.random().float(f32);
phi /= 2.0;
phi += deltaPhi / 4.0;
// unlike deltaPhi for caves, previous value is multiplied by 0.9 instead of 0.75
deltaPhi *= 0.9;
deltaPhi += rnd.random().float(f32) - rnd.random().float(f32);
var radius = abundance * std.math.sin(@intToFloat(f32,len) * 3.14159 / vl) + 1;
fillOblateSpheroid(@floatToInt(i32, vx), @floatToInt(i32, vy), @floatToInt(i32, vz), radius, oreBlockID);
}
}
fn make_ores() void {
var numCoal = @floatToInt(usize, 256.0 * 64.0 * 256.0 * 0.9 / 16384.0);
var numIron = @floatToInt(usize, 256.0 * 64.0 * 256.0 * 0.7 / 16384.0);
var numGold = @floatToInt(usize, 256.0 * 64.0 * 256.0 * 0.5 / 16384.0);
var i: usize = 0;
while(i < numCoal) : (i += 1) {
create_vein(0.9, 16);
}
i = 0;
while(i < numIron) : (i += 1) {
create_vein(0.7, 15);
}
i = 0;
while(i < numGold) : (i += 1) {
create_vein(0.5, 14);
}
}
fn create_cave() void {
var vx = @intToFloat(f32, rnd.random().int(u32) % 256);
var vy = @intToFloat(f32, rnd.random().int(u32) % 64);
var vz = @intToFloat(f32, rnd.random().int(u32) % 256);
var vl = std.math.floor(rnd.random().float(f32) + rnd.random().float(f32)) * 200;
var theta = rnd.random().float(f32) * 3.14159 * 2.0;
var deltaTheta : f32 = 0;
var phi = rnd.random().float(f32) * 3.14159 * 2.0;
var deltaPhi : f32 = 0;
var caveRadius = rnd.random().float(f32) * rnd.random().float(f32) * 2.0;
var len : usize = 0;
while(len < @floatToInt(usize, vl)) : (len += 1) {
vx += std.math.sin(theta) * std.math.cos(phi);
vy += std.math.cos(theta) * std.math.cos(phi);
vz += std.math.sin(phi);
theta += deltaTheta * 0.2;
deltaTheta *= 0.9;
deltaTheta += rnd.random().float(f32) - rnd.random().float(f32);
phi /= 2.0;
phi += deltaPhi / 4.0;
deltaPhi *= 0.75;
deltaPhi += rnd.random().float(f32) - rnd.random().float(f32);
if(rnd.random().float(f32) >= 0.25) {
var cx = vx + @intToFloat(f32, @mod(rnd.random().int(i32), 4) - 2) * 0.2;
var cy = vy + @intToFloat(f32, @mod(rnd.random().int(i32), 4) - 2) * 0.2;
var cz = vz + @intToFloat(f32, @mod(rnd.random().int(i32), 4) - 2) * 0.2;
var radius = (64 - cy) / 64;
radius = 1.2 + (radius * 3.5 + 1) * caveRadius;
radius = radius * std.math.sin(@intToFloat(f32, len) * 3.14159 / vl);
fillOblateSpheroid(@floatToInt(i32, cx), @floatToInt(i32, cy), @floatToInt(i32, cz), radius, 0);
}
}
}
fn make_caves() void {
var numCaves : usize = 256 * 64 * 256 / 8192;
var i : usize = 0;
while(i < numCaves) : (i += 1) {
create_cave();
}
}
fn create_flowers() void {
var flowerType = @mod(rnd.random().int(u8), 2) + 37;
var px = @mod(rnd.random().int(usize), 256);
var pz = @mod(rnd.random().int(usize), 256);
var i : usize = 0;
while(i < 10) : (i += 1) {
var fx = px;
var fz = pz;
var c: usize = 0;
while(c < 5) : (c += 1) {
fx += @mod(rnd.random().int(usize), 6);
var sub = @mod(rnd.random().int(usize), 6);
if(fx > sub)
fx -= sub;
sub = @mod(rnd.random().int(usize), 6);
fz += @mod(rnd.random().int(usize), 6);
if(fz > sub)
fz -= sub;
if(fx >= 0 and fx < 256 and fz >= 0 and fz < 256) {
var fy = heightMap[@intCast(usize, fx + fz * 256)] + 1;
var blk = worldData[getIdx(@intCast(u32, fx), @intCast(u32, fy), @intCast(u32, fz))];
var blkB = worldData[getIdx(@intCast(u32, fx), @intCast(u32, fy) - 1, @intCast(u32, fz))];
if(blk == 0 and blkB == 2) {
worldData[getIdx(@intCast(u32, fx), @intCast(u32, fy), @intCast(u32, fz))] = flowerType;
}
}
}
}
}
fn create_mushrooms() void {
var mushType = @mod(rnd.random().int(u8), 2) + 39;
var px = @mod(rnd.random().int(usize), 256);
var py = @mod(rnd.random().int(usize), 64);
var pz = @mod(rnd.random().int(usize), 256);
var i : usize = 0;
while(i < 20) : (i += 1) {
var fx = px;
var fy = py;
var fz = pz;
var c: usize = 0;
while(c < 5) : (c += 1) {
fx += @mod(rnd.random().int(usize), 6);
var sub = @mod(rnd.random().int(usize), 6);
if(fx > sub)
fx -= sub;
sub = @mod(rnd.random().int(usize), 6);
fz += @mod(rnd.random().int(usize), 6);
if(fz > sub)
fz -= sub;
sub = @mod(rnd.random().int(usize), 2);
fy += @mod(rnd.random().int(usize), 2);
if(fy > sub)
fy -= sub;
if(fx >= 0 and fx < 256 and fz >= 0 and fz < 256 and fy > 0 and fy < 64) {
var blk = worldData[getIdx(@intCast(u32, fx), @intCast(u32, fy), @intCast(u32, fz))];
var blkB = worldData[getIdx(@intCast(u32, fx), @intCast(u32, fy) - 1, @intCast(u32, fz))];
if(blk == 0 and blkB == 1) {
worldData[getIdx(@intCast(u32, fx), @intCast(u32, fy), @intCast(u32, fz))] = mushType;
}
}
}
}
}
pub fn growTree(x: u32, y: u32, z: u32, h: u32) bool {
if(x > 2 and z > 2 and y > 0 and y < 50 and x < 254 and z < 254){
if(isSpaceForTree(x, y, z)){
const max = y + h;
var m : u32 = max;
while(m >= y) : (m -= 1) {
if(m == max) {
worldData[getIdx(x - 1, m, z)] = 18;
worldData[getIdx(x + 1, m, z)] = 18;
worldData[getIdx(x, m, z - 1)] = 18;
worldData[getIdx(x, m, z + 1)] = 18;
worldData[getIdx(x, m, z)] = 18;
} else if(m == max - 1) {
worldData[getIdx(x - 1, m, z)] = 18;
worldData[getIdx(x + 1, m, z)] = 18;
worldData[getIdx(x, m, z - 1)] = 18;
worldData[getIdx(x, m, z + 1)] = 18;
if(@mod(rnd.random().int(usize), 2) == 0)
worldData[getIdx(x - 1, m, z - 1)] = 18;
if(@mod(rnd.random().int(usize), 2) == 0)
worldData[getIdx(x + 1, m, z - 1)] = 18;
if(@mod(rnd.random().int(usize), 2) == 0)
worldData[getIdx(x - 1, m, z + 1)] = 18;
if(@mod(rnd.random().int(usize), 2) == 0)
worldData[getIdx(x + 1, m, z + 1)] = 18;
worldData[getIdx(x, m, z)] = 17;
} else if(m == max - 2 or m == max - 3) {
worldData[getIdx(x - 1, m, z)] = 18;
worldData[getIdx(x + 1, m, z)] = 18;
worldData[getIdx(x, m, z - 1)] = 18;
worldData[getIdx(x, m, z + 1)] = 18;
worldData[getIdx(x - 1, m, z - 1)] = 18;
worldData[getIdx(x + 1, m, z - 1)] = 18;
worldData[getIdx(x - 1, m, z + 1)] = 18;
worldData[getIdx(x + 1, m, z + 1)] = 18;
worldData[getIdx(x - 2, m, z - 1)] = 18;
worldData[getIdx(x - 2, m, z)] = 18;
worldData[getIdx(x - 2, m, z + 1)] = 18;
worldData[getIdx(x + 2, m, z - 1)] = 18;
worldData[getIdx(x + 2, m, z)] = 18;
worldData[getIdx(x + 2, m, z + 1)] = 18;
worldData[getIdx(x - 1, m, z + 2)] = 18;
worldData[getIdx(x, m, z + 2)] = 18;
worldData[getIdx(x + 1, m, z + 2)] = 18;
worldData[getIdx(x - 1, m, z - 2)] = 18;
worldData[getIdx(x, m, z - 2)] = 18;
worldData[getIdx(x + 1, m, z - 2)] = 18;
if(@mod(rnd.random().int(usize), 2) == 0)
worldData[getIdx(x - 2, m, z - 2)] = 18;
if(@mod(rnd.random().int(usize), 2) == 0)
worldData[getIdx(x + 2, m, z - 2)] = 18;
if(@mod(rnd.random().int(usize), 2) == 0)
worldData[getIdx(x - 2, m, z + 2)] = 18;
if(@mod(rnd.random().int(usize), 2) == 0)
worldData[getIdx(x + 2, m, z + 2)] = 18;
worldData[getIdx(x, m, z)] = 17;
} else {
worldData[getIdx(x, m, z)] = 17;
}
}
return true;
}
}
return false;
}
fn isSpaceForTree(x: u32, y: u32, z: u32) bool {
var i : u32 = 0;
while(i < 6) : (i += 1) {
var cx = x - 2;
while(cx <= x + 2) : (cx += 1) {
var cz = z - 2;
while(cz <= z + 2) : (cz += 1) {
var idx = getIdx(cx, y + i, cz);
var blk = worldData[idx];
if(blk != 0)
return false;
}
}
}
return true;
}
fn create_tree() void {
var px = @mod(rnd.random().int(usize), 256);
var pz = @mod(rnd.random().int(usize), 256);
var i : usize = 0;
while(i < 20) : (i += 1) {
var tx = px;
var tz = pz;
var c: usize = 0;
while(c < 20) : (c += 1) {
tx += @mod(rnd.random().int(usize), 6);
var sub = @mod(rnd.random().int(usize), 6);
if(tx > sub)
tx -= sub;
sub = @mod(rnd.random().int(usize), 6);
tz += @mod(rnd.random().int(usize), 6);
if(tz > sub)
tz -= sub;
if(tx >= 0 and tx < 256 and tz >= 0 and tz < 256 and rnd.random().float(f32) <= 0.25) {
var ty = heightMap[@intCast(usize, tx + tz * 256)] + 1;
var th = 4 + @mod(rnd.random().int(usize), 3);
_ = growTree(@intCast(u32, tx), @intCast(u32, ty), @intCast(u32, tz), @intCast(u32, th));
}
}
}
}
fn create_plants() void {
const numFlowers = 256 * 256 / 3000;
var i : usize = 0;
while(i < numFlowers) : (i += 1) {
create_flowers();
}
const numMushrooms = 256 * 256 * 64 / 2000;
i = 0;
while(i < numMushrooms) : (i += 1) {
create_mushrooms();
}
const numTrees = 256 * 256 / 4000;
i = 0;
while(i < numTrees) : (i += 1) {
create_tree();
}
}