# vehicle_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: vehicle_cmd.cpp 18498 2009-12-14 19:17:18Z 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 "gfx_func.h"
    00015 #include "news_func.h"
    00016 #include "airport.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
    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   CommandCost return_value = CMD_ERROR;
    00134   VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
    00135   bool start_stop = HasBit(p2, 5);
    00136   bool vehicle_list_window = HasBit(p2, 6);
    00137
    00138   if (vehicle_list_window) {
    00139     uint32 id = p1;
    00140     uint16 window_type = p2 & VLW_MASK;
    00141
    00142     GenerateVehicleSortList(&list, vehicle_type, _current_company, id, window_type);
    00143   } else {
    00144     /* Get the list of vehicles in the depot */
    00145     BuildDepotVehicleList(vehicle_type, tile, &list, NULL);
    00146   }
    00147
    00148   for (uint i = 0; i < list.Length(); i++) {
    00149     const Vehicle *v = list[i];
    00150
    00151     if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
    00152
    00153     if (!vehicle_list_window) {
    00154       if (vehicle_type == VEH_TRAIN) {
    00155         if (!Train::From(v)->IsInDepot()) continue;
    00156       } else {
    00157         if (!(v->vehstatus & VS_HIDDEN)) continue;
    00158       }
    00159     }
    00160
    00161     CommandCost ret = DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
    00162
    00163     if (CmdSucceeded(ret)) {
    00164       return_value = CommandCost();
    00165       /* We know that the command is valid for at least one vehicle.
    00166        * If we haven't set DC_EXEC, then there is no point in continueing because it will be valid */
    00167       if (!(flags & DC_EXEC)) break;
    00168     }
    00169   }
    00170
    00171   return return_value;
    00172 }
    00173
    00182 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00183 {
    00184   VehicleList list;
    00185
    00186   CommandCost cost(EXPENSES_NEW_VEHICLES);
    00187   VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
    00188   uint sell_command = GetCmdSellVeh(vehicle_type);
    00189
    00190   /* Get the list of vehicles in the depot */
    00191   BuildDepotVehicleList(vehicle_type, tile, &list, &list);
    00192
    00193   for (uint i = 0; i < list.Length(); i++) {
    00194     CommandCost ret = DoCommand(tile, list[i]->index, 1, flags, sell_command);
    00195     if (CmdSucceeded(ret)) cost.AddCost(ret);
    00196   }
    00197
    00198   if (cost.GetCost() == 0) return CMD_ERROR; // no vehicles to sell
    00199   return cost;
    00200 }
    00201
    00213 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00214 {
    00215   VehicleList list;
    00216   CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
    00217   VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
    00218   bool all_or_nothing = HasBit(p2, 0);
    00219
    00220   if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
    00221
    00222   /* Get the list of vehicles in the depot */
    00223   BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
    00224
    00225   bool did_something = false;
    00226
    00227   for (uint i = 0; i < list.Length(); i++) {
    00228     const Vehicle *v = list[i];
    00229
    00230     /* Ensure that the vehicle completely in the depot */
    00231     if (!v->IsInDepot()) continue;
    00232
    00233     CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
    00234
    00235     if (CmdSucceeded(ret)) {
    00236       did_something = true;
    00237       cost.AddCost(ret);
    00238     } else {
    00239       if (ret.GetErrorMessage() != STR_ERROR_AUTOREPLACE_NOTHING_TO_DO && all_or_nothing) {
    00240         /* We failed to replace a vehicle even though we set all or nothing.
    00241          * We should never reach this if DC_EXEC is set since then it should
    00242          * have failed the estimation guess. */
    00243         assert(!(flags & DC_EXEC));
    00244         /* Now we will have to return an error. */
    00245         return CMD_ERROR;
    00246       }
    00247     }
    00248   }
    00249
    00250   if (!did_something) {
    00251     /* Either we didn't replace anything or something went wrong.
    00252      * Either way we want to return an error and not execute this command. */
    00253     cost = CMD_ERROR;
    00254   }
    00255
    00256   return cost;
    00257 }
    00258
    00263 static CommandCost GetRefitCost(EngineID engine_type)
    00264 {
    00265   ExpensesType expense_type;
    00266   const Engine *e = Engine::Get(engine_type);
    00267   Price base_price;
    00268   uint cost_factor = e->info.refit_cost;
    00269   switch (e->type) {
    00270     case VEH_SHIP:
    00271       base_price = PR_BUILD_VEHICLE_SHIP;
    00272       expense_type = EXPENSES_SHIP_RUN;
    00273       break;
    00274
    00275     case VEH_ROAD:
    00276       base_price = PR_BUILD_VEHICLE_ROAD;
    00277       expense_type = EXPENSES_ROADVEH_RUN;
    00278       break;
    00279
    00280     case VEH_AIRCRAFT:
    00281       base_price = PR_BUILD_VEHICLE_AIRCRAFT;
    00282       expense_type = EXPENSES_AIRCRAFT_RUN;
    00283       break;
    00284
    00285     case VEH_TRAIN:
    00286       base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
    00287       cost_factor <<= 1;
    00288       expense_type = EXPENSES_TRAIN_RUN;
    00289       break;
    00290
    00291     default: NOT_REACHED();
    00292   }
    00293   return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grffile, -10));
    00294 }
    00295
    00306 CommandCost RefitVehicle(Vehicle *v, bool only_this, CargoID new_cid, byte new_subtype, DoCommandFlag flags)
    00307 {
    00308   CommandCost cost(v->GetExpenseType(false));
    00309   uint total_capacity = 0;
    00310   bool success = false;
    00311
    00312   v->InvalidateNewGRFCacheOfChain();
    00313   for (; v != NULL; v = (only_this ? NULL : v->Next())) {
    00314     const Engine *e = Engine::Get(v->engine_type);
    00315     if (!e->CanCarryCargo() || !HasBit(e->info.refit_mask, new_cid)) continue;
    00316     success = true;
    00317
    00318     /* Back up the vehicle's cargo type */
    00319     CargoID temp_cid = v->cargo_type;
    00320     byte temp_subtype = v->cargo_subtype;
    00321     v->cargo_type = new_cid;
    00322     v->cargo_subtype = new_subtype;
    00323
    00324     uint16 mail_capacity;
    00325     uint amount = GetVehicleCapacity(v, &mail_capacity);
    00326     total_capacity += amount;
    00327
    00328     /* Restore the original cargo type */
    00329     v->cargo_type = temp_cid;
    00330     v->cargo_subtype = temp_subtype;
    00331
    00332     if (new_cid != v->cargo_type) {
    00333       cost.AddCost(GetRefitCost(v->engine_type));
    00334     }
    00335
    00336     if (flags & DC_EXEC) {
    00337       v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
    00338       v->cargo_type = new_cid;
    00339       v->cargo_cap = amount;
    00340       v->cargo_subtype = new_subtype;
    00341       if (v->type == VEH_AIRCRAFT) {
    00342         Vehicle *u = v->Next();
    00343         u->cargo_cap = mail_capacity;
    00344         u->cargo.Truncate(mail_capacity);
    00345       }
    00346     }
    00347   }
    00348
    00349   _returned_refit_capacity = total_capacity;
    00350   return success ? cost : CMD_ERROR;
    00351 }
    00352
    00357 static bool IsUniqueVehicleName(const char *name)
    00358 {
    00359   const Vehicle *v;
    00360
    00361   FOR_ALL_VEHICLES(v) {
    00362     if (v->name != NULL && strcmp(v->name, name) == 0) return false;
    00363   }
    00364
    00365   return true;
    00366 }
    00367
    00372 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
    00373 {
    00374   char buf[256];
    00375
    00376   /* Find the position of the first digit in the last group of digits. */
    00377   size_t number_position;
    00378   for (number_position = strlen(src->name); number_position > 0; number_position--) {
    00379     /* The design of UTF-8 lets this work simply without having to check
    00380      * for UTF-8 sequences. */
    00381     if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
    00382   }
    00383
    00384   /* Format buffer and determine starting number. */
    00385   int num;
    00386   if (number_position == strlen(src->name)) {
    00387     /* No digit at the end, so start at number 2. */
    00388     strecpy(buf, src->name, lastof(buf));
    00389     strecat(buf, " ", lastof(buf));
    00390     number_position = strlen(buf);
    00391     num = 2;
    00392   } else {
    00393     /* Found digits, parse them and start at the next number. */
    00394     strecpy(buf, src->name, lastof(buf));
    00395     buf[number_position] = '\0';
    00396     num = strtol(&src->name[number_position], NULL, 10) + 1;
    00397   }
    00398
    00399   /* Check if this name is already taken. */
    00400   for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
    00401     /* Attach the number to the temporary name. */
    00402     seprintf(&buf[number_position], lastof(buf), "%d", num);
    00403
    00404     /* Check the name is unique. */
    00405     if (IsUniqueVehicleName(buf)) {
    00406       dst->name = strdup(buf);
    00407       break;
    00408     }
    00409   }
    00410
    00411   /* All done. If we didn't find a name, it'll just use its default. */
    00412 }
    00413
    00422 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00423 {
    00424   CommandCost total_cost(EXPENSES_NEW_VEHICLES);
    00425   uint32 build_argument = 2;
    00426
    00427   Vehicle *v = Vehicle::GetIfValid(p1);
    00428   if (v == NULL) return CMD_ERROR;
    00429   Vehicle *v_front = v;
    00430   Vehicle *w = NULL;
    00431   Vehicle *w_front = NULL;
    00432   Vehicle *w_rear = NULL;
    00433
    00434   /*
    00435    * v_front is the front engine in the original vehicle
    00436    * v is the car/vehicle of the original vehicle, that is currently being copied
    00437    * w_front is the front engine of the cloned vehicle
    00438    * w is the car/vehicle currently being cloned
    00439    * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
    00440    */
    00441
    00442   if (!CheckOwnership(v->owner)) return CMD_ERROR;
    00443
    00444   if (v->type == VEH_TRAIN && (!Train::From(v)->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
    00445
    00446   /* check that we can allocate enough vehicles */
    00447   if (!(flags & DC_EXEC)) {
    00448     int veh_counter = 0;
    00449     do {
    00450       veh_counter++;
    00451     } while ((v = v->Next()) != NULL);
    00452
    00453     if (!Vehicle::CanAllocateItem(veh_counter)) {
    00454       return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
    00455     }
    00456   }
    00457
    00458   v = v_front;
    00459
    00460   do {
    00461     if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
    00462       /* we build the rear ends of multiheaded trains with the front ones */
    00463       continue;
    00464     }
    00465
    00466     /* In case we're building a multi headed vehicle and the maximum number of
    00467      * vehicles is almost reached (e.g. max trains - 1) not all vehicles would
    00468      * be cloned. When the non-primary engines were build they were seen as
    00469      * 'new' vehicles whereas they would immediately be joined with a primary
    00470      * engine. This caused the vehicle to be not build as 'the limit' had been
    00471      * reached, resulting in partially build vehicles and such. */
    00472     DoCommandFlag build_flags = flags;
    00473     if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
    00474
    00475     CommandCost cost = DoCommand(tile, v->engine_type, build_argument, build_flags, GetCmdBuildVeh(v));
    00476     build_argument = 3; // ensure that we only assign a number to the first engine
    00477
    00478     if (CmdFailed(cost)) {
    00479       /* Can't build a part, then sell the stuff we already made; clear up the mess */
    00480       if (w_front != NULL) DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00481       return cost;
    00482     }
    00483
    00484     total_cost.AddCost(cost);
    00485
    00486     if (flags & DC_EXEC) {
    00487       w = Vehicle::Get(_new_vehicle_id);
    00488
    00489       if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
    00490         SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
    00491       }
    00492
    00493       if (v->type == VEH_TRAIN && !Train::From(v)->IsFrontEngine()) {
    00494         /* this s a train car
    00495          * add this unit to the end of the train */
    00496         CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
    00497         if (CmdFailed(result)) {
    00498           /* The train can't be joined to make the same consist as the original.
    00499            * Sell what we already made (clean up) and return an error.           */
    00500           DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00501           DoCommand(w_front->tile, w->index,       1, flags, GetCmdSellVeh(w));
    00502           return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
    00503         }
    00504       } else {
    00505         /* this is a front engine or not a train. */
    00506         w_front = w;
    00507         w->service_interval = v->service_interval;
    00508       }
    00509       w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
    00510     }
    00511   } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
    00512
    00513   if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
    00514     /* for trains this needs to be the front engine due to the callback function */
    00515     _new_vehicle_id = w_front->index;
    00516   }
    00517
    00518   if (flags & DC_EXEC) {
    00519     /* Cloned vehicles belong to the same group */
    00520     DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
    00521   }
    00522
    00523
    00524   /* Take care of refitting. */
    00525   w = w_front;
    00526   v = v_front;
    00527
    00528   /* Both building and refitting are influenced by newgrf callbacks, which
    00529    * makes it impossible to accurately estimate the cloning costs. In
    00530    * particular, it is possible for engines of the same type to be built with
    00531    * different numbers of articulated parts, so when refitting we have to
    00532    * loop over real vehicles first, and then the articulated parts of those
    00533    * vehicles in a different loop. */
    00534   do {
    00535     do {
    00536       if (flags & DC_EXEC) {
    00537         assert(w != NULL);
    00538
    00539         /* Find out what's the best sub type */
    00540         byte subtype = GetBestFittingSubType(v, w);
    00541         if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
    00542           CommandCost cost = DoCommand(0, w->index, v->cargo_type | (subtype << 8) | 1U << 16, flags, GetCmdRefitVeh(v));
    00543           if (CmdSucceeded(cost)) total_cost.AddCost(cost);
    00544         }
    00545
    00546         if (w->type == VEH_TRAIN && Train::From(w)->HasArticulatedPart()) {
    00547           w = Train::From(w)->GetNextArticPart();
    00548         } else if (w->type == VEH_ROAD && RoadVehicle::From(w)->HasArticulatedPart()) {
    00549           w = w->Next();
    00550         } else {
    00551           break;
    00552         }
    00553       } else {
    00554         const Engine *e = Engine::Get(v->engine_type);
    00555         CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
    00556
    00557         if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
    00558           total_cost.AddCost(GetRefitCost(v->engine_type));
    00559         }
    00560       }
    00561
    00562       if (v->type == VEH_TRAIN && Train::From(v)->HasArticulatedPart()) {
    00563         v = Train::From(v)->GetNextArticPart();
    00564       } else if (v->type == VEH_ROAD && RoadVehicle::From(v)->HasArticulatedPart()) {
    00565         v = v->Next();
    00566       } else {
    00567         break;
    00568       }
    00569     } while (v != NULL);
    00570
    00571     if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = Train::From(w)->GetNextVehicle();
    00572   } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
    00573
    00574   if (flags & DC_EXEC) {
    00575     /*
    00576      * Set the orders of the vehicle. Cannot do it earlier as we need
    00577      * the vehicle refitted before doing this, otherwise the moved
    00578      * cargo types might not match (passenger vs non-passenger)
    00579      */
    00580     DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
    00581
    00582     /* Now clone the vehicle's name, if it has one. */
    00583     if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
    00584   }
    00585
    00586   /* Since we can't estimate the cost of cloning a vehicle accurately we must
    00587    * check whether the company has enough money manually. */
    00588   if (!CheckCompanyHasMoney(total_cost)) {
    00589     if (flags & DC_EXEC) {
    00590       /* The vehicle has already been bought, so now it must be sold again. */
    00591       DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00592     }
    00593     return CMD_ERROR;
    00594   }
    00595
    00596   return total_cost;
    00597 }
    00598
    00609 CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
    00610 {
    00611   VehicleList list;
    00612
    00613   GenerateVehicleSortList(&list, type, owner, id, vlw_flag);
    00614
    00615   /* Send all the vehicles to a depot */
    00616   for (uint i = 0; i < list.Length(); i++) {
    00617     const Vehicle *v = list[i];
    00618     CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
    00619
    00620     /* Return 0 if DC_EXEC is not set this is a valid goto depot command)
    00621      * In this case we know that at least one vehicle can be sent to a depot
    00622      * and we will issue the command. We can now safely quit the loop, knowing
    00623      * it will succeed at least once. With DC_EXEC we really need to send them to the depot */
    00624     if (CmdSucceeded(ret) && !(flags & DC_EXEC)) {
    00625       return CommandCost();
    00626     }
    00627   }
    00628
    00629   return (flags & DC_EXEC) ? CommandCost() : CMD_ERROR;
    00630 }
    00631
    00640 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00641 {
    00642   Vehicle *v = Vehicle::GetIfValid(p1);
    00643   if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
    00644
    00645   bool reset = StrEmpty(text);
    00646
    00647   if (!reset) {
    00648     if (strlen(text) >= MAX_LENGTH_VEHICLE_NAME_BYTES) return CMD_ERROR;
    00649     if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
    00650   }
    00651
    00652   if (flags & DC_EXEC) {
    00653     free(v->name);
    00654     v->name = reset ? NULL : strdup(text);
    00655     InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
    00656     MarkWholeScreenDirty();
    00657   }
    00658
    00659   return CommandCost();
    00660 }
    00661
    00662
    00671 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00672 {
    00673   Vehicle *v = Vehicle::GetIfValid(p1);
    00674   if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
    00675
    00676   uint16 serv_int = GetServiceIntervalClamped(p2, v->owner); // Double check the service interval from the user-input
    00677   if (serv_int != p2) return CMD_ERROR;
    00678
    00679   if (flags & DC_EXEC) {
    00680     v->service_interval = serv_int;
    00681     SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
    00682   }
    00683
    00684   return CommandCost();
    00685 }