Ticket #4012: placer_fix2016-6-28.diff

File placer_fix2016-6-28.diff, 3.8 KB (added by FeXoR, 8 years ago)

Fixes SimpleObject.place() placing entities outside playable map and add const MAP_BORDER_WIDTH

  • binaries/data/mods/public/maps/random/rmgen/library.js

     
    66const HEIGHT_UNITS_PER_METRE = 92;
    77const MIN_MAP_SIZE = 128;
    88const MAX_MAP_SIZE = 512;
     9const MAP_BORDER_WIDTH = 3;
    910const FALLBACK_CIV = "athen";
    1011/**
    1112 * Constants needed for heightmap_manipulation.js
     
    283284
    284285function placeObject(x, z, type, player, angle)
    285286{
    286     if (g_Map.validT(x, z, 3))
     287    if (g_Map.validT(x, z, MAP_BORDER_WIDTH))
    287288        g_Map.addObject(new Entity(type, player, x, z, angle));
    288289}
    289290
  • binaries/data/mods/public/maps/random/rmgen/placer.js

     
    453453            {
    454454                failCount++;
    455455                if (failCount > maxFailCount)
    456                 {
    457456                    return undefined;
    458                 }
    459457            }
    460458        }
    461459    }
     
    555553            {
    556554                failCount++;
    557555                if (failCount > maxFailCount)
    558                 {
    559556                    return undefined;
    560                 }
    561557            }
    562558        }
    563559    }
     
    602598        else
    603599        {
    604600            for (var j = 0; j < objs.length; ++j)
    605             {
    606601                resultObjs.push(objs[j]);
    607             }
    608602        }
    609603    }
    610604   
     
    612606    length = resultObjs.length;
    613607    for (var i=0; i < length; i++)
    614608    {
    615         if (g_Map.validT(round(resultObjs[i].position.x/CELL_SIZE), round(resultObjs[i].position.z/CELL_SIZE), 3))
     609        if (g_Map.validT(resultObjs[i].position.x / CELL_SIZE, resultObjs[i].position.z / CELL_SIZE, MAP_BORDER_WIDTH))
    616610            g_Map.addObject(resultObjs[i]);
    617611       
     612        // Convert position to integer number of tiles
    618613        if (this.tileClass !== undefined)
    619         {   // Convert position to integer number of tiles
    620614            this.tileClass.add(Math.floor(resultObjs[i].position.x/CELL_SIZE), Math.floor(resultObjs[i].position.z/CELL_SIZE));
    621         }
    622615    }
    623616   
    624617    return true;
     
    653646    var placer = this.elements[randInt(this.elements.length)];
    654647   
    655648    var objs = placer.place(this.x, this.z, player, this.avoidSelf, constraint);
     649    // Failure
    656650    if (objs === undefined)
    657     {   // Failure
     651    {
    658652        return false;
    659653    }
    660654    else
    661655    {
    662656        for (var j = 0; j < objs.length; ++j)
    663         {
    664657            resultObjs.push(objs[j]);
    665         }
    666658    }
    667659   
    668660    // Add placed objects to map
     
    671663    {
    672664        g_Map.addObject(resultObjs[i]);
    673665       
     666        // Convert position to integer number of tiles
    674667        if (this.tileClass !== undefined)
    675         {   // Convert position to integer number of tiles
    676668            this.tileClass.add(Math.floor(resultObjs[i].position.x/CELL_SIZE), Math.floor(resultObjs[i].position.z/CELL_SIZE));
    677         }
    678669    }
    679670   
    680671    return true;
  • binaries/data/mods/public/maps/random/rmgen/terrain.js

     
    3131function SimpleTerrain(texture, treeType)
    3232{
    3333    if (texture === undefined)
    34     {
    3534        throw("SimpleTerrain: texture not defined");
    36     }
    3735   
    3836    this.texture = texture;
    3937    this.treeType = treeType;
     
    4341SimpleTerrain.prototype.constructor = SimpleTerrain;
    4442SimpleTerrain.prototype.placeNew = function(x, z)
    4543{
    46     if (this.treeType !== undefined && g_Map.validT(round(x), round(z), 3))
    47     {
     44    if (this.treeType !== undefined && g_Map.validT(round(x), round(z), MAP_BORDER_WIDTH))
    4845        g_Map.terrainObjects[x][z] = new Entity(this.treeType, 0, x+0.5, z+0.5, randFloat()*TWO_PI);
    49     }
    5046   
    5147    g_Map.texture[x][z] = g_Map.getTextureID(this.texture);
    5248};
     
    6359function RandomTerrain(terrains)
    6460{
    6561    if (!(terrains instanceof Array) || !terrains.length)
    66     {
    6762        throw("RandomTerrain: Invalid terrains array");
    68     }
    6963   
    7064    this.terrains = terrains;
    7165}