* Helper to run the refit cost callback. * @param v The vehicle we are refitting, can be nullptr. * @param engine_type Which engine to refit * @param new_cargo_type Cargo type we are refitting to. * @param new_subtype New cargo subtype. * @param[out] auto_refit_allowed The refit is allowed as an auto-refit. * @return Price for refitting */
| 271 | * @return Price for refitting |
| 272 | */ |
| 273 | static int GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoType new_cargo_type, uint8_t new_subtype, bool *auto_refit_allowed) |
| 274 | { |
| 275 | /* Prepare callback param with info about the new cargo type. */ |
| 276 | const Engine *e = Engine::Get(engine_type); |
| 277 | |
| 278 | /* Is this vehicle a NewGRF vehicle? */ |
| 279 | if (e->GetGRF() != nullptr) { |
| 280 | const CargoSpec *cs = CargoSpec::Get(new_cargo_type); |
| 281 | uint32_t param1 = (cs->classes.base() << 16) | (new_subtype << 8) | e->GetGRF()->cargo_map[new_cargo_type]; |
| 282 | |
| 283 | uint16_t cb_res = GetVehicleCallback(CBID_VEHICLE_REFIT_COST, param1, 0, engine_type, v); |
| 284 | if (cb_res != CALLBACK_FAILED) { |
| 285 | *auto_refit_allowed = HasBit(cb_res, 14); |
| 286 | int factor = GB(cb_res, 0, 14); |
| 287 | if (factor >= 0x2000) factor -= 0x4000; // Treat as signed integer. |
| 288 | return factor; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | *auto_refit_allowed = e->info.refit_cost == 0; |
| 293 | return (v == nullptr || v->cargo_type != new_cargo_type) ? e->info.refit_cost : 0; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Learn the price of refitting a certain engine |
no test coverage detected