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
00034 #include "table/strings.h"
00035
00036
00037 const uint32 _veh_build_proc_table[] = {
00038 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
00039 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
00040 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
00041 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
00042 };
00043
00044 const uint32 _veh_sell_proc_table[] = {
00045 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
00046 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
00047 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
00048 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
00049 };
00050
00051 const uint32 _veh_refit_proc_table[] = {
00052 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
00053 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
00054 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
00055 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
00056 };
00057
00058 const uint32 _send_to_depot_proc_table[] = {
00059
00060 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT) | CMD_NO_TEST_IF_IN_NETWORK,
00061 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
00062 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
00063 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
00064 };
00065
00066
00067 CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00068 CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00069 CommandCost CmdBuildShip (TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00070 CommandCost CmdBuildAircraft (TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00071
00083 CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00084 {
00085
00086 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
00087
00088 VehicleType type;
00089 switch (GetTileType(tile)) {
00090 case MP_RAILWAY: type = VEH_TRAIN; break;
00091 case MP_ROAD: type = VEH_ROAD; break;
00092 case MP_WATER: type = VEH_SHIP; break;
00093 case MP_STATION: type = VEH_AIRCRAFT; break;
00094 default: NOT_REACHED();
00095 }
00096
00097
00098 EngineID eid = GB(p1, 0, 16);
00099 if (!IsEngineBuildable(eid, type, _current_company)) return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type);
00100
00101 const Engine *e = Engine::Get(eid);
00102 CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
00103
00104
00105 if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
00106
00107
00108 uint num_vehicles;
00109 switch (type) {
00110 case VEH_TRAIN: num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); break;
00111 case VEH_ROAD: num_vehicles = 1 + CountArticulatedParts(eid, false); break;
00112 case VEH_SHIP: num_vehicles = 1; break;
00113 case VEH_AIRCRAFT: num_vehicles = e->u.air.subtype & AIR_CTOL ? 2 : 3; break;
00114 default: NOT_REACHED();
00115 }
00116 if (!Vehicle::CanAllocateItem(num_vehicles)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00117
00118
00119
00120
00121 UnitID unit_num = (flags & DC_AUTOREPLACE || (type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON)) ? 0 : GetFreeUnitNumber(type);
00122 if (unit_num == UINT16_MAX) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00123
00124 Vehicle *v;
00125 switch (type) {
00126 case VEH_TRAIN: value.AddCost(CmdBuildRailVehicle(tile, flags, e, GB(p1, 16, 16), &v)); break;
00127 case VEH_ROAD: value.AddCost(CmdBuildRoadVehicle(tile, flags, e, GB(p1, 16, 16), &v)); break;
00128 case VEH_SHIP: value.AddCost(CmdBuildShip (tile, flags, e, GB(p1, 16, 16), &v)); break;
00129 case VEH_AIRCRAFT: value.AddCost(CmdBuildAircraft (tile, flags, e, GB(p1, 16, 16), &v)); break;
00130 default: NOT_REACHED();
00131 }
00132
00133 if (value.Succeeded() && flags & DC_EXEC) {
00134 v->unitnumber = unit_num;
00135 v->value = value.GetCost();
00136
00137 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00138 InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
00139 SetWindowDirty(WC_COMPANY, _current_company);
00140 if (IsLocalCompany()) {
00141 InvalidateAutoreplaceWindow(v->engine_type, v->group_id);
00142 }
00143
00144 Company::Get(_current_company)->num_engines[eid]++;
00145
00146 if (v->IsPrimaryVehicle()) OrderBackup::Restore(v, p2);
00147 }
00148
00149 return value;
00150 }
00151
00152 CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16 data, uint32 user);
00153
00166 CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00167 {
00168 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00169 if (v == NULL) return CMD_ERROR;
00170
00171 Vehicle *front = v->First();
00172
00173 CommandCost ret = CheckOwnership(front->owner);
00174 if (ret.Failed()) return ret;
00175
00176 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00177
00178 if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00179
00180
00181 if (p1 & MAKE_ORDER_BACKUP_FLAG &&
00182 front->orders.list != NULL &&
00183 !front->orders.list->IsShared() &&
00184 !Order::CanAllocateItem(front->orders.list->GetNumOrders())) {
00185
00186
00187 p1 &= ~MAKE_ORDER_BACKUP_FLAG;
00188 }
00189
00190 if (v->type == VEH_TRAIN) {
00191 ret = CmdSellRailWagon(flags, v, GB(p1, 20, 12), p2);
00192 } else {
00193 ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
00194
00195 if (flags & DC_EXEC) {
00196 if (v->IsPrimaryVehicle() && p1 & MAKE_ORDER_BACKUP_FLAG) OrderBackup::Backup(v, p2);
00197 delete front;
00198 }
00199 }
00200
00201 return ret;
00202 }
00203
00209 static CommandCost GetRefitCost(EngineID engine_type)
00210 {
00211 ExpensesType expense_type;
00212 const Engine *e = Engine::Get(engine_type);
00213 Price base_price;
00214 uint cost_factor = e->info.refit_cost;
00215 switch (e->type) {
00216 case VEH_SHIP:
00217 base_price = PR_BUILD_VEHICLE_SHIP;
00218 expense_type = EXPENSES_SHIP_RUN;
00219 break;
00220
00221 case VEH_ROAD:
00222 base_price = PR_BUILD_VEHICLE_ROAD;
00223 expense_type = EXPENSES_ROADVEH_RUN;
00224 break;
00225
00226 case VEH_AIRCRAFT:
00227 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00228 expense_type = EXPENSES_AIRCRAFT_RUN;
00229 break;
00230
00231 case VEH_TRAIN:
00232 base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
00233 cost_factor <<= 1;
00234 expense_type = EXPENSES_TRAIN_RUN;
00235 break;
00236
00237 default: NOT_REACHED();
00238 }
00239 return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grf_prop.grffile, -10));
00240 }
00241
00253 static CommandCost RefitVehicle(Vehicle *v, bool only_this, uint8 num_vehicles, CargoID new_cid, byte new_subtype, DoCommandFlag flags)
00254 {
00255 CommandCost cost(v->GetExpenseType(false));
00256 uint total_capacity = 0;
00257 uint total_mail_capacity = 0;
00258 num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
00259
00260 VehicleSet vehicles_to_refit;
00261 if (!only_this) {
00262 GetVehicleSet(vehicles_to_refit, v, num_vehicles);
00263
00264 v = v->First();
00265 }
00266
00267 v->InvalidateNewGRFCacheOfChain();
00268 for (; v != NULL; v = (only_this ? NULL : v->Next())) {
00269 if (v->type == VEH_TRAIN && !vehicles_to_refit.Contains(v->index) && !only_this) continue;
00270
00271 const Engine *e = Engine::Get(v->engine_type);
00272 if (!e->CanCarryCargo()) continue;
00273
00274
00275 bool refittable = HasBit(e->info.refit_mask, new_cid);
00276 if (!refittable && v->cargo_type != new_cid) continue;
00277
00278
00279 CargoID temp_cid = v->cargo_type;
00280 byte temp_subtype = v->cargo_subtype;
00281 if (refittable) {
00282 v->cargo_type = new_cid;
00283 v->cargo_subtype = new_subtype;
00284 }
00285
00286 uint16 mail_capacity = 0;
00287 uint amount = GetVehicleCapacity(v, &mail_capacity);
00288 total_capacity += amount;
00289
00290 total_mail_capacity += mail_capacity;
00291
00292 if (!refittable) continue;
00293
00294
00295 v->cargo_type = temp_cid;
00296 v->cargo_subtype = temp_subtype;
00297
00298 if (new_cid != v->cargo_type) {
00299 cost.AddCost(GetRefitCost(v->engine_type));
00300 }
00301
00302 if (flags & DC_EXEC) {
00303 v->refit_cap = (v->cargo_type == new_cid) ? min(amount, v->cargo_cap) : 0;
00304 v->cargo.Truncate(v->refit_cap);
00305 v->cargo_type = new_cid;
00306 v->cargo_cap = amount;
00307 v->cargo_subtype = new_subtype;
00308 if (v->type == VEH_AIRCRAFT) {
00309 Vehicle *u = v->Next();
00310 u->refit_cap = min(u->cargo_cap, mail_capacity);
00311 u->cargo_cap = mail_capacity;
00312 u->cargo.Truncate(mail_capacity);
00313 }
00314 }
00315 }
00316
00317 _returned_refit_capacity = total_capacity;
00318 _returned_mail_refit_capacity = total_mail_capacity;
00319 return cost;
00320 }
00321
00336 CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00337 {
00338 Vehicle *v = Vehicle::GetIfValid(p1);
00339 if (v == NULL) return CMD_ERROR;
00340
00341
00342
00343 if (!IsCompanyBuildableVehicleType(v->type)) return CMD_ERROR;
00344
00345 Vehicle *front = v->First();
00346
00347 CommandCost ret = CheckOwnership(front->owner);
00348 if (ret.Failed()) return ret;
00349
00350
00351 if (v != front && (v->type == VEH_SHIP || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
00352 if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00353 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00354
00355
00356 CargoID new_cid = GB(p2, 0, 5);
00357 byte new_subtype = GB(p2, 8, 8);
00358 if (new_cid >= NUM_CARGO) return CMD_ERROR;
00359
00360
00361 bool only_this = HasBit(p2, 7) || front->type == VEH_SHIP || front->type == VEH_AIRCRAFT;
00362 uint8 num_vehicles = GB(p2, 16, 8);
00363
00364 CommandCost cost = RefitVehicle(v, only_this, num_vehicles, new_cid, new_subtype, flags);
00365
00366 if (flags & DC_EXEC) {
00367
00368 switch (v->type) {
00369 case VEH_TRAIN:
00370 Train::From(front)->ConsistChanged(false);
00371 break;
00372 case VEH_ROAD:
00373 RoadVehUpdateCache(RoadVehicle::From(front));
00374 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) RoadVehicle::From(front)->CargoChanged();
00375 break;
00376
00377 case VEH_SHIP:
00378 case VEH_AIRCRAFT:
00379 v->InvalidateNewGRFCacheOfChain();
00380 v->colourmap = PAL_NONE;
00381 break;
00382
00383 default: NOT_REACHED();
00384 }
00385
00386 InvalidateWindowData(WC_VEHICLE_DETAILS, front->index);
00387 SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
00388 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00389 } else {
00390
00391 v->InvalidateNewGRFCacheOfChain();
00392 }
00393
00394 return cost;
00395 }
00396
00406 CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00407 {
00408
00409 if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
00410
00411 Vehicle *v = Vehicle::GetIfValid(p1);
00412 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00413
00414 CommandCost ret = CheckOwnership(v->owner);
00415 if (ret.Failed()) return ret;
00416
00417 if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00418
00419 switch (v->type) {
00420 case VEH_TRAIN:
00421 if ((v->vehstatus & VS_STOPPED) && Train::From(v)->gcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_POWER);
00422 break;
00423
00424 case VEH_SHIP:
00425 case VEH_ROAD:
00426 break;
00427
00428 case VEH_AIRCRAFT: {
00429 Aircraft *a = Aircraft::From(v);
00430
00431 if (!(v->vehstatus & VS_CRASHED) && a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
00432 break;
00433 }
00434
00435 default: return CMD_ERROR;
00436 }
00437
00438
00439
00440 uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
00441 if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF && HasBit(p2, 0)) {
00442 StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
00443 return_cmd_error(error);
00444 }
00445
00446 if (flags & DC_EXEC) {
00447 if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
00448
00449 v->vehstatus ^= VS_STOPPED;
00450 if (v->type != VEH_TRAIN) v->cur_speed = 0;
00451 v->MarkDirty();
00452 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00453 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00454 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
00455 }
00456 return CommandCost();
00457 }
00458
00470 CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00471 {
00472 VehicleList list;
00473 bool do_start = HasBit(p1, 0);
00474 bool vehicle_list_window = HasBit(p1, 1);
00475
00476 VehicleListIdentifier vli;
00477 if (!vli.Unpack(p2)) return CMD_ERROR;
00478 if (!IsCompanyBuildableVehicleType(vli.vtype)) return CMD_ERROR;
00479
00480 if (vehicle_list_window) {
00481 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00482 } else {
00483
00484 BuildDepotVehicleList(vli.vtype, tile, &list, NULL);
00485 }
00486
00487 for (uint i = 0; i < list.Length(); i++) {
00488 const Vehicle *v = list[i];
00489
00490 if (!!(v->vehstatus & VS_STOPPED) != do_start) continue;
00491
00492 if (!vehicle_list_window) {
00493 if (vli.vtype == VEH_TRAIN) {
00494 if (!Train::From(v)->IsInDepot()) continue;
00495 } else {
00496 if (!(v->vehstatus & VS_HIDDEN)) continue;
00497 }
00498 }
00499
00500
00501 DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
00502 }
00503
00504 return CommandCost();
00505 }
00506
00516 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00517 {
00518 VehicleList list;
00519
00520 CommandCost cost(EXPENSES_NEW_VEHICLES);
00521 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00522 uint sell_command = GetCmdSellVeh(vehicle_type);
00523
00524 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00525
00526
00527 BuildDepotVehicleList(vehicle_type, tile, &list, &list);
00528
00529 CommandCost last_error = CMD_ERROR;
00530 bool had_success = false;
00531 for (uint i = 0; i < list.Length(); i++) {
00532 CommandCost ret = DoCommand(tile, list[i]->index | (1 << 20), 0, flags, sell_command);
00533 if (ret.Succeeded()) {
00534 cost.AddCost(ret);
00535 had_success = true;
00536 } else {
00537 last_error = ret;
00538 }
00539 }
00540
00541 return had_success ? cost : last_error;
00542 }
00543
00553 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00554 {
00555 VehicleList list;
00556 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
00557 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00558
00559 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00560 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
00561
00562
00563 BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
00564
00565 for (uint i = 0; i < list.Length(); i++) {
00566 const Vehicle *v = list[i];
00567
00568
00569 if (!v->IsInDepot()) continue;
00570
00571 CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
00572
00573 if (ret.Succeeded()) cost.AddCost(ret);
00574 }
00575 return cost;
00576 }
00577
00583 static bool IsUniqueVehicleName(const char *name)
00584 {
00585 const Vehicle *v;
00586
00587 FOR_ALL_VEHICLES(v) {
00588 if (v->name != NULL && strcmp(v->name, name) == 0) return false;
00589 }
00590
00591 return true;
00592 }
00593
00599 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
00600 {
00601 char buf[256];
00602
00603
00604 size_t number_position;
00605 for (number_position = strlen(src->name); number_position > 0; number_position--) {
00606
00607
00608 if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
00609 }
00610
00611
00612 int num;
00613 byte padding = 0;
00614 if (number_position == strlen(src->name)) {
00615
00616 strecpy(buf, src->name, lastof(buf));
00617 strecat(buf, " ", lastof(buf));
00618 number_position = strlen(buf);
00619 num = 2;
00620 } else {
00621
00622 strecpy(buf, src->name, lastof(buf));
00623 buf[number_position] = '\0';
00624 char *endptr;
00625 num = strtol(&src->name[number_position], &endptr, 10) + 1;
00626 padding = endptr - &src->name[number_position];
00627 }
00628
00629
00630 for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
00631
00632 seprintf(&buf[number_position], lastof(buf), "%0*d", padding, num);
00633
00634
00635 if (IsUniqueVehicleName(buf)) {
00636 dst->name = strdup(buf);
00637 break;
00638 }
00639 }
00640
00641
00642 }
00643
00653 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00654 {
00655 CommandCost total_cost(EXPENSES_NEW_VEHICLES);
00656
00657 Vehicle *v = Vehicle::GetIfValid(p1);
00658 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00659 Vehicle *v_front = v;
00660 Vehicle *w = NULL;
00661 Vehicle *w_front = NULL;
00662 Vehicle *w_rear = NULL;
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 CommandCost ret = CheckOwnership(v->owner);
00673 if (ret.Failed()) return ret;
00674
00675 if (v->type == VEH_TRAIN && (!v->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
00676
00677
00678 if (!(flags & DC_EXEC)) {
00679 int veh_counter = 0;
00680 do {
00681 veh_counter++;
00682 } while ((v = v->Next()) != NULL);
00683
00684 if (!Vehicle::CanAllocateItem(veh_counter)) {
00685 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00686 }
00687 }
00688
00689 v = v_front;
00690
00691 do {
00692 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00693
00694 continue;
00695 }
00696
00697
00698
00699
00700
00701
00702
00703 DoCommandFlag build_flags = flags;
00704 if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
00705
00706 CommandCost cost = DoCommand(tile, v->engine_type | (1 << 16), 0, build_flags, GetCmdBuildVeh(v));
00707
00708 if (cost.Failed()) {
00709
00710 if (w_front != NULL) DoCommand(w_front->tile, w_front->index | (1 << 20), 0, flags, GetCmdSellVeh(w_front));
00711 return cost;
00712 }
00713
00714 total_cost.AddCost(cost);
00715
00716 if (flags & DC_EXEC) {
00717 w = Vehicle::Get(_new_vehicle_id);
00718
00719 if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
00720 SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
00721 }
00722
00723 if (v->type == VEH_TRAIN && !v->IsFrontEngine()) {
00724
00725
00726 CommandCost result = DoCommand(0, w->index | 1 << 20, w_rear->index, flags, CMD_MOVE_RAIL_VEHICLE);
00727 if (result.Failed()) {
00728
00729
00730 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00731 DoCommand(w_front->tile, w->index | 1 << 20, 0, flags, GetCmdSellVeh(w));
00732 return result;
00733 }
00734 } else {
00735
00736 w_front = w;
00737 w->service_interval = v->service_interval;
00738 }
00739 w_rear = w;
00740 }
00741 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00742
00743 if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
00744
00745 _new_vehicle_id = w_front->index;
00746 }
00747
00748 if (flags & DC_EXEC) {
00749
00750 DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
00751 }
00752
00753
00754
00755 w = w_front;
00756 v = v_front;
00757
00758
00759
00760
00761
00762
00763
00764 do {
00765 do {
00766 if (flags & DC_EXEC) {
00767 assert(w != NULL);
00768
00769
00770 byte subtype = GetBestFittingSubType(v, w);
00771 if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
00772 CommandCost cost = DoCommand(0, w->index, v->cargo_type | 1U << 7 | (subtype << 8), flags, GetCmdRefitVeh(v));
00773 if (cost.Succeeded()) total_cost.AddCost(cost);
00774 }
00775
00776 if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
00777 w = w->GetNextArticulatedPart();
00778 } else {
00779 break;
00780 }
00781 } else {
00782 const Engine *e = Engine::Get(v->engine_type);
00783 CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00784
00785 if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
00786 total_cost.AddCost(GetRefitCost(v->engine_type));
00787 }
00788 }
00789
00790 if (v->IsGroundVehicle() && v->HasArticulatedPart()) {
00791 v = v->GetNextArticulatedPart();
00792 } else {
00793 break;
00794 }
00795 } while (v != NULL);
00796
00797 if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = w->GetNextVehicle();
00798 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00799
00800 if (flags & DC_EXEC) {
00801
00802
00803
00804
00805
00806 DoCommand(0, w_front->index | (p2 & 1 ? CO_SHARE : CO_COPY) << 30, v_front->index, flags, CMD_CLONE_ORDER);
00807
00808
00809 if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
00810 }
00811
00812
00813
00814 if (!CheckCompanyHasMoney(total_cost)) {
00815 if (flags & DC_EXEC) {
00816
00817 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00818 }
00819 return total_cost;
00820 }
00821
00822 return total_cost;
00823 }
00824
00832 static CommandCost SendAllVehiclesToDepot(DoCommandFlag flags, bool service, const VehicleListIdentifier &vli)
00833 {
00834 VehicleList list;
00835
00836 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00837
00838
00839 bool had_success = false;
00840 for (uint i = 0; i < list.Length(); i++) {
00841 const Vehicle *v = list[i];
00842 CommandCost ret = DoCommand(v->tile, v->index | (service ? DEPOT_SERVICE : 0U) | DEPOT_DONT_CANCEL, 0, flags, GetCmdSendToDepot(vli.vtype));
00843
00844 if (ret.Succeeded()) {
00845 had_success = true;
00846
00847
00848
00849
00850
00851 if (!(flags & DC_EXEC)) break;
00852 }
00853 }
00854
00855 return had_success ? CommandCost() : CMD_ERROR;
00856 }
00857
00869 CommandCost CmdSendVehicleToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00870 {
00871 if (p1 & DEPOT_MASS_SEND) {
00872
00873 VehicleListIdentifier vli;
00874 if (!vli.Unpack(p2)) return CMD_ERROR;
00875 return SendAllVehiclesToDepot(flags, (p1 & DEPOT_SERVICE) != 0, vli);
00876 }
00877
00878 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00879 if (v == NULL) return CMD_ERROR;
00880
00881 return v->SendToDepot(flags, (DepotCommand)(p1 & DEPOT_COMMAND_MASK));
00882 }
00883
00893 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00894 {
00895 Vehicle *v = Vehicle::GetIfValid(p1);
00896 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00897
00898 CommandCost ret = CheckOwnership(v->owner);
00899 if (ret.Failed()) return ret;
00900
00901 bool reset = StrEmpty(text);
00902
00903 if (!reset) {
00904 if (Utf8StringLength(text) >= MAX_LENGTH_VEHICLE_NAME_CHARS) return CMD_ERROR;
00905 if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00906 }
00907
00908 if (flags & DC_EXEC) {
00909 free(v->name);
00910 v->name = reset ? NULL : strdup(text);
00911 InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
00912 MarkWholeScreenDirty();
00913 }
00914
00915 return CommandCost();
00916 }
00917
00918
00928 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00929 {
00930 Vehicle *v = Vehicle::GetIfValid(p1);
00931 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00932
00933 CommandCost ret = CheckOwnership(v->owner);
00934 if (ret.Failed()) return ret;
00935
00936 uint16 serv_int = GetServiceIntervalClamped(p2, v->owner);
00937 if (serv_int != p2) return CMD_ERROR;
00938
00939 if (flags & DC_EXEC) {
00940 v->service_interval = serv_int;
00941 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
00942 }
00943
00944 return CommandCost();
00945 }