tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@3959: #include "roadveh.h" tron@3961: #include "ship.h" truelight@0: #include "news.h" rubidium@8254: #include "player_func.h" truelight@0: #include "engine.h" celestar@1601: #include "debug.h" matthijs@1752: #include "vehicle_gui.h" matthijs@1758: #include "depot.h" bjarni@2676: #include "train.h" bjarni@4662: #include "aircraft.h" peter1138@6113: #include "cargotype.h" rubidium@6643: #include "group.h" smatz@8064: #include "order.h" rubidium@8114: #include "strings_func.h" rubidium@8116: #include "command_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8131: #include "functions.h" rubidium@8211: #include "variables.h" rubidium@8212: #include "autoreplace_func.h" bjarni@8363: #include "articulated_vehicles.h" bjarni@2244: rubidium@8264: #include "table/strings.h" rubidium@8264: bjarni@2599: /* bjarni@2599: * move the cargo from one engine to another if possible bjarni@2599: */ bjarni@2599: static void MoveVehicleCargo(Vehicle *dest, Vehicle *source) bjarni@2599: { bjarni@2599: Vehicle *v = dest; bjarni@2599: bjarni@2599: do { bjarni@2599: do { bjarni@2599: if (source->cargo_type != dest->cargo_type) rubidium@4434: continue; // cargo not compatible bjarni@2599: rubidium@7010: if (dest->cargo.Count() == dest->cargo_cap) rubidium@4434: continue; // the destination vehicle is already full bjarni@2599: rubidium@7010: uint units_moved = min(source->cargo.Count(), dest->cargo_cap - dest->cargo.Count()); rubidium@7010: source->cargo.MoveTo(&dest->cargo, units_moved); bjarni@2599: bjarni@2599: // copy the age of the cargo bjarni@2599: dest->day_counter = source->day_counter; bjarni@2599: dest->tick_counter = source->tick_counter; bjarni@2599: rubidium@7492: } while (source->cargo.Count() > 0 && (dest = dest->Next()) != NULL); bjarni@2599: dest = v; rubidium@7492: } while ((source = source->Next()) != NULL); rubidium@5693: rubidium@5693: /* rubidium@5693: * The of the train will be incorrect at this moment. This is due rubidium@5693: * to the fact that removing the old wagon updates the weight of rubidium@5693: * the complete train, which is without the weight of cargo we just rubidium@5693: * moved back into some (of the) new wagon(s). rubidium@5693: */ rubidium@7497: if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->First()); bjarni@2599: } bjarni@2599: bjarni@4741: static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, const EngineID engine_type) bjarni@4741: { bjarni@4741: const Order *o; bjarni@4741: const Vehicle *u; bjarni@4741: rubidium@6259: if (v->type == VEH_TRAIN) { rubidium@7497: u = v->First(); bjarni@4741: } else { bjarni@4741: u = v; bjarni@4741: } bjarni@4741: bjarni@4741: FOR_VEHICLE_ORDERS(u, o) { bjarni@4782: if (!(o->refit_cargo < NUM_CARGO)) continue; bjarni@4741: if (!CanRefitTo(v->engine_type, o->refit_cargo)) continue; bjarni@4741: if (!CanRefitTo(engine_type, o->refit_cargo)) return false; bjarni@4741: } bjarni@4741: bjarni@4741: return true; bjarni@4741: } bjarni@4741: bjarni@4554: /** bjarni@4554: * Function to find what type of cargo to refit to when autoreplacing bjarni@4554: * @param *v Original vehicle, that is being replaced bjarni@4554: * @param engine_type The EngineID of the vehicle that is being replaced to bjarni@4554: * @return The cargo type to replace to bjarni@4554: * CT_NO_REFIT is returned if no refit is needed bjarni@4554: * CT_INVALID is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible bjarni@4554: */ bjarni@4554: static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type) bjarni@4554: { bjarni@6505: CargoID new_cargo_type = GetEngineCargoType(engine_type); bjarni@4554: bjarni@6505: if (new_cargo_type == CT_INVALID) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity bjarni@4554: bjarni@8362: if (v->cargo_cap != 0 && (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->cargo_type))) { bjarni@4741: if (VerifyAutoreplaceRefitForOrders(v, engine_type)) { rubidium@5587: return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->cargo_type; bjarni@4741: } else { bjarni@4741: return CT_INVALID; bjarni@4741: } bjarni@4741: } rubidium@6259: if (v->type != VEH_TRAIN) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want bjarni@4554: bjarni@4554: /* Below this line it's safe to assume that the vehicle in question is a train */ bjarni@4554: bjarni@4554: if (v->cargo_cap != 0) return CT_INVALID; // trying to replace a vehicle with cargo capacity into another one with incompatible cargo type bjarni@4554: bjarni@4554: /* the old engine didn't have cargo capacity, but the new one does bjarni@4554: * now we will figure out what cargo the train is carrying and refit to fit this */ rubidium@7497: v = v->First(); bjarni@4554: do { bjarni@4554: if (v->cargo_cap == 0) continue; bjarni@4554: /* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */ bjarni@4554: if (v->cargo_type == new_cargo_type) return CT_NO_REFIT; bjarni@4554: if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type; rubidium@7492: } while ((v = v->Next()) != NULL); bjarni@4554: return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one bjarni@4554: } bjarni@4554: bjarni@2552: /* Replaces a vehicle (used to be called autorenew) tron@2639: * This function is only called from MaybeReplaceVehicle() bjarni@2552: * Must be called with _current_player set to the owner of the vehicle bjarni@2552: * @param w Vehicle to replace bjarni@2552: * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts tron@2639: * @return value is cost of the replacement or CMD_ERROR bjarni@2552: */ rubidium@6990: static CommandCost ReplaceVehicle(Vehicle **w, byte flags, Money total_cost) bjarni@2552: { rubidium@6943: CommandCost cost; rubidium@6943: CommandCost sell_value; bjarni@2599: Vehicle *old_v = *w; bjarni@2552: const Player *p = GetPlayer(old_v->owner); bjarni@2552: EngineID new_engine_type; bjarni@2552: const UnitID cached_unitnumber = old_v->unitnumber; bjarni@2552: bool new_front = false; bjarni@2552: Vehicle *new_v = NULL; peter1138@8258: char *vehicle_name = NULL; bjarni@4554: CargoID replacement_cargo_type; bjarni@2552: rubidium@6643: /* If the vehicle belongs to a group, check if the group is protected from the global autoreplace. rubidium@6643: * If not, chek if an global auto replacement is defined */ rubidium@6643: new_engine_type = (IsValidGroupID(old_v->group_id) && GetGroup(old_v->group_id)->replace_protection) ? rubidium@6643: INVALID_ENGINE : rubidium@7258: EngineReplacementForPlayer(p, old_v->engine_type, ALL_GROUP); rubidium@6643: rubidium@6643: /* If we don't set new_egnine_type previously, we try to check if an autoreplacement was defined rubidium@6643: * for the group and the engine_type of the vehicle */ rubidium@7258: if (new_engine_type == INVALID_ENGINE && !IsAllGroupID(old_v->group_id)) { rubidium@6643: new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type, old_v->group_id); rubidium@6643: } bjarni@2552: rubidium@6650: if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type; rubidium@6650: bjarni@4554: replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type); bjarni@4554: bjarni@4554: /* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */ rubidium@6950: if (replacement_cargo_type == CT_INVALID) return CommandCost(); bjarni@4554: bjarni@5792: sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v)); bjarni@4728: bjarni@4728: /* We give the player a loan of the same amount as the sell value. bjarni@4728: * This is needed in case he needs the income from the sale to build the new vehicle. bjarni@4728: * We take it back if building fails or when we really sell the old engine */ bjarni@4728: SubtractMoneyFromPlayer(sell_value); bjarni@4728: bjarni@5792: cost = DoCommand(old_v->tile, new_engine_type, 3, flags, GetCmdBuildVeh(old_v)); bjarni@4728: if (CmdFailed(cost)) { rubidium@6950: /* Take back the money we just gave the player */ rubidium@6950: sell_value.MultiplyCost(-1); rubidium@6950: SubtractMoneyFromPlayer(sell_value); bjarni@4728: return cost; bjarni@4728: } bjarni@2552: bjarni@6746: if (replacement_cargo_type != CT_NO_REFIT) { bjarni@6746: /* add refit cost */ rubidium@6943: CommandCost refit_cost = GetRefitCost(new_engine_type); bjarni@8363: if (old_v->type == VEH_TRAIN && RailVehInfo(new_engine_type)->railveh_type == RAILVEH_MULTIHEAD) { bjarni@8363: /* Since it's a dualheaded engine we have to pay once more because the rear end is being refitted too. */ bjarni@8363: refit_cost.AddCost(refit_cost); bjarni@8363: } rubidium@6950: cost.AddCost(refit_cost); bjarni@6746: } bjarni@4554: bjarni@2552: if (flags & DC_EXEC) { bjarni@2564: new_v = GetVehicle(_new_vehicle_id); rubidium@4434: *w = new_v; //we changed the vehicle, so MaybeReplaceVehicle needs to work on the new one. Now we tell it what the new one is bjarni@2552: bjarni@2552: /* refit if needed */ bjarni@4554: if (replacement_cargo_type != CT_NO_REFIT) { bjarni@5792: if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) { bjarni@4613: /* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */ bjarni@4613: error("Autoreplace failed to refit. Replace engine %d to %d and refit to cargo %d", old_v->engine_type, new_v->engine_type, replacement_cargo_type); bjarni@4611: } bjarni@2552: } bjarni@4611: skidd13@7928: if (new_v->type == VEH_TRAIN && HasBit(old_v->u.rail.flags, VRF_REVERSE_DIRECTION) && !IsMultiheaded(new_v) && !(new_v->Next() != NULL && IsArticulatedPart(new_v->Next()))) { bjarni@3896: // we are autorenewing to a single engine, so we will turn it as the old one was turned as well skidd13@7931: SetBit(new_v->u.rail.flags, VRF_REVERSE_DIRECTION); bjarni@3896: } bjarni@2552: rubidium@6259: if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) { bjarni@2552: /* this is a railcar. We need to move the car into the train bjarni@2552: * We add the new engine after the old one instead of replacing it. It will give the same result anyway when we bjarni@2552: * sell the old engine in a moment bjarni@2552: */ bjarni@6735: /* Get the vehicle in front of the one we move out */ rubidium@7497: Vehicle *front = old_v->Previous(); bjarni@6735: /* If the vehicle in front is the rear end of a dualheaded engine, then we need to use the one in front of that one */ bjarni@7526: if (IsRearDualheaded(front)) front = front->Previous(); bjarni@2678: /* Now we move the old one out of the train */ tron@3491: DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@6706: /* Add the new vehicle */ bjarni@6706: DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@2552: } else { bjarni@2552: // copy/clone the orders tron@3491: DoCommand(0, (old_v->index << 16) | new_v->index, IsOrderListShared(old_v) ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER); bjarni@2552: new_v->cur_order_index = old_v->cur_order_index; bjarni@2552: ChangeVehicleViewWindow(old_v, new_v); bjarni@2575: new_v->profit_this_year = old_v->profit_this_year; bjarni@2575: new_v->profit_last_year = old_v->profit_last_year; bjarni@3679: new_v->service_interval = old_v->service_interval; rubidium@6647: DoCommand(0, old_v->group_id, new_v->index, flags, CMD_ADD_VEHICLE_GROUP); bjarni@2552: new_front = true; rubidium@4434: new_v->unitnumber = old_v->unitnumber; // use the same unit number bjarni@6551: new_v->dest_tile = old_v->dest_tile; bjarni@2552: bjarni@2552: new_v->current_order = old_v->current_order; rubidium@6259: if (old_v->type == VEH_TRAIN && GetNextVehicle(old_v) != NULL){ bjarni@2842: Vehicle *temp_v = GetNextVehicle(old_v); bjarni@2842: bjarni@2842: // move the entire train to the new engine, excluding the old engine bjarni@2842: if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) { bjarni@2842: // we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead bjarni@2842: temp_v = GetNextVehicle(temp_v); bjarni@2842: } bjarni@2842: bjarni@2842: if (temp_v != NULL) { tron@3491: DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@2681: } bjarni@2552: } bjarni@2552: } bjarni@2678: /* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */ rubidium@7497: MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v); peter1138@2716: peter1138@2716: // Get the name of the old vehicle if it has a custom name. peter1138@8258: if (old_v->name != NULL) vehicle_name = strdup(old_v->name); bjarni@4554: } else { // flags & DC_EXEC not set rubidium@6950: CommandCost tmp_move; bjarni@7524: bjarni@7524: if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) { bjarni@7527: Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts bjarni@7524: if (next_veh != NULL) { bjarni@7524: /* Verify that the wagons can be placed on the engine in question. bjarni@7524: * This is done by building an engine, test if the wagons can be added and then sell the test engine. */ bjarni@7524: DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(old_v)); bjarni@7524: Vehicle *temp = GetVehicle(_new_vehicle_id); bjarni@7524: tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE); bjarni@7524: DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v)); bjarni@7524: } bjarni@6800: } bjarni@6800: bjarni@4554: /* Ensure that the player will not end up having negative money while autoreplacing bjarni@4554: * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */ rubidium@6952: if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) { rubidium@6950: /* Pay back the loan */ rubidium@6950: sell_value.MultiplyCost(-1); rubidium@6950: SubtractMoneyFromPlayer(sell_value); bjarni@4728: return CMD_ERROR; bjarni@4728: } bjarni@2552: } bjarni@2552: bjarni@4728: /* Take back the money we just gave the player just before building the vehicle bjarni@4728: * The player will get the same amount now that the sale actually takes place */ rubidium@6950: sell_value.MultiplyCost(-1); rubidium@6950: SubtractMoneyFromPlayer(sell_value); bjarni@4728: bjarni@4554: /* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */ rubidium@6950: cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v))); bjarni@2552: bjarni@2552: if (new_front) { bjarni@4554: /* now we assign the old unitnumber to the new vehicle */ bjarni@2552: new_v->unitnumber = cached_unitnumber; bjarni@2552: } bjarni@2552: bjarni@4554: /* Transfer the name of the old vehicle */ peter1138@8258: if ((flags & DC_EXEC) && vehicle_name != NULL) { peter1138@2716: _cmd_text = vehicle_name; tron@3491: DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE); peter1138@8258: free(vehicle_name); peter1138@2716: } peter1138@2716: bjarni@2552: return cost; bjarni@2552: } bjarni@2552: tron@2639: /** replaces a vehicle if it's set for autoreplace or is too old tron@2639: * (used to be called autorenew) tron@2639: * @param v The vehicle to replace rubidium@4434: * if the vehicle is a train, v needs to be the front engine bjarni@4662: * @param check Checks if the replace is valid. No action is done at all bjarni@4662: * @param display_costs If set, a cost animation is shown (only if check is false) bjarni@4662: * @return CMD_ERROR if something went wrong. Otherwise the price of the replace tron@2639: */ rubidium@6943: CommandCost MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs) bjarni@2552: { bjarni@2552: Vehicle *w; bjarni@2552: const Player *p = GetPlayer(v->owner); bjarni@2552: byte flags = 0; rubidium@6950: CommandCost cost, temp_cost; bjarni@5686: bool stopped; tron@2805: tron@2805: /* Remember the length in case we need to trim train later on bjarni@3175: * If it's not a train, the value is unused bjarni@3175: * round up to the length of the tiles used for the train instead of the train length instead bjarni@3175: * Useful when newGRF uses custom length */ rubidium@6259: uint16 old_total_length = (v->type == VEH_TRAIN ? celestar@3422: (v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE : celestar@3422: -1 celestar@3422: ); bjarni@3175: bjarni@2552: bjarni@2552: _current_player = v->owner; bjarni@2552: bjarni@5794: assert(IsPlayerBuildableVehicleType(v)); bjarni@2579: rubidium@4434: assert(v->vehstatus & VS_STOPPED); // the vehicle should have been stopped in VehicleEnteredDepotThisTick() if needed bjarni@2590: bjarni@5686: /* Remember the flag v->leave_depot_instantly because if we replace the vehicle, the vehicle holding this flag will be sold bjarni@5686: * If it is set, then we only stopped the vehicle to replace it (if needed) and we will need to start it again. bjarni@5686: * We also need to reset the flag since it should remain false except from when the vehicle enters a depot until autoreplace is handled in the same tick */ bjarni@5686: stopped = v->leave_depot_instantly; bjarni@5686: v->leave_depot_instantly = false; bjarni@2552: tron@2639: for (;;) { rubidium@8230: cost = CommandCost(EXPENSES_NEW_VEHICLES); bjarni@2552: w = v; bjarni@2552: do { bjarni@7526: if (w->type == VEH_TRAIN && IsRearDualheaded(w)) { bjarni@2676: /* we build the rear ends of multiheaded trains with the front ones */ bjarni@2676: continue; bjarni@2676: } bjarni@2676: bjarni@2552: // check if the vehicle should be replaced tron@2639: if (!p->engine_renew || tron@2639: w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old tron@2639: w->max_age == 0) { // rail cars got a max age of 0 rubidium@6643: /* If the vehicle belongs to a group, check if the group is protected from the global autoreplace. rubidium@6643: If not, chek if an global auto remplacement is defined */ rubidium@6643: if (IsValidGroupID(w->group_id)) { rubidium@6643: if (!EngineHasReplacementForPlayer(p, w->engine_type, w->group_id) && ( rubidium@6643: GetGroup(w->group_id)->replace_protection || rubidium@7258: !EngineHasReplacementForPlayer(p, w->engine_type, ALL_GROUP))) { rubidium@6643: continue; rubidium@6643: } rubidium@7258: } else if (IsDefaultGroupID(w->group_id)) { rubidium@7258: if (!EngineHasReplacementForPlayer(p, w->engine_type, DEFAULT_GROUP) && rubidium@7258: !EngineHasReplacementForPlayer(p, w->engine_type, ALL_GROUP)) { rubidium@7258: continue; rubidium@7258: } rubidium@7258: } else if (!EngineHasReplacementForPlayer(p, w->engine_type, ALL_GROUP)) { bjarni@2552: continue; rubidium@6643: } bjarni@2552: } bjarni@2552: bjarni@2552: /* Now replace the vehicle */ rubidium@6950: temp_cost = ReplaceVehicle(&w, flags, cost.GetCost()); bjarni@2552: bjarni@6800: if (CmdFailed(temp_cost)) break; // replace failed for some reason. Leave the vehicle alone bjarni@6800: tron@2639: if (flags & DC_EXEC && rubidium@6259: (w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE)) { tron@2639: /* now we bought a new engine and sold the old one. We need to fix the tron@2639: * pointers in order to avoid pointing to the old one for trains: these tron@2639: * pointers should point to the front engine and not the cars tron@2639: */ bjarni@2552: v = w; bjarni@2552: } rubidium@6950: cost.AddCost(temp_cost); rubidium@6259: } while (w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL); bjarni@2552: rubidium@6952: if (!(flags & DC_EXEC) && (p->player_money < (cost.GetCost() + p->engine_renew_money) || cost.GetCost() == 0)) { rubidium@6952: if (!check && p->player_money < (cost.GetCost() + p->engine_renew_money) && ( _local_player == v->owner ) && cost.GetCost() != 0) { bjarni@2552: StringID message; bjarni@2552: SetDParam(0, v->unitnumber); bjarni@2552: switch (v->type) { rubidium@6259: case VEH_TRAIN: message = STR_TRAIN_AUTORENEW_FAILED; break; rubidium@6259: case VEH_ROAD: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break; rubidium@6259: case VEH_SHIP: message = STR_SHIP_AUTORENEW_FAILED; break; rubidium@6259: case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED; break; bjarni@2552: // This should never happen bjarni@2552: default: NOT_REACHED(); message = 0; break; bjarni@2552: } bjarni@2552: bjarni@2552: AddNewsItem(message, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); bjarni@2552: } tron@2639: if (stopped) v->vehstatus &= ~VS_STOPPED; bjarni@4676: if (display_costs) _current_player = OWNER_NONE; bjarni@4662: return CMD_ERROR; bjarni@2552: } bjarni@2552: bjarni@2552: if (flags & DC_EXEC) { rubidium@4434: break; // we are done replacing since the loop ran once with DC_EXEC bjarni@4662: } else if (check) { bjarni@4662: /* It's a test only and we know that we can do this bjarni@4662: * NOTE: payment for wagon removal is NOT included in this price */ bjarni@4662: return cost; bjarni@2552: } bjarni@2552: // now we redo the loop, but this time we actually do stuff since we know that we can do it bjarni@2552: flags |= DC_EXEC; bjarni@2552: } bjarni@2552: tron@2805: /* If setting is on to try not to exceed the old length of the train with the replacement */ rubidium@6259: if (v->type == VEH_TRAIN && p->renew_keep_length) { bjarni@2617: Vehicle *temp; bjarni@2617: w = v; tron@2805: tron@2805: while (v->u.rail.cached_total_length > old_total_length) { bjarni@2617: // the train is too long. We will remove cars one by one from the start of the train until it's short enough belugas@5868: while (w != NULL && RailVehInfo(w->engine_type)->railveh_type != RAILVEH_WAGON) { bjarni@2617: w = GetNextVehicle(w); bjarni@2617: } bjarni@2617: if (w == NULL) { bjarni@2617: // we failed to make the train short enough bjarni@2617: SetDParam(0, v->unitnumber); bjarni@2617: AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); bjarni@2617: break; bjarni@2617: } bjarni@2617: temp = w; bjarni@2617: w = GetNextVehicle(w); tron@3491: DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@2678: MoveVehicleCargo(v, temp); rubidium@6950: cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON)); bjarni@2617: } bjarni@2617: } bjarni@2617: tron@2639: if (stopped) v->vehstatus &= ~VS_STOPPED; bjarni@4662: if (display_costs) { rubidium@6950: if (IsLocalPlayer()) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost()); bjarni@4662: _current_player = OWNER_NONE; bjarni@4662: } bjarni@4662: return cost; bjarni@2552: }