MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / HandleRefit

Method HandleRefit

src/linkgraph/refresh.cpp:68–118  ·  view source on GitHub ↗

* Handle refit orders by updating capacities and refit_capacities. * @param refit_cargo Cargo to refit to. * @return True if any vehicle was refit; false if none was. */

Source from the content-addressed store, hash-verified

66 * @return True if any vehicle was refit; false if none was.
67 */
68bool LinkRefresher::HandleRefit(CargoType refit_cargo)
69{
70 this->cargo = refit_cargo;
71 RefitList::iterator refit_it = this->refit_capacities.begin();
72 bool any_refit = false;
73 for (Vehicle *v = this->vehicle; v != nullptr; v = v->Next()) {
74 const Engine *e = Engine::Get(v->engine_type);
75 if (!HasBit(e->info.refit_mask, this->cargo)) {
76 ++refit_it;
77 continue;
78 }
79 any_refit = true;
80
81 /* Back up the vehicle's cargo type */
82 CargoType temp_cargo_type = v->cargo_type;
83 uint8_t temp_subtype = v->cargo_subtype;
84 v->cargo_type = this->cargo;
85 v->cargo_subtype = GetBestFittingSubType(v, v, this->cargo);
86
87 uint16_t mail_capacity = 0;
88 uint amount = e->DetermineCapacity(v, &mail_capacity);
89
90 /* Restore the original cargo type */
91 v->cargo_type = temp_cargo_type;
92 v->cargo_subtype = temp_subtype;
93
94 /* Skip on next refit. */
95 if (this->cargo != refit_it->cargo && refit_it->remaining > 0) {
96 this->capacities[refit_it->cargo] -= refit_it->remaining;
97 refit_it->remaining = 0;
98 } else if (amount < refit_it->remaining) {
99 this->capacities[refit_it->cargo] -= refit_it->remaining - amount;
100 refit_it->remaining = amount;
101 }
102 refit_it->capacity = amount;
103 refit_it->cargo = this->cargo;
104
105 ++refit_it;
106
107 /* Special case for aircraft with mail. */
108 if (v->type == VEH_AIRCRAFT) {
109 if (mail_capacity < refit_it->remaining) {
110 this->capacities[refit_it->cargo] -= refit_it->remaining - mail_capacity;
111 refit_it->remaining = mail_capacity;
112 }
113 refit_it->capacity = mail_capacity;
114 break; // aircraft have only one vehicle
115 }
116 }
117 return any_refit;
118}
119
120/**
121 * Restore capacities and refit_capacities as vehicle might have been able to load now.

Callers 1

RefreshLinksMethod · 0.95

Calls 5

HasBitFunction · 0.85
GetBestFittingSubTypeFunction · 0.85
DetermineCapacityMethod · 0.80
beginMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected