# vehicle_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: vehicle_cmd.cpp 18866 2010-01-18 22:57:21Z 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   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 (ret.Succeeded()) {
    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 (ret.Succeeded()) 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 (ret.Succeeded()) {
    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
    00311   v->InvalidateNewGRFCacheOfChain();
    00312   for (; v != NULL; v = (only_this ? NULL : v->Next())) {
    00313     const Engine *e = Engine::Get(v->engine_type);
    00314     if (!e->CanCarryCargo() || !HasBit(e->info.refit_mask, new_cid)) continue;
    00315
    00316     /* Back up the vehicle's cargo type */
    00317     CargoID temp_cid = v->cargo_type;
    00318     byte temp_subtype = v->cargo_subtype;
    00319     v->cargo_type = new_cid;
    00320     v->cargo_subtype = new_subtype;
    00321
    00322     uint16 mail_capacity;
    00323     uint amount = GetVehicleCapacity(v, &mail_capacity);
    00324     total_capacity += amount;
    00325
    00326     /* Restore the original cargo type */
    00327     v->cargo_type = temp_cid;
    00328     v->cargo_subtype = temp_subtype;
    00329
    00330     if (new_cid != v->cargo_type) {
    00331       cost.AddCost(GetRefitCost(v->engine_type));
    00332     }
    00333
    00334     if (flags & DC_EXEC) {
    00335       v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
    00336       v->cargo_type = new_cid;
    00337       v->cargo_cap = amount;
    00338       v->cargo_subtype = new_subtype;
    00339       if (v->type == VEH_AIRCRAFT) {
    00340         Vehicle *u = v->Next();
    00341         u->cargo_cap = mail_capacity;
    00342         u->cargo.Truncate(mail_capacity);
    00343       }
    00344     }
    00345   }
    00346
    00347   _returned_refit_capacity = total_capacity;
    00348   return cost;
    00349 }
    00350
    00355 static bool IsUniqueVehicleName(const char *name)
    00356 {
    00357   const Vehicle *v;
    00358
    00359   FOR_ALL_VEHICLES(v) {
    00360     if (v->name != NULL && strcmp(v->name, name) == 0) return false;
    00361   }
    00362
    00363   return true;
    00364 }
    00365
    00370 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
    00371 {
    00372   char buf[256];
    00373
    00374   /* Find the position of the first digit in the last group of digits. */
    00375   size_t number_position;
    00376   for (number_position = strlen(src->name); number_position > 0; number_position--) {
    00377     /* The design of UTF-8 lets this work simply without having to check
    00378      * for UTF-8 sequences. */
    00379     if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
    00380   }
    00381
    00382   /* Format buffer and determine starting number. */
    00383   int num;
    00384   if (number_position == strlen(src->name)) {
    00385     /* No digit at the end, so start at number 2. */
    00386     strecpy(buf, src->name, lastof(buf));
    00387     strecat(buf, " ", lastof(buf));
    00388     number_position = strlen(buf);
    00389     num = 2;
    00390   } else {
    00391     /* Found digits, parse them and start at the next number. */
    00392     strecpy(buf, src->name, lastof(buf));
    00393     buf[number_position] = '\0';
    00394     num = strtol(&src->name[number_position], NULL, 10) + 1;
    00395   }
    00396
    00397   /* Check if this name is already taken. */
    00398   for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
    00399     /* Attach the number to the temporary name. */
    00400     seprintf(&buf[number_position], lastof(buf), "%d", num);
    00401
    00402     /* Check the name is unique. */
    00403     if (IsUniqueVehicleName(buf)) {
    00404       dst->name = strdup(buf);
    00405       break;
    00406     }
    00407   }
    00408
    00409   /* All done. If we didn't find a name, it'll just use its default. */
    00410 }
    00411
    00420 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00421 {
    00422   CommandCost total_cost(EXPENSES_NEW_VEHICLES);
    00423   uint32 build_argument = 2;
    00424
    00425   Vehicle *v = Vehicle::GetIfValid(p1);
    00426   if (v == NULL) return CMD_ERROR;
    00427   Vehicle *v_front = v;
    00428   Vehicle *w = NULL;
    00429   Vehicle *w_front = NULL;
    00430   Vehicle *w_rear = NULL;
    00431
    00432   /*
    00433    * v_front is the front engine in the original vehicle
    00434    * v is the car/vehicle of the original vehicle, that is currently being copied
    00435    * w_front is the front engine of the cloned vehicle
    00436    * w is the car/vehicle currently being cloned
    00437    * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
    00438    */
    00439
    00440   if (!CheckOwnership(v->owner)) return CMD_ERROR;
    00441
    00442   if (v->type == VEH_TRAIN && (!Train::From(v)->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
    00443
    00444   /* check that we can allocate enough vehicles */
    00445   if (!(flags & DC_EXEC)) {
    00446     int veh_counter = 0;
    00447     do {
    00448       veh_counter++;
    00449     } while ((v = v->Next()) != NULL);
    00450
    00451     if (!Vehicle::CanAllocateItem(veh_counter)) {
    00452       return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
    00453     }
    00454   }
    00455
    00456   v = v_front;
    00457
    00458   do {
    00459     if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
    00460       /* we build the rear ends of multiheaded trains with the front ones */
    00461       continue;
    00462     }
    00463
    00464     /* In case we're building a multi headed vehicle and the maximum number of
    00465      * vehicles is almost reached (e.g. max trains - 1) not all vehicles would
    00466      * be cloned. When the non-primary engines were build they were seen as
    00467      * 'new' vehicles whereas they would immediately be joined with a primary
    00468      * engine. This caused the vehicle to be not build as 'the limit' had been
    00469      * reached, resulting in partially build vehicles and such. */
    00470     DoCommandFlag build_flags = flags;
    00471     if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
    00472
    00473     CommandCost cost = DoCommand(tile, v->engine_type, build_argument, build_flags, GetCmdBuildVeh(v));
    00474     build_argument = 3; // ensure that we only assign a number to the first engine
    00475
    00476     if (cost.Failed()) {
    00477       /* Can't build a part, then sell the stuff we already made; clear up the mess */
    00478       if (w_front != NULL) DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00479       return cost;
    00480     }
    00481
    00482     total_cost.AddCost(cost);
    00483
    00484     if (flags & DC_EXEC) {
    00485       w = Vehicle::Get(_new_vehicle_id);
    00486
    00487       if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
    00488         SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
    00489       }
    00490
    00491       if (v->type == VEH_TRAIN && !Train::From(v)->IsFrontEngine()) {
    00492         /* this s a train car
    00493          * add this unit to the end of the train */
    00494         CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
    00495         if (result.Failed()) {
    00496           /* The train can't be joined to make the same consist as the original.
    00497            * Sell what we already made (clean up) and return an error.           */
    00498           DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00499           DoCommand(w_front->tile, w->index,       1, flags, GetCmdSellVeh(w));
    00500           return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
    00501         }
    00502       } else {
    00503         /* this is a front engine or not a train. */
    00504         w_front = w;
    00505         w->service_interval = v->service_interval;
    00506       }
    00507       w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
    00508     }
    00509   } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
    00510
    00511   if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
    00512     /* for trains this needs to be the front engine due to the callback function */
    00513     _new_vehicle_id = w_front->index;
    00514   }
    00515
    00516   if (flags & DC_EXEC) {
    00517     /* Cloned vehicles belong to the same group */
    00518     DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
    00519   }
    00520
    00521
    00522   /* Take care of refitting. */
    00523   w = w_front;
    00524   v = v_front;
    00525
    00526   /* Both building and refitting are influenced by newgrf callbacks, which
    00527    * makes it impossible to accurately estimate the cloning costs. In
    00528    * particular, it is possible for engines of the same type to be built with
    00529    * different numbers of articulated parts, so when refitting we have to
    00530    * loop over real vehicles first, and then the articulated parts of those
    00531    * vehicles in a different loop. */
    00532   do {
    00533     do {
    00534       if (flags & DC_EXEC) {
    00535         assert(w != NULL);
    00536
    00537         /* Find out what's the best sub type */
    00538         byte subtype = GetBestFittingSubType(v, w);
    00539         if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
    00540           CommandCost cost = DoCommand(0, w->index, v->cargo_type | (subtype << 8) | 1U << 16, flags, GetCmdRefitVeh(v));
    00541           if (cost.Succeeded()) total_cost.AddCost(cost);
    00542         }
    00543
    00544         if (w->type == VEH_TRAIN && Train::From(w)->HasArticulatedPart()) {
    00545           w = Train::From(w)->GetNextArticPart();
    00546         } else if (w->type == VEH_ROAD && RoadVehicle::From(w)->HasArticulatedPart()) {
    00547           w = w->Next();
    00548         } else {
    00549           break;
    00550         }
    00551       } else {
    00552         const Engine *e = Engine::Get(v->engine_type);
    00553         CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
    00554
    00555         if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
    00556           total_cost.AddCost(GetRefitCost(v->engine_type));
    00557         }
    00558       }
    00559
    00560       if (v->type == VEH_TRAIN && Train::From(v)->HasArticulatedPart()) {
    00561         v = Train::From(v)->GetNextArticPart();
    00562       } else if (v->type == VEH_ROAD && RoadVehicle::From(v)->HasArticulatedPart()) {
    00563         v = v->Next();
    00564       } else {
    00565         break;
    00566       }
    00567     } while (v != NULL);
    00568
    00569     if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = Train::From(w)->GetNextVehicle();
    00570   } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
    00571
    00572   if (flags & DC_EXEC) {
    00573     /*
    00574      * Set the orders of the vehicle. Cannot do it earlier as we need
    00575      * the vehicle refitted before doing this, otherwise the moved
    00576      * cargo types might not match (passenger vs non-passenger)
    00577      */
    00578     DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
    00579
    00580     /* Now clone the vehicle's name, if it has one. */
    00581     if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
    00582   }
    00583
    00584   /* Since we can't estimate the cost of cloning a vehicle accurately we must
    00585    * check whether the company has enough money manually. */
    00586   if (!CheckCompanyHasMoney(total_cost)) {
    00587     if (flags & DC_EXEC) {
    00588       /* The vehicle has already been bought, so now it must be sold again. */
    00589       DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
    00590     }
    00591     return total_cost;
    00592   }
    00593
    00594   return total_cost;
    00595 }
    00596
    00607 CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
    00608 {
    00609   VehicleList list;
    00610
    00611   GenerateVehicleSortList(&list, type, owner, id, vlw_flag);
    00612
    00613   /* Send all the vehicles to a depot */
    00614   for (uint i = 0; i < list.Length(); i++) {
    00615     const Vehicle *v = list[i];
    00616     CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
    00617
    00618     /* Return 0 if DC_EXEC is not set this is a valid goto depot command)
    00619      * In this case we know that at least one vehicle can be sent to a depot
    00620      * and we will issue the command. We can now safely quit the loop, knowing
    00621      * it will succeed at least once. With DC_EXEC we really need to send them to the depot */
    00622     if (ret.Succeeded() && !(flags & DC_EXEC)) {
    00623       return CommandCost();
    00624     }
    00625   }
    00626
    00627   return (flags & DC_EXEC) ? CommandCost() : CMD_ERROR;
    00628 }
    00629
    00638 CommandCost CmdRenameVehicle(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   bool reset = StrEmpty(text);
    00644
    00645   if (!reset) {
    00646     if (strlen(text) >= MAX_LENGTH_VEHICLE_NAME_BYTES) return CMD_ERROR;
    00647     if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
    00648   }
    00649
    00650   if (flags & DC_EXEC) {
    00651     free(v->name);
    00652     v->name = reset ? NULL : strdup(text);
    00653     InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
    00654     MarkWholeScreenDirty();
    00655   }
    00656
    00657   return CommandCost();
    00658 }
    00659
    00660
    00669 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00670 {
    00671   Vehicle *v = Vehicle::GetIfValid(p1);
    00672   if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
    00673
    00674   uint16 serv_int = GetServiceIntervalClamped(p2, v->owner); // Double check the service interval from the user-input
    00675   if (serv_int != p2) return CMD_ERROR;
    00676
    00677   if (flags & DC_EXEC) {
    00678     v->service_interval = serv_int;
    00679     SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
    00680   }
    00681
    00682   return CommandCost();
    00683 }