OF_FACT2.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_FACT2.CPP
  21. //Description : Firm Factory - AI functions
  22. #include <OINFO.h>
  23. #include <OUNIT.h>
  24. #include <OGAME.h>
  25. #include <OFONT.h>
  26. #include <ONATION.h>
  27. #include <ORAWRES.h>
  28. #include <ORACERES.h>
  29. #include <OTOWNRES.h>
  30. #include <OWORLD.h>
  31. #include <OF_MINE.h>
  32. #include <OF_MARK.h>
  33. #include <OF_FACT.h>
  34. //------- Begin of function FirmFactory::process_ai -----------//
  35. //
  36. void FirmFactory::process_ai()
  37. {
  38. if( info.game_date%15==firm_recno%15 )
  39. {
  40. if( think_change_production() )
  41. return;
  42. }
  43. //------- recruit workers ---------//
  44. if( info.game_date%15==firm_recno%15 )
  45. {
  46. if( worker_count < MAX_WORKER )
  47. ai_recruit_worker();
  48. }
  49. //---- think about building market place to link to ----//
  50. if( info.game_date%30==firm_recno%30 )
  51. think_build_market();
  52. //---- think about ways to increase productivity ----//
  53. if( info.game_date%30==firm_recno%30 )
  54. think_inc_productivity();
  55. }
  56. //--------- End of function FirmFactory::process_ai -----------//
  57. //------- Begin of function FirmFactory::think_build_market -----------//
  58. //
  59. int FirmFactory::think_build_market()
  60. {
  61. if( no_neighbor_space ) // if there is no space in the neighbor area for building a new firm.
  62. return 0;
  63. Nation* nationPtr = nation_array[nation_recno];
  64. //--- check whether the AI can build a new firm next this firm ---//
  65. if( !nationPtr->can_ai_build(FIRM_MARKET) )
  66. return 0;
  67. //----------------------------------------------------//
  68. // If there is already a firm queued for building with
  69. // a building location that is within the effective range
  70. // of the this firm.
  71. //----------------------------------------------------//
  72. if( nationPtr->is_build_action_exist(FIRM_MARKET, center_x, center_y) )
  73. return 0;
  74. //-- only build one market place next to this factory, check if there is any existing one --//
  75. FirmMarket* firmPtr;
  76. for(int i=0; i<linked_firm_count; i++)
  77. {
  78. err_when(!linked_firm_array[i] || firm_array.is_deleted(linked_firm_array[i]));
  79. firmPtr = (FirmMarket*) firm_array[linked_firm_array[i]];
  80. if( firmPtr->firm_id!=FIRM_MARKET )
  81. continue;
  82. //----- if this is a retail market of our own ------//
  83. if( firmPtr->nation_recno == nation_recno &&
  84. ((FirmMarket*)firmPtr)->is_retail_market )
  85. {
  86. return 0;
  87. }
  88. }
  89. //------ queue building a new market -------//
  90. short buildXLoc, buildYLoc;
  91. if( !nationPtr->find_best_firm_loc(FIRM_MARKET, loc_x1, loc_y1, buildXLoc, buildYLoc) )
  92. {
  93. no_neighbor_space = 1;
  94. return 0;
  95. }
  96. nationPtr->add_action(buildXLoc, buildYLoc, loc_x1, loc_y1, ACTION_AI_BUILD_FIRM, FIRM_MARKET);
  97. return 1;
  98. }
  99. //--------- End of function FirmFactory::think_build_market -----------//
  100. //------- Begin of function FirmFactory::think_inc_productivity -------//
  101. //
  102. int FirmFactory::think_inc_productivity()
  103. {
  104. //----------------------------------------------//
  105. //
  106. // If this factory has a medium to high level of stock,
  107. // this means the bottleneck is not at the factories,
  108. // building more factories won't solve the problem.
  109. //
  110. //----------------------------------------------//
  111. if( stock_qty > max_stock_qty * 0.1 && production_30days() > 30 )
  112. return 0;
  113. //----------------------------------------------//
  114. //
  115. // If this factory has a low level of raw materials,
  116. // this means the bottleneck is at the raw material supply.
  117. //
  118. //----------------------------------------------//
  119. if( raw_stock_qty < max_raw_stock_qty * 0.2 )
  120. return 0;
  121. return think_hire_inn_unit();
  122. }
  123. //--------- End of function FirmFactory::think_inc_productivity -------//
  124. //------- Begin of function FirmFactory::think_change_production -------//
  125. //
  126. int FirmFactory::think_change_production()
  127. {
  128. if( cur_month_production + last_month_production > 0 ||
  129. raw_stock_qty > 0 )
  130. {
  131. return 0;
  132. }
  133. if( info.game_date < setup_date + 30 ) // only change production after the factory has been running for at least one month
  134. return 0;
  135. //-- only build one market place next to this factory, check if there is any existing one --//
  136. #define MIN_FACTORY_IMPORT_STOCK_QTY 20
  137. Firm* firmPtr;
  138. int curRating, bestRating=0, bestProductId=0, bestIsOwn=0;
  139. for(int i=0; i<linked_firm_count; i++)
  140. {
  141. //### begin alex 25/9 ###//
  142. //firmPtr = (FirmMine*) firm_array[linked_firm_array[i]];
  143. firmPtr = firm_array[linked_firm_array[i]];
  144. //#### end alex 25/9 ####//
  145. if( firmPtr->firm_id!=FIRM_MINE && firmPtr->firm_id!=FIRM_MARKET )
  146. continue;
  147. //--- if this link to this market is disabled, enable it now ---//
  148. if( linked_firm_enable_array[i] == LINK_DE )
  149. toggle_firm_link( i+1, 1, COMMAND_AI );
  150. if( linked_firm_enable_array[i] != LINK_EE )
  151. continue;
  152. curRating=0;
  153. //-------- if this is a mine ------//
  154. if( firmPtr->firm_id == FIRM_MINE )
  155. {
  156. FirmMine* firmMine = (FirmMine*) firmPtr;
  157. if( firmMine->stock_qty >= MIN_FACTORY_IMPORT_STOCK_QTY )
  158. {
  159. curRating = (int) firmMine->stock_qty;
  160. if( curRating > bestRating )
  161. {
  162. if( firmPtr->nation_recno == nation_recno || !bestIsOwn ) // try to get raw materials from own firms first
  163. {
  164. bestRating = curRating;
  165. bestProductId = firmMine->raw_id;
  166. bestIsOwn = firmPtr->nation_recno == nation_recno;
  167. }
  168. }
  169. }
  170. }
  171. //-------- if this is a market ------//
  172. else if( firmPtr->firm_id == FIRM_MARKET )
  173. {
  174. FirmMarket* firmMarket = (FirmMarket*) firmPtr;
  175. MarketGoods* marketGoods = firmMarket->market_goods_array;
  176. for( int j=0 ; j<MAX_MARKET_GOODS ; j++, marketGoods++ )
  177. {
  178. if( marketGoods->raw_id &&
  179. marketGoods->stock_qty >= MIN_FACTORY_IMPORT_STOCK_QTY )
  180. {
  181. curRating = (int) marketGoods->stock_qty;
  182. if( curRating > bestRating )
  183. {
  184. if( firmPtr->nation_recno == nation_recno || !bestIsOwn ) // try to get raw materials from own firms first
  185. {
  186. bestRating = curRating;
  187. bestProductId = marketGoods->raw_id;
  188. bestIsOwn = firmPtr->nation_recno == nation_recno;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. //------------------------------------//
  196. if( bestProductId )
  197. {
  198. set_production(bestProductId);
  199. return 1;
  200. }
  201. else
  202. {
  203. if( info.game_date > setup_date + 60 )
  204. {
  205. ai_del_firm(); // delete the firm if there is no raw materials available after it has been built for over 2 months
  206. return 1;
  207. }
  208. }
  209. return 0;
  210. }
  211. //--------- End of function FirmFactory::think_change_production -------//
  212. //------- Begin of function FirmFactory::ai_has_excess_worker -------//
  213. //
  214. // Return whether this firm has any excessive workers or not.
  215. //
  216. int FirmFactory::ai_has_excess_worker()
  217. {
  218. //--- if the actual production is lower than the productivity, than the firm must be under-capacity.
  219. if( worker_count > 4 ) // at least keep 4 workers
  220. {
  221. return stock_qty > max_stock_qty * (float) 0.9 &&
  222. production_30days() < productivity*25; // take 25 days instead of 30 days so there will be small chance of errors.
  223. }
  224. return 0;
  225. }
  226. //--------- End of function FirmFactory::ai_has_excess_worker -------//