(info) Other great resources: Official JS API docs, Scripting Forum

getThreatsNear(x,y[,range])

Returns an array of dangerous enemy objects near a specific location on the map.

 

Code

const DEFAULT_THREAT_RANGE = 10; // default range (in tiles) for getThreatsNear() function


// getThreatsNear(x,y[,range]) will return an array of dangerous enemy objects near x,y

var getThreatsNear = (function() {
  var oldApproach = function(x,y,range) {
    range = range || DEFAULT_THREAT_RANGE;
    var enemies = getEnemyPlayerIDs();
    var list = [];
    // build list of dangerous enemy objects
    enemies.forEach(function(enemy) {
      // use specific enums to reduce size of list
      list = list.concat(enumDroid(enemy,DROID_CYBORG));
      list = list.concat(enumDroid(enemy,DROID_WEAPON));
      list = list.concat(enumStruct(enemy,DEFENSE));
    });
    // filter to those within range
    list = list.filter(function(obj) {
      return (distBetweenTwoPoints(obj.x,obj.y,x,y) < range);
    });
    return list;
  }
  var newApproach = function(x,y,range) {
    range = range || DEFAULT_THREAT_RANGE;
    // get list of all enemy objects within range
    var list = enumRange(x,y,range,ENEMIES);
    // filter to dangerous objects
    list = list.filter(function(obj) {
      switch (obj.type) {
        case DROID: return (obj.droidType == DROID_WEAPON || obj.droidType == DROID_CYBORG);
        case STRUCTURE: return (obj.stattype == DEFENCE);
      }
      return false;
    });
    return list;
  }
  // return relevant function based on presence of enumRange function
  return (typeof enumRange != "undefined") ? newApproach : oldApproach;
})();

Example

// quantify the danger level of a location based
// on number of dangerous enemy objects nearby
function dangerLevel(x,y,range) {
  return getThreatsNear(x,y,range).length;
}
 
// you could then use dangerLevel() function to work
// out which enemy oil derrick is the safest to attack
function safestEnemyDerrick() {
  var enemyDerricks = [];
  var enemies = getEnemyPlayerIDs();
  var danger;
  // build list of enemy derricks...
  enemies.forEach(function(enemy) {
    enemyDerricks = enemyDerricks.concat(enumStruct(enemy,RESOURCE_EXTRACTOR));
  });
  // if enemy has none, return null...
  if (enemyDerricks.length == 0) return null;
  // find safest enemy derrick to attack...
  enemyDerricks.currentDanger = Number.MAX_VALUE;
  enemyDerricks.forEach(function(derrick) {
    danger = dangerLevel(derrick.x,derrick.y);
    if (danger < enemyDerricks.currentDanger) {
      enemyDerricks.currentDanger = danger;
      enemyDerricks.safestDerrick = derrick;
    }
  });
  return enemyDerricks.safestDerrick;
}

Availability

Requires:

See also

Related articles: