# vehicle_cmd.cpp

Go to the documentation of this file.

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