vehicle_cmd.cpp

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

Generated on Wed Dec 30 20:40:08 2009 for OpenTTD by  doxygen 1.5.6