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_ECO.CPP
22 //Description: AI economy functions
23 
24 #include <ALL.h>
25 #include <OUNIT.h>
26 #include <OFIRMALL.h>
27 #include <ONATION.h>
28 
29 
30 //--------- Begin of function Nation::think_reduce_expense --------//
31 
think_reduce_expense()32 void Nation::think_reduce_expense()
33 {
34 	if( true_profit_365days() > 0 || cash > 5000 * pref_cash_reserve / 100 )
35 		return;
36 
37 	//-------- close down firms ---------//
38 
39 	int  curRating, bestRating=0;
40 	Firm *firmPtr, *bestFirm=NULL;
41 
42 	for( int i=firm_array.size() ; i>0 ; i-- )
43 	{
44 		if( firm_array.is_deleted(i) )
45 			continue;
46 
47 		firmPtr = firm_array[i];
48 
49 		if( firmPtr->nation_recno != nation_recno )
50 			continue;
51 
52 		if( firmPtr->firm_id == FIRM_WAR_FACTORY ||
53 			 firmPtr->firm_id == FIRM_RESEARCH )
54 		{
55 			curRating = 100-(int)firmPtr->productivity;
56 		}
57 		else
58 			continue;
59 
60 		if( curRating > bestRating )
61 		{
62 			bestRating = curRating;
63 			bestFirm = firmPtr;
64 		}
65 	}
66 
67 	if( bestFirm )
68 		bestFirm->ai_del_firm();
69 }
70 //---------- End of function Nation::think_reduce_expense --------//
71 
72 
73 //----- Begin of function Nation::ai_should_spend -----//
74 //
75 // This function returns whether this nation should make
76 // a new spending.
77 //
78 // <int>   importanceRating - how important is the spending to the
79 //								    	nation.
80 // [float] spendAmt			 - if this is not given, then it will
81 //										consider generally - whether the
82 //										nation should spend money generally.
83 //
ai_should_spend(int importanceRating,float spendAmt)84 int Nation::ai_should_spend(int importanceRating, float spendAmt)
85 {
86 	if( cash < spendAmt )
87 		return 0;
88 
89 	float fixedExpense = fixed_expense_365days();
90 	float stdCashLevel = MAX(fixedExpense,2000) * (150+pref_cash_reserve) / 100;
91 	float trueProfit = true_profit_365days();
92 
93 	//----- if we are losing money, don't spend on non-important things -----//
94 
95 	if( trueProfit < 0 )
96 	{
97 		if( 400 * (-trueProfit) / fixedExpense > importanceRating )
98 			return 0;
99 	}
100 
101 	//--------------------------------------//
102 
103 	float curCashLevel = 100 * (cash-spendAmt) / (stdCashLevel*2);
104 
105 	return importanceRating >= (100-curCashLevel);
106 }
107 //------ End of function Nation::ai_should_spend ------//
108 
109 
110 //----- Begin of function Nation::ai_should_spend_war -----//
111 //
112 // This function returns whether the nation should spend money
113 // or war.
114 //
115 // <int> enemyMilitaryRating - the military_rank_rating() of the enemy
116 // [int] considerCeaseFire	  - whether this function is called when considering ceasing fire
117 //										 (default:0)
118 //
ai_should_spend_war(int enemyMilitaryRating,int considerCeaseFire)119 int Nation::ai_should_spend_war(int enemyMilitaryRating, int considerCeaseFire)
120 {
121 	int importanceRating = 30 + pref_military_development/5;		// 30 to 50
122 
123 	importanceRating += military_rank_rating() - enemyMilitaryRating*2;
124 
125 	if( considerCeaseFire )    	// only when we are very powerful, we will start a battle. So won't cease fire too soon after declaring war
126 		importanceRating += 20;		// less eary to return 0, for cease fire
127 
128 	return ai_should_spend(importanceRating);
129 }
130 //------ End of function Nation::ai_should_spend_war ------//
131 
132