# vehicle_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: vehicle_cmd.cpp 19024 2010-02-05 21:51:26Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "roadveh.h"
    00014 #include "news_func.h"
    00015 #include "airport.h"
    00016 #include "command_func.h"
    00017 #include "company_func.h"
    00018 #include "vehicle_gui.h"
    00019 #include "train.h"
    00020 #include "aircraft.h"
    00021 #include "newgrf_engine.h"
    00022 #include "newgrf_text.h"
    00023 #include "functions.h"
    00024 #include "window_func.h"
    00025 #include "vehicle_func.h"
    00026 #include "string_func.h"
    00027 #include "depot_map.h"
    00028 #include "vehiclelist.h"
    00029 #include "engine_base.h"
    00030
    00031 #include "table/strings.h"
    00032
    00033 /* Tables used in vehicle.h to find the right command for a certain vehicle type */
    00034 const uint32 _veh_build_proc_table[] = {
    00035   CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
    00036   CMD_BUILD_ROAD_VEH     | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
    00037   CMD_BUILD_SHIP         | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
    00038   CMD_BUILD_AIRCRAFT     | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
    00039 };
    00040
    00041 const uint32 _veh_sell_proc_table[] = {
    00042   CMD_SELL_RAIL_WAGON | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
    00043   CMD_SELL_ROAD_VEH   | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
    00044   CMD_SELL_SHIP       | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
    00045   CMD_SELL_AIRCRAFT   | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
    00046 };
    00047
    00048 const uint32 _veh_refit_proc_table[] = {
    00049   CMD_REFIT_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
    00050   CMD_REFIT_ROAD_VEH     | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
    00051   CMD_REFIT_SHIP         | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
    00052   CMD_REFIT_AIRCRAFT     | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
    00053 };
    00054
    00055 const uint32 _send_to_depot_proc_table[] = {
    00056   /* TrainGotoDepot has a nice randomizer in the pathfinder, which causes desyncs... */
    00057   CMD_SEND_TRAIN_TO_DEPOT     | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT) | CMD_NO_TEST_IF_IN_NETWORK,
    00058   CMD_SEND_ROADVEH_TO_DEPOT   | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
    00059   CMD_SEND_SHIP_TO_DEPOT      | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
    00060   CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
    00061 };
    00062
    00071 CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00072 {
    00073   /* Disable the effect of p2 bit 0, when DC_AUTOREPLACE is not set */
    00074   if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
    00075
    00076   Vehicle *v = Vehicle::GetIfValid(p1);
    00077   if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00078
    00079   switch (v->type) {
    00080     case VEH_TRAIN:
    00081       if ((v->vehstatus & VS_STOPPED) && Train::From(v)->tcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_CATENARY);
    00082       break;
    00083
    00084     case VEH_SHIP:
    00085     case VEH_ROAD:
    00086       break;
    00087
    00088     case VEH_AIRCRAFT: {
    00089       Aircraft *a = Aircraft::From(v);
    00090       /* cannot stop airplane when in flight, or when taking off / landing */
    00091       if (a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
    00092     } break;
    00093
    00094     default: return CMD_ERROR;
    00095   }
    00096
    00097   /* Check if this vehicle can be started/stopped. The callback will fail or
    00098    * return 0xFF if it can. */
    00099   uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
    00100   if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF && HasBit(p2, 0)) {
    00101     StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
    00102     return_cmd_error(error);
    00103   }
    00104
    00105   if (flags & DC_EXEC) {
    00106     if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
    00107
    00108     v->vehstatus ^= VS_STOPPED;
    00109     if (v->type != VEH_TRAIN) v->cur_speed = 0; // trains can stop 'slowly'
    00110     v->MarkDirty();
    00111     SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
    00112     SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
    00113     SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
    00114   }
    00115   return CommandCost();
    00116 }
    00117
    00130 CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00131 {
    00132   VehicleList list;
    00133   VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
    00134   bool start_stop = HasBit(p2, 5);
    00135   bool vehicle_list_window = HasBit(p2, 6);
    00136
    00137   if (vehicle_list_window) {
    00138     uint32 id = p1;
    00139     uint16 window_type = p2 & VLW_MASK;
    00140
    00141     GenerateVehicleSortList(&list, vehicle_type, _current_company, id, window_type);
    00142   } else {
    00143     /* Get the list of vehicles in the depot */
    00144     BuildDepotVehicleList(vehicle_type, tile, &list, NULL);
    00145   }
    00146
    00147   for (uint i = 0; i < list.Length(); i++) {
    00148     const Vehicle *v = list[i];
    00149
    00150     if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
    00151
    00152     if (!vehicle_list_window) {
    00153       if (vehicle_type == VEH_TRAIN) {
    00154         if (!Train::From(v)->IsInDepot()) continue;
    00155       } else {
    00156         if (!(v->vehstatus & VS_HIDDEN)) continue;
    00157       }
    00158     }
    00159
    00160     /* Just try and don't care if some vehicle's can't be stopped. */
    00161     DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
    00162   }
    00163
    00164   return CommandCost();
    00165 }
    00166
    00175 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00176 {
    00177   VehicleList list;
    00178
    00179   CommandCost cost(EXPENSES_NEW_VEHICLES);
    00180   VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
    00181   uint sell_command = GetCmdSellVeh(vehicle_type);
    00182
    00183   /* Get the list of vehicles in the depot */
    00184   BuildDepotVehicleList(vehicle_type, tile, &list, &list);
    00185
    00186   for (uint i = 0; i < list.Length(); i++) {
    00187     CommandCost ret = DoCommand(tile, list[i]->index, 1, flags, sell_command);
    00188     if (ret.Succeeded()) cost.AddCost(ret);
    00189   }
    00190
    00191   if (cost.GetCost() == 0) return CMD_ERROR; // no vehicles to sell
    00192   return cost;
    00193 }
    00194
    00204 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00205 {
    00206   VehicleList list;
    00207   CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
    00208   VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
    00209
    00210   if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
    00211
    00212   /* Get the list of vehicles in the depot */
    00213   BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
    00214
    00215   for (uint i = 0; i < list.Length(); i++) {
    00216     const Vehicle *v = list[i];
    00217
    00218     /* Ensure that the vehicle completely in the depot */
    00219     if (!v->IsInDepot()) continue;
    00220
    00221     CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
    00222
    00223     if (ret.Succeeded()) cost.AddCost(ret);
    00224   }
    00225   return cost;
    00226 }
    00227
    00232 static CommandCost GetRefitCost(EngineID engine_type)
    00233 {
    00234   ExpensesType expense_type;
    00235   const Engine *e = Engine::Get(engine_type);
    00236   Price base_price;
    00237   uint cost_factor = e->info.refit_cost;
    00238   switch (e->type) {
    00239     case VEH_SHIP:
    00240       base_price = PR_BUILD_VEHICLE_SHIP;
    00241       expense_type = EXPENSES_SHIP_RUN;
    00242       break;
    00243
    00244     case VEH_ROAD:
    00245       base_price = PR_BUILD_VEHICLE_ROAD;
    00246       expense_type = EXPENSES_ROADVEH_RUN;
    00247       break;
    00248
    00249     case VEH_AIRCRAFT:
    00250       base_price = PR_BUILD_VEHICLE_AIRCRAFT;
    00251       expense_type = EXPENSES_AIRCRAFT_RUN;
    00252       break;
    00253
    00254     case VEH_TRAIN:
    00255       base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
    00256       cost_factor <<= 1;
    00257       expense_type = EXPENSES_TRAIN_RUN;
    00258       break;
    00259
    00260     default: NOT_REACHED();
    00261   }
    00262   return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grffile, -10));
    00263 }
    00264
    00275 CommandCost RefitVehicle(Vehicle *v, bool only_this, CargoID new_cid, byte new_subtype, DoCommandFlag flags)
    00276 {
    00277   CommandCost cost(v->GetExpenseType(false));
    00278   uint total_capacity = 0;
    00279
    00280   v->InvalidateNewGRFCacheOfChain();
    00281   for (; v != NULL; v = (only_this ? NULL : v->Next())) {
    00282     const Engine *e = Engine::Get(v->engine_type);
    00283     if (!e->CanCarryCargo() || !HasBit(e->info.refit_mask, new_cid)) continue;
    00284
    00285     /* Back up the vehicle's cargo type */
    00286     CargoID temp_cid = v->cargo_type;
    00287     byte temp_subtype = v->cargo_subtype;
    00288     v->cargo_type = new_cid;
    00289     v->cargo_subtype = new_subtype;
    00290
    00291     uint16 mail_capacity;
    00292     uint amount = GetVehicleCapacity(v, &mail_capacity);
    00293     total_capacity += amount;
    00294
    00295     /* Restore the original cargo type */
    00296     v->cargo_type = temp_cid;
    00297     v->cargo_subtype = temp_subtype;
    00298
    00299     if (new_cid != v->cargo_type) {
    00300       cost.AddCost(GetRefitCost(v->engine_type));
    00301     }
    00302
    00303     if (flags & DC_EXEC) {
    00304       v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
    00305       v->cargo_type = new_cid;
    00306       v->cargo_cap = amount;
    00307       v->cargo_subtype = new_subtype;
    00308       if (v->type == VEH_AIRCRAFT) {
    00309         Vehicle *u = v->Next();
    00310         u->cargo_cap = mail_capacity;
    00311         u->cargo.Truncate(mail_capacity);
    00312       }
    00313     }
    00314   }
    00315
    00316   _returned_refit_capacity = total_capacity;
    00317   return cost;
    00318 }
    00319
    00324 static bool IsUniqueVehicleName(const char *name)
    00325 {
    00326   const Vehicle *v;
    00327
    00328   FOR_ALL_VEHICLES(v) {
    00329     if (v->name != NULL && strcmp(v->name, name) == 0) return false;
    00330   }
    00331
    00332   return true;
    00333 }
    00334
    00339 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
    00340 {
    00341   char buf[256];
    00342
    00343   /* Find the position of the first digit in the last group of digits. */
    00344   size_t number_position;
    00345   for (number_position = strlen(src->name); number_position > 0; number_position--) {
    00346     /* The design of UTF-8 lets this work simply without having to check
    00347      * for UTF-8 sequences. */
    00348     if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
    00349   }
    00350
    00351   /* Format buffer and determine starting number. */
    00352   int num;
    00353   if (number_position == strlen(src->name)) {
    00354     /* No digit at the end, so start at number 2. */
    00355     strecpy(buf, src->name, lastof(buf));
    00356     strecat(buf, " ", lastof(buf));
    00357     number_position = strlen(buf);
    00358     num = 2;
    00359   } else {
    00360     /* Found digits, parse them and start at the next number. */
    00361     strecpy(buf, src->name, lastof(buf));
    00362     buf[number_position] = '\0';
    00363     num = strtol(&src->name[number_position], NULL, 10) + 1;
    00364   }
    00365
    00366   /* Check if this name is already taken. */
    00367   for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
    00368     /* Attach the number to the temporary name. */
    00369     seprintf(&buf[number_position], lastof(buf), "%d", num);
    00370
    00371     /* Check the name is unique. */
    00372     if (IsUniqueVehicleName(buf)) {
    00373       dst->name = strdup(buf);
    00374       break;
    00375     }
    00376   }
    00377
    00378   /* All done. If we didn't find a name, it'll just use its default. */
    00379 }
    00380
    00389 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00390 {
    00391   CommandCost total_cost(EXPENSES_NEW_VEHICLES);
    00392   uint32 build_argument = 2;
    00393
    00394   Vehicle *v = Vehicle::GetIfValid(p1);
    00395   if (v == NULL) return CMD_ERROR;
    00396   Vehicle *v_front = v;
    00397   Vehicle *w = NULL;
    00398   Vehicle *w_front = NULL;
    00399   Vehicle *w_rear = NULL;
    00400
    00401   /*
    00402    * v_front is the front engine in the original vehicle
    00403    * v is the car/vehicle of the original vehicle, that is currently being copied
    00404    * w_front is the front engine of the cloned vehicle
    00405    * w is the car/vehicle currently being cloned
    00406    * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
    00407    */
    00408
    00409   if (!CheckOwnership(v->owner)) return CMD_ERROR;
    00410
    00411   if (v->type == VEH_TRAIN && (!Train::From(v)->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
    00412
    00413   /* check that we can allocate enough vehicles */
    00414   if (!(flags & DC_EXEC)) {
    00415     int veh_counter = 0;
    00416     do {
    00417       veh_counter++;
    00418     } while ((v = v->Next()) != NULL);
    00419
    00420     if (!Vehicle::CanAllocateItem(veh_counter)) {
    00421       return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
    00422     }
    00423   }
    00424
    00425   v = v_front;
    00426
    00427   do {
    00428     if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
    00429       /* we build the rear ends of multiheaded trains with the front ones */
    00430       continue;
    00431     }
    00432
    00433     /* In case we're building a multi headed vehicle and the maximum number of
    00434      * vehicles is almost reached (e.g. max trains - 1) not all vehicles would
    00435      * be cloned. When the non-primary engines were build they were seen as
    00436      * 'new' vehicles whereas they would immediately be joined with a primary
    00437      * engine. This caused the vehicle to be not build as 'the limit' had been
    00438      * reached, resulting in partially build vehicles and such. */
    00439     DoCommandFlag build_flags = flags;
    00440     if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
    00441
    00442     CommandCost cost = DoCommand(tile, v->engine_type, build_argument, build_flags, GetCmdBuildVeh(v));
    00443     build_argument = 3; // ensure that we only assign a number to the first engine
    00444
    00445     if (cost.Failed()) {
    00446       /* Can't build a part, then sell the stuff we already made; clear up the mess */
    00447       if (w_front != NULL) DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00448       return cost;
    00449     }
    00450
    00451     total_cost.AddCost(cost);
    00452
    00453     if (flags & DC_EXEC) {
    00454       w = Vehicle::Get(_new_vehicle_id);
    00455
    00456       if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
    00457         SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
    00458       }
    00459
    00460       if (v->type == VEH_TRAIN && !Train::From(v)->IsFrontEngine()) {
    00461         /* this s a train car
    00462          * add this unit to the end of the train */
    00463         CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
    00464         if (result.Failed()) {
    00465           /* The train can't be joined to make the same consist as the original.
    00466            * Sell what we already made (clean up) and return an error.           */
    00467           DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00468           DoCommand(w_front->tile, w->index,       1, flags, GetCmdSellVeh(w));
    00469           return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
    00470         }
    00471       } else {
    00472         /* this is a front engine or not a train. */
    00473         w_front = w;
    00474         w->service_interval = v->service_interval;
    00475       }
    00476       w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
    00477     }
    00478   } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
    00479
    00480   if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
    00481     /* for trains this needs to be the front engine due to the callback function */
    00482     _new_vehicle_id = w_front->index;
    00483   }
    00484
    00485   if (flags & DC_EXEC) {
    00486     /* Cloned vehicles belong to the same group */
    00487     DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
    00488   }
    00489
    00490
    00491   /* Take care of refitting. */
    00492   w = w_front;
    00493   v = v_front;
    00494
    00495   /* Both building and refitting are influenced by newgrf callbacks, which
    00496    * makes it impossible to accurately estimate the cloning costs. In
    00497    * particular, it is possible for engines of the same type to be built with
    00498    * different numbers of articulated parts, so when refitting we have to
    00499    * loop over real vehicles first, and then the articulated parts of those
    00500    * vehicles in a different loop. */
    00501   do {
    00502     do {
    00503       if (flags & DC_EXEC) {
    00504         assert(w != NULL);
    00505
    00506         /* Find out what's the best sub type */
    00507         byte subtype = GetBestFittingSubType(v, w);
    00508         if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
    00509           CommandCost cost = DoCommand(0, w->index, v->cargo_type | (subtype << 8) | 1U << 16, flags, GetCmdRefitVeh(v));
    00510           if (cost.Succeeded()) total_cost.AddCost(cost);
    00511         }
    00512
    00513         if (w->type == VEH_TRAIN && Train::From(w)->HasArticulatedPart()) {
    00514           w = Train::From(w)->GetNextArticPart();
    00515         } else if (w->type == VEH_ROAD && RoadVehicle::From(w)->HasArticulatedPart()) {
    00516           w = w->Next();
    00517         } else {
    00518           break;
    00519         }
    00520       } else {
    00521         const Engine *e = Engine::Get(v->engine_type);
    00522         CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
    00523
    00524         if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
    00525           total_cost.AddCost(GetRefitCost(v->engine_type));
    00526         }
    00527       }
    00528
    00529       if (v->type == VEH_TRAIN && Train::From(v)->HasArticulatedPart()) {
    00530         v = Train::From(v)->GetNextArticPart();
    00531       } else if (v->type == VEH_ROAD && RoadVehicle::From(v)->HasArticulatedPart()) {
    00532         v = v->Next();
    00533       } else {
    00534         break;
    00535       }
    00536     } while (v != NULL);
    00537
    00538     if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = Train::From(w)->GetNextVehicle();
    00539   } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
    00540
    00541   if (flags & DC_EXEC) {
    00542     /*
    00543      * Set the orders of the vehicle. Cannot do it earlier as we need
    00544      * the vehicle refitted before doing this, otherwise the moved
    00545      * cargo types might not match (passenger vs non-passenger)
    00546      */
    00547     DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
    00548
    00549     /* Now clone the vehicle's name, if it has one. */
    00550     if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
    00551   }
    00552
    00553   /* Since we can't estimate the cost of cloning a vehicle accurately we must
    00554    * check whether the company has enough money manually. */
    00555   if (!CheckCompanyHasMoney(total_cost)) {
    00556     if (flags & DC_EXEC) {
    00557       /* The vehicle has already been bought, so now it must be sold again. */
    00558       DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00559     }
    00560     return total_cost;
    00561   }
    00562
    00563   return total_cost;
    00564 }
    00565
    00576 CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
    00577 {
    00578   VehicleList list;
    00579
    00580   GenerateVehicleSortList(&list, type, owner, id, vlw_flag);
    00581
    00582   /* Send all the vehicles to a depot */
    00583   for (uint i = 0; i < list.Length(); i++) {
    00584     const Vehicle *v = list[i];
    00585     CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
    00586
    00587     /* Return 0 if DC_EXEC is not set this is a valid goto depot command)
    00588      * In this case we know that at least one vehicle can be sent to a depot
    00589      * and we will issue the command. We can now safely quit the loop, knowing
    00590      * it will succeed at least once. With DC_EXEC we really need to send them to the depot */
    00591     if (ret.Succeeded() && !(flags & DC_EXEC)) {
    00592       return CommandCost();
    00593     }
    00594   }
    00595
    00596   return (flags & DC_EXEC) ? CommandCost() : CMD_ERROR;
    00597 }
    00598
    00607 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00608 {
    00609   Vehicle *v = Vehicle::GetIfValid(p1);
    00610   if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
    00611
    00612   bool reset = StrEmpty(text);
    00613
    00614   if (!reset) {
    00615     if (strlen(text) >= MAX_LENGTH_VEHICLE_NAME_BYTES) return CMD_ERROR;
    00616     if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
    00617   }
    00618
    00619   if (flags & DC_EXEC) {
    00620     free(v->name);
    00621     v->name = reset ? NULL : strdup(text);
    00622     InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
    00623     MarkWholeScreenDirty();
    00624   }
    00625
    00626   return CommandCost();
    00627 }
    00628
    00629
    00638 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00639 {
    00640   Vehicle *v = Vehicle::GetIfValid(p1);
    00641   if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
    00642
    00643   uint16 serv_int = GetServiceIntervalClamped(p2, v->owner); // Double check the service interval from the user-input
    00644   if (serv_int != p2) return CMD_ERROR;
    00645
    00646   if (flags & DC_EXEC) {
    00647     v->service_interval = serv_int;
    00648     SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
    00649   }
    00650
    00651   return CommandCost();
    00652 }