OAI_ECO.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_ECO.CPP
  21. //Description: AI economy functions
  22. #include <ALL.h>
  23. #include <OUNIT.h>
  24. #include <OFIRMALL.h>
  25. #include <ONATION.h>
  26. //--------- Begin of function Nation::think_reduce_expense --------//
  27. void Nation::think_reduce_expense()
  28. {
  29. if( true_profit_365days() > 0 || cash > 5000 * pref_cash_reserve / 100 )
  30. return;
  31. //-------- close down firms ---------//
  32. int curRating, bestRating=0;
  33. Firm *firmPtr, *bestFirm=NULL;
  34. for( int i=firm_array.size() ; i>0 ; i-- )
  35. {
  36. if( firm_array.is_deleted(i) )
  37. continue;
  38. firmPtr = firm_array[i];
  39. if( firmPtr->nation_recno != nation_recno )
  40. continue;
  41. if( firmPtr->firm_id == FIRM_WAR_FACTORY ||
  42. firmPtr->firm_id == FIRM_RESEARCH )
  43. {
  44. curRating = 100-(int)firmPtr->productivity;
  45. }
  46. else
  47. continue;
  48. if( curRating > bestRating )
  49. {
  50. bestRating = curRating;
  51. bestFirm = firmPtr;
  52. }
  53. }
  54. if( bestFirm )
  55. bestFirm->ai_del_firm();
  56. }
  57. //---------- End of function Nation::think_reduce_expense --------//
  58. //----- Begin of function Nation::ai_should_spend -----//
  59. //
  60. // This function returns whether this nation should make
  61. // a new spending.
  62. //
  63. // <int> importanceRating - how important is the spending to the
  64. // nation.
  65. // [float] spendAmt - if this is not given, then it will
  66. // consider generally - whether the
  67. // nation should spend money generally.
  68. //
  69. int Nation::ai_should_spend(int importanceRating, float spendAmt)
  70. {
  71. if( cash < spendAmt )
  72. return 0;
  73. float fixedExpense = fixed_expense_365days();
  74. float stdCashLevel = max(fixedExpense,2000) * (150+pref_cash_reserve) / 100;
  75. float trueProfit = true_profit_365days();
  76. //----- if we are losing money, don't spend on non-important things -----//
  77. if( trueProfit < 0 )
  78. {
  79. if( 400 * (-trueProfit) / fixedExpense > importanceRating )
  80. return 0;
  81. }
  82. //--------------------------------------//
  83. float curCashLevel = 100 * (cash-spendAmt) / (stdCashLevel*2);
  84. return importanceRating >= (100-curCashLevel);
  85. }
  86. //------ End of function Nation::ai_should_spend ------//
  87. //----- Begin of function Nation::ai_should_spend_war -----//
  88. //
  89. // This function returns whether the nation should spend money
  90. // or war.
  91. //
  92. // <int> enemyMilitaryRating - the military_rank_rating() of the enemy
  93. // [int] considerCeaseFire - whether this function is called when considering ceasing fire
  94. // (default:0)
  95. //
  96. int Nation::ai_should_spend_war(int enemyMilitaryRating, int considerCeaseFire)
  97. {
  98. int importanceRating = 30 + pref_military_development/5; // 30 to 50
  99. importanceRating += military_rank_rating() - enemyMilitaryRating*2;
  100. if( considerCeaseFire ) // only when we are very powerful, we will start a battle. So won't cease fire too soon after declaring war
  101. importanceRating += 20; // less eary to return 0, for cease fire
  102. return ai_should_spend(importanceRating);
  103. }
  104. //------ End of function Nation::ai_should_spend_war ------//