JS API: requesting a researchPathTo() function

For AI and campaign script related discussions and questions
Post Reply
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

JS API: requesting a researchPathTo() function

Post 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 :)
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: JS API: requesting a researchPathTo() function

Post 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.
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: JS API: requesting a researchPathTo() function

Post by aubergine »

Awesome, thanks Per!
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: JS API: requesting a researchPathTo() function

Post by aubergine »

Is this available in 3.1 Beta 4? Or only 3.2+?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
Emdek
Regular
Regular
Posts: 1329
Joined: 24 Jan 2010, 13:14
Location: Poland
Contact:

Re: JS API: requesting a researchPathTo() function

Post by Emdek »

Nadszedł już czas, najwyższy czas, nienawiść zniszczyć w sobie.
The time has come, the high time, to destroy hatred in oneself.


Beware! Mad Qt Evangelist.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: JS API: requesting a researchPathTo() function

Post by Per »

Per wrote:(for master only)
Post Reply