Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /* From now we'll calculate with fractional cargo amounts.
  2.      * First determine how much cargo we really have. */
  3.     amount *= best_rating + 1;
  4.  
  5.     uint moving = 0;
  6.     for (auto &p : used_stations) {
  7.         uint owner = p.first->owner;
  8.         /* Multiply the amount by (company best / sum of best for each company) to get cargo allocated to a company
  9.          * and by (station rating / sum of ratings in a company) to get the result for a single station. */
  10.         uint a = amount * company_best[owner] * p.first->goods[type].rating;
  11.         uint b = best_sum * company_sum[owner];
  12.         p.second = a / b;
  13.         moving += p.second;
  14.         p.second |= (65535 * (a % b) / b) << 16;
  15.     }
  16.  
  17.     /* If there is some cargo left due to rounding issues distribute it according to the highest remainder. */
  18.     if (amount > moving) {
  19.         std::sort(used_stations.begin(), used_stations.end(), [](const StationInfo &a, const StationInfo &b) {
  20.             return b.second >> 16 < a.second >> 16;
  21.         });
  22.  
  23.         assert(amount - moving <= used_stations.size());
  24.         for (uint i = 0; i < amount - moving; i++) {
  25.             used_stations[i].second++;
  26.         }
  27.     }
  28.  
  29.     uint moved = 0;
  30.     for (auto &p : used_stations) {
  31.         moved += UpdateStationWaiting(p.first, type, GB(p.second, 0, 16), source_type, source_id);
  32.     }
  33.  
  34.     return moved;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement