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    : OF_WAR2.CPP
22 //Description : Firm War Factory - AI functions
23 
24 #include <ONATION.h>
25 #include <OINFO.h>
26 #include <OTOWN.h>
27 #include <OUNIT.h>
28 #include <OTECHRES.h>
29 #include <OF_WAR.h>
30 
31 //--------- Begin of function FirmWar::process_ai ---------//
32 
process_ai()33 void FirmWar::process_ai()
34 {
35 	//---- think about which technology to research ----//
36 
37 	if( !build_unit_id )
38 		think_new_production();
39 
40 	//------- recruit workers ---------//
41 
42 	if( info.game_date%15==firm_recno%15 )
43 	{
44 		if( worker_count < MAX_WORKER )
45 			ai_recruit_worker();
46 	}
47 
48 	//----- think about closing down this firm -----//
49 
50 	if( info.game_date%30==firm_recno%30 )
51 	{
52 		if( think_del() )
53 			return;
54 	}
55 }
56 //----------- End of function FirmWar::process_ai -----------//
57 
58 
59 //------- Begin of function FirmWar::think_del -----------//
60 //
61 // Think about deleting this firm.
62 //
think_del()63 int FirmWar::think_del()
64 {
65 	if( worker_count > 0 )
66 		return 0;
67 
68 	//-- check whether the firm is linked to any towns or not --//
69 
70 	for( int i=0 ; i<linked_town_count ; i++ )
71 	{
72 		if( linked_town_enable_array[i] == LINK_EE )
73 			return 0;
74 	}
75 
76 	//------------------------------------------------//
77 
78 	ai_del_firm();
79 
80 	return 1;
81 }
82 //--------- End of function FirmWar::think_del -----------//
83 
84 
85 //----- Begin of function FirmWar::think_new_production ------//
86 //
87 // Think about which weapon to produce.
88 //
think_new_production()89 void FirmWar::think_new_production()
90 {
91 	//----- first see if we have enough money to build & support the weapon ----//
92 
93 	if( !should_build_new_weapon() )
94 		return;
95 
96 	//---- calculate the average instance count of all available weapons ---//
97 
98 	int 		 weaponTypeCount=0, totalWeaponCount=0;
99 	UnitInfo* unitInfo;
100 
101 	int unitId;
102 	for( unitId=1; unitId<=MAX_UNIT_TYPE ; unitId++ )
103 	{
104 		unitInfo = unit_res[unitId];
105 
106 		if( unitInfo->unit_class != UNIT_CLASS_WEAPON ||
107 			 unitInfo->get_nation_tech_level(nation_recno) == 0 )
108 		{
109 			continue;
110 		}
111 
112 		if( unitId == UNIT_EXPLOSIVE_CART )		// AI doesn't use Porcupine
113 			continue;
114 
115 		weaponTypeCount++;
116 		totalWeaponCount += unitInfo->nation_unit_count_array[nation_recno-1];
117 	}
118 
119 	if( weaponTypeCount==0 )		// none of weapon technologies is available
120 		return;
121 
122 	int averageWeaponCount = totalWeaponCount/weaponTypeCount;
123 
124 	//----- think about which is best to build now ------//
125 
126 	int curRating, bestRating=0, bestUnitId=0;
127 
128 	for( unitId=1; unitId<=MAX_UNIT_TYPE ; unitId++ )
129 	{
130 		unitInfo = unit_res[unitId];
131 
132 		if( unitInfo->unit_class != UNIT_CLASS_WEAPON )
133 			continue;
134 
135 		int techLevel = unitInfo->get_nation_tech_level(nation_recno);
136 
137 		if( techLevel==0 )
138 			continue;
139 
140 		if( unitId == UNIT_EXPLOSIVE_CART )		//**BUGHERE, don't produce it yet, it needs a different usage than the others.
141 			continue;
142 
143 		int unitCount = unitInfo->nation_unit_count_array[nation_recno-1];
144 
145 		curRating = averageWeaponCount-unitCount + techLevel*3;
146 
147 		if( curRating > bestRating )
148 		{
149 			bestRating = curRating;
150 			bestUnitId = unitId;
151 		}
152 	}
153 
154 	//------------------------------------//
155 
156 	if( bestUnitId )
157 		add_queue( bestUnitId );
158 }
159 //------ End of function FirmWar::think_new_production -------//
160 
161 
162 //----- Begin of function FirmWar::should_build_new_weapon ------//
163 //
should_build_new_weapon()164 int FirmWar::should_build_new_weapon()
165 {
166 	//----- first see if we have enough money to build & support the weapon ----//
167 
168 	Nation* nationPtr = nation_array[nation_recno];
169 
170 	if( nationPtr->true_profit_365days() < 0 )		// don't build new weapons if we are currently losing money
171 		return 0;
172 
173 	if( nationPtr->expense_365days(EXPENSE_WEAPON) >
174 		 nationPtr->income_365days() * 30 + nationPtr->pref_use_weapon/2 )		// if weapon expenses are larger than 30% to 80% of the total income, don't build new weapons
175 	{
176 		return 0;
177 	}
178 
179 	//----- see if there is any space on existing camps -----//
180 
181 	Firm* firmPtr;
182 
183 	for( int i=0 ; i<nationPtr->ai_camp_count ; i++ )
184 	{
185 		firmPtr = firm_array[ nationPtr->ai_camp_array[i] ];
186 
187 		if( firmPtr->region_id != region_id )
188 			continue;
189 
190 		if( firmPtr->worker_count < MAX_WORKER )		// there is space in this firm
191 			return 1;
192 	}
193 
194 	return 0;
195 }
196 //------ End of function FirmWar::should_build_new_weapon -------//
197