ResourceTrader.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * ResourceTrader.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. */
  9. #include "ResourceTrader.h"
  10. namespace NK2AI
  11. {
  12. bool ResourceTrader::trade(const std::unique_ptr<BuildAnalyzer> & buildAnalyzer, std::shared_ptr<CCallback> cc, TResources freeResources)
  13. {
  14. // TODO: Mircea: Maybe include based on how close danger is: X as default + proportion of close danger or something around that
  15. constexpr float ARMY_GOLD_RATIO_PER_MAKE_TURN_PASS = 0.1f;
  16. constexpr float EXPENDABLE_BULK_RATIO = 0.3f;
  17. bool haveTraded = false;
  18. ObjectInstanceID marketId;
  19. // TODO: Mircea: What about outside town markets that have better rates than a single town for example?
  20. // Are those used anywhere? To inspect.
  21. for (const auto * const town : cc->getTownsInfo())
  22. {
  23. if (town->hasBuiltSomeTradeBuilding())
  24. {
  25. marketId = town->id;
  26. break;
  27. }
  28. }
  29. if (!marketId.hasValue())
  30. return false;
  31. const CGObjectInstance * obj = cc->getObj(marketId, false);
  32. assert(obj);
  33. // if (!obj)
  34. // return false;
  35. const auto * market = dynamic_cast<const IMarket *>(obj);
  36. assert(market);
  37. // if (!market)
  38. // return false;
  39. bool shouldTryToTrade = true;
  40. while(shouldTryToTrade)
  41. {
  42. shouldTryToTrade = false;
  43. buildAnalyzer->update();
  44. // if we favor getResourcesRequiredNow is better on short term, if we favor getTotalResourcesRequired is better on long term
  45. TResources missingNow = buildAnalyzer->getMissingResourcesNow(ARMY_GOLD_RATIO_PER_MAKE_TURN_PASS);
  46. if(missingNow.empty())
  47. break;
  48. TResources income = buildAnalyzer->getDailyIncome();
  49. // We don't want to sell something that's necessary later on, though that could make short term a bit harder sometimes
  50. TResources freeAfterMissingTotal = buildAnalyzer->getFreeResourcesAfterMissingTotal(ARMY_GOLD_RATIO_PER_MAKE_TURN_PASS);
  51. #if NK2AI_TRACE_LEVEL >= 2
  52. logAi->info("ResourceTrader: Free %s. FreeAfterMissingTotal %s. MissingNow %s", freeResources.toString(), freeAfterMissingTotal.toString(), missingNow.toString());
  53. #endif
  54. if(ResourceTrader::tradeHelper(EXPENDABLE_BULK_RATIO, market, missingNow, income, freeAfterMissingTotal, buildAnalyzer, cc))
  55. {
  56. haveTraded = true;
  57. shouldTryToTrade = true;
  58. }
  59. }
  60. return haveTraded;
  61. }
  62. bool ResourceTrader::tradeHelper(
  63. float EXPENDABLE_BULK_RATIO,
  64. const IMarket * market,
  65. TResources missingNow,
  66. TResources income,
  67. TResources freeAfterMissingTotal,
  68. const std::unique_ptr<BuildAnalyzer> & buildAnalyzer,
  69. std::shared_ptr<CCallback> cc
  70. )
  71. {
  72. constexpr int EMPTY = -1;
  73. int mostWanted = EMPTY;
  74. TResource mostWantedScoreNeg = std::numeric_limits<TResource>::max();
  75. int mostExpendable = EMPTY;
  76. TResource mostExpendableAmountPos = 0;
  77. // Find the most wanted resource
  78. for(int i = 0; i < missingNow.size(); ++i)
  79. {
  80. if(missingNow[i] == 0)
  81. continue;
  82. const TResource score = income[i] - missingNow[i];
  83. if(score < mostWantedScoreNeg)
  84. {
  85. mostWanted = i;
  86. mostWantedScoreNeg = score;
  87. }
  88. }
  89. // Find the most expendable resource
  90. for(int i = 0; i < missingNow.size(); ++i)
  91. {
  92. const TResource amountToSell = freeAfterMissingTotal[i];
  93. if(amountToSell == 0)
  94. continue;
  95. bool okToSell = false;
  96. if(i == GameResID::GOLD)
  97. {
  98. // TODO: Mircea: Check if we should negate isGoldPressureOverMax() instead
  99. if(income[GameResID::GOLD] > 0 && !buildAnalyzer->isGoldPressureOverMax())
  100. okToSell = true;
  101. }
  102. else
  103. {
  104. okToSell = true;
  105. }
  106. if(amountToSell > mostExpendableAmountPos && okToSell)
  107. {
  108. mostExpendable = i;
  109. mostExpendableAmountPos = amountToSell;
  110. }
  111. }
  112. #if NK2AI_TRACE_LEVEL >= 2
  113. logAi->trace(
  114. "ResourceTrader: mostWanted: %d, mostWantedScoreNeg %d, mostExpendable: %d, mostExpendableAmountPos %d",
  115. mostWanted,
  116. mostWantedScoreNeg,
  117. mostExpendable,
  118. mostExpendableAmountPos
  119. );
  120. #endif
  121. if(mostExpendable == mostWanted || mostWanted == EMPTY || mostExpendable == EMPTY)
  122. return false;
  123. int givenPerUnit;
  124. int receivedPerUnit;
  125. market->getOffer(mostExpendable, mostWanted, givenPerUnit, receivedPerUnit, EMarketMode::RESOURCE_RESOURCE);
  126. if(!givenPerUnit || !receivedPerUnit)
  127. {
  128. logGlobal->error(
  129. "ResourceTrader: No offer for %d of %d, given %d, received %d. Should never happen",
  130. mostExpendable,
  131. mostWanted,
  132. givenPerUnit,
  133. receivedPerUnit
  134. );
  135. return false;
  136. }
  137. // TODO: Mircea: if 15 wood and 14 gems, gems can be used a lot more for buying other things
  138. if(givenPerUnit > mostExpendableAmountPos)
  139. return false;
  140. TResource multiplier = std::min(
  141. static_cast<int>(mostExpendableAmountPos * EXPENDABLE_BULK_RATIO / givenPerUnit),
  142. missingNow[mostWanted] / receivedPerUnit
  143. ); // for gold we have to / receivedUnits, because 1 ore gives many gold units
  144. if(multiplier == 0) // could happen for very small values due to EXPENDABLE_BULK_RATIO
  145. multiplier = 1;
  146. const TResource givenMultiplied = givenPerUnit * multiplier;
  147. if(givenMultiplied > freeAfterMissingTotal[mostExpendable])
  148. {
  149. logGlobal->error("ResourceTrader: Something went wrong with the multiplier %d", multiplier);
  150. return false;
  151. }
  152. cc->trade(market->getObjInstanceID(), EMarketMode::RESOURCE_RESOURCE, GameResID(mostExpendable), GameResID(mostWanted), givenMultiplied);
  153. #if NK2AI_TRACE_LEVEL >= 2
  154. logAi->info("ResourceTrader: Traded %d of %s for %d receivedPerUnit of %s", givenMultiplied, mostExpendable, receivedPerUnit, mostWanted);
  155. #endif
  156. return true;
  157. }
  158. }