Loading

Revision differences

Old revision #pfsmdufv4New revision #p2pn4keww
6    Station *st1 = NULL;   // Station with best rating  6    Station *st1 = NULL;   // Station with best rating  
7    Station *st2 = NULL;   // Second best station  7    Station *st2 = NULL;   // Second best station  
8    Station *st3 = NULL;   // Third best station  8    Station *st3 = NULL;   // Third best station  
  9    Station *st4 = NULL;   // Fourth best station  
  10    Station *st5 = NULL;   // Fifth best station  
  11    Station *st6 = NULL;   // Sixth best station  
9    uint best_rating1 = 0; // rating of st1  12    uint best_rating1 = 0; // rating of st1  
10    uint best_rating2 = 0; // rating of st2  13    uint best_rating2 = 0; // rating of st2  
11    uint best_rating3 = 0; // rating of st3  14    uint best_rating3 = 0; // rating of st3  
  15    uint best_rating4 = 0; // rating of st4  
  16    uint best_rating5 = 0; // rating of st5  
  17    uint best_rating6 = 0; // rating of st6  
12  18  
13    for (Station * const *st_iter = all_stations->Begin(); st_iter != all_stations->End(); ++st_iter) {  19    for (Station * const *st_iter = all_stations->Begin(); st_iter != all_stations->End(); ++st_iter) {  
14        Station *st = *st_iter;  20        Station *st = *st_iter;  
  
26            if (st->facilities == FACIL_BUS_STOP) continue; // non-passengers are never served by just a bus stop  32            if (st->facilities == FACIL_BUS_STOP) continue; // non-passengers are never served by just a bus stop  
27        }  33        }  
28  34  
29        /* This station can be used, add it to st1/st2/st3 */  29        /* This station can be used, add it to st1/st2/st3/st4/st5/st6 */
30        if (st1 == NULL || st->goods[type].rating >= best_rating1) {  36        if (st1 == NULL || st->goods[type].rating >= best_rating1) {  
31            st3 = st2;  31            st6 = st5; best_rating6 = best_rating5;
32            best_rating3 = best_rating2;  32            st5 = st4; best_rating5 = best_rating4;
33            st2 = st1;  33            st4 = st3; best_rating4 = best_rating3;
34            best_rating2 = best_rating1;  34            st3 = st2; best_rating3 = best_rating2;
35            st1 = st;  35            st2 = st1; best_rating2 = best_rating1;
36            best_rating1 = st->goods[type].rating;  36            st1 = st; best_rating1 = st->goods[type].rating;
37        } else if (st2 == NULL || st->goods[type].rating >= best_rating2) {  43        } else if (st2 == NULL || st->goods[type].rating >= best_rating2) {  
38            st3 = st2;  44            st6 = st5; best_rating6 = best_rating5;
39            best_rating3 = best_rating2;  45            st5 = st4; best_rating5 = best_rating4;
40            st2 = st;  46            st4 = st3; best_rating4 = best_rating3;
41            best_rating2 = st->goods[type].rating;  47            st3 = st2; best_rating3 = best_rating2;
   48            st2 = st; best_rating2 = st->goods[type].rating;
42        } else if (st3 == NULL || st->goods[type].rating >= best_rating3) {  49        } else if (st3 == NULL || st->goods[type].rating >= best_rating3) {  
43            st3 = st;  50            st6 = st5; best_rating6 = best_rating5;
44            best_rating3 = st->goods[type].rating;  51            st5 = st4; best_rating5 = best_rating4;
   52            st4 = st3; best_rating4 = best_rating3;
   53            st3 = st; best_rating3 = st->goods[type].rating;
   54        } else if (st4 == NULL || st->goods[type].rating >= best_rating4) {
   55            st6 = st5; best_rating6 = best_rating5;
   56            st5 = st4; best_rating5 = best_rating4;
   57            st4 = st; best_rating4 = st->goods[type].rating;
   58        } else if (st5 == NULL || st->goods[type].rating >= best_rating5) {
   59            st6 = st5; best_rating6 = best_rating5;
   60            st5 = st; best_rating5 = st->goods[type].rating;
   61        } else if (st6 == NULL || st->goods[type].rating >= best_rating6) {
   62            st6 = st; best_rating6 = st->goods[type].rating;
45        }  63        }  
46    }  64    }  
47  65  
  
