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 #include "company_base.h"
00034
00035 #include "table/strings.h"
00036
00037
00038 const uint32 _veh_build_proc_table[] = {
00039 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
00040 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
00041 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
00042 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
00043 };
00044
00045 const uint32 _veh_sell_proc_table[] = {
00046 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
00047 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
00048 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
00049 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
00050 };
00051
00052 const uint32 _veh_refit_proc_table[] = {
00053 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
00054 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
00055 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
00056 CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
00057 };
00058
00059 const uint32 _send_to_depot_proc_table[] = {
00060 CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT),
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 GroupStatistics::CountEngine(v, 1);
00145 GroupStatistics::UpdateAutoreplace(_current_company);
00146
00147 if (v->IsPrimaryVehicle()) {
00148 GroupStatistics::CountVehicle(v, 1);
00149 OrderBackup::Restore(v, p2);
00150 }
00151 }
00152
00153 return value;
00154 }
00155
00156 CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16 data, uint32 user);
00157
00170 CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00171 {
00172 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00173 if (v == NULL) return CMD_ERROR;
00174
00175 Vehicle *front = v->First();
00176
00177 CommandCost ret = CheckOwnership(front->owner);
00178 if (ret.Failed()) return ret;
00179
00180 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00181
00182 if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00183
00184
00185 if (p1 & MAKE_ORDER_BACKUP_FLAG &&
00186 front->orders.list != NULL &&
00187 !front->orders.list->IsShared() &&
00188 !Order::CanAllocateItem(front->orders.list->GetNumOrders())) {
00189
00190
00191 p1 &= ~MAKE_ORDER_BACKUP_FLAG;
00192 }
00193
00194 if (v->type == VEH_TRAIN) {
00195 ret = CmdSellRailWagon(flags, v, GB(p1, 20, 12), p2);
00196 } else {
00197 ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
00198
00199 if (flags & DC_EXEC) {
00200 if (front->IsPrimaryVehicle() && p1 & MAKE_ORDER_BACKUP_FLAG) OrderBackup::Backup(front, p2);
00201 delete front;
00202 }
00203 }
00204
00205 return ret;
00206 }
00207
00217 static int GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype, bool *auto_refit_allowed)
00218 {
00219
00220 const Engine *e = Engine::Get(engine_type);
00221
00222
00223 if (e->GetGRF() != NULL) {
00224 const CargoSpec *cs = CargoSpec::Get(new_cid);
00225 uint32 param1 = (cs->classes << 16) | (new_subtype << 8) | e->GetGRF()->cargo_map[new_cid];
00226
00227 uint16 cb_res = GetVehicleCallback(CBID_VEHICLE_REFIT_COST, param1, 0, engine_type, v);
00228 if (cb_res != CALLBACK_FAILED) {
00229 *auto_refit_allowed = HasBit(cb_res, 14);
00230 int factor = GB(cb_res, 0, 14);
00231 if (factor >= 0x2000) factor -= 0x4000;
00232 return factor;
00233 }
00234 }
00235
00236 *auto_refit_allowed = e->info.refit_cost == 0;
00237 return (v == NULL || v->cargo_type != new_cid) ? e->info.refit_cost : 0;
00238 }
00239
00249 static CommandCost GetRefitCost(const Vehicle *v, EngineID engine_type, CargoID new_cid, byte new_subtype, bool *auto_refit_allowed)
00250 {
00251 ExpensesType expense_type;
00252 const Engine *e = Engine::Get(engine_type);
00253 Price base_price;
00254 int cost_factor = GetRefitCostFactor(v, engine_type, new_cid, new_subtype, auto_refit_allowed);
00255 switch (e->type) {
00256 case VEH_SHIP:
00257 base_price = PR_BUILD_VEHICLE_SHIP;
00258 expense_type = EXPENSES_SHIP_RUN;
00259 break;
00260
00261 case VEH_ROAD:
00262 base_price = PR_BUILD_VEHICLE_ROAD;
00263 expense_type = EXPENSES_ROADVEH_RUN;
00264 break;
00265
00266 case VEH_AIRCRAFT:
00267 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00268 expense_type = EXPENSES_AIRCRAFT_RUN;
00269 break;
00270
00271 case VEH_TRAIN:
00272 base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
00273 cost_factor <<= 1;
00274 expense_type = EXPENSES_TRAIN_RUN;
00275 break;
00276
00277 default: NOT_REACHED();
00278 }
00279 if (cost_factor < 0) {
00280 return CommandCost(expense_type, -GetPrice(base_price, -cost_factor, e->GetGRF(), -10));
00281 } else {
00282 return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->GetGRF(), -10));
00283 }
00284 }
00285
00287 struct RefitResult {
00288 Vehicle *v;
00289 uint capacity;
00290 uint mail_capacity;
00291 };
00292
00305 static CommandCost RefitVehicle(Vehicle *v, bool only_this, uint8 num_vehicles, CargoID new_cid, byte new_subtype, DoCommandFlag flags, bool auto_refit)
00306 {
00307 CommandCost cost(v->GetExpenseType(false));
00308 uint total_capacity = 0;
00309 uint total_mail_capacity = 0;
00310 num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
00311
00312 VehicleSet vehicles_to_refit;
00313 if (!only_this) {
00314 GetVehicleSet(vehicles_to_refit, v, num_vehicles);
00315
00316 v = v->First();
00317 }
00318
00319 static SmallVector<RefitResult, 16> refit_result;
00320 refit_result.Clear();
00321
00322 v->InvalidateNewGRFCacheOfChain();
00323 for (; v != NULL; v = (only_this ? NULL : v->Next())) {
00324 if (v->type == VEH_TRAIN && !vehicles_to_refit.Contains(v->index) && !only_this) continue;
00325
00326 const Engine *e = v->GetEngine();
00327 if (!e->CanCarryCargo()) continue;
00328
00329
00330
00331 bool refittable = HasBit(e->info.refit_mask, new_cid) && (!auto_refit || HasBit(e->info.misc_flags, EF_AUTO_REFIT));
00332 if (!refittable && v->cargo_type != new_cid) continue;
00333
00334
00335 CargoID temp_cid = v->cargo_type;
00336 byte temp_subtype = v->cargo_subtype;
00337 if (refittable) {
00338 v->cargo_type = new_cid;
00339 v->cargo_subtype = new_subtype;
00340 }
00341
00342 uint16 mail_capacity = 0;
00343 uint amount = e->DetermineCapacity(v, &mail_capacity);
00344 total_capacity += amount;
00345
00346 total_mail_capacity += mail_capacity;
00347
00348 if (!refittable) continue;
00349
00350
00351 v->cargo_type = temp_cid;
00352 v->cargo_subtype = temp_subtype;
00353
00354 bool auto_refit_allowed;
00355 CommandCost refit_cost = GetRefitCost(v, v->engine_type, new_cid, new_subtype, &auto_refit_allowed);
00356 if (auto_refit && !auto_refit_allowed) {
00357
00358 total_capacity -= amount;
00359 total_mail_capacity -= mail_capacity;
00360
00361 if (v->cargo_type == new_cid) {
00362
00363 total_capacity += v->cargo_cap;
00364 if (v->type == VEH_AIRCRAFT) total_mail_capacity += v->Next()->cargo_cap;
00365 }
00366 continue;
00367 }
00368 cost.AddCost(refit_cost);
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 RefitResult *result = refit_result.Append();
00380 result->v = v;
00381 result->capacity = amount;
00382 result->mail_capacity = mail_capacity;
00383 }
00384
00385 if (flags & DC_EXEC) {
00386
00387 for (RefitResult *result = refit_result.Begin(); result != refit_result.End(); result++) {
00388 Vehicle *u = result->v;
00389 u->refit_cap = (u->cargo_type == new_cid) ? min(result->capacity, u->refit_cap) : 0;
00390 if (u->cargo.Count() > u->refit_cap) u->cargo.Truncate(u->cargo.Count() - u->refit_cap);
00391 u->cargo_type = new_cid;
00392 u->cargo_cap = result->capacity;
00393 u->cargo_subtype = new_subtype;
00394 if (u->type == VEH_AIRCRAFT) {
00395 Vehicle *w = u->Next();
00396 w->refit_cap = min(w->refit_cap, result->mail_capacity);
00397 w->cargo_cap = result->mail_capacity;
00398 if (w->cargo.Count() > w->refit_cap) w->cargo.Truncate(w->cargo.Count() - w->refit_cap);
00399 }
00400 }
00401 }
00402
00403 refit_result.Clear();
00404 _returned_refit_capacity = total_capacity;
00405 _returned_mail_refit_capacity = total_mail_capacity;
00406 return cost;
00407 }
00408
00424 CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00425 {
00426 Vehicle *v = Vehicle::GetIfValid(p1);
00427 if (v == NULL) return CMD_ERROR;
00428
00429
00430
00431 if (!IsCompanyBuildableVehicleType(v->type)) return CMD_ERROR;
00432
00433 Vehicle *front = v->First();
00434
00435 CommandCost ret = CheckOwnership(front->owner);
00436 if (ret.Failed()) return ret;
00437
00438 bool auto_refit = HasBit(p2, 6);
00439
00440
00441 if (v != front && (v->type == VEH_SHIP || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
00442
00443 if ((!auto_refit || !front->current_order.IsType(OT_LOADING)) && !front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
00444 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00445
00446
00447 CargoID new_cid = GB(p2, 0, 5);
00448 byte new_subtype = GB(p2, 8, 8);
00449 if (new_cid >= NUM_CARGO) return CMD_ERROR;
00450
00451
00452 bool only_this = HasBit(p2, 7) || front->type == VEH_SHIP || front->type == VEH_AIRCRAFT;
00453 uint8 num_vehicles = GB(p2, 16, 8);
00454
00455 CommandCost cost = RefitVehicle(v, only_this, num_vehicles, new_cid, new_subtype, flags, auto_refit);
00456
00457 if (flags & DC_EXEC) {
00458
00459 switch (v->type) {
00460 case VEH_TRAIN:
00461 Train::From(front)->ConsistChanged(auto_refit);
00462 break;
00463 case VEH_ROAD:
00464 RoadVehUpdateCache(RoadVehicle::From(front), auto_refit);
00465 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) RoadVehicle::From(front)->CargoChanged();
00466 break;
00467
00468 case VEH_SHIP:
00469 v->InvalidateNewGRFCacheOfChain();
00470 v->colourmap = PAL_NONE;
00471 Ship::From(v)->UpdateCache();
00472 break;
00473
00474 case VEH_AIRCRAFT:
00475 v->InvalidateNewGRFCacheOfChain();
00476 v->colourmap = PAL_NONE;
00477 UpdateAircraftCache(Aircraft::From(v), true);
00478 break;
00479
00480 default: NOT_REACHED();
00481 }
00482
00483 InvalidateWindowData(WC_VEHICLE_DETAILS, front->index);
00484 SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
00485 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00486 } else {
00487
00488 v->InvalidateNewGRFCacheOfChain();
00489 }
00490
00491 return cost;
00492 }
00493
00503 CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00504 {
00505
00506 if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
00507
00508 Vehicle *v = Vehicle::GetIfValid(p1);
00509 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00510
00511 CommandCost ret = CheckOwnership(v->owner);
00512 if (ret.Failed()) return ret;
00513
00514 if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
00515
00516 switch (v->type) {
00517 case VEH_TRAIN:
00518 if ((v->vehstatus & VS_STOPPED) && Train::From(v)->gcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_POWER);
00519 break;
00520
00521 case VEH_SHIP:
00522 case VEH_ROAD:
00523 break;
00524
00525 case VEH_AIRCRAFT: {
00526 Aircraft *a = Aircraft::From(v);
00527
00528 if (!(v->vehstatus & VS_CRASHED) && a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
00529 break;
00530 }
00531
00532 default: return CMD_ERROR;
00533 }
00534
00535 if (HasBit(p2, 0)) {
00536
00537 uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
00538 StringID error = STR_NULL;
00539 if (callback != CALLBACK_FAILED) {
00540 if (v->GetGRF()->grf_version < 8) {
00541
00542 if (callback < 0x400 && GB(callback, 0, 8) != 0xFF) error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
00543 } else {
00544 if (callback < 0x400) {
00545 error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
00546 } else {
00547 switch (callback) {
00548 case 0x400:
00549 break;
00550
00551 default:
00552 error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
00553 break;
00554 }
00555 }
00556 }
00557 }
00558 if (error != STR_NULL) return_cmd_error(error);
00559 }
00560
00561 if (flags & DC_EXEC) {
00562 if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
00563
00564 v->vehstatus ^= VS_STOPPED;
00565 if (v->type != VEH_TRAIN) v->cur_speed = 0;
00566 v->MarkDirty();
00567 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
00568 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00569 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
00570 }
00571 return CommandCost();
00572 }
00573
00585 CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00586 {
00587 VehicleList list;
00588 bool do_start = HasBit(p1, 0);
00589 bool vehicle_list_window = HasBit(p1, 1);
00590
00591 VehicleListIdentifier vli;
00592 if (!vli.Unpack(p2)) return CMD_ERROR;
00593 if (!IsCompanyBuildableVehicleType(vli.vtype)) return CMD_ERROR;
00594
00595 if (vehicle_list_window) {
00596 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00597 } else {
00598
00599 BuildDepotVehicleList(vli.vtype, tile, &list, NULL);
00600 }
00601
00602 for (uint i = 0; i < list.Length(); i++) {
00603 const Vehicle *v = list[i];
00604
00605 if (!!(v->vehstatus & VS_STOPPED) != do_start) continue;
00606
00607 if (!vehicle_list_window && !v->IsChainInDepot()) continue;
00608
00609
00610 DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
00611 }
00612
00613 return CommandCost();
00614 }
00615
00625 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00626 {
00627 VehicleList list;
00628
00629 CommandCost cost(EXPENSES_NEW_VEHICLES);
00630 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00631
00632 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00633
00634 uint sell_command = GetCmdSellVeh(vehicle_type);
00635
00636
00637 BuildDepotVehicleList(vehicle_type, tile, &list, &list);
00638
00639 CommandCost last_error = CMD_ERROR;
00640 bool had_success = false;
00641 for (uint i = 0; i < list.Length(); i++) {
00642 CommandCost ret = DoCommand(tile, list[i]->index | (1 << 20), 0, flags, sell_command);
00643 if (ret.Succeeded()) {
00644 cost.AddCost(ret);
00645 had_success = true;
00646 } else {
00647 last_error = ret;
00648 }
00649 }
00650
00651 return had_success ? cost : last_error;
00652 }
00653
00663 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00664 {
00665 VehicleList list;
00666 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
00667 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
00668
00669 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
00670 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
00671
00672
00673 BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
00674
00675 for (uint i = 0; i < list.Length(); i++) {
00676 const Vehicle *v = list[i];
00677
00678
00679 if (!v->IsChainInDepot()) continue;
00680
00681 CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
00682
00683 if (ret.Succeeded()) cost.AddCost(ret);
00684 }
00685 return cost;
00686 }
00687
00693 static bool IsUniqueVehicleName(const char *name)
00694 {
00695 const Vehicle *v;
00696
00697 FOR_ALL_VEHICLES(v) {
00698 if (v->name != NULL && strcmp(v->name, name) == 0) return false;
00699 }
00700
00701 return true;
00702 }
00703
00709 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
00710 {
00711 char buf[256];
00712
00713
00714 size_t number_position;
00715 for (number_position = strlen(src->name); number_position > 0; number_position--) {
00716
00717
00718 if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
00719 }
00720
00721
00722 int num;
00723 byte padding = 0;
00724 if (number_position == strlen(src->name)) {
00725
00726 strecpy(buf, src->name, lastof(buf));
00727 strecat(buf, " ", lastof(buf));
00728 number_position = strlen(buf);
00729 num = 2;
00730 } else {
00731
00732 strecpy(buf, src->name, lastof(buf));
00733 buf[number_position] = '\0';
00734 char *endptr;
00735 num = strtol(&src->name[number_position], &endptr, 10) + 1;
00736 padding = endptr - &src->name[number_position];
00737 }
00738
00739
00740 for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
00741
00742 seprintf(&buf[number_position], lastof(buf), "%0*d", padding, num);
00743
00744
00745 if (IsUniqueVehicleName(buf)) {
00746 dst->name = strdup(buf);
00747 break;
00748 }
00749 }
00750
00751
00752 }
00753
00763 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00764 {
00765 CommandCost total_cost(EXPENSES_NEW_VEHICLES);
00766
00767 Vehicle *v = Vehicle::GetIfValid(p1);
00768 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00769 Vehicle *v_front = v;
00770 Vehicle *w = NULL;
00771 Vehicle *w_front = NULL;
00772 Vehicle *w_rear = NULL;
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782 CommandCost ret = CheckOwnership(v->owner);
00783 if (ret.Failed()) return ret;
00784
00785 if (v->type == VEH_TRAIN && (!v->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
00786
00787
00788 if (!(flags & DC_EXEC)) {
00789 int veh_counter = 0;
00790 do {
00791 veh_counter++;
00792 } while ((v = v->Next()) != NULL);
00793
00794 if (!Vehicle::CanAllocateItem(veh_counter)) {
00795 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00796 }
00797 }
00798
00799 v = v_front;
00800
00801 do {
00802 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00803
00804 continue;
00805 }
00806
00807
00808
00809
00810
00811
00812
00813 DoCommandFlag build_flags = flags;
00814 if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
00815
00816 CommandCost cost = DoCommand(tile, v->engine_type | (1 << 16), 0, build_flags, GetCmdBuildVeh(v));
00817
00818 if (cost.Failed()) {
00819
00820 if (w_front != NULL) DoCommand(w_front->tile, w_front->index | (1 << 20), 0, flags, GetCmdSellVeh(w_front));
00821 return cost;
00822 }
00823
00824 total_cost.AddCost(cost);
00825
00826 if (flags & DC_EXEC) {
00827 w = Vehicle::Get(_new_vehicle_id);
00828
00829 if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
00830 SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
00831 }
00832
00833 if (v->type == VEH_TRAIN && !v->IsFrontEngine()) {
00834
00835
00836 CommandCost result = DoCommand(0, w->index | 1 << 20, w_rear->index, flags, CMD_MOVE_RAIL_VEHICLE);
00837 if (result.Failed()) {
00838
00839
00840 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00841 DoCommand(w_front->tile, w->index | 1 << 20, 0, flags, GetCmdSellVeh(w));
00842 return result;
00843 }
00844 } else {
00845
00846 w_front = w;
00847 w->service_interval = v->service_interval;
00848 w->SetServiceIntervalIsCustom(v->ServiceIntervalIsCustom());
00849 w->SetServiceIntervalIsPercent(v->ServiceIntervalIsPercent());
00850 }
00851 w_rear = w;
00852 }
00853 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00854
00855 if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
00856
00857 _new_vehicle_id = w_front->index;
00858 }
00859
00860 if (flags & DC_EXEC) {
00861
00862 DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
00863 }
00864
00865
00866
00867 w = w_front;
00868 v = v_front;
00869
00870
00871
00872
00873
00874
00875
00876 do {
00877 do {
00878 if (flags & DC_EXEC) {
00879 assert(w != NULL);
00880
00881
00882 byte subtype = GetBestFittingSubType(v, w, v->cargo_type);
00883 if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
00884 CommandCost cost = DoCommand(0, w->index, v->cargo_type | 1U << 7 | (subtype << 8), flags, GetCmdRefitVeh(v));
00885 if (cost.Succeeded()) total_cost.AddCost(cost);
00886 }
00887
00888 if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
00889 w = w->GetNextArticulatedPart();
00890 } else {
00891 break;
00892 }
00893 } else {
00894 const Engine *e = v->GetEngine();
00895 CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00896
00897 if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
00898 bool dummy;
00899 total_cost.AddCost(GetRefitCost(NULL, v->engine_type, v->cargo_type, v->cargo_subtype, &dummy));
00900 }
00901 }
00902
00903 if (v->IsGroundVehicle() && v->HasArticulatedPart()) {
00904 v = v->GetNextArticulatedPart();
00905 } else {
00906 break;
00907 }
00908 } while (v != NULL);
00909
00910 if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = w->GetNextVehicle();
00911 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
00912
00913 if (flags & DC_EXEC) {
00914
00915
00916
00917
00918
00919 DoCommand(0, w_front->index | (p2 & 1 ? CO_SHARE : CO_COPY) << 30, v_front->index, flags, CMD_CLONE_ORDER);
00920
00921
00922 if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
00923 }
00924
00925
00926
00927 if (!CheckCompanyHasMoney(total_cost)) {
00928 if (flags & DC_EXEC) {
00929
00930 DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
00931 }
00932 return total_cost;
00933 }
00934
00935 return total_cost;
00936 }
00937
00945 static CommandCost SendAllVehiclesToDepot(DoCommandFlag flags, bool service, const VehicleListIdentifier &vli)
00946 {
00947 VehicleList list;
00948
00949 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
00950
00951
00952 bool had_success = false;
00953 for (uint i = 0; i < list.Length(); i++) {
00954 const Vehicle *v = list[i];
00955 CommandCost ret = DoCommand(v->tile, v->index | (service ? DEPOT_SERVICE : 0U) | DEPOT_DONT_CANCEL, 0, flags, GetCmdSendToDepot(vli.vtype));
00956
00957 if (ret.Succeeded()) {
00958 had_success = true;
00959
00960
00961
00962
00963
00964 if (!(flags & DC_EXEC)) break;
00965 }
00966 }
00967
00968 return had_success ? CommandCost() : CMD_ERROR;
00969 }
00970
00982 CommandCost CmdSendVehicleToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00983 {
00984 if (p1 & DEPOT_MASS_SEND) {
00985
00986 VehicleListIdentifier vli;
00987 if (!vli.Unpack(p2)) return CMD_ERROR;
00988 return SendAllVehiclesToDepot(flags, (p1 & DEPOT_SERVICE) != 0, vli);
00989 }
00990
00991 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
00992 if (v == NULL) return CMD_ERROR;
00993 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
00994
00995 return v->SendToDepot(flags, (DepotCommand)(p1 & DEPOT_COMMAND_MASK));
00996 }
00997
01007 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01008 {
01009 Vehicle *v = Vehicle::GetIfValid(p1);
01010 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01011
01012 CommandCost ret = CheckOwnership(v->owner);
01013 if (ret.Failed()) return ret;
01014
01015 bool reset = StrEmpty(text);
01016
01017 if (!reset) {
01018 if (Utf8StringLength(text) >= MAX_LENGTH_VEHICLE_NAME_CHARS) return CMD_ERROR;
01019 if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
01020 }
01021
01022 if (flags & DC_EXEC) {
01023 free(v->name);
01024 v->name = reset ? NULL : strdup(text);
01025 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 1);
01026 MarkWholeScreenDirty();
01027 }
01028
01029 return CommandCost();
01030 }
01031
01032
01045 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01046 {
01047 Vehicle *v = Vehicle::GetIfValid(p1);
01048 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01049
01050 CommandCost ret = CheckOwnership(v->owner);
01051 if (ret.Failed()) return ret;
01052
01053 const Company *company = Company::Get(v->owner);
01054 bool iscustom = HasBit(p2, 16);
01055 bool ispercent = iscustom ? HasBit(p2, 17) : company->settings.vehicle.servint_ispercent;
01056
01057 uint16 serv_int;
01058 if (iscustom) {
01059 serv_int = GB(p2, 0, 16);
01060 if (serv_int != GetServiceIntervalClamped(serv_int, ispercent)) return CMD_ERROR;
01061 } else {
01062 serv_int = CompanyServiceInterval(company, v->type);
01063 }
01064
01065 if (flags & DC_EXEC) {
01066 v->SetServiceInterval(serv_int);
01067 v->SetServiceIntervalIsCustom(iscustom);
01068 v->SetServiceIntervalIsPercent(ispercent);
01069 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
01070 }
01071
01072 return CommandCost();
01073 }