Skip to content

Commit 2c490dd

Browse files
authoredMar 29, 2018
Mgcarpathian: Mapgen loop optimisations. fabs() -> std::fabs()
* Mgcarpathian: ZYX -> ZXY mapgen loop optimisation. * 'pow(n, 3)' to 'n * n * n' type optimisations. * fabs() -> std::fabs().
1 parent 8f827ee commit 2c490dd

File tree

1 file changed

+80
-72
lines changed

1 file changed

+80
-72
lines changed
 

Diff for: ‎src/mapgen/mapgen_carpathian.cpp

+80-72
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void MapgenCarpathianParams::writeParams(Settings *settings) const
192192
}
193193

194194

195-
///////////////////////////////////////////////////////////////////////////////
195+
////////////////////////////////////////////////////////////////////////////////
196196

197197

198198
// Lerp function
@@ -212,7 +212,7 @@ float MapgenCarpathian::getSteps(float noise)
212212
}
213213

214214

215-
///////////////////////////////////////////////////////////////////////////////
215+
////////////////////////////////////////////////////////////////////////////////
216216

217217

218218
void MapgenCarpathian::makeChunk(BlockMakeData *data)
@@ -298,7 +298,7 @@ void MapgenCarpathian::makeChunk(BlockMakeData *data)
298298
}
299299

300300

301-
///////////////////////////////////////////////////////////////////////////////
301+
////////////////////////////////////////////////////////////////////////////////
302302

303303

304304
int MapgenCarpathian::getSpawnLevelAtPoint(v2s16 p)
@@ -338,14 +338,15 @@ float MapgenCarpathian::terrainLevelAtPoint(s16 x, s16 z)
338338
float hill2 = getLerp(height3, height4, mnt_var);
339339
float hill3 = getLerp(height3, height2, mnt_var);
340340
float hill4 = getLerp(height1, height4, mnt_var);
341-
float hilliness = std::fmax(std::fmin(hill1, hill2), std::fmin(hill3, hill4));
341+
float hilliness =
342+
std::fmax(std::fmin(hill1, hill2), std::fmin(hill3, hill4));
342343

343344
// Rolling hills
344345
float hill_mnt = hilliness * pow(n_hills, 2.f);
345346
float hills = pow(hter, 3.f) * hill_mnt;
346347

347348
// Ridged mountains
348-
float ridge_mnt = hilliness * (1.f - fabs(n_ridge_mnt));
349+
float ridge_mnt = hilliness * (1.f - std::fabs(n_ridge_mnt));
349350
float ridged_mountains = pow(rter, 3.f) * ridge_mnt;
350351

351352
// Step (terraced) mountains
@@ -364,7 +365,7 @@ float MapgenCarpathian::terrainLevelAtPoint(s16 x, s16 z)
364365
}
365366

366367

367-
///////////////////////////////////////////////////////////////////////////////
368+
////////////////////////////////////////////////////////////////////////////////
368369

369370

370371
int MapgenCarpathian::generateTerrain()
@@ -373,10 +374,6 @@ int MapgenCarpathian::generateTerrain()
373374
MapNode mn_stone(c_stone);
374375
MapNode mn_water(c_water_source);
375376

376-
s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
377-
u32 index2d = 0;
378-
u32 index3d = 0;
379-
380377
// Calculate noise for terrain generation
381378
noise_base->perlinMap2D(node_min.X, node_min.Z);
382379
noise_height1->perlinMap2D(node_min.X, node_min.Z);
@@ -392,70 +389,81 @@ int MapgenCarpathian::generateTerrain()
392389
noise_mnt_var->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
393390

