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 "train.h"
00020 #include "aircraft.h"
00021 #include "newgrf_text.h"
00022 #include "vehicle_func.h"
00023 #include "string_func.h"
00024 #include "depot_map.h"
00025 #include "vehiclelist.h"
00026 #include "engine_func.h"
00027 #include "articulated_vehicles.h"
00028 #include "autoreplace_gui.h"
00029 #include "group.h"
00030 #include "order_backup.h"
00031 #include "ship.h"
00032 #include "newgrf.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 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT),
00060 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
00061 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
00062 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
00063 };
00064
00065
00066 CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00067 CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00068 CommandCost CmdBuildShip (TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00069 CommandCost CmdBuildAircraft (TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
00070
00082 CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00083 {
00084
00085 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
00086
00087 VehicleType type;
00088 switch (GetTileType(tile)) {
00089 case MP_RAILWAY: type = VEH_TRAIN; break;
00090 case MP_ROAD: type = VEH_ROAD; break;
00091 case MP_WATER: type = VEH_SHIP; break;
00092 case MP_STATION: type = VEH_AIRCRAFT; break;
00093 default: NOT_REACHED();
00094 }
00095
00096
00097 EngineID eid = GB(p1, 0, 16);
00098 if (!IsEngineBuildable(eid, type, _current_company)) return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type);
00099
00100 const Engine *e = Engine::Get(eid);
00101 CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
00102
00103
00104 if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
00105
00106
00107 uint num_vehicles;
00108 switch (type) {
00109 case VEH_TRAIN: num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); break;
00110 case VEH_ROAD: num_vehicles = 1 + CountArticulatedParts(eid, false); break;
00111 case VEH_SHIP: num_vehicles = 1; break;
00112 case VEH_AIRCRAFT: num_vehicles = e->u.air.subtype & AIR_CTOL ? 2 : 3; break;
00113 default: NOT_REACHED();
00114 }
00115 if (!Vehicle::CanAllocateItem(num_vehicles)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00116
00117
00118
00119
00120 UnitID unit_num = (flags & DC_AUTOREPLACE || (type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON)) ? 0 : GetFreeUnitNumber(type);
00121 if (unit_num == UINT16_MAX) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00122
00123 Vehicle *v;
00124 switch (type) {
00125 case VEH_TRAIN: value.AddCost(CmdBuildRailVehicle(tile, flags, e, GB(p1, 16, 16), &v)); break;
00126 case VEH_ROAD: value.AddCost(CmdBuildRoadVehicle(tile, flags, e, GB(p1, 16, 16), &v)); break;
00127 case VEH_SHIP: value.AddCost(CmdBuildShip (tile, flags, e, GB(p1, 16, 16), &v)); break;
00128 case VEH_AIRCRAFT: value.AddCost(CmdBuildAircraft (tile, flags, e, GB(p1, 16, 16), &v)); break;
00129 default: NOT_REACHED();
00130 }
00131
00132 if (value.Succeeded() && flags & DC_EXEC) {
00133 v->unitnumber = unit_num;
00134 v->value = value.GetCost();
00135
00136 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00137 InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
00138 SetWindowDirty(WC_COMPANY, _current_company);
00139 if (IsLocalCompany()) {
00140 InvalidateAutoreplaceWindow(v->engine_type, v->group_id);
00141 }
00142
00143 GroupStatistics::CountEngine(v, 1);
00144 GroupStatistics::UpdateAutoreplace(_current_company);
00145
00146 if (v->IsPrimaryVehicle()) {
00147 GroupStatistics::CountVehicle(v, 1);
00148 OrderBackup::Restore(v, p2);
00149 }
00150 }
00151
00152 return value;
00153 }
00154
00155 CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16 data, uint32 user);
00156
00169 CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00170 {
00171 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00172 if (v == NULL) return CMD_ERROR;
00173
00174 Vehicle *front = v->First();
00175
00176 CommandCost ret = CheckOwnership(front->owner);
00177 if (ret.Failed()) return ret;
00178
00179 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00180
00181 if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00182
00183
00184 if (p1 & MAKE_ORDER_BACKUP_FLAG &&
00185 front->orders.list != NULL &&
00186 !front->orders.list->IsShared() &&
00187 !Order::CanAllocateItem(front->orders.list->GetNumOrders())) {
00188
00189
00190 p1 &= ~MAKE_ORDER_BACKUP_FLAG;
00191 }
00192
00193 if (v->type == VEH_TRAIN) {
00194 ret = CmdSellRailWagon(flags, v, GB(p1, 20, 12), p2);
00195 } else {
00196 ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
00197
00198 if (flags & DC_EXEC) {
00199 if (v->IsPrimaryVehicle() && p1 & MAKE_ORDER_BACKUP_FLAG) OrderBackup::Backup(v, p2);
00200 delete front;
00201 }
00202 }
00203
00204 return ret;
00205 }
00206
00216 static int GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype, bool *auto_refit_allowed)
00217 {
00218
00219 const Engine *e = Engine::Get(engine_type);
00220
00221
00222 if (e->GetGRF() != NULL) {
00223 const CargoSpec *cs = CargoSpec::Get(new_cid);
00224 uint32 param1 = (cs->classes << 16) | (new_subtype << 8) | e->GetGRF()->cargo_map[new_cid];
00225
00226 uint16 cb_res = GetVehicleCallback(CBID_VEHICLE_REFIT_COST, param1, 0, engine_type, v);
00227 if (cb_res != CALLBACK_FAILED) {
00228 *auto_refit_allowed = HasBit(cb_res, 14);
00229 int factor = GB(cb_res, 0, 14);
00230 if (factor >= 0x2000) factor -= 0x4000;
00231 return factor;
00232 }
00233 }
00234
00235 *auto_refit_allowed = e->info.refit_cost == 0;
00236 return (v == NULL || v->cargo_type != new_cid) ? e->info.refit_cost : 0;
00237 }
00238
00248 static CommandCost GetRefitCost(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype, bool *auto_refit_allowed)
00249 {
00250 ExpensesType expense_type;
00251 const Engine *e = Engine::Get(engine_type);
00252 Price base_price;
00253 int cost_factor = GetRefitCostFactor(v, engine_type, new_cid, new_subtype, auto_refit_allowed);
00254 switch (e->type) {
00255 case VEH_SHIP:
00256 base_price = PR_BUILD_VEHICLE_SHIP;
00257 expense_type = EXPENSES_SHIP_RUN;
00258 break;
00259
00260 case VEH_ROAD:
00261 base_price = PR_BUILD_VEHICLE_ROAD;
00262 expense_type = EXPENSES_ROADVEH_RUN;
00263 break;
00264
00265 case VEH_AIRCRAFT:
00266 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00267 expense_type = EXPENSES_AIRCRAFT_RUN;
00268 break;
00269
00270 case VEH_TRAIN:
00271 base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
00272 cost_factor <<= 1;
00273 expense_type = EXPENSES_TRAIN_RUN;
00274 break;
00275
00276 default: NOT_REACHED();
00277 }
00278 if (cost_factor < 0) {
00279 return CommandCost(expense_type, -GetPrice(base_price, -cost_factor, e->GetGRF(), -10));
00280 } else {
00281 return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->GetGRF(), -10));
00282 }
00283 }
00284
00286 struct RefitResult {
00287 Vehicle *v;
00288 uint capacity;
00289 uint mail_capacity;
00290 };
00291
00304 static CommandCost RefitVehicle(Vehicle *v, bool only_this, uint8 num_vehicles, CargoID new_cid, byte new_subtype, DoCommandFlag flags, bool auto_refit)
00305 {
00306 CommandCost cost(v->GetExpenseType(false));
00307 uint total_capacity = 0;
00308 uint total_mail_capacity = 0;
00309 num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
00310
00311 VehicleSet vehicles_to_refit;
00312 if (!only_this) {
00313 GetVehicleSet(vehicles_to_refit, v, num_vehicles);
00314
00315 v = v->First();
00316 }
00317
00318 static SmallVector<RefitResult, 16> refit_result;
00319 refit_result.Clear();
00320
00321 v->InvalidateNewGRFCacheOfChain();
00322 for (; v != NULL; v = (only_this ? NULL : v->Next())) {
00323 if (v->type == VEH_TRAIN && !vehicles_to_refit.Contains(v->index) && !only_this) continue;
00324
00325 const Engine *e = v->GetEngine();
00326 if (!e->CanCarryCargo()) continue;
00327
00328
00329
00330 bool refittable = HasBit(e->info.refit_mask, new_cid) && (!auto_refit || HasBit(e->info.misc_flags, EF_AUTO_REFIT));
00331 if (!refittable && v->cargo_type != new_cid) continue;
00332
00333
00334 CargoID temp_cid = v->cargo_type;
00335 byte temp_subtype = v->cargo_subtype;
00336 if (refittable) {
00337 v->cargo_type = new_cid;
00338 v->cargo_subtype = new_subtype;
00339 }
00340
00341 uint16 mail_capacity = 0;
00342 uint amount = e->DetermineCapacity(v, &mail_capacity);
00343 total_capacity += amount;
00344
00345 total_mail_capacity += mail_capacity;
00346
00347 if (!refittable) continue;
00348
00349
00350 v->cargo_type = temp_cid;
00351 v->cargo_subtype = temp_subtype;
00352
00353 bool auto_refit_allowed;
00354 CommandCost refit_cost = GetRefitCost(v, v->engine_type, new_cid, new_subtype, &auto_refit_allowed);
00355 if (auto_refit && !auto_refit_allowed) {
00356
00357 total_capacity -= amount;
00358 total_mail_capacity -= mail_capacity;
00359
00360 if (v->cargo_type == new_cid) {
00361
00362 total_capacity += v->cargo_cap;
00363 if (v->type == VEH_AIRCRAFT) total_mail_capacity += v->Next()->cargo_cap;
00364 }
00365 continue;
00366 }
00367 cost.AddCost(refit_cost);
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 RefitResult *result = refit_result.Append();
00379 result->v = v;
00380 result->capacity = amount;
00381 result->mail_capacity = mail_capacity;
00382 }
00383
00384 if (flags & DC_EXEC) {
00385
00386 for (RefitResult *result = refit_result.Begin(); result != refit_result.End(); result++) {
00387 Vehicle *u = result->v;
00388 u->refit_cap = (u->cargo_type == new_cid) ? min(result->capacity, u->cargo_cap) : 0;
00389 u->cargo.Truncate(u->refit_cap);
00390 u->cargo_type = new_cid;
00391 u->cargo_cap = result->capacity;
00392 u->cargo_subtype = new_subtype;
00393 if (u->type == VEH_AIRCRAFT) {
00394 Vehicle *w = u->Next();
00395 w->refit_cap = min(w->cargo_cap, result->mail_capacity);
00396 w->cargo_cap = result->mail_capacity;
00397 w->cargo.Truncate(result->mail_capacity);
00398 }
00399 }
00400 }
00401
00402 refit_result.Clear();
00403 _returned_refit_capacity = total_capacity;
00404 _returned_mail_refit_capacity = total_mail_capacity;
00405 return cost;
00406 }
00407
00423 CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00424 {
00425 Vehicle *v = Vehicle::GetIfValid(p1);
00426 if (v == NULL) return CMD_ERROR;
00427
00428
00429
00430 if (!IsCompanyBuildableVehicleType(v->type)) return CMD_ERROR;
00431
00432 Vehicle *front = v->First();
00433
00434 CommandCost ret = CheckOwnership(front->owner);
00435 if (ret.Failed()) return ret;
00436
00437 bool auto_refit = HasBit(p2, 6);
00438
00439
00440 if (v != front && (v->type == VEH_SHIP || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
00441
00442 if ((!auto_refit || !front->current_order.IsType(OT_LOADING)) && !front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00443 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00444
00445
00446 CargoID new_cid = GB(p2, 0, 5);
00447 byte new_subtype = GB(p2, 8, 8);
00448 if (new_cid >= NUM_CARGO) return CMD_ERROR;
00449
00450
00451 bool only_this = HasBit(p2, 7) || front->type == VEH_SHIP || front->type == VEH_AIRCRAFT;
00452 uint8 num_vehicles = GB(p2, 16, 8);
00453
00454 CommandCost cost = RefitVehicle(v, only_this, num_vehicles, new_cid, new_subtype, flags, auto_refit);
00455
00456 if (flags & DC_EXEC) {
00457
00458 switch (v->type) {
00459 case VEH_TRAIN:
00460 Train::From(front)->ConsistChanged(auto_refit);
00461 break;
00462 case VEH_ROAD:
00463 RoadVehUpdateCache(RoadVehicle::From(front), auto_refit);
00464 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) RoadVehicle::From(front)->CargoChanged();
00465 break;
00466
00467 case VEH_SHIP:
00468 v->InvalidateNewGRFCacheOfChain();
00469 v->colourmap = PAL_NONE;
00470 Ship::From(v)->UpdateCache();
00471 break;
00472
00473 case VEH_AIRCRAFT:
00474 v->InvalidateNewGRFCacheOfChain();
00475 v->colourmap = PAL_NONE;
00476 UpdateAircraftCache(Aircraft::From(v), true);
00477 break;
00478
00479 default: NOT_REACHED();
00480 }
00481
00482 InvalidateWindowData(WC_VEHICLE_DETAILS, front->index);
00483 SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
00484 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00485 } else {
00486
00487 v->InvalidateNewGRFCacheOfChain();
00488 }
00489
00490 return cost;
00491 }
00492
00502 CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00503 {
00504
00505 if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
00506
00507 Vehicle *v = Vehicle::GetIfValid(p1);
00508 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00509
00510 CommandCost ret = CheckOwnership(v->owner);
00511 if (ret.Failed()) return ret;
00512
00513 if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00514
00515 switch (v->type) {
00516 case VEH_TRAIN:
00517 if ((v->vehstatus & VS_STOPPED) && Train::From(v)->gcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_POWER);
00518 break;
00519
00520 case VEH_SHIP:
00521 case VEH_ROAD:
00522 break;
00523
00524 case VEH_AIRCRAFT: {
00525 Aircraft *a = Aircraft::From(v);
00526
00527 if (!(v->vehstatus & VS_CRASHED) && a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
00528 break;
00529 }
00530
00531 default: return CMD_ERROR;
00532 }
00533
00534 if (HasBit(p2, 0)) {
00535
00536 uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
00537 StringID error = STR_NULL;
00538 if (callback != CALLBACK_FAILED) {
00539 if (v->GetGRF()->grf_version < 8) {
00540
00541 if (callback < 0x400 && GB(callback, 0, 8) != 0xFF) error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
00542 } else {
00543 if (callback < 0x400) {
00544 error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
00545 } else {
00546 switch (callback) {
00547 case 0x400:
00548 break;
00549
00550 default:
00551 error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
00552 break;
00553 }
00554 }
00555 }
00556 }
00557 if (error != STR_NULL) return_cmd_error(error);
00558 }
00559
00560 if (flags & DC_EXEC) {
00561 if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
00562
00563 v->vehstatus ^= VS_STOPPED;
00564 if (v->type != VEH_TRAIN) v->cur_speed = 0;
00565 v->MarkDirty();
00566 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
00567 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00568 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
00569 }
00570 return CommandCost();
00571 }
00572
00584 CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00585 {
00586 VehicleList list;
00587 bool do_start = HasBit(p1, 0);
00588 bool vehicle_list_window = HasBit(p1, 1);
00589
00590 VehicleListIdentifier vli;
00591 if (!vli.Unpack(p2)) return CMD_ERROR;
00592 if (!IsCompanyBuildableVehicleType(vli.vtype)) return CMD_ERROR;
00593
00594 if (vehicle_list_window) {
00595 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00596 } else {
00597
00598 BuildDepotVehicleList(vli.vtype, tile, &list, NULL);
00599 }
00600
00601 for (uint i = 0; i < list.Length(); i++) {
00602 const Vehicle *v = list[i];
00603
00604 if (!!(v->vehstatus & VS_STOPPED) != do_start) continue;
00605
00606 if (!vehicle_list_window) {
00607 if (vli.vtype == VEH_TRAIN) {
00608 if (!Train::From(v)->IsInDepot()) continue;
00609 } else {
00610 if (!(v->vehstatus & VS_HIDDEN)) continue;
00611 }
00612 }
00613
00614
00615 DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
00616 }
00617
00618 return CommandCost();
00619 }
00620
00630 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00631 {
00632 VehicleList list;
00633
00634 CommandCost cost(EXPENSES_NEW_VEHICLES);
00635 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00636
00637 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00638
00639 uint sell_command = GetCmdSellVeh(vehicle_type);
00640
00641
00642 BuildDepotVehicleList(vehicle_type, tile, &list, &list);
00643
00644 CommandCost last_error = CMD_ERROR;
00645 bool had_success = false;
00646 for (uint i = 0; i < list.Length(); i++) {
00647 CommandCost ret = DoCommand(tile, list[i]->index | (1 << 20), 0, flags, sell_command);
00648 if (ret.Succeeded()) {
00649 cost.AddCost(ret);
00650 had_success = true;
00651 } else {
00652 last_error = ret;
00653 }
00654 }
00655
00656 return had_success ? cost : last_error;
00657 }
00658
00668 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00669 {
00670 VehicleList list;
00671 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
00672 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00673
00674 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00675 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
00676
00677
00678 BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
00679
00680 for (uint i = 0; i < list.Length(); i++) {
00681 const Vehicle *v = list[i];
00682
00683
00684 if (!v->IsInDepot()) continue;
00685
00686 CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
00687
00688 if (ret.Succeeded()) cost.AddCost(ret);
00689 }
00690 return cost;
00691 }
00692
00698 static bool IsUniqueVehicleName(const char *name)
00699 {
00700 const Vehicle *v;
00701
00702 FOR_ALL_VEHICLES(v) {
00703 if (v->name != NULL && strcmp(v->name, name) == 0) return false;
00704 }
00705
00706 return true;
00707 }
00708
00714 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
00715 {
00716 char buf[256];
00717
00718
00719 size_t number_position;
00720 for (number_position = strlen(src->name); number_position > 0; number_position--) {
00721
00722
00723 if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
00724 }
00725
00726
00727 int num;
00728 byte padding = 0;
00729 if (number_position == strlen(src->name)) {
00730
00731 strecpy(buf, src->name, lastof(buf));
00732 strecat(buf, " ", lastof(buf));
00733 number_position = strlen(buf);
00734 num = 2;
00735 } else {
00736
00737 strecpy(buf, src->name, lastof(buf));
00738 buf[number_position] = '\0';
00739 char *endptr;
00740 num = strtol(&src->name[number_position], &endptr, 10) + 1;
00741 padding = endptr - &src->name[number_position];
00742 }
00743
00744
00745 for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
00746
00747 seprintf(&buf[number_position], lastof(buf), "%0*d", padding, num);
00748
00749
00750 if (IsUniqueVehicleName(buf)) {
00751 dst->name = strdup(buf);
00752 break;
00753 }
00754 }
00755
00756
00757 }
00758
00768 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00769 {
00770 CommandCost total_cost(EXPENSES_NEW_VEHICLES);
00771
00772 Vehicle *v = Vehicle::GetIfValid(p1);
00773 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00774 Vehicle *v_front = v;
00775 Vehicle *w = NULL;
00776 Vehicle *w_front = NULL;
00777 Vehicle *w_rear = NULL;
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787 CommandCost ret = CheckOwnership(v->owner);
00788 if (ret.Failed()) return ret;
00789
00790 if (v->type == VEH_TRAIN && (!v->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
00791
00792
00793 if (!(flags & DC_EXEC)) {
00794 int veh_counter = 0;
00795 do {
00796 veh_counter++;
00797 } while ((v = v->Next()) != NULL);
00798
00799 if (!Vehicle::CanAllocateItem(veh_counter)) {
00800 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00801 }
00802 }
00803
00804 v = v_front;
00805
00806 do {
00807 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00808
00809 continue;
00810 }
00811
00812
00813
00814
00815
00816
00817
00818 DoCommandFlag build_flags = flags;
00819 if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
00820
00821 CommandCost cost = DoCommand(tile, v->engine_type | (1 << 16), 0, build_flags, GetCmdBuildVeh(v));
00822
00823 if (cost.Failed()) {
00824
00825 if (w_front != NULL) DoCommand(w_front->tile, w_front->index | (1 << 20), 0, flags, GetCmdSellVeh(w_front));
00826 return cost;
00827 }
00828
00829 total_cost.AddCost(cost);
00830
00831 if (flags & DC_EXEC) {
00832 w = Vehicle::Get(_new_vehicle_id);
00833
00834 if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
00835 SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
00836 }
00837
00838 if (v->type == VEH_TRAIN && !v->IsFrontEngine()) {
00839
00840
00841 CommandCost result = DoCommand(0, w->index | 1 << 20, w_rear->index, flags, CMD_MOVE_RAIL_VEHICLE);
00842 if (result.Failed()) {
00843
00844
00845 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00846 DoCommand(w_front->tile, w->index | 1 << 20, 0, flags, GetCmdSellVeh(w));
00847 return result;
00848 }
00849 } else {
00850
00851 w_front = w;
00852 w->service_interval = v->service_interval;
00853 }
00854 w_rear = w;
00855 }
00856 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00857
00858 if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
00859
00860 _new_vehicle_id = w_front->index;
00861 }
00862
00863 if (flags & DC_EXEC) {
00864
00865 DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
00866 }
00867
00868
00869
00870 w = w_front;
00871 v = v_front;
00872
00873
00874
00875
00876
00877
00878
00879 do {
00880 do {
00881 if (flags & DC_EXEC) {
00882 assert(w != NULL);
00883
00884
00885 byte subtype = GetBestFittingSubType(v, w, v->cargo_type);
00886 if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
00887 CommandCost cost = DoCommand(0, w->index, v->cargo_type | 1U << 7 | (subtype << 8), flags, GetCmdRefitVeh(v));
00888 if (cost.Succeeded()) total_cost.AddCost(cost);
00889 }
00890
00891 if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
00892 w = w->GetNextArticulatedPart();
00893 } else {
00894 break;
00895 }
00896 } else {
00897 const Engine *e = v->GetEngine();
00898 CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00899
00900 if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
00901 bool dummy;
00902 total_cost.AddCost(GetRefitCost(NULL, v->engine_type, v->cargo_type, v->cargo_subtype, &dummy));
00903 }
00904 }
00905
00906 if (v->IsGroundVehicle() && v->HasArticulatedPart()) {
00907 v = v->GetNextArticulatedPart();
00908 } else {
00909 break;
00910 }
00911 } while (v != NULL);
00912
00913 if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = w->GetNextVehicle();
00914 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00915
00916 if (flags & DC_EXEC) {
00917
00918
00919
00920
00921
00922 DoCommand(0, w_front->index | (p2 & 1 ? CO_SHARE : CO_COPY) << 30, v_front->index, flags, CMD_CLONE_ORDER);
00923
00924
00925 if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
00926 }
00927
00928
00929
00930 if (!CheckCompanyHasMoney(total_cost)) {
00931 if (flags & DC_EXEC) {
00932
00933 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00934 }
00935 return total_cost;
00936 }
00937
00938 return total_cost;
00939 }
00940
00948 static CommandCost SendAllVehiclesToDepot(DoCommandFlag flags, bool service, const VehicleListIdentifier &vli)
00949 {
00950 VehicleList list;
00951
00952 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00953
00954
00955 bool had_success = false;
00956 for (uint i = 0; i < list.Length(); i++) {
00957 const Vehicle *v = list[i];
00958 CommandCost ret = DoCommand(v->tile, v->index | (service ? DEPOT_SERVICE : 0U) | DEPOT_DONT_CANCEL, 0, flags, GetCmdSendToDepot(vli.vtype));
00959
00960 if (ret.Succeeded()) {
00961 had_success = true;
00962
00963
00964
00965
00966
00967 if (!(flags & DC_EXEC)) break;
00968 }
00969 }
00970
00971 return had_success ? CommandCost() : CMD_ERROR;
00972 }
00973
00985 CommandCost CmdSendVehicleToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00986 {
00987 if (p1 & DEPOT_MASS_SEND) {
00988
00989 VehicleListIdentifier vli;
00990 if (!vli.Unpack(p2)) return CMD_ERROR;
00991 return SendAllVehiclesToDepot(flags, (p1 & DEPOT_SERVICE) != 0, vli);
00992 }
00993
00994 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00995 if (v == NULL) return CMD_ERROR;
00996
00997 return v->SendToDepot(flags, (DepotCommand)(p1 & DEPOT_COMMAND_MASK));
00998 }
00999
01009 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01010 {
01011 Vehicle *v = Vehicle::GetIfValid(p1);
01012 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01013
01014 CommandCost ret = CheckOwnership(v->owner);
01015 if (ret.Failed()) return ret;
01016
01017 bool reset = StrEmpty(text);
01018
01019 if (!reset) {
01020 if (Utf8StringLength(text) >= MAX_LENGTH_VEHICLE_NAME_CHARS) return CMD_ERROR;
01021 if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01022 }
01023
01024 if (flags & DC_EXEC) {
01025 free(v->name);
01026 v->name = reset ? NULL : strdup(text);
01027 InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
01028 MarkWholeScreenDirty();
01029 }
01030
01031 return CommandCost();
01032 }
01033
01034
01044 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01045 {
01046 Vehicle *v = Vehicle::GetIfValid(p1);
01047 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01048
01049 CommandCost ret = CheckOwnership(v->owner);
01050 if (ret.Failed()) return ret;
01051
01052 uint16 serv_int = GetServiceIntervalClamped(p2, v->owner);
01053 if (serv_int != p2) return CMD_ERROR;
01054
01055 if (flags & DC_EXEC) {
01056 v->service_interval = serv_int;
01057 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
01058 }
01059
01060 return CommandCost();
01061 }