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