00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "roadveh.h"
00014 #include "gfx_func.h"
00015 #include "news_func.h"
00016 #include "airport.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_engine.h"
00023 #include "newgrf_text.h"
00024 #include "functions.h"
00025 #include "window_func.h"
00026 #include "vehicle_func.h"
00027 #include "string_func.h"
00028 #include "depot_map.h"
00029 #include "vehiclelist.h"
00030 #include "infrastructure_func.h"
00031
00032 #include "table/strings.h"
00033
00034
00035 const uint32 _veh_build_proc_table[] = {
00036 CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
00037 CMD_BUILD_ROAD_VEH | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
00038 CMD_BUILD_SHIP | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
00039 CMD_BUILD_AIRCRAFT | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
00040 };
00041
00042 const uint32 _veh_sell_proc_table[] = {
00043 CMD_SELL_RAIL_WAGON | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
00044 CMD_SELL_ROAD_VEH | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
00045 CMD_SELL_SHIP | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
00046 CMD_SELL_AIRCRAFT | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
00047 };
00048
00049 const uint32 _veh_refit_proc_table[] = {
00050 CMD_REFIT_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
00051 CMD_REFIT_ROAD_VEH | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
00052 CMD_REFIT_SHIP | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
00053 CMD_REFIT_AIRCRAFT | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
00054 };
00055
00056 const uint32 _send_to_depot_proc_table[] = {
00057
00058 CMD_SEND_TRAIN_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT) | CMD_NO_TEST_IF_IN_NETWORK,
00059 CMD_SEND_ROADVEH_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
00060 CMD_SEND_SHIP_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
00061 CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
00062 };
00063
00072 CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00073 {
00074
00075 if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
00076
00077 Vehicle *v = Vehicle::GetIfValid(p1);
00078 if (v == NULL || !CheckVehicleControlAllowed(v) || !v->IsPrimaryVehicle()) return CMD_ERROR;
00079
00080 switch (v->type) {
00081 case VEH_TRAIN:
00082 if ((v->vehstatus & VS_STOPPED) && Train::From(v)->tcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_CATENARY);
00083 break;
00084
00085 case VEH_SHIP:
00086 case VEH_ROAD:
00087 break;
00088
00089 case VEH_AIRCRAFT: {
00090 Aircraft *a = Aircraft::From(v);
00091
00092 if (a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
00093 } break;
00094
00095 default: return CMD_ERROR;
00096 }
00097
00098
00099
00100 uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
00101 if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF && HasBit(p2, 0)) {
00102 StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
00103 return_cmd_error(error);
00104 }
00105
00106 if (flags & DC_EXEC) {
00107 if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
00108
00109 v->vehstatus ^= VS_STOPPED;
00110 if (v->type != VEH_TRAIN) v->cur_speed = 0;
00111 v->MarkDirty();
00112 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00113 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00114 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
00115 }
00116 return CommandCost();
00117 }
00118
00131 CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00132 {
00133 VehicleList list;
00134 CommandCost return_value = CMD_ERROR;
00135 VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
00136 bool start_stop = HasBit(p2, 5);
00137 bool vehicle_list_window = HasBit(p2, 6);
00138
00139 if (vehicle_list_window) {
00140 uint32 id = p1;
00141 uint16 window_type = p2 & VLW_MASK;
00142
00143 GenerateVehicleSortList(&list, vehicle_type, _current_company, id, window_type);
00144 } else {
00145
00146 BuildDepotVehicleList(vehicle_type, tile, &list, NULL);
00147 }
00148
00149 for (uint i = 0; i < list.Length(); i++) {
00150 const Vehicle *v = list[i];
00151
00152 if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
00153
00154 if (!vehicle_list_window) {
00155 if (vehicle_type == VEH_TRAIN) {
00156 if (!Train::From(v)->IsInDepot()) continue;
00157 } else {
00158 if (!(v->vehstatus & VS_HIDDEN)) continue;
00159 }
00160 }
00161
00162 CommandCost ret = DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
00163
00164 if (CmdSucceeded(ret)) {
00165 return_value = CommandCost();
00166
00167
00168 if (!(flags & DC_EXEC)) break;
00169 }
00170 }
00171
00172 return return_value;
00173 }
00174
00183 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00184 {
00185 VehicleList list;
00186
00187 CommandCost cost(EXPENSES_NEW_VEHICLES);
00188 VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
00189 uint sell_command = GetCmdSellVeh(vehicle_type);
00190
00191
00192 BuildDepotVehicleList(vehicle_type, tile, &list, &list);
00193
00194 for (uint i = 0; i < list.Length(); i++) {
00195 CommandCost ret = DoCommand(tile, list[i]->index, 1, flags, sell_command);
00196 if (CmdSucceeded(ret)) cost.AddCost(ret);
00197 }
00198
00199 if (cost.GetCost() == 0) return CMD_ERROR;
00200 return cost;
00201 }
00202
00214 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00215 {
00216 VehicleList list;
00217 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
00218 VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
00219 bool all_or_nothing = HasBit(p2, 0);
00220
00221 if (!IsDepotTile(tile) || !CheckInfraUsageAllowed(GetTileOwner(tile), vehicle_type)) return CMD_ERROR;
00222
00223
00224 BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
00225
00226 bool did_something = false;
00227
00228 for (uint i = 0; i < list.Length(); i++) {
00229 const Vehicle *v = list[i];
00230
00231
00232 if (!v->IsInDepot()) continue;
00233
00234 CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
00235
00236 if (CmdSucceeded(ret)) {
00237 did_something = true;
00238 cost.AddCost(ret);
00239 } else {
00240 if (ret.GetErrorMessage() != STR_ERROR_AUTOREPLACE_NOTHING_TO_DO && all_or_nothing) {
00241
00242
00243
00244 assert(!(flags & DC_EXEC));
00245
00246 return CMD_ERROR;
00247 }
00248 }
00249 }
00250
00251 if (!did_something) {
00252
00253
00254 cost = CMD_ERROR;
00255 }
00256
00257 return cost;
00258 }
00259
00264 static CommandCost GetRefitCost(EngineID engine_type)
00265 {
00266 ExpensesType expense_type;
00267 const Engine *e = Engine::Get(engine_type);
00268 Price base_price;
00269 uint cost_factor = e->info.refit_cost;
00270 switch (e->type) {
00271 case VEH_SHIP:
00272 base_price = PR_BUILD_VEHICLE_SHIP;
00273 expense_type = EXPENSES_SHIP_RUN;
00274 break;
00275
00276 case VEH_ROAD:
00277 base_price = PR_BUILD_VEHICLE_ROAD;
00278 expense_type = EXPENSES_ROADVEH_RUN;
00279 break;
00280
00281 case VEH_AIRCRAFT:
00282 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00283 expense_type = EXPENSES_AIRCRAFT_RUN;
00284 break;
00285
00286 case VEH_TRAIN:
00287 base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
00288 cost_factor <<= 1;
00289 expense_type = EXPENSES_TRAIN_RUN;
00290 break;
00291
00292 default: NOT_REACHED();
00293 }
00294 return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grffile, -10));
00295 }
00296
00307 CommandCost RefitVehicle(Vehicle *v, bool only_this, CargoID new_cid, byte new_subtype, DoCommandFlag flags)
00308 {
00309 CommandCost cost(v->GetExpenseType(false));
00310 uint total_capacity = 0;
00311 bool success = false;
00312
00313 v->InvalidateNewGRFCacheOfChain();
00314 for (; v != NULL; v = (only_this ? NULL : v->Next())) {
00315 const Engine *e = Engine::Get(v->engine_type);
00316 if (!e->CanCarryCargo() || !HasBit(e->info.refit_mask, new_cid)) continue;
00317 success = true;
00318
00319
00320 CargoID temp_cid = v->cargo_type;
00321 byte temp_subtype = v->cargo_subtype;
00322 v->cargo_type = new_cid;
00323 v->cargo_subtype = new_subtype;
00324
00325 uint16 mail_capacity;
00326 uint amount = GetVehicleCapacity(v, &mail_capacity);
00327 total_capacity += amount;
00328
00329
00330 v->cargo_type = temp_cid;
00331 v->cargo_subtype = temp_subtype;
00332
00333 if (new_cid != v->cargo_type) {
00334 cost.AddCost(GetRefitCost(v->engine_type));
00335 }
00336
00337 if (flags & DC_EXEC) {
00338 v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
00339 v->cargo_type = new_cid;
00340 v->cargo_cap = amount;
00341 v->cargo_subtype = new_subtype;
00342 if (v->type == VEH_AIRCRAFT) {
00343 Vehicle *u = v->Next();
00344 u->cargo_cap = mail_capacity;
00345 u->cargo.Truncate(mail_capacity);
00346 }
00347 }
00348 }
00349
00350 _returned_refit_capacity = total_capacity;
00351 return success ? cost : CMD_ERROR;
00352 }
00353
00358 static bool IsUniqueVehicleName(const char *name)
00359 {
00360 const Vehicle *v;
00361
00362 FOR_ALL_VEHICLES(v) {
00363 if (v->name != NULL && strcmp(v->name, name) == 0) return false;
00364 }
00365
00366 return true;
00367 }
00368
00373 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
00374 {
00375 char buf[256];
00376
00377
00378 size_t number_position;
00379 for (number_position = strlen(src->name); number_position > 0; number_position--) {
00380
00381
00382 if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
00383 }
00384
00385
00386 int num;
00387 if (number_position == strlen(src->name)) {
00388
00389 strecpy(buf, src->name, lastof(buf));
00390 strecat(buf, " ", lastof(buf));
00391 number_position = strlen(buf);
00392 num = 2;
00393 } else {
00394
00395 strecpy(buf, src->name, lastof(buf));
00396 buf[number_position] = '\0';
00397 num = strtol(&src->name[number_position], NULL, 10) + 1;
00398 }
00399
00400
00401 for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
00402
00403 seprintf(&buf[number_position], lastof(buf), "%d", num);
00404
00405
00406 if (IsUniqueVehicleName(buf)) {
00407 dst->name = strdup(buf);
00408 break;
00409 }
00410 }
00411
00412
00413 }
00414
00423 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00424 {
00425 CommandCost total_cost(EXPENSES_NEW_VEHICLES);
00426 uint32 build_argument = 2;
00427
00428 Vehicle *v = Vehicle::GetIfValid(p1);
00429 if (v == NULL) return CMD_ERROR;
00430 Vehicle *v_front = v;
00431 Vehicle *w = NULL;
00432 Vehicle *w_front = NULL;
00433 Vehicle *w_rear = NULL;
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 if (!CheckOwnership(v->owner)) return CMD_ERROR;
00444
00445 if (v->type == VEH_TRAIN && (!Train::From(v)->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
00446
00447
00448 if (!(flags & DC_EXEC)) {
00449 int veh_counter = 0;
00450 do {
00451 veh_counter++;
00452 } while ((v = v->Next()) != NULL);
00453
00454 if (!Vehicle::CanAllocateItem(veh_counter)) {
00455 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00456 }
00457 }
00458
00459 v = v_front;
00460
00461 do {
00462 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00463
00464 continue;
00465 }
00466
00467
00468
00469
00470
00471
00472
00473 DoCommandFlag build_flags = flags;
00474 if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
00475
00476 CommandCost cost = DoCommand(tile, v->engine_type, build_argument, build_flags, GetCmdBuildVeh(v));
00477 build_argument = 3;
00478
00479 if (CmdFailed(cost)) {
00480
00481 if (w_front != NULL) DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
00482 return cost;
00483 }
00484
00485 total_cost.AddCost(cost);
00486
00487 if (flags & DC_EXEC) {
00488 w = Vehicle::Get(_new_vehicle_id);
00489
00490 if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
00491 SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
00492 }
00493
00494 if (v->type == VEH_TRAIN && !Train::From(v)->IsFrontEngine()) {
00495
00496
00497 CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
00498 if (CmdFailed(result)) {
00499
00500
00501 DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
00502 DoCommand(w_front->tile, w->index, 1, flags, GetCmdSellVeh(w));
00503 return result;
00504 }
00505 } else {
00506
00507 w_front = w;
00508 w->service_interval = v->service_interval;
00509 }
00510 w_rear = w;
00511 }
00512 } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
00513
00514 if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
00515
00516 _new_vehicle_id = w_front->index;
00517 }
00518
00519 if (flags & DC_EXEC) {
00520
00521 DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
00522 }
00523
00524
00525
00526 w = w_front;
00527 v = v_front;
00528
00529
00530
00531
00532
00533
00534
00535 do {
00536 do {
00537 if (flags & DC_EXEC) {
00538 assert(w != NULL);
00539
00540
00541 byte subtype = GetBestFittingSubType(v, w);
00542 if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
00543 CommandCost cost = DoCommand(0, w->index, v->cargo_type | (subtype << 8) | 1U << 16, flags, GetCmdRefitVeh(v));
00544 if (CmdSucceeded(cost)) total_cost.AddCost(cost);
00545 }
00546
00547 if (w->type == VEH_TRAIN && Train::From(w)->HasArticulatedPart()) {
00548 w = Train::From(w)->GetNextArticPart();
00549 } else if (w->type == VEH_ROAD && RoadVehicle::From(w)->HasArticulatedPart()) {
00550 w = w->Next();
00551 } else {
00552 break;
00553 }
00554 } else {
00555 const Engine *e = Engine::Get(v->engine_type);
00556 CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00557
00558 if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
00559 total_cost.AddCost(GetRefitCost(v->engine_type));
00560 }
00561 }
00562
00563 if (v->type == VEH_TRAIN && Train::From(v)->HasArticulatedPart()) {
00564 v = Train::From(v)->GetNextArticPart();
00565 } else if (v->type == VEH_ROAD && RoadVehicle::From(v)->HasArticulatedPart()) {
00566 v = v->Next();
00567 } else {
00568 break;
00569 }
00570 } while (v != NULL);
00571
00572 if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = Train::From(w)->GetNextVehicle();
00573 } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
00574
00575 if (flags & DC_EXEC) {
00576
00577
00578
00579
00580
00581 DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
00582
00583
00584 if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
00585 }
00586
00587
00588
00589 if (!CheckCompanyHasMoney(total_cost)) {
00590 if (flags & DC_EXEC) {
00591
00592 DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
00593 }
00594 return CMD_ERROR;
00595 }
00596
00597 return total_cost;
00598 }
00599
00610 CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
00611 {
00612 VehicleList list;
00613
00614 GenerateVehicleSortList(&list, type, owner, id, vlw_flag);
00615
00616
00617 for (uint i = 0; i < list.Length(); i++) {
00618 const Vehicle *v = list[i];
00619 CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
00620
00621
00622
00623
00624
00625 if (CmdSucceeded(ret) && !(flags & DC_EXEC)) {
00626 return CommandCost();
00627 }
00628 }
00629
00630 return (flags & DC_EXEC) ? CommandCost() : CMD_ERROR;
00631 }
00632
00641 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00642 {
00643 Vehicle *v = Vehicle::GetIfValid(p1);
00644 if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
00645
00646 bool reset = StrEmpty(text);
00647
00648 if (!reset) {
00649 if (strlen(text) >= MAX_LENGTH_VEHICLE_NAME_BYTES) return CMD_ERROR;
00650 if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00651 }
00652
00653 if (flags & DC_EXEC) {
00654 free(v->name);
00655 v->name = reset ? NULL : strdup(text);
00656 InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
00657 MarkWholeScreenDirty();
00658 }
00659
00660 return CommandCost();
00661 }
00662
00663
00672 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00673 {
00674 Vehicle *v = Vehicle::GetIfValid(p1);
00675 if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
00676
00677 uint16 serv_int = GetServiceIntervalClamped(p2, v->owner);
00678 if (serv_int != p2) return CMD_ERROR;
00679
00680 if (flags & DC_EXEC) {
00681 v->service_interval = serv_int;
00682 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
00683 }
00684
00685 return CommandCost();
00686 }