OAI_MILI.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_MILI.CPP
  21. //Description: AI - think about expanding the military force
  22. #include <ALL.h>
  23. #include <OTOWN.h>
  24. #include <OF_CAMP.h>
  25. #include <ONATION.h>
  26. //--------- Begin of function Nation::think_military --------//
  27. void Nation::think_military()
  28. {
  29. //---- don't build new camp if we our food consumption > production ----//
  30. if( yearly_food_change() < 0 )
  31. {
  32. think_close_camp(); // think about closing down an existing one
  33. return;
  34. }
  35. //--- think about whether it should expand now ---//
  36. if( !ai_should_expand_military() && !ai_is_troop_need_new_camp() )
  37. return;
  38. //----- think about where to expand -----//
  39. int bestRating=0, curRating;
  40. Town *townPtr, *bestTownPtr=NULL;
  41. for( int i=ai_town_count-1 ; i>=0 ; i-- )
  42. {
  43. townPtr = town_array[ ai_town_array[i] ];
  44. if( !townPtr->is_base_town ) // only expand on base towns
  45. continue;
  46. if( townPtr->no_neighbor_space ) // if there is no space in the neighbor area for building a new firm.
  47. continue;
  48. curRating = townPtr->population; //**BUGHERE, to be modified.
  49. if( curRating > bestRating )
  50. {
  51. bestRating = curRating;
  52. bestTownPtr = townPtr;
  53. }
  54. }
  55. if( !bestTownPtr )
  56. return;
  57. //--------- queue building the camp now ---------//
  58. short buildXLoc, buildYLoc;
  59. if( !find_best_firm_loc(FIRM_CAMP, bestTownPtr->loc_x1, bestTownPtr->loc_y1, buildXLoc, buildYLoc) )
  60. {
  61. bestTownPtr->no_neighbor_space = 1;
  62. return;
  63. }
  64. add_action(buildXLoc, buildYLoc, bestTownPtr->loc_x1, bestTownPtr->loc_y1, ACTION_AI_BUILD_FIRM, FIRM_CAMP);
  65. }
  66. //--------- Begin of function Nation::think_military --------//
  67. //--------- Begin of function Nation::ai_should_expand_military --------//
  68. int Nation::ai_should_expand_military()
  69. {
  70. //----- don't expand if it is losing money -----//
  71. if( true_profit_365days() < 0 )
  72. return 0;
  73. //----- expand if it has enough cash -----//
  74. if( !ai_should_spend(pref_military_development/2) )
  75. return 0;
  76. //--- check whether the current military force is already big enough ---//
  77. if( ai_camp_count * 9 > total_jobless_population * (50+pref_military_development) / 150 ) // (50 to 150) / 150
  78. return 0;
  79. //---- see if any of the camps are still needed soldiers -----//
  80. int i, soldierCount, freeSpaceCount=0;
  81. FirmCamp* firmCamp;
  82. for( i=ai_camp_count-1 ; i>=0 ; i-- )
  83. {
  84. firmCamp = (FirmCamp*) firm_array[ ai_camp_array[i] ];
  85. if( firmCamp->should_close_flag ) // exclude those going to be closed down
  86. continue;
  87. //---- only build a new one when existing ones are all full ----//
  88. soldierCount = (firmCamp->overseer_recno>0) + firmCamp->worker_count +
  89. firmCamp->patrol_unit_count;
  90. freeSpaceCount += 9-soldierCount;
  91. if( firmCamp->ai_recruiting_soldier )
  92. return 0;
  93. }
  94. return freeSpaceCount < 9; // build build new ones when the existing ones are almost full
  95. }
  96. //--------- Begin of function Nation::ai_should_expand_military --------//
  97. //--------- Begin of function Nation::ai_is_troop_need_new_camp --------//
  98. int Nation::ai_is_troop_need_new_camp()
  99. {
  100. //--- check whether the current military force is already big enough ---//
  101. if( ai_has_too_many_camp() )
  102. return 0;
  103. //----- expand if it has enough cash -----//
  104. if( !ai_should_spend(50+pref_military_development/2) )
  105. return 0;
  106. //----- if existing camps can already host all units ----//
  107. int neededSoldierSpace = total_human_count+total_weapon_count - ai_camp_count*MAX_WORKER;
  108. int neededGeneralSpace = total_general_count - ai_camp_count;
  109. return neededSoldierSpace >= 9 + 20 * (100-pref_military_development) / 200 &&
  110. neededGeneralSpace >= 2 + 4 * (100-pref_military_development) / 200;
  111. }
  112. //--------- Begin of function Nation::ai_is_troop_need_new_camp --------//
  113. //--------- Begin of function Nation::ai_has_too_many_camp --------//
  114. int Nation::ai_has_too_many_camp()
  115. {
  116. return ai_camp_count * 9 > total_jobless_population * (150+pref_military_development) / 150; // (150 to 250) / 150
  117. }
  118. //--------- Begin of function Nation::ai_has_too_many_camp --------//
  119. //--------- Begin of function Nation::think_close_camp --------//
  120. int Nation::think_close_camp()
  121. {
  122. return 0;
  123. /*
  124. FirmCamp *firmCamp, *closeCamp=NULL;
  125. int curRating, bestRating=0;
  126. for( int i=ai_camp_count-1 ; i>=0 ; i-- )
  127. {
  128. firmCamp = (FirmCamp*) firm_array[ ai_camp_array[i] ];
  129. curRating = world.distance_rating(firmCamp->center_x, firmCamp->center_y
  130. */
  131. }
  132. //--------- Begin of function Nation::think_close_camp --------//