Since it came up in the Pro/Agg argument, I got interested in how the AI chooses its next research item. The code for this is in the file CvPlayerAI.cpp, in the function CvPlayerAI::AI_bestTech(). It is a very long function, and rather hard to understand in terms of what it does in the game. But we can try! A lot of the work has already been done. I found a fairly accurate decription in this thread:
http://forums.civfanatics.com/showthread.php?t=205759&page=3
and there are probably others, too. Note that the thread above is a Fall from Heaven mod thread, but it appears to describe the same code I am looking at quite well.
Anyway, here is a somewhat adapted version of the description linked above.
The BTS 3.19 AI Tech Choice Code
All techs within a pathlength of one to three techs up the tree are evaluated. The tech with the best value is chosen.
(1) The base value of a tech is largely random:
(2) To that, a great many small bonuses (in the range of 100-200 points) may be added. I am ignoring all of the small ones, and rare ones (i.e. there are a number of them for AIs alone on a continent). I will try to list all bonuses of 500+, and in the order they are defined in the code. Note that when I list a particular tech, that is not what the code is looking at: it only uses the features of techs, not the techs themselves.
(3) Then some addends coming from values defined in XML files, that I am not sure of the magnitude of:
(4) Finally, there are some multiplicative factors. These will tend to carry a lot of weight. The value #turns_left is the number of turns left to research the tech for that AI; since the AI has the same science output for all evals, in essence all it does is build in the cost of the tech.
http://forums.civfanatics.com/showthread.php?t=205759&page=3
and there are probably others, too. Note that the thread above is a Fall from Heaven mod thread, but it appears to describe the same code I am looking at quite well.
Anyway, here is a somewhat adapted version of the description linked above.
The BTS 3.19 AI Tech Choice Code
All techs within a pathlength of one to three techs up the tree are evaluated. The tech with the best value is chosen.
(1) The base value of a tech is largely random:
- value = 1 + rand(0-1999) + any research progress
(2) To that, a great many small bonuses (in the range of 100-200 points) may be added. I am ignoring all of the small ones, and rare ones (i.e. there are a number of them for AIs alone on a continent). I will try to list all bonuses of 500+, and in the order they are defined in the code. Note that when I list a particular tech, that is not what the code is looking at: it only uses the features of techs, not the techs themselves.
- for Alphabet: +500, and +500 more for each known civ
- for Currency: +600 (just +200 if have not met anyone)
- for Writing and have met someone: +500, or +900 with a coastal city
- for Biology: +500
- for Fishing: +600 per coastal city
- for ability to trade across coast/ocean: +100 per foreign city on other continents. If AI has coastal city: +400, or +1000 if alone on continent
- for ability to trade on river: +1000
- for unlocking military units. This is based on an attribute units have called their UNITAI, which seems to be basically a hint to AIs as to how to use them. Most fall below the 500 point threshold for noting here, particularly naval units. Land units, especially when the AI is planning a war, get values of 800 or more.
- for unlocking a unique unit: +600 in addition to the military unit bonus
- for unlocking buildings. Typically, they're worth 150 each. A unique building is worth 750. Lots of other small or rare modifiers.
- the highest-value building gets its value added again
- +500-1300, if the pathlength is 1, and the tech enables building at least one wonder
- +1000 for Projects (Apollo, spaceship parts)
- if the pathlength is 1 or 2, and nobody has the tech yet
- +1-3600 for a unfounded religion (only 2/3 that if pathlength is 2). AIs with a favorite religion want it bad, adding +1200; otherwise they weight it 2/3 as much. As religions are founded their value declines: divide amount by (1 + #holycities), although this factor does not decline for AIs with AI_STRATEGY_CULTURE1, who instead add +500. Then, if AI has no religion in any city, 2x the value, +500.
- +100 + rand(2400) for unfounded corps
- +200 + rand(3200) for a tech with a free great person still available
- +200-3400 for tech granting a free tech (Liberalism)
(3) Then some addends coming from values defined in XML files, that I am not sure of the magnitude of:
- the tech's AI weight is added (from the CIV4TechInfos.xml, field <iAIWeight>).
- (if non-human), any flavors of the leader (from their XML file) are multiplied by 20 times the flavor weight of that tech (from their XML file).
(4) Finally, there are some multiplicative factors. These will tend to carry a lot of weight. The value #turns_left is the number of turns left to research the tech for that AI; since the AI has the same science output for all evals, in essence all it does is build in the cost of the tech.
- if it is a repeatable tech (namely, Future Tech), the value is divided by 10.
- if evaluating a free tech (i.e. the AI won Liberalism), the value is multiplied by #turns_left/10
- if this isn't a free tech, the value is multiplied by 1000/(#turns_left + 1)
- multiply value for would-be monopoly techs, but just 10% of the time. A tech that is unknown to all known civs gets its value increased 25%*(#teams - 2), up to max of 150%.
- the minimum value for a tech is always 1.