Loading

Revision differences

Old revision #p2pn4kewwNew revision #pnufelyg1
  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  
1uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)  9uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)  
2{  10{  
3    /* Return if nothing to do. Also the rounding below fails for 0. */  11    /* Return if nothing to do. Also the rounding below fails for 0. */  
  
16    uint best_rating5 = 0; // rating of st5  24    uint best_rating5 = 0; // rating of st5  
17    uint best_rating6 = 0; // rating of st6  25    uint best_rating6 = 0; // rating of st6  
18  26  
  27    StationList used_stations;  
  28  
19    for (Station * const *st_iter = all_stations->Begin(); st_iter != all_stations->End(); ++st_iter) {  29    for (Station * const *st_iter = all_stations->Begin(); st_iter != all_stations->End(); ++st_iter) {  
20        Station *st = *st_iter;  30        Station *st = *st_iter;  
21  31  
  
33        }  43        }  
34  44  
35        /* This station can be used, add it to st1/st2/st3/st4/st5/st6 */  45        /* This station can be used, add it to st1/st2/st3/st4/st5/st6 */  
  46        used_stations.Include(st);  
  47  
36        if (st1 == NULL || st->goods[type].rating >= best_rating1) {  48        if (st1 == NULL || st->goods[type].rating >= best_rating1) {  
37            st6 = st5; best_rating6 = best_rating5;  49            st6 = st5; best_rating6 = best_rating5;  
38            st5 = st4; best_rating5 = best_rating4;  50            st5 = st4; best_rating5 = best_rating4;  
  
63        }  75        }  
64    }  76    }  
65  77  
  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     
66    /* no stations around at all? */  121    /* no stations around at all? */  
67    if (st1 == NULL) return 0;  122    if (st1 == NULL) return 0;  
68  123  
  
159    }  214    }  
160  215  
161    if (st6 == NULL) {  216    if (st6 == NULL) {  
162        /* Five stations around, the best five (highest rating) are in st1, st2, st3, st4 and st5*/  162        /* Five stations around, the best five (highest rating) are in st1, st2, st3, st4 and st5 */
163        assert(st1 != NULL);  218        assert(st1 != NULL);  
164        assert(st2 != NULL);  219        assert(st2 != NULL);  
165        assert(st3 != NULL);  220        assert(st3 != NULL);