OpenTTD Source 20250205-master-gfd85ab1e2c
vehicle_cmd.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "roadveh.h"
12#include "news_func.h"
13#include "airport.h"
14#include "command_func.h"
15#include "company_func.h"
16#include "train.h"
17#include "aircraft.h"
18#include "newgrf_text.h"
19#include "vehicle_func.h"
20#include "string_func.h"
21#include "depot_map.h"
22#include "vehiclelist.h"
23#include "engine_func.h"
25#include "autoreplace_gui.h"
26#include "group.h"
27#include "order_backup.h"
28#include "ship.h"
29#include "newgrf.h"
30#include "company_base.h"
31#include "core/random_func.hpp"
32#include "vehicle_cmd.h"
33#include "aircraft_cmd.h"
34#include "autoreplace_cmd.h"
35#include "group_cmd.h"
36#include "order_cmd.h"
37#include "roadveh_cmd.h"
38#include "train_cmd.h"
39#include "ship_cmd.h"
40#include <sstream>
41#include <iomanip>
42
43#include "table/strings.h"
44
45#include "safeguards.h"
46
47/* Tables used in vehicle_func.h to find the right error message for a certain vehicle type */
48const StringID _veh_build_msg_table[] = {
49 STR_ERROR_CAN_T_BUY_TRAIN,
50 STR_ERROR_CAN_T_BUY_ROAD_VEHICLE,
51 STR_ERROR_CAN_T_BUY_SHIP,
52 STR_ERROR_CAN_T_BUY_AIRCRAFT,
53};
54
55const StringID _veh_sell_msg_table[] = {
56 STR_ERROR_CAN_T_SELL_TRAIN,
57 STR_ERROR_CAN_T_SELL_ROAD_VEHICLE,
58 STR_ERROR_CAN_T_SELL_SHIP,
59 STR_ERROR_CAN_T_SELL_AIRCRAFT,
60};
61
62const StringID _veh_sell_all_msg_table[] = {
63 STR_ERROR_CAN_T_SELL_ALL_TRAIN,
64 STR_ERROR_CAN_T_SELL_ALL_ROAD_VEHICLE,
65 STR_ERROR_CAN_T_SELL_ALL_SHIP,
66 STR_ERROR_CAN_T_SELL_ALL_AIRCRAFT,
67};
68
69const StringID _veh_autoreplace_msg_table[] = {
70 STR_ERROR_CAN_T_AUTOREPLACE_TRAIN,
71 STR_ERROR_CAN_T_AUTOREPLACE_ROAD_VEHICLE,
72 STR_ERROR_CAN_T_AUTOREPLACE_SHIP,
73 STR_ERROR_CAN_T_AUTOREPLACE_AIRCRAFT,
74};
75
76const StringID _veh_refit_msg_table[] = {
77 STR_ERROR_CAN_T_REFIT_TRAIN,
78 STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE,
79 STR_ERROR_CAN_T_REFIT_SHIP,
80 STR_ERROR_CAN_T_REFIT_AIRCRAFT,
81};
82
83const StringID _send_to_depot_msg_table[] = {
84 STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT,
85 STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT,
86 STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT,
87 STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR,
88};
89
90
101std::tuple<CommandCost, VehicleID, uint, uint16_t, CargoArray> CmdBuildVehicle(DoCommandFlag flags, TileIndex tile, EngineID eid, bool use_free_vehicles, CargoType cargo, ClientID client_id)
102{
103 /* Elementary check for valid location. */
104 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return { CMD_ERROR, INVALID_VEHICLE, 0, 0, {} };
105
106 VehicleType type = GetDepotVehicleType(tile);
107
108 /* Validate the engine type. */
109 if (!IsEngineBuildable(eid, type, _current_company)) return { CommandCost(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type), INVALID_VEHICLE, 0, 0, {} };
110
111 /* Validate the cargo type. */
112 if (cargo >= NUM_CARGO && IsValidCargoType(cargo)) return { CMD_ERROR, INVALID_VEHICLE, 0, 0, {} };
113
114 const Engine *e = Engine::Get(eid);
116
117 /* Engines without valid cargo should not be available */
118 CargoType default_cargo = e->GetDefaultCargoType();
119 if (!IsValidCargoType(default_cargo)) return { CMD_ERROR, INVALID_VEHICLE, 0, 0, {} };
120
121 bool refitting = IsValidCargoType(cargo) && cargo != default_cargo;
122
123 /* Check whether the number of vehicles we need to build can be built according to pool space. */
124 uint num_vehicles;
125 switch (type) {
126 case VEH_TRAIN: num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); break;
127 case VEH_ROAD: num_vehicles = 1 + CountArticulatedParts(eid, false); break;
128 case VEH_SHIP: num_vehicles = 1; break;
129 case VEH_AIRCRAFT: num_vehicles = e->u.air.subtype & AIR_CTOL ? 2 : 3; break;
130 default: NOT_REACHED(); // Safe due to IsDepotTile()
131 }
132 if (!Vehicle::CanAllocateItem(num_vehicles)) return { CommandCost(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME), INVALID_VEHICLE, 0, 0, {} };
133
134 /* Check whether we can allocate a unit number. Autoreplace does not allocate
135 * an unit number as it will (always) reuse the one of the replaced vehicle
136 * and (train) wagons don't have an unit number in any scenario. */
137 UnitID unit_num = (flags & DC_QUERY_COST || flags & DC_AUTOREPLACE || (type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON)) ? 0 : GetFreeUnitNumber(type);
138 if (unit_num == UINT16_MAX) return { CommandCost(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME), INVALID_VEHICLE, 0, 0, {} };
139
140 /* If we are refitting we need to temporarily purchase the vehicle to be able to
141 * test it. */
142 DoCommandFlag subflags = flags;
143 if (refitting && !(flags & DC_EXEC)) subflags |= DC_EXEC | DC_AUTOREPLACE;
144
145 /* Vehicle construction needs random bits, so we have to save the random
146 * seeds to prevent desyncs. */
147 SavedRandomSeeds saved_seeds;
148 SaveRandomSeeds(&saved_seeds);
149
150 Vehicle *v = nullptr;
151 switch (type) {
152 case VEH_TRAIN: value.AddCost(CmdBuildRailVehicle(subflags, tile, e, &v)); break;
153 case VEH_ROAD: value.AddCost(CmdBuildRoadVehicle(subflags, tile, e, &v)); break;
154 case VEH_SHIP: value.AddCost(CmdBuildShip (subflags, tile, e, &v)); break;
155 case VEH_AIRCRAFT: value.AddCost(CmdBuildAircraft (subflags, tile, e, &v)); break;
156 default: NOT_REACHED(); // Safe due to IsDepotTile()
157 }
158
159 VehicleID veh_id = INVALID_VEHICLE;
160 uint refitted_capacity = 0;
161 uint16_t refitted_mail_capacity = 0;
162 CargoArray cargo_capacities{};
163 if (value.Succeeded()) {
164 if (subflags & DC_EXEC) {
165 v->unitnumber = unit_num;
166 v->value = value.GetCost();
167 veh_id = v->index;
168 }
169
170 if (refitting) {
171 /* Refit only one vehicle. If we purchased an engine, it may have gained free wagons. */
172 CommandCost cc;
173 std::tie(cc, refitted_capacity, refitted_mail_capacity, cargo_capacities) = CmdRefitVehicle(flags, v->index, cargo, 0, false, false, 1);
174 value.AddCost(cc);
175 } else {
176 /* Fill in non-refitted capacities */
177 if (e->type == VEH_TRAIN || e->type == VEH_ROAD) {
178 cargo_capacities = GetCapacityOfArticulatedParts(eid);
179 refitted_capacity = cargo_capacities[default_cargo];
180 refitted_mail_capacity = 0;
181 } else {
182 refitted_capacity = e->GetDisplayDefaultCapacity(&refitted_mail_capacity);
183 cargo_capacities[default_cargo] = refitted_capacity;
184 CargoType mail = GetCargoTypeByLabel(CT_MAIL);
185 if (IsValidCargoType(mail)) cargo_capacities[mail] = refitted_mail_capacity;
186 }
187 }
188
189 if (flags & DC_EXEC) {
190 if (type == VEH_TRAIN && use_free_vehicles && !(flags & DC_AUTOREPLACE) && Train::From(v)->IsEngine()) {
191 /* Move any free wagons to the new vehicle. */
193 }
194
198 if (IsLocalCompany()) {
199 InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the auto replace window (must be called before incrementing num_engines)
200 }
201 }
202
203 if (subflags & DC_EXEC) {
206
207 if (v->IsPrimaryVehicle()) {
209 if (!(subflags & DC_AUTOREPLACE)) OrderBackup::Restore(v, client_id);
210 }
211
212 Company::Get(v->owner)->freeunits[v->type].UseID(v->unitnumber);
213 }
214
215
216 /* If we are not in DC_EXEC undo everything */
217 if (flags != subflags) {
219 }
220 }
221
222 /* Only restore if we actually did some refitting */
223 if (flags != subflags) RestoreRandomSeeds(saved_seeds);
224
225 return { value, veh_id, refitted_capacity, refitted_mail_capacity, cargo_capacities };
226}
227
237CommandCost CmdSellVehicle(DoCommandFlag flags, VehicleID v_id, bool sell_chain, bool backup_order, ClientID client_id)
238{
239 Vehicle *v = Vehicle::GetIfValid(v_id);
240 if (v == nullptr) return CMD_ERROR;
241
242 Vehicle *front = v->First();
243
244 CommandCost ret = CheckOwnership(front->owner);
245 if (ret.Failed()) return ret;
246
247 if (front->vehstatus & VS_CRASHED) return CommandCost(STR_ERROR_VEHICLE_IS_DESTROYED);
248
249 if (!front->IsStoppedInDepot()) return CommandCost(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
250
251 /* Can we actually make the order backup, i.e. are there enough orders? */
252 if (backup_order &&
253 front->orders != nullptr &&
254 !front->orders->IsShared() &&
256 /* Only happens in exceptional cases when there aren't enough orders anyhow.
257 * Thus it should be safe to just drop the orders in that case. */
258 backup_order = false;
259 }
260
261 if (v->type == VEH_TRAIN) {
262 ret = CmdSellRailWagon(flags, v, sell_chain, backup_order, client_id);
263 } else {
265
266 if (flags & DC_EXEC) {
267 if (front->IsPrimaryVehicle() && backup_order) OrderBackup::Backup(front, client_id);
268 delete front;
269 }
270 }
271
272 return ret;
273}
274
284static int GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoType new_cargo_type, uint8_t new_subtype, bool *auto_refit_allowed)
285{
286 /* Prepare callback param with info about the new cargo type. */
287 const Engine *e = Engine::Get(engine_type);
288
289 /* Is this vehicle a NewGRF vehicle? */
290 if (e->GetGRF() != nullptr) {
291 const CargoSpec *cs = CargoSpec::Get(new_cargo_type);
292 uint32_t param1 = (cs->classes << 16) | (new_subtype << 8) | e->GetGRF()->cargo_map[new_cargo_type];
293
294 uint16_t cb_res = GetVehicleCallback(CBID_VEHICLE_REFIT_COST, param1, 0, engine_type, v);
295 if (cb_res != CALLBACK_FAILED) {
296 *auto_refit_allowed = HasBit(cb_res, 14);
297 int factor = GB(cb_res, 0, 14);
298 if (factor >= 0x2000) factor -= 0x4000; // Treat as signed integer.
299 return factor;
300 }
301 }
302
303 *auto_refit_allowed = e->info.refit_cost == 0;
304 return (v == nullptr || v->cargo_type != new_cargo_type) ? e->info.refit_cost : 0;
305}
306
316static CommandCost GetRefitCost(const Vehicle *v, EngineID engine_type, CargoType new_cargo_type, uint8_t new_subtype, bool *auto_refit_allowed)
317{
318 ExpensesType expense_type;
319 const Engine *e = Engine::Get(engine_type);
320 Price base_price;
321 int cost_factor = GetRefitCostFactor(v, engine_type, new_cargo_type, new_subtype, auto_refit_allowed);
322 switch (e->type) {
323 case VEH_SHIP:
324 base_price = PR_BUILD_VEHICLE_SHIP;
325 expense_type = EXPENSES_SHIP_RUN;
326 break;
327
328 case VEH_ROAD:
329 base_price = PR_BUILD_VEHICLE_ROAD;
330 expense_type = EXPENSES_ROADVEH_RUN;
331 break;
332
333 case VEH_AIRCRAFT:
334 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
335 expense_type = EXPENSES_AIRCRAFT_RUN;
336 break;
337
338 case VEH_TRAIN:
339 base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
340 cost_factor <<= 1;
341 expense_type = EXPENSES_TRAIN_RUN;
342 break;
343
344 default: NOT_REACHED();
345 }
346 if (cost_factor < 0) {
347 return CommandCost(expense_type, -GetPrice(base_price, -cost_factor, e->GetGRF(), -10));
348 } else {
349 return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->GetGRF(), -10));
350 }
351}
352
360
373static std::tuple<CommandCost, uint, uint16_t, CargoArray> RefitVehicle(Vehicle *v, bool only_this, uint8_t num_vehicles, CargoType new_cargo_type, uint8_t new_subtype, DoCommandFlag flags, bool auto_refit)
374{
375 CommandCost cost(v->GetExpenseType(false));
376 uint total_capacity = 0;
377 uint total_mail_capacity = 0;
378 num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
379 CargoArray cargo_capacities{};
380
381 VehicleSet vehicles_to_refit;
382 if (!only_this) {
383 GetVehicleSet(vehicles_to_refit, v, num_vehicles);
384 /* In this case, we need to check the whole chain. */
385 v = v->First();
386 }
387
388 std::vector<RefitResult> refit_result;
389
391 uint8_t actual_subtype = new_subtype;
392 for (; v != nullptr; v = (only_this ? nullptr : v->Next())) {
393 /* Reset actual_subtype for every new vehicle */
394 if (!v->IsArticulatedPart()) actual_subtype = new_subtype;
395
396 if (v->type == VEH_TRAIN && std::ranges::find(vehicles_to_refit, v->index) == vehicles_to_refit.end() && !only_this) continue;
397
398 const Engine *e = v->GetEngine();
399 if (!e->CanCarryCargo()) continue;
400
401 /* If the vehicle is not refittable, or does not allow automatic refitting,
402 * count its capacity nevertheless if the cargo matches */
403 bool refittable = HasBit(e->info.refit_mask, new_cargo_type) && (!auto_refit || e->info.misc_flags.Test(EngineMiscFlag::AutoRefit));
404 if (!refittable && v->cargo_type != new_cargo_type) {
405 uint amount = e->DetermineCapacity(v, nullptr);
406 if (amount > 0) cargo_capacities[v->cargo_type] += amount;
407 continue;
408 }
409
410 /* Determine best fitting subtype if requested */
411 if (actual_subtype == 0xFF) {
412 actual_subtype = GetBestFittingSubType(v, v, new_cargo_type);
413 }
414
415 /* Back up the vehicle's cargo type */
416 CargoType temp_cargo_type = v->cargo_type;
417 uint8_t temp_subtype = v->cargo_subtype;
418 if (refittable) {
419 v->cargo_type = new_cargo_type;
420 v->cargo_subtype = actual_subtype;
421 }
422
423 uint16_t mail_capacity = 0;
424 uint amount = e->DetermineCapacity(v, &mail_capacity);
425 total_capacity += amount;
426 /* mail_capacity will always be zero if the vehicle is not an aircraft. */
427 total_mail_capacity += mail_capacity;
428
429 cargo_capacities[new_cargo_type] += amount;
430 CargoType mail = GetCargoTypeByLabel(CT_MAIL);
431 if (IsValidCargoType(mail)) cargo_capacities[mail] += mail_capacity;
432
433 if (!refittable) continue;
434
435 /* Restore the original cargo type */
436 v->cargo_type = temp_cargo_type;
437 v->cargo_subtype = temp_subtype;
438
439 bool auto_refit_allowed;
440 CommandCost refit_cost = GetRefitCost(v, v->engine_type, new_cargo_type, actual_subtype, &auto_refit_allowed);
441 if (auto_refit && (flags & DC_QUERY_COST) == 0 && !auto_refit_allowed) {
442 /* Sorry, auto-refitting not allowed, subtract the cargo amount again from the total.
443 * When querrying cost/capacity (for example in order refit GUI), we always assume 'allowed'.
444 * It is not predictable. */
445 total_capacity -= amount;
446 total_mail_capacity -= mail_capacity;
447
448 if (v->cargo_type == new_cargo_type) {
449 /* Add the old capacity nevertheless, if the cargo matches */
450 total_capacity += v->cargo_cap;
451 if (v->type == VEH_AIRCRAFT) total_mail_capacity += v->Next()->cargo_cap;
452 }
453 continue;
454 }
455 cost.AddCost(refit_cost);
456
457 /* Record the refitting.
458 * Do not execute the refitting immediately, so DetermineCapacity and GetRefitCost do the same in test and exec run.
459 * (weird NewGRFs)
460 * Note:
461 * - If the capacity of vehicles depends on other vehicles in the chain, the actual capacity is
462 * set after RefitVehicle() via ConsistChanged() and friends. The estimation via _returned_refit_capacity will be wrong.
463 * - We have to call the refit cost callback with the pre-refit configuration of the chain because we want refit and
464 * autorefit to behave the same, and we need its result for auto_refit_allowed.
465 */
466 refit_result.push_back({v, amount, mail_capacity, actual_subtype});
467 }
468
469 if (flags & DC_EXEC) {
470 /* Store the result */
471 for (RefitResult &result : refit_result) {
472 Vehicle *u = result.v;
473 u->refit_cap = (u->cargo_type == new_cargo_type) ? std::min<uint16_t>(result.capacity, u->refit_cap) : 0;
474 if (u->cargo.TotalCount() > u->refit_cap) u->cargo.Truncate(u->cargo.TotalCount() - u->refit_cap);
475 u->cargo_type = new_cargo_type;
476 u->cargo_cap = result.capacity;
477 u->cargo_subtype = result.subtype;
478 if (u->type == VEH_AIRCRAFT) {
479 Vehicle *w = u->Next();
480 assert(w != nullptr);
481 w->refit_cap = std::min<uint16_t>(w->refit_cap, result.mail_capacity);
482 w->cargo_cap = result.mail_capacity;
483 if (w->cargo.TotalCount() > w->refit_cap) w->cargo.Truncate(w->cargo.TotalCount() - w->refit_cap);
484 }
485 }
486 }
487
488 refit_result.clear();
489 return { cost, total_capacity, total_mail_capacity, cargo_capacities };
490}
491
504std::tuple<CommandCost, uint, uint16_t, CargoArray> CmdRefitVehicle(DoCommandFlag flags, VehicleID veh_id, CargoType new_cargo_type, uint8_t new_subtype, bool auto_refit, bool only_this, uint8_t num_vehicles)
505{
506 Vehicle *v = Vehicle::GetIfValid(veh_id);
507 if (v == nullptr) return { CMD_ERROR, 0, 0, {} };
508
509 /* Don't allow disasters and sparks and such to be refitted.
510 * We cannot check for IsPrimaryVehicle as autoreplace also refits in free wagon chains. */
511 if (!IsCompanyBuildableVehicleType(v->type)) return { CMD_ERROR, 0, 0, {} };
512
513 Vehicle *front = v->First();
514
515 CommandCost ret = CheckOwnership(front->owner);
516 if (ret.Failed()) return { ret, 0, 0, {} };
517
518 bool free_wagon = v->type == VEH_TRAIN && Train::From(front)->IsFreeWagon(); // used by autoreplace/renew
519
520 /* Don't allow shadows and such to be refitted. */
521 if (v != front && (v->type == VEH_SHIP || v->type == VEH_AIRCRAFT)) return { CMD_ERROR, 0, 0, {} };
522
523 /* Allow auto-refitting only during loading and normal refitting only in a depot. */
524 if ((flags & DC_QUERY_COST) == 0 && // used by the refit GUI, including the order refit GUI.
525 !free_wagon && // used by autoreplace/renew
526 (!auto_refit || !front->current_order.IsType(OT_LOADING)) && // refit inside stations
527 !front->IsStoppedInDepot()) { // refit inside depots
528 return { CommandCost(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type), 0, 0, {} };
529 }
530
531 if (front->vehstatus & VS_CRASHED) return { CommandCost(STR_ERROR_VEHICLE_IS_DESTROYED), 0, 0, {} };
532
533 /* Check cargo */
534 if (new_cargo_type >= NUM_CARGO) return { CMD_ERROR, 0, 0, {} };
535
536 /* For ships and aircraft there is always only one. */
537 only_this |= front->type == VEH_SHIP || front->type == VEH_AIRCRAFT;
538
539 auto [cost, refit_capacity, mail_capacity, cargo_capacities] = RefitVehicle(v, only_this, num_vehicles, new_cargo_type, new_subtype, flags, auto_refit);
540
541 if (flags & DC_EXEC) {
542 /* Update the cached variables */
543 switch (v->type) {
544 case VEH_TRAIN:
545 Train::From(front)->ConsistChanged(auto_refit ? CCF_AUTOREFIT : CCF_REFIT);
546 break;
547 case VEH_ROAD:
548 RoadVehUpdateCache(RoadVehicle::From(front), auto_refit);
549 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) RoadVehicle::From(front)->CargoChanged();
550 break;
551
552 case VEH_SHIP:
555 break;
556
557 case VEH_AIRCRAFT:
560 break;
561
562 default: NOT_REACHED();
563 }
564 front->MarkDirty();
565
566 if (!free_wagon) {
569 }
571 } else {
572 /* Always invalidate the cache; querycost might have filled it. */
574 }
575
576 return { cost, refit_capacity, mail_capacity, cargo_capacities };
577}
578
586CommandCost CmdStartStopVehicle(DoCommandFlag flags, VehicleID veh_id, bool evaluate_startstop_cb)
587{
588 /* Disable the effect of p2 bit 0, when DC_AUTOREPLACE is not set */
589 if ((flags & DC_AUTOREPLACE) == 0) evaluate_startstop_cb = true;
590
591 Vehicle *v = Vehicle::GetIfValid(veh_id);
592 if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
593
595 if (ret.Failed()) return ret;
596
597 if (v->vehstatus & VS_CRASHED) return CommandCost(STR_ERROR_VEHICLE_IS_DESTROYED);
598
599 switch (v->type) {
600 case VEH_TRAIN:
601 if ((v->vehstatus & VS_STOPPED) && Train::From(v)->gcache.cached_power == 0) return CommandCost(STR_ERROR_TRAIN_START_NO_POWER);
602 break;
603
604 case VEH_SHIP:
605 case VEH_ROAD:
606 break;
607
608 case VEH_AIRCRAFT: {
609 Aircraft *a = Aircraft::From(v);
610 /* cannot stop airplane when in flight, or when taking off / landing */
611 if (a->state >= STARTTAKEOFF && a->state < TERM7) return CommandCost(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
612 if (HasBit(a->flags, VAF_HELI_DIRECT_DESCENT)) return CommandCost(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
613 break;
614 }
615
616 default: return CMD_ERROR;
617 }
618
619 if (evaluate_startstop_cb) {
620 /* Check if this vehicle can be started/stopped. Failure means 'allow'. */
621 uint16_t callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
622 StringID error = STR_NULL;
623 if (callback != CALLBACK_FAILED) {
624 if (v->GetGRF()->grf_version < 8) {
625 /* 8 bit result 0xFF means 'allow' */
626 if (callback < 0x400 && GB(callback, 0, 8) != 0xFF) error = GetGRFStringID(v->GetGRFID(), GRFSTR_MISC_GRF_TEXT + callback);
627 } else {
628 if (callback < 0x400) {
629 error = GetGRFStringID(v->GetGRFID(), GRFSTR_MISC_GRF_TEXT + callback);
630 } else {
631 switch (callback) {
632 case 0x400: // allow
633 break;
634
635 default: // unknown reason -> disallow
636 error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
637 break;
638 }
639 }
640 }
641 }
642 if (error != STR_NULL) return CommandCost(error);
643 }
644
645 if (flags & DC_EXEC) {
647
648 v->vehstatus ^= VS_STOPPED;
649 if (v->type != VEH_TRAIN) v->cur_speed = 0; // trains can stop 'slowly'
650
651 /* Unbunching data is no longer valid. */
653
654 v->MarkDirty();
659 }
660 return CommandCost();
661}
662
672CommandCost CmdMassStartStopVehicle(DoCommandFlag flags, TileIndex tile, bool do_start, bool vehicle_list_window, const VehicleListIdentifier &vli)
673{
674 VehicleList list;
675
676 if (!vli.Valid()) return CMD_ERROR;
678
679 if (vehicle_list_window) {
680 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
681 } else {
682 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
683 /* Get the list of vehicles in the depot */
684 BuildDepotVehicleList(vli.vtype, tile, &list, nullptr);
685 }
686
687 for (const Vehicle *v : list) {
688 if (!!(v->vehstatus & VS_STOPPED) != do_start) continue;
689
690 if (!vehicle_list_window && !v->IsChainInDepot()) continue;
691
692 /* Just try and don't care if some vehicle's can't be stopped. */
693 Command<CMD_START_STOP_VEHICLE>::Do(flags, v->index, false);
694 }
695
696 return CommandCost();
697}
698
707{
708 VehicleList list;
709
711
712 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
713 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
714
715 /* Get the list of vehicles in the depot */
716 BuildDepotVehicleList(vehicle_type, tile, &list, &list);
717
718 CommandCost last_error = CMD_ERROR;
719 bool had_success = false;
720 for (const Vehicle *v : list) {
721 CommandCost ret = Command<CMD_SELL_VEHICLE>::Do(flags, v->index, true, false, INVALID_CLIENT_ID);
722 if (ret.Succeeded()) {
723 cost.AddCost(ret);
724 had_success = true;
725 } else {
726 last_error = ret;
727 }
728 }
729
730 return had_success ? cost : last_error;
731}
732
741{
742 VehicleList list;
744
745 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
746 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
747
748 /* Get the list of vehicles in the depot */
749 BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
750
751 for (const Vehicle *v : list) {
752 /* Ensure that the vehicle completely in the depot */
753 if (!v->IsChainInDepot()) continue;
754
756
757 if (ret.Succeeded()) cost.AddCost(ret);
758 }
759 return cost;
760}
761
767bool IsUniqueVehicleName(const std::string &name)
768{
769 for (const Vehicle *v : Vehicle::Iterate()) {
770 if (!v->name.empty() && v->name == name) return false;
771 }
772
773 return true;
774}
775
781static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
782{
783 std::string buf;
784
785 /* Find the position of the first digit in the last group of digits. */
786 size_t number_position;
787 for (number_position = src->name.length(); number_position > 0; number_position--) {
788 /* The design of UTF-8 lets this work simply without having to check
789 * for UTF-8 sequences. */
790 if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
791 }
792
793 /* Format buffer and determine starting number. */
794 long num;
795 uint8_t padding = 0;
796 if (number_position == src->name.length()) {
797 /* No digit at the end, so start at number 2. */
798 buf = src->name;
799 buf += " ";
800 number_position = buf.length();
801 num = 2;
802 } else {
803 /* Found digits, parse them and start at the next number. */
804 buf = src->name.substr(0, number_position);
805
806 auto num_str = src->name.substr(number_position);
807 padding = (uint8_t)num_str.length();
808
809 std::istringstream iss(num_str);
810 iss >> num;
811 num++;
812 }
813
814 /* Check if this name is already taken. */
815 for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
816 std::ostringstream oss;
817
818 /* Attach the number to the temporary name. */
819 oss << buf << std::setw(padding) << std::setfill('0') << std::internal << num;
820
821 /* Check the name is unique. */
822 auto new_name = oss.str();
823 if (IsUniqueVehicleName(new_name)) {
824 dst->name = new_name;
825 break;
826 }
827 }
828
829 /* All done. If we didn't find a name, it'll just use its default. */
830}
831
840std::tuple<CommandCost, VehicleID> CmdCloneVehicle(DoCommandFlag flags, TileIndex tile, VehicleID veh_id, bool share_orders)
841{
843
844 Vehicle *v = Vehicle::GetIfValid(veh_id);
845 if (v == nullptr || !v->IsPrimaryVehicle()) return { CMD_ERROR, INVALID_VEHICLE };
846 Vehicle *v_front = v;
847 Vehicle *w = nullptr;
848 Vehicle *w_front = nullptr;
849 Vehicle *w_rear = nullptr;
850
851 /*
852 * v_front is the front engine in the original vehicle
853 * v is the car/vehicle of the original vehicle that is currently being copied
854 * w_front is the front engine of the cloned vehicle
855 * w is the car/vehicle currently being cloned
856 * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
857 */
858
860 if (ret.Failed()) return { ret, INVALID_VEHICLE };
861
862 if (v->type == VEH_TRAIN && (!v->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return { CMD_ERROR, INVALID_VEHICLE };
863
864 /* check that we can allocate enough vehicles */
865 if (!(flags & DC_EXEC)) {
866 int veh_counter = 0;
867 do {
868 veh_counter++;
869 } while ((v = v->Next()) != nullptr);
870
871 if (!Vehicle::CanAllocateItem(veh_counter)) {
872 return { CommandCost(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME), INVALID_VEHICLE };
873 }
874 }
875
876 v = v_front;
877
878 VehicleID new_veh_id = INVALID_VEHICLE;
879 do {
880 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
881 /* we build the rear ends of multiheaded trains with the front ones */
882 continue;
883 }
884
885 /* In case we're building a multi headed vehicle and the maximum number of
886 * vehicles is almost reached (e.g. max trains - 1) not all vehicles would
887 * be cloned. When the non-primary engines were build they were seen as
888 * 'new' vehicles whereas they would immediately be joined with a primary
889 * engine. This caused the vehicle to be not build as 'the limit' had been
890 * reached, resulting in partially build vehicles and such. */
891 DoCommandFlag build_flags = flags;
892 if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
893
894 CommandCost cost;
895 std::tie(cost, new_veh_id, std::ignore, std::ignore, std::ignore) = Command<CMD_BUILD_VEHICLE>::Do(build_flags, tile, v->engine_type, false, INVALID_CARGO, INVALID_CLIENT_ID);
896
897 if (cost.Failed()) {
898 /* Can't build a part, then sell the stuff we already made; clear up the mess */
899 if (w_front != nullptr) Command<CMD_SELL_VEHICLE>::Do(flags, w_front->index, true, false, INVALID_CLIENT_ID);
900 return { cost, INVALID_VEHICLE };
901 }
902
903 total_cost.AddCost(cost);
904
905 if (flags & DC_EXEC) {
906 w = Vehicle::Get(new_veh_id);
907
908 if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
910 }
911
912 if (v->type == VEH_TRAIN && !v->IsFrontEngine()) {
913 /* this s a train car
914 * add this unit to the end of the train */
915 CommandCost result = Command<CMD_MOVE_RAIL_VEHICLE>::Do(flags, w->index, w_rear->index, true);
916 if (result.Failed()) {
917 /* The train can't be joined to make the same consist as the original.
918 * Sell what we already made (clean up) and return an error. */
919 Command<CMD_SELL_VEHICLE>::Do(flags, w_front->index, true, false, INVALID_CLIENT_ID);
921 return { result, INVALID_VEHICLE }; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
922 }
923 } else {
924 /* this is a front engine or not a train. */
925 w_front = w;
927 w->SetServiceIntervalIsCustom(v->ServiceIntervalIsCustom());
928 w->SetServiceIntervalIsPercent(v->ServiceIntervalIsPercent());
929 }
930 w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
931 }
932 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != nullptr);
933
934 if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
935 /* for trains this needs to be the front engine due to the callback function */
936 new_veh_id = w_front->index;
937 }
938
939 if (flags & DC_EXEC) {
940 /* Cloned vehicles belong to the same group */
941 Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, v_front->group_id, w_front->index, false, VehicleListIdentifier{});
942 }
943
944
945 /* Take care of refitting. */
946 w = w_front;
947 v = v_front;
948
949 /* Both building and refitting are influenced by newgrf callbacks, which
950 * makes it impossible to accurately estimate the cloning costs. In
951 * particular, it is possible for engines of the same type to be built with
952 * different numbers of articulated parts, so when refitting we have to
953 * loop over real vehicles first, and then the articulated parts of those
954 * vehicles in a different loop. */
955 do {
956 do {
957 if (flags & DC_EXEC) {
958 assert(w != nullptr);
959
960 /* Find out what's the best sub type */
961 uint8_t subtype = GetBestFittingSubType(v, w, v->cargo_type);
962 if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
963 CommandCost cost = std::get<0>(Command<CMD_REFIT_VEHICLE>::Do(flags, w->index, v->cargo_type, subtype, false, true, 0));
964 if (cost.Succeeded()) total_cost.AddCost(cost);
965 }
966
967 if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
968 w = w->GetNextArticulatedPart();
969 } else {
970 break;
971 }
972 } else {
973 const Engine *e = v->GetEngine();
974 CargoType initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : INVALID_CARGO);
975
976 if (v->cargo_type != initial_cargo && IsValidCargoType(initial_cargo)) {
977 bool dummy;
978 total_cost.AddCost(GetRefitCost(nullptr, v->engine_type, v->cargo_type, v->cargo_subtype, &dummy));
979 }
980 }
981
982 if (v->IsGroundVehicle() && v->HasArticulatedPart()) {
983 v = v->GetNextArticulatedPart();
984 } else {
985 break;
986 }
987 } while (v != nullptr);
988
989 if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = w->GetNextVehicle();
990 } while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != nullptr);
991
992 if (flags & DC_EXEC) {
993 /*
994 * Set the orders of the vehicle. Cannot do it earlier as we need
995 * the vehicle refitted before doing this, otherwise the moved
996 * cargo types might not match (passenger vs non-passenger)
997 */
998 CommandCost result = Command<CMD_CLONE_ORDER>::Do(flags, (share_orders ? CO_SHARE : CO_COPY), w_front->index, v_front->index);
999 if (result.Failed()) {
1000 /* The vehicle has already been bought, so now it must be sold again. */
1001 Command<CMD_SELL_VEHICLE>::Do(flags, w_front->index, true, false, INVALID_CLIENT_ID);
1002 return { result, INVALID_VEHICLE };
1003 }
1004
1005 /* Now clone the vehicle's name, if it has one. */
1006 if (!v_front->name.empty()) CloneVehicleName(v_front, w_front);
1007
1008 /* Since we can't estimate the cost of cloning a vehicle accurately we must
1009 * check whether the company has enough money manually. */
1010 if (!CheckCompanyHasMoney(total_cost)) {
1011 /* The vehicle has already been bought, so now it must be sold again. */
1012 Command<CMD_SELL_VEHICLE>::Do(flags, w_front->index, true, false, INVALID_CLIENT_ID);
1013 return { total_cost, INVALID_VEHICLE };
1014 }
1015 }
1016
1017 return { total_cost, new_veh_id };
1018}
1019
1028{
1029 VehicleList list;
1030
1031 if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
1032
1033 /* Send all the vehicles to a depot */
1034 bool had_success = false;
1035 for (uint i = 0; i < list.size(); i++) {
1036 const Vehicle *v = list[i];
1038
1039 if (ret.Succeeded()) {
1040 had_success = true;
1041
1042 /* Return 0 if DC_EXEC is not set this is a valid goto depot command)
1043 * In this case we know that at least one vehicle can be sent to a depot
1044 * and we will issue the command. We can now safely quit the loop, knowing
1045 * it will succeed at least once. With DC_EXEC we really need to send them to the depot */
1046 if (!(flags & DC_EXEC)) break;
1047 }
1048 }
1049
1050 return had_success ? CommandCost() : CMD_ERROR;
1051}
1052
1062{
1063 if (depot_cmd.Test(DepotCommandFlag::MassSend)) {
1064 /* Mass goto depot requested */
1065 if (!vli.Valid()) return CMD_ERROR;
1066 return SendAllVehiclesToDepot(flags, depot_cmd.Test(DepotCommandFlag::Service), vli);
1067 }
1068
1069 Vehicle *v = Vehicle::GetIfValid(veh_id);
1070 if (v == nullptr) return CMD_ERROR;
1071 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
1072
1073 return v->SendToDepot(flags, depot_cmd);
1074}
1075
1083CommandCost CmdRenameVehicle(DoCommandFlag flags, VehicleID veh_id, const std::string &text)
1084{
1085 Vehicle *v = Vehicle::GetIfValid(veh_id);
1086 if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
1087
1089 if (ret.Failed()) return ret;
1090
1091 bool reset = text.empty();
1092
1093 if (!reset) {
1095 if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE);
1096 }
1097
1098 if (flags & DC_EXEC) {
1099 if (reset) {
1100 v->name.clear();
1101 } else {
1102 v->name = text;
1103 }
1106 }
1107
1108 return CommandCost();
1109}
1110
1111
1121CommandCost CmdChangeServiceInt(DoCommandFlag flags, VehicleID veh_id, uint16_t serv_int, bool is_custom, bool is_percent)
1122{
1123 Vehicle *v = Vehicle::GetIfValid(veh_id);
1124 if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
1125
1127 if (ret.Failed()) return ret;
1128
1129 const Company *company = Company::Get(v->owner);
1130 is_percent = is_custom ? is_percent : company->settings.vehicle.servint_ispercent;
1131
1132 if (is_custom) {
1133 if (serv_int != GetServiceIntervalClamped(serv_int, is_percent)) return CMD_ERROR;
1134 } else {
1135 serv_int = CompanyServiceInterval(company, v->type);
1136 }
1137
1138 if (flags & DC_EXEC) {
1139 v->SetServiceInterval(serv_int);
1140 v->SetServiceIntervalIsCustom(is_custom);
1141 v->SetServiceIntervalIsPercent(is_percent);
1143 }
1144
1145 return CommandCost();
1146}
Base for aircraft.
@ VAF_HELI_DIRECT_DESCENT
The helicopter is descending directly at its destination (helipad or in front of hangar)
Definition aircraft.h:45
void UpdateAircraftCache(Aircraft *v, bool update_range=false)
Update cached values of an aircraft.
CommandCost CmdBuildAircraft(DoCommandFlag flags, TileIndex tile, const Engine *e, Vehicle **ret)
Build an aircraft.
Command definitions related to aircraft.
Various declarations for airports.
@ STARTTAKEOFF
Airplane has arrived at a runway for take-off.
Definition airport.h:72
@ TERM7
Heading for terminal 7.
Definition airport.h:80
CargoArray GetCapacityOfArticulatedParts(EngineID engine)
Get the capacity of the parts of a given engine.
uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
Count the number of articulated parts of an engine.
Functions related to articulated vehicles.
Command definitions related to autoreplace.
void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g)
Rebuild the left autoreplace list if an engine is removed or added.
Functions related to the autoreplace GUIs.
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
bool IsValidCargoType(CargoType t)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:105
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:74
Common return value for all commands.
bool Succeeded() const
Did this command succeed?
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Money GetCost() const
The costs as made up to this moment.
bool Failed() const
Did this command fail?
Enum-as-bit-set wrapper.
constexpr bool Test(Tenum value) const
Test if the enum value is set.
uint Truncate(uint max_move=UINT_MAX)
Truncates the cargo in this list to the given amount.
uint TotalCount() const
Returns sum of cargo, including reserved cargo.
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
DoCommandFlag
List of flags for a command.
@ DC_AUTOREPLACE
autoreplace/autorenew is in progress, this shall disable vehicle limits when building,...
@ DC_QUERY_COST
query cost only, don't build.
@ DC_EXEC
execute the given command
Definition of stuff that is very close to a company, like the company struct itself.
int CompanyServiceInterval(const Company *c, VehicleType type)
Get the service interval for the given company and vehicle type.
bool CheckCompanyHasMoney(CommandCost &cost)
Verify whether the company can pay the bill.
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
bool IsLocalCompany()
Is the current company the local company?
Map related accessors for depots.
bool IsDepotTile(Tile tile)
Is the given tile a tile with a depot on it?
Definition depot_map.h:41
VehicleType GetDepotVehicleType(Tile t)
Get the type of vehicles that can use a depot.
Definition depot_map.h:65
bool do_start
flag for starting playback of next_file at next opportunity
Definition dmusic.cpp:122
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition economy.cpp:966
ExpensesType
Types of expenses.
@ EXPENSES_ROADVEH_RUN
Running costs road vehicles.
@ EXPENSES_TRAIN_RUN
Running costs trains.
@ EXPENSES_AIRCRAFT_RUN
Running costs aircraft.
@ EXPENSES_SHIP_RUN
Running costs ships.
@ EXPENSES_NEW_VEHICLES
New vehicles.
Price
Enumeration of all base prices for use with Prices.
bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
Check if an engine is buildable.
Definition engine.cpp:1254
Functions related to engines.
@ AIR_CTOL
Conventional Take Off and Landing, i.e. planes.
Definition engine_type.h:97
@ AutoRefit
Automatic refitting is allowed.
uint16_t EngineID
Unique identification number of an engine.
Definition engine_type.h:23
@ RAILVEH_WAGON
simple wagon, not motorized
Definition engine_type.h:31
@ RAILVEH_MULTIHEAD
indicates a combination of two locomotives
Definition engine_type.h:30
Base class for groups and group functions.
Command definitions related to engine groups.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1529
virtual void MarkDirty()
Marks the vehicles to be redrawn and updates cached variables.
ClientID
'Unique' identifier to be given to clients
@ INVALID_CLIENT_ID
Client is not part of anything.
Base for the NewGRF implementation.
@ CBID_VEHICLE_REFIT_COST
Called to determine the cost factor for refitting a vehicle.
@ CBID_VEHICLE_START_STOP_CHECK
Called when the company (or AI) tries to start or stop a vehicle.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
uint16_t GetVehicleCallback(CallbackID callback, uint32_t param1, uint32_t param2, EngineID engine, const Vehicle *v)
Evaluate a newgrf callback for vehicles.
StringID GetGRFStringID(uint32_t grfid, GRFStringID stringid)
Returns the index for this stringid associated with its grfID.
Header of Action 04 "universal holder" structure and functions.
static constexpr GRFStringID GRFSTR_MISC_GRF_TEXT
Miscellaneous GRF text range.
Functions related to news.
void DeleteVehicleNews(VehicleID vid, AdviceType advice_type=AdviceType::Invalid)
Delete news with a given advice type about a vehicle.
@ VehicleWaiting
The vehicle is waiting in the depot.
Functions related to order backups.
uint16_t GetServiceIntervalClamped(int interval, bool ispercent)
Clamp the service interval to the correct min/max.
Command definitions related to orders.
Pseudo random number generator.
void SaveRandomSeeds(SavedRandomSeeds *storage)
Saves the current seeds.
void RestoreRandomSeeds(const SavedRandomSeeds &storage)
Restores previously saved seeds.
Road vehicle states.
void RoadVehUpdateCache(RoadVehicle *v, bool same_length=false)
Update the cache of a road vehicle.
CommandCost CmdBuildRoadVehicle(DoCommandFlag flags, TileIndex tile, const Engine *e, Vehicle **ret)
Build a road vehicle.
Command definitions related to road vehicles.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:57
Base for ships.
CommandCost CmdBuildShip(DoCommandFlag flags, TileIndex tile, const Engine *e, Vehicle **ret)
Build a ship.
Definition ship_cmd.cpp:895
Command definitions related to ships.
Definition of base types and functions in a cross-platform compatible way.
size_t Utf8StringLength(const char *s)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition string.cpp:359
Functions related to low-level strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
uint8_t subtype
Type of aircraft.
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition aircraft.h:72
uint8_t state
State of the airport.
Definition aircraft.h:77
uint8_t flags
Aircraft flags.
Definition aircraft.h:81
std::string name
Name of vehicle.
uint16_t service_interval
The interval for (automatic) servicing; either in days or %.
void ResetDepotUnbunching()
Resets all the data used for depot unbunching.
VehicleType type
Type of vehicle.
Class for storing amounts of cargo.
Definition cargo_type.h:112
Specification of a cargo type.
Definition cargotype.h:77
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:84
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:140
CompanySettings settings
settings specific for each company
VehicleDefaultSettings vehicle
default settings for vehicles
EngineMiscFlags misc_flags
Miscellaneous flags.
Money GetCost() const
Return how much a new engine costs.
Definition engine.cpp:318
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition engine.cpp:201
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition engine_base.h:61
uint GetDisplayDefaultCapacity(uint16_t *mail_capacity=nullptr) const
Determines the default cargo capacity of an engine for display purposes.
CargoType GetDefaultCargoType() const
Determines the default cargo type of an engine.
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition engine.cpp:168
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID)
Definition newgrf.h:131
VehicleSettings vehicle
options for vehicles
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
static void Restore(Vehicle *v, uint32_t user)
Restore the data of this order to the given vehicle.
static void Backup(const Vehicle *v, uint32_t user)
Create an order backup for the given vehicle.
VehicleOrderID GetNumOrders() const
Get number of orders in the order list.
Definition order_base.h:318
bool IsShared() const
Is this a shared order list?
Definition order_base.h:337
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:70
Tindex index
Index of this pool item.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static Titem * Get(size_t index)
Returns Titem with given index.
Helper structure for RefitVehicle()
uint capacity
New capacity of vehicle.
Vehicle * v
Vehicle to refit.
uint mail_capacity
New mail capacity of aircraft.
uint8_t subtype
cargo subtype to refit to
Stores the state of all random number generators.
void UpdateCache()
Update the caches of this ship.
Definition ship_cmd.cpp:232
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
bool servint_ispercent
service intervals are in percents
The information about a vehicle list.
Definition vehiclelist.h:29
VehicleType vtype
The vehicle type associated with this list.
Definition vehiclelist.h:31
uint8_t roadveh_acceleration_model
realistic acceleration for road vehicles
Vehicle data structure.
EngineID engine_type
The type of engine used for this vehicle.
const Engine * GetEngine() const
Retrieves the engine of the vehicle.
Definition vehicle.cpp:747
bool IsStoppedInDepot() const
Check whether the vehicle is in the depot and stopped.
virtual ExpensesType GetExpenseType(bool income) const
Sets the expense type associated to this vehicle type.
Vehicle * GetNextArticulatedPart() const
Get the next part of an articulated engine.
CommandCost SendToDepot(DoCommandFlag flags, DepotCommandFlags command)
Send this vehicle to the depot using the given command(s).
Definition vehicle.cpp:2583
VehicleCargoList cargo
The cargo this vehicle is carrying.
uint16_t cargo_cap
total capacity
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Vehicle * GetNextVehicle() const
Get the next real (non-articulated part) vehicle in the consist.
GroupID group_id
Index of group Pool array.
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
CargoType cargo_type
type of cargo this vehicle is carrying
debug_inline bool IsFrontEngine() const
Check if the vehicle is a front engine.
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Order current_order
The current order (+ status, like: loading)
Vehicle * Next() const
Get the next vehicle of this vehicle.
const GRFFile * GetGRF() const
Retrieve the NewGRF the vehicle is tied to.
Definition vehicle.cpp:757
OrderList * orders
Pointer to the order list for this vehicle.
Money value
Value of the vehicle.
uint16_t refit_cap
Capacity left over from before last refit.
uint8_t vehstatus
Status.
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the vehicle is tied to.
Definition vehicle.cpp:767
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
uint16_t cur_speed
current speed
uint8_t cargo_subtype
Used for livery refits (NewGRF variations)
debug_inline bool IsGroundVehicle() const
Check if the vehicle is a ground vehicle.
TileIndex tile
Current tile index.
void InvalidateNewGRFCacheOfChain()
Invalidates cached NewGRF variables of all vehicles in the chain (after the current vehicle)
Owner owner
Which company owns the vehicle?
UnitID unitnumber
unit number, for display purposes only
bool IsTileOwner(Tile tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition tile_map.h:214
Base for the train class.
@ VRF_REVERSE_DIRECTION
Reverse the visible direction of the vehicle.
Definition train.h:28
void NormalizeTrainVehInDepot(const Train *u)
Move all free vehicles in the depot to the train.
@ CCF_AUTOREFIT
Valid changes for autorefitting in stations.
Definition train.h:50
@ CCF_REFIT
Valid changes for refitting in a depot.
Definition train.h:51
CommandCost CmdBuildRailVehicle(DoCommandFlag flags, TileIndex tile, const Engine *e, Vehicle **ret)
Build a railroad vehicle.
CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *t, bool sell_chain, bool backup_order, ClientID user)
Sell a (single) train wagon/engine.
Command definitions related to trains.
uint16_t UnitID
Type for the company global vehicle unit number.
void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8_t num_vehicles)
Calculates the set of vehicles that will be affected by a given selection.
Definition vehicle.cpp:3218
UnitID GetFreeUnitNumber(VehicleType type)
Get an unused unit number for a vehicle (if allowed).
Definition vehicle.cpp:1895
@ VS_STOPPED
Vehicle is stopped by the player.
@ VS_CRASHED
Vehicle is crashed.
CommandCost CmdSendVehicleToDepot(DoCommandFlag flags, VehicleID veh_id, DepotCommandFlags depot_cmd, const VehicleListIdentifier &vli)
Send a vehicle to the depot.
static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
Clone the custom name of a vehicle, adding or incrementing a number.
bool IsUniqueVehicleName(const std::string &name)
Test if a name is unique among vehicle names.
std::tuple< CommandCost, VehicleID > CmdCloneVehicle(DoCommandFlag flags, TileIndex tile, VehicleID veh_id, bool share_orders)
Clone a vehicle.
CommandCost CmdSellVehicle(DoCommandFlag flags, VehicleID v_id, bool sell_chain, bool backup_order, ClientID client_id)
Sell a vehicle.
static CommandCost SendAllVehiclesToDepot(DoCommandFlag flags, bool service, const VehicleListIdentifier &vli)
Send all vehicles of type to depots.
CommandCost CmdRenameVehicle(DoCommandFlag flags, VehicleID veh_id, const std::string &text)
Give a custom name to your vehicle.
static CommandCost GetRefitCost(const Vehicle *v, EngineID engine_type, CargoType new_cargo_type, uint8_t new_subtype, bool *auto_refit_allowed)
Learn the price of refitting a certain engine.
static int GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoType new_cargo_type, uint8_t new_subtype, bool *auto_refit_allowed)
Helper to run the refit cost callback.
CommandCost CmdStartStopVehicle(DoCommandFlag flags, VehicleID veh_id, bool evaluate_startstop_cb)
Start/Stop a vehicle.
CommandCost CmdDepotSellAllVehicles(DoCommandFlag flags, TileIndex tile, VehicleType vehicle_type)
Sells all vehicles in a depot.
static std::tuple< CommandCost, uint, uint16_t, CargoArray > RefitVehicle(Vehicle *v, bool only_this, uint8_t num_vehicles, CargoType new_cargo_type, uint8_t new_subtype, DoCommandFlag flags, bool auto_refit)
Refits a vehicle (chain).
std::tuple< CommandCost, uint, uint16_t, CargoArray > CmdRefitVehicle(DoCommandFlag flags, VehicleID veh_id, CargoType new_cargo_type, uint8_t new_subtype, bool auto_refit, bool only_this, uint8_t num_vehicles)
Refits a vehicle to the specified cargo type.
CommandCost CmdDepotMassAutoReplace(DoCommandFlag flags, TileIndex tile, VehicleType vehicle_type)
Autoreplace all vehicles in the depot.
std::tuple< CommandCost, VehicleID, uint, uint16_t, CargoArray > CmdBuildVehicle(DoCommandFlag flags, TileIndex tile, EngineID eid, bool use_free_vehicles, CargoType cargo, ClientID client_id)
Build a vehicle.
CommandCost CmdChangeServiceInt(DoCommandFlag flags, VehicleID veh_id, uint16_t serv_int, bool is_custom, bool is_percent)
Change the service interval of a vehicle.
CommandCost CmdMassStartStopVehicle(DoCommandFlag flags, TileIndex tile, bool do_start, bool vehicle_list_window, const VehicleListIdentifier &vli)
Starts or stops a lot of vehicles.
Command definitions for vehicles.
Functions related to vehicles.
uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoType dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition vehicle_gui.h:97
VehicleType
Available vehicle types.
@ VEH_ROAD
Road vehicle type.
@ VEH_AIRCRAFT
Aircraft vehicle type.
@ VEH_SHIP
Ship vehicle type.
@ VEH_TRAIN
Train vehicle type.
@ MassSend
Tells that it's a mass send to depot command (type in VLW flag)
@ DontCancel
Don't cancel current goto depot command if any.
@ Service
The vehicle will leave the depot right after arrival (service only)
uint32_t VehicleID
The type all our vehicle IDs have.
static const uint MAX_LENGTH_VEHICLE_NAME_CHARS
The maximum length of a vehicle name in characters including '\0'.
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
@ WID_VV_START_STOP
Start or stop this vehicle, and show information about the current state.
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
Generate a list of vehicles based on window type.
void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
Generate a list of vehicles inside a depot.
Functions and type for generating vehicle lists.
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition vehiclelist.h:53
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition window.cpp:3125
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3217
void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, WidgetID widget_index)
Mark a particular widget in a particular window as dirty (in need of repainting)
Definition window.cpp:3112
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition window.cpp:3099
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3234
@ WC_VEHICLE_DEPOT
Depot view; Window numbers:
@ WC_VEHICLE_DETAILS
Vehicle details; Window numbers:
@ WC_COMPANY
Company view; Window numbers:
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers: