How does the A.I. think?
Basically, the computer opponents "want" the same thing as the player - to conquer the world.
However, their current formula to detirmine how to do that is pretty unadvanced currently.
The first step in the turn of each A.I. is to detirmine whether or not it is wise to attack one of one's neighbors. This is accomplished as follows :
| function determine_defender(countries, ind_att) { var ind_defender = -1; var ind; //determine country with minimum count of men for(ind = 0; ind < nbr.length; ++ind) return ind_defender; |
Ok. I won't pretend to know exactally what all this coding means since it was not written by me, but what I do understand is that somehow the neighbors (adjacent countries to the nation whose turn it is) are scanned. Only the one with the smallest amount of troops is a possible target. And the nation only becomes a potential target if the attacker nation has at least 1.5 times as many troops (in their country, not necessarily all to be used in the attack).
function should_fight(att, def) if (percent > random) return true; |
What is first happening here is that the computer divides the attacker's men by the defender's and multiplies by 10. So for example the attack has 150 men is his home country and the attacker has 50, so (150/50) * 10 = 30. 30 becomes the variable percent (to detirmined the percentage likelihood of attack).
Second step : If percent is greater than 25 then it automatically jumps up 5. So percent is now 35.
Now, third step : If the target (potential defender) country is particularly rich (double the economy and gold of the attacker) the chances of attack go up 15%. If twice as poor (less or equal to half of the attacker countries econ + gold) attack chances go down 15%. If neither applies the percent chance of attack remains the same.
Just for fun we'll say the defender is indeed twice as wealthy. So now the chance has risen to an even 50%. A random number is selected. If over 50 the attack is not made and the A.I. develops his nation (trains, builds, hires or moves) instead. If under 50... the battle rages...
| var g_att, g_def, g_att_assigned, g_total_skill;
function fight(att, def) g_att = att; g_def = def; g_att_assigned = att_assigned; g_total_skill
= total_skill; |
First step now is detirmine how many men from the attacking nation are actually going to war. Obviously the attacker can't send them all or he'd be defenseless himself to other neighbors (unless he was surrounded by only friendly/his own nations).
So the computer generates a random number between one and 11 and adds to to 64 and thus between 65 and 75% of the attacker's forces come to fight while the rest remain home to guard the motherland.
| function fight_men() { var outstr = ''; if( (g_att_assigned > 0) && (g_def.men > 0) ) } if (g_def.men == 0) //attacker won } txtBattles.innerHTML += outstr; |
Despite all the code (a lot of it updating stats and displaying information onscreen) the battle process is beautifully simple. The attacker and defender skills are added up and a random number between 1 and the sum of their totals is created. If that number falls between 1 and the attacker's skill then the defender loses a man, if it's higher the defneder loses one. The fight goes on like this until one of the nations loses all their troops.
| function prepare(att, selchoice, selvalue) { var choice; var args; var max_eco = 5000; if (args > 1) choice = selchoice; else { if ( NoHostileNbrs(att) && att.men >=50 ) choice = 4; else if (att.men < 25) choice = 1; else if (att.economy == max_eco) choice = Math.floor( (Math.random() * 2000000) % 2 + 1 ); else choice = Math.floor( (Math.random() * 2000000) % 3 + 1 ); } var hire = 0, outstr=''; outstr = '<img align="left" src="' + att.objleader.leader
+ '.gif"/><BR/>'; case 2: case 3: print_stats(gcountries); |
Now the majority of A.I. turns are not going to be battles. The rest of the time the AI will either train, hire or build into their economy. They will also automatically transfer troops to an adjancent territory if (A) There are no hostile neighbors to attack and (B) They have 50 troops or more in their country.
Also, the Economy rate maxes out at 5000 so if the economy is at that level then the AI will choose only between hiring and training.
Outside of these exceptions the AI will randomly hire, train or build. Their chances of doing any one of these three is 33.33333etc%.
![]()