Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Test 0 - orig: 7.5 new: 16.2 (215.4%) samu: 18.9 (250.4%)
- Test 1 - orig: 20.3 new: 25.2 (123.7%) samu: 90.2 (443.4%)
- Test 2 - orig: 32.3 new: 74.2 (229.8%) samu: 268.0 (829.6%)
- Test 3 - orig: 48.8 new: 133.6 (274.0%) samu: 390.9 (801.9%)
- Test 4 - orig: 49.4 new: 137.0 (277.3%) samu: 403.2 (816.0%)
- Test 5 - orig: 147.6 new: 449.9 (304.8%) samu: 853.6 (578.3%)
- Test 6 - orig: 143.8 new: 485.1 (337.4%) samu: 878.6 (611.2%)
- Test 7 - orig: 143.8 new: 303.1 (210.8%) samu: 545.7 (379.5%)
- /** The type of cargo to be compared. */
- static CargoID _cargo_type_comparator;
- static bool CompareCargoRatings(const std::pair<Station *, uint> &a, const std::pair<Station *, uint> &b)
- {
- return b.first->goods[_cargo_type_comparator].rating < a.first->goods[_cargo_type_comparator].rating;
- }
- uint MoveGoodsToStationSamu(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)
- {
- /* Return if nothing to do. Also the rounding below fails for 0. */
- if (amount == 0) return 0;
- uint company_best[OWNER_NONE + 1] = {}; // best rating for each company, including OWNER_NONE
- uint company_sum[OWNER_NONE + 1] = {}; // sum of ratings for each company
- uint best_rating = 0;
- uint best_sum = 0; // sum of best ratings for each company
- typedef std::vector<std::pair<Station *, uint>> UsedStations;
- UsedStations used_stations;
- for (Station *st : *all_stations) {
- if (!CanMoveGoodsToStation(st, type)) continue;
- used_stations.emplace_back(std::make_pair(st, 0));
- uint st_owner = st->owner;
- byte r = st->goods[type].rating;
- if (r > company_best[st_owner]) {
- best_sum += r - company_best[st_owner]; // it's usually faster than iterating companies later
- company_best[st_owner] = r;
- if (r > best_rating) {
- best_rating = r;
- }
- }
- company_sum[st_owner] += r;
- }
- /* no stations around at all? */
- if (best_rating == 0) return 0;
- /* From now we'll calculate with fractal cargo amounts.
- * First determine how much cargo we really have. */
- amount *= best_rating + 1;
- if (used_stations.size() == 1) {
- /* only one station around */
- return UpdateStationWaiting(used_stations[0].first, type, amount, source_type, source_id);
- }
- uint moved_sum = 0;
- for (auto &p : used_stations) {
- uint st_owner = p.first->owner;
- uint station_amount = amount * company_best[st_owner] * p.first->goods[type].rating / best_sum / company_sum[st_owner];
- moved_sum += station_amount;
- p.second = station_amount;
- }
- uint remainder = amount - moved_sum;
- if (remainder > 0) {
- std::sort(used_stations.begin(), used_stations.end(), CompareCargoRatings);
- for (uint i = 0; i < remainder; i++)
- used_stations[i].second++;
- }
- uint moving = 0;
- for (auto &p : used_stations) {
- moving += UpdateStationWaiting(p.first, type, p.second, source_type, source_id);
- }
- return moving;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement