00001
00002
00003
00004
00005
00006
00007
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_text.h"
00023 #include "window_func.h"
00024 #include "vehicle_func.h"
00025 #include "string_func.h"
00026 #include "depot_map.h"
00027 #include "vehiclelist.h"
00028 #include "engine_func.h"
00029 #include "articulated_vehicles.h"
00030 #include "autoreplace_gui.h"
00031 #include "company_base.h"
00032 #include "order_backup.h"
00033 #include "ship.h"
00034 #include "cargodest_func.h"
00035
00036 #include "table/strings.h"
00037
00038
00039 const uint32 _veh_build_proc_table[] = {
00040 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
00041 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
00042 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
00043 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
00044 };
00045
00046 const uint32 _veh_sell_proc_table[] = {
00047 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
00048 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
00049 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
00050 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
00051 };
00052
00053 const uint32 _veh_refit_proc_table[] = {
00054 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
00055 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
00056 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
00057 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
00058 };
00059
00060 const uint32 _send_to_depot_proc_table[] = {
00061
00062 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT) | CMD_NO_TEST_IF_IN_NETWORK,
00063 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
00064 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
00065 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
00066 };
00067
00068
00069 CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00070 CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00071 CommandCost CmdBuildShip (TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00072 CommandCost CmdBuildAircraft (TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00073
00085 CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00086 {
00087
00088 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
00089
00090 VehicleType type;
00091 switch (GetTileType(tile)) {
00092 case MP_RAILWAY: type = VEH_TRAIN; break;
00093 case MP_ROAD: type = VEH_ROAD; break;
00094 case MP_WATER: type = VEH_SHIP; break;
00095 case MP_STATION: type = VEH_AIRCRAFT; break;
00096 default: NOT_REACHED();
00097 }
00098
00099
00100 EngineID eid = GB(p1, 0, 16);
00101 if (!IsEngineBuildable(eid, type, _current_company)) return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type);
00102
00103 const Engine *e = Engine::Get(eid);
00104 CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
00105
00106
00107 if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
00108
00109
00110 uint num_vehicles;
00111 switch (type) {
00112 case VEH_TRAIN: num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); break;
00113 case VEH_ROAD: num_vehicles = 1 + CountArticulatedParts(eid, false); break;
00114 case VEH_SHIP: num_vehicles = 1; break;
00115 case VEH_AIRCRAFT: num_vehicles = e->u.air.subtype & AIR_CTOL ? 2 : 3; break;
00116 default: NOT_REACHED();
00117 }
00118 if (!Vehicle::CanAllocateItem(num_vehicles)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00119
00120
00121
00122
00123 UnitID unit_num = (flags & DC_AUTOREPLACE || (type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON)) ? 0 : GetFreeUnitNumber(type);
00124 if (unit_num == UINT16_MAX) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00125
00126 Vehicle *v;
00127 switch (type) {
00128 case VEH_TRAIN: value.AddCost(CmdBuildRailVehicle(tile, flags, e, GB(p1, 16, 16), &v)); break;
00129 case VEH_ROAD: value.AddCost(CmdBuildRoadVehicle(tile, flags, e, GB(p1, 16, 16), &v)); break;
00130 case VEH_SHIP: value.AddCost(CmdBuildShip (tile, flags, e, GB(p1, 16, 16), &v)); break;
00131 case VEH_AIRCRAFT: value.AddCost(CmdBuildAircraft (tile, flags, e, GB(p1, 16, 16), &v)); break;
00132 default: NOT_REACHED();
00133 }
00134
00135 if (value.Succeeded() && flags & DC_EXEC) {
00136 v->unitnumber = unit_num;
00137 v->value = value.GetCost();
00138
00139 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00140 InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
00141 SetWindowDirty(WC_COMPANY, _current_company);
00142 if (IsLocalCompany()) {
00143 InvalidateAutoreplaceWindow(v->engine_type, v->group_id);
00144 }
00145
00146 Company::Get(_current_company)->num_engines[eid]++;
00147
00148 if (v->IsPrimaryVehicle()) {
00149 OrderBackup::Restore(v, p2);
00150 PrefillRouteLinks(v);
00151 }
00152 }
00153
00154 return value;
00155 }
00156
00157 CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16 data, uint32 user);
00158
00171 CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00172 {
00173 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00174 if (v == NULL) return CMD_ERROR;
00175
00176 Vehicle *front = v->First();
00177
00178 CommandCost ret = CheckOwnership(front->owner);
00179 if (ret.Failed()) return ret;
00180
00181 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00182
00183 if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00184
00185
00186 if (p1 & MAKE_ORDER_BACKUP_FLAG &&
00187 front->orders.list != NULL &&
00188 !front->orders.list->IsShared() &&
00189 !Order::CanAllocateItem(front->orders.list->GetNumOrders())) {
00190
00191
00192 p1 &= ~MAKE_ORDER_BACKUP_FLAG;
00193 }
00194
00195 if (v->type == VEH_TRAIN) {
00196 ret = CmdSellRailWagon(flags, v, GB(p1, 20, 12), p2);
00197 } else {
00198 ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
00199
00200 if (flags & DC_EXEC) {
00201 if (v->IsPrimaryVehicle() && p1 & MAKE_ORDER_BACKUP_FLAG) OrderBackup::Backup(v, p2);
00202 delete front;
00203 }
00204 }
00205
00206 return ret;
00207 }
00208
00214 static CommandCost GetRefitCost(EngineID engine_type)
00215 {
00216 ExpensesType expense_type;
00217 const Engine *e = Engine::Get(engine_type);
00218 Price base_price;
00219 uint cost_factor = e->info.refit_cost;
00220 switch (e->type) {
00221 case VEH_SHIP:
00222 base_price = PR_BUILD_VEHICLE_SHIP;
00223 expense_type = EXPENSES_SHIP_RUN;
00224 break;
00225
00226 case VEH_ROAD:
00227 base_price = PR_BUILD_VEHICLE_ROAD;
00228 expense_type = EXPENSES_ROADVEH_RUN;
00229 break;
00230
00231 case VEH_AIRCRAFT:
00232 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00233 expense_type = EXPENSES_AIRCRAFT_RUN;
00234 break;
00235
00236 case VEH_TRAIN:
00237 base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
00238 cost_factor <<= 1;
00239 expense_type = EXPENSES_TRAIN_RUN;
00240 break;
00241
00242 default: NOT_REACHED();
00243 }
00244 return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grf_prop.grffile, -10));
00245 }
00246
00258 static CommandCost RefitVehicle(Vehicle *v, bool only_this, uint8 num_vehicles, CargoID new_cid, byte new_subtype, DoCommandFlag flags)
00259 {
00260 CommandCost cost(v->GetExpenseType(false));
00261 uint total_capacity = 0;
00262 uint total_mail_capacity = 0;
00263 num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
00264
00265 VehicleSet vehicles_to_refit;
00266 if (!only_this) {
00267 GetVehicleSet(vehicles_to_refit, v, num_vehicles);
00268
00269 v = v->First();
00270 }
00271
00272 v->InvalidateNewGRFCacheOfChain();
00273 for (; v != NULL; v = (only_this ? NULL : v->Next())) {
00274 if (v->type == VEH_TRAIN && !vehicles_to_refit.Contains(v->index) && !only_this) continue;
00275
00276 const Engine *e = Engine::Get(v->engine_type);
00277 if (!e->CanCarryCargo()) continue;
00278
00279
00280 bool refittable = HasBit(e->info.refit_mask, new_cid);
00281 if (!refittable && v->cargo_type != new_cid) continue;
00282
00283
00284 CargoID temp_cid = v->cargo_type;
00285 byte temp_subtype = v->cargo_subtype;
00286 if (refittable) {
00287 v->cargo_type = new_cid;
00288 v->cargo_subtype = new_subtype;
00289 }
00290
00291 uint16 mail_capacity = 0;
00292 uint amount = GetVehicleCapacity(v, &mail_capacity);
00293 total_capacity += amount;
00294
00295 total_mail_capacity += mail_capacity;
00296
00297 if (!refittable) continue;
00298
00299
00300 v->cargo_type = temp_cid;
00301 v->cargo_subtype = temp_subtype;
00302
00303 if (new_cid != v->cargo_type) {
00304 cost.AddCost(GetRefitCost(v->engine_type));
00305 }
00306
00307 if (flags & DC_EXEC) {
00308 v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
00309 v->cargo_type = new_cid;
00310 v->cargo_cap = amount;
00311 v->cargo_subtype = new_subtype;
00312 if (v->type == VEH_AIRCRAFT) {
00313 Vehicle *u = v->Next();
00314 u->cargo_cap = mail_capacity;
00315 u->cargo.Truncate(mail_capacity);
00316 }
00317 }
00318 }
00319
00320 _returned_refit_capacity = total_capacity;
00321 _returned_mail_refit_capacity = total_mail_capacity;
00322 return cost;
00323 }
00324
00339 CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00340 {
00341 Vehicle *v = Vehicle::GetIfValid(p1);
00342 if (v == NULL) return CMD_ERROR;
00343
00344
00345
00346 if (!IsCompanyBuildableVehicleType(v->type)) return CMD_ERROR;
00347
00348 Vehicle *front = v->First();
00349
00350 CommandCost ret = CheckOwnership(front->owner);
00351 if (ret.Failed()) return ret;
00352
00353
00354 if (v != front && (v->type == VEH_SHIP || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
00355 if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00356 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00357
00358
00359 CargoID new_cid = GB(p2, 0, 5);
00360 byte new_subtype = GB(p2, 8, 8);
00361 if (new_cid >= NUM_CARGO) return CMD_ERROR;
00362
00363
00364 bool only_this = HasBit(p2, 7) || front->type == VEH_SHIP || front->type == VEH_AIRCRAFT;
00365 uint8 num_vehicles = GB(p2, 16, 8);
00366
00367 CommandCost cost = RefitVehicle(v, only_this, num_vehicles, new_cid, new_subtype, flags);
00368
00369 if (flags & DC_EXEC) {
00370
00371 switch (v->type) {
00372 case VEH_TRAIN:
00373 Train::From(front)->ConsistChanged(false);
00374 break;
00375 case VEH_ROAD:
00376 RoadVehUpdateCache(RoadVehicle::From(front));
00377 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) RoadVehicle::From(front)->CargoChanged();
00378 break;
00379
00380 case VEH_SHIP:
00381 v->InvalidateNewGRFCacheOfChain();
00382 v->colourmap = PAL_NONE;
00383 Ship::From(v)->UpdateCache();
00384 break;
00385 case VEH_AIRCRAFT:
00386 v->InvalidateNewGRFCacheOfChain();
00387 v->colourmap = PAL_NONE;
00388 UpdateAircraftCache(Aircraft::From(v));
00389 break;
00390
00391 default: NOT_REACHED();
00392 }
00393
00394 if (front->IsPrimaryVehicle()) PrefillRouteLinks(front);
00395
00396 InvalidateWindowData(WC_VEHICLE_DETAILS, front->index);
00397 SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
00398 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00399 } else {
00400
00401 v->InvalidateNewGRFCacheOfChain();
00402 }
00403
00404 return cost;
00405 }
00406
00416 CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00417 {
00418
00419 if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
00420
00421 Vehicle *v = Vehicle::GetIfValid(p1);
00422 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00423
00424 CommandCost ret = CheckOwnership(v->owner);
00425 if (ret.Failed()) return ret;
00426
00427 if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00428
00429 switch (v->type) {
00430 case VEH_TRAIN:
00431 if ((v->vehstatus & VS_STOPPED) && Train::From(v)->gcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_POWER);
00432 break;
00433
00434 case VEH_SHIP:
00435 case VEH_ROAD:
00436 break;
00437
00438 case VEH_AIRCRAFT: {
00439 Aircraft *a = Aircraft::From(v);
00440
00441 if (!(v->vehstatus & VS_CRASHED) && a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
00442 break;
00443 }
00444
00445 default: return CMD_ERROR;
00446 }
00447
00448
00449
00450 uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
00451 if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF && HasBit(p2, 0)) {
00452 StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
00453 return_cmd_error(error);
00454 }
00455
00456 if (flags & DC_EXEC) {
00457 if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
00458
00459 v->vehstatus ^= VS_STOPPED;
00460 if (v->type != VEH_TRAIN) v->cur_speed = 0;
00461 v->MarkDirty();
00462 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00463 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00464 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
00465 }
00466 return CommandCost();
00467 }
00468
00480 CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00481 {
00482 VehicleList list;
00483 bool do_start = HasBit(p1, 0);
00484 bool vehicle_list_window = HasBit(p1, 1);
00485
00486 VehicleListIdentifier vli;
00487 if (!vli.Unpack(p2)) return CMD_ERROR;
00488 if (!IsCompanyBuildableVehicleType(vli.vtype)) return CMD_ERROR;
00489
00490 if (vehicle_list_window) {
00491 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00492 } else {
00493
00494 BuildDepotVehicleList(vli.vtype, tile, &list, NULL);
00495 }
00496
00497 for (uint i = 0; i < list.Length(); i++) {
00498 const Vehicle *v = list[i];
00499
00500 if (!!(v->vehstatus & VS_STOPPED) != do_start) continue;
00501
00502 if (!vehicle_list_window) {
00503 if (vli.vtype == VEH_TRAIN) {
00504 if (!Train::From(v)->IsInDepot()) continue;
00505 } else {
00506 if (!(v->vehstatus & VS_HIDDEN)) continue;
00507 }
00508 }
00509
00510
00511 DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
00512 }
00513
00514 return CommandCost();
00515 }
00516
00526 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00527 {
00528 VehicleList list;
00529
00530 CommandCost cost(EXPENSES_NEW_VEHICLES);
00531 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00532 uint sell_command = GetCmdSellVeh(vehicle_type);
00533
00534 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00535
00536
00537 BuildDepotVehicleList(vehicle_type, tile, &list, &list);
00538
00539 CommandCost last_error = CMD_ERROR;
00540 bool had_success = false;
00541 for (uint i = 0; i < list.Length(); i++) {
00542 CommandCost ret = DoCommand(tile, list[i]->index | (1 << 20), 0, flags, sell_command);
00543 if (ret.Succeeded()) {
00544 cost.AddCost(ret);
00545 had_success = true;
00546 } else {
00547 last_error = ret;
00548 }
00549 }
00550
00551 return had_success ? cost : last_error;
00552 }
00553
00563 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00564 {
00565 VehicleList list;
00566 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
00567 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00568
00569 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00570 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
00571
00572
00573 BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
00574
00575 for (uint i = 0; i < list.Length(); i++) {
00576 const Vehicle *v = list[i];
00577
00578
00579 if (!v->IsInDepot()) continue;
00580
00581 CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
00582
00583 if (ret.Succeeded()) cost.AddCost(ret);
00584 }
00585 return cost;
00586 }
00587
00593 static bool IsUniqueVehicleName(const char *name)
00594 {
00595 const Vehicle *v;
00596
00597 FOR_ALL_VEHICLES(v) {
00598 if (v->name != NULL && strcmp(v->name, name) == 0) return false;
00599 }
00600
00601 return true;
00602 }
00603
00609 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
00610 {
00611 char buf[256];
00612
00613
00614 size_t number_position;
00615 for (number_position = strlen(src->name); number_position > 0; number_position--) {
00616
00617
00618 if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
00619 }
00620
00621
00622 int num;
00623 byte padding = 0;
00624 if (number_position == strlen(src->name)) {
00625
00626 strecpy(buf, src->name, lastof(buf));
00627 strecat(buf, " ", lastof(buf));
00628 number_position = strlen(buf);
00629 num = 2;
00630 } else {
00631
00632 strecpy(buf, src->name, lastof(buf));
00633 buf[number_position] = '\0';
00634 char *endptr;
00635 num = strtol(&src->name[number_position], &endptr, 10) + 1;
00636 padding = endptr - &src->name[number_position];
00637 }
00638
00639
00640 for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
00641
00642 seprintf(&buf[number_position], lastof(buf), "%0*d", padding, num);
00643
00644
00645 if (IsUniqueVehicleName(buf)) {
00646 dst->name = strdup(buf);
00647 break;
00648 }
00649 }
00650
00651
00652 }
00653
00663 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00664 {
00665 CommandCost total_cost(EXPENSES_NEW_VEHICLES);
00666
00667 Vehicle *v = Vehicle::GetIfValid(p1);
00668 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00669 Vehicle *v_front = v;
00670 Vehicle *w = NULL;
00671 Vehicle *w_front = NULL;
00672 Vehicle *w_rear = NULL;
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682 CommandCost ret = CheckOwnership(v->owner);
00683 if (ret.Failed()) return ret;
00684
00685 if (v->type == VEH_TRAIN && (!v->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
00686
00687
00688 if (!(flags & DC_EXEC)) {
00689 int veh_counter = 0;
00690 do {
00691 veh_counter++;
00692 } while ((v = v->Next()) != NULL);
00693
00694 if (!Vehicle::CanAllocateItem(veh_counter)) {
00695 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00696 }
00697 }
00698
00699 v = v_front;
00700
00701 do {
00702 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00703
00704 continue;
00705 }
00706
00707
00708
00709
00710
00711
00712
00713 DoCommandFlag build_flags = flags;
00714 if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
00715
00716 CommandCost cost = DoCommand(tile, v->engine_type | (1 << 16), 0, build_flags, GetCmdBuildVeh(v));
00717
00718 if (cost.Failed()) {
00719
00720 if (w_front != NULL) DoCommand(w_front->tile, w_front->index | (1 << 20), 0, flags, GetCmdSellVeh(w_front));
00721 return cost;
00722 }
00723
00724 total_cost.AddCost(cost);
00725
00726 if (flags & DC_EXEC) {
00727 w = Vehicle::Get(_new_vehicle_id);
00728
00729 if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
00730 SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
00731 }
00732
00733 if (v->type == VEH_TRAIN && !v->IsFrontEngine()) {
00734
00735
00736 CommandCost result = DoCommand(0, w->index | 1 << 20, w_rear->index, flags, CMD_MOVE_RAIL_VEHICLE);
00737 if (result.Failed()) {
00738
00739
00740 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00741 DoCommand(w_front->tile, w->index | 1 << 20, 0, flags, GetCmdSellVeh(w));
00742 return result;
00743 }
00744 } else {
00745
00746 w_front = w;
00747 w->service_interval = v->service_interval;
00748 }
00749 w_rear = w;
00750 }
00751 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00752
00753 if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
00754
00755 _new_vehicle_id = w_front->index;
00756 }
00757
00758 if (flags & DC_EXEC) {
00759
00760 DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
00761 }
00762
00763
00764
00765 w = w_front;
00766 v = v_front;
00767
00768
00769
00770
00771
00772
00773
00774 do {
00775 do {
00776 if (flags & DC_EXEC) {
00777 assert(w != NULL);
00778
00779
00780 byte subtype = GetBestFittingSubType(v, w);
00781 if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
00782 CommandCost cost = DoCommand(0, w->index, v->cargo_type | 1U << 7 | (subtype << 8), flags, GetCmdRefitVeh(v));
00783 if (cost.Succeeded()) total_cost.AddCost(cost);
00784 }
00785
00786 if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
00787 w = w->GetNextArticulatedPart();
00788 } else {
00789 break;
00790 }
00791 } else {
00792 const Engine *e = Engine::Get(v->engine_type);
00793 CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00794
00795 if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
00796 total_cost.AddCost(GetRefitCost(v->engine_type));
00797 }
00798 }
00799
00800 if (v->IsGroundVehicle() && v->HasArticulatedPart()) {
00801 v = v->GetNextArticulatedPart();
00802 } else {
00803 break;
00804 }
00805 } while (v != NULL);
00806
00807 if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = w->GetNextVehicle();
00808 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00809
00810 if (flags & DC_EXEC) {
00811
00812
00813
00814
00815
00816 DoCommand(0, w_front->index | (p2 & 1 ? CO_SHARE : CO_COPY) << 30, v_front->index, flags, CMD_CLONE_ORDER);
00817
00818
00819 if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
00820 }
00821
00822
00823
00824 if (!CheckCompanyHasMoney(total_cost)) {
00825 if (flags & DC_EXEC) {
00826
00827 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00828 }
00829 return total_cost;
00830 }
00831
00832 return total_cost;
00833 }
00834
00842 static CommandCost SendAllVehiclesToDepot(DoCommandFlag flags, bool service, const VehicleListIdentifier &vli)
00843 {
00844 VehicleList list;
00845
00846 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00847
00848
00849 bool had_success = false;
00850 for (uint i = 0; i < list.Length(); i++) {
00851 const Vehicle *v = list[i];
00852 CommandCost ret = DoCommand(v->tile, v->index | (service ? DEPOT_SERVICE : 0U) | DEPOT_DONT_CANCEL, 0, flags, GetCmdSendToDepot(vli.vtype));
00853
00854 if (ret.Succeeded()) {
00855 had_success = true;
00856
00857
00858
00859
00860
00861 if (!(flags & DC_EXEC)) break;
00862 }
00863 }
00864
00865 return had_success ? CommandCost() : CMD_ERROR;
00866 }
00867
00879 CommandCost CmdSendVehicleToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00880 {
00881 if (p1 & DEPOT_MASS_SEND) {
00882
00883 VehicleListIdentifier vli;
00884 if (!vli.Unpack(p2)) return CMD_ERROR;
00885 return SendAllVehiclesToDepot(flags, (p1 & DEPOT_SERVICE) != 0, vli);
00886 }
00887
00888 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00889 if (v == NULL) return CMD_ERROR;
00890
00891 return v->SendToDepot(flags, (DepotCommand)(p1 & DEPOT_COMMAND_MASK));
00892 }
00893
00903 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00904 {
00905 Vehicle *v = Vehicle::GetIfValid(p1);
00906 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00907
00908 CommandCost ret = CheckOwnership(v->owner);
00909 if (ret.Failed()) return ret;
00910
00911 bool reset = StrEmpty(text);
00912
00913 if (!reset) {
00914 if (Utf8StringLength(text) >= MAX_LENGTH_VEHICLE_NAME_CHARS) return CMD_ERROR;
00915 if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00916 }
00917
00918 if (flags & DC_EXEC) {
00919 free(v->name);
00920 v->name = reset ? NULL : strdup(text);
00921 InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
00922 MarkWholeScreenDirty();
00923 }
00924
00925 return CommandCost();
00926 }
00927
00928
00938 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00939 {
00940 Vehicle *v = Vehicle::GetIfValid(p1);
00941 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00942
00943 CommandCost ret = CheckOwnership(v->owner);
00944 if (ret.Failed()) return ret;
00945
00946 uint16 serv_int = GetServiceIntervalClamped(p2, v->owner);
00947 if (serv_int != p2) return CMD_ERROR;
00948
00949 if (flags & DC_EXEC) {
00950 v->service_interval = serv_int;
00951 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
00952 }
00953
00954 return CommandCost();
00955 }