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