OAI_GRAN.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OAI_GRAN.CPP
  21. //Description: AI grand plans
  22. #include <OCONFIG.h>
  23. #include <OTALKRES.h>
  24. #include <ORACERES.h>
  25. #include <OREGIONS.h>
  26. #include <OF_CAMP.h>
  27. #include <ONATION.h>
  28. //----- Begin of function Nation::think_grand_plan -----//
  29. //
  30. void Nation::think_grand_plan()
  31. {
  32. think_deal_with_all_enemy();
  33. think_against_mine_monopoly();
  34. think_ally_against_big_enemy();
  35. }
  36. //------ End of function Nation::think_grand_plan ------//
  37. //----- Begin of function Nation::total_alliance_military -----//
  38. //
  39. // Return the total power of this nation and its friendly/allied
  40. // nation.
  41. //
  42. int Nation::total_alliance_military()
  43. {
  44. int totalPower = military_rank_rating();
  45. for( int i=nation_array.size() ; i>0 ; i-- )
  46. {
  47. if( nation_array.is_deleted(i) || i==nation_recno )
  48. continue;
  49. switch( get_relation_status(i) )
  50. {
  51. case NATION_ALLIANCE:
  52. totalPower += nation_array[i]->military_rank_rating() * 3 / 4; // 75%
  53. break;
  54. /*
  55. case NATION_FRIENDLY:
  56. totalPower += nation_array[i]->military_rank_rating() / 2; //50%
  57. break;
  58. */
  59. }
  60. }
  61. return totalPower;
  62. }
  63. //------ End of function Nation::total_alliance_military ------//
  64. //----- Begin of function Nation::total_enemy_military -----//
  65. //
  66. // Return the total power of this nation's enemies and potential
  67. // enemies.
  68. //
  69. int Nation::total_enemy_military()
  70. {
  71. Nation *nationPtr;
  72. int totalPower=0, relationStatus;
  73. // int relationStatus2, enemyRelationStatus;
  74. for( int i=nation_array.size() ; i>0 ; i-- )
  75. {
  76. if( nation_array.is_deleted(i) || i==nation_recno )
  77. continue;
  78. nationPtr = nation_array[i];
  79. relationStatus = get_relation_status(i);
  80. if( relationStatus == NATION_HOSTILE )
  81. {
  82. totalPower += nationPtr->military_rank_rating();
  83. }
  84. /*
  85. else
  86. {
  87. //--- check this nation's status with our enemies ---//
  88. enemyRelationStatus = 0;
  89. for( int j=nation_array.size() ; j>0 ; j-- )
  90. {
  91. if( nation_array.is_deleted(j) || j==nation_recno )
  92. continue;
  93. if( get_relation_status(j) != NATION_HOSTILE ) // only if this is one of our enemies.
  94. continue;
  95. //--- check if it is allied or friendly to any of our enemies ---//
  96. relationStatus2 = nationPtr->get_relation_status(j);
  97. if( relationStatus2 > enemyRelationStatus ) // Friendly will replace none, Alliance will replace Friendly
  98. enemyRelationStatus = relationStatus2;
  99. }
  100. if( enemyRelationStatus == NATION_ALLIANCE )
  101. totalPower += nationPtr->military_rank_rating() * 3 / 4; // 75%
  102. else if( enemyRelationStatus == NATION_FRIENDLY )
  103. totalPower += nationPtr->military_rank_rating() / 2; // 50%
  104. }
  105. */
  106. }
  107. return totalPower;
  108. }
  109. //------ End of function Nation::total_enemy_military ------//
  110. //----- Begin of function Nation::total_enemy_count -----//
  111. //
  112. int Nation::total_enemy_count()
  113. {
  114. int totalEnemy=0;
  115. for( int i=nation_array.size() ; i>0 ; i-- )
  116. {
  117. if( nation_array.is_deleted(i) || i==nation_recno )
  118. continue;
  119. if( get_relation_status(i) == NATION_HOSTILE )
  120. totalEnemy++;
  121. }
  122. return totalEnemy;
  123. }
  124. //------ End of function Nation::total_enemy_count ------//
  125. //----- Begin of function Nation::think_deal_with_all_enemy -----//
  126. //
  127. // Think about dealing with the enemy. The following are the
  128. // actions a nation can take to deal with its enemies.
  129. //
  130. // >ask our allies to attack the enemy.
  131. //
  132. // >try to break the enemy's existing alliance/friendly treaty with other
  133. // nations - to reduce its alliance strength.
  134. //
  135. // >convert enemy's allies to ours.
  136. //
  137. // >ask other nations to impose trade embargos on the enemy.
  138. //
  139. void Nation::think_deal_with_all_enemy()
  140. {
  141. Nation* nationPtr;
  142. int ourMilitary = military_rank_rating();
  143. int enemyCount = total_enemy_count();
  144. for( int i=1 ; i<=nation_array.size() ; i++ )
  145. {
  146. if( nation_array.is_deleted(i) || nation_recno == i )
  147. continue;
  148. if( get_relation_status(i) != NATION_HOSTILE )
  149. continue;
  150. nationPtr = nation_array[i];
  151. //------- think about eliminating the enemy ------//
  152. int rc = 0;
  153. if( nationPtr->total_population==0 ) // the enemy has no towns left
  154. rc = 1;
  155. if( enemyCount==1 &&
  156. ourMilitary > 100-pref_military_courage/5 ) // 80 to 100
  157. {
  158. int enemyMilitary = nationPtr->military_rank_rating();
  159. if( enemyMilitary < 20 && ai_should_spend_war(enemyMilitary) )
  160. rc = 1;
  161. }
  162. if( rc )
  163. {
  164. if( think_eliminate_enemy_firm(i) )
  165. continue;
  166. if( think_eliminate_enemy_town(i) )
  167. continue;
  168. think_eliminate_enemy_unit(i);
  169. continue;
  170. }
  171. //----- think about dealing with the enemy with diplomacy -----//
  172. think_deal_with_one_enemy(i);
  173. }
  174. }
  175. //------ End of function Nation::think_deal_with_all_enemy ------//
  176. //----- Begin of function Nation::think_deal_with_one_enemy -----//
  177. //
  178. void Nation::think_deal_with_one_enemy(int enemyNationRecno)
  179. {
  180. Nation* nationPtr;
  181. NationRelation* nationRelation;
  182. for( int i=1 ; i<=nation_array.size() ; i++ )
  183. {
  184. if( nation_array.is_deleted(i) || nation_recno == i )
  185. continue;
  186. nationPtr = nation_array[i];
  187. nationRelation = nationPtr->get_relation(nation_recno);
  188. //--- if this nation is already allied to us, request it to declare war with the enemy ---//
  189. if( nationRelation->status == NATION_ALLIANCE &&
  190. nationPtr->get_relation_status(enemyNationRecno) != NATION_HOSTILE )
  191. {
  192. if( should_diplomacy_retry(TALK_REQUEST_DECLARE_WAR, i) )
  193. {
  194. talk_res.ai_send_talk_msg(i, nation_recno, TALK_REQUEST_DECLARE_WAR, enemyNationRecno);
  195. continue;
  196. }
  197. }
  198. //---- if this nation is not friendly or alliance to our enemy ----//
  199. if( nationPtr->get_relation_status(enemyNationRecno) < NATION_FRIENDLY )
  200. {
  201. //--- and this nation is neutral or friendly with us ---//
  202. if( nationRelation->status >= NATION_NEUTRAL &&
  203. nationPtr->get_relation(enemyNationRecno)->trade_treaty )
  204. {
  205. //--- ask it to join a trade embargo on the enemy ---//
  206. if( should_diplomacy_retry(TALK_REQUEST_TRADE_EMBARGO, i) )
  207. talk_res.ai_send_talk_msg(i, nation_recno, TALK_REQUEST_TRADE_EMBARGO, enemyNationRecno );
  208. }
  209. }
  210. else //---- if this nation is friendly or alliance to our enemy ----//
  211. {
  212. //----- and this nation is not at war with us -----//
  213. if( nationRelation->status != NATION_HOSTILE )
  214. {
  215. //--- if we do not have trade treaty with this nation, propose one ---//
  216. if( !nationRelation->trade_treaty )
  217. {
  218. if( should_diplomacy_retry(TALK_PROPOSE_TRADE_TREATY, i) )
  219. talk_res.ai_send_talk_msg(i, nation_recno, TALK_PROPOSE_TRADE_TREATY );
  220. }
  221. else //--- if we already have a trade treaty with this nation ---//
  222. {
  223. // if this nation is already friendly to us, propose an alliance treaty now --//
  224. if( nationRelation->status == NATION_FRIENDLY )
  225. {
  226. if( should_diplomacy_retry(TALK_PROPOSE_ALLIANCE_TREATY, i) )
  227. talk_res.ai_send_talk_msg(i, nation_recno, TALK_PROPOSE_ALLIANCE_TREATY );
  228. }
  229. //-- if the nation has significiant trade with us, propose a friendly treaty now --//
  230. else if( nationPtr->trade_rating(nation_recno) > 10 ||
  231. nationPtr->ai_trade_with_rating(nation_recno) >= 50 ) // or if the product complement each other very well
  232. {
  233. if( should_diplomacy_retry(TALK_PROPOSE_FRIENDLY_TREATY, i) )
  234. talk_res.ai_send_talk_msg(i, nation_recno, TALK_PROPOSE_FRIENDLY_TREATY );
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. //------ End of function Nation::think_deal_with_one_enemy ------//
  242. //----- Begin of function Nation::think_surrender -----//
  243. //
  244. int Nation::think_surrender()
  245. {
  246. //--- don't surrender if the nation still has a town ---//
  247. int rc=0;
  248. if( total_population == 0 )
  249. rc = 1;
  250. if( cash <= 0 && income_365days()==0 )
  251. rc = 1;
  252. if( !rc )
  253. return 0;
  254. //---- see if there is any nation worth getting our surrender ---//
  255. Nation* nationPtr;
  256. int curRating, bestRating=0, bestNationRecno=0;
  257. if( !king_unit_recno ) // if there is no successor to the king, the nation will tend more to surrender
  258. bestRating = -100;
  259. for( int i=nation_array.size() ; i>0 ; i-- )
  260. {
  261. if( nation_array.is_deleted(i) || i==nation_recno )
  262. continue;
  263. nationPtr = nation_array[i];
  264. if( nationPtr->cash <= 300 ) // don't surrender to an ecnomically handicapped nation
  265. continue;
  266. curRating = ai_surrender_to_rating(i);
  267. //--- if the nation will tend to surrender if there is only a small number of units left ---//
  268. curRating += 50 - total_unit_count*5;
  269. if( curRating > bestRating )
  270. {
  271. bestRating = curRating;
  272. bestNationRecno = i;
  273. }
  274. }
  275. //---------------------------------------//
  276. if( bestNationRecno )
  277. {
  278. surrender(bestNationRecno);
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. //------ End of function Nation::think_surrender ------//
  284. //----- Begin of function Nation::think_unite_against_big_enemy -----//
  285. //
  286. int Nation::think_unite_against_big_enemy()
  287. {
  288. if( info.game_date - info.game_start_date <
  289. 365 * 3 * (100+pref_military_development) / 100 ) // only do this after 3 to 6 years into the game
  290. {
  291. return 0;
  292. }
  293. //-----------------------------------------------//
  294. if( config.ai_aggressiveness < OPTION_HIGH )
  295. return 0;
  296. if( config.ai_aggressiveness == OPTION_HIGH )
  297. {
  298. if( m.random(10)!=0 )
  299. return 0;
  300. }
  301. else // OPTION_VERY_HIGH
  302. {
  303. if( m.random(5)!=0 )
  304. return 0;
  305. }
  306. //---------------------------------------//
  307. int enemyNationRecno = nation_array.max_overall_nation_recno;
  308. if( !enemyNationRecno )
  309. return 0;
  310. Nation* enemyNation = nation_array[enemyNationRecno];
  311. //----- only against human players -----//
  312. if( enemyNation->is_ai() )
  313. return 0;
  314. //---- find the overall rank rating of the second most powerful computer kingdom ---//
  315. Nation* nationPtr;
  316. int secondBestOverall=0, secondBestNationRecno=0;
  317. for( int i=nation_array.size() ; i>0 ; i-- )
  318. {
  319. if( nation_array.is_deleted(i) || i==enemyNationRecno )
  320. continue;
  321. nationPtr = nation_array[i];
  322. if( !nationPtr->is_ai() ) // don't count human players
  323. continue;
  324. if( nationPtr->overall_rank_rating() > secondBestOverall )
  325. {
  326. secondBestOverall = nationPtr->overall_rank_rating();
  327. secondBestNationRecno = i;
  328. }
  329. }
  330. if( !secondBestNationRecno || secondBestNationRecno==nation_recno )
  331. return 0;
  332. //------- don't surrender to hostile nation -------//
  333. if( get_relation_status(secondBestNationRecno) < NATION_NEUTRAL )
  334. return 0;
  335. //--- if all AI kingdoms are way behind the human players, unite to against the human player ---//
  336. int compareRating;
  337. if( config.ai_aggressiveness == OPTION_HIGH )
  338. compareRating = 50;
  339. else // OPTION_VERY_AGGRESSIVE
  340. compareRating = 80;
  341. if( secondBestOverall < compareRating &&
  342. secondBestNationRecno != nation_recno )
  343. {
  344. surrender(secondBestNationRecno);
  345. return 1;
  346. }
  347. return 0;
  348. }
  349. //------ End of function Nation::think_unite_against_big_enemy ------//
  350. //----- Begin of function Nation::ai_surrender_to_rating -----//
  351. //
  352. // return a rating on how much the nation will tend to surrender
  353. // to the specific nation.
  354. //
  355. int Nation::ai_surrender_to_rating(int nationRecno)
  356. {
  357. Nation* nationPtr = nation_array[nationRecno];
  358. NationRelation* nationRelation = get_relation(nationRecno);
  359. //--- higher tendency to surrender to a powerful nation ---//
  360. int curRating = nationPtr->overall_rank_rating() - overall_rank_rating();
  361. curRating += (nationRelation->ai_relation_level-40);
  362. curRating += (int) nationRelation->good_relation_duration_rating*3;
  363. curRating += (int) nationPtr->reputation/2;
  364. //------ shouldn't surrender to an enemy --------//
  365. if( nationRelation->status == NATION_HOSTILE )
  366. curRating -= 100;
  367. //--- if the race of the kings are the same, the chance is higher ---//
  368. if( race_res.is_same_race( nationPtr->race_id, race_id ) )
  369. curRating += 20;
  370. return curRating;
  371. }
  372. //------ End of function Nation::ai_surrender_to_rating ------//
  373. //----- Begin of function Nation::think_eliminate_enemy_town -----//
  374. //
  375. // This function is called to eliminate remaining enemy firms
  376. // when all enemy towns have been destroyed.
  377. //
  378. int Nation::think_eliminate_enemy_town(int enemyNationRecno)
  379. {
  380. //---- look for enemy firms to attack ----//
  381. int hasWar;
  382. Town *townPtr;
  383. for( int i=town_array.size() ; i>0 ; i-- )
  384. {
  385. if( town_array.is_deleted(i) )
  386. continue;
  387. townPtr = town_array[i];
  388. if( townPtr->nation_recno != enemyNationRecno )
  389. continue;
  390. //--- only attack if we have any base town in the enemy town's region ---//
  391. if( base_town_count_in_region(townPtr->region_id)==0 )
  392. continue;
  393. //----- take into account of the mobile units around this town -----//
  394. int mobileCombatLevel = mobile_defense_combat_level(townPtr->center_x, townPtr->center_y, townPtr->nation_recno, 1, hasWar);
  395. if( mobileCombatLevel == -1 ) // do not attack this town because a battle is already going on
  396. continue;
  397. //---- calculate the combat level of this target town ----//
  398. int townCombatLevel = townPtr->protection_available();
  399. return ai_attack_target(townPtr->loc_x1, townPtr->loc_y1, mobileCombatLevel + townCombatLevel);
  400. }
  401. return 0;
  402. }
  403. //------ End of function Nation::think_eliminate_enemy_town ------//
  404. //----- Begin of function Nation::think_eliminate_enemy_firm -----//
  405. //
  406. // This function is called to eliminate remaining enemy firms
  407. // when all enemy towns have been destroyed.
  408. //
  409. int Nation::think_eliminate_enemy_firm(int enemyNationRecno)
  410. {
  411. //---- look for enemy firms to attack ----//
  412. int hasWar;
  413. Firm *firmPtr;
  414. for( int i=firm_array.size() ; i>0 ; i-- )
  415. {
  416. if( firm_array.is_deleted(i) )
  417. continue;
  418. firmPtr = firm_array[i];
  419. if( firmPtr->nation_recno != enemyNationRecno )
  420. continue;
  421. //--- only attack if we have any base town in the enemy firm's region ---//
  422. if( base_town_count_in_region(firmPtr->region_id)==0 )
  423. continue;
  424. //----- take into account of the mobile units around this town -----//
  425. int mobileCombatLevel = mobile_defense_combat_level(firmPtr->center_x, firmPtr->center_y, firmPtr->nation_recno, 1, hasWar);
  426. if( mobileCombatLevel == -1 ) // do not attack this town because a battle is already going on
  427. continue;
  428. //---- calculate the combat level of this target firm ----//
  429. int firmCombatLevel;
  430. if( firmPtr->firm_id == FIRM_CAMP ) // other civilian firms
  431. firmCombatLevel = ((FirmCamp*)firmPtr)->total_combat_level();
  432. else
  433. firmCombatLevel = firmPtr->worker_count * 10; // civilian firms have very low combat level
  434. return ai_attack_target(firmPtr->loc_x1, firmPtr->loc_y1, mobileCombatLevel + firmCombatLevel);
  435. }
  436. return 0;
  437. }
  438. //------ End of function Nation::think_eliminate_enemy_firm ------//
  439. //----- Begin of function Nation::think_eliminate_enemy_unit -----//
  440. //
  441. // This function is called to eliminate remaining enemy firms
  442. // when all enemy towns have been destroyed.
  443. //
  444. int Nation::think_eliminate_enemy_unit(int enemyNationRecno)
  445. {
  446. Unit *unitPtr;
  447. int hasWar;
  448. for( int i=unit_array.size() ; i>0 ; i-- )
  449. {
  450. if( unit_array.is_deleted(i) )
  451. continue;
  452. unitPtr = unit_array[i];
  453. if( unitPtr->nation_recno != enemyNationRecno )
  454. continue;
  455. if( !unitPtr->is_visible() || unitPtr->mobile_type != UNIT_LAND ) // only deal with land units now
  456. continue;
  457. //--- only attack if we have any base town in the enemy unit's region ---//
  458. if( base_town_count_in_region(unitPtr->region_id()) == 0 )
  459. continue;
  460. //----- take into account of the mobile units around this town -----//
  461. int mobileCombatLevel = mobile_defense_combat_level(unitPtr->next_x_loc(), unitPtr->next_y_loc(), unitPtr->nation_recno, 1, hasWar);
  462. if( mobileCombatLevel == -1 ) // do not attack this town because a battle is already going on
  463. continue;
  464. return ai_attack_target(unitPtr->next_x_loc(), unitPtr->next_y_loc(), mobileCombatLevel + (int) unitPtr->unit_power());
  465. }
  466. return 0;
  467. }
  468. //------ End of function Nation::think_eliminate_enemy_unit ------//
  469. //----- Begin of function Nation::think_ally_against_big_enemy -----//
  470. //
  471. // Think about allying against a big enemy
  472. //
  473. int Nation::think_ally_against_big_enemy()
  474. {
  475. if( info.game_date < info.game_start_date + 365 + nation_recno*70 ) // don't ask for tribute too soon, as in the beginning, the ranking are all the same for all nations
  476. return 0;
  477. //---------------------------------------//
  478. int enemyNationRecno = nation_array.max_overall_nation_recno;
  479. if( enemyNationRecno == nation_recno )
  480. return 0;
  481. //-- if AI aggressiveness > high, only deal against the player, but not other kingdoms ---//
  482. if( config.ai_aggressiveness >= OPTION_HIGH )
  483. {
  484. if( nation_array[enemyNationRecno]->is_ai() )
  485. return 0;
  486. }
  487. //-- if AI aggressiveness is low, don't do this against the human player --//
  488. else if( config.ai_aggressiveness == OPTION_LOW )
  489. {
  490. if( !nation_array[enemyNationRecno]->is_ai() )
  491. return 0;
  492. }
  493. //--- increase the ai_relation_level towards other nations except the enemy so we can ally against the enemy ---//
  494. Nation* enemyNation = nation_array[enemyNationRecno];
  495. int incRelationLevel = (100-overall_rank_rating())/10;
  496. int i;
  497. for( i=nation_array.size() ; i>0 ; i-- )
  498. {
  499. if( nation_array.is_deleted(i) )
  500. continue;
  501. if( i==nation_recno || i==enemyNationRecno )
  502. continue;
  503. int thisIncLevel = incRelationLevel * (100-get_relation(i)->ai_relation_level) / 100;
  504. change_ai_relation_level( i, thisIncLevel );
  505. }
  506. //---- don't have all nations doing it the same time ----//
  507. if( m.random(nation_array.ai_nation_count)==0 )
  508. return 0;
  509. //---- if the trade rating is high, stay war-less with it ----//
  510. if( trade_rating(enemyNationRecno) +
  511. ai_trade_with_rating(enemyNationRecno) > 100 - pref_trading_tendency/3 )
  512. {
  513. return 0;
  514. }
  515. //---- if the nation relation level is still high, then request aid/tribute ----//
  516. NationRelation* nationRelation = get_relation(enemyNationRecno);
  517. if( nationRelation->ai_relation_level > 30 )
  518. {
  519. int talkId;
  520. if( nationRelation->status >= NATION_FRIENDLY )
  521. talkId = TALK_DEMAND_AID;
  522. else
  523. talkId = TALK_DEMAND_TRIBUTE;
  524. if( should_diplomacy_retry(talkId, enemyNationRecno) )
  525. {
  526. static short aidAmountArray[] = { 500, 1000, 2000 };
  527. int aidAmount = aidAmountArray[m.random(3)];
  528. talk_res.ai_send_talk_msg(enemyNationRecno, nation_recno, talkId, aidAmount);
  529. }
  530. return 0;
  531. }
  532. //-------------------------------------//
  533. Nation* nationPtr;
  534. NationRelation *ourNationRelation, *enemyNationRelation;
  535. for( i=nation_array.size() ; i>0 ; i-- )
  536. {
  537. if( nation_array.is_deleted(i) )
  538. continue;
  539. if( i==nation_recno || i==enemyNationRecno )
  540. continue;
  541. nationPtr = nation_array[i];
  542. ourNationRelation = get_relation(i);
  543. enemyNationRelation = enemyNation->get_relation(i);
  544. }
  545. return 0;
  546. }
  547. //------ End of function Nation::think_ally_against_big_enemy ------//
  548. //----- Begin of function Nation::ai_should_attack_friendly -----//
  549. //
  550. // This function returns whether this nation should attack
  551. // the given friendly nation.
  552. //
  553. // <int> friendlyNationRecno - the nation recno of the friendly nation.
  554. // <int> attackTemptation - a rating from 0 to 100 indicating
  555. // temptation of attacking the target.
  556. //
  557. int Nation::ai_should_attack_friendly(int friendlyNationRecno, int attackTemptation)
  558. {
  559. Nation *friendlyNation = nation_array[friendlyNationRecno];
  560. NationRelation *nationRelation = get_relation(friendlyNationRecno);
  561. //--- don't change terminate treaty too soon ---//
  562. if( info.game_date < nationRelation->last_change_status_date+60+pref_honesty/2 ) // only after 60 to 110 days
  563. return 0;
  564. //------------------------------------------------//
  565. int resistanceRating = friendlyNation->military_rank_rating()
  566. - military_rank_rating();
  567. resistanceRating += nationRelation->ai_relation_level - 50;
  568. resistanceRating += trade_rating(friendlyNationRecno);
  569. return attackTemptation > resistanceRating;
  570. }
  571. //------ End of function Nation::ai_should_attack_friendly ------//
  572. //----- Begin of function Nation::ai_end_treaty -----//
  573. //
  574. // Terminate the treaty with the given nation.
  575. //
  576. // <int> nationRecno - the nation to terminate treaty with.
  577. //
  578. void Nation::ai_end_treaty(int nationRecno)
  579. {
  580. NationRelation *nationRelation = get_relation(nationRecno);
  581. err_when( nationRelation->status < NATION_FRIENDLY );
  582. if( nationRelation->status == NATION_FRIENDLY )
  583. {
  584. talk_res.ai_send_talk_msg(nationRecno, nation_recno, TALK_END_FRIENDLY_TREATY, 0, 0, 1); // 1-force send
  585. }
  586. else if( nationRelation->status == NATION_ALLIANCE )
  587. {
  588. talk_res.ai_send_talk_msg(nationRecno, nation_recno, TALK_END_ALLIANCE_TREATY, 0, 0, 1);
  589. }
  590. err_when( nationRelation->status >= NATION_FRIENDLY ); // when the status is still friendly or alliance
  591. }
  592. //------ End of function Nation::ai_end_treaty ------//
  593. //----- Begin of function Nation::ai_has_enough_food -----//
  594. //
  595. // return whether this nation has enough food or not.
  596. //
  597. int Nation::ai_has_enough_food()
  598. {
  599. return food > 2000 + 2000 * pref_food_reserve / 100
  600. || yearly_food_change() > 0;
  601. }
  602. //------ End of function Nation::ai_has_enough_food ------//
  603. //----- Begin of function Nation::think_attack_enemy_firm -----//
  604. //
  605. // Think about attacking a specific type of firm of a specific enemy.
  606. //
  607. int Nation::think_attack_enemy_firm(int enemyNationRecno, int firmId)
  608. {
  609. if( !largest_town_recno )
  610. return 0;
  611. Town* ourLargestTown = town_array[largest_town_recno];
  612. Nation *nationPtr = nation_array[enemyNationRecno];
  613. Firm *firmPtr, *targetFirm=NULL;
  614. int curRating, bestRating=0, targetCombatLevel, hasWar;
  615. for( int i=firm_array.size() ; i>0 ; i-- )
  616. {
  617. if( firm_array.is_deleted(i) )
  618. continue;
  619. firmPtr = firm_array[i];
  620. if( firmPtr->firm_id != firmId ||
  621. firmPtr->nation_recno != enemyNationRecno )
  622. {
  623. continue;
  624. }
  625. int combatLevel = enemy_firm_combat_level(firmPtr, 1, hasWar);
  626. if( combatLevel==0 ) // no protection with this mine
  627. {
  628. targetFirm = firmPtr;
  629. targetCombatLevel = combatLevel;
  630. break;
  631. }
  632. curRating = world.distance_rating( firmPtr->center_x, firmPtr->center_y,
  633. ourLargestTown->center_x, ourLargestTown->center_y );
  634. curRating += 1000 - combatLevel/5;
  635. if( curRating > bestRating )
  636. {
  637. bestRating = curRating;
  638. targetFirm = firmPtr;
  639. targetCombatLevel = combatLevel;
  640. }
  641. }
  642. if( !targetFirm )
  643. return 0;
  644. //---------------------------------------------//
  645. int useAllCamp=1;
  646. return ai_attack_target( targetFirm->loc_x1, targetFirm->loc_y1,
  647. targetCombatLevel, 0, 0, 0, 0, useAllCamp );
  648. }
  649. //------ End of function Nation::think_attack_enemy_firm ------//
  650. //----- Begin of function Nation::think_against_mine_monopoly -----//
  651. //
  652. int Nation::think_against_mine_monopoly()
  653. {
  654. //-- only think this after the game has been running for at least one year --//
  655. if( config.ai_aggressiveness < OPTION_HIGH ) // only attack if aggressiveness >= high
  656. return 0;
  657. if( info.game_date - info.game_start_date > 365 )
  658. return 0;
  659. if( profit_365days() > 0 ) // if we are making a profit, don't attack
  660. return 0;
  661. //-- for high aggressiveness, it will check cash before attack, for very high aggressiveness, it won't check cash before attack ---//
  662. if( config.ai_aggressiveness < OPTION_VERY_HIGH ) // only attack if aggressiveness >= high
  663. {
  664. if( cash > 2000 + 1000 * pref_cash_reserve / 100 ) // only attack if we run short of cash
  665. return 0;
  666. }
  667. //--------------------------------------------------------//
  668. if( !largest_town_recno )
  669. return 0;
  670. //--------------------------------------------------//
  671. int baseRegionId = town_array[largest_town_recno]->region_id;
  672. // ##### patch begin Gilbert 16/3 ########//
  673. //#ifdef AMPLUS
  674. // no region stat (region is too small), don't care
  675. if( !region_array[baseRegionId]->region_stat_id )
  676. return 0;
  677. //#endif
  678. // ##### end begin Gilbert 16/3 ########//
  679. RegionStat* regionStat = region_array.get_region_stat(baseRegionId);
  680. //---- if we already have a mine in this region ----//
  681. if( regionStat->mine_nation_count_array[nation_recno-1] > 0 )
  682. return 0;
  683. //----- if there is no mine in this region -----//
  684. if( regionStat->raw_count == 0 )
  685. return 0;
  686. //----- if enemies have occupied all mines -----//
  687. int mineCount, totalMineCount=0;
  688. int curRating, bestRating=0, targetNationRecno=0;
  689. int i;
  690. for( i=nation_array.size() ; i>0 ; i-- )
  691. {
  692. if( nation_array.is_deleted(i) )
  693. continue;
  694. //------ only deal with human players ------//
  695. if( nation_array[i]->is_ai() || i==nation_recno )
  696. continue;
  697. //------------------------------------------//
  698. mineCount = regionStat->mine_nation_count_array[i-1];
  699. totalMineCount += mineCount;
  700. curRating = mineCount * 100
  701. - get_relation(i)->ai_relation_level
  702. - trade_rating(i);
  703. if( curRating > bestRating )
  704. {
  705. bestRating = curRating;
  706. targetNationRecno = i;
  707. }
  708. }
  709. if( !targetNationRecno )
  710. return 0;
  711. //--- if the relationship with this nation is still good, don't attack yet, ask for aid first ---//
  712. NationRelation* nationRelation = get_relation(targetNationRecno);
  713. if( nationRelation->ai_relation_level > 30 )
  714. {
  715. int talkId;
  716. if( nationRelation->status >= NATION_FRIENDLY )
  717. talkId = TALK_DEMAND_AID;
  718. else
  719. talkId = TALK_DEMAND_TRIBUTE;
  720. if( should_diplomacy_retry(talkId, targetNationRecno) )
  721. {
  722. static short aidAmountArray[] = { 500, 1000, 2000 };
  723. int aidAmount = aidAmountArray[m.random(3)];
  724. talk_res.ai_send_talk_msg(targetNationRecno, nation_recno, talkId, aidAmount);
  725. }
  726. return 0;
  727. }
  728. //------- attack one of the target enemy's mines -------//
  729. Firm* firmPtr;
  730. for( i=firm_array.size() ; i>0 ; i-- )
  731. {
  732. if( firm_array.is_deleted(i) )
  733. continue;
  734. firmPtr = firm_array[i];
  735. if( firmPtr->firm_id != FIRM_MINE ||
  736. firmPtr->nation_recno != targetNationRecno ||
  737. firmPtr->region_id != baseRegionId )
  738. {
  739. continue;
  740. }
  741. //--------------------------------------------//
  742. int hasWar;
  743. int targetCombatLevel = enemy_firm_combat_level(firmPtr, 1, hasWar);
  744. return ai_attack_target( firmPtr->loc_x1, firmPtr->loc_y1,
  745. targetCombatLevel, 0, 0, 0, 0, 1 ); // 1-use all camps
  746. }
  747. return 0;
  748. }
  749. //------ End of function Nation::think_against_mine_monopoly ------//