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_TOWN.CPP
22 //Description: AI - processing town
23
24 #include <ALL.h>
25 #include <OUNIT.h>
26 #include <OF_INN.h>
27 #include <OTOWN.h>
28 #include <OREGIONS.h>
29 #include <ONATION.h>
30
31
32 //--------- Begin of function Nation::think_town --------//
33 //
think_town()34 void Nation::think_town()
35 {
36 optimize_town_race();
37 }
38 //---------- End of function Nation::think_town --------//
39
40
41 //--------- Begin of function Nation::optimize_town_race --------//
42 //
43 // Optimize the distribution of different races in different towns.
44 //
optimize_town_race()45 void Nation::optimize_town_race()
46 {
47 RegionStat* regionStat = region_array.region_stat_array;
48
49 for( int i=0 ; i<region_array.region_stat_count ; i++, regionStat++ )
50 {
51 if( regionStat->town_nation_count_array[nation_recno-1] > 0 )
52 optimize_town_race_region( regionStat->region_id );
53 }
54 }
55 //---------- End of function Nation::optimize_town_race --------//
56
57
58 //--------- Begin of function Nation::optimize_town_race_region --------//
59 //
60 // Optimize the distribution of different races in different towns in
61 // a single region.
62 //
optimize_town_race_region(int regionId)63 void Nation::optimize_town_race_region(int regionId)
64 {
65 //---- reckon the minority jobless pop of each race ----//
66
67 int racePopArray[MAX_RACE];
68
69 memset( racePopArray, 0, sizeof(racePopArray) );
70
71 int i, j, majorityRace;
72 Town* townPtr;
73
74 for( i=0 ; i<ai_town_count ; i++ )
75 {
76 townPtr = town_array[ ai_town_array[i] ];
77
78 if( townPtr->region_id != regionId )
79 continue;
80
81 majorityRace = townPtr->majority_race();
82
83 for( j=0 ; j<MAX_RACE ; j++ )
84 {
85 if( j+1 != majorityRace )
86 racePopArray[j] += townPtr->jobless_race_pop_array[j];
87 }
88 }
89
90 //--- locate for towns with minority being majority and those minority race can move to ---//
91
92 Town* destTown;
93
94 for( int raceId=1 ; raceId<=MAX_RACE ; raceId++ )
95 {
96 if( racePopArray[raceId-1] == 0 ) // we don't have any minority of this race
97 continue;
98
99 destTown = NULL;
100
101 for( i=0 ; i<ai_town_count ; i++ )
102 {
103 townPtr = town_array[ ai_town_array[i] ];
104
105 if( townPtr->region_id != regionId )
106 continue;
107
108 if( !townPtr->is_base_town )
109 continue;
110
111 if( townPtr->majority_race() == raceId &&
112 townPtr->population < MAX_TOWN_POPULATION )
113 {
114 destTown = townPtr;
115 break;
116 }
117 }
118
119 if( !destTown )
120 continue;
121
122 //---- if there is a suitable town for minority to move to ---//
123
124 for( i=0 ; i<ai_town_count ; i++ )
125 {
126 townPtr = town_array[ ai_town_array[i] ];
127
128 if( townPtr->region_id != regionId )
129 continue;
130
131 //---- move minority units from towns -----//
132
133 int joblessCount = townPtr->jobless_race_pop_array[raceId-1];
134
135 if( joblessCount > 0 &&
136 townPtr->majority_race() != raceId )
137 {
138 int migrateCount = MIN(8, joblessCount); // migrate a maximum of 8 units at a time
139
140 add_action( destTown->loc_x1, destTown->loc_y1,
141 townPtr->loc_x1, townPtr->loc_y1, ACTION_AI_SETTLE_TO_OTHER_TOWN, 0, migrateCount);
142 }
143 }
144 }
145 }
146 //---------- End of function Nation::optimize_town_race_region --------//
147