summaryrefslogtreecommitdiff
path: root/src/mg_ore.cpp
diff options
context:
space:
mode:
authorparamat <mat.gregory@virginmedia.com>2016-02-21 13:28:34 +0000
committerparamat <mat.gregory@virginmedia.com>2016-02-23 23:51:29 +0000
commit8591713405419c434801c61ca59a9b6c019eb957 (patch)
tree48434b5d10f72ec8309e65fae1c32aae231daf5f /src/mg_ore.cpp
parent147425483a9e1afa2a2a4d9c0d5fa8b68d105644 (diff)
downloadminetest-8591713405419c434801c61ca59a9b6c019eb957.tar.gz
minetest-8591713405419c434801c61ca59a9b6c019eb957.tar.bz2
minetest-8591713405419c434801c61ca59a9b6c019eb957.zip
Sheet Ore: Eliminate crash caused by PcgRandom range max < min
In the calculation of y_start, when 'column height max' was large it caused nmin.Y + max_height > nmax.Y - max_height Now, in this situation y_start is set to the midpoint between nmin.Y and nmax.Y Limit y0 and y1 to between nmin.Y and nmax.Y, otherwise index calculation, which has no checks for limits, places them at unwanted locations in the voxelmanip
Diffstat (limited to 'src/mg_ore.cpp')
-rw-r--r--src/mg_ore.cpp13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/mg_ore.cpp b/src/mg_ore.cpp
index 6b6e0d7a7..257901614 100644
--- a/src/mg_ore.cpp
+++ b/src/mg_ore.cpp
@@ -179,7 +179,12 @@ void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
MapNode n_ore(c_ore, 0, ore_param2);
u16 max_height = column_height_max;
- int y_start = pr.range(nmin.Y + max_height, nmax.Y - max_height);
+ int y_start_min = nmin.Y + max_height;
+ int y_start_max = nmax.Y - max_height;
+
+ int y_start = y_start_min < y_start_max ?
+ pr.range(y_start_min, y_start_max) :
+ (y_start_min + y_start_max) / 2;
if (!noise) {
int sx = nmax.X - nmin.X + 1;
@@ -204,10 +209,10 @@ void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
u16 height = pr.range(column_height_min, column_height_max);
int ymidpoint = y_start + noiseval;
- int y0 = ymidpoint - height * (1 - column_midpoint_factor);
- int y1 = y0 + height;
+ int y0 = MYMAX(nmin.Y, ymidpoint - height * (1 - column_midpoint_factor));
+ int y1 = MYMIN(nmax.Y, y0 + height - 1);
- for (int y = y0; y < y1; y++) {
+ for (int y = y0; y <= y1; y++) {
u32 i = vm->m_area.index(x, y, z);
if (!vm->m_area.contains(i))
continue;