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_DEFE.CPP
22 //Description: AI on defense
23 
24 #include <ALL.h>
25 #include <OTALKRES.h>
26 #include <ONATION.h>
27 
28 //----- Begin of function Nation::ai_defend -----//
29 //
30 // <int> attackerUnitRecno - unit recno of the attacker.
31 //
ai_defend(int attackerUnitRecno)32 int Nation::ai_defend(int attackerUnitRecno)
33 {
34 	//--- don't call for defense too frequently, only call once 7 days (since this function will be called every time our king/firm/town is attacked, so this filtering is necessary ---//
35 
36 	if( info.game_date < ai_last_defend_action_date+7 )
37 		return 0;
38 
39 	ai_last_defend_action_date = info.game_date;
40 
41 	//---------- analyse the situation first -----------//
42 
43 	Unit* attackerUnit = unit_array[attackerUnitRecno];
44 
45 	err_when( attackerUnit->nation_recno == nation_recno );
46 
47 	int attackerXLoc = attackerUnit->next_x_loc();
48 	int attackerYLoc = attackerUnit->next_y_loc();
49 
50 	int hasWar;
51 
52 	int enemyCombatLevel = mobile_defense_combat_level( attackerXLoc, attackerYLoc,
53 		attackerUnit->nation_recno, 0, hasWar );		// 0-don't return immediately even if there is war around this town
54 
55 	//-- the value returned is enemy strength minus your own strength, so if it's positive, it means that your enemy is stronger than you, otherwise you're stronger than your enemy --//
56 
57 	int attackCombatLevel = ai_attack_target(attackerXLoc, attackerYLoc, enemyCombatLevel, 1);		// 1-defense mode
58 
59 	//------ request military aid from allies ----//
60 
61 	if( attackCombatLevel < enemyCombatLevel && attackerUnit->nation_recno )
62 	{
63 		ai_request_military_aid();
64 	}
65 
66 	return 1;
67 }
68 //----- End of function Nation::ai_defend -----//
69 
70 
71 //----- Begin of function Nation::ai_request_military_aid -----//
72 //
73 // Request allied nations to provide immediate military aid.
74 //
ai_request_military_aid()75 int Nation::ai_request_military_aid()
76 {
77 	for( int i=nation_array.size() ; i>0 ; i-- )
78 	{
79 		if( nation_array.is_deleted(i) )
80 			continue;
81 
82 		if( get_relation(i)->status != NATION_ALLIANCE )
83 			continue;
84 
85 		if( should_diplomacy_retry(TALK_REQUEST_MILITARY_AID, i) )
86 		{
87 			talk_res.ai_send_talk_msg(i, nation_recno, TALK_REQUEST_MILITARY_AID);
88 			return 1;
89 		}
90 	}
91 
92 	return 0;
93 }
94 //----- End of function Nation::ai_request_military_aid -----//
95