Loading

Paste #p2pn4keww

  1. uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)
  2. {
  3.     /* Return if nothing to do. Also the rounding below fails for 0. */
  4.     if (amount == 0) return 0;
  5.  
  6.     Station *st1 = NULL;   // Station with best rating
  7.     Station *st2 = NULL;   // Second 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
  12.     uint best_rating1 = 0; // rating of st1
  13.     uint best_rating2 = 0; // rating of st2
  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
  18.  
  19.     for (Station * const *st_iter = all_stations->Begin(); st_iter != all_stations->End(); ++st_iter) {
  20.         Station *st = *st_iter;
  21.  
  22.         /* Is the station reserved exclusively for somebody else? */
  23.         if (st->town->exclusive_counter > 0 && st->town->exclusivity != st->owner) continue;
  24.  
  25.         if (st->goods[type].rating == 0) continue; // Lowest possible rating, better not to give cargo anymore
  26.  
  27.         if (_settings_game.order.selectgoods && !st->goods[type].HasVehicleEverTriedLoading()) continue; // Selectively servicing stations, and not this one
  28.  
  29.         if (IsCargoInClass(type, CC_PASSENGERS)) {
  30.             if (st->facilities == FACIL_TRUCK_STOP) continue; // passengers are never served by just a truck stop
  31.         } else {
  32.             if (st->facilities == FACIL_BUS_STOP) continue; // non-passengers are never served by just a bus stop
  33.         }
  34.  
  35.         /* This station can be used, add it to st1/st2/st3/st4/st5/st6 */
  36.         if (st1 == NULL || st->goods[type].rating >= best_rating1) {
  37.             st6 = st5; best_rating6 = best_rating5;
  38.             st5 = st4; best_rating5 = best_rating4;
  39.             st4 = st3; best_rating4 = best_rating3;
  40.             st3 = st2; best_rating3 = best_rating2;
  41.             st2 = st1; best_rating2 = best_rating1;
  42.             st1 = st; best_rating1 = st->goods[type].rating;
  43.         } else if (st2 == NULL || st->goods[type].rating >= best_rating2) {
  44.             st6 = st5; best_rating6 = best_rating5;
  45.             st5 = st4; best_rating5 = best_rating4;
  46.             st4 = st3; best_rating4 = best_rating3;
  47.             st3 = st2; best_rating3 = best_rating2;
  48.             st2 = st; best_rating2 = st->goods[type].rating;
  49.         } else if (st3 == NULL || st->goods[type].rating >= best_rating3) {
  50.             st6 = st5; best_rating6 = best_rating5;
  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;
  63.         }
  64.     }
  65.  
  66.     /* no stations around at all? */
  67.     if (st1 == NULL) return 0;
  68.  
  69.     /* From now we'll calculate with fractal cargo amounts.
  70.      * First determine how much cargo we really have. */
  71.     amount *= best_rating1 + 1;
  72.  
  73.     if (st2 == NULL) {
  74.         /* only one station around */
  75.         return UpdateStationWaiting(st1, type, amount, source_type, source_id);
  76.     }
  77.  
  78.     if (st3 == NULL) {
  79.         /* two stations around, the best two (highest rating) are in st1 and st2 */
  80.         assert(st1 != NULL);
  81.         assert(st2 != NULL);
  82.         assert(best_rating1 != 0 || best_rating2 != 0);
  83.  
  84.         /* Then determine the amount the worst station gets. We do it this way as the
  85.          * best should get a bonus, which in this case is the rounding difference from
  86.          * this calculation. In reality that will mean the bonus will be pretty low.
  87.          * Nevertheless, the best station should always get the most cargo regardless
  88.          * of rounding issues. */
  89.         uint worst_cargo = amount * best_rating2 / (best_rating1 + best_rating2);
  90.         assert(worst_cargo <= (amount - worst_cargo));
  91.  
  92.         /* And then send the cargo to the stations! */
  93.         uint moved = UpdateStationWaiting(st1, type, amount - worst_cargo, source_type, source_id);
  94.         /* These two UpdateStationWaiting's can't be in the statement as then the order
  95.          * of execution would be undefined and that could cause desyncs with callbacks. */
  96.         return moved + UpdateStationWaiting(st2, type, worst_cargo, source_type, source_id);
  97.     }
  98.  
  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 */
  202.     assert(st1 != NULL);
  203.     assert(st2 != NULL);
  204.     assert(st3 != NULL);
  205.     assert(st4 != NULL);
  206.     assert(st5 != NULL);
  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
  211.      * best should get a bonus, which in this case is the rounding difference from
  212.      * this calculation. In reality that will mean the bonus will be pretty low.
  213.      * Nevertheless, the best station should always get the most cargo regardless
  214.      * of rounding issues. */
  215.     uint cargo_st6 = amount * best_rating6 / (best_rating1 + best_rating2 + best_rating3 + best_rating4 + best_rating5 + best_rating6);
  216.     assert(cargo_st6 <= (amount - cargo_st6));
  217.  
  218.     uint remaining = amount - cargo_st6;
  219.     uint cargo_st5 = remaining * best_rating5 / (best_rating1 + best_rating2 + best_rating3 + best_rating4 + best_rating5);
  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);
  236.  
  237.     /* And then send the cargo to the stations! */
  238.     uint moved = UpdateStationWaiting(st1, type, cargo_st1, source_type, source_id);
  239.     moved += UpdateStationWaiting(st2, type, cargo_st2, source_type, source_id);
  240.     moved += UpdateStationWaiting(st3, type, 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);
  244. }

Version history

Revision # Author Created at
pfsmdufv4 Anonymous 24 Oct 2017, 18:35:28 UTC Diff

Comments