78        return moved + UpdateStationWaiting(st2, type, worst_cargo, source_type, source_id);  96        return moved + UpdateStationWaiting(st2, type, worst_cargo, source_type, source_id);  
79    }  97    }  
80  98  
81    /* three stations around, the best three (highest rating) are in st1, st2 and st3 */  99    if (st4 == NULL) {
   100        /* three stations around, the best three (highest rating) are in st1, st2 and st3 */
   101        assert(st1 != NULL);
   102        assert(st2 != NULL);
   103        assert(st3 != NULL);
   104        assert(best_rating1 != 0 || best_rating2 != 0 || best_rating3 != 0);
   105
   106        /* Then determine the amount the worst stations get. We do it this way as the
   107         * best should get a bonus, which in this case is the rounding difference from
   108         * this calculation. In reality that will mean the bonus will be pretty low.
   109         * Nevertheless, the best station should always get the most cargo regardless
   110         * of rounding issues. */
   111        uint cargo_st3 = amount * best_rating3 / (best_rating1 + best_rating2 + best_rating3);
   112        assert(cargo_st3 <= (amount - cargo_st3));
   113
   114        uint remaining = amount - cargo_st3;
   115        uint cargo_st2 = remaining * best_rating2 / (best_rating1 + best_rating2);
   116        assert(cargo_st2 <= (remaining - cargo_st2));
   117
   118        uint cargo_st1 = remaining - cargo_st2;
   119        assert(amount == cargo_st1 + cargo_st2 + cargo_st3);
   120
   121        /* And then send the cargo to the stations! */
   122        uint moved = UpdateStationWaiting(st1, type, cargo_st1, source_type, source_id);
   123        moved += UpdateStationWaiting(st2, type, cargo_st2, source_type, source_id);
   124        return moved + UpdateStationWaiting(st3, type, cargo_st3, source_type, source_id);
   125    }
   126
   127    if (st5 == NULL) {
   128        /* Four stations around, the best four (highest rating) are in st1, st2, st3 and st4 */
   129        assert(st1 != NULL);
   130        assert(st2 != NULL);
   131        assert(st3 != NULL);
   132        assert(st4 != NULL);
   133        assert(best_rating1 != 0 || best_rating2 != 0 || best_rating3 != 0 || best_rating4 != 0);
   134
   135        /* Then determine the amount the worst stations get. We do it this way as the
   136         * best should get a bonus, which in this case is the rounding difference from
   137         * this calculation. In reality that will mean the bonus will be pretty low.
   138         * Nevertheless, the best station should always get the most cargo regardless
   139         * of rounding issues. */
   140        uint cargo_st4 = amount * best_rating4 / (best_rating1 + best_rating2 + best_rating3 + best_rating4);
   141        assert(cargo_st4 <= (amount - cargo_st4));
   142
   143        uint remaining = amount - cargo_st4;
   144        uint cargo_st3 = remaining * best_rating3 / (best_rating1 + best_rating2 + best_rating3);
   145        assert(cargo_st3 <= (remaining - cargo_st3));
   146
   147        remaining -= cargo_st3;
   148        uint cargo_st2 = remaining * best_rating2 / (best_rating1 + best_rating2);
   149        assert(cargo_st2 <= (remaining - cargo_st2));
   150
   151        uint cargo_st1 = remaining - cargo_st2;
   152        assert(amount == cargo_st1 + cargo_st2 + cargo_st3 + cargo_st4);
   153
   154        /* And then send the cargo to the stations! */
   155        uint moved = UpdateStationWaiting(st1, type, cargo_st1, source_type, source_id);
   156        moved += UpdateStationWaiting(st2, type, cargo_st2, source_type, source_id);
   157        moved += UpdateStationWaiting(st3, type, cargo_st3, source_type, source_id);
   158        return moved + UpdateStationWaiting(st4, type, cargo_st4, source_type, source_id);
   159    }
   160
   161    if (st6 == NULL) {
   162        /* Five stations around, the best five (highest rating) are in st1, st2, st3, st4 and st5*/
   163        assert(st1 != NULL);
   164        assert(st2 != NULL);
   165        assert(st3 != NULL);
   166        assert(st4 != NULL);
   167        assert(st5 != NULL);
   168        assert(best_rating1 != 0 || best_rating2 != 0 || best_rating3 != 0 || best_rating4 != 0 || best_rating5 != 0);
   169
   170        /* Then determine the amount the worst stations get. We do it this way as the
   171         * best should get a bonus, which in this case is the rounding difference from
   172         * this calculation. In reality that will mean the bonus will be pretty low.
   173         * Nevertheless, the best station should always get the most cargo regardless
   174         * of rounding issues. */
   175        uint cargo_st5 = amount * best_rating5 / (best_rating1 + best_rating2 + best_rating3 + best_rating4 + best_rating5);
   176        assert(cargo_st5 <= (amount - cargo_st5));
   177
   178        uint remaining = amount - cargo_st5;
   179        uint cargo_st4 = remaining * best_rating4 / (best_rating1 + best_rating2 + best_rating3 + best_rating4);
   180        assert(cargo_st4 <= (remaining - cargo_st4));
   181
   182        remaining -= cargo_st4;
   183        uint cargo_st3 = remaining * best_rating3 / (best_rating1 + best_rating2 + best_rating3);
   184        assert(cargo_st3 <= (remaining - cargo_st3));
   185
   186        remaining -= cargo_st3;
   187        uint cargo_st2 = remaining * best_rating2 / (best_rating1 + best_rating2);
   188        assert(cargo_st2 <= (remaining - cargo_st2));
   189
   190        uint cargo_st1 = remaining - cargo_st2;
   191        assert(amount == cargo_st1 + cargo_st2 + cargo_st3 + cargo_st4 + cargo_st5);
   192
   193        /* And then send the cargo to the stations! */
   194        uint moved = UpdateStationWaiting(st1, type, cargo_st1, source_type, source_id);
   195        moved += UpdateStationWaiting(st2, type, cargo_st2, source_type, source_id);
   196        moved += UpdateStationWaiting(st3, type, cargo_st3, source_type, source_id);
   197        moved += UpdateStationWaiting(st4, type, cargo_st4, source_type, source_id);
   198        return moved + UpdateStationWaiting(st5, type, cargo_st5, source_type, source_id);
   199    }
   200
   201    /* Six stations around, the best six (highest rating) are in st1, st2, st3, st4, st5 and st6 */
82    assert(st1 != NULL);  202    assert(st1 != NULL);  
83    assert(st2 != NULL);  203    assert(st2 != NULL);  
84    assert(st3 != NULL);  204    assert(st3 != NULL);  
85    assert(best_rating1 != 0 || best_rating2 != 0 || best_rating3 !=0);  205    assert(st4 != NULL);
86  206    assert(st5 != NULL);
87    /* Then determine the amount the worst station gets. We do it this way as the  207    assert(st6 != NULL);
   208    assert(best_rating1 != 0 || best_rating2 != 0 || best_rating3 != 0 || best_rating4 != 0 || best_rating5 != 0 || best_rating6 != 0);
   209
   210    /* Then determine the amount the worst stations get. We do it this way as the
88     * best should get a bonus, which in this case is the rounding difference from  211     * best should get a bonus, which in this case is the rounding difference from  
89     * this calculation. In reality that will mean the bonus will be pretty low.  212     * this calculation. In reality that will mean the bonus will be pretty low.  
90     * Nevertheless, the best station should always get the most cargo regardless  213     * Nevertheless, the best station should always get the most cargo regardless  
91     * of rounding issues. */  214     * of rounding issues. */  
92    uint worst_cargo_st3 = amount * best_rating3 / (best_rating1 + best_rating2 + best_rating3);  215    uint cargo_st6 = amount * best_rating6 / (best_rating1 + best_rating2 + best_rating3 + best_rating4 + best_rating5 + best_rating6);
93    assert(worst_cargo_st3 <= (amount - worst_cargo_st3));  216    assert(cargo_st6 <= (amount - cargo_st6));
94  217
95    uint remaining_amount = amount - worst_cargo_st3;  218    uint remaining = amount - cargo_st6;
96    uint worst_cargo_st2 = remaining_amount * best_rating2 / (best_rating1 + best_rating2);  219    uint cargo_st5 = remaining * best_rating5 / (best_rating1 + best_rating2 + best_rating3 + best_rating4 + best_rating5);
97    assert(worst_cargo_st2 <= (remaining_amount - worst_cargo_st2));  220    assert(cargo_st5 <= (remaining - cargo_st5));
   221
   222    remaining -= cargo_st5;
   223    uint cargo_st4 = remaining * best_rating4 / (best_rating1 + best_rating2 + best_rating3 + best_rating4);
   224    assert(cargo_st4 <= (remaining - cargo_st4));
   225
   226    remaining -= cargo_st4;
   227    uint cargo_st3 = remaining * best_rating3 / (best_rating1 + best_rating2 + best_rating3);
   228    assert(cargo_st3 <= (remaining - cargo_st3));
   229
   230    remaining -= cargo_st3;
   231    uint cargo_st2 = remaining * best_rating2 / (best_rating1 + best_rating2);
   232    assert(cargo_st2 <= (remaining - cargo_st2));
   233
   234    uint cargo_st1 = remaining - cargo_st2;
   235    assert(amount == cargo_st1 + cargo_st2 + cargo_st3 + cargo_st4 + cargo_st5 + cargo_st6);
98  236  
99    /* And then send the cargo to the stations! */  237    /* And then send the cargo to the stations! */  
100    uint moved = UpdateStationWaiting(st1, type, remaining_amount - worst_cargo_st2, source_type, source_id);  238    uint moved = UpdateStationWaiting(st1, type, cargo_st1, source_type, source_id);
101    /* HALP - I GOT 3 UpdateStationWaitings now! */  239    moved += UpdateStationWaiting(st2, type, cargo_st2, source_type, source_id);
102    moved += UpdateStationWaiting(st2, type, worst_cargo_st2, source_type, source_id);  240    moved += UpdateStationWaiting(st3, type, cargo_st3, source_type, source_id);
103    return moved + UpdateStationWaiting(st3, type, worst_cargo_st3, source_type, source_id);  241    moved += UpdateStationWaiting(st4, type, cargo_st4, source_type, source_id);
   242    moved += UpdateStationWaiting(st5, type, cargo_st5, source_type, source_id);
   243    return moved + UpdateStationWaiting(st6, type, cargo_st6, source_type, source_id);
104} 244}