OF_WAR2.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_WAR2.CPP
  21. //Description : Firm War Factory - AI functions
  22. #include <ONATION.h>
  23. #include <OINFO.h>
  24. #include <OTOWN.h>
  25. #include <OUNIT.h>
  26. #include <OTECHRES.h>
  27. #include <OF_WAR.h>
  28. //--------- Begin of function FirmWar::process_ai ---------//
  29. void FirmWar::process_ai()
  30. {
  31. //---- think about which technology to research ----//
  32. if( !build_unit_id )
  33. think_new_production();
  34. //------- recruit workers ---------//
  35. if( info.game_date%15==firm_recno%15 )
  36. {
  37. if( worker_count < MAX_WORKER )
  38. ai_recruit_worker();
  39. }
  40. //----- think about closing down this firm -----//
  41. if( info.game_date%30==firm_recno%30 )
  42. {
  43. if( think_del() )
  44. return;
  45. }
  46. }
  47. //----------- End of function FirmWar::process_ai -----------//
  48. //------- Begin of function FirmWar::think_del -----------//
  49. //
  50. // Think about deleting this firm.
  51. //
  52. int FirmWar::think_del()
  53. {
  54. if( worker_count > 0 )
  55. return 0;
  56. //-- check whether the firm is linked to any towns or not --//
  57. for( int i=0 ; i<linked_town_count ; i++ )
  58. {
  59. if( linked_town_enable_array[i] == LINK_EE )
  60. return 0;
  61. }
  62. //------------------------------------------------//
  63. ai_del_firm();
  64. return 1;
  65. }
  66. //--------- End of function FirmWar::think_del -----------//
  67. //----- Begin of function FirmWar::think_new_production ------//
  68. //
  69. // Think about which weapon to produce.
  70. //
  71. void FirmWar::think_new_production()
  72. {
  73. //----- first see if we have enough money to build & support the weapon ----//
  74. if( !should_build_new_weapon() )
  75. return;
  76. //---- calculate the average instance count of all available weapons ---//
  77. int weaponTypeCount=0, totalWeaponCount=0;
  78. UnitInfo* unitInfo;
  79. int unitId;
  80. for( unitId=1; unitId<=MAX_UNIT_TYPE ; unitId++ )
  81. {
  82. unitInfo = unit_res[unitId];
  83. if( unitInfo->unit_class != UNIT_CLASS_WEAPON ||
  84. unitInfo->get_nation_tech_level(nation_recno) == 0 )
  85. {
  86. continue;
  87. }
  88. if( unitId == UNIT_EXPLOSIVE_CART ) // AI doesn't use Porcupine
  89. continue;
  90. weaponTypeCount++;
  91. totalWeaponCount += unitInfo->nation_unit_count_array[nation_recno-1];
  92. }
  93. if( weaponTypeCount==0 ) // none of weapon technologies is available
  94. return;
  95. int averageWeaponCount = totalWeaponCount/weaponTypeCount;
  96. //----- think about which is best to build now ------//
  97. int curRating, bestRating=0, bestUnitId=0;
  98. for( unitId=1; unitId<=MAX_UNIT_TYPE ; unitId++ )
  99. {
  100. unitInfo = unit_res[unitId];
  101. if( unitInfo->unit_class != UNIT_CLASS_WEAPON )
  102. continue;
  103. int techLevel = unitInfo->get_nation_tech_level(nation_recno);
  104. if( techLevel==0 )
  105. continue;
  106. if( unitId == UNIT_EXPLOSIVE_CART ) //**BUGHERE, don't produce it yet, it needs a different usage than the others.
  107. continue;
  108. int unitCount = unitInfo->nation_unit_count_array[nation_recno-1];
  109. curRating = averageWeaponCount-unitCount + techLevel*3;
  110. if( curRating > bestRating )
  111. {
  112. bestRating = curRating;
  113. bestUnitId = unitId;
  114. }
  115. }
  116. //------------------------------------//
  117. if( bestUnitId )
  118. add_queue( bestUnitId );
  119. }
  120. //------ End of function FirmWar::think_new_production -------//
  121. //----- Begin of function FirmWar::should_build_new_weapon ------//
  122. //
  123. int FirmWar::should_build_new_weapon()
  124. {
  125. //----- first see if we have enough money to build & support the weapon ----//
  126. Nation* nationPtr = nation_array[nation_recno];
  127. if( nationPtr->true_profit_365days() < 0 ) // don't build new weapons if we are currently losing money
  128. return 0;
  129. if( nationPtr->expense_365days(EXPENSE_WEAPON) >
  130. nationPtr->income_365days() * 30 + nationPtr->pref_use_weapon/2 ) // if weapon expenses are larger than 30% to 80% of the total income, don't build new weapons
  131. {
  132. return 0;
  133. }
  134. //----- see if there is any space on existing camps -----//
  135. Firm* firmPtr;
  136. for( int i=0 ; i<nationPtr->ai_camp_count ; i++ )
  137. {
  138. firmPtr = firm_array[ nationPtr->ai_camp_array[i] ];
  139. if( firmPtr->region_id != region_id )
  140. continue;
  141. if( firmPtr->worker_count < MAX_WORKER ) // there is space in this firm
  142. return 1;
  143. }
  144. return 0;
  145. }
  146. //------ End of function FirmWar::should_build_new_weapon -------//