Advertisement
Guest User

Untitled

a guest
Jan 10th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. Test 0 - orig: 7.5 new: 16.2 (215.4%) samu: 18.9 (250.4%)
  2. Test 1 - orig: 20.3 new: 25.2 (123.7%) samu: 90.2 (443.4%)
  3. Test 2 - orig: 32.3 new: 74.2 (229.8%) samu: 268.0 (829.6%)
  4. Test 3 - orig: 48.8 new: 133.6 (274.0%) samu: 390.9 (801.9%)
  5. Test 4 - orig: 49.4 new: 137.0 (277.3%) samu: 403.2 (816.0%)
  6. Test 5 - orig: 147.6 new: 449.9 (304.8%) samu: 853.6 (578.3%)
  7. Test 6 - orig: 143.8 new: 485.1 (337.4%) samu: 878.6 (611.2%)
  8. Test 7 - orig: 143.8 new: 303.1 (210.8%) samu: 545.7 (379.5%)
  9.  
  10.  
  11. /** The type of cargo to be compared. */
  12. static CargoID _cargo_type_comparator;
  13.  
  14. static bool CompareCargoRatings(const std::pair<Station *, uint> &a, const std::pair<Station *, uint> &b)
  15. {
  16. return b.first->goods[_cargo_type_comparator].rating < a.first->goods[_cargo_type_comparator].rating;
  17. }
  18.  
  19. uint MoveGoodsToStationSamu(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)
  20. {
  21. /* Return if nothing to do. Also the rounding below fails for 0. */
  22. if (amount == 0) return 0;
  23.  
  24. uint company_best[OWNER_NONE + 1] = {}; // best rating for each company, including OWNER_NONE
  25. uint company_sum[OWNER_NONE + 1] = {}; // sum of ratings for each company
  26. uint best_rating = 0;
  27. uint best_sum = 0; // sum of best ratings for each company
  28.  
  29. typedef std::vector<std::pair<Station *, uint>> UsedStations;
  30. UsedStations used_stations;
  31.  
  32. for (Station *st : *all_stations) {
  33. if (!CanMoveGoodsToStation(st, type)) continue;
  34.  
  35. used_stations.emplace_back(std::make_pair(st, 0));
  36. uint st_owner = st->owner;
  37.  
  38. byte r = st->goods[type].rating;
  39. if (r > company_best[st_owner]) {
  40. best_sum += r - company_best[st_owner]; // it's usually faster than iterating companies later
  41. company_best[st_owner] = r;
  42. if (r > best_rating) {
  43. best_rating = r;
  44. }
  45. }
  46. company_sum[st_owner] += r;
  47. }
  48.  
  49. /* no stations around at all? */
  50. if (best_rating == 0) return 0;
  51.  
  52. /* From now we'll calculate with fractal cargo amounts.
  53. * First determine how much cargo we really have. */
  54. amount *= best_rating + 1;
  55.  
  56. if (used_stations.size() == 1) {
  57. /* only one station around */
  58. return UpdateStationWaiting(used_stations[0].first, type, amount, source_type, source_id);
  59. }
  60.  
  61. uint moved_sum = 0;
  62. for (auto &p : used_stations) {
  63. uint st_owner = p.first->owner;
  64. uint station_amount = amount * company_best[st_owner] * p.first->goods[type].rating / best_sum / company_sum[st_owner];
  65. moved_sum += station_amount;
  66. p.second = station_amount;
  67. }
  68.  
  69. uint remainder = amount - moved_sum;
  70. if (remainder > 0) {
  71. std::sort(used_stations.begin(), used_stations.end(), CompareCargoRatings);
  72. for (uint i = 0; i < remainder; i++)
  73. used_stations[i].second++;
  74. }
  75.  
  76. uint moving = 0;
  77. for (auto &p : used_stations) {
  78. moving += UpdateStationWaiting(p.first, type, p.second, source_type, source_id);
  79. }
  80.  
  81. return moving;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement