OF_MINE2.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_MINE2.CPP
  21. //Description : Firm Mine - AI functions
  22. #include <OINFO.h>
  23. #include <OUNIT.h>
  24. #include <ORACERES.h>
  25. #include <OGAME.h>
  26. #include <OWORLD.h>
  27. #include <ONATION.h>
  28. #include <OSITE.h>
  29. #include <OF_MINE.h>
  30. #include <OF_FACT.h>
  31. #include <OF_MARK.h>
  32. //------- Begin of function FirmMine::process_ai -----------//
  33. //
  34. void FirmMine::process_ai()
  35. {
  36. //---- if the reserve has exhaust ----//
  37. if( !raw_id || reserve_qty==0 )
  38. {
  39. err_when( under_construction );
  40. ai_del_firm();
  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 factory and market place to link to ----//
  50. int rc = info.game_date%30==firm_recno%30;
  51. if( nation_array[nation_recno]->ai_factory_count==0 ) // if the nation doesn't have any factories yet, give building factory a higher priority
  52. rc = info.game_date%5==firm_recno%5;
  53. if( rc )
  54. {
  55. if( !think_build_factory(raw_id) )
  56. ai_should_build_factory_count = 0; // reset the counter
  57. think_build_market(); // don't build it in FirmMine, when it builds a factory the factory will build a mine.
  58. }
  59. //---- think about ways to increase productivity ----//
  60. if( info.game_date%30==firm_recno%30 )
  61. think_inc_productivity();
  62. }
  63. //--------- End of function FirmMine::process_ai -----------//
  64. //------- Begin of function FirmMine::think_build_market -----------//
  65. //
  66. int FirmMine::think_build_market()
  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* nationPtr = nation_array[nation_recno];
  71. //-- only build a new market when the mine has a larger supply than the demands from factories and market places
  72. if( stock_qty < max_stock_qty * 0.2 )
  73. return 0;
  74. //--- check whether the AI can build a new firm next this firm ---//
  75. if( !nationPtr->can_ai_build(FIRM_MARKET) )
  76. return 0;
  77. //-- only build one market place next to this mine, check if there is any existing one --//
  78. FirmMarket* firmPtr;
  79. for(int i=0; i<linked_firm_count; i++)
  80. {
  81. err_when(!linked_firm_array[i] || firm_array.is_deleted(linked_firm_array[i]));
  82. firmPtr = (FirmMarket*) firm_array[linked_firm_array[i]];
  83. if(firmPtr->firm_id!=FIRM_MARKET)
  84. continue;
  85. //------ if this market is our own one ------//
  86. if( firmPtr->nation_recno == nation_recno &&
  87. ((FirmMarket*)firmPtr)->is_retail_market==0 ) // if it already has a raw material market, then no need to build a new one
  88. {
  89. return 0;
  90. }
  91. }
  92. //------ queue building a new market -------//
  93. short buildXLoc, buildYLoc;
  94. if( !nationPtr->find_best_firm_loc(FIRM_MARKET, loc_x1, loc_y1, buildXLoc, buildYLoc) )
  95. {
  96. no_neighbor_space = 1;
  97. return 0;
  98. }
  99. nationPtr->add_action(buildXLoc, buildYLoc, loc_x1, loc_y1, ACTION_AI_BUILD_FIRM, FIRM_MARKET);
  100. return 1;
  101. }
  102. //--------- End of function FirmMine::think_build_market -----------//
  103. //------- Begin of function FirmMine::think_inc_productivity -------//
  104. //
  105. int FirmMine::think_inc_productivity()
  106. {
  107. //----------------------------------------------//
  108. //
  109. // If this factory has a medium to high level of stock,
  110. // this means the bottleneck is not at the factories,
  111. // building more factories won't solve the problem.
  112. //
  113. //----------------------------------------------//
  114. if( stock_qty > max_stock_qty * 0.1 && production_30days() > 30 )
  115. return 0;
  116. if( reserve_qty < MAX_RAW_RESERVE_QTY/10 )
  117. return 0;
  118. //------ try to get skilled workers from inns and other firms ------//
  119. // return think_hire_inn_unit();
  120. return 0;
  121. }
  122. //--------- End of function FirmMine::think_inc_productivity -------//
  123. //------- Begin of function FirmMine::ai_has_excess_worker -------//
  124. //
  125. // Return whether this firm has any excessive workers or not.
  126. //
  127. int FirmMine::ai_has_excess_worker()
  128. {
  129. //--- if the actual production is lower than the productivity, than the firm must be under-capacity.
  130. if( worker_count > 4 ) // at least keep 4 workers
  131. {
  132. return stock_qty > max_stock_qty * (float) 0.9 &&
  133. production_30days() < productivity*25; // take 25 days instead of 30 days so there will be small chance of errors.
  134. }
  135. else if( worker_count > 1 )
  136. {
  137. return reserve_qty < worker_count*200;
  138. }
  139. else // don't leave if there is only one miner there
  140. {
  141. return 0;
  142. }
  143. }
  144. //--------- End of function FirmMine::ai_has_excess_worker -------//