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_MONS.CPP
22 //Description: AI functions for dealing with the monsters.
23 
24 #include <OF_MONS.h>
25 #include <ONATION.h>
26 #include <OCONFIG.h>
27 #include <OMONSRES.h>
28 
29 //--------- Begin of function Nation::think_attack_monster --------//
30 
think_attack_monster()31 int Nation::think_attack_monster()
32 {
33 	if( config.monster_type == OPTION_MONSTER_NONE )		// no monsters in the game
34 		return 0;
35 
36 	//--- if the AI has run out of money and is currently cheating, it will have a urgent need to attack monsters to get money ---//
37 
38 	int useAllCamp = income_365days(INCOME_CHEAT) > 0;
39 
40 	if( !useAllCamp )		// use all camps to attack the monster
41 	{
42 		if( !is_at_war() )
43 		{
44 			if( cash < 500 && military_rank_rating() >= 75-pref_attack_monster/4 )		//  50 to 75
45 				useAllCamp = 1 ;
46 		}
47 	}
48 
49 	if( !useAllCamp )
50 	{
51 		if( military_rank_rating() < 50-pref_attack_monster/4 )		// don't attack if the military strength is too low, 25 to 50
52 			return 0;
53 	}
54 
55 	//------- select a monster target ---------//
56 
57 	int targetCombatLevel;
58 
59 	int targetFirmRecno = think_monster_target(targetCombatLevel);
60 
61 	if( !targetFirmRecno )
62 		return 0;
63 
64 	targetCombatLevel = targetCombatLevel * 150 / 100;		// X 150%
65 
66 	//--- we need to use veteran soldiers to attack powerful monsters ---//
67 
68 	FirmMonster* targetFirm = (FirmMonster*) firm_array[targetFirmRecno];
69 
70 	int monsterLevel = monster_res[targetFirm->monster_id]->level;
71 
72 	int attackerMinCombatLevel = 0;
73 
74 	if( targetCombatLevel > 100 )			// if the nation's cash runs very low, it will attack anyway
75 		attackerMinCombatLevel = 20 + monsterLevel*3;
76 
77 	//--- call ai_attack_target() to attack the target town ---//
78 
79 	return ai_attack_target(targetFirm->loc_x1, targetFirm->loc_y1, targetCombatLevel, 0, 0, attackerMinCombatLevel, 0, useAllCamp );
80 }
81 //---------- End of function Nation::think_attack_monster --------//
82 
83 
84 //--------- Begin of function Nation::think_monster_target --------//
85 //
86 // Think about the best monster target in the given region.
87 //
88 // <int&> targetCombatLevel - var for returning the total combat level of the target firm.
89 //
think_monster_target(int & targetCombatLevel)90 int Nation::think_monster_target(int& targetCombatLevel)
91 {
92 	if( !largest_town_recno )
93 		return 0;
94 
95 	Town* largestTown = town_array[largest_town_recno];
96 	Firm* firmPtr;
97 	int	combatLevel;
98 	int	curRating, bestRating= -10000, bestFirmRecno=0, hasWar;
99 
100 	for( int firmRecno=firm_array.size() ; firmRecno>0 ; firmRecno-- )
101 	{
102 		if( firm_array.is_deleted(firmRecno) )
103 			continue;
104 
105 		firmPtr = firm_array[firmRecno];
106 
107 		if( firmPtr->firm_id != FIRM_MONSTER ||
108 			 firmPtr->region_id != largestTown->region_id )
109 		{
110 			continue;
111 		}
112 
113 		//----- take into account of the mobile units around this town -----//
114 
115 		int mobileCombatLevel = mobile_defense_combat_level(firmPtr->center_x, firmPtr->center_y, firmPtr->nation_recno, 1, hasWar);
116 
117 		if( mobileCombatLevel == -1 )		// do not attack this town because a battle is already going on
118 			continue;
119 
120 		curRating = 3 * misc.points_distance( largestTown->center_x, largestTown->center_y,
121 													  firmPtr->center_x, firmPtr->center_y );
122 
123 		combatLevel = mobileCombatLevel +
124 						  ((FirmMonster*)firmPtr)->total_combat_level();
125 
126 		curRating -= combatLevel;
127 
128 		//---------------------------------//
129 
130 		if( curRating > bestRating )
131 		{
132 			targetCombatLevel = combatLevel;
133 			bestRating        = curRating;
134 			bestFirmRecno     = firmRecno;
135 		}
136 	}
137 
138 	return bestFirmRecno;
139 }
140 //---------- End of function Nation::think_monster_target --------//
141 
142