Page 1 of 1

JS API: requesting a researchPathTo() function

Posted: 21 Feb 2012, 05:05
by aubergine
(EDIT: researchRequiredFor() would be a better function name btw.)

The JS API has a very useful pursueResearch() function that will work out which item in a list of technologies isn't yet researched, and then determine what research is needed next to get to that item.

However, in order to make adaptive research paths in an AI, it would be very useful if the AI could get a list of the technologies required to achieve a specific goal.

For example, let's say the AI hasn't yet researched AA defences and it's just been attacked by VTOLs. Obviously, it needs to prioritise its AA research inline with the overall design of the AI. So, if the AI is focussing on rockets, then I believe the obvious choice would be to go for Sunburst ("R-Defense-Sunburst") as an initial AA defence.

But, it might also have Hurricane ("AASite-QuadMg1") in it's research plan, but only after researching Sunburst as it's doesn't want to put too much effort in to machineguns initially. However, if an ally has been prioritising machine gun research, it might be that Hurricane is actually going to be quicker/cheaper for the AI to research than Sunburst - the AI needs a way to work this fact out.

Assuming the AI is strapped for cash, it would want to know which is the cheapest goal to achieve:

Code: Select all

var researchPath = [ /* big list of research items */ ];

var noAATech = true; // initially we have no AA tech

function costToResearch(tech) {
  var cost = 0;
  var reqs = researchRequiredFor(tech);
  for (var r=0; r<reqs.length; r++) {
    cost += getResearch(reqs[r]).power;
  }
  return cost;
}

function cheapestGoal(tech1,tech2) {
  return (costToResearch(tech1) < costToResearch(tech2)) ? tech1 : tech2;
}

function eventAttacked(victim,attacker) {
  if (noAATech && isVTOL(attacker)) {
    researchPath.unshift(cheapestGoal("R-Defense-Sunburst", "AASite-QuadMg1"));
    noAATech = false; // we've now prioritised AA tech
  }
}

function eventResearched(research,structure) {
  // in case we've researched AA defences before first VTOLs attack us
  // Note: && has precedence over ||
  // (see: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence)
  if (noAATech && research.name == "R-Defense-Sunburst" || research.name == "AASite-QuadMg1") {
    noAATech = false;
  }
}
The JS API could just provide a function equivalent to what cheapestGoal() is doing, however I can think of lots of other scenarios where I want a list of outstanding research to achieve a tech goal (ie. the researchRequiredFor() function) so would prefer that lower level function as it gives me the most flexibility.

The alternate is to start hardcoding research trees in to AIs which would be bad for my health :)

Re: JS API: requesting a researchPathTo() function

Posted: 21 Feb 2012, 21:22
by Per
Added function findResearch(research) that returns an array of remaining research game objects for the given goal (for master only). The array is sorted starting with goal and going down the dependency stack from there.

Re: JS API: requesting a researchPathTo() function

Posted: 22 Feb 2012, 01:31
by aubergine
Awesome, thanks Per!

Re: JS API: requesting a researchPathTo() function

Posted: 24 Feb 2012, 19:53
by aubergine
Is this available in 3.1 Beta 4? Or only 3.2+?

Re: JS API: requesting a researchPathTo() function

Posted: 24 Feb 2012, 20:17
by Emdek

Re: JS API: requesting a researchPathTo() function

Posted: 24 Feb 2012, 23:14
by Per
Per wrote:(for master only)