394391
//// Place nodes
395-
for (s16 z = node_min.Z; z <= node_max.Z; z++) {
396-
for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
397-
u32 vi = vm->m_area.index(node_min.X, y, z);
398-
for (s16 x = node_min.X; x <= node_max.X;
399-
x++, vi++, index2d++, index3d++) {
400-
if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
401-
continue;
402-
403-
// Base terrain
404-
float ground = noise_base->result[index2d];
405-
406-
// Gradient & shallow seabed
407-
s32 grad = (y < water_level) ? grad_wl + (water_level - y) * 3 : 1 - y;
408-
409-
// Hill/Mountain height (hilliness)
410-
float height1 = noise_height1->result[index2d];
411-
float height2 = noise_height2->result[index2d];
412-
float height3 = noise_height3->result[index2d];
413-
float height4 = noise_height4->result[index2d];
414-
float mnt_var = noise_mnt_var->result[index3d];
415-
// Combine height noises and apply 3D variation
416-
float hill1 = getLerp(height1, height2, mnt_var);
417-
float hill2 = getLerp(height3, height4, mnt_var);
418-
float hill3 = getLerp(height3, height2, mnt_var);
419-
float hill4 = getLerp(height1, height4, mnt_var);
420-
// 'hilliness' determines whether hills/mountains are
421-
// small or large
422-
float hilliness = std::fmax(std::fmin(hill1, hill2), std::fmin(hill3, hill4));
423-
424-
// Rolling hills
425-
float hter = noise_hills_terrain->result[index2d];
426-
float n_hills = noise_hills->result[index2d];
427-
float hill_mnt = hilliness * pow(n_hills, 2.f);
428-
float hills = pow(fabs(hter), 3.f) * hill_mnt;
429-
430-
// Ridged mountains
431-
float rter = noise_ridge_terrain->result[index2d];
432-
float n_ridge_mnt = noise_ridge_mnt->result[index2d];
433-
float ridge_mnt = hilliness * (1.f - fabs(n_ridge_mnt));
434-
float ridged_mountains = pow(fabs(rter), 3.f) * ridge_mnt;
435-
436-
// Step (terraced) mountains
437-
float ster = noise_step_terrain->result[index2d];
438-
float n_step_mnt = noise_step_mnt->result[index2d];
439-
float step_mnt = hilliness * getSteps(n_step_mnt);
440-
float step_mountains = pow(fabs(ster), 3.f) * step_mnt;
441-
442-
// Final terrain level
443-
float mountains = hills + ridged_mountains + step_mountains;
444-
float surface_level = ground + mountains + grad;
445-
446-
if (y < surface_level) {
447-
vm->m_data[vi] = mn_stone; // Stone
448-
if (y > stone_surface_max_y)
449-
stone_surface_max_y = y;
450-
} else if (y <= water_level) {
451-
vm->m_data[vi] = mn_water; // Sea water
452-
} else {
453-
vm->m_data[vi] = mn_air; // Air
454-
}
392+
const v3s16 &em = vm->m_area.getExtent();
393+
s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
394+
u32 index2d = 0;
395+
396+
for (s16 z = node_min.Z; z <= node_max.Z; z++)
397+
for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
398+
// Base terrain
399+
float ground = noise_base->result[index2d];
400+
401+
// Hill/Mountain height (hilliness)
402+
float height1 = noise_height1->result[index2d];
403+
float height2 = noise_height2->result[index2d];
404+
float height3 = noise_height3->result[index2d];
405+
float height4 = noise_height4->result[index2d];
406+
407+
// Rolling hills
408+
float hterabs = std::fabs(noise_hills_terrain->result[index2d]);
409+
float n_hills = noise_hills->result[index2d];
410+
float hill_mnt = hterabs * hterabs * hterabs * n_hills * n_hills;
411+
412+
// Ridged mountains
413+
float rterabs = std::fabs(noise_ridge_terrain->result[index2d]);
414+
float n_ridge_mnt = noise_ridge_mnt->result[index2d];
415+
float ridge_mnt = rterabs * rterabs * rterabs *
416+
(1.f - std::fabs(n_ridge_mnt));
417+
418+
// Step (terraced) mountains
419+
float sterabs = std::fabs(noise_step_terrain->result[index2d]);
420+
float n_step_mnt = noise_step_mnt->result[index2d];
421+
float step_mnt = sterabs * sterabs * sterabs * getSteps(n_step_mnt);
422+
423+
// Initialise 3D noise index and voxelmanip index to column base
424+
u32 index3d = (z - node_min.Z) * zstride_1u1d + (x - node_min.X);
425+
u32 vi = vm->m_area.index(x, node_min.Y - 1, z);
426+
427+
for (s16 y = node_min.Y - 1; y <= node_max.Y + 1;
428+
y++,
429+
index3d += ystride,
430+
VoxelArea::add_y(em, vi, 1)) {
431+
if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
432+
continue;
433+
434+
// Combine height noises and apply 3D variation
435+
float mnt_var = noise_mnt_var->result[index3d];
436+
float hill1 = getLerp(height1, height2, mnt_var);
437+
float hill2 = getLerp(height3, height4, mnt_var);
438+
float hill3 = getLerp(height3, height2, mnt_var);
439+
float hill4 = getLerp(height1, height4, mnt_var);
440+
441+
// 'hilliness' determines whether hills/mountains are
442+
// small or large
443+
float hilliness =
444+
std::fmax(std::fmin(hill1, hill2), std::fmin(hill3, hill4));
445+
float hills = hill_mnt * hilliness;
446+
float ridged_mountains = ridge_mnt * hilliness;
447+
float step_mountains = step_mnt * hilliness;
448+
449+
// Gradient & shallow seabed
450+
s32 grad = (y < water_level) ? grad_wl + (water_level - y) * 3 :
451+
1 - y;
452+
453+
// Final terrain level
454+
float mountains = hills + ridged_mountains + step_mountains;
455+
float surface_level = ground + mountains + grad;
456+
457+
if (y < surface_level) {
458+
vm->m_data[vi] = mn_stone; // Stone
459+
if (y > stone_surface_max_y)
460+
stone_surface_max_y = y;
461+
} else if (y <= water_level) {
462+
vm->m_data[vi] = mn_water; // Sea water
463+
} else {
464+
vm->m_data[vi] = mn_air; // Air
455465
}
456-
index2d -= ystride;
457466
}
458-
index2d += ystride;
459467
}
460468

461469
return stone_surface_max_y;

0 commit comments

Comments
 (0)