OAI_BUIL.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. //Filename : OAI_ACT.CPP
  21. //Description: AI - building firms
  22. #include <ALL.h>
  23. #include <OUNIT.h>
  24. #include <OF_CAMP.h>
  25. #include <OREGIONS.h>
  26. #include <OCONFIG.h>
  27. #include <OSITE.h>
  28. #include <ONATION.h>
  29. //--------- Begin of function Nation::think_build_firm --------//
  30. void Nation::think_build_firm()
  31. {
  32. if( !ai_should_build_mine() )
  33. return;
  34. //----- think building mine --------//
  35. if( think_build_mine() )
  36. return;
  37. //---- if there is a mine action currently -----//
  38. think_destroy_raw_site_guard();
  39. }
  40. //---------- End of function Nation::think_build_firm --------//
  41. //--------- Begin of function Nation::think_build_mine --------//
  42. //
  43. int Nation::think_build_mine()
  44. {
  45. //------- queue to build the new mine -------//
  46. short xLoc, yLoc, refXLoc, refYLoc;
  47. int rawId = seek_mine(xLoc, yLoc, refXLoc, refYLoc); //reference location is the raw material location
  48. if( !rawId )
  49. return 0;
  50. //--- if we have a mine producing that raw type already ---//
  51. if( raw_count_array[rawId-1] > 0 )
  52. {
  53. if( !ai_should_spend(20) ) // then it's not important to build it
  54. return 0;
  55. }
  56. //-------------------------------------------//
  57. // If the map is set to unexplored, wait for a
  58. // reasonable amount of time before moving out
  59. // to build the mine.
  60. //-------------------------------------------//
  61. if( !config.explore_whole_map )
  62. {
  63. int i;
  64. for( i=0 ; i<ai_town_count ; i++ )
  65. {
  66. Town* townPtr = town_array[ ai_town_array[i] ];
  67. int rawDistance = m.points_distance(xLoc, yLoc, townPtr->center_x, townPtr->center_y);
  68. if( info.game_date-info.game_start_date >
  69. rawDistance * (5-config.ai_aggressiveness) / 5 ) // 3 to 5 / 5
  70. {
  71. break;
  72. }
  73. }
  74. if( i==ai_town_count )
  75. return 0;
  76. }
  77. return add_action(xLoc, yLoc, refXLoc, refYLoc, ACTION_AI_BUILD_FIRM, FIRM_MINE);
  78. }
  79. //---------- End of function Nation::think_build_mine --------//
  80. //--------- Begin of function Nation::ai_should_build_mine --------//
  81. //
  82. int Nation::ai_should_build_mine()
  83. {
  84. //---- only build mines when it has enough population ----//
  85. if( total_jobless_population < (100-pref_economic_development)/2 )
  86. return 0;
  87. if( total_jobless_population < 16 ) // only build mine when you have enough population to support the economic chain: mine + factory + camp
  88. return 0;
  89. if( site_array.untapped_raw_count==0 )
  90. return 0;
  91. if( !can_ai_build(FIRM_MINE) )
  92. return 0;
  93. //--- don't build additional mines unless we have enough population and demand to support it ----//
  94. if( ai_mine_count == 1 )
  95. {
  96. if( true_profit_365days() < 0 && total_population < 40+pref_economic_development/5 )
  97. return 0;
  98. }
  99. //-- if the nation is already in the process of building a new one --//
  100. if( is_action_exist( ACTION_AI_BUILD_FIRM, FIRM_MINE ) )
  101. return 0;
  102. //--- if the population is low, make sure existing mines are in full production before building a new one ---//
  103. if( total_jobless_population < 30 )
  104. {
  105. Firm* firmPtr;
  106. for( int i=0 ; i<ai_mine_count ; i++ )
  107. {
  108. firmPtr = firm_array[ ai_mine_array[i] ];
  109. if( firmPtr->worker_count < MAX_WORKER )
  110. return 0;
  111. if( firmPtr->linked_firm_count==0 && !firmPtr->no_neighbor_space ) // if the firm does not have any linked firms, that means the firm is still not in full operation
  112. return 0;
  113. }
  114. }
  115. return 1;
  116. }
  117. //---------- End of function Nation::ai_should_build_mine --------//
  118. //--------- Begin of function Nation::ai_attack_unit_in_area --------//
  119. //
  120. // AI attacks all units that are not friendly to us within the given area.
  121. //
  122. // <int> xLoc1, yLoc1, xLoc2, yLoc2 - coordination of the area.
  123. //
  124. void Nation::ai_attack_unit_in_area(int xLoc1, int yLoc1, int xLoc2, int yLoc2)
  125. {
  126. int enemyXLoc, enemyYLoc, enemyCombatLevel=0;
  127. int enemyStatus = NATION_FRIENDLY;
  128. Location* locPtr;
  129. Unit* unitPtr;
  130. //--------------------------------------------------//
  131. for( int yLoc=yLoc1 ; yLoc<=yLoc2 ; yLoc++ )
  132. {
  133. for( int xLoc=xLoc1 ; xLoc<=xLoc2 ; xLoc++ )
  134. {
  135. locPtr = world.get_loc(xLoc, yLoc);
  136. if( !locPtr->has_unit(UNIT_LAND) )
  137. continue;
  138. unitPtr = unit_array[ locPtr->unit_recno(UNIT_LAND) ];
  139. //--- if there is an idle unit on the mine building site ---//
  140. if( unitPtr->cur_action != SPRITE_IDLE || unitPtr->nation_recno==0 )
  141. continue;
  142. //----- if this is our spy cloaked in another nation, reveal its true identity -----//
  143. if( unitPtr->nation_recno != nation_recno &&
  144. unitPtr->true_nation_recno() == nation_recno )
  145. {
  146. unitPtr->spy_change_nation(nation_recno, COMMAND_AI);
  147. }
  148. //--- if this is our own unit, order him to stay out of the building site ---//
  149. if( unitPtr->nation_recno == nation_recno )
  150. {
  151. unitPtr->think_normal_human_action(); // send the unit to a firm or a town
  152. }
  153. else //--- if it is an enemy unit, attack it ------//
  154. {
  155. int nationStatus = get_relation_status(unitPtr->nation_recno);
  156. if( nationStatus < enemyStatus ) // if the status is worse than the current target
  157. {
  158. enemyXLoc = xLoc;
  159. enemyYLoc = yLoc;
  160. enemyStatus = nationStatus;
  161. enemyCombatLevel += (int) unitPtr->unit_power();
  162. }
  163. }
  164. }
  165. }
  166. //--- if there are enemies on our firm building site, attack them ---//
  167. if( enemyCombatLevel )
  168. {
  169. ai_attack_target( enemyXLoc, enemyYLoc, enemyCombatLevel );
  170. }
  171. }
  172. //---------- End of function Nation::ai_attack_unit_in_area --------//
  173. //--------- Begin of function Nation::think_destroy_raw_site_guard --------//
  174. //
  175. int Nation::think_destroy_raw_site_guard()
  176. {
  177. Site* sitePtr;
  178. Location* locPtr;
  179. Unit* unitPtr;
  180. for( int i=site_array.size() ; i>0 ; i-- )
  181. {
  182. if( site_array.is_deleted(i) )
  183. continue;
  184. sitePtr = site_array[i];
  185. //--- if there is already a mine built on this raw site ---//
  186. if( sitePtr->has_mine )
  187. continue;
  188. //----- if there is a unit standing on this site -----//
  189. locPtr = world.get_loc( sitePtr->map_x_loc, sitePtr->map_y_loc );
  190. if( !locPtr->has_unit(UNIT_LAND) )
  191. continue;
  192. unitPtr = unit_array[ locPtr->unit_recno(UNIT_LAND) ];
  193. if( unitPtr->cur_action != SPRITE_IDLE ) // only attack if this unit is idle
  194. continue;
  195. if( unitPtr->nation_recno == nation_recno ) // don't attack our own units
  196. continue;
  197. //------ check if we have a presence in this region ----//
  198. // ####### patch begin Gilbert 16/3 ########//
  199. //if( region_array.get_region_stat(sitePtr->region_id)->base_town_nation_count_array[nation_recno-1] == 0 )
  200. // continue;
  201. if( base_town_count_in_region(sitePtr->region_id) == 0 )
  202. continue;
  203. // ####### patch end Gilbert 16/3 ########//
  204. //------ check the relationship with this unit ------//
  205. //
  206. // If we are friendly with this nation, don't attack it.
  207. //
  208. //---------------------------------------------------//
  209. if( get_relation_status(unitPtr->nation_recno) >= NATION_FRIENDLY )
  210. continue;
  211. //--------- attack the enemy unit ---------//
  212. int hasWar;
  213. int enemyCombatLevel = mobile_defense_combat_level( sitePtr->map_x_loc,
  214. sitePtr->map_y_loc, unitPtr->nation_recno, 1, hasWar );
  215. if( enemyCombatLevel == - 1 ) // a war is going on here, don't attack this target
  216. continue;
  217. if( ai_attack_target(sitePtr->map_x_loc, sitePtr->map_y_loc, enemyCombatLevel, 0, 0, 0, 0, 1) ) // 1-use all camps
  218. return 1;
  219. }
  220. return 0;
  221. }
  222. //---------- End of function Nation::think_destroy_raw_site_guard --------//
  223. //--------- Begin of function Nation::ai_supported_inn_count --------//
  224. //
  225. // Return the number of inns this nation can support.
  226. //
  227. int Nation::ai_supported_inn_count()
  228. {
  229. float fixedExpense = fixed_expense_365days();
  230. int innCount = int( cash / 5000 * (100+pref_hire_unit) / 100 );
  231. innCount = min(3, innCount); // maximum 3 inns, minimum 1 inn.
  232. return max(1, innCount);
  233. }
  234. //---------- End of function Nation::ai_supported_inn_count --------//
  235. //--------- Begin of function Nation::ai_has_should_close_camp --------//
  236. //
  237. // Return whether the this nation has any camps that should be closed.
  238. //
  239. // <int> regionId - only camps in this region are counted.
  240. //
  241. int Nation::ai_has_should_close_camp(int regionId)
  242. {
  243. //--- if this nation has some firms going to be closed ---//
  244. if( firm_should_close_array[FIRM_CAMP-1] > 0 )
  245. {
  246. //--- check if any of them are in the same region as the current town ---//
  247. for( int i=ai_camp_count-1 ; i>=0 ; i-- )
  248. {
  249. FirmCamp* firmCamp = (FirmCamp*) firm_array[ ai_camp_array[i] ];
  250. if( firmCamp->should_close_flag && firmCamp->region_id == regionId )
  251. {
  252. return 1;
  253. }
  254. }
  255. }
  256. return 0;
  257. }
  258. //---------- End of function Nation::ai_has_should_close_camp --------//