* Sell a vehicle. * @param flags for command. * @param v_id vehicle ID being sold. * @param sell_chain sell the vehicle and all vehicles following it in the chain. * @param backup_order make a backup of the vehicle's order (if an engine). * @param client_id User. * @return the cost of this operation or an error. */
| 234 | * @return the cost of this operation or an error. |
| 235 | */ |
| 236 | CommandCost CmdSellVehicle(DoCommandFlags flags, VehicleID v_id, bool sell_chain, bool backup_order, ClientID client_id) |
| 237 | { |
| 238 | Vehicle *v = Vehicle::GetIfValid(v_id); |
| 239 | if (v == nullptr) return CMD_ERROR; |
| 240 | |
| 241 | Vehicle *front = v->First(); |
| 242 | |
| 243 | CommandCost ret = CheckOwnership(front->owner); |
| 244 | if (ret.Failed()) return ret; |
| 245 | |
| 246 | if (front->vehstatus.Test(VehState::Crashed)) return CommandCost(STR_ERROR_VEHICLE_IS_DESTROYED); |
| 247 | |
| 248 | if (!front->IsStoppedInDepot()) return CommandCost(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type); |
| 249 | |
| 250 | if (v->type == VEH_TRAIN) { |
| 251 | ret = CmdSellRailWagon(flags, v, sell_chain, backup_order, client_id); |
| 252 | } else { |
| 253 | ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value); |
| 254 | |
| 255 | if (flags.Test(DoCommandFlag::Execute)) { |
| 256 | if (front->IsPrimaryVehicle() && backup_order) OrderBackup::Backup(front, client_id); |
| 257 | delete front; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Helper to run the refit cost callback. |
nothing calls this directly
no test coverage detected