Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Excerpt

Determine if more generators are required to power our oil derricks.

 

Code

Code Block
themeRDark
languagejavascript
linenumberstrue
function generatorsRequired(player) {
  if (player == null) {
    player == me;
  } else if (player<0 || player>maxPlayers) {
    return 0;
  }
  var num_res  = enumStruct(player,RESOURCE_EXTRACTOR).length;
  var num_gens = enumStruct(player,POWER_GEN).length;
  var gens_needed = Math.ceil(num_res/4);
  return Math.max(0,gens_needed-num_gens);
}

Example

Code Block
themeRDark
languagejavascript
linenumberstrue
// build generator if needed
if (generatorsRequired()) {
  // tell a free truck to build a power generator
}
 
// build all required generators
var i;
if (i=generatorsRequired()) while (-1<--i) {
  // tell a free truck to build a power generator
}
 
// how many more generators does player 3 need?
console("Player 3 needs "+generatorsRequired(3)+" more generators to power their resource extractors.");

Notes

If you just want a super-quick check to see if more generators are required, without being able to specify which player or find out how many generators are required, you could use this simplified alternative:

Code Block
themeRDark
languagejavascript
linenumberstrue
function generatorsRequired() {
  return (enumStruct(me,RESOURCE_EXTRACTOR).length > enumStruct(me,POWER_GEN).length*4);
}
 
// do I need more generators?
if (generatorsRequired()) {
  // build a generator
}
Div
classbox

Availability

Requires:

  • Warzone 3.1 Beta 1 and above.
Div
classbox

See also

Related articles: