annotate src/vehicle_cmd.cpp @ 15852:ca2ecbcbbb22 draft

(svn r20534) -Codechange: unify the naming of the 'vehicle must be stopped in depot' strings
author yexo <yexo@openttd.org>
date Wed, 18 Aug 2010 00:30:12 +0000
parents 17927bf26be2
children 43d172eac154
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
1 /* $Id$ */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
2
12778
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12632
diff changeset
3 /*
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12632
diff changeset
4 * This file is part of OpenTTD.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12632
diff changeset
5 * 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.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12632
diff changeset
6 * 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.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12632
diff changeset
7 * 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/>.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12632
diff changeset
8 */
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12632
diff changeset
9
11350
8a62f1cb73a3 (svn r15700) -Codechange: split Cmd* from signs.cpp to signs_cmd.cpp.
rubidium <rubidium@openttd.org>
parents: 11349
diff changeset
10 /** @file vehicle_cmd.cpp Commands for vehicles. */
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
11
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
12 #include "stdafx.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
13 #include "roadveh.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
14 #include "news_func.h"
12228
240adc64d01a (svn r16643) -Codechange: replace GetStationByTile() by Station::GetByTile()
smatz <smatz@openttd.org>
parents: 12114
diff changeset
15 #include "airport.h"
15037
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
16 #include "cmd_helper.h"
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
17 #include "command_func.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
18 #include "company_func.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
19 #include "vehicle_gui.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
20 #include "train.h"
11982
9add5306a01e (svn r16388) -Codechange: move u.air to Aircraft
rubidium <rubidium@openttd.org>
parents: 11949
diff changeset
21 #include "aircraft.h"
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
22 #include "newgrf_engine.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
23 #include "newgrf_text.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
24 #include "functions.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
25 #include "window_func.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
26 #include "vehicle_func.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
27 #include "string_func.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
28 #include "depot_map.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
29 #include "vehiclelist.h"
14258
a899d4e5ee1a (svn r18809) -Codechange/Cleanup: remove unneeded headers from some files, if a header require a header make it include that header
rubidium <rubidium@openttd.org>
parents: 14232
diff changeset
30 #include "engine_base.h"
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
31 #include "engine_func.h"
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
32 #include "articulated_vehicles.h"
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
33 #include "autoreplace_gui.h"
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
34 #include "company_base.h"
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
35
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
36 #include "table/strings.h"
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
37
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
38 /* Tables used in vehicle.h to find the right command for a certain vehicle type */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
39 const uint32 _veh_build_proc_table[] = {
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
40 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
41 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
42 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
43 CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
44 };
12485
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
45
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
46 const uint32 _veh_sell_proc_table[] = {
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
47 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
48 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
49 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
50 CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
51 };
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
52
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
53 const uint32 _veh_refit_proc_table[] = {
12485
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
54 CMD_REFIT_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
55 CMD_REFIT_ROAD_VEH | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
56 CMD_REFIT_SHIP | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
57 CMD_REFIT_AIRCRAFT | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
58 };
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
59
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
60 const uint32 _send_to_depot_proc_table[] = {
12485
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
61 /* TrainGotoDepot has a nice randomizer in the pathfinder, which causes desyncs... */
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
62 CMD_SEND_TRAIN_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT) | CMD_NO_TEST_IF_IN_NETWORK,
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
63 CMD_SEND_ROADVEH_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
64 CMD_SEND_SHIP_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
cf4a59021c9d (svn r16922) -Codechange: unduplicate some vehicle CMD_*s and move the setting of the error message to a single place
rubidium <rubidium@openttd.org>
parents: 12483
diff changeset
65 CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
66 };
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
67
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
68
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
69 CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
70 CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
71 CommandCost CmdBuildShip (TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
72 CommandCost CmdBuildAircraft (TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **v);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
73
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
74 /**
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
75 * Build a vehicle.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
76 * @param tile tile of depot where the vehicle is built
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
77 * @param flags for command
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
78 * @param p1 various bitstuffed data
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
79 * bits 0-15: vehicle type being built.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
80 * bits 16-31: vehicle type specific bits passed on to the vehicle build functions.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
81 * @param p2 unused
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
82 * @param text unused
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
83 * @return the cost of this operation or an error
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
84 */
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
85 CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
86 {
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
87 /* Elementary check for valid location. */
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
88 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
89
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
90 VehicleType type;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
91 switch (GetTileType(tile)) {
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
92 case MP_RAILWAY: type = VEH_TRAIN; break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
93 case MP_ROAD: type = VEH_ROAD; break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
94 case MP_WATER: type = VEH_SHIP; break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
95 case MP_STATION: type = VEH_AIRCRAFT; break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
96 default: NOT_REACHED(); // Safe due to IsDepotTile()
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
97 }
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
98
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
99 /* Validate the engine type. */
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
100 EngineID eid = GB(p1, 0, 16);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
101 if (!IsEngineBuildable(eid, type, _current_company)) return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
102
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
103 const Engine *e = Engine::Get(eid);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
104 CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
105
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
106 /* Engines without valid cargo should not be available */
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
107 if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
108
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
109 /* Check whether the number of vehicles we need to build can be built according to pool space. */
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
110 uint num_vehicles;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
111 switch (type) {
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
112 case VEH_TRAIN: num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
113 case VEH_ROAD: num_vehicles = 1 + CountArticulatedParts(eid, false); break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
114 case VEH_SHIP: num_vehicles = 1; break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
115 case VEH_AIRCRAFT: num_vehicles = e->u.air.subtype & AIR_CTOL ? 2 : 3; break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
116 default: NOT_REACHED(); // Safe due to IsDepotTile()
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
117 }
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
118 if (!Vehicle::CanAllocateItem(num_vehicles)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
119
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
120 /* Check whether we can allocate a unit number. Autoreplace does not allocate
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
121 * an unit number as it will (always) reuse the one of the replaced vehicle
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
122 * and (train) wagons don't have an unit number in any scenario. */
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
123 UnitID unit_num = (flags & DC_AUTOREPLACE || (type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON)) ? 0 : GetFreeUnitNumber(type);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
124 if (unit_num == UINT16_MAX) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
125
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
126 Vehicle *v;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
127 switch (type) {
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
128 case VEH_TRAIN: value.AddCost(CmdBuildRailVehicle(tile, flags, e, GB(p1, 16, 16), &v)); break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
129 case VEH_ROAD: value.AddCost(CmdBuildRoadVehicle(tile, flags, e, GB(p1, 16, 16), &v)); break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
130 case VEH_SHIP: value.AddCost(CmdBuildShip (tile, flags, e, GB(p1, 16, 16), &v)); break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
131 case VEH_AIRCRAFT: value.AddCost(CmdBuildAircraft (tile, flags, e, GB(p1, 16, 16), &v)); break;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
132 default: NOT_REACHED(); // Safe due to IsDepotTile()
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
133 }
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
134
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
135 if (value.Succeeded() && flags & DC_EXEC) {
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
136 v->unitnumber = unit_num;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
137 v->value = value.GetCost();
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
138
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
139 InvalidateWindowData(WC_VEHICLE_DEPOT, tile);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
140 InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
141 SetWindowDirty(WC_COMPANY, _current_company);
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
142 if (IsLocalCompany()) {
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
143 InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the auto replace window
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
144 }
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
145
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
146 Company::Get(_current_company)->num_engines[eid]++;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
147 }
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
148
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
149 return value;
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
150 }
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
151
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
152 CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16 data);
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
153
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
154 /**
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
155 * Sell a vehicle.
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
156 * @param tile unused.
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
157 * @param flags for command.
15851
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
158 * @param p1 various bitstuffed data.
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
159 * bits 0-15: vehicle ID being sold.
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
160 * bits 16-31: vehicle type specific bits passed on to the vehicle build functions.
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
161 * @param p2 unused.
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
162 * @param text unused.
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
163 * @return the cost of this operation or an error.
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
164 */
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
165 CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
166 {
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
167 Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 16));
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
168 if (v == NULL) return CMD_ERROR;
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
169
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
170 Vehicle *front = v->First();
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
171
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
172 CommandCost ret = CheckOwnership(front->owner);
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
173 if (ret.Failed()) return ret;
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
174
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
175 if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
176
15852
ca2ecbcbbb22 (svn r20534) -Codechange: unify the naming of the 'vehicle must be stopped in depot' strings
yexo <yexo@openttd.org>
parents: 15851
diff changeset
177 if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
178
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
179 if (v->type == VEH_TRAIN) {
15851
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
180 ret = CmdSellRailWagon(flags, v, GB(p1, 16, 16));
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
181 } else {
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
182 ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
183
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
184 if (flags & DC_EXEC) {
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
185 delete front;
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
186 }
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
187 }
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
188
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
189 return ret;
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
190 }
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
191
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
192 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
193 * Start/Stop a vehicle
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
194 * @param tile unused
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
195 * @param flags type of operation
15088
a63b4234140f (svn r19714) -Feature: ctrl+click on a vehicle to start/stop it
smatz <smatz@openttd.org>
parents: 15040
diff changeset
196 * @param p1 vehicle to start/stop, don't forget to change CcStartStopVehicle if you modify this!
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
197 * @param p2 bit 0: Shall the start/stop newgrf callback be evaluated (only valid with DC_AUTOREPLACE for network safety)
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
198 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
199 * @return the cost of this operation or an error
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
200 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
201 CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
202 {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
203 /* Disable the effect of p2 bit 0, when DC_AUTOREPLACE is not set */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
204 if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
205
11949
c89301974eb8 (svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents: 11924
diff changeset
206 Vehicle *v = Vehicle::GetIfValid(p1);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
207 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
208
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
209 CommandCost ret = CheckOwnership(v->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
210 if (ret.Failed()) return ret;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
211
15323
7ef84bf590b7 (svn r19964) -Add: Show an error message when trying to start a crashed or destroyed vehicle.
terkhen <terkhen@openttd.org>
parents: 15312
diff changeset
212 if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
7ef84bf590b7 (svn r19964) -Add: Show an error message when trying to start a crashed or destroyed vehicle.
terkhen <terkhen@openttd.org>
parents: 15312
diff changeset
213
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
214 switch (v->type) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
215 case VEH_TRAIN:
14750
bf6990330311 (svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14465
diff changeset
216 if ((v->vehstatus & VS_STOPPED) && Train::From(v)->acc_cache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_CATENARY);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
217 break;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
218
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
219 case VEH_SHIP:
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
220 case VEH_ROAD:
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
221 break;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
222
11982
9add5306a01e (svn r16388) -Codechange: move u.air to Aircraft
rubidium <rubidium@openttd.org>
parents: 11949
diff changeset
223 case VEH_AIRCRAFT: {
12114
f020ec6be498 (svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
smatz <smatz@openttd.org>
parents: 12082
diff changeset
224 Aircraft *a = Aircraft::From(v);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
225 /* cannot stop airplane when in flight, or when taking off / landing */
15312
9c5f42023621 (svn r19953) -Fix [FS#3874]: Don't show an error message when trying to start/stop a crashed plane.
terkhen <terkhen@openttd.org>
parents: 15088
diff changeset
226 if (!(v->vehstatus & VS_CRASHED) && a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
15618
e4640748f223 (svn r20281) -Codechange: unify case scope closure + break coding style
rubidium <rubidium@openttd.org>
parents: 15323
diff changeset
227 break;
e4640748f223 (svn r20281) -Codechange: unify case scope closure + break coding style
rubidium <rubidium@openttd.org>
parents: 15323
diff changeset
228 }
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
229
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
230 default: return CMD_ERROR;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
231 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
232
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
233 /* Check if this vehicle can be started/stopped. The callback will fail or
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
234 * return 0xFF if it can. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
235 uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
236 if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF && HasBit(p2, 0)) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
237 StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
238 return_cmd_error(error);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
239 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
240
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
241 if (flags & DC_EXEC) {
12483
84df0c285bc4 (svn r16920) -Codechange: shuffle some strings around to simplify looking up vehicle type specific strings for a specific message
rubidium <rubidium@openttd.org>
parents: 12482
diff changeset
242 if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
243
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
244 v->vehstatus ^= VS_STOPPED;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
245 if (v->type != VEH_TRAIN) v->cur_speed = 0; // trains can stop 'slowly'
13094
c72c656565a6 (svn r17594) -Fix: Vehicle image was not always updated when needed.
frosch <frosch@openttd.org>
parents: 13070
diff changeset
246 v->MarkDirty();
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13015
diff changeset
247 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13015
diff changeset
248 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13015
diff changeset
249 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
250 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
251 return CommandCost();
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
252 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
253
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
254 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
255 * Starts or stops a lot of vehicles
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
256 * @param tile Tile of the depot where the vehicles are started/stopped (only used for depots)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
257 * @param flags type of operation
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
258 * @param p1 Station/Order/Depot ID (only used for vehicle list windows)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
259 * @param p2 bitmask
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
260 * - bit 0-4 Vehicle type
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
261 * - bit 5 false = start vehicles, true = stop vehicles
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
262 * - bit 6 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case
15037
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
263 * - bit 8-11 Vehicle List Window type (ignored unless bit 6 is set)
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
264 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
265 * @return the cost of this operation or an error
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
266 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
267 CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
268 {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
269 VehicleList list;
15037
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
270 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p2);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
271 bool start_stop = HasBit(p2, 5);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
272 bool vehicle_list_window = HasBit(p2, 6);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
273
15037
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
274 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
275
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
276 if (vehicle_list_window) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
277 uint32 id = p1;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
278 uint16 window_type = p2 & VLW_MASK;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
279
15039
53c099f9a3a9 (svn r19656) -Codechange/Fix: Report back if invalid vehicle lists are requested.
frosch <frosch@openttd.org>
parents: 15037
diff changeset
280 if (!GenerateVehicleSortList(&list, vehicle_type, _current_company, id, window_type)) return CMD_ERROR;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
281 } else {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
282 /* Get the list of vehicles in the depot */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
283 BuildDepotVehicleList(vehicle_type, tile, &list, NULL);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
284 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
285
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
286 for (uint i = 0; i < list.Length(); i++) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
287 const Vehicle *v = list[i];
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
288
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
289 if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
290
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
291 if (!vehicle_list_window) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
292 if (vehicle_type == VEH_TRAIN) {
13933
a71da8c8c5e2 (svn r18465) -Codechange: simplify CheckTrainInDepot and remove some unneeded wrapper functions
rubidium <rubidium@openttd.org>
parents: 13743
diff changeset
293 if (!Train::From(v)->IsInDepot()) continue;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
294 } else {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
295 if (!(v->vehstatus & VS_HIDDEN)) continue;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
296 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
297 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
298
14465
a03b4e5abfbd (svn r19024) -Fix [FS#3577]: mass stopping/starting/autoreplacing gave empty errors when there were no vehicles
rubidium <rubidium@openttd.org>
parents: 14314
diff changeset
299 /* Just try and don't care if some vehicle's can't be stopped. */
a03b4e5abfbd (svn r19024) -Fix [FS#3577]: mass stopping/starting/autoreplacing gave empty errors when there were no vehicles
rubidium <rubidium@openttd.org>
parents: 14314
diff changeset
300 DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
301 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
302
14465
a03b4e5abfbd (svn r19024) -Fix [FS#3577]: mass stopping/starting/autoreplacing gave empty errors when there were no vehicles
rubidium <rubidium@openttd.org>
parents: 14314
diff changeset
303 return CommandCost();
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
304 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
305
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
306 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
307 * Sells all vehicles in a depot
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
308 * @param tile Tile of the depot where the depot is
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
309 * @param flags type of operation
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
310 * @param p1 Vehicle type
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
311 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
312 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
313 * @return the cost of this operation or an error
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
314 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
315 CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
316 {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
317 VehicleList list;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
318
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
319 CommandCost cost(EXPENSES_NEW_VEHICLES);
15037
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
320 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
12494
c6663c02b4b2 (svn r16931) -Cleanup (r16922): one semicolon too much
smatz <smatz@openttd.org>
parents: 12487
diff changeset
321 uint sell_command = GetCmdSellVeh(vehicle_type);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
322
15037
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
323 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
324
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
325 /* Get the list of vehicles in the depot */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
326 BuildDepotVehicleList(vehicle_type, tile, &list, &list);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
327
14897
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
328 CommandCost last_error = CMD_ERROR;
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
329 bool had_success = false;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
330 for (uint i = 0; i < list.Length(); i++) {
15851
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
331 CommandCost ret = DoCommand(tile, list[i]->index | (1 << 16), 0, flags, sell_command);
14897
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
332 if (ret.Succeeded()) {
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
333 cost.AddCost(ret);
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
334 had_success = true;
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
335 } else {
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
336 last_error = ret;
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
337 }
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
338 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
339
14897
671004f47e42 (svn r19493) -Codechange: Keep track of last error in CmdDepotSellAllVehicles().
alberth <alberth@openttd.org>
parents: 14893
diff changeset
340 return had_success ? cost : last_error;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
341 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
342
14465
a03b4e5abfbd (svn r19024) -Fix [FS#3577]: mass stopping/starting/autoreplacing gave empty errors when there were no vehicles
rubidium <rubidium@openttd.org>
parents: 14314
diff changeset
343 /**
a03b4e5abfbd (svn r19024) -Fix [FS#3577]: mass stopping/starting/autoreplacing gave empty errors when there were no vehicles
rubidium <rubidium@openttd.org>
parents: 14314
diff changeset
344 * Autoreplace all vehicles in the depot
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
345 * @param tile Tile of the depot where the vehicles are
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
346 * @param flags type of operation
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
347 * @param p1 Type of vehicle
14465
a03b4e5abfbd (svn r19024) -Fix [FS#3577]: mass stopping/starting/autoreplacing gave empty errors when there were no vehicles
rubidium <rubidium@openttd.org>
parents: 14314
diff changeset
348 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
349 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
350 * @return the cost of this operation or an error
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
351 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
352 CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
353 {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
354 VehicleList list;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
355 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
15037
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
356 VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
357
15037
ae5f1c6f349c (svn r19654) -Codechange: Use Extract<> in more places.
frosch <frosch@openttd.org>
parents: 14901
diff changeset
358 if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
359 if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
360
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
361 /* Get the list of vehicles in the depot */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
362 BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
363
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
364 for (uint i = 0; i < list.Length(); i++) {
12015
f51061a22830 (svn r16421) -Codechange: do not unnecessarily remove constness or unnecessarily add it.
rubidium <rubidium@openttd.org>
parents: 11987
diff changeset
365 const Vehicle *v = list[i];
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
366
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
367 /* Ensure that the vehicle completely in the depot */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
368 if (!v->IsInDepot()) continue;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
369
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
370 CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
371
14465
a03b4e5abfbd (svn r19024) -Fix [FS#3577]: mass stopping/starting/autoreplacing gave empty errors when there were no vehicles
rubidium <rubidium@openttd.org>
parents: 14314
diff changeset
372 if (ret.Succeeded()) cost.AddCost(ret);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
373 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
374 return cost;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
375 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
376
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
377 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
378 * Learn the price of refitting a certain engine
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
379 * @param engine_type Which engine to refit
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
380 * @return Price for refitting
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
381 */
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
382 static CommandCost GetRefitCost(EngineID engine_type)
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
383 {
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
384 ExpensesType expense_type;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
385 const Engine *e = Engine::Get(engine_type);
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
386 Price base_price;
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
387 uint cost_factor = e->info.refit_cost;
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
388 switch (e->type) {
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
389 case VEH_SHIP:
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
390 base_price = PR_BUILD_VEHICLE_SHIP;
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
391 expense_type = EXPENSES_SHIP_RUN;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
392 break;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
393
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
394 case VEH_ROAD:
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
395 base_price = PR_BUILD_VEHICLE_ROAD;
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
396 expense_type = EXPENSES_ROADVEH_RUN;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
397 break;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
398
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
399 case VEH_AIRCRAFT:
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
400 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
401 expense_type = EXPENSES_AIRCRAFT_RUN;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
402 break;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
403
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
404 case VEH_TRAIN:
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
405 base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
406 cost_factor <<= 1;
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
407 expense_type = EXPENSES_TRAIN_RUN;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
408 break;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
409
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
410 default: NOT_REACHED();
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
411 }
15763
a60a50f29f64 (svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
rubidium <rubidium@openttd.org>
parents: 15620
diff changeset
412 return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grf_prop.grffile, -10));
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
413 }
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
414
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
415 /**
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
416 * Refits a vehicle (chain).
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
417 * This is the vehicle-type independent part of the CmdRefitXXX functions.
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
418 * @param v The vehicle to refit.
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
419 * @param only_this Whether to only refit this vehicle, or the whole chain.
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
420 * @param new_cid Cargotype to refit to
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
421 * @param new_subtype Cargo subtype to refit to
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
422 * @param flags Command flags
14886
3128bb512766 (svn r19482) -Doc: Doxygen additions/updates.
alberth <alberth@openttd.org>
parents: 14815
diff changeset
423 * @return Refit cost.
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
424 */
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
425 CommandCost RefitVehicle(Vehicle *v, bool only_this, CargoID new_cid, byte new_subtype, DoCommandFlag flags)
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
426 {
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
427 CommandCost cost(v->GetExpenseType(false));
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
428 uint total_capacity = 0;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
429
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
430 v->InvalidateNewGRFCacheOfChain();
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
431 for (; v != NULL; v = (only_this ? NULL : v->Next())) {
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
432 const Engine *e = Engine::Get(v->engine_type);
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
433 if (!e->CanCarryCargo() || !HasBit(e->info.refit_mask, new_cid)) continue;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
434
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
435 /* Back up the vehicle's cargo type */
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
436 CargoID temp_cid = v->cargo_type;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
437 byte temp_subtype = v->cargo_subtype;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
438 v->cargo_type = new_cid;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
439 v->cargo_subtype = new_subtype;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
440
13417
216dfd60fb2f (svn r17926) -Fix (r9352): Make the decision whether aircraft carry mail consistent. Now always the cargo class decides.
frosch <frosch@openttd.org>
parents: 13390
diff changeset
441 uint16 mail_capacity;
216dfd60fb2f (svn r17926) -Fix (r9352): Make the decision whether aircraft carry mail consistent. Now always the cargo class decides.
frosch <frosch@openttd.org>
parents: 13390
diff changeset
442 uint amount = GetVehicleCapacity(v, &mail_capacity);
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
443 total_capacity += amount;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
444
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
445 /* Restore the original cargo type */
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
446 v->cargo_type = temp_cid;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
447 v->cargo_subtype = temp_subtype;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
448
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
449 if (new_cid != v->cargo_type) {
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
450 cost.AddCost(GetRefitCost(v->engine_type));
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
451 }
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
452
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
453 if (flags & DC_EXEC) {
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
454 v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
455 v->cargo_type = new_cid;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
456 v->cargo_cap = amount;
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
457 v->cargo_subtype = new_subtype;
13417
216dfd60fb2f (svn r17926) -Fix (r9352): Make the decision whether aircraft carry mail consistent. Now always the cargo class decides.
frosch <frosch@openttd.org>
parents: 13390
diff changeset
458 if (v->type == VEH_AIRCRAFT) {
216dfd60fb2f (svn r17926) -Fix (r9352): Make the decision whether aircraft carry mail consistent. Now always the cargo class decides.
frosch <frosch@openttd.org>
parents: 13390
diff changeset
459 Vehicle *u = v->Next();
216dfd60fb2f (svn r17926) -Fix (r9352): Make the decision whether aircraft carry mail consistent. Now always the cargo class decides.
frosch <frosch@openttd.org>
parents: 13390
diff changeset
460 u->cargo_cap = mail_capacity;
216dfd60fb2f (svn r17926) -Fix (r9352): Make the decision whether aircraft carry mail consistent. Now always the cargo class decides.
frosch <frosch@openttd.org>
parents: 13390
diff changeset
461 u->cargo.Truncate(mail_capacity);
216dfd60fb2f (svn r17926) -Fix (r9352): Make the decision whether aircraft carry mail consistent. Now always the cargo class decides.
frosch <frosch@openttd.org>
parents: 13390
diff changeset
462 }
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
463 }
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
464 }
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
465
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
466 _returned_refit_capacity = total_capacity;
14176
41303b56f42c (svn r18724) -Fix [FS#3475] (r17899): refitting a non-refittable vehicle to it's default cargotype failed, causing problems for AIs
yexo <yexo@openttd.org>
parents: 13962
diff changeset
467 return cost;
13390
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
468 }
d3edbd44dacd (svn r17899) -Codechange: Deduplicate code for refitting vehicles.
frosch <frosch@openttd.org>
parents: 13175
diff changeset
469
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
470 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
471 * Test if a name is unique among vehicle names.
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
472 * @param name Name to test.
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
473 * @return True ifffffff the name is unique.
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
474 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
475 static bool IsUniqueVehicleName(const char *name)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
476 {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
477 const Vehicle *v;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
478
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
479 FOR_ALL_VEHICLES(v) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
480 if (v->name != NULL && strcmp(v->name, name) == 0) return false;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
481 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
482
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
483 return true;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
484 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
485
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
486 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
487 * Clone the custom name of a vehicle, adding or incrementing a number.
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
488 * @param src Source vehicle, with a custom name.
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
489 * @param dst Destination vehicle.
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
490 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
491 static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
492 {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
493 char buf[256];
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
494
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
495 /* Find the position of the first digit in the last group of digits. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
496 size_t number_position;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
497 for (number_position = strlen(src->name); number_position > 0; number_position--) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
498 /* The design of UTF-8 lets this work simply without having to check
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
499 * for UTF-8 sequences. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
500 if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
501 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
502
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
503 /* Format buffer and determine starting number. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
504 int num;
14901
a79ad74932d1 (svn r19498) -Feature [FS#3710]: Keep number padding intact when cloning vehicle names.
peter1138 <peter1138@openttd.org>
parents: 14898
diff changeset
505 byte padding = 0;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
506 if (number_position == strlen(src->name)) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
507 /* No digit at the end, so start at number 2. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
508 strecpy(buf, src->name, lastof(buf));
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
509 strecat(buf, " ", lastof(buf));
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
510 number_position = strlen(buf);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
511 num = 2;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
512 } else {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
513 /* Found digits, parse them and start at the next number. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
514 strecpy(buf, src->name, lastof(buf));
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
515 buf[number_position] = '\0';
14901
a79ad74932d1 (svn r19498) -Feature [FS#3710]: Keep number padding intact when cloning vehicle names.
peter1138 <peter1138@openttd.org>
parents: 14898
diff changeset
516 char *endptr;
a79ad74932d1 (svn r19498) -Feature [FS#3710]: Keep number padding intact when cloning vehicle names.
peter1138 <peter1138@openttd.org>
parents: 14898
diff changeset
517 num = strtol(&src->name[number_position], &endptr, 10) + 1;
a79ad74932d1 (svn r19498) -Feature [FS#3710]: Keep number padding intact when cloning vehicle names.
peter1138 <peter1138@openttd.org>
parents: 14898
diff changeset
518 padding = endptr - &src->name[number_position];
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
519 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
520
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
521 /* Check if this name is already taken. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
522 for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
523 /* Attach the number to the temporary name. */
14901
a79ad74932d1 (svn r19498) -Feature [FS#3710]: Keep number padding intact when cloning vehicle names.
peter1138 <peter1138@openttd.org>
parents: 14898
diff changeset
524 seprintf(&buf[number_position], lastof(buf), "%0*d", padding, num);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
525
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
526 /* Check the name is unique. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
527 if (IsUniqueVehicleName(buf)) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
528 dst->name = strdup(buf);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
529 break;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
530 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
531 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
532
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
533 /* All done. If we didn't find a name, it'll just use its default. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
534 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
535
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
536 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
537 * Clone a vehicle. If it is a train, it will clone all the cars too
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
538 * @param tile tile of the depot where the cloned vehicle is build
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
539 * @param flags type of operation
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
540 * @param p1 the original vehicle's index
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
541 * @param p2 1 = shared orders, else copied orders
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
542 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
543 * @return the cost of this operation or an error
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
544 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
545 CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
546 {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
547 CommandCost total_cost(EXPENSES_NEW_VEHICLES);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
548
11949
c89301974eb8 (svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents: 11924
diff changeset
549 Vehicle *v = Vehicle::GetIfValid(p1);
15040
3314e49704e7 (svn r19657) -Fix: Add saneness checks for front vehicles.
frosch <frosch@openttd.org>
parents: 15039
diff changeset
550 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
551 Vehicle *v_front = v;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
552 Vehicle *w = NULL;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
553 Vehicle *w_front = NULL;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
554 Vehicle *w_rear = NULL;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
555
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
556 /*
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
557 * v_front is the front engine in the original vehicle
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
558 * v is the car/vehicle of the original vehicle, that is currently being copied
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
559 * w_front is the front engine of the cloned vehicle
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
560 * w is the car/vehicle currently being cloned
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
561 * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
562 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
563
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
564 CommandCost ret = CheckOwnership(v->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
565 if (ret.Failed()) return ret;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
566
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12228
diff changeset
567 if (v->type == VEH_TRAIN && (!Train::From(v)->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
568
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
569 /* check that we can allocate enough vehicles */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
570 if (!(flags & DC_EXEC)) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
571 int veh_counter = 0;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
572 do {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
573 veh_counter++;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
574 } while ((v = v->Next()) != NULL);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
575
11900
46d45a7511f9 (svn r16300) -Codechange: reduce usage of Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11730
diff changeset
576 if (!Vehicle::CanAllocateItem(veh_counter)) {
11730
a39da1e4c1f5 (svn r16118) -Change/cleanup: remove the hexadecimal 'in TTD the string had this ID' from 'some' strings and replace the string name with something more sensible.
rubidium <rubidium@openttd.org>
parents: 11368
diff changeset
577 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
578 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
579 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
580
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
581 v = v_front;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
582
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
583 do {
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12300
diff changeset
584 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
585 /* we build the rear ends of multiheaded trains with the front ones */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
586 continue;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
587 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
588
13015
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
589 /* In case we're building a multi headed vehicle and the maximum number of
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
590 * vehicles is almost reached (e.g. max trains - 1) not all vehicles would
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
591 * be cloned. When the non-primary engines were build they were seen as
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
592 * 'new' vehicles whereas they would immediately be joined with a primary
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
593 * engine. This caused the vehicle to be not build as 'the limit' had been
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
594 * reached, resulting in partially build vehicles and such. */
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
595 DoCommandFlag build_flags = flags;
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
596 if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
597
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15848
diff changeset
598 CommandCost cost = DoCommand(tile, v->engine_type | (1 << 16), 0, build_flags, GetCmdBuildVeh(v));
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
599
14314
60d313b887b8 (svn r18866) -Codechange: remove the CmdFailed(ret)/CmdSucceeded(ret) wrapper functions
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
600 if (cost.Failed()) {
13015
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
601 /* Can't build a part, then sell the stuff we already made; clear up the mess */
15851
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
602 if (w_front != NULL) DoCommand(w_front->tile, w_front->index | (1 << 16), 0, flags, GetCmdSellVeh(w_front));
13015
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
603 return cost;
29efbe666d4a (svn r17509) -Fix [FS#3196]: if building a part fails during cloning, sell what was already cloned instead of leaving it 'for free'
rubidium <rubidium@openttd.org>
parents: 12953
diff changeset
604 }
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
605
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
606 total_cost.AddCost(cost);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
607
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
608 if (flags & DC_EXEC) {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11900
diff changeset
609 w = Vehicle::Get(_new_vehicle_id);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
610
12114
f020ec6be498 (svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
smatz <smatz@openttd.org>
parents: 12082
diff changeset
611 if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
f020ec6be498 (svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
smatz <smatz@openttd.org>
parents: 12082
diff changeset
612 SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
613 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
614
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12228
diff changeset
615 if (v->type == VEH_TRAIN && !Train::From(v)->IsFrontEngine()) {
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
616 /* this s a train car
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
617 * add this unit to the end of the train */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
618 CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
14314
60d313b887b8 (svn r18866) -Codechange: remove the CmdFailed(ret)/CmdSucceeded(ret) wrapper functions
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
619 if (result.Failed()) {
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
620 /* The train can't be joined to make the same consist as the original.
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
621 * Sell what we already made (clean up) and return an error. */
15851
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
622 DoCommand(w_front->tile, w_front->index | 1 << 16, 0, flags, GetCmdSellVeh(w_front));
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
623 DoCommand(w_front->tile, w->index | 1 << 16, 0, flags, GetCmdSellVeh(w));
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
624 return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
625 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
626 } else {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
627 /* this is a front engine or not a train. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
628 w_front = w;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
629 w->service_interval = v->service_interval;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
630 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
631 w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
632 }
12306
d0ff55a976ad (svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents: 12305
diff changeset
633 } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
634
12082
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 12032
diff changeset
635 if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
636 /* for trains this needs to be the front engine due to the callback function */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
637 _new_vehicle_id = w_front->index;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
638 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
639
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
640 if (flags & DC_EXEC) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
641 /* Cloned vehicles belong to the same group */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
642 DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
643 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
644
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
645
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
646 /* Take care of refitting. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
647 w = w_front;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
648 v = v_front;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
649
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
650 /* Both building and refitting are influenced by newgrf callbacks, which
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
651 * makes it impossible to accurately estimate the cloning costs. In
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
652 * particular, it is possible for engines of the same type to be built with
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
653 * different numbers of articulated parts, so when refitting we have to
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
654 * loop over real vehicles first, and then the articulated parts of those
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
655 * vehicles in a different loop. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
656 do {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
657 do {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
658 if (flags & DC_EXEC) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
659 assert(w != NULL);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
660
13962
9b98b4f050e5 (svn r18498) -Fix [FS#2616]: cloning of vehicles could create vehicles with invalid cargo sub types for the build year of the vehicle. Fall back to another cargo sub type with the exact same name, otherwise fallback to cargo sub type 0.
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
661 /* Find out what's the best sub type */
9b98b4f050e5 (svn r18498) -Fix [FS#2616]: cloning of vehicles could create vehicles with invalid cargo sub types for the build year of the vehicle. Fall back to another cargo sub type with the exact same name, otherwise fallback to cargo sub type 0.
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
662 byte subtype = GetBestFittingSubType(v, w);
9b98b4f050e5 (svn r18498) -Fix [FS#2616]: cloning of vehicles could create vehicles with invalid cargo sub types for the build year of the vehicle. Fall back to another cargo sub type with the exact same name, otherwise fallback to cargo sub type 0.
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
663 if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
9b98b4f050e5 (svn r18498) -Fix [FS#2616]: cloning of vehicles could create vehicles with invalid cargo sub types for the build year of the vehicle. Fall back to another cargo sub type with the exact same name, otherwise fallback to cargo sub type 0.
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
664 CommandCost cost = DoCommand(0, w->index, v->cargo_type | (subtype << 8) | 1U << 16, flags, GetCmdRefitVeh(v));
14314
60d313b887b8 (svn r18866) -Codechange: remove the CmdFailed(ret)/CmdSucceeded(ret) wrapper functions
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
665 if (cost.Succeeded()) total_cost.AddCost(cost);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
666 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
667
12305
217431f4bc67 (svn r16722) -Codechange: unify the naming of the Is/Set/HasArticulatedPart functions
rubidium <rubidium@openttd.org>
parents: 12304
diff changeset
668 if (w->type == VEH_TRAIN && Train::From(w)->HasArticulatedPart()) {
12306
d0ff55a976ad (svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents: 12305
diff changeset
669 w = Train::From(w)->GetNextArticPart();
12305
217431f4bc67 (svn r16722) -Codechange: unify the naming of the Is/Set/HasArticulatedPart functions
rubidium <rubidium@openttd.org>
parents: 12304
diff changeset
670 } else if (w->type == VEH_ROAD && RoadVehicle::From(w)->HasArticulatedPart()) {
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
671 w = w->Next();
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
672 } else {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
673 break;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
674 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
675 } else {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11900
diff changeset
676 const Engine *e = Engine::Get(v->engine_type);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
677 CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
678
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
679 if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
680 total_cost.AddCost(GetRefitCost(v->engine_type));
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
681 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
682 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
683
12305
217431f4bc67 (svn r16722) -Codechange: unify the naming of the Is/Set/HasArticulatedPart functions
rubidium <rubidium@openttd.org>
parents: 12304
diff changeset
684 if (v->type == VEH_TRAIN && Train::From(v)->HasArticulatedPart()) {
12306
d0ff55a976ad (svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents: 12305
diff changeset
685 v = Train::From(v)->GetNextArticPart();
12305
217431f4bc67 (svn r16722) -Codechange: unify the naming of the Is/Set/HasArticulatedPart functions
rubidium <rubidium@openttd.org>
parents: 12304
diff changeset
686 } else if (v->type == VEH_ROAD && RoadVehicle::From(v)->HasArticulatedPart()) {
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
687 v = v->Next();
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
688 } else {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
689 break;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
690 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
691 } while (v != NULL);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
692
12306
d0ff55a976ad (svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents: 12305
diff changeset
693 if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = Train::From(w)->GetNextVehicle();
d0ff55a976ad (svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents: 12305
diff changeset
694 } while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
695
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
696 if (flags & DC_EXEC) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
697 /*
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
698 * Set the orders of the vehicle. Cannot do it earlier as we need
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
699 * the vehicle refitted before doing this, otherwise the moved
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
700 * cargo types might not match (passenger vs non-passenger)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
701 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
702 DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
703
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
704 /* Now clone the vehicle's name, if it has one. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
705 if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
706 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
707
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
708 /* Since we can't estimate the cost of cloning a vehicle accurately we must
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
709 * check whether the company has enough money manually. */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
710 if (!CheckCompanyHasMoney(total_cost)) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
711 if (flags & DC_EXEC) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
712 /* The vehicle has already been bought, so now it must be sold again. */
15851
17927bf26be2 (svn r20533) -Codechange: shuffle a bit with the bits in the sell command
rubidium <rubidium@openttd.org>
parents: 15850
diff changeset
713 DoCommand(w_front->tile, w_front->index | 1 << 16, 0, flags, GetCmdSellVeh(w_front));
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
714 }
14232
860ea22258ba (svn r18783) -Codechange: make CheckCompanyHasMoney set an error on the CommandCost it tests when you don't have enough money instead of setting a global variable.
rubidium <rubidium@openttd.org>
parents: 14176
diff changeset
715 return total_cost;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
716 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
717
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
718 return total_cost;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
719 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
720
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
721 /**
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
722 * Send all vehicles of type to depots
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
723 * @param type type of vehicle
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
724 * @param flags the flags used for DoCommand()
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
725 * @param service should the vehicles only get service in the depots
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
726 * @param owner owner of the vehicles to send
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
727 * @param vlw_flag tells what kind of list requested the goto depot
13070
1c4c4ae8d716 (svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents: 13067
diff changeset
728 * @param id general purpose id whoms meaning is given by @c vlw_flag; e.g. StationID for station lists
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
729 * @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
730 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
731 CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
732 {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
733 VehicleList list;
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
734
15039
53c099f9a3a9 (svn r19656) -Codechange/Fix: Report back if invalid vehicle lists are requested.
frosch <frosch@openttd.org>
parents: 15037
diff changeset
735 if (!GenerateVehicleSortList(&list, type, owner, id, vlw_flag)) return CMD_ERROR;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
736
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
737 /* Send all the vehicles to a depot */
14893
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
738 bool had_success = false;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
739 for (uint i = 0; i < list.Length(); i++) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
740 const Vehicle *v = list[i];
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
741 CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
742
14893
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
743 if (ret.Succeeded()) {
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
744 had_success = true;
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
745
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
746 /* Return 0 if DC_EXEC is not set this is a valid goto depot command)
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
747 * In this case we know that at least one vehicle can be sent to a depot
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
748 * and we will issue the command. We can now safely quit the loop, knowing
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
749 * it will succeed at least once. With DC_EXEC we really need to send them to the depot */
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
750 if (!(flags & DC_EXEC)) break;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
751 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
752 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
753
14893
0a799fd7f1a8 (svn r19489) -Codechange: Introduce a had_success variable in SendAllVehiclesToDepot().
alberth <alberth@openttd.org>
parents: 14886
diff changeset
754 return had_success ? CommandCost() : CMD_ERROR;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
755 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
756
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
757 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
758 * Give a custom name to your vehicle
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
759 * @param tile unused
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
760 * @param flags type of operation
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
761 * @param p1 vehicle ID to name
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
762 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
763 * @param text the new name or an empty string when resetting to the default
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
764 * @return the cost of this operation or an error
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
765 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
766 CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
767 {
11949
c89301974eb8 (svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents: 11924
diff changeset
768 Vehicle *v = Vehicle::GetIfValid(p1);
15040
3314e49704e7 (svn r19657) -Fix: Add saneness checks for front vehicles.
frosch <frosch@openttd.org>
parents: 15039
diff changeset
769 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
770
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
771 CommandCost ret = CheckOwnership(v->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
772 if (ret.Failed()) return ret;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
773
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
774 bool reset = StrEmpty(text);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
775
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
776 if (!reset) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
777 if (strlen(text) >= MAX_LENGTH_VEHICLE_NAME_BYTES) return CMD_ERROR;
12632
e41943276802 (svn r17075) -Codechange: rename ~750 strings to be more uniform with their relatives
rubidium <rubidium@openttd.org>
parents: 12619
diff changeset
778 if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
779 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
780
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
781 if (flags & DC_EXEC) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
782 free(v->name);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
783 v->name = reset ? NULL : strdup(text);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
784 InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
785 MarkWholeScreenDirty();
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
786 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
787
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
788 return CommandCost();
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
789 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
790
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
791
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
792 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15618
diff changeset
793 * Change the service interval of a vehicle
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
794 * @param tile unused
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
795 * @param flags type of operation
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
796 * @param p1 vehicle ID that is being service-interval-changed
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
797 * @param p2 new service interval
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
798 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
799 * @return the cost of this operation or an error
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
800 */
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
801 CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
802 {
11949
c89301974eb8 (svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents: 11924
diff changeset
803 Vehicle *v = Vehicle::GetIfValid(p1);
15040
3314e49704e7 (svn r19657) -Fix: Add saneness checks for front vehicles.
frosch <frosch@openttd.org>
parents: 15039
diff changeset
804 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
805
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
806 CommandCost ret = CheckOwnership(v->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14750
diff changeset
807 if (ret.Failed()) return ret;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
808
12032
2afeb10ae6ba (svn r16439) -Change: Make the default vehicle servicing settings company-based settings, so in a multiplayer game everyone can change them.
yexo <yexo@openttd.org>
parents: 12015
diff changeset
809 uint16 serv_int = GetServiceIntervalClamped(p2, v->owner); // Double check the service interval from the user-input
2afeb10ae6ba (svn r16439) -Change: Make the default vehicle servicing settings company-based settings, so in a multiplayer game everyone can change them.
yexo <yexo@openttd.org>
parents: 12015
diff changeset
810 if (serv_int != p2) return CMD_ERROR;
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
811
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
812 if (flags & DC_EXEC) {
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
813 v->service_interval = serv_int;
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13015
diff changeset
814 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
11349
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
815 }
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
816
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
817 return CommandCost();
bf07f69e1221 (svn r15699) -Codechange: split Cmd* from vehicle.cpp to vehicle_cmd.cpp.
rubidium <rubidium@openttd.org>
parents:
diff changeset
818 }