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

Prioritising research

This article is in dire need of a technical review and update. Ignore it for now.

 

Scenario

Based on events in the game, how can I change my research priorities?

This is a tricky one, and the solution below is very much a work in progress (it's not even been tested yet)...

Solution

A normal research path looks something like this:

this.researchPath = [
  "R-Vehicle-Prop-Halftracks",
  "R-Wpn-MG-Damage02",
  "R-Wpn-Rocket-Damage01",
  "R-Struc-PowerModuleMk1",
  "R-Wpn-MG3Mk1",
  ... etc ...
  "R-Defense-Sunburst",
  "R-Defense-Super-Rocket",
  "R-Wpn-MG-Damage08",
  "R-Struc-VTOLPad-Upgrade03",
  "R-Struc-Materials03",
  "R-Defense-WallTower-TwinAGun",
  "R-Defense-IDFRocket",
  "R-Vehicle-Body06",
  "R-Defense-AASite-QuadRotMg",
  ... and so on ...
]

When you pass that array to the pursueResearch() function, it will find the first item in the list that's not been researched and do whatever research is required next to progress towards that goal.

Now, let say your AI doesn't put too much effort in to researching Anti-Aircraft (AA) defences, instead it focusses mainly on droid upgrades and some defences. If VTOLs start attacking sooner than expected, your AI is going to be "caught with its pants down" - in other words, it's not able to build any AA defences because it's not researched them. Worse still, if it doesn't adapt its research to prioritise AA technology, it could be quite some time before it's able to start making AA defences.

We need a way to bump the priority of some technologies in order to adapt to unexpected (or more commonly "earlier than expected") game situations.

Basic solution

Let's start simple - in this case we just want to bump our first AA tech up to the top of the research list so we can at least start building some AA sites:

Give AA tech max priority after an air raid
eventAttacked(victim,attacker) {
  if (isVTOL(attacker)) {
    for (var r=0; r<researchPath.length; r++) {
      if (researchPath[r] == "R-Defense-Sunburst") {
        if (r==0) break; // it's already prioritised
        // remove the array element (splice), then insert it at the very start of the array (unshift)
        researchPath.unshift(researchPath.splice(r,1)[0]);
        break; // AA defence now prioritised, nothing more we can do from here
      }
    }
  }

Obviously, this could be massively simplified to:

Give AA tech max priority after an air raid
eventAttacked(victim,attacker) {
  if (isVTOL(attacker)) {
    if (researchPath[0] != "R-Defense-Sunburst") { // not yet prioritised
        researchPath.unshift("R-Defense-Sunburst"); // just insert a duplicate of this tech at start of list
    }
  }
}

Now your AI will give maximum priority to getting that AA research over everything else, so it'll soon be able to build some AA defences.

Advanced solution

(info) I have some ideas on this, but it's going to be a few weeks before I get chance to worth though them.