OAI_DIPL.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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_DIPL.CPP
  21. //Description: AI functions on diplomacy
  22. #include <OTALKRES.h>
  23. #include <OCONFIG.h>
  24. #include <OTECHRES.h>
  25. #include <ONATION.h>
  26. //----- Begin of function Nation::think_diplomacy -----//
  27. //
  28. void Nation::think_diplomacy()
  29. {
  30. //--- process incoming messages first, so we won't send out the same request to nation which has already proposed the same thing ---//
  31. int nationRecno = nation_recno;
  32. process_action(0, ACTION_AI_PROCESS_TALK_MSG);
  33. if( nation_array.is_deleted(nationRecno) ) // the nation may have been deleted, if the nation accepts a purchase kingdom offer
  34. return;
  35. //---- thinking about war first -----//
  36. if( think_declare_war() )
  37. return;
  38. //----- think buy food first -------//
  39. think_request_buy_food(); // don't return even if this request is sent
  40. //----- think request cease fire ----//
  41. if( think_request_cease_war() )
  42. return;
  43. //------ thinking about treaty ---------//
  44. if( think_trade_treaty() )
  45. return;
  46. if( think_propose_alliance_treaty() ) // try proposing alliance treaty first, then try proposing friendly treaty
  47. return;
  48. if( think_propose_friendly_treaty() )
  49. return;
  50. if( think_end_treaty() )
  51. return;
  52. //-------- think about other matters --------//
  53. if( think_demand_tribute_aid() )
  54. return;
  55. if( think_give_tech() )
  56. return;
  57. if( think_demand_tech() )
  58. return;
  59. //---- think about offering to purchase throne ----//
  60. if( think_request_surrender() )
  61. return;
  62. }
  63. //------ End of function Nation::think_diplomacy ------//
  64. //----- Begin of function Nation::should_diplomacy_retry -----//
  65. //
  66. int Nation::should_diplomacy_retry(int talkId, int nationRecno)
  67. {
  68. if( !talk_res.can_send_msg(nationRecno, nation_recno, talkId ) )
  69. return 0;
  70. int retryInterval;
  71. //--- shorter retry interval for demand talk message ----//
  72. if( talkId == TALK_DEMAND_TRIBUTE ||
  73. talkId == TALK_DEMAND_AID ||
  74. talkId == TALK_DEMAND_TECH )
  75. {
  76. retryInterval = 60 + 60 * (100-pref_diplomacy_retry) / 100; // 2-4 months
  77. }
  78. else
  79. {
  80. retryInterval = 90 + 270 * (100-pref_diplomacy_retry) / 100; // 3 months to 12 months before next try
  81. }
  82. return info.game_date >
  83. get_relation(nationRecno)->last_talk_reject_date_array[talkId-1] + retryInterval;
  84. }
  85. //------ End of function Nation::should_diplomacy_retry ------//
  86. //----- Begin of function Nation::ai_notify_reply -----//
  87. //
  88. // Notify this AI nation that there is a reply to one
  89. // of the diplomatic messages that it has sent out.
  90. //
  91. void Nation::ai_notify_reply(int talkMsgRecno)
  92. {
  93. err_when( talk_res.is_talk_msg_deleted(talkMsgRecno) );
  94. TalkMsg* talkMsg = talk_res.get_talk_msg(talkMsgRecno);
  95. int relationChange = 0;
  96. NationRelation* nationRelation = get_relation(talkMsg->to_nation_recno);
  97. if( talkMsg->reply_type == REPLY_REJECT )
  98. nationRelation->last_talk_reject_date_array[talkMsg->talk_id-1] = info.game_date;
  99. else
  100. nationRelation->last_talk_reject_date_array[talkMsg->talk_id-1] = 0;
  101. switch( talkMsg->talk_id )
  102. {
  103. case TALK_PROPOSE_TRADE_TREATY:
  104. if( talkMsg->reply_type == REPLY_ACCEPT )
  105. relationChange = pref_trading_tendency/10;
  106. else
  107. relationChange = -pref_trading_tendency/10;
  108. break;
  109. case TALK_PROPOSE_FRIENDLY_TREATY:
  110. case TALK_PROPOSE_ALLIANCE_TREATY:
  111. if( talkMsg->reply_type == REPLY_REJECT )
  112. relationChange = -5;
  113. break;
  114. case TALK_REQUEST_MILITARY_AID:
  115. if( talkMsg->reply_type == REPLY_ACCEPT )
  116. relationChange = 0; // the AI never knows whether the player has really aided him in the war
  117. else
  118. relationChange = -(20-pref_military_courage/10); // -10 to -20
  119. break;
  120. case TALK_REQUEST_TRADE_EMBARGO:
  121. if( talkMsg->reply_type == REPLY_ACCEPT )
  122. relationChange = (10+pref_trading_tendency/10); // +10 to +20
  123. else
  124. relationChange = -(10+pref_trading_tendency/20); // -10 to -15
  125. break;
  126. case TALK_REQUEST_CEASE_WAR:
  127. if( talkMsg->reply_type == REPLY_REJECT )
  128. relationChange = -5;
  129. break;
  130. case TALK_REQUEST_DECLARE_WAR:
  131. if( talkMsg->reply_type == REPLY_ACCEPT )
  132. relationChange = pref_allying_tendency/10;
  133. else
  134. relationChange = -30;
  135. break;
  136. case TALK_REQUEST_BUY_FOOD:
  137. if( talkMsg->reply_type == REPLY_ACCEPT )
  138. relationChange = pref_food_reserve/10;
  139. else
  140. relationChange = -pref_food_reserve/10;
  141. break;
  142. case TALK_DEMAND_TRIBUTE:
  143. case TALK_DEMAND_AID:
  144. if( talkMsg->reply_type == REPLY_ACCEPT )
  145. {
  146. //-- the less cash the nation, the more it will appreciate the tribute --//
  147. relationChange = 100 * talkMsg->talk_para1 / max(1000, (int) cash);
  148. }
  149. else
  150. {
  151. relationChange = -(400-pref_peacefulness)/10; // -30 to 40 points depending the peacefulness preference
  152. }
  153. break;
  154. case TALK_DEMAND_TECH:
  155. if( talkMsg->reply_type == REPLY_ACCEPT )
  156. relationChange = 10+pref_use_weapon/5; // +10 to +30
  157. else
  158. relationChange = -(10+pref_use_weapon/10); // -10 to -20
  159. break;
  160. case TALK_GIVE_TRIBUTE:
  161. case TALK_GIVE_AID:
  162. case TALK_GIVE_TECH:
  163. if( talkMsg->reply_type == REPLY_REJECT ) // reject your gift
  164. relationChange = -5;
  165. break;
  166. case TALK_REQUEST_SURRENDER: // no relation change on this request
  167. break;
  168. default:
  169. err_here();
  170. }
  171. //------- chance relationship now -------//
  172. if( relationChange < 0 )
  173. relationChange -= relationChange * (200-pref_forgiveness) / 200;
  174. if( relationChange != 0 )
  175. change_ai_relation_level( talkMsg->to_nation_recno, relationChange );
  176. //---- think about giving tribute to become more friendly with the nation so it will accept our request next time ---//
  177. if( talkMsg->reply_type == REPLY_REJECT )
  178. {
  179. if( think_give_tribute_aid( talkMsg ) )
  180. return;
  181. //--- if our request was rejected, end treaty if the ai_nation_relation is low enough ---//
  182. if( talkMsg->talk_id != TALK_PROPOSE_ALLIANCE_TREATY && // the rejected request is not alliance treaty
  183. nationRelation->status >= NATION_FRIENDLY &&
  184. nationRelation->ai_relation_level < 40-pref_allying_tendency/5 ) // 20 to 40
  185. {
  186. int talkId;
  187. if( nationRelation->status == NATION_FRIENDLY )
  188. talkId = TALK_END_FRIENDLY_TREATY;
  189. else
  190. talkId = TALK_END_ALLIANCE_TREATY;
  191. talk_res.ai_send_talk_msg(talkMsg->to_nation_recno, nation_recno, talkId);
  192. }
  193. //----- declare war if ai_relation_level==0 -----//
  194. else if( nationRelation->ai_relation_level == 0 )
  195. {
  196. //--------- declare war ---------//
  197. if( config.ai_aggressiveness >= OPTION_HIGH || pref_peacefulness < 50 )
  198. {
  199. talk_res.ai_send_talk_msg(talkMsg->to_nation_recno, nation_recno, TALK_DECLARE_WAR);
  200. //------- attack immediately --------//
  201. if( config.ai_aggressiveness >= OPTION_VERY_HIGH ||
  202. ( config.ai_aggressiveness >= OPTION_HIGH && pref_peacefulness < 50 ) )
  203. {
  204. if( largest_town_recno )
  205. {
  206. think_capture_new_enemy_town( town_array[largest_town_recno], 1 ); // 1-use forces from all camps to attack the target
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. //------ End of function Nation::ai_notify_reply ------//
  214. //----- Begin of function Nation::think_propose_friendly_treaty -----//
  215. //
  216. int Nation::think_propose_friendly_treaty()
  217. {
  218. //--- think about which nation this nation should propose treaty to ---//
  219. int curRating, bestRating=0, bestNationRecno=0;
  220. NationRelation* nationRelation;
  221. for( int i=1 ; i<=nation_array.size() ; i++ )
  222. {
  223. if( nation_array.is_deleted(i) || i==nation_recno )
  224. continue;
  225. nationRelation = get_relation(i);
  226. if( !nationRelation->has_contact || nationRelation->status >= NATION_FRIENDLY )
  227. continue;
  228. if( !should_diplomacy_retry(TALK_PROPOSE_FRIENDLY_TREATY, i) )
  229. continue;
  230. curRating = consider_friendly_treaty(i);
  231. if( curRating > bestRating )
  232. {
  233. bestRating = curRating;
  234. bestNationRecno = i;
  235. }
  236. }
  237. if( bestNationRecno )
  238. {
  239. talk_res.ai_send_talk_msg(bestNationRecno, nation_recno, TALK_PROPOSE_FRIENDLY_TREATY );
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. //------ End of function Nation::think_propose_friendly_treaty ------//
  245. //----- Begin of function Nation::think_propose_alliance_treaty -----//
  246. //
  247. int Nation::think_propose_alliance_treaty()
  248. {
  249. //--- think about which nation this nation should propose treaty to ---//
  250. int curRating, bestRating=0, bestNationRecno=0;
  251. NationRelation* nationRelation;
  252. for( int i=1 ; i<=nation_array.size() ; i++ )
  253. {
  254. if( nation_array.is_deleted(i) || i==nation_recno )
  255. continue;
  256. nationRelation = get_relation(i);
  257. if( !nationRelation->has_contact || nationRelation->status == NATION_ALLIANCE )
  258. continue;
  259. if( !should_diplomacy_retry(TALK_PROPOSE_ALLIANCE_TREATY, i) )
  260. continue;
  261. curRating = consider_alliance_treaty(i);
  262. if( curRating > bestRating )
  263. {
  264. bestRating = curRating;
  265. bestNationRecno = i;
  266. }
  267. }
  268. if( bestNationRecno )
  269. {
  270. talk_res.ai_send_talk_msg(bestNationRecno, nation_recno, TALK_PROPOSE_ALLIANCE_TREATY );
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. //------ End of function Nation::think_propose_alliance_treaty ------//
  276. //----- Begin of function Nation::think_request_cease_war -----//
  277. //
  278. int Nation::think_request_cease_war()
  279. {
  280. Nation* nationPtr;
  281. NationRelation* nationRelation;
  282. for( int i=1 ; i<=nation_array.size() ; i++ )
  283. {
  284. if( nation_array.is_deleted(i) || i==nation_recno )
  285. continue;
  286. nationPtr = nation_array[i];
  287. nationRelation = get_relation(i);
  288. if( nationRelation->status != NATION_HOSTILE )
  289. continue;
  290. if( !should_diplomacy_retry(TALK_REQUEST_CEASE_WAR, i) )
  291. continue;
  292. //----- think about if it should cease war with the nation ------//
  293. if( consider_cease_war(i) > 0 )
  294. {
  295. talk_res.ai_send_talk_msg(i, nation_recno, TALK_REQUEST_CEASE_WAR);
  296. }
  297. //--------------------------------------------//
  298. // The relation improves slowly if there is
  299. // no attack. However, if there is any battles
  300. // started between the two nations, the status will be
  301. // set to hostile and ai_relation_level will be set to 0 again.
  302. //--------------------------------------------//
  303. else
  304. {
  305. change_ai_relation_level(i, 1);
  306. }
  307. }
  308. return 0;
  309. }
  310. //------ End of function Nation::think_request_cease_war ------//
  311. //----- Begin of function Nation::think_end_treaty -----//
  312. //
  313. int Nation::think_end_treaty()
  314. {
  315. if( pref_honesty < 30 ) // never formally end a treaty if the honesty is < 30
  316. return 0;
  317. Nation* nationPtr;
  318. NationRelation* nationRelation;
  319. for( int i=1 ; i<=nation_array.size() ; i++ )
  320. {
  321. if( nation_array.is_deleted(i) || i==nation_recno )
  322. continue;
  323. nationRelation = get_relation(i);
  324. if( nationRelation->status < NATION_FRIENDLY )
  325. continue;
  326. if( nationRelation->ai_secret_attack ||
  327. ( nationRelation->ai_relation_level < 30 && trade_rating(i) < 50 ) )
  328. {
  329. //--- don't change terminate treaty too soon ---//
  330. if( info.game_date < nationRelation->last_change_status_date+60+pref_honesty/2 ) // only after 60 to 110 days
  331. continue;
  332. //----------------------------------------//
  333. if( !talk_res.can_send_msg(i, nation_recno, nationRelation->status==NATION_FRIENDLY ? TALK_END_FRIENDLY_TREATY : TALK_END_ALLIANCE_TREATY) )
  334. continue;
  335. nationPtr = nation_array[i];
  336. //-----------------------------------------//
  337. // What makes it tend to end treaty:
  338. // -higher honesty
  339. // -a larger overall power over the target nation.
  340. //
  341. // If honesty is > 50, if will end treaty
  342. // if its power is equal to the enemy.
  343. //
  344. // If honesty is < 50, if will end treaty
  345. // if its power is larger than the enemy.
  346. //
  347. // If honesty is > 50, if will end treaty
  348. // even if its power is lower than the enemy.
  349. //-----------------------------------------//
  350. if( pref_honesty-50 > nationPtr->overall_rating - overall_rating )
  351. {
  352. if( nationRelation->status == NATION_FRIENDLY )
  353. talk_res.ai_send_talk_msg(i, nation_recno, TALK_END_FRIENDLY_TREATY);
  354. else
  355. talk_res.ai_send_talk_msg(i, nation_recno, TALK_END_ALLIANCE_TREATY);
  356. return 1;
  357. }
  358. }
  359. }
  360. return 0;
  361. }
  362. //------ End of function Nation::think_end_treaty ------//
  363. //----- Begin of function Nation::think_trade_treaty -----//
  364. //
  365. int Nation::think_trade_treaty()
  366. {
  367. Nation* nationPtr;
  368. NationRelation *ourRelation;
  369. for( int i=1 ; i<=nation_array.size() ; i++ )
  370. {
  371. if( nation_array.is_deleted(i) || i==nation_recno )
  372. continue;
  373. nationPtr = nation_array[i];
  374. ourRelation = get_relation(i);
  375. if( !ourRelation->has_contact )
  376. continue;
  377. //------- propose a trade treaty --------//
  378. if( !ourRelation->trade_treaty )
  379. {
  380. if( consider_trade_treaty(i) > 0 )
  381. {
  382. if( should_diplomacy_retry(TALK_PROPOSE_TRADE_TREATY, i) )
  383. {
  384. talk_res.ai_send_talk_msg(i, nation_recno, TALK_PROPOSE_TRADE_TREATY);
  385. ourRelation->ai_demand_trade_treaty = 0;
  386. return 1;
  387. }
  388. }
  389. }
  390. }
  391. return 0;
  392. }
  393. //------ End of function Nation::think_trade_treaty ------//
  394. //----- Begin of function Nation::think_request_buy_food -----//
  395. //
  396. int Nation::think_request_buy_food()
  397. {
  398. //------ first see if we need to buy food ------//
  399. int yearFoodChange = yearly_food_change();
  400. int neededFoodLevel;
  401. if( yearFoodChange > 0 )
  402. {
  403. if( food > 0 )
  404. return 0;
  405. else
  406. neededFoodLevel = (int) -food; // if the food is negative
  407. }
  408. else
  409. {
  410. neededFoodLevel = -yearFoodChange * (100+pref_food_reserve) / 50;
  411. if( food > neededFoodLevel ) // one to three times (based on pref_food_reserve) of the food needed in a year,
  412. return 0;
  413. }
  414. //----- think about which nation to buy food from -----//
  415. Nation *nationPtr, *bestNation=NULL;
  416. int curRating, bestRating=0;
  417. int relationStatus;
  418. int i;
  419. for( i=1 ; i<=nation_array.size() ; i++ )
  420. {
  421. if( nation_array.is_deleted(i) || i==nation_recno )
  422. continue;
  423. nationPtr = nation_array[i];
  424. if( nationPtr->food < 500 ) // if the nation is short of food itself. The minimum request purchase qty is 500
  425. continue;
  426. relationStatus = get_relation_status(i);
  427. if( relationStatus == NATION_HOSTILE || !get_relation(i)->has_contact )
  428. continue;
  429. if( nationPtr->yearly_food_change() < 0 &&
  430. nationPtr->food < 1500 )
  431. {
  432. continue;
  433. }
  434. if( !should_diplomacy_retry(TALK_REQUEST_BUY_FOOD, i) )
  435. continue;
  436. //-----------------------------------//
  437. curRating = relationStatus*20 +
  438. (int)nationPtr->food / 100 +
  439. (int)nationPtr->yearly_food_change() / 10;
  440. if( curRating > bestRating )
  441. {
  442. bestRating = curRating;
  443. bestNation = nationPtr;
  444. }
  445. }
  446. if( !bestNation )
  447. return 0;
  448. //------------------------------------//
  449. static short buyQtyArray[] = { 500, 1000, 2000, 4000 };
  450. int buyQty=0, buyPrice;
  451. for( i=3 ; i>=0 ; i-- )
  452. {
  453. if( bestNation->food/2 > buyQtyArray[i] )
  454. {
  455. buyQty = buyQtyArray[i];
  456. break;
  457. }
  458. }
  459. if( buyQty == 0 )
  460. return 0;
  461. //------- set the offering price ------//
  462. if( food < neededFoodLevel/4 ) // if we need the food badly
  463. {
  464. buyPrice = 30;
  465. }
  466. else if( food < neededFoodLevel/3 )
  467. {
  468. buyPrice = 20;
  469. }
  470. else
  471. {
  472. if( bestNation->food > bestNation->all_population() * PERSON_FOOD_YEAR_CONSUMPTION * 5 && // if the nation has plenty of food
  473. bestNation->cash < bestNation->fixed_expense_365days() / 2 ) // if the nation runs short of cash
  474. {
  475. buyPrice = 5;
  476. }
  477. else
  478. buyPrice = 10;
  479. }
  480. talk_res.ai_send_talk_msg(bestNation->nation_recno, nation_recno, TALK_REQUEST_BUY_FOOD, buyQty, buyPrice);
  481. return 1;
  482. }
  483. //------ End of function Nation::think_request_buy_food ------//
  484. //----- Begin of function Nation::think_declare_war -----//
  485. //
  486. int Nation::think_declare_war()
  487. {
  488. NationRelation* nationRelation;
  489. int rc=0;
  490. //---- don't declare a new war if we already has enemies ---//
  491. int i;
  492. for( i=1 ; i<=nation_array.size() ; i++ )
  493. {
  494. if( nation_array.is_deleted(i) || i==nation_recno )
  495. continue;
  496. if( get_relation(i)->status == NATION_HOSTILE )
  497. return 0;
  498. }
  499. //------------------------------------------------//
  500. int targetStrength, minStrength=0x1000, bestTargetNation=0;
  501. for( i=1 ; i<=nation_array.size() ; i++ )
  502. {
  503. if( nation_array.is_deleted(i) || i==nation_recno )
  504. continue;
  505. nationRelation = get_relation(i);
  506. if( !nationRelation->has_contact )
  507. continue;
  508. if( nationRelation->status == NATION_HOSTILE ) // already at war
  509. continue;
  510. if( nationRelation->ai_relation_level >= 10 )
  511. continue;
  512. if( !ai_should_spend( 100-trade_rating(i) ) ) // if trade_rating is 0, importanceRating will be 100, if trade_rating is 100, importanceRating will be 0
  513. continue;
  514. //----------------------------------------//
  515. Nation* targetNation = nation_array[i];
  516. targetStrength = targetNation->military_rank_rating() +
  517. targetNation->population_rank_rating()/2 +
  518. targetNation->economic_rank_rating()/3;
  519. if( targetStrength < minStrength )
  520. {
  521. minStrength = targetStrength;
  522. bestTargetNation = i;
  523. }
  524. }
  525. //------------------------------------------//
  526. if( bestTargetNation )
  527. {
  528. if( should_diplomacy_retry(TALK_DECLARE_WAR, bestTargetNation) )
  529. {
  530. talk_res.ai_send_talk_msg(bestTargetNation, nation_recno, TALK_DECLARE_WAR);
  531. return 1;
  532. }
  533. }
  534. return 0;
  535. }
  536. //------ End of function Nation::think_declare_war ------//
  537. //----- Begin of function Nation::think_give_tribute_aid -----//
  538. //
  539. // This function is called when a nation rejected our request
  540. // which is important to us and we want to give tribute to the
  541. // nation so it may accept next time.
  542. //
  543. // <TalkMsg*> rejectedMsg - the TalkMsg that has been rejected.
  544. //
  545. int Nation::think_give_tribute_aid(TalkMsg* rejectedMsg)
  546. {
  547. //-----------get the talk id. ------------//
  548. int talkId;
  549. int talkNationRecno = rejectedMsg->to_nation_recno;
  550. int rejectedTalkId = rejectedMsg->talk_id;
  551. NationRelation* nationRelation = get_relation(talkNationRecno);
  552. if( nationRelation->status >= NATION_FRIENDLY )
  553. talkId = TALK_GIVE_AID;
  554. else
  555. talkId = TALK_GIVE_TRIBUTE;
  556. //-------- don't give tribute too frequently -------//
  557. if( info.game_date <
  558. nationRelation->last_talk_reject_date_array[talkId-1] + 365 - pref_allying_tendency )
  559. {
  560. return 0;
  561. }
  562. //---- think if the nation should spend money now ----//
  563. static short tributeAmountArray[] = { 500, 1000 };
  564. int tributeAmount = tributeAmountArray[m.random(2)];
  565. if( !ai_should_spend(0, (float) tributeAmount) ) // importance rating is 0
  566. return 0;
  567. //--------------------------------------//
  568. Nation* talkNation = nation_array[talkNationRecno];
  569. int rc;
  570. if( rejectedTalkId == TALK_PROPOSE_TRADE_TREATY )
  571. {
  572. rc = ai_trade_with_rating(talkNationRecno) > 100-pref_trading_tendency/2;
  573. }
  574. else if ( rejectedTalkId == TALK_PROPOSE_FRIENDLY_TREATY ||
  575. rejectedTalkId == TALK_PROPOSE_ALLIANCE_TREATY )
  576. {
  577. int curRating = talkNation->trade_rating(talkNationRecno) +
  578. ai_trade_with_rating(talkNationRecno) +
  579. talkNation->overall_rating - overall_rating;
  580. int acceptRating = 200-pref_trading_tendency/4
  581. -pref_allying_tendency/4;
  582. rc = curRating >= acceptRating;
  583. }
  584. //--------------------------------------//
  585. else if( rejectedTalkId == TALK_REQUEST_CEASE_WAR )
  586. {
  587. rc = talkNation->military_rank_rating() >
  588. military_rank_rating() + (100-pref_peacefulness)/2;
  589. }
  590. //--------------------------------------//
  591. if( rc )
  592. {
  593. //------ give tribute --------//
  594. talk_res.ai_send_talk_msg(talkNationRecno, nation_recno, talkId, tributeAmount);
  595. nationRelation->last_talk_reject_date_array[talkId-1] = info.game_date;
  596. //------ request again after giving tribute ----//
  597. nationRelation->last_talk_reject_date_array[rejectedTalkId-1] = 0; // reset the rejected talk id.
  598. talk_res.ai_send_talk_msg(talkNationRecno, nation_recno, rejectedTalkId, rejectedMsg->talk_para1, rejectedMsg->talk_para2 );
  599. }
  600. return rc;
  601. }
  602. //------ End of function Nation::think_give_tribute_aid ------//
  603. //----- Begin of function Nation::think_demand_tribute_aid -----//
  604. //
  605. // Demand tribute when the nation's economy is good and its
  606. // military is weak.
  607. //
  608. int Nation::think_demand_tribute_aid()
  609. {
  610. if( info.game_date < info.game_start_date + 180 + nation_recno*50 ) // don't ask for tribute too soon, as in the beginning, the ranking are all the same for all nations
  611. return 0;
  612. //--------------------------------------//
  613. Nation* nationPtr;
  614. int totalNation=nation_array.size();
  615. int nationRecno=m.random(totalNation)+1;
  616. int curRating, requestRating;
  617. int talkId;
  618. int ourMilitary = military_rank_rating();
  619. int ourEconomy = economic_rank_rating();
  620. for( int i=totalNation ; i>0 ; i-- )
  621. {
  622. if( ++nationRecno > totalNation )
  623. nationRecno = 1;
  624. if( nation_array.is_deleted(nationRecno) || nationRecno==nation_recno )
  625. continue;
  626. nationPtr = nation_array[nationRecno];
  627. //-- only demand tribute from non-friendly nations --//
  628. if( get_relation(nationRecno)->status <= NATION_NEUTRAL )
  629. talkId = TALK_DEMAND_TRIBUTE;
  630. else
  631. talkId = TALK_DEMAND_AID;
  632. //-----------------------------------------------//
  633. float fixedExpense = fixed_expense_365days();
  634. if( talkId == TALK_DEMAND_TRIBUTE )
  635. {
  636. if( !should_diplomacy_retry(talkId, nationRecno) )
  637. continue;
  638. curRating = ourMilitary - nationPtr->military_rank_rating();
  639. if( curRating < 0 )
  640. continue;
  641. //----------------------------------------------//
  642. //
  643. // Some nation will actually consider the ability
  644. // of the target nation to pay tribute, so nation
  645. // will not and just ask anyway.
  646. //
  647. //----------------------------------------------//
  648. if( pref_economic_development > 50 )
  649. {
  650. int addRating = nationPtr->economic_rank_rating()-ourEconomy;
  651. if( addRating > 0 )
  652. curRating += addRating;
  653. }
  654. requestRating = 20 + trade_rating(nationRecno)/2 +
  655. (100-pref_peacefulness)/3;
  656. if( cash < fixedExpense && fixedExpense != 0 )
  657. requestRating -= int( (float) requestRating * cash / fixedExpense);
  658. }
  659. else
  660. {
  661. if( cash >= fixedExpense )
  662. continue;
  663. if( cash > fixedExpense * (50+pref_cash_reserve) / 300 && // if the nation is runing short of cash, don't wait a while until next retry, retry immediately
  664. !should_diplomacy_retry(talkId, nationRecno) )
  665. {
  666. continue;
  667. }
  668. //----- only ask for aid when the nation is short of cash ----//
  669. curRating = (ourMilitary - nationPtr->military_rank_rating())/2 +
  670. ( nationPtr->economic_rank_rating()-ourEconomy );
  671. requestRating = 20 + 50 * (int)(cash / fixedExpense);
  672. }
  673. //----- if this is a human player's nation -----//
  674. if( !nationPtr->is_ai() )
  675. {
  676. switch( config.ai_aggressiveness )
  677. {
  678. case OPTION_LOW:
  679. requestRating += 40; // don't go against the player too easily
  680. break;
  681. case OPTION_HIGH:
  682. requestRating -= 20;
  683. break;
  684. case OPTION_VERY_HIGH:
  685. requestRating -= 40;
  686. break;
  687. }
  688. //--- if the nation has plenty of cash, demand from it ----//
  689. if( nationPtr->cash > cash && config.ai_aggressiveness >= OPTION_HIGH )
  690. {
  691. requestRating -= (int) (nationPtr->cash - cash)/500;
  692. }
  693. }
  694. //--------------------------------------//
  695. if( curRating > requestRating )
  696. {
  697. int tributeAmount;
  698. if( curRating - requestRating > 120 )
  699. tributeAmount = 4000;
  700. else if( curRating - requestRating > 80 )
  701. tributeAmount = 3000;
  702. else if( curRating - requestRating > 40 )
  703. tributeAmount = 2000;
  704. else if( curRating - requestRating > 20 )
  705. tributeAmount = 1000;
  706. else
  707. tributeAmount = 500;
  708. talk_res.ai_send_talk_msg(nationRecno, nation_recno, talkId, tributeAmount);
  709. return 1;
  710. }
  711. }
  712. return 0;
  713. }
  714. //------ End of function Nation::think_demand_tribute_aid ------//
  715. //----- Begin of function Nation::think_demand_tech -----//
  716. //
  717. int Nation::think_demand_tech()
  718. {
  719. if( m.random(10) > 0 ) // only 1/10 chance of calling this function
  720. return 0;
  721. Nation* nationPtr;
  722. int totalNation=nation_array.size();
  723. int nationRecno=m.random(totalNation)+1;
  724. for( int i=totalNation ; i>0 ; i-- )
  725. {
  726. if( ++nationRecno > totalNation )
  727. nationRecno = 1;
  728. if( nation_array.is_deleted(nationRecno) || nationRecno==nation_recno )
  729. continue;
  730. nationPtr = nation_array[nationRecno];
  731. if( nationPtr->total_tech_level() == 0 )
  732. continue;
  733. if( !should_diplomacy_retry(TALK_DEMAND_TECH, nationRecno) )
  734. continue;
  735. //--- don't request from hostile or tense nations -----//
  736. if( get_relation(nationRecno)->status < NATION_NEUTRAL )
  737. continue;
  738. //---- scan which tech that the nation has but we don't have ----//
  739. int techId;
  740. for( techId=1 ; techId<=tech_res.tech_count ; techId++ )
  741. {
  742. TechInfo *techInfo = tech_res[techId];
  743. if( techInfo->get_nation_tech_level(nation_recno)==0 &&
  744. techInfo->get_nation_tech_level(nationRecno) > 0 )
  745. {
  746. break;
  747. }
  748. }
  749. if( techId > tech_res.tech_count )
  750. continue;
  751. //-------- send the message now ---------//
  752. talk_res.ai_send_talk_msg(nationRecno, nation_recno, TALK_DEMAND_TECH, techId);
  753. return 1;
  754. }
  755. return 0;
  756. }
  757. //------ End of function Nation::think_demand_tech ------//
  758. //----- Begin of function Nation::think_give_tech -----//
  759. //
  760. int Nation::think_give_tech()
  761. {
  762. return 0;
  763. }
  764. //------ End of function Nation::think_give_tech ------//
  765. //----- Begin of function Nation::think_request_surrender -----//
  766. //
  767. int Nation::think_request_surrender()
  768. {
  769. if( m.random(5) != 0 ) // don't do this too often
  770. return 0;
  771. //---- only do so when we have enough cash ----//
  772. if( cash < fixed_expense_365days() + 5000 + 10000 * pref_cash_reserve / 100 )
  773. return 0;
  774. if( profit_365days() < 0 && cash < 20000 ) // don't ask if we are losing money and the cash isn't plenty
  775. return 0;
  776. //----- calculate the amount this nation can offer ----//
  777. int offerAmount = (int)cash - min(5000, (int)fixed_expense_365days());
  778. static int amtArray[] = { 5000, 7500, 10000, 15000, 20000, 30000, 40000, 50000 };
  779. int i;
  780. for( i=7 ; i>=0 ; i-- )
  781. {
  782. if( offerAmount >= amtArray[i] )
  783. {
  784. offerAmount = amtArray[i];
  785. break;
  786. }
  787. }
  788. if( i<0 )
  789. return 0;
  790. //---------------------------------------------//
  791. Nation* nationPtr;
  792. int ourOverallRankRating = overall_rank_rating();
  793. int totalNation = nation_array.size();
  794. int nationRecno = m.random(totalNation)+1;
  795. for( i=0 ; i<totalNation ; i++ )
  796. {
  797. if( ++nationRecno > totalNation )
  798. nationRecno = 1;
  799. if( nation_array.is_deleted(nationRecno) || nation_recno==nationRecno )
  800. continue;
  801. nationPtr = nation_array[nationRecno];
  802. //--- don't ask for a kingdom that is more powerful to surrender to us ---//
  803. if( nationPtr->cash > 100 ) // unless it is running short of cash
  804. {
  805. if( nationPtr->overall_rank_rating() > ourOverallRankRating )
  806. continue;
  807. }
  808. //-------------------------------------------//
  809. if( !should_diplomacy_retry(TALK_REQUEST_SURRENDER, nationRecno) )
  810. continue;
  811. //-------------------------------------------//
  812. talk_res.ai_send_talk_msg(nationRecno, nation_recno,
  813. TALK_REQUEST_SURRENDER, offerAmount/10 ); // divide by 10 to cope with <short>'s upper limit
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. //------ End of function Nation::think_request_surrender ------//