Loading

Revision differences

Old revision #pnufelyg1New revision #pempnuqzd
1static int CDECL StationRatingSorter(const Station * const *a, const Station * const *b)    
2{    
3//    GSortT(used_stations->Begin(), used_stations.Lenght(), );    
4    const Station *sta = *a;    
5    const Station *stb = *b;    
6    return stb->goods[0].rating - sta->goods[0].rating;    
7}    
8    
9uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)  1uint 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;  
13    
14    Station *st1 = NULL;   // Station with best rating  5    Station *st1 = NULL;   // Station with best rating  
15    Station *st2 = NULL;   // Second best station  6    Station *st2 = NULL;   // Second best station  
16    Station *st3 = NULL;   // Third best station  7    Station *st3 = NULL;   // Third best station  
  
24    uint best_rating5 = 0; // rating of st5  15    uint best_rating5 = 0; // rating of st5  
25    uint best_rating6 = 0; // rating of st6  16    uint best_rating6 = 0; // rating of st6  
26  17  
27    StationList used_stations;    
28    
29    for (Station * const *st_iter = all_stations->Begin(); st_iter != all_stations->End(); ++st_iter) {  18    for (Station * const *st_iter = all_stations->Begin(); st_iter != all_stations->End(); ++st_iter) {  
30        Station *st = *st_iter;  19        Station *st = *st_iter;  
31  20  
  
43        }  32        }  
44  33  
45        /* This station can be used, add it to st1/st2/st3/st4/st5/st6 */  34        /* This station can be used, add it to st1/st2/st3/st4/st5/st6 */  
46        used_stations.Include(st);    
47    
48        if (st1 == NULL || st->goods[type].rating >= best_rating1) {  35        if (st1 == NULL || st->goods[type].rating >= best_rating1) {  
49            st6 = st5; best_rating6 = best_rating5;  36            st6 = st5; best_rating6 = best_rating5;  
50            st5 = st4; best_rating5 = best_rating4;  37            st5 = st4; best_rating5 = best_rating4;  
  
75        }  62        }  
76    }  63    }  
77  64  
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       
121    /* no stations around at all? */  65    /* no stations around at all? */  
122    if (st1 == NULL) return 0;  66    if (st1 == NULL) return 0;  
123  67  
  
141         * this calculation. In reality that will mean the bonus will be pretty low.  85         * this calculation. In reality that will mean the bonus will be pretty low.  
142         * Nevertheless, the best station should always get the most cargo regardless  86         * Nevertheless, the best station should always get the most cargo regardless  
143         * of rounding issues. */  87         * of rounding issues. */  
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);
149        /* These two UpdateStationWaiting's can't be in the statement as then the order  96        /* These two UpdateStationWaiting's can't be in the statement as then the order  
150         * of execution would be undefined and that could cause desyncs with callbacks. */  97         * of execution would be undefined and that could cause desyncs with callbacks. */  
151        return moved + UpdateStationWaiting(st2, type, worst_cargo, source_type, source_id);  151        return moved + UpdateStationWaiting(st2, type, cargo_st2, source_type, source_id);
152    }  99    }  
153  100  
154    if (st4 == NULL) {  101    if (st4 == NULL) {