Ticket #4152: heightmapFix2016-12-5.patch

File heightmapFix2016-12-5.patch, 3.3 KB (added by FeXoR, 7 years ago)

Early continue suggested by elexis and Vladislav

  • binaries/data/mods/public/maps/random/heightmap/heightmap.js

     
    107107 * @param {array} [entityList=[g_Gaia.stoneLarge, g_Gaia.metalLarge]] - Entity/actor strings to be placed with placeObject()
    108108 * @param {integer} [maxTries=1000] - How often random player distributions are rolled to be compared
    109109 * @param {boolean} [isCircular=g_MapSettings.CircularMap] - If the map is circular or rectangular
     110 * @return {array} [placements] Array of points where entities were placed
    110111 */
    111112function distributeEntitiesByHeight(heightRange, avoidPoints, minDistance = 30, entityList = [g_Gaia.stoneLarge, g_Gaia.metalLarge], maxTries = 1000, heightmap = g_Map.height, isCircular = g_MapSettings.CircularMap)
    112113{
    113     let placements = deepcopy(avoidPoints);
    114     let validTiles = [];
     114    let validPoints = [];
    115115    let r = 0.5 * (heightmap.length - 1); // Map center x/y as well as radius
    116116    for (let x = minDistance; x < heightmap.length - minDistance; ++x)
     117    {
    117118        for (let y = minDistance; y < heightmap[0].length - minDistance; ++y)
    118             if (heightmap[x][y] > heightRange.min && heightmap[x][y] < heightRange.max) // Has the right height
    119                 if (!isCircular || r - getDistance(x, y, r, r) >= minDistance) // Is far enough away from map border
    120                     validTiles.push({ "x": x, "y": y });
     119        {
     120            if (heightmap[x][y] < heightRange.min || heightmap[x][y] > heightRange.max)
     121                continue; // Out of height range
     122            if (isCircular && r - getDistance(x + 0.5, y + 0.5, r, r) < minDistance)
     123                continue; // Too close to map border
    121124
     125            // Avoid avoidPoints by minDistance
     126            let isValid = true;
     127            for (let i = 0; i < avoidPoints.length; ++i)
     128            {
     129                if (getDistance(x + 0.5, y + 0.5, avoidPoints[i].x, avoidPoints[i].y) < minDistance)
     130                {
     131                    isValid = false;
     132                    break;
     133                }
     134            }
     135            if (isValid)
     136                validPoints.push({ "x": x + 0.5, "y": y + 0.5 });
     137        }
     138    }
     139
     140    let placements = [];
     141    // Early return if no valid poins found
     142    if (!validPoints.length)
     143    {
     144        log("No placement points found for the given arguments: " +  new Error().stack);
     145        return placements;
     146    }
     147
    122148    for (let tries = 0; tries < maxTries; ++tries)
    123149    {
    124         let tile = validTiles[randInt(validTiles.length)];
     150       
     151        let checkPointIndex = randInt(validPoints.length);
     152        let checkPoint = validPoints[checkPointIndex];
    125153        let isValid = true;
    126154        for (let p = 0; p < placements.length; ++p)
    127155        {
    128             if (getDistance(placements[p].x, placements[p].y, tile.x, tile.y) < minDistance)
     156            if (getDistance(placements[p].x, placements[p].y, checkPoint.x, checkPoint.y) < minDistance)
    129157            {
    130158                isValid = false;
    131159                break;
     
    133161        }
    134162        if (isValid)
    135163        {
    136             placeObject(tile.x, tile.y, entityList[randInt(entityList.length)], 0, randFloat(0, 2*PI));
    137             placements.push(tile);
     164            placeObject(checkPoint.x, checkPoint.y, entityList[randInt(entityList.length)], 0, randFloat(0, 2*PI));
     165            placements.push(checkPoint);
    138166        }
     167
     168        validPoints.splice(checkPointIndex);
     169        if (!validPoints.length) // No more valid points left
     170            break;
    139171    }
     172
     173    if (!placements.length)
     174        log("Nothing was placed: " +  new Error().stack);
     175
     176    return placements;
    140177}
    141178
    142179/**