Advertisement
Guest User

calculate_need_for_ps

a guest
Apr 25th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. /**
  2.  * calculates how much a productionsite of type \arg bo is needed inside it's
  3.  * economy. \arg prio is initial value for this calculation
  4.  *
  5.  * \returns the calculated priority
  6.  */
  7. int32_t DefaultAI::calculate_need_for_ps(BuildingObserver & bo, int32_t prio)
  8. {
  9.     // some randomness to avoid that defaultAI is building always
  10.     // the same (always == another game but same map with
  11.     // defaultAI on same coords)
  12.     prio += time(nullptr) % 3 - 1;
  13.  
  14.     // check if current economy can supply enough material for
  15.     // production.
  16.     for (uint32_t k = 0; k < bo.inputs.size(); ++k) {
  17.         prio += 2 * wares.at(bo.inputs.at(k)).producers;
  18.         prio -= wares.at(bo.inputs.at(k)).consumers;
  19.     }
  20.     if (bo.inputs.empty())
  21.         prio += 4;
  22.  
  23.     int32_t output_prio = 0;
  24.     for (uint32_t k = 0; k < bo.outputs.size(); ++k) {
  25.         WareObserver & wo = wares.at(bo.outputs.at(k));
  26.         if (wo.consumers > 0) {
  27.             output_prio += wo.preciousness;
  28.             output_prio += wo.consumers * 2;
  29.             output_prio -= wo.producers * 2;
  30.             if (bo.total_count() == 0)
  31.                 output_prio += 10; // add a big bonus
  32.         }
  33.     }
  34.     if (bo.outputs.size() > 1)
  35.         output_prio = static_cast<int32_t>
  36.             (ceil(output_prio / sqrt(static_cast<double>(bo.outputs.size()))));
  37.     prio += 2 * output_prio;
  38.  
  39.     // If building consumes some wares, multiply with current statistics of all
  40.     // other buildings of this type to avoid constructing buildings where already
  41.     // some are running on low resources.
  42.     // Else at least add a part of the stats t the calculation.
  43.     if (!bo.inputs.empty()) {
  44.         prio *= bo.current_stats;
  45.         prio /= 100;
  46.     } else
  47.         prio = ((prio * bo.current_stats) / 100) + (prio / 2);
  48.  
  49.     return prio;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement