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 */
|
---|
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);
|
---|
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);
|
---|