| 9 | uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)
| 1 | uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)
|
|---|
| 10 | {
| 2 | {
|
|---|
| 11 | /* Return if nothing to do. Also the rounding below fails for 0. */
| 3 | /* Return if nothing to do. Also the rounding below fails for 0. */
|
|---|
| 12 | if (amount == 0) return 0;
| 4 | if (amount == 0) return 0;
|
|---|
| 78 | uint no_stations = used_stations.Length();
| | |
|---|
| 79 |
| | |
|---|
| 80 | GSortT(used_stations.Begin(), used_stations.Length(), &StationRatingSorter);
| | |
|---|
| 81 |
| | |
|---|
| 82 | if (no_stations > 1) {
| | |
|---|
| 83 | Station *st1 = &used_stations.Begin();
| | |
|---|
| 84 | Station *st2 = &st1++;
| | |
|---|
| 85 | uint offset = 0;
| | |
|---|
| 86 |
| | |
|---|
| 87 | while (no_stations > 1) {
| | |
|---|
| 88 | const int diff = st1->goods[type].rating - st2->goods[type].rating;
| | |
|---|
| 89 | if (diff <= 0) {
| | |
|---|
| 90 | if (offset != 0) {
| | |
|---|
| 91 | st1 += offset;
| | |
|---|
| 92 | st2 += offset;
| | |
|---|
| 93 | offset = 0;
| | |
|---|
| 94 | continue;
| | |
|---|
| 95 | }
| | |
|---|
| 96 |
| | |
|---|
| 97 | st1++;
| | |
|---|
| 98 | st2++;
| | |
|---|
| 99 | no_stations--;
| | |
|---|
| 100 | } else {
| | |
|---|
| 101 | Swap(*st1, *st2);
| | |
|---|
| 102 |
| | |
|---|
| 103 | if (st1 == *used_stations.Begin()) continue;
| | |
|---|
| 104 |
| | |
|---|
| 105 | st1--;
| | |
|---|
| 106 | st2--;
| | |
|---|
| 107 | offset++;
| | |
|---|
| 108 | }
| | |
|---|
| 109 | }
| | |
|---|
| 110 | // for (Station **st_iter = used_stations.Begin(); st_iter != used_stations.End(); ++st_iter) {
| | |
|---|
| 111 | // Station *st1 = *st_iter;
| | |
|---|
| 112 | // Station *st2 = *st_iter + 1;
| | |
|---|
| 113 | //
| | |
|---|
| 114 | // if (st1->goods[type].rating >= st2->goods[type].rating) {
| | |
|---|
| 115 | // st2 = st1;
| | |
|---|
| 116 | // }
| | |
|---|
| 117 | //
| | |
|---|
| 118 | // }
| | |
|---|
| 119 | }
| | |
|---|
| 120 |
| | |
|---|
| 144 | uint worst_cargo = amount * best_rating2 / (best_rating1 + best_rating2);
| 88 | uint cargo_st2 = amount * best_rating2 / (best_rating1 + best_rating2);
|
|---|
| 145 | assert(worst_cargo <= (amount - worst_cargo));
| 89 | assert(cargo_st2 <= (amount - cargo_st2));
|
|---|
| 146 |
| 90 |
|
|---|
| 147 | /* And then send the cargo to the stations! */
| 91 | uint cargo_st1 = amount - cargo_st2;
|
|---|
| 148 | uint moved = UpdateStationWaiting(st1, type, amount - worst_cargo, source_type, source_id);
| 92 | assert(amount = cargo_st1 + cargo_st2);
|
|---|
| | | 93 |
|
|---|
| | | 94 | /* And then send the cargo to the stations! */
|
|---|
| | | 95 | uint moved = UpdateStationWaiting(st1, type, cargo_st1, source_type, source_id);
|
|---|