Other great resources: Official JS API docs, Scripting Forum
getThreatsNear(x,y[,range])
- Aubergine
Owned by Aubergine
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; }
See also
Related articles:
- DEFENSE – a defensive structure (eg. howitzer emplacement)
- distBetweenTwoPoints() – calculates distance between two points on the map
- DROID – a droid game object
- DROID_CYBORG – a cyborg weapon droid
- DROID_WEAPON – a tank/vtol weapon droid
- ENEMIES – a constant that represents "all enemy players" to the JS API
- enumDroid() – gets a list of droids
- enumRange() – gets a list of objects within a given range of a map location
- enumStruct() – gets a list of structures
- getEnemyPlayerIDs() – returns an array of all enemy (including scavs) player IDs
- RESOURCE_EXTRACTOR – an oil derrick
- STRUCTURE – a structure game object