OF_HARB2.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 : OF_HARB2.CPP
  21. //Description : Firm Harbor - AI functions
  22. #include <OINFO.h>
  23. #include <OU_MARI.h>
  24. #include <ONATION.h>
  25. #include <OF_HARB.h>
  26. //------- Begin of function FirmHarbor::process_ai -----------//
  27. //
  28. void FirmHarbor::process_ai()
  29. {
  30. if( info.game_date%30 == firm_recno%30 )
  31. think_build_ship();
  32. /*
  33. if( info.game_date%90 == firm_recno%90 )
  34. think_build_firm();
  35. if( info.game_date%60 == firm_recno%60 )
  36. think_trade();
  37. */
  38. }
  39. //--------- End of function FirmHarbor::process_ai -----------//
  40. //------- Begin of function FirmHarbor::think_build_firm -----------//
  41. //
  42. void FirmHarbor::think_build_firm()
  43. {
  44. Nation* ownNation = nation_array[nation_recno];
  45. if( ownNation->cash < 2000 ) // don't build if the cash is too low
  46. return;
  47. if( ownNation->true_profit_365days() < (50-ownNation->pref_use_marine)*20 ) // -1000 to +1000
  48. return;
  49. //----- think about building markets ------//
  50. if( ownNation->pref_trading_tendency >= 60 )
  51. {
  52. if( ai_build_firm(FIRM_MARKET) )
  53. return;
  54. }
  55. //----- think about building camps ------//
  56. if( ownNation->pref_military_development/2 +
  57. (linked_firm_count + ownNation->ai_ship_count + ship_count) * 10 +
  58. ownNation->total_jobless_population*2 > 150 )
  59. {
  60. ai_build_firm(FIRM_CAMP);
  61. }
  62. }
  63. //--------- End of function FirmHarbor::think_build_firm -----------//
  64. //------- Begin of function FirmHarbor::ai_build_firm -----------//
  65. //
  66. int FirmHarbor::ai_build_firm(int firmId)
  67. {
  68. if( no_neighbor_space ) // if there is no space in the neighbor area for building a new firm.
  69. return 0;
  70. Nation* ownNation = nation_array[nation_recno];
  71. //--- check whether the AI can build a new firm next this firm ---//
  72. if( !ownNation->can_ai_build(firmId) )
  73. return 0;
  74. //-- only build one market place next to this mine, check if there is any existing one --//
  75. //### begin alex 24/9 ###//
  76. /*FirmMarket* firmPtr;
  77. for(int i=0; i<linked_firm_count; i++)
  78. {
  79. err_when(!linked_firm_array[i] || firm_array.is_deleted(linked_firm_array[i]));
  80. firmPtr = (FirmMarket*) firm_array[linked_firm_array[i]];
  81. if( firmPtr->firm_id!=firmId )
  82. continue;
  83. //------ if this market is our own one ------//
  84. if( firmPtr->nation_recno == nation_recno )
  85. return 0;
  86. }*/
  87. Firm* firmPtr;
  88. for(int i=0; i<linked_firm_count; i++)
  89. {
  90. err_when(!linked_firm_array[i] || firm_array.is_deleted(linked_firm_array[i]));
  91. firmPtr = firm_array[linked_firm_array[i]];
  92. if( firmPtr->firm_id!=firmId )
  93. continue;
  94. //------ if this market is our own one ------//
  95. if( firmPtr->nation_recno == nation_recno )
  96. return 0;
  97. }
  98. //#### end alex 24/9 ####//
  99. //------ queue building a new market -------//
  100. short buildXLoc, buildYLoc;
  101. if( !ownNation->find_best_firm_loc(firmId, loc_x1, loc_y1, buildXLoc, buildYLoc) )
  102. {
  103. no_neighbor_space = 1;
  104. return 0;
  105. }
  106. ownNation->add_action(buildXLoc, buildYLoc, loc_x1, loc_y1, ACTION_AI_BUILD_FIRM, firmId);
  107. return 1;
  108. }
  109. //--------- End of function FirmHarbor::ai_build_firm -----------//
  110. //------- Begin of function FirmHarbor::think_trade -----------//
  111. //
  112. int FirmHarbor::think_trade()
  113. {
  114. //---- see if we have any free ship available ----//
  115. Nation* ownNation = nation_array[nation_recno];
  116. UnitMarine* unitMarine = ai_get_free_trade_ship();
  117. if( !unitMarine )
  118. return 0;
  119. //--- if this harbor is not linked to any trade firms, return now ---//
  120. if( total_linked_trade_firm() == 0 )
  121. return 0;
  122. //----- scan for another harbor to trade with this harbor ----//
  123. Firm* firmPtr;
  124. FirmHarbor* firmHarbor;
  125. int i;
  126. for( i=firm_array.size() ; i>0 ; i-- )
  127. {
  128. if( firm_array.is_deleted(i) )
  129. continue;
  130. firmPtr = firm_array[i];
  131. //### begin alex 24/9 ###//
  132. if(firmPtr->firm_id!=FIRM_HARBOR)
  133. continue;
  134. //#### end alex 24/9 ####//
  135. firmHarbor = (FirmHarbor*) firmPtr;
  136. if( firmHarbor->total_linked_trade_firm() == 0 )
  137. continue;
  138. if( !ownNation->has_trade_ship(firm_recno, i) ) // if there has been any ships trading between these two harbors yet
  139. break;
  140. }
  141. if( i==0 )
  142. return 0;
  143. //------ try to set up a sea trade now ------//
  144. unitMarine->set_stop( 1, loc_x1, loc_y1, COMMAND_AI);
  145. unitMarine->set_stop( 2, firmHarbor->loc_x1, firmHarbor->loc_y1, COMMAND_AI);
  146. unitMarine->set_stop_pick_up(1, AUTO_PICK_UP, COMMAND_AI);
  147. unitMarine->set_stop_pick_up(2, AUTO_PICK_UP, COMMAND_AI);
  148. return 1;
  149. }
  150. //--------- End of function FirmHarbor::think_trade -----------//
  151. //------ Begin of function FirmHarbor::ai_get_free_trade_ship ------//
  152. //
  153. UnitMarine* FirmHarbor::ai_get_free_trade_ship()
  154. {
  155. Nation* ownNation = nation_array[nation_recno];
  156. UnitMarine* unitMarine;
  157. for( int i=ownNation->ai_ship_count-1 ; i>=0 ; i-- )
  158. {
  159. unitMarine = (UnitMarine*) unit_array[ ownNation->ai_ship_array[i] ];
  160. //--- if this is a goods carrying ship and it doesn't have a defined trade route ---//
  161. if( unitMarine->stop_defined_num < 2 &&
  162. unit_res[unitMarine->unit_id]->carry_goods_capacity > 0 )
  163. {
  164. return unitMarine;
  165. }
  166. }
  167. return NULL;
  168. }
  169. //-------- End of function FirmHarbor::ai_get_free_trade_ship ------//
  170. //------- Begin of function FirmHarbor::think_build_ship -----------//
  171. //
  172. void FirmHarbor::think_build_ship()
  173. {
  174. if( build_unit_id ) // if it's currently building a ship
  175. return;
  176. if( !can_build_ship() ) // full, cannot build anymore
  177. return;
  178. Nation* ownNation = nation_array[nation_recno];
  179. if( !ownNation->ai_should_spend( 50+ownNation->pref_use_marine/4 ) )
  180. return;
  181. //---------------------------------------------//
  182. //
  183. // For Transport, in most cases, an AI will just
  184. // need one to two.
  185. //
  186. // For Caravel and Galleon, the AI will build as many
  187. // as the harbor can hold if any of the human players
  188. // has more ships than the AI has.
  189. //
  190. //---------------------------------------------//
  191. int buildId=0, rc=0;
  192. int enemyShipCount = ownNation->max_human_battle_ship_count(); // return the no. of ships of the enemy that is strongest on the sea
  193. if( unit_res[UNIT_GALLEON]->get_nation_tech_level(nation_recno) > 0 )
  194. {
  195. buildId = UNIT_GALLEON;
  196. if( ownNation->ai_ship_count < 2 + (ownNation->pref_use_marine>50) )
  197. rc = 1;
  198. else
  199. rc = enemyShipCount > ownNation->ai_ship_count;
  200. }
  201. else if( unit_res[UNIT_CARAVEL]->get_nation_tech_level(nation_recno) > 0 )
  202. {
  203. buildId = UNIT_CARAVEL;
  204. if( ownNation->ai_ship_count < 2 + (ownNation->pref_use_marine>50) )
  205. rc = 1;
  206. else
  207. rc = enemyShipCount > ownNation->ai_ship_count;
  208. }
  209. else
  210. {
  211. buildId = UNIT_TRANSPORT;
  212. if( ownNation->ai_ship_count < 2 )
  213. rc = 1;
  214. }
  215. //---------------------------------------------//
  216. if( buildId && rc )
  217. build_ship( buildId, COMMAND_AI );
  218. }
  219. //--------- End of function FirmHarbor::think_build_ship -----------//