OAI_TALK.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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_TALK.CPP
  21. //Description: AI routines on diplomacy.
  22. #include <OCONFIG.h>
  23. #include <OTALKRES.h>
  24. #include <OTECHRES.h>
  25. #include <OF_MARK.h>
  26. #include <ONATION.h>
  27. //-------- Declare static functions ---------//
  28. static int has_sent_same_msg(TalkMsg* talkMsg);
  29. //----- Begin of function Nation::ai_process_talk_msg -----//
  30. //
  31. // action_para - recno of the message in talk_res.talk_msg_array.
  32. //
  33. int Nation::ai_process_talk_msg(ActionNode* actionNode)
  34. {
  35. if( talk_res.is_talk_msg_deleted(actionNode->action_para) ) // if the talk message has been deleted
  36. return -1;
  37. TalkMsg* talkMsg = talk_res.get_talk_msg(actionNode->action_para);
  38. err_when( talkMsg->talk_id < 1 || talkMsg->talk_id > MAX_TALK_TYPE );
  39. err_when( talkMsg->from_nation_recno == nation_recno );
  40. err_when( talkMsg->to_nation_recno != nation_recno );
  41. if( !talkMsg->is_valid_to_reply() ) // if it is no longer valid
  42. return -1;
  43. //----- call the consider function -------//
  44. if( talkMsg->reply_type == REPLY_WAITING )
  45. {
  46. int rc = consider_talk_msg(talkMsg);
  47. if( rc==1 ) // if rc is not 1 or 0, than the consider function have processed itself, no need to call reply_talk_msg() here
  48. talk_res.reply_talk_msg( actionNode->action_para, REPLY_ACCEPT, COMMAND_AI );
  49. else if( rc==0 )
  50. talk_res.reply_talk_msg( actionNode->action_para, REPLY_REJECT, COMMAND_AI );
  51. // don't reply if rc is neither 0 or 1
  52. }
  53. else
  54. err_here();
  55. return -1; // always return -1 to remove the action from action_array.
  56. }
  57. //------ End of function Nation::ai_process_talk_msg ------//
  58. //----- Begin of function Nation::consider_talk_msg -----//
  59. //
  60. int Nation::consider_talk_msg(TalkMsg* talkMsg)
  61. {
  62. //--------------------------------------------//
  63. // Whether the nation has already sent out a
  64. // message that is the same as the one it received.
  65. // If so, accept the message right now.
  66. //--------------------------------------------//
  67. switch( talkMsg->talk_id )
  68. {
  69. case TALK_PROPOSE_TRADE_TREATY:
  70. case TALK_PROPOSE_FRIENDLY_TREATY:
  71. case TALK_PROPOSE_ALLIANCE_TREATY:
  72. case TALK_REQUEST_TRADE_EMBARGO:
  73. case TALK_REQUEST_CEASE_WAR:
  74. case TALK_REQUEST_DECLARE_WAR:
  75. if( has_sent_same_msg(talkMsg) )
  76. return 1;
  77. };
  78. //-------------------------------//
  79. switch( talkMsg->talk_id )
  80. {
  81. case TALK_PROPOSE_TRADE_TREATY:
  82. return consider_trade_treaty(talkMsg->from_nation_recno) >= 0; // the returned value is the curRating - acceptRating, if >=0, means it accepts
  83. case TALK_PROPOSE_FRIENDLY_TREATY:
  84. return consider_friendly_treaty(talkMsg->from_nation_recno) >= 0;
  85. case TALK_PROPOSE_ALLIANCE_TREATY:
  86. return consider_alliance_treaty(talkMsg->from_nation_recno) >= 0;
  87. case TALK_REQUEST_MILITARY_AID:
  88. return consider_military_aid(talkMsg);
  89. case TALK_REQUEST_TRADE_EMBARGO:
  90. return consider_trade_embargo(talkMsg);
  91. case TALK_REQUEST_CEASE_WAR:
  92. return consider_cease_war(talkMsg->from_nation_recno) >= 0;
  93. case TALK_REQUEST_DECLARE_WAR:
  94. return consider_declare_war(talkMsg);
  95. case TALK_REQUEST_BUY_FOOD:
  96. return consider_sell_food(talkMsg);
  97. case TALK_GIVE_TRIBUTE:
  98. return consider_take_tribute(talkMsg);
  99. case TALK_DEMAND_TRIBUTE:
  100. return consider_give_tribute(talkMsg);
  101. case TALK_GIVE_AID:
  102. return consider_take_aid(talkMsg);
  103. case TALK_DEMAND_AID:
  104. return consider_give_aid(talkMsg);
  105. case TALK_GIVE_TECH:
  106. return consider_take_tech(talkMsg);
  107. case TALK_DEMAND_TECH:
  108. return consider_give_tech(talkMsg);
  109. case TALK_REQUEST_SURRENDER:
  110. return consider_accept_surrender_request(talkMsg);
  111. default:
  112. err_here();
  113. return 0;
  114. }
  115. }
  116. //------ End of function Nation::consider_talk_msg ------//
  117. //----- Begin of function Nation::notify_talk_msg -----//
  118. //
  119. // Notify the AI for a notification only message (reply not needed.)
  120. //
  121. // This function is called directly from TalkRes::send_talk_msg_now()
  122. // when the message is sent.
  123. //
  124. void Nation::notify_talk_msg(TalkMsg* talkMsg)
  125. {
  126. int relationChange=0;
  127. NationRelation* nationRelation = get_relation(talkMsg->from_nation_recno);
  128. switch( talkMsg->talk_id )
  129. {
  130. case TALK_END_TRADE_TREATY: // it's a notification message only, no accept or reject
  131. relationChange = -5;
  132. nationRelation->last_talk_reject_date_array[TALK_PROPOSE_TRADE_TREATY-1] = info.game_date;
  133. break;
  134. case TALK_END_FRIENDLY_TREATY: // it's a notification message only, no accept or reject
  135. case TALK_END_ALLIANCE_TREATY:
  136. relationChange = -5;
  137. nationRelation->last_talk_reject_date_array[TALK_PROPOSE_FRIENDLY_TREATY-1] = info.game_date;
  138. nationRelation->last_talk_reject_date_array[TALK_PROPOSE_ALLIANCE_TREATY-1] = info.game_date;
  139. break;
  140. case TALK_DECLARE_WAR: // it already drops to zero when the status is set to hostile
  141. break;
  142. case TALK_GIVE_TRIBUTE:
  143. case TALK_GIVE_AID:
  144. //--------------------------------------------------------------//
  145. // The less cash the nation, the more it will appreciate the
  146. // tribute.
  147. //
  148. // $1000 for 100 ai relation increase if the nation's cash is 1000.
  149. //--------------------------------------------------------------//
  150. relationChange = 100 * talkMsg->talk_para1 / max(1000, (int) cash);
  151. break;
  152. case TALK_GIVE_TECH:
  153. //--------------------------------------------------------------//
  154. // The lower tech the nation has, the more it will appreciate the
  155. // tech giveaway.
  156. //
  157. // Giving a level 2 weapon which the nation is unknown of
  158. // increase the ai relation by 60 if its pref_use_weapon is 100.
  159. // (by 30 if its pref_use_weapon is 0).
  160. //--------------------------------------------------------------//
  161. {
  162. int ownLevel = tech_res[talkMsg->talk_para1]->get_nation_tech_level(nation_recno);
  163. if( talkMsg->talk_para2 > ownLevel )
  164. relationChange = 30 * (talkMsg->talk_para2-ownLevel)
  165. * (100+pref_use_weapon) / 200;
  166. break;
  167. }
  168. case TALK_SURRENDER:
  169. break;
  170. default:
  171. err_here();
  172. }
  173. //------- chance relationship now -------//
  174. if( relationChange < 0 )
  175. relationChange -= relationChange * pref_forgiveness / 100;
  176. if( relationChange != 0 )
  177. change_ai_relation_level( talkMsg->from_nation_recno, relationChange );
  178. }
  179. //------ End of function Nation::notify_talk_msg ------//
  180. //----- Begin of function Nation::consider_trade_treaty -----//
  181. //
  182. // Consider agreeing to open up trade with the given nation.
  183. //
  184. int Nation::consider_trade_treaty(int withNationRecno)
  185. {
  186. NationRelation* nationRelation = get_relation(withNationRecno);
  187. //---- don't accept new trade treaty soon when the trade treaty was terminated not too long ago ----//
  188. if( info.game_date < nationRelation->last_talk_reject_date_array[TALK_END_TRADE_TREATY-1] + 365 - pref_forgiveness )
  189. return 0;
  190. //-- if we look forward to have a trade treaty with this nation ourselves --//
  191. if( nationRelation->ai_demand_trade_treaty )
  192. return 1;
  193. return ai_trade_with_rating(withNationRecno) > 0;
  194. }
  195. //------ End of function Nation::consider_trade_treaty ------//
  196. //----- Begin of function Nation::ai_trade_with_rating -----//
  197. //
  198. // Return a rating from 0 to 100 indicating how important
  199. // will be for us to trade with the given nation.
  200. //
  201. int Nation::ai_trade_with_rating(int withNationRecno)
  202. {
  203. Nation* nationPtr = nation_array[withNationRecno];
  204. int tradeRating=0;
  205. for( int i=0 ; i<MAX_RAW ; i++ )
  206. {
  207. //--------------------------------------------------------------//
  208. //
  209. // If we have the raw material and it doesn't have, then we
  210. // can export to it. And it is more favorite if the nation's
  211. // population is high, so we can export more.
  212. //
  213. //--------------------------------------------------------------//
  214. if( raw_count_array[i] && !nationPtr->raw_count_array[i] )
  215. tradeRating += min(30, nationPtr->total_population/3);
  216. //--------------------------------------------------------------//
  217. //
  218. // If the nation has the supply a raw material that we don't
  219. // have, then we can import it.
  220. //
  221. //--------------------------------------------------------------//
  222. else if( nationPtr->raw_count_array[i] && !raw_count_array[i] )
  223. tradeRating += 30;
  224. }
  225. return tradeRating;
  226. }
  227. //------ End of function Nation::ai_trade_with_rating ------//
  228. //----- Begin of function Nation::consider_friendly_treaty -----//
  229. //
  230. int Nation::consider_friendly_treaty(int withNationRecno)
  231. {
  232. NationRelation* nationRelation = get_relation(withNationRecno);
  233. if( nationRelation->status >= NATION_FRIENDLY ) // already has a friendly relationship
  234. return -1; // -1 means don't reply
  235. if( nationRelation->ai_relation_level < 20 )
  236. return -1;
  237. //------- some consideration first -------//
  238. if( !should_consider_friendly(withNationRecno) )
  239. return -1;
  240. //------ total import and export amounts --------//
  241. int curRating = consider_alliance_rating(withNationRecno);
  242. int acceptRating = 60 - pref_allying_tendency/8 - pref_peacefulness/4; // range of acceptRating: 23 to 60
  243. return curRating - acceptRating;
  244. }
  245. //------ End of function Nation::consider_friendly_treaty ------//
  246. //----- Begin of function Nation::consider_alliance_treaty -----//
  247. //
  248. int Nation::consider_alliance_treaty(int withNationRecno)
  249. {
  250. NationRelation* nationRelation = get_relation(withNationRecno);
  251. if( nationRelation->status >= NATION_ALLIANCE ) // already has a friendly relationship
  252. return -1; // -1 means don't reply
  253. if( nationRelation->ai_relation_level < 40 )
  254. return -1;
  255. //------- some consideration first -------//
  256. if( !should_consider_friendly(withNationRecno) )
  257. return -1;
  258. //------ total import and export amounts --------//
  259. int curRating = consider_alliance_rating(withNationRecno);
  260. int acceptRating = 80 - pref_allying_tendency/4 - pref_peacefulness/8; // range of acceptRating: 43 to 80
  261. return curRating - acceptRating;
  262. }
  263. //------ End of function Nation::consider_alliance_treaty ------//
  264. //----- Begin of function Nation::consider_cease_war -----//
  265. //
  266. // This function is shared by think_request_cease_war().
  267. //
  268. int Nation::consider_cease_war(int withNationRecno)
  269. {
  270. NationRelation* nationRelation = get_relation(withNationRecno);
  271. if( nationRelation->status != NATION_HOSTILE )
  272. return -1; // -1 means don't reply
  273. //---- if we are attacking the nation, don't cease fire ----//
  274. if( ai_attack_target_nation_recno == withNationRecno )
  275. return -1;
  276. //---- if we are planning to capture the enemy's town ---//
  277. if( ai_capture_enemy_town_recno &&
  278. !town_array.is_deleted(ai_capture_enemy_town_recno) &&
  279. town_array[ai_capture_enemy_town_recno]->nation_recno == withNationRecno )
  280. {
  281. return -1;
  282. }
  283. //--- don't cease fire too soon after a war is declared ---//
  284. if( info.game_date < nationRelation->last_change_status_date + 60 + (100-pref_peacefulness) ) // more peaceful nation may cease fire sooner (but the minimum is 60 days).
  285. return -1;
  286. //------ if we're run short of money for war -----//
  287. Nation* withNation = nation_array[withNationRecno];
  288. if( !ai_should_spend_war(withNation->military_rank_rating(), 1) ) // if we shouldn't spend any more on war, then return 1
  289. return 1;
  290. //------------------------------------------------//
  291. int curRating = consider_alliance_rating(withNationRecno);
  292. //------------------------------------//
  293. //
  294. // Tend to be easilier to accept cease-fire if this nation's
  295. // military strength is weak.
  296. //
  297. // If the nation's peacefulness concern is high, it will
  298. // also be more likely to accept cease-fire.
  299. //
  300. //-------------------------------------//
  301. //--- if the enemy is more power than us, tend more to request cease-fire ---//
  302. curRating += total_enemy_military() - military_rank_rating();
  303. curRating += ai_trade_with_rating(withNationRecno) * (100+pref_trading_tendency) / 300; // when we have excessive supply, we may want to cease-fire with our enemy
  304. curRating -= (military_rank_rating()-50)/2; // if our military ranking is high, we may like to continue the war, otherwise the nation should try to cease-fire
  305. curRating -= nationRelation->started_war_on_us_count*10; // the number of times this nation has started a war with us, the higher the number, the more unlikely we will accept cease-fire
  306. int acceptRating = pref_peacefulness/4;
  307. return curRating - acceptRating;
  308. }
  309. //------ End of function Nation::consider_cease_war ------//
  310. //----- Begin of function Nation::consider_sell_food -----//
  311. //
  312. // talkMsg->talk_para1 - qty of food wanted to buy.
  313. // talkMsg->talk_para2 - buying price offered for 10 food.
  314. //
  315. int Nation::consider_sell_food(TalkMsg* talkMsg)
  316. {
  317. int relationStatus = get_relation_status(talkMsg->from_nation_recno);
  318. if( relationStatus == NATION_HOSTILE )
  319. return 0;
  320. //--- if after selling the food, the remaining is not enough for its own consumption for ? years ---//
  321. float newFood = food-talkMsg->talk_para1;
  322. float yearConsumption = (float) yearly_food_consumption();
  323. int offeredAmount = talkMsg->talk_para2;
  324. int relationLevel = get_relation(talkMsg->from_nation_recno)->ai_relation_level;
  325. if( newFood < 1000 + 1000 * pref_food_reserve / 100 )
  326. return 0;
  327. if( relationLevel >= 50 )
  328. offeredAmount += 5; // increase the chance of selling food
  329. else if( relationLevel < 30 ) // decrease the chance of selling food
  330. offeredAmount -=5 ;
  331. //---- if we run short of cash, we tend to accept the offer ---//
  332. float fixedExpense = fixed_expense_365days();
  333. if( cash < fixedExpense )
  334. offeredAmount += (int) (20 * (fixedExpense-cash) / fixedExpense);
  335. //---------------------------------//
  336. float reserveYears = (float) (100+pref_food_reserve) / 100; // 1 to 2 years
  337. if( yearly_food_change() > 0 &&
  338. newFood > yearConsumption * reserveYears )
  339. {
  340. if( offeredAmount >= 10 ) // offered >= $10
  341. {
  342. return 1;
  343. }
  344. else // < $10, only if we have plenty of reserve
  345. {
  346. if( newFood > yearConsumption * reserveYears * 2 )
  347. return 1;
  348. }
  349. }
  350. else
  351. {
  352. if( offeredAmount >= 20 )
  353. {
  354. if( yearly_food_change() > 0 &&
  355. newFood > yearConsumption * reserveYears / 2 )
  356. {
  357. return 1;
  358. }
  359. }
  360. if( offeredAmount >= 30 )
  361. {
  362. return yearly_food_change() > 0 ||
  363. newFood > yearConsumption * reserveYears;
  364. }
  365. }
  366. return 0;
  367. }
  368. //------ End of function Nation::consider_sell_food ------//
  369. //----- Begin of function Nation::should_consider_friendly -----//
  370. //
  371. int Nation::should_consider_friendly(int withNationRecno)
  372. {
  373. Nation* withNation = nation_array[withNationRecno];
  374. //------- if this is a larger nation -------//
  375. if( overall_rank_rating() / 100 > 50 )
  376. {
  377. //--- big nations don't ally with their biggest opponents ---//
  378. int maxOverallRating=0;
  379. int biggestOpponentNationRecno=0;
  380. for( int i=nation_array.size() ; i>0 ; i-- )
  381. {
  382. if( nation_array.is_deleted(i) || i==nation_recno )
  383. continue;
  384. int overallRating = nation_array[i]->overall_rating;
  385. if( overallRating > maxOverallRating )
  386. {
  387. maxOverallRating = overallRating;
  388. biggestOpponentNationRecno = i;
  389. }
  390. }
  391. if( biggestOpponentNationRecno == withNationRecno )
  392. return 0;
  393. }
  394. //--- don't ally with nations with too low reputation ---//
  395. return withNation->reputation >= min(20, reputation) - 20;
  396. }
  397. //------ End of function Nation::should_consider_friendly -----//
  398. //----- Begin of function Nation::consider_alliance_rating -----//
  399. //
  400. // Return a rating from 0 to 100 for whether this nation should ally
  401. // with the given nation.
  402. //
  403. int Nation::consider_alliance_rating(int nationRecno)
  404. {
  405. Nation* nationPtr = nation_array[nationRecno];
  406. //---- the current relation affect the alliance tendency ---//
  407. NationRelation* nationRelation = get_relation(nationRecno);
  408. int allianceRating = nationRelation->ai_relation_level-20;
  409. //--- if the nation has a bad record of starting wars with us before, decrease the rating ---//
  410. allianceRating -= nationRelation->started_war_on_us_count * 20;
  411. //------ add the trade rating -------//
  412. int tradeRating = trade_rating(nationRecno) + // existing trade amount
  413. ai_trade_with_rating(nationRecno)/2; // possible trade
  414. allianceRating += tradeRating;
  415. //---- if the nation's power is larger than us, it's a plus ----//
  416. int powerRating = nationPtr->military_rank_rating() - military_rank_rating(); // if the nation's power is larger than ours, it's good to form treaty with them
  417. if( powerRating > 0 )
  418. allianceRating += powerRating;
  419. return allianceRating;
  420. }
  421. //------ End of function Nation::consider_alliance_rating -----//
  422. //----- Begin of function Nation::consider_take_tribute -----//
  423. //
  424. // talkMsg->talk_para1 - amount of the tribute.
  425. //
  426. int Nation::consider_take_tribute(TalkMsg* talkMsg)
  427. {
  428. int cashSignificance = 100 * talkMsg->talk_para1 / max(1000, (int) cash);
  429. //--- It does not necessarily want the tribute ---//
  430. int aiRelationLevel = get_relation(talkMsg->from_nation_recno)->ai_relation_level;
  431. if( true_profit_365days() > 0 &&
  432. cashSignificance < (100-aiRelationLevel)/5 )
  433. {
  434. return 0;
  435. }
  436. //----------- take the tribute ------------//
  437. int relationChange = cashSignificance * (100+pref_cash_reserve) / 200;
  438. change_ai_relation_level( talkMsg->from_nation_recno, relationChange );
  439. return 1;
  440. }
  441. //------ End of function Nation::consider_take_tribute ------//
  442. //----- Begin of function Nation::consider_take_aid -----//
  443. //
  444. // talkMsg->talk_para1 - amount of the tribute.
  445. //
  446. int Nation::consider_take_aid(TalkMsg* talkMsg)
  447. {
  448. int cashSignificance = 100 * talkMsg->talk_para1 / max(1000, (int) cash);
  449. //--- It does not necessarily want the tribute ---//
  450. int aiRelationLevel = get_relation(talkMsg->from_nation_recno)->ai_relation_level;
  451. if( true_profit_365days() > 0 &&
  452. cashSignificance < (100-aiRelationLevel)/5 )
  453. {
  454. return 0;
  455. }
  456. //----------- take the tribute ------------//
  457. int relationChange = cashSignificance * (100+pref_cash_reserve) / 200;
  458. change_ai_relation_level( talkMsg->from_nation_recno, relationChange );
  459. return 1;
  460. }
  461. //------ End of function Nation::consider_take_aid ------//
  462. //-------- Begin of static function has_sent_same_msg --------//
  463. //
  464. // Whether the nation has already sent out a message that is
  465. // the same as the one it received.
  466. //
  467. static int has_sent_same_msg(TalkMsg* talkMsgPtr)
  468. {
  469. TalkMsg talkMsg;
  470. memcpy( &talkMsg, talkMsgPtr, sizeof(TalkMsg) );
  471. talkMsg.from_nation_recno = talkMsg.to_nation_recno;
  472. talkMsg.to_nation_recno = talkMsg.from_nation_recno;
  473. return talk_res.is_talk_msg_exist(&talkMsg, 1); // 1-check talk_para1 & talk_para2
  474. }
  475. //------ End of static function has_sent_same_msg ------//
  476. //----- Begin of function Nation::consider_take_tech -----//
  477. //
  478. // talkMsg->talk_para1 - id. of the technology.
  479. // talkMsg->talk_para2 - level of the technology.
  480. //
  481. int Nation::consider_take_tech(TalkMsg* talkMsg)
  482. {
  483. int ourTechLevel = tech_res[talkMsg->talk_para1]->get_nation_tech_level(nation_recno);
  484. if( ourTechLevel >= talkMsg->talk_para2 )
  485. return 0;
  486. int relationChange = (talkMsg->talk_para2-ourTechLevel) * (15+pref_use_weapon/10);
  487. change_ai_relation_level( talkMsg->from_nation_recno, relationChange );
  488. return 1;
  489. }
  490. //------ End of function Nation::consider_take_tech ------//
  491. //----- Begin of function Nation::surplus_supply_rating -----//
  492. //
  493. // Return a rating from 0 to 100 indicating how much surplus
  494. // of supply this nation has in markets.
  495. //
  496. int Nation::surplus_supply_rating()
  497. {
  498. FirmMarket* firmMarket;
  499. int stockQty, totalStockQty=0, totalStockSlot=0;
  500. for( int i=ai_market_count-1; i>=0 ; i-- )
  501. {
  502. firmMarket = (FirmMarket*) firm_array[ ai_market_array[i] ];
  503. err_when( firmMarket->firm_id != FIRM_MARKET );
  504. MarketGoods* marketGoods = firmMarket->market_goods_array;
  505. for( int j=0 ; j<MAX_MARKET_GOODS ; j++, marketGoods++ )
  506. {
  507. if( marketGoods->raw_id || marketGoods->product_raw_id )
  508. {
  509. stockQty = (int) marketGoods->stock_qty;
  510. totalStockQty += stockQty;
  511. totalStockSlot++;
  512. }
  513. }
  514. }
  515. if( totalStockSlot==0 )
  516. return 0;
  517. int avgStockQty = totalStockQty / totalStockSlot;
  518. return 100 * avgStockQty / MAX_MARKET_STOCK;
  519. }
  520. //------ End of function Nation::surplus_supply_rating ------//
  521. //----- Begin of function Nation::consider_give_aid -----//
  522. //
  523. // talkMsg->talk_para1 - amount of the tribute.
  524. //
  525. int Nation::consider_give_aid(TalkMsg* talkMsg)
  526. {
  527. //-------- don't give tribute too frequently -------//
  528. NationRelation* nationRelation = get_relation(talkMsg->from_nation_recno);
  529. if( info.game_date <
  530. nationRelation->last_talk_reject_date_array[TALK_GIVE_AID-1]
  531. + 365 - pref_allying_tendency )
  532. {
  533. return 0;
  534. }
  535. //--------------------------------------------------//
  536. int importanceRating = (int) nationRelation->good_relation_duration_rating;
  537. if( nationRelation->status >= NATION_FRIENDLY &&
  538. ai_should_spend( importanceRating, talkMsg->talk_para1 ) ) // 0-importance is 0
  539. {
  540. if( info.game_date > nationRelation->last_change_status_date
  541. + 720 - pref_allying_tendency ) // we have allied with this nation for quite some while
  542. {
  543. nationRelation->last_talk_reject_date_array[TALK_GIVE_AID-1] = info.game_date;
  544. return 1;
  545. }
  546. }
  547. return 0;
  548. }
  549. //------ End of function Nation::consider_give_aid ------//
  550. //----- Begin of function Nation::consider_give_tribute -----//
  551. //
  552. // talkMsg->talk_para1 - amount of the tribute.
  553. //
  554. int Nation::consider_give_tribute(TalkMsg* talkMsg)
  555. {
  556. //-------- don't give tribute too frequently -------//
  557. NationRelation* nationRelation = get_relation(talkMsg->from_nation_recno);
  558. if( info.game_date <
  559. nationRelation->last_talk_reject_date_array[TALK_GIVE_TRIBUTE-1] + 365 - pref_allying_tendency )
  560. {
  561. return 0;
  562. }
  563. //---------------------------------------------//
  564. int relationStatus = get_relation_status(talkMsg->from_nation_recno);
  565. Nation* fromNation = nation_array[talkMsg->from_nation_recno];
  566. if( true_profit_365days() < 0 ) // don't give tribute if we are losing money
  567. return 0;
  568. int reserveYears = 1 + 3 * pref_cash_reserve / 100; // 1 to 4 years
  569. if( cash-talkMsg->talk_para1 < fixed_expense_365days() * reserveYears )
  570. return 0;
  571. int militaryDiff = fromNation->military_rank_rating() - military_rank_rating();
  572. if( militaryDiff > 10+pref_military_courage/2 )
  573. {
  574. nationRelation->last_talk_reject_date_array[TALK_GIVE_TRIBUTE-1] = info.game_date;
  575. return 1;
  576. }
  577. return 0;
  578. }
  579. //------ End of function Nation::consider_give_tribute ------//
  580. //----- Begin of function Nation::consider_give_tech -----//
  581. //
  582. // Consider giving the latest level of the technology to the nation.
  583. //
  584. // talkMsg->talk_para1 - id. of the technology.
  585. //
  586. int Nation::consider_give_tech(TalkMsg* talkMsg)
  587. {
  588. //-------- don't give tribute too frequently -------//
  589. NationRelation* nationRelation = get_relation(talkMsg->from_nation_recno);
  590. if( info.game_date <
  591. nationRelation->last_talk_reject_date_array[TALK_GIVE_TECH-1] + 365 - pref_allying_tendency )
  592. {
  593. return 0;
  594. }
  595. //----------------------------------------------------//
  596. int importanceRating = (int) nationRelation->good_relation_duration_rating;
  597. if( nationRelation->status == NATION_ALLIANCE &&
  598. importanceRating + pref_allying_tendency/10 > 30 )
  599. {
  600. nationRelation->last_talk_reject_date_array[TALK_GIVE_TECH-1] = info.game_date;
  601. return 1;
  602. }
  603. return 0;
  604. }
  605. //------ End of function Nation::consider_give_tech ------//
  606. //----- Begin of function Nation::consider_declare_war -----//
  607. //
  608. // Consider the request of declaring war on the target nation.
  609. //
  610. // talk_para1 - the recno nation to declare war with.
  611. //
  612. int Nation::consider_declare_war(TalkMsg* talkMsg)
  613. {
  614. //--- if it even won't consider trade embargo, there is no reason that it will consider declaring war ---//
  615. if( !consider_trade_embargo(talkMsg) )
  616. return 0;
  617. //---------------------------------------//
  618. int fromRelationRating = ai_overall_relation_rating(talkMsg->from_nation_recno);
  619. int againstRelationRating = ai_overall_relation_rating(talkMsg->talk_para1);
  620. Nation* againstNation = nation_array[talkMsg->talk_para1];
  621. NationRelation* fromRelation = get_relation(talkMsg->from_nation_recno);
  622. NationRelation* againstRelation = get_relation(talkMsg->talk_para1);
  623. //--- if we don't have a good enough relation with the requesting nation, turn down the request ---//
  624. if( fromRelation->good_relation_duration_rating < 10 )
  625. return 0;
  626. //--- if we are more friendly with the against nation than the requesting nation, turn down the request ---//
  627. if( againstRelation->good_relation_duration_rating >
  628. fromRelation->good_relation_duration_rating )
  629. {
  630. return 0;
  631. }
  632. //--- if the nation is having a financial difficulty, it won't agree ---//
  633. if( cash < 2000 * pref_cash_reserve / 100 )
  634. return 0;
  635. //--------------------------------------------//
  636. int acceptRating = 100 + againstNation->total_enemy_military() -
  637. military_rank_rating();
  638. //--- it won't declare war with a friendly or allied nation easily ---//
  639. if( againstRelation->status >= NATION_FRIENDLY ) // no need to handle NATION_ALLIANCE separately as ai_overall_relation_relation() has already taken it into account
  640. acceptRating += 100;
  641. return fromRelationRating - againstRelationRating > acceptRating;
  642. }
  643. //------ End of function Nation::consider_declare_war ------//
  644. //----- Begin of function Nation::consider_trade_embargo -----//
  645. //
  646. int Nation::consider_trade_embargo(TalkMsg* talkMsg)
  647. {
  648. int fromRelationRating = ai_overall_relation_rating(talkMsg->from_nation_recno);
  649. int againstRelationRating = ai_overall_relation_rating(talkMsg->talk_para1);
  650. NationRelation* fromRelation = get_relation(talkMsg->from_nation_recno);
  651. NationRelation* againstRelation = get_relation(talkMsg->talk_para1);
  652. //--- if we don't have a good enough relation with the requesting nation, turn down the request ---//
  653. if( fromRelation->good_relation_duration_rating < 5 )
  654. return 0;
  655. //--- if we are more friendly with the against nation than the requesting nation, turn down the request ---//
  656. if( againstRelation->good_relation_duration_rating >
  657. fromRelation->good_relation_duration_rating )
  658. {
  659. return 0;
  660. }
  661. //--- if we have a large trade with the against nation or have a larger trade with the against nation than the requesting nation ---//
  662. int fromTrade = trade_rating(talkMsg->from_nation_recno);
  663. int againstTrade = trade_rating(talkMsg->talk_para1);
  664. if( againstTrade > 40 ||
  665. ( againstTrade > 10 && againstTrade - fromTrade > 15 ) )
  666. {
  667. return 0;
  668. }
  669. //--- if the nation is having a financial difficulty, it won't agree ---//
  670. if( cash < 2000 * pref_cash_reserve / 100 )
  671. return 0;
  672. //--------------------------------------------//
  673. int acceptRating = 75;
  674. //--- it won't declare war with a friendly or allied nation easily ---//
  675. if( againstRelation->status >= NATION_FRIENDLY ) // no need to handle NATION_ALLIANCE separately as ai_overall_relation_relation() has already taken it into account
  676. acceptRating += 100;
  677. return fromRelationRating - againstRelationRating > acceptRating;
  678. }
  679. //------ End of function Nation::consider_trade_embargo ------//
  680. //----- Begin of function Nation::consider_military_aid -----//
  681. //
  682. int Nation::consider_military_aid(TalkMsg* talkMsg)
  683. {
  684. Nation* fromNation = nation_array[talkMsg->from_nation_recno];
  685. NationRelation* fromRelation = get_relation(talkMsg->from_nation_recno);
  686. //----- don't aid too frequently ------//
  687. if( info.game_date < fromRelation->last_military_aid_date + 200 - pref_allying_tendency )
  688. return 0;
  689. //------- only when the AI relation >= 60 --------//
  690. if( fromRelation->ai_relation_level < 60 )
  691. return 0;
  692. //--- if the requesting nation is not at war now ----//
  693. if( !fromNation->is_at_war() )
  694. return 0;
  695. //---- can't aid if we are at war ourselves -----//
  696. if( is_at_war() )
  697. return 0;
  698. //--- if the nation is having a financial difficulty, it won't agree ---//
  699. if( cash < 2000 * pref_cash_reserve / 100 )
  700. return 0;
  701. //----- can't aid if we are too weak ourselves ---//
  702. if( ai_general_count*10 + total_human_count < 100-pref_military_courage/2 )
  703. return 0;
  704. //----- see what units are attacking the nation -----//
  705. if( unit_array.is_deleted(fromNation->last_attacker_unit_recno) )
  706. return 0;
  707. Unit* unitPtr = unit_array[ fromNation->last_attacker_unit_recno ];
  708. if( unitPtr->nation_recno == nation_recno ) // if it's our own units
  709. return 0;
  710. if( unitPtr->nation_recno == 0 )
  711. return 0;
  712. if( !unitPtr->is_visible() )
  713. return 0;
  714. //------ only attack if it's a common enemy to us and our ally -----//
  715. if( get_relation(unitPtr->nation_recno)->status != NATION_HOSTILE )
  716. return 0;
  717. //------- calculate the combat level of the target units there ------//
  718. int hasWar;
  719. int targetCombatLevel = mobile_defense_combat_level( unitPtr->next_x_loc(), unitPtr->next_y_loc(),
  720. unitPtr->nation_recno, 0, hasWar );
  721. if( ai_attack_target(unitPtr->next_x_loc(), unitPtr->next_y_loc(), targetCombatLevel, 0, 1 ) ) //0-not defense mode, 1-just move to flag
  722. {
  723. fromRelation->last_military_aid_date = info.game_date;
  724. return 1;
  725. }
  726. return 0;
  727. }
  728. //------ End of function Nation::consider_military_aid ------//
  729. //----- Begin of function Nation::consider_accept_surrender_request -----//
  730. //
  731. // Consider accepting the cash offer and sell the throne to another kingdom.
  732. //
  733. // talkMsg->talk_para1 - the amount offered.
  734. //
  735. int Nation::consider_accept_surrender_request(TalkMsg* talkMsg)
  736. {
  737. Nation* nationPtr = nation_array[talkMsg->from_nation_recno];
  738. int offeredAmt = talkMsg->talk_para1 * 10; // *10 to restore its original value which has been divided by 10 to cope with <short> upper limit
  739. //---- don't surrender to the player if the player is already the most powerful nation ---//
  740. if( !nationPtr->is_ai() && config.ai_aggressiveness >= OPTION_HIGH )
  741. {
  742. if( nation_array.max_overall_nation_recno == nationPtr->nation_recno )
  743. return 0;
  744. }
  745. // If our economy is good, then it is harder to convince us
  746. // to surrender. But when we are running out of cash,
  747. // we ignore all normal thinking in the following block.
  748. if( !(cash < 100 && profit_365days() < 0) )
  749. {
  750. //----- never surrender to a weaker nation ------//
  751. if( nationPtr->overall_rank_rating() < overall_rank_rating() )
  752. return 0;
  753. //------ don't surrender if we are still strong -----//
  754. if( overall_rank_rating() > 30 + pref_peacefulness/4 ) // 30 to 55
  755. return 0;
  756. //---- don't surrender if our cash is more than the amount they offered ----//
  757. if( offeredAmt < cash * (75+pref_cash_reserve/2) / 100 ) // 75% to 125%
  758. return 0;
  759. //-- if there are only two nations left, don't surrender if we still have some power --//
  760. if( nation_array.nation_count == 2 )
  761. {
  762. if( overall_rank_rating() > 20 - 10 * pref_military_courage / 100 )
  763. return 0;
  764. }
  765. }
  766. //-------------------------------------//
  767. int surrenderToRating = ai_surrender_to_rating(talkMsg->from_nation_recno);
  768. surrenderToRating += 100 * offeredAmt / 13000;
  769. int acceptRating = overall_rank_rating()*13 + 100;
  770. //------ AI aggressiveness effects -------//
  771. switch( config.ai_aggressiveness )
  772. {
  773. case OPTION_HIGH:
  774. if( nationPtr->is_ai() ) // tend to accept AI kingdom offer easier
  775. acceptRating -= 75;
  776. else
  777. acceptRating += 75;
  778. break;
  779. case OPTION_VERY_HIGH:
  780. if( nationPtr->is_ai() ) // tend to accept AI kingdom offer easier
  781. acceptRating -= 150;
  782. else
  783. acceptRating += 150;
  784. break;
  785. }
  786. return surrenderToRating > acceptRating;
  787. }
  788. //------ End of function Nation::consider_accept_surrender_request ------//
  789. //----- Begin of function Nation::ai_overall_relation_rating -----//
  790. //
  791. // Return the overall relation rating of this nation with the
  792. // specific nation.
  793. //
  794. int Nation::ai_overall_relation_rating(int withNationRecno)
  795. {
  796. NationRelation* nationRelation = get_relation(withNationRecno);
  797. Nation* nationPtr = nation_array[withNationRecno];
  798. int overallRating = nationRelation->ai_relation_level +
  799. (int) nationRelation->good_relation_duration_rating +
  800. (int) nationPtr->reputation +
  801. nationPtr->military_rank_rating() +
  802. trade_rating(withNationRecno) +
  803. ai_trade_with_rating(withNationRecno)/2 +
  804. nationPtr->total_alliance_military();
  805. return overallRating;
  806. }
  807. //------ End of function Nation::ai_overall_relation_rating ------//