Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
themeEclipse
languagejavascript
titleWork out who our main aggressors are
linenumberstrue
// make an array to count attacks by each player
var attackers = new Array(playerData.length);
// initialise values in array
for (var i=0;i<attackers.length;i++) attackers[i] = 0;
 
// now monitor attacks
function eventAttacked(victim,attacker) {
  // increment counter for the attacking player
  attackers[attacker.player] += 1;
  // find player who has attacked us most
  var maxAttacks = 0;
  var mainAggressor = attacker.player;
  for (var i=0;i<attackers.length;i++) {
    if (attackers[i]>maxAttacks) {
      maxAttacks = attackers[i];
      mainAggressor = i;
    }
  }
  console("Attacked "+maxAttacks+" time(s) by player #"+mainAggressor+" ("+playerData[mainAggressor].colour+")");
}
Code Block
themeEclipse
languagejavascript
titleIs attacked unit retreating?
linenumberstrue
// useful if you want to play audio messages based on what attacked droid is doing
function eventAttacked(victim) {
  if (victim.type == DROID) {
    switch (victim.order) {
      case DORDER_RTB: {
        // droid is returning to base (retreating)
        break;
      }
      case DORDER_RTR:
      case DORDER_RTR_SPECIFIED: {
        // droid is returning for repairs
      }
      case DORDER_REARM: {
        // VTOL is returning for repairs/rearming
      }
    }
  }
}

 

See also

  • bind() – monitor when stuff that doesn't belong to you gets destroyed
  • Droids – summary of all API features relating to droids
  • eventAttackedUnthrottled() – unthrottled version of eventAttacked()
  • eventDestroyed() – monitor when your stuff gets destroyed
  • Structures – summary of all API features relating to structures