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 alternate is to start hardcoding research trees in to AIs which would be bad for my health