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 "newgrf.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 GroupStatistics::CountEngine(v, 1);
00147 GroupStatistics::UpdateAutoreplace(_current_company);
00148
00149 if (v->IsPrimaryVehicle()) {
00150 GroupStatistics::CountVehicle(v, 1);
00151 OrderBackup::Restore(v, p2);
00152 }
00153 }
00154
00155 return value;
00156 }
00157
00158 CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16 data, uint32 user);
00159
00172 CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00173 {
00174 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00175 if (v == NULL) return CMD_ERROR;
00176
00177 Vehicle *front = v->First();
00178
00179 CommandCost ret = CheckOwnership(front->owner);
00180 if (ret.Failed()) return ret;
00181
00182 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00183
00184 if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00185
00186
00187 if (p1 & MAKE_ORDER_BACKUP_FLAG &&
00188 front->orders.list != NULL &&
00189 !front->orders.list->IsShared() &&
00190 !Order::CanAllocateItem(front->orders.list->GetNumOrders())) {
00191
00192
00193 p1 &= ~MAKE_ORDER_BACKUP_FLAG;
00194 }
00195
00196 if (v->type == VEH_TRAIN) {
00197 ret = CmdSellRailWagon(flags, v, GB(p1, 20, 12), p2);
00198 } else {
00199 ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
00200
00201 if (flags & DC_EXEC) {
00202 if (v->IsPrimaryVehicle() && p1 & MAKE_ORDER_BACKUP_FLAG) OrderBackup::Backup(v, p2);
00203 delete front;
00204 }
00205 }
00206
00207 return ret;
00208 }
00209
00211 static int GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype, bool *auto_refit_allowed)
00212 {
00213
00214 const Engine *e = Engine::Get(engine_type);
00215
00216
00217 if (e->GetGRF() != NULL) {
00218 const CargoSpec *cs = CargoSpec::Get(new_cid);
00219 uint32 param1 = (cs->classes << 16) | (new_subtype << 8) | e->GetGRF()->cargo_map[new_cid];
00220
00221 uint16 cb_res = GetVehicleCallback(CBID_VEHICLE_REFIT_COST, param1, 0, engine_type, v);
00222 if (cb_res != CALLBACK_FAILED) {
00223 *auto_refit_allowed = HasBit(cb_res, 14);
00224 int factor = GB(cb_res, 0, 14);
00225 if (factor >= 0x2000) factor -= 0x4000;
00226 return factor;
00227 }
00228 }
00229
00230 *auto_refit_allowed = e->info.refit_cost == 0;
00231 return e->info.refit_cost;
00232 }
00233
00243 static CommandCost GetRefitCost(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype, bool *auto_refit_allowed)
00244 {
00245 ExpensesType expense_type;
00246 const Engine *e = Engine::Get(engine_type);
00247 Price base_price;
00248 int cost_factor = GetRefitCostFactor(v, engine_type, new_cid, new_subtype, auto_refit_allowed);
00249 switch (e->type) {
00250 case VEH_SHIP:
00251 base_price = PR_BUILD_VEHICLE_SHIP;
00252 expense_type = EXPENSES_SHIP_RUN;
00253 break;
00254
00255 case VEH_ROAD:
00256 base_price = PR_BUILD_VEHICLE_ROAD;
00257 expense_type = EXPENSES_ROADVEH_RUN;
00258 break;
00259
00260 case VEH_AIRCRAFT:
00261 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00262 expense_type = EXPENSES_AIRCRAFT_RUN;
00263 break;
00264
00265 case VEH_TRAIN:
00266 base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
00267 cost_factor <<= 1;
00268 expense_type = EXPENSES_TRAIN_RUN;
00269 break;
00270
00271 default: NOT_REACHED();
00272 }
00273 if (cost_factor < 0) {
00274 return CommandCost(expense_type, -GetPrice(base_price, -cost_factor, e->GetGRF(), -10));
00275 } else {
00276 return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->GetGRF(), -10));
00277 }
00278 }
00279
00292 static CommandCost RefitVehicle(Vehicle *v, bool only_this, uint8 num_vehicles, CargoID new_cid, byte new_subtype, DoCommandFlag flags, bool auto_refit)
00293 {
00294 CommandCost cost(v->GetExpenseType(false));
00295 uint total_capacity = 0;
00296 uint total_mail_capacity = 0;
00297 num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
00298
00299 VehicleSet vehicles_to_refit;
00300 if (!only_this) {
00301 GetVehicleSet(vehicles_to_refit, v, num_vehicles);
00302
00303 v = v->First();
00304 }
00305
00306 v->InvalidateNewGRFCacheOfChain();
00307 for (; v != NULL; v = (only_this ? NULL : v->Next())) {
00308 if (v->type == VEH_TRAIN && !vehicles_to_refit.Contains(v->index) && !only_this) continue;
00309
00310 const Engine *e = v->GetEngine();
00311 if (!e->CanCarryCargo()) continue;
00312
00313
00314
00315 bool refittable = HasBit(e->info.refit_mask, new_cid) && (!auto_refit || HasBit(e->info.misc_flags, EF_AUTO_REFIT));
00316 if (!refittable && v->cargo_type != new_cid) continue;
00317
00318
00319 CargoID temp_cid = v->cargo_type;
00320 byte temp_subtype = v->cargo_subtype;
00321 if (refittable) {
00322 v->cargo_type = new_cid;
00323 v->cargo_subtype = new_subtype;
00324 }
00325
00326 uint16 mail_capacity = 0;
00327 uint amount = e->DetermineCapacity(v, &mail_capacity);
00328 total_capacity += amount;
00329
00330 total_mail_capacity += mail_capacity;
00331
00332 if (!refittable) continue;
00333
00334
00335 v->cargo_type = temp_cid;
00336 v->cargo_subtype = temp_subtype;
00337
00338 if (new_cid != v->cargo_type) {
00339 bool auto_refit_allowed;
00340 CommandCost refit_cost = GetRefitCost(v, v->engine_type, new_cid, new_subtype, &auto_refit_allowed);
00341 if (auto_refit && !auto_refit_allowed) {
00342
00343 total_capacity -= amount;
00344 total_mail_capacity -= mail_capacity;
00345 continue;
00346 }
00347 cost.AddCost(refit_cost);
00348 }
00349
00350 if (flags & DC_EXEC) {
00351 v->refit_cap = (v->cargo_type == new_cid) ? min(amount, v->cargo_cap) : 0;
00352 v->cargo.Truncate(v->refit_cap);
00353 v->cargo_type = new_cid;
00354 v->cargo_cap = amount;
00355 v->cargo_subtype = new_subtype;
00356 if (v->type == VEH_AIRCRAFT) {
00357 Vehicle *u = v->Next();
00358 u->refit_cap = min(u->cargo_cap, mail_capacity);
00359 u->cargo_cap = mail_capacity;
00360 u->cargo.Truncate(mail_capacity);
00361 }
00362 }
00363 }
00364
00365 _returned_refit_capacity = total_capacity;
00366 _returned_mail_refit_capacity = total_mail_capacity;
00367 return cost;
00368 }
00369
00385 CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00386 {
00387 Vehicle *v = Vehicle::GetIfValid(p1);
00388 if (v == NULL) return CMD_ERROR;
00389
00390
00391
00392 if (!IsCompanyBuildableVehicleType(v->type)) return CMD_ERROR;
00393
00394 Vehicle *front = v->First();
00395
00396 CommandCost ret = CheckOwnership(front->owner);
00397 if (ret.Failed()) return ret;
00398
00399 bool auto_refit = HasBit(p2, 6);
00400
00401
00402 if (v != front && (v->type == VEH_SHIP || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
00403
00404 if ((!auto_refit || !front->current_order.IsType(OT_LOADING)) && !front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00405 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00406
00407
00408 CargoID new_cid = GB(p2, 0, 5);
00409 byte new_subtype = GB(p2, 8, 8);
00410 if (new_cid >= NUM_CARGO) return CMD_ERROR;
00411
00412
00413 bool only_this = HasBit(p2, 7) || front->type == VEH_SHIP || front->type == VEH_AIRCRAFT;
00414 uint8 num_vehicles = GB(p2, 16, 8);
00415
00416 CommandCost cost = RefitVehicle(v, only_this, num_vehicles, new_cid, new_subtype, flags, auto_refit);
00417
00418 if (flags & DC_EXEC) {
00419
00420 switch (v->type) {
00421 case VEH_TRAIN:
00422 Train::From(front)->ConsistChanged(auto_refit);
00423 break;
00424 case VEH_ROAD:
00425 RoadVehUpdateCache(RoadVehicle::From(front), auto_refit);
00426 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) RoadVehicle::From(front)->CargoChanged();
00427 break;
00428
00429 case VEH_SHIP:
00430 v->InvalidateNewGRFCacheOfChain();
00431 v->colourmap = PAL_NONE;
00432 Ship::From(v)->UpdateCache();
00433 break;
00434
00435 case VEH_AIRCRAFT:
00436 v->InvalidateNewGRFCacheOfChain();
00437 v->colourmap = PAL_NONE;
00438 UpdateAircraftCache(Aircraft::From(v));
00439 break;
00440
00441 default: NOT_REACHED();
00442 }
00443
00444 InvalidateWindowData(WC_VEHICLE_DETAILS, front->index);
00445 SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
00446 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00447 } else {
00448
00449 v->InvalidateNewGRFCacheOfChain();
00450 }
00451
00452 return cost;
00453 }
00454
00464 CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00465 {
00466
00467 if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
00468
00469 Vehicle *v = Vehicle::GetIfValid(p1);
00470 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00471
00472 CommandCost ret = CheckOwnership(v->owner);
00473 if (ret.Failed()) return ret;
00474
00475 if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00476
00477 switch (v->type) {
00478 case VEH_TRAIN:
00479 if ((v->vehstatus & VS_STOPPED) && Train::From(v)->gcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_POWER);
00480 break;
00481
00482 case VEH_SHIP:
00483 case VEH_ROAD:
00484 break;
00485
00486 case VEH_AIRCRAFT: {
00487 Aircraft *a = Aircraft::From(v);
00488
00489 if (!(v->vehstatus & VS_CRASHED) && a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
00490 break;
00491 }
00492
00493 default: return CMD_ERROR;
00494 }
00495
00496 if (HasBit(p2, 0)) {
00497
00498 uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
00499 StringID error = STR_NULL;
00500 if (callback != CALLBACK_FAILED) {
00501 if (v->GetGRF()->grf_version < 8) {
00502
00503 if (callback < 0x400 && GB(callback, 0, 8) != 0xFF) error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
00504 } else {
00505 if (callback < 0x400) {
00506 error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
00507 } else {
00508 switch (callback) {
00509 case 0x400:
00510 break;
00511
00512 default:
00513 error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
00514 break;
00515 }
00516 }
00517 }
00518 }
00519 if (error != STR_NULL) return_cmd_error(error);
00520 }
00521
00522 if (flags & DC_EXEC) {
00523 if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
00524
00525 v->vehstatus ^= VS_STOPPED;
00526 if (v->type != VEH_TRAIN) v->cur_speed = 0;
00527 v->MarkDirty();
00528 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00529 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00530 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
00531 }
00532 return CommandCost();
00533 }
00534
00546 CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00547 {
00548 VehicleList list;
00549 bool do_start = HasBit(p1, 0);
00550 bool vehicle_list_window = HasBit(p1, 1);
00551
00552 VehicleListIdentifier vli;
00553 if (!vli.Unpack(p2)) return CMD_ERROR;
00554 if (!IsCompanyBuildableVehicleType(vli.vtype)) return CMD_ERROR;
00555
00556 if (vehicle_list_window) {
00557 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00558 } else {
00559
00560 BuildDepotVehicleList(vli.vtype, tile, &list, NULL);
00561 }
00562
00563 for (uint i = 0; i < list.Length(); i++) {
00564 const Vehicle *v = list[i];
00565
00566 if (!!(v->vehstatus & VS_STOPPED) != do_start) continue;
00567
00568 if (!vehicle_list_window) {
00569 if (vli.vtype == VEH_TRAIN) {
00570 if (!Train::From(v)->IsInDepot()) continue;
00571 } else {
00572 if (!(v->vehstatus & VS_HIDDEN)) continue;
00573 }
00574 }
00575
00576
00577 DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
00578 }
00579
00580 return CommandCost();
00581 }
00582
00592 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00593 {
00594 VehicleList list;
00595
00596 CommandCost cost(EXPENSES_NEW_VEHICLES);
00597 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00598
00599 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00600
00601 uint sell_command = GetCmdSellVeh(vehicle_type);
00602
00603
00604 BuildDepotVehicleList(vehicle_type, tile, &list, &list);
00605
00606 CommandCost last_error = CMD_ERROR;
00607 bool had_success = false;
00608 for (uint i = 0; i < list.Length(); i++) {
00609 CommandCost ret = DoCommand(tile, list[i]->index | (1 << 20), 0, flags, sell_command);
00610 if (ret.Succeeded()) {
00611 cost.AddCost(ret);
00612 had_success = true;
00613 } else {
00614 last_error = ret;
00615 }
00616 }
00617
00618 return had_success ? cost : last_error;
00619 }
00620
00630 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00631 {
00632 VehicleList list;
00633 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
00634 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00635
00636 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00637 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
00638
00639
00640 BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
00641
00642 for (uint i = 0; i < list.Length(); i++) {
00643 const Vehicle *v = list[i];
00644
00645
00646 if (!v->IsInDepot()) continue;
00647
00648 CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
00649
00650 if (ret.Succeeded()) cost.AddCost(ret);
00651 }
00652 return cost;
00653 }
00654
00660 static bool IsUniqueVehicleName(const char *name)
00661 {
00662 const Vehicle *v;
00663
00664 FOR_ALL_VEHICLES(v) {
00665 if (v->name != NULL && strcmp(v->name, name) == 0) return false;
00666 }
00667
00668 return true;
00669 }
00670
00676 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
00677 {
00678 char buf[256];
00679
00680
00681 size_t number_position;
00682 for (number_position = strlen(src->name); number_position > 0; number_position--) {
00683
00684
00685 if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
00686 }
00687
00688
00689 int num;
00690 byte padding = 0;
00691 if (number_position == strlen(src->name)) {
00692
00693 strecpy(buf, src->name, lastof(buf));
00694 strecat(buf, " ", lastof(buf));
00695 number_position = strlen(buf);
00696 num = 2;
00697 } else {
00698
00699 strecpy(buf, src->name, lastof(buf));
00700 buf[number_position] = '\0';
00701 char *endptr;
00702 num = strtol(&src->name[number_position], &endptr, 10) + 1;
00703 padding = endptr - &src->name[number_position];
00704 }
00705
00706
00707 for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
00708
00709 seprintf(&buf[number_position], lastof(buf), "%0*d", padding, num);
00710
00711
00712 if (IsUniqueVehicleName(buf)) {
00713 dst->name = strdup(buf);
00714 break;
00715 }
00716 }
00717
00718
00719 }
00720
00730 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00731 {
00732 CommandCost total_cost(EXPENSES_NEW_VEHICLES);
00733
00734 Vehicle *v = Vehicle::GetIfValid(p1);
00735 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00736 Vehicle *v_front = v;
00737 Vehicle *w = NULL;
00738 Vehicle *w_front = NULL;
00739 Vehicle *w_rear = NULL;
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749 CommandCost ret = CheckOwnership(v->owner);
00750 if (ret.Failed()) return ret;
00751
00752 if (v->type == VEH_TRAIN && (!v->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
00753
00754
00755 if (!(flags & DC_EXEC)) {
00756 int veh_counter = 0;
00757 do {
00758 veh_counter++;
00759 } while ((v = v->Next()) != NULL);
00760
00761 if (!Vehicle::CanAllocateItem(veh_counter)) {
00762 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00763 }
00764 }
00765
00766 v = v_front;
00767
00768 do {
00769 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00770
00771 continue;
00772 }
00773
00774
00775
00776
00777
00778
00779
00780 DoCommandFlag build_flags = flags;
00781 if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
00782
00783 CommandCost cost = DoCommand(tile, v->engine_type | (1 << 16), 0, build_flags, GetCmdBuildVeh(v));
00784
00785 if (cost.Failed()) {
00786
00787 if (w_front != NULL) DoCommand(w_front->tile, w_front->index | (1 << 20), 0, flags, GetCmdSellVeh(w_front));
00788 return cost;
00789 }
00790
00791 total_cost.AddCost(cost);
00792
00793 if (flags & DC_EXEC) {
00794 w = Vehicle::Get(_new_vehicle_id);
00795
00796 if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
00797 SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
00798 }
00799
00800 if (v->type == VEH_TRAIN && !v->IsFrontEngine()) {
00801
00802
00803 CommandCost result = DoCommand(0, w->index | 1 << 20, w_rear->index, flags, CMD_MOVE_RAIL_VEHICLE);
00804 if (result.Failed()) {
00805
00806
00807 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00808 DoCommand(w_front->tile, w->index | 1 << 20, 0, flags, GetCmdSellVeh(w));
00809 return result;
00810 }
00811 } else {
00812
00813 w_front = w;
00814 w->service_interval = v->service_interval;
00815 }
00816 w_rear = w;
00817 }
00818 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00819
00820 if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
00821
00822 _new_vehicle_id = w_front->index;
00823 }
00824
00825 if (flags & DC_EXEC) {
00826
00827 DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
00828 }
00829
00830
00831
00832 w = w_front;
00833 v = v_front;
00834
00835
00836
00837
00838
00839
00840
00841 do {
00842 do {
00843 if (flags & DC_EXEC) {
00844 assert(w != NULL);
00845
00846
00847 byte subtype = GetBestFittingSubType(v, w);
00848 if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
00849 CommandCost cost = DoCommand(0, w->index, v->cargo_type | 1U << 7 | (subtype << 8), flags, GetCmdRefitVeh(v));
00850 if (cost.Succeeded()) total_cost.AddCost(cost);
00851 }
00852
00853 if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
00854 w = w->GetNextArticulatedPart();
00855 } else {
00856 break;
00857 }
00858 } else {
00859 const Engine *e = v->GetEngine();
00860 CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00861
00862 if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
00863 bool dummy;
00864 total_cost.AddCost(GetRefitCost(NULL, v->engine_type, v->cargo_type, v->cargo_subtype, &dummy));
00865 }
00866 }
00867
00868 if (v->IsGroundVehicle() && v->HasArticulatedPart()) {
00869 v = v->GetNextArticulatedPart();
00870 } else {
00871 break;
00872 }
00873 } while (v != NULL);
00874
00875 if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = w->GetNextVehicle();
00876 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00877
00878 if (flags & DC_EXEC) {
00879
00880
00881
00882
00883
00884 DoCommand(0, w_front->index | (p2 & 1 ? CO_SHARE : CO_COPY) << 30, v_front->index, flags, CMD_CLONE_ORDER);
00885
00886
00887 if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
00888 }
00889
00890
00891
00892 if (!CheckCompanyHasMoney(total_cost)) {
00893 if (flags & DC_EXEC) {
00894
00895 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00896 }
00897 return total_cost;
00898 }
00899
00900 return total_cost;
00901 }
00902
00910 static CommandCost SendAllVehiclesToDepot(DoCommandFlag flags, bool service, const VehicleListIdentifier &vli)
00911 {
00912 VehicleList list;
00913
00914 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00915
00916
00917 bool had_success = false;
00918 for (uint i = 0; i < list.Length(); i++) {
00919 const Vehicle *v = list[i];
00920 CommandCost ret = DoCommand(v->tile, v->index | (service ? DEPOT_SERVICE : 0U) | DEPOT_DONT_CANCEL, 0, flags, GetCmdSendToDepot(vli.vtype));
00921
00922 if (ret.Succeeded()) {
00923 had_success = true;
00924
00925
00926
00927
00928
00929 if (!(flags & DC_EXEC)) break;
00930 }
00931 }
00932
00933 return had_success ? CommandCost() : CMD_ERROR;
00934 }
00935
00947 CommandCost CmdSendVehicleToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00948 {
00949 if (p1 & DEPOT_MASS_SEND) {
00950
00951 VehicleListIdentifier vli;
00952 if (!vli.Unpack(p2)) return CMD_ERROR;
00953 return SendAllVehiclesToDepot(flags, (p1 & DEPOT_SERVICE) != 0, vli);
00954 }
00955
00956 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00957 if (v == NULL) return CMD_ERROR;
00958
00959 return v->SendToDepot(flags, (DepotCommand)(p1 & DEPOT_COMMAND_MASK));
00960 }
00961
00971 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00972 {
00973 Vehicle *v = Vehicle::GetIfValid(p1);
00974 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00975
00976 CommandCost ret = CheckOwnership(v->owner);
00977 if (ret.Failed()) return ret;
00978
00979 bool reset = StrEmpty(text);
00980
00981 if (!reset) {
00982 if (Utf8StringLength(text) >= MAX_LENGTH_VEHICLE_NAME_CHARS) return CMD_ERROR;
00983 if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00984 }
00985
00986 if (flags & DC_EXEC) {
00987 free(v->name);
00988 v->name = reset ? NULL : strdup(text);
00989 InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
00990 MarkWholeScreenDirty();
00991 }
00992
00993 return CommandCost();
00994 }
00995
00996
01006 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01007 {
01008 Vehicle *v = Vehicle::GetIfValid(p1);
01009 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01010
01011 CommandCost ret = CheckOwnership(v->owner);
01012 if (ret.Failed()) return ret;
01013
01014 uint16 serv_int = GetServiceIntervalClamped(p2, v->owner);
01015 if (serv_int != p2) return CMD_ERROR;
01016
01017 if (flags & DC_EXEC) {
01018 v->service_interval = serv_int;
01019 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
01020 }
01021
01022 return CommandCost();
01023 }