You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OpenTTD-patches/src/vehicle_cmd.cpp

1868 lines
61 KiB
C++

/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file vehicle_cmd.cpp Commands for vehicles. */
#include "stdafx.h"
#include "roadveh.h"
#include "news_func.h"
#include "airport.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "company_func.h"
#include "train.h"
#include "aircraft.h"
#include "newgrf_text.h"
#include "vehicle_func.h"
#include "string_func.h"
#include "depot_map.h"
#include "vehiclelist.h"
#include "engine_func.h"
#include "articulated_vehicles.h"
#include "autoreplace_gui.h"
#include "group.h"
#include "order_backup.h"
#include "infrastructure_func.h"
#include "ship.h"
#include "newgrf.h"
#include "company_base.h"
#include "core/random_func.hpp"
#include "tbtr_template_vehicle.h"
#include "tbtr_template_vehicle_func.h"
#include "scope.h"
#include <sstream>
#include <iomanip>
#include <cctype>
#include "table/strings.h"
#include "safeguards.h"
/* Tables used in vehicle.h to find the right command for a certain vehicle type */
const uint32_t _veh_build_proc_table[] = {
CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
CMD_BUILD_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
};
const uint32_t _veh_sell_proc_table[] = {
CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
};
const uint32_t _veh_refit_proc_table[] = {
CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
};
const uint32_t _send_to_depot_proc_table[] = {
CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT),
CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
CMD_SEND_VEHICLE_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
};
CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, Vehicle **v);
CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, Vehicle **v);
CommandCost CmdBuildShip (TileIndex tile, DoCommandFlag flags, const Engine *e, Vehicle **v);
CommandCost CmdBuildAircraft (TileIndex tile, DoCommandFlag flags, const Engine *e, Vehicle **v);
static CommandCost GetRefitCost(const Vehicle *v, EngineID engine_type, CargoID new_cid, uint8_t new_subtype, bool *auto_refit_allowed);
/**
* Build a vehicle.
* @param tile tile of depot where the vehicle is built
* @param flags for command
* @param p1 various bitstuffed data
* bits 0-15: vehicle type being built.
* bits 16-23: vehicle type specific bits.
* Trains:
* * bit 16: prevent any free cars from being added to the train.
* bits 24-31: refit cargo type.
* @param p2 User
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
/* Elementary check for valid location. */
if (!IsDepotTile(tile)) return CMD_ERROR;
VehicleType type = GetDepotVehicleType(tile);
if (!IsTileOwner(tile, _current_company)) {
if (!_settings_game.economy.infrastructure_sharing[type]) return_cmd_error(STR_ERROR_CANT_PURCHASE_OTHER_COMPANY_DEPOT);
const Company *c = Company::GetIfValid(GetTileOwner(tile));
if (c == nullptr || !c->settings.infra_others_buy_in_depot[type]) return_cmd_error(STR_ERROR_CANT_PURCHASE_OTHER_COMPANY_DEPOT);
}
/* Validate the engine type. */
EngineID eid = GB(p1, 0, 16);
if (!IsEngineBuildable(eid, type, _current_company)) return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type);
/* Validate the cargo type. */
CargoID cargo = GB(p1, 24, 8);
if (cargo >= NUM_CARGO && cargo != INVALID_CARGO) return CMD_ERROR;
const Engine *e = Engine::Get(eid);
CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
/* Engines without valid cargo should not be available */
CargoID default_cargo = e->GetDefaultCargoType();
if (default_cargo == INVALID_CARGO) return CMD_ERROR;
bool refitting = cargo != INVALID_CARGO && cargo != default_cargo;
/* Check whether the number of vehicles we need to build can be built according to pool space. */
uint num_vehicles;
switch (type) {
case VEH_TRAIN: num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); break;
case VEH_ROAD: num_vehicles = 1 + CountArticulatedParts(eid, false); break;
case VEH_SHIP: num_vehicles = 1 + CountArticulatedParts(eid, false); break;
case VEH_AIRCRAFT: num_vehicles = e->u.air.subtype & AIR_CTOL ? 2 : 3; break;
default: NOT_REACHED(); // Safe due to IsDepotTile()
}
if (!Vehicle::CanAllocateItem(num_vehicles)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
/* Check whether we can allocate a unit number. Autoreplace does not allocate
* an unit number as it will (always) reuse the one of the replaced vehicle
* and (train) wagons don't have an unit number in any scenario. */
UnitID unit_num = (flags & DC_QUERY_COST || flags & DC_AUTOREPLACE || (type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON)) ? 0 : GetFreeUnitNumber(type);
if (unit_num == UINT16_MAX) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
/* If we are refitting we need to temporarily purchase the vehicle to be able to
* test it. */
DoCommandFlag subflags = flags;
if (refitting && !(flags & DC_EXEC)) subflags |= DC_EXEC | DC_AUTOREPLACE;
/* Vehicle construction needs random bits, so we have to save the random
* seeds to prevent desyncs. */
SavedRandomSeeds saved_seeds;
SaveRandomSeeds(&saved_seeds);
Vehicle *v = nullptr;
switch (type) {
case VEH_TRAIN: value.AddCost(CmdBuildRailVehicle(tile, subflags, e, &v)); break;
case VEH_ROAD: value.AddCost(CmdBuildRoadVehicle(tile, subflags, e, &v)); break;
case VEH_SHIP: value.AddCost(CmdBuildShip (tile, subflags, e, &v)); break;
case VEH_AIRCRAFT: value.AddCost(CmdBuildAircraft (tile, subflags, e, &v)); break;
default: NOT_REACHED(); // Safe due to IsDepotTile()
}
if (value.Succeeded()) {
if (subflags & DC_EXEC) {
v->unitnumber = unit_num;
v->value = value.GetCost();
}
if (refitting) {
/* Refit only one vehicle. If we purchased an engine, it may have gained free wagons.
* For ships try to refit all parts. */
value.AddCost(CmdRefitVehicle(tile, flags, v->index, cargo | (v->type == VEH_SHIP ? 0 : (1 << 16)), nullptr));
} else {
/* Fill in non-refitted capacities */
if (e->type == VEH_TRAIN || e->type == VEH_ROAD || e->type == VEH_SHIP) {
_returned_vehicle_capacities = GetCapacityOfArticulatedParts(eid);
_returned_refit_capacity = _returned_vehicle_capacities[default_cargo];
_returned_mail_refit_capacity = 0;
} else {
_returned_refit_capacity = e->GetDisplayDefaultCapacity(&_returned_mail_refit_capacity);
_returned_vehicle_capacities.Clear();
_returned_vehicle_capacities[default_cargo] = _returned_refit_capacity;
CargoID mail = GetCargoIDByLabel(CT_MAIL);
if (IsValidCargoID(mail)) _returned_vehicle_capacities[mail] = _returned_mail_refit_capacity;
}
}
if (flags & DC_EXEC) {
if (type == VEH_TRAIN && !HasBit(p1, 16) && !(flags & DC_AUTOREPLACE) && Train::From(v)->IsEngine()) {
/* Move any free wagons to the new vehicle. */
NormalizeTrainVehInDepot(Train::From(v));
}
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
InvalidateWindowClassesData(WC_DEPARTURES_BOARD, 0);
SetWindowDirty(WC_COMPANY, _current_company);
if (IsLocalCompany()) {
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the auto replace window (must be called before incrementing num_engines)
}
}
if (subflags & DC_EXEC) {
GroupStatistics::CountEngine(v, 1);
GroupStatistics::UpdateAutoreplace(_current_company);
if (v->IsPrimaryVehicle()) {
GroupStatistics::CountVehicle(v, 1);
if (!(subflags & DC_AUTOREPLACE)) OrderBackup::Restore(v, p2);
}
Company::Get(v->owner)->freeunits[v->type].UseID(v->unitnumber);
}
/* If we are not in DC_EXEC undo everything */
if (flags != subflags) {
DoCommand(0, v->index, 0, DC_EXEC, GetCmdSellVeh(v));
}
}
/* Only restore if we actually did some refitting */
if (flags != subflags) RestoreRandomSeeds(saved_seeds);
return value;
}
CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16_t data, uint32_t user);
/**
* Sell a vehicle.
* @param tile unused.
* @param flags for command.
* @param p1 various bitstuffed data.
* bits 0-19: vehicle ID being sold.
* bits 20-30: vehicle type specific bits passed on to the vehicle build functions.
* bit 31: make a backup of the vehicle's order (if an engine).
* @param p2 User.
* @param text unused.
* @return the cost of this operation or an error.
*/
CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
if (v == nullptr) return CMD_ERROR;
Vehicle *front = v->First();
CommandCost ret = CheckOwnership(front->owner);
if (ret.Failed()) return ret;
if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
/* Do this check only if the vehicle to be moved is non-virtual */
if (!HasBit(p1, 21)) {
if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
}
/* Can we actually make the order backup, i.e. are there enough orders? */
if (p1 & MAKE_ORDER_BACKUP_FLAG &&
front->orders != nullptr &&
!front->orders->IsShared() &&
!Order::CanAllocateItem(front->orders->GetNumOrders())) {
/* Only happens in exceptional cases when there aren't enough orders anyhow.
* Thus it should be safe to just drop the orders in that case. */
p1 &= ~MAKE_ORDER_BACKUP_FLAG;
}
if (v->type == VEH_TRAIN) {
ret = CmdSellRailWagon(flags, v, GB(p1, 20, 12), p2);
} else {
ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
if (flags & DC_EXEC) {
if (front->IsPrimaryVehicle() && p1 & MAKE_ORDER_BACKUP_FLAG) OrderBackup::Backup(front, p2);
delete front;
}
}
return ret;
}
CommandCost CmdSellVirtualVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
Train *v = Train::GetIfValid(GB(p1, 0, 20));
if (v == nullptr || !v->IsVirtual()) return CMD_ERROR;
return CmdSellVehicle(tile, flags, p1, p2, text);
}
/**
* Helper to run the refit cost callback.
* @param v The vehicle we are refitting, can be nullptr.
* @param engine_type Which engine to refit
* @param new_cid Cargo type we are refitting to.
* @param new_subtype New cargo subtype.
* @param[out] auto_refit_allowed The refit is allowed as an auto-refit.
* @return Price for refitting
*/
static int GetRefitCostFactor(const Vehicle *v, EngineID engine_type, CargoID new_cid, uint8_t new_subtype, bool *auto_refit_allowed)
{
/* Prepare callback param with info about the new cargo type. */
const Engine *e = Engine::Get(engine_type);
/* Is this vehicle a NewGRF vehicle? */
if (e->GetGRF() != nullptr && (e->callbacks_used & SGCU_VEHICLE_REFIT_COST) != 0) {
const CargoSpec *cs = CargoSpec::Get(new_cid);
uint32_t param1 = (cs->classes << 16) | (new_subtype << 8) | e->GetGRF()->cargo_map[new_cid];
uint16_t cb_res = GetVehicleCallback(CBID_VEHICLE_REFIT_COST, param1, 0, engine_type, v);
if (cb_res != CALLBACK_FAILED) {
*auto_refit_allowed = HasBit(cb_res, 14);
int factor = GB(cb_res, 0, 14);
if (factor >= 0x2000) factor -= 0x4000; // Treat as signed integer.
return factor;
}
}
*auto_refit_allowed = e->info.refit_cost == 0;
return (v == nullptr || v->cargo_type != new_cid) ? e->info.refit_cost : 0;
}
/**
* Learn the price of refitting a certain engine
* @param v The vehicle we are refitting, can be nullptr.
* @param engine_type Which engine to refit
* @param new_cid Cargo type we are refitting to.
* @param new_subtype New cargo subtype.
* @param[out] auto_refit_allowed The refit is allowed as an auto-refit.
* @return Price for refitting
*/
static CommandCost GetRefitCost(const Vehicle *v, EngineID engine_type, CargoID new_cid, uint8_t new_subtype, bool *auto_refit_allowed)
{
ExpensesType expense_type;
const Engine *e = Engine::Get(engine_type);
Price base_price;
int cost_factor = GetRefitCostFactor(v, engine_type, new_cid, new_subtype, auto_refit_allowed);
switch (e->type) {
case VEH_SHIP:
base_price = PR_BUILD_VEHICLE_SHIP;
expense_type = EXPENSES_SHIP_RUN;
break;
case VEH_ROAD:
base_price = PR_BUILD_VEHICLE_ROAD;
expense_type = EXPENSES_ROADVEH_RUN;
break;
case VEH_AIRCRAFT:
base_price = PR_BUILD_VEHICLE_AIRCRAFT;
expense_type = EXPENSES_AIRCRAFT_RUN;
break;
case VEH_TRAIN:
base_price = (e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN;
cost_factor <<= 1;
expense_type = EXPENSES_TRAIN_RUN;
break;
default: NOT_REACHED();
}
if (cost_factor < 0) {
return CommandCost(expense_type, -GetPrice(base_price, -cost_factor, e->GetGRF(), -10));
} else {
return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->GetGRF(), -10));
}
}
/** Helper structure for RefitVehicle() */
struct RefitResult {
Vehicle *v; ///< Vehicle to refit
uint capacity; ///< New capacity of vehicle
uint mail_capacity; ///< New mail capacity of aircraft
uint8_t subtype; ///< cargo subtype to refit to
};
/**
* Refits a vehicle (chain).
* This is the vehicle-type independent part of the CmdRefitXXX functions.
* @param v The vehicle to refit.
* @param only_this Whether to only refit this vehicle, or to check the rest of them.
* @param num_vehicles Number of vehicles to refit (not counting articulated parts). Zero means the whole chain.
* @param new_cid Cargotype to refit to
* @param new_subtype Cargo subtype to refit to. 0xFF means to try keeping the same subtype according to GetBestFittingSubType().
* @param flags Command flags
* @param auto_refit Refitting is done as automatic refitting outside a depot.
* @return Refit cost.
*/
static CommandCost RefitVehicle(Vehicle *v, bool only_this, uint8_t num_vehicles, CargoID new_cid, uint8_t new_subtype, DoCommandFlag flags, bool auto_refit)
{
CommandCost cost(v->GetExpenseType(false));
uint total_capacity = 0;
uint total_mail_capacity = 0;
num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
_returned_vehicle_capacities.Clear();
VehicleSet vehicles_to_refit;
if (!only_this) {
GetVehicleSet(vehicles_to_refit, v, num_vehicles);
/* In this case, we need to check the whole chain. */
v = v->First();
}
std::vector<RefitResult> refit_result;
v->InvalidateNewGRFCacheOfChain();
uint8_t actual_subtype = new_subtype;
for (; v != nullptr; v = (only_this ? nullptr : v->Next())) {
/* Reset actual_subtype for every new vehicle */
if (!v->IsArticulatedPart()) actual_subtype = new_subtype;
if (v->type == VEH_TRAIN && std::find(vehicles_to_refit.begin(), vehicles_to_refit.end(), v->index) == vehicles_to_refit.end() && !only_this) continue;
const Engine *e = v->GetEngine();
if (!e->CanCarryCargo()) continue;
/* If the vehicle is not refittable, or does not allow automatic refitting,
* count its capacity nevertheless if the cargo matches */
bool refittable = HasBit(e->info.refit_mask, new_cid) && (!auto_refit || HasBit(e->info.misc_flags, EF_AUTO_REFIT));
if (!refittable && v->cargo_type != new_cid) {
uint amount = e->DetermineCapacity(v, nullptr);
if (amount > 0) _returned_vehicle_capacities[v->cargo_type] += amount;
continue;
}
/* Determine best fitting subtype if requested */
if (actual_subtype == 0xFF) {
actual_subtype = GetBestFittingSubType(v, v, new_cid);
}
/* Back up the vehicle's cargo type */
CargoID temp_cid = v->cargo_type;
uint8_t temp_subtype = v->cargo_subtype;
if (refittable) {
v->cargo_type = new_cid;
v->cargo_subtype = actual_subtype;
}
uint16_t mail_capacity = 0;
uint amount = e->DetermineCapacity(v, &mail_capacity);
total_capacity += amount;
/* mail_capacity will always be zero if the vehicle is not an aircraft. */
total_mail_capacity += mail_capacity;
_returned_vehicle_capacities[new_cid] += amount;
CargoID mail = GetCargoIDByLabel(CT_MAIL);
if (IsValidCargoID(mail)) _returned_vehicle_capacities[mail] += mail_capacity;
if (!refittable) continue;
/* Restore the original cargo type */
v->cargo_type = temp_cid;
v->cargo_subtype = temp_subtype;
bool auto_refit_allowed;
CommandCost refit_cost = GetRefitCost(v, v->engine_type, new_cid, actual_subtype, &auto_refit_allowed);
if (auto_refit && (flags & DC_QUERY_COST) == 0 && !auto_refit_allowed) {
/* Sorry, auto-refitting not allowed, subtract the cargo amount again from the total.
* When querrying cost/capacity (for example in order refit GUI), we always assume 'allowed'.
* It is not predictable. */
total_capacity -= amount;
total_mail_capacity -= mail_capacity;
if (v->cargo_type == new_cid) {
/* Add the old capacity nevertheless, if the cargo matches */
total_capacity += v->cargo_cap;
if (v->type == VEH_AIRCRAFT) total_mail_capacity += v->Next()->cargo_cap;
}
continue;
}
cost.AddCost(refit_cost);
/* Record the refitting.
* Do not execute the refitting immediately, so DetermineCapacity and GetRefitCost do the same in test and exec run.
* (weird NewGRFs)
* Note:
* - If the capacity of vehicles depends on other vehicles in the chain, the actual capacity is
* set after RefitVehicle() via ConsistChanged() and friends. The estimation via _returned_refit_capacity will be wrong.
* - We have to call the refit cost callback with the pre-refit configuration of the chain because we want refit and
* autorefit to behave the same, and we need its result for auto_refit_allowed.
*/
refit_result.push_back({v, amount, mail_capacity, actual_subtype});
}
if (flags & DC_EXEC) {
/* Store the result */
for (RefitResult &result : refit_result) {
Vehicle *u = result.v;
u->refit_cap = (u->cargo_type == new_cid) ? std::min<uint16_t>(result.capacity, u->refit_cap) : 0;
if (u->cargo.TotalCount() > u->refit_cap) u->cargo.Truncate(u->cargo.TotalCount() - u->refit_cap);
u->cargo_type = new_cid;
u->cargo_cap = result.capacity;
u->cargo_subtype = result.subtype;
if (u->type == VEH_AIRCRAFT) {
Vehicle *w = u->Next();
assert(w != nullptr);
w->refit_cap = std::min<uint16_t>(w->refit_cap, result.mail_capacity);
w->cargo_cap = result.mail_capacity;
if (w->cargo.TotalCount() > w->refit_cap) w->cargo.Truncate(w->cargo.TotalCount() - w->refit_cap);
}
}
}
refit_result.clear();
_returned_refit_capacity = total_capacity;
_returned_mail_refit_capacity = total_mail_capacity;
return cost;
}
/**
* Refits a vehicle to the specified cargo type.
* @param tile unused
* @param flags type of operation
* @param p1 vehicle ID to refit
* @param p2 various bitstuffed elements
* - p2 = (bit 0-7) - New cargo type to refit to.
* - p2 = (bit 8-15) - New cargo subtype to refit to. 0xFF means to try keeping the same subtype according to GetBestFittingSubType().
* - p2 = (bit 16-23) - Number of vehicles to refit (not counting articulated parts). Zero means all vehicles.
* Only used if "refit only this vehicle" is false.
* - p2 = (bit 24) - Automatic refitting.
* - p2 = (bit 25) - Refit only this vehicle. Used only for cloning vehicles.
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
Vehicle *v = Vehicle::GetIfValid(p1);
if (v == nullptr) return CMD_ERROR;
/* Don't allow disasters and sparks and such to be refitted.
* We cannot check for IsPrimaryVehicle as autoreplace also refits in free wagon chains. */
if (!IsCompanyBuildableVehicleType(v->type)) return CMD_ERROR;
Vehicle *front = v->First();
bool auto_refit = HasBit(p2, 24);
bool is_virtual_train = v->type == VEH_TRAIN && Train::From(front)->IsVirtual();
bool free_wagon = v->type == VEH_TRAIN && Train::From(front)->IsFreeWagon(); // used by autoreplace/renew
if (is_virtual_train) {
CommandCost ret = CheckOwnership(front->owner);
if (ret.Failed()) return ret;
} else {
CommandCost ret = CheckVehicleControlAllowed(v);
if (ret.Failed()) return ret;
}
/* Don't allow shadows and such to be refitted. */
if (v != front && (v->type == VEH_AIRCRAFT)) return CMD_ERROR;
/* Allow auto-refitting only during loading and normal refitting only in a depot. */
if (!is_virtual_train) {
if ((flags & DC_QUERY_COST) == 0 && // used by the refit GUI, including the order refit GUI.
!free_wagon && // used by autoreplace/renew
(!auto_refit || !front->current_order.IsType(OT_LOADING)) && // refit inside stations
!front->IsStoppedInDepot()) { // refit inside depots
return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
}
Squashed commit of the following: commit b17f39a2016dc11a6a9815f398d690d82a6a59aa Merge: 67b3190 3bb7c47 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Feb 12 19:44:34 2016 +0100 Merge branch 'merge/trunk27506' into dev commit 3bb7c4768580198b7316bfeebc4b870d355439e8 Merge: 14929fe 9db36bd Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Feb 12 19:43:53 2016 +0100 Merge remote-tracking branch 'openttd/master' into merge/trunk27506 commit 14929fe3536e2aa5b4d6a43d0d55043da7a2f252 Merge: af15609 4b8c698 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Feb 10 22:14:25 2016 +0100 Merge branch 'master' into merge/trunk27506 commit 67b319060b4b88b72c94b0e0c2c9fdcf1c2fd95d Author: Thomas Schmidt <streen01@gmx.de> Date: Sat Feb 28 20:17:13 2015 +0100 removed 2 unused function calls commit af15609c938eb388dd507b16fb7b6d547c54c2da Merge: 5465c88 b251ba3 Author: Thomas Schmidt <streen01@gmx.de> Date: Sat Feb 28 15:12:33 2015 +0100 Merge branch 'trunk' into merge_trunk commit 5465c88c8016c5e7910570ab5795222e8348c703 Author: me <streen01@gmx.de> Date: Sat Feb 28 10:59:41 2015 +0100 regenerated MSVS project files forgot to do this, they still retained the old filenames commit 0391455e29c5ed794fcd0f58c63ff98dc52685ac Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Feb 26 16:53:05 2015 +0100 removed the patch files from this repo again that was a rather dum idea, it made the difference patch between branches trunk and tbtr huge. the patch files are now being tracked again in the supplimentary repo 'tbtr_proj', that will keep this fork clean and creating diff-patches will be much easier commit 8395d40386c8d620c90fb4be66cf6679408ac975 Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Feb 26 16:27:40 2015 +0100 fix for reported bug by DC-1: crash in station gui the template gui item was added to the drop-down list that was also shown in a station gui, but there was no action present when this item was selected in a station gui. per default the game would commit suicide by called NOT_REACHED() at the default case of the according switch-statement. commit 833873245d33bd77105a82a584d9bec2362419bc Merge: 39596be 8688c95 Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Feb 26 15:08:53 2015 +0100 Merge branch 'fix_disableTemplateOrderCheck' into tbtr commit 8688c95a01ed5933a35a08597bbf45ff148f5a67 Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Feb 26 15:06:25 2015 +0100 added fix by DC-1 don't check the orders list of a virtual vehicle commit 39596beff9a815a0f9b2ea3abe5d82c3ec5933e7 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 22 10:47:58 2015 +0100 added history of patches for the mod commit b3ae74ac4e9143202a1fda1333a91c3716ebb21e Merge: 9a601a1 ee756e1 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 22 10:03:04 2015 +0100 Merge branch 'tbtr' into merge_tbtr commit ee756e1c2229534f1cc05edb97269b0c83ddde66 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Feb 17 22:25:50 2015 +0100 removed nonsensical comments + disabled code commit e7d37f0500c56c84a36ce8b93eafb31f800e1086 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Feb 17 21:30:38 2015 +0100 added some missing renames in includes commit 63c2b13766b077e4f2923f321e95d53356dee2db Merge: e92e6ba 9752606 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Feb 17 21:22:11 2015 +0100 Merge branch 'feat_renameFiles' into merge_renameFiles Conflicts: src/tbtr_template_gui_create_virtualtrain.cpp commit 975260643d212f8cac72485f2011011210622849 Author: Thomas Schmidt <streen01@gmx.de> Date: Mon Feb 9 22:11:18 2015 +0100 replaced source file prefix: aaa -> tbtr commit e92e6ba7089564886d17dd5c1fd8d85ea0ca4ac7 Merge: 62d2f80 ac16eab Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 15:02:19 2015 +0100 Merge branch 'rm_TODOs' into dev Conflicts: src/aaa_template_gui_main.cpp commit 62d2f809edf170cfbeb0599822c4c3d4f9a1fefe Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 14:59:36 2015 +0100 i++ -> ++i commit ac16eabc082f62b9fe2ef6c11a314f8e9a28c26b Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 14:34:36 2015 +0100 rm'ed TODOs commit 22f642f32265882b8f99b409b517823991c08101 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 14:17:49 2015 +0100 rm TODO yes, depends on the selected template because the button "Start replacing" means, to start the replacement for the currently selected group and template (create a templatereplacement object for this combination) commit 60d8192838e340a3cf6899979361c997df73b716 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 14:17:26 2015 +0100 rm'ed TODO: included task in TODO-list commit 39e42674ac9f5ad5dd056b613e80ef4e754c1153 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 11:19:36 2015 +0100 changed window class in use: WC_NONE -> WC_TEMPLATE_GUI_MAIN commit cadfac96e21aeb862b75e0454197ddce89fb728c Author: Thomas Schmidt <streen01@gmx.de> Date: Mon Jan 26 23:18:29 2015 +0100 removed a weird call to deleteAllTmplReplacements was a TODO task, it was set to delete all template replacements belonging to group with id -1, which does not exist, ever commit dc1058464c29f61b6197dec556ec468d1ff38451 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 25 23:27:03 2015 +0100 removed some TODOs commit 7afeb17db512600424039099a0f4bd78882fcd8e Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 25 11:35:47 2015 +0100 removed all MYGUI comments tried to replace them with useful comments where necessary added a few new TODOs here and there commit 6b9453224a77811062254e6bce7dac4074b829a8 Merge: 292a5aa 687bc4c Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 20:47:06 2015 +0100 Merge branch 'fix_compiler_warnings' into dev commit 687bc4c34fbb9ddeaf15b4857b235a9709dd85be Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 20:43:26 2015 +0100 fixed all remaining warnings commit ada08d7097772e325b7852fd058d8bad7036ae4d Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 18:25:45 2015 +0100 removed testing code that produced a warning commit f3b1568384f36998aeb1fa51c1fab4cfb96c7f93 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 00:07:34 2015 +0100 removed unused variable REPLACEMENT_IN_PROGRESS commit 5aa9098880070cfaa3d2815f445497b2886933f9 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 00:02:43 2015 +0100 removed variable 'mode' from ClickedOnVehicle() member function of class TemplateCreateWindow in the depot gui the mode variable is used to decide whether a vehicle is started or dragged or ... here, we only drag so the mode is never used commit 292a5aa9dba9cf1d0003e84055fb95357f922454 Merge: 8f6df8c 2bb12bc Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Jan 14 23:41:29 2015 +0100 Merge branch 'feat_mergePatch0.4c' into dev commit 2bb12bcf283cccc8869bf537b79b22f479cb7203 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Jan 14 23:32:04 2015 +0100 added vi's .swp files to .gitignore commit aecf6f549b32f92342f8e0b65158bebef6270537 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Jan 13 20:15:25 2015 +0100 corrected UpdateViewport code was VehicleUpdateViewport(Vehicle*, bool) before is Vehicle::UpdateViewport(bool, bool) now commit ae199283fd5ac0199cef1c4c980561122d030199 Author: Thomas Schmidt <streen01@gmx.de> Date: Mon Jan 12 22:34:22 2015 +0100 updated code for EngineNumberSorter commit 9735035c6dd4ded9bb76958722dc25e26ced5f05 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 11 18:36:17 2015 +0100 removed unused parameter 'part_of_chain' from cargo movement code commit b8b86e1f2592288ddcfb46a0a5d81c3257da60d3 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 4 21:44:17 2015 +0100 Reimplemented moving of cargo - uses the new shift function - manages to spread the old cargo of replaced vehicles from a chain across the memebers of the newly constructed chain some TODOs are left within the code and some testing needs to be done, how this behaves when there is more than one vehicle being replaced commit 0d76e1bfe10ef207ac5e4018976e9fba0b0bb25e Author: Thomas Schmidt <streen01@gmx.de> Date: Sat Jan 3 01:05:54 2015 +0100 fixed saveload code for TemplateVehicle commit ba0ea6975f48fe38c2b5376ebc83c23d6bb6151c Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 11:32:23 2015 +0100 final changes for the merge - removed the WDF_UNLICK_BUTTON - updated ctor calls to Window() - disabled the engine number sorter commit 9cc213335046b3febfe6649fde40b00e1bb43d5b Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 11:29:03 2015 +0100 disabled cargo movement during templrpl need to reimplement this step since the cargo is now moved packet-by-packet and not as a complete list from a vehicle onto another vehicle anymore commit 39743806d0156f8547670c525af0e59083dbcd49 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 11:16:54 2015 +0100 replaced cargo function 'Count' - not available anymore: VehicleCargoList::Count() - using StoredCount() for now, should check if this is the correct count commit 9b240bbf9b2ee5659bbcb518e9e2767103861254 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 01:27:56 2015 +0100 final corrections for template_gui_create_virtualtrain commit cf0d48d8fa052ff521e1fac0ec75d75107c9b76e Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 01:20:30 2015 +0100 disbabled usage of not-anymore-existing newgrf_engine.h::ListPositionOfEngine commit 81da16b7f0c3ea2417b24707329d1d971a67e82e Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 01:09:53 2015 +0100 fixed typo in value WID_BV_SORT_ASSENDING_DESCENDING commit c8f81a5c3df5ccf4858bda64a53979af510ccd87 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 01:09:25 2015 +0100 create_virtual_train: uint GetEngineListHeight not static commit bd29d99f80bd824e28104f3bc839fc2a5abdd297 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 00:57:25 2015 +0100 template_gui_create: static WindowDesc not const commit edee9c1c544845459102328209b98d424cfd3248 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 00:44:50 2015 +0100 updated call to Window::FinishInitNested commit 25fc3cb7ed6db15f42bd3fdff9506621fbba3d72 Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Jan 1 23:56:48 2015 +0100 updated ctor calls for classes derived from Window - first param in the constructor used to be const WindowDesc*, now it is WindowDesc* commit 54d710170f1ce9cf5539cd525744ca61f4089e7b Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Jan 1 02:50:29 2015 +0100 updated constr calls to WindowDesc::WindowDesc need a const char* at 2nd pos now commit 7c954141f00666dec4c9559019a1a4af3b452372 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:30:02 2014 +0100 applied patch vehicle.cpp commit aa12720049a3dfb1c2e02d453813bd567b67ff60 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:22:54 2014 +0100 applied patch vehicle_gui.cpp (failed hunks 2,4,5/6) commit 2b8e70f15478072264f1e063418f8de0744a98e1 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:13:29 2014 +0100 applied patch train_cmd.cpp (failed hunk 1/8) commit 47499523bf1ed0cce5fdf6cc2a7102e571dcb07d Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:07:00 2014 +0100 applied patch newgrf_engine.cpp commit 7a40c62a7b5ab8059981270252a7def69eacb7d7 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:02:52 2014 +0100 applied patch vehicle_cmd.cpp (failed hunks 2,3/3) commit 277839abd8cb7eb277e4ed6cb72e0f3da5b7e479 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 01:56:35 2014 +0100 applied patch saveload.h (failed hunk 1/1) commit 7b64c87ad3dede6442a88dafa5aa8a6a3e0db812 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 01:53:56 2014 +0100 applied patch group_gui.cpp (failed hunks 2,3/4) commit 8075261c526004e21534fa0ab80429132d5f634b Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 01:46:24 2014 +0100 applied patch vehiclelist.cpp (failed hunk 1/2) commit b1c197c0f38e45fb50dad7f7e33f1438b150a34f Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 01:42:04 2014 +0100 applied patch train.h (failed hunk 1/3) commit 81bfa209e92fa74387420cc85851767d2737c1b0 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch viewport.cpp.patch (file src/viewport.cpp) commit 5c083054544eabac9260a75033198c665b169215 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch train_gui.cpp.patch (file src/train_gui.cpp) commit 3c3534621c6b37530035faadfa092d70fed724c9 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch source.list.patch (file source.list) commit 6bbb071431882d4bab43023f7194f96c824e78e5 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch saveload_internal.h.patch (file src/saveload/saveload_internal.h) commit 158640eb786cc7867c9e689eb8a92a209e528a83 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch saveload.cpp.patch (file src/saveload/saveload.cpp) commit e171ad716c126e98bc045f5ce574ac6161f3ab4f Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch openttd_vs90.vcproj.patch (file projects/openttd_vs90.vcproj) commit b77486d89c12a80f73f088759da760abd0af7f49 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch openttd_vs80.vcproj.patch (file projects/openttd_vs80.vcproj) commit 57f9c52fc580da51e20bd40f116fe66c9a0f3669 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch openttd_vs100.vcxproj.patch (file projects/openttd_vs100.vcxproj) commit bda1f739a415600a7f522b1c7f9ca53fa7713ed3 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch openttd_vs100.vcxproj.filters.patch (file projects/openttd_vs100.vcxproj.filters) commit ed96771b03e726e5cb56cac9a8328c3a1e63856b Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch newgrf_spritegroup.cpp.patch (file src/newgrf_spritegroup.cpp) commit 3df57e0d855fef5f54be4fd8d25e231a7eb3c3f1 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch group_cmd.cpp.patch (file src/group_cmd.cpp) commit da31ca4b67d6993f127c6cecac717eb286ead4e6 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch english.txt.patch (file src/lang/english.txt) commit ddc0af7139fccbae4060c70440f17c763e3bba96 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch depot_gui.cpp.patch (file src/depot_gui.cpp) commit 88aca9db192c6a2b92185f56505caf8b91d23ab4 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch autoreplace_cmd.cpp.patch (file src/autoreplace_cmd.cpp) commit 45ca80f7c9847ac3afe181a0badeb12bbbd5ed0d Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch articulated_vehicles.cpp.patch (file src/articulated_vehicles.cpp) commit 44bd0bf2e77f366b61f96b0f4ca564f2e2e5814a Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch afterload.cpp.patch (file src/saveload/afterload.cpp) commit 679f9b327f9d3f3bec327ae0266f289981972c85 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_veh_sl.cpp.patch (file src/saveload/aaa_template_veh_sl.cpp) commit ebcec221ec7c1988e85ba458283ff362e034e6d5 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_vehicle.h.patch (file src/aaa_template_vehicle.h) commit ad690e74b95d2aa07157b73834eef672c63ef901 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_vehicle_func.h.patch (file src/aaa_template_vehicle_func.h) commit 5982153c369432fc694daaa91a06dfefeeb29485 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_vehicle_func.cpp.patch (file src/aaa_template_vehicle_func.cpp) commit 773f889e165b013de96736fa380d5ab5c311b3dd Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_vehicle.cpp.patch (file src/aaa_template_vehicle.cpp) commit 03af781d69a09863d3b76ee4911e5eecd90a7cf5 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_replacement_sl.cpp.patch (file src/saveload/aaa_template_replacement_sl.cpp) commit ab6cb0562fd390d551670dbc27e0c3c94c8554db Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_gui_replaceall.cpp.patch (file src/aaa_template_gui_replaceall.cpp) commit d88452a6195c55e39bceb3ea7689fc546c4eee6a Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_gui_main.cpp.patch (file src/aaa_template_gui_main.cpp) commit ab6ac687f355d400ad9ebc154e75477671a8e0fa Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:42:21 2014 +0100 applied patch aaa_template_gui_create_virtualtrain.cpp.patch (file src/aaa_template_gui_create_virtualtrain.cpp) commit 288d14b9b145cb045b6a287d23cf3be4f2712ede Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:38:12 2014 +0100 applied patch aaa_template_gui_create.cpp.patch (file src/aaa_template_gui_create.cpp) commit 5342db70e07fb7c1f3c41654abd2c6a4c51472c4 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:18:32 2014 +0100 applied aaa_* header files commit 6f14e94a0ad715a33a2653cf6c12e1c2981ace8d Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:29:55 2014 +0100 applied vehicle_base.h.patch commit b76a5ce921fab5d81b60755ce66db71e38664e9b Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:29:28 2014 +0100 applied window_type.h.patch commit d33d738c7e3477de3f12affcc74c88194a61c442 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:28:30 2014 +0100 applied newgrf_engine.h.patch commit 931fd1143706bc76aa145e1430645cb4496f9f4a Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:27:21 2014 +0100 applied vehicle_gui.h.patch commit f6c4ab089dad5a4a01401e18cffa8f20e02f733e Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:00:52 2014 +0100 applied vehicle_gui_base.h.patch commit 5f7378136758fcc4987791d264856169950cbfe2 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 12:34:23 2014 +0100 applied build_vehicle_widget.h.patch commit 5c6fc73847a2f34573260721bb68c7b552d546bc Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 12:01:10 2014 +0100 applied autoreplace_func.h commit 7636f27011841d01e5f954c855dfa0cf1859e0e0 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 12:01:00 2014 +0100 applied newgrf.h Remove some spurious whitespace changes, update projects files.
8 years ago
}
if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
/* Check cargo */
CargoID new_cid = GB(p2, 0, 8);
uint8_t new_subtype = GB(p2, 8, 8);
if (new_cid >= NUM_CARGO) return CMD_ERROR;
/* For aircraft there is always only one. */
uint8_t num_vehicles = GB(p2, 16, 8);
bool only_this = HasBit(p2, 25) || front->type == VEH_AIRCRAFT || (front->type == VEH_SHIP && num_vehicles == 1);
CommandCost cost = RefitVehicle(v, only_this, num_vehicles, new_cid, new_subtype, flags, auto_refit);
if (is_virtual_train && !(flags & DC_QUERY_COST)) cost.MultiplyCost(0);
if (flags & DC_EXEC) {
/* Update the cached variables */
switch (v->type) {
case VEH_TRAIN:
Train::From(front)->ConsistChanged(auto_refit ? CCF_AUTOREFIT : CCF_REFIT);
break;
case VEH_ROAD:
RoadVehUpdateCache(RoadVehicle::From(front), auto_refit);
if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) RoadVehicle::From(front)->CargoChanged();
break;
case VEH_SHIP:
v->InvalidateNewGRFCacheOfChain();
Ship::From(front)->UpdateCache();
break;
case VEH_AIRCRAFT:
v->InvalidateNewGRFCacheOfChain();
UpdateAircraftCache(Aircraft::From(v), true);
break;
default: NOT_REACHED();
}
front->MarkDirty();
if (!free_wagon) {
InvalidateWindowData(WC_VEHICLE_DETAILS, front->index);
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
InvalidateWindowClassesData(WC_DEPARTURES_BOARD, 0);
}
Squashed commit of the following: commit b17f39a2016dc11a6a9815f398d690d82a6a59aa Merge: 67b3190 3bb7c47 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Feb 12 19:44:34 2016 +0100 Merge branch 'merge/trunk27506' into dev commit 3bb7c4768580198b7316bfeebc4b870d355439e8 Merge: 14929fe 9db36bd Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Feb 12 19:43:53 2016 +0100 Merge remote-tracking branch 'openttd/master' into merge/trunk27506 commit 14929fe3536e2aa5b4d6a43d0d55043da7a2f252 Merge: af15609 4b8c698 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Feb 10 22:14:25 2016 +0100 Merge branch 'master' into merge/trunk27506 commit 67b319060b4b88b72c94b0e0c2c9fdcf1c2fd95d Author: Thomas Schmidt <streen01@gmx.de> Date: Sat Feb 28 20:17:13 2015 +0100 removed 2 unused function calls commit af15609c938eb388dd507b16fb7b6d547c54c2da Merge: 5465c88 b251ba3 Author: Thomas Schmidt <streen01@gmx.de> Date: Sat Feb 28 15:12:33 2015 +0100 Merge branch 'trunk' into merge_trunk commit 5465c88c8016c5e7910570ab5795222e8348c703 Author: me <streen01@gmx.de> Date: Sat Feb 28 10:59:41 2015 +0100 regenerated MSVS project files forgot to do this, they still retained the old filenames commit 0391455e29c5ed794fcd0f58c63ff98dc52685ac Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Feb 26 16:53:05 2015 +0100 removed the patch files from this repo again that was a rather dum idea, it made the difference patch between branches trunk and tbtr huge. the patch files are now being tracked again in the supplimentary repo 'tbtr_proj', that will keep this fork clean and creating diff-patches will be much easier commit 8395d40386c8d620c90fb4be66cf6679408ac975 Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Feb 26 16:27:40 2015 +0100 fix for reported bug by DC-1: crash in station gui the template gui item was added to the drop-down list that was also shown in a station gui, but there was no action present when this item was selected in a station gui. per default the game would commit suicide by called NOT_REACHED() at the default case of the according switch-statement. commit 833873245d33bd77105a82a584d9bec2362419bc Merge: 39596be 8688c95 Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Feb 26 15:08:53 2015 +0100 Merge branch 'fix_disableTemplateOrderCheck' into tbtr commit 8688c95a01ed5933a35a08597bbf45ff148f5a67 Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Feb 26 15:06:25 2015 +0100 added fix by DC-1 don't check the orders list of a virtual vehicle commit 39596beff9a815a0f9b2ea3abe5d82c3ec5933e7 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 22 10:47:58 2015 +0100 added history of patches for the mod commit b3ae74ac4e9143202a1fda1333a91c3716ebb21e Merge: 9a601a1 ee756e1 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 22 10:03:04 2015 +0100 Merge branch 'tbtr' into merge_tbtr commit ee756e1c2229534f1cc05edb97269b0c83ddde66 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Feb 17 22:25:50 2015 +0100 removed nonsensical comments + disabled code commit e7d37f0500c56c84a36ce8b93eafb31f800e1086 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Feb 17 21:30:38 2015 +0100 added some missing renames in includes commit 63c2b13766b077e4f2923f321e95d53356dee2db Merge: e92e6ba 9752606 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Feb 17 21:22:11 2015 +0100 Merge branch 'feat_renameFiles' into merge_renameFiles Conflicts: src/tbtr_template_gui_create_virtualtrain.cpp commit 975260643d212f8cac72485f2011011210622849 Author: Thomas Schmidt <streen01@gmx.de> Date: Mon Feb 9 22:11:18 2015 +0100 replaced source file prefix: aaa -> tbtr commit e92e6ba7089564886d17dd5c1fd8d85ea0ca4ac7 Merge: 62d2f80 ac16eab Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 15:02:19 2015 +0100 Merge branch 'rm_TODOs' into dev Conflicts: src/aaa_template_gui_main.cpp commit 62d2f809edf170cfbeb0599822c4c3d4f9a1fefe Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 14:59:36 2015 +0100 i++ -> ++i commit ac16eabc082f62b9fe2ef6c11a314f8e9a28c26b Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 14:34:36 2015 +0100 rm'ed TODOs commit 22f642f32265882b8f99b409b517823991c08101 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 14:17:49 2015 +0100 rm TODO yes, depends on the selected template because the button "Start replacing" means, to start the replacement for the currently selected group and template (create a templatereplacement object for this combination) commit 60d8192838e340a3cf6899979361c997df73b716 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 14:17:26 2015 +0100 rm'ed TODO: included task in TODO-list commit 39e42674ac9f5ad5dd056b613e80ef4e754c1153 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Feb 8 11:19:36 2015 +0100 changed window class in use: WC_NONE -> WC_TEMPLATE_GUI_MAIN commit cadfac96e21aeb862b75e0454197ddce89fb728c Author: Thomas Schmidt <streen01@gmx.de> Date: Mon Jan 26 23:18:29 2015 +0100 removed a weird call to deleteAllTmplReplacements was a TODO task, it was set to delete all template replacements belonging to group with id -1, which does not exist, ever commit dc1058464c29f61b6197dec556ec468d1ff38451 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 25 23:27:03 2015 +0100 removed some TODOs commit 7afeb17db512600424039099a0f4bd78882fcd8e Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 25 11:35:47 2015 +0100 removed all MYGUI comments tried to replace them with useful comments where necessary added a few new TODOs here and there commit 6b9453224a77811062254e6bce7dac4074b829a8 Merge: 292a5aa 687bc4c Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 20:47:06 2015 +0100 Merge branch 'fix_compiler_warnings' into dev commit 687bc4c34fbb9ddeaf15b4857b235a9709dd85be Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 20:43:26 2015 +0100 fixed all remaining warnings commit ada08d7097772e325b7852fd058d8bad7036ae4d Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 18:25:45 2015 +0100 removed testing code that produced a warning commit f3b1568384f36998aeb1fa51c1fab4cfb96c7f93 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 00:07:34 2015 +0100 removed unused variable REPLACEMENT_IN_PROGRESS commit 5aa9098880070cfaa3d2815f445497b2886933f9 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 18 00:02:43 2015 +0100 removed variable 'mode' from ClickedOnVehicle() member function of class TemplateCreateWindow in the depot gui the mode variable is used to decide whether a vehicle is started or dragged or ... here, we only drag so the mode is never used commit 292a5aa9dba9cf1d0003e84055fb95357f922454 Merge: 8f6df8c 2bb12bc Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Jan 14 23:41:29 2015 +0100 Merge branch 'feat_mergePatch0.4c' into dev commit 2bb12bcf283cccc8869bf537b79b22f479cb7203 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Jan 14 23:32:04 2015 +0100 added vi's .swp files to .gitignore commit aecf6f549b32f92342f8e0b65158bebef6270537 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Jan 13 20:15:25 2015 +0100 corrected UpdateViewport code was VehicleUpdateViewport(Vehicle*, bool) before is Vehicle::UpdateViewport(bool, bool) now commit ae199283fd5ac0199cef1c4c980561122d030199 Author: Thomas Schmidt <streen01@gmx.de> Date: Mon Jan 12 22:34:22 2015 +0100 updated code for EngineNumberSorter commit 9735035c6dd4ded9bb76958722dc25e26ced5f05 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 11 18:36:17 2015 +0100 removed unused parameter 'part_of_chain' from cargo movement code commit b8b86e1f2592288ddcfb46a0a5d81c3257da60d3 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Jan 4 21:44:17 2015 +0100 Reimplemented moving of cargo - uses the new shift function - manages to spread the old cargo of replaced vehicles from a chain across the memebers of the newly constructed chain some TODOs are left within the code and some testing needs to be done, how this behaves when there is more than one vehicle being replaced commit 0d76e1bfe10ef207ac5e4018976e9fba0b0bb25e Author: Thomas Schmidt <streen01@gmx.de> Date: Sat Jan 3 01:05:54 2015 +0100 fixed saveload code for TemplateVehicle commit ba0ea6975f48fe38c2b5376ebc83c23d6bb6151c Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 11:32:23 2015 +0100 final changes for the merge - removed the WDF_UNLICK_BUTTON - updated ctor calls to Window() - disabled the engine number sorter commit 9cc213335046b3febfe6649fde40b00e1bb43d5b Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 11:29:03 2015 +0100 disabled cargo movement during templrpl need to reimplement this step since the cargo is now moved packet-by-packet and not as a complete list from a vehicle onto another vehicle anymore commit 39743806d0156f8547670c525af0e59083dbcd49 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 11:16:54 2015 +0100 replaced cargo function 'Count' - not available anymore: VehicleCargoList::Count() - using StoredCount() for now, should check if this is the correct count commit 9b240bbf9b2ee5659bbcb518e9e2767103861254 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 01:27:56 2015 +0100 final corrections for template_gui_create_virtualtrain commit cf0d48d8fa052ff521e1fac0ec75d75107c9b76e Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 01:20:30 2015 +0100 disbabled usage of not-anymore-existing newgrf_engine.h::ListPositionOfEngine commit 81da16b7f0c3ea2417b24707329d1d971a67e82e Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 01:09:53 2015 +0100 fixed typo in value WID_BV_SORT_ASSENDING_DESCENDING commit c8f81a5c3df5ccf4858bda64a53979af510ccd87 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 01:09:25 2015 +0100 create_virtual_train: uint GetEngineListHeight not static commit bd29d99f80bd824e28104f3bc839fc2a5abdd297 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 00:57:25 2015 +0100 template_gui_create: static WindowDesc not const commit edee9c1c544845459102328209b98d424cfd3248 Author: Thomas Schmidt <streen01@gmx.de> Date: Fri Jan 2 00:44:50 2015 +0100 updated call to Window::FinishInitNested commit 25fc3cb7ed6db15f42bd3fdff9506621fbba3d72 Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Jan 1 23:56:48 2015 +0100 updated ctor calls for classes derived from Window - first param in the constructor used to be const WindowDesc*, now it is WindowDesc* commit 54d710170f1ce9cf5539cd525744ca61f4089e7b Author: Thomas Schmidt <streen01@gmx.de> Date: Thu Jan 1 02:50:29 2015 +0100 updated constr calls to WindowDesc::WindowDesc need a const char* at 2nd pos now commit 7c954141f00666dec4c9559019a1a4af3b452372 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:30:02 2014 +0100 applied patch vehicle.cpp commit aa12720049a3dfb1c2e02d453813bd567b67ff60 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:22:54 2014 +0100 applied patch vehicle_gui.cpp (failed hunks 2,4,5/6) commit 2b8e70f15478072264f1e063418f8de0744a98e1 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:13:29 2014 +0100 applied patch train_cmd.cpp (failed hunk 1/8) commit 47499523bf1ed0cce5fdf6cc2a7102e571dcb07d Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:07:00 2014 +0100 applied patch newgrf_engine.cpp commit 7a40c62a7b5ab8059981270252a7def69eacb7d7 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 02:02:52 2014 +0100 applied patch vehicle_cmd.cpp (failed hunks 2,3/3) commit 277839abd8cb7eb277e4ed6cb72e0f3da5b7e479 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 01:56:35 2014 +0100 applied patch saveload.h (failed hunk 1/1) commit 7b64c87ad3dede6442a88dafa5aa8a6a3e0db812 Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 01:53:56 2014 +0100 applied patch group_gui.cpp (failed hunks 2,3/4) commit 8075261c526004e21534fa0ab80429132d5f634b Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 01:46:24 2014 +0100 applied patch vehiclelist.cpp (failed hunk 1/2) commit b1c197c0f38e45fb50dad7f7e33f1438b150a34f Author: Thomas Schmidt <streen01@gmx.de> Date: Wed Dec 31 01:42:04 2014 +0100 applied patch train.h (failed hunk 1/3) commit 81bfa209e92fa74387420cc85851767d2737c1b0 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch viewport.cpp.patch (file src/viewport.cpp) commit 5c083054544eabac9260a75033198c665b169215 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch train_gui.cpp.patch (file src/train_gui.cpp) commit 3c3534621c6b37530035faadfa092d70fed724c9 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch source.list.patch (file source.list) commit 6bbb071431882d4bab43023f7194f96c824e78e5 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch saveload_internal.h.patch (file src/saveload/saveload_internal.h) commit 158640eb786cc7867c9e689eb8a92a209e528a83 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch saveload.cpp.patch (file src/saveload/saveload.cpp) commit e171ad716c126e98bc045f5ce574ac6161f3ab4f Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch openttd_vs90.vcproj.patch (file projects/openttd_vs90.vcproj) commit b77486d89c12a80f73f088759da760abd0af7f49 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch openttd_vs80.vcproj.patch (file projects/openttd_vs80.vcproj) commit 57f9c52fc580da51e20bd40f116fe66c9a0f3669 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch openttd_vs100.vcxproj.patch (file projects/openttd_vs100.vcxproj) commit bda1f739a415600a7f522b1c7f9ca53fa7713ed3 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch openttd_vs100.vcxproj.filters.patch (file projects/openttd_vs100.vcxproj.filters) commit ed96771b03e726e5cb56cac9a8328c3a1e63856b Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch newgrf_spritegroup.cpp.patch (file src/newgrf_spritegroup.cpp) commit 3df57e0d855fef5f54be4fd8d25e231a7eb3c3f1 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:10 2014 +0100 applied patch group_cmd.cpp.patch (file src/group_cmd.cpp) commit da31ca4b67d6993f127c6cecac717eb286ead4e6 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch english.txt.patch (file src/lang/english.txt) commit ddc0af7139fccbae4060c70440f17c763e3bba96 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch depot_gui.cpp.patch (file src/depot_gui.cpp) commit 88aca9db192c6a2b92185f56505caf8b91d23ab4 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch autoreplace_cmd.cpp.patch (file src/autoreplace_cmd.cpp) commit 45ca80f7c9847ac3afe181a0badeb12bbbd5ed0d Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch articulated_vehicles.cpp.patch (file src/articulated_vehicles.cpp) commit 44bd0bf2e77f366b61f96b0f4ca564f2e2e5814a Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch afterload.cpp.patch (file src/saveload/afterload.cpp) commit 679f9b327f9d3f3bec327ae0266f289981972c85 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_veh_sl.cpp.patch (file src/saveload/aaa_template_veh_sl.cpp) commit ebcec221ec7c1988e85ba458283ff362e034e6d5 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_vehicle.h.patch (file src/aaa_template_vehicle.h) commit ad690e74b95d2aa07157b73834eef672c63ef901 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_vehicle_func.h.patch (file src/aaa_template_vehicle_func.h) commit 5982153c369432fc694daaa91a06dfefeeb29485 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_vehicle_func.cpp.patch (file src/aaa_template_vehicle_func.cpp) commit 773f889e165b013de96736fa380d5ab5c311b3dd Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_vehicle.cpp.patch (file src/aaa_template_vehicle.cpp) commit 03af781d69a09863d3b76ee4911e5eecd90a7cf5 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_replacement_sl.cpp.patch (file src/saveload/aaa_template_replacement_sl.cpp) commit ab6cb0562fd390d551670dbc27e0c3c94c8554db Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_gui_replaceall.cpp.patch (file src/aaa_template_gui_replaceall.cpp) commit d88452a6195c55e39bceb3ea7689fc546c4eee6a Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:43:09 2014 +0100 applied patch aaa_template_gui_main.cpp.patch (file src/aaa_template_gui_main.cpp) commit ab6ac687f355d400ad9ebc154e75477671a8e0fa Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:42:21 2014 +0100 applied patch aaa_template_gui_create_virtualtrain.cpp.patch (file src/aaa_template_gui_create_virtualtrain.cpp) commit 288d14b9b145cb045b6a287d23cf3be4f2712ede Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:38:12 2014 +0100 applied patch aaa_template_gui_create.cpp.patch (file src/aaa_template_gui_create.cpp) commit 5342db70e07fb7c1f3c41654abd2c6a4c51472c4 Author: Thomas Schmidt <streen01@gmx.de> Date: Tue Dec 30 01:18:32 2014 +0100 applied aaa_* header files commit 6f14e94a0ad715a33a2653cf6c12e1c2981ace8d Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:29:55 2014 +0100 applied vehicle_base.h.patch commit b76a5ce921fab5d81b60755ce66db71e38664e9b Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:29:28 2014 +0100 applied window_type.h.patch commit d33d738c7e3477de3f12affcc74c88194a61c442 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:28:30 2014 +0100 applied newgrf_engine.h.patch commit 931fd1143706bc76aa145e1430645cb4496f9f4a Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:27:21 2014 +0100 applied vehicle_gui.h.patch commit f6c4ab089dad5a4a01401e18cffa8f20e02f733e Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 17:00:52 2014 +0100 applied vehicle_gui_base.h.patch commit 5f7378136758fcc4987791d264856169950cbfe2 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 12:34:23 2014 +0100 applied build_vehicle_widget.h.patch commit 5c6fc73847a2f34573260721bb68c7b552d546bc Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 12:01:10 2014 +0100 applied autoreplace_func.h commit 7636f27011841d01e5f954c855dfa0cf1859e0e0 Author: Thomas Schmidt <streen01@gmx.de> Date: Sun Dec 28 12:01:00 2014 +0100 applied newgrf.h Remove some spurious whitespace changes, update projects files.
8 years ago
/* virtual vehicles get their cargo changed by the TemplateCreateWindow, so set this dirty instead of a depot window */
if (HasBit(front->subtype, GVSF_VIRTUAL)) {
SetWindowClassesDirty(WC_CREATE_TEMPLATE);
} else {
SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
}
} else {
/* Always invalidate the cache; querycost might have filled it. */
v->InvalidateNewGRFCacheOfChain();
}
return cost;
}
/**
* Start/Stop a vehicle
* @param tile unused
* @param flags type of operation
* @param p1 vehicle to start/stop, don't forget to change CcStartStopVehicle if you modify this!
* @param p2 bit 0: Shall the start/stop newgrf callback be evaluated (only valid with DC_AUTOREPLACE for network safety)
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
/* Disable the effect of p2 bit 0, when DC_AUTOREPLACE is not set */
if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
Vehicle *v = Vehicle::GetIfValid(p1);
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
CommandCost ret = CheckVehicleControlAllowed(v);
if (ret.Failed()) return ret;
if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
switch (v->type) {
case VEH_TRAIN:
if ((v->vehstatus & VS_STOPPED) && Train::From(v)->gcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_POWER);
break;
case VEH_SHIP:
case VEH_ROAD:
break;
case VEH_AIRCRAFT: {
Aircraft *a = Aircraft::From(v);
/* cannot stop airplane when in flight, or when taking off / landing */
if (a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
if (HasBit(a->flags, VAF_HELI_DIRECT_DESCENT)) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
break;
}
default: return CMD_ERROR;
}
if (HasBit(p2, 0)) {
/* Check if this vehicle can be started/stopped. Failure means 'allow'. */
uint16_t callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
StringID error = STR_NULL;
if (callback != CALLBACK_FAILED) {
if (v->GetGRF()->grf_version < 8) {
/* 8 bit result 0xFF means 'allow' */
if (callback < 0x400 && GB(callback, 0, 8) != 0xFF) error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
} else {
if (callback < 0x400) {
error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
} else {
switch (callback) {
case 0x400: // allow
break;
default: // unknown reason -> disallow
error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
break;
}
}
}
}
if (error != STR_NULL) return_cmd_error(error);
}
if (flags & DC_EXEC) {
if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
v->ClearSeparation();
if (HasBit(v->vehicle_flags, VF_TIMETABLE_SEPARATION)) ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
v->vehstatus ^= VS_STOPPED;
if (v->type == VEH_ROAD) {
if (!RoadVehicle::From(v)->IsRoadVehicleOnLevelCrossing()) v->cur_speed = 0;
} else if (v->type != VEH_TRAIN) {
v->cur_speed = 0; // trains can stop 'slowly'
}
if (v->type == VEH_TRAIN && !(v->vehstatus & VS_STOPPED) && v->cur_speed == 0 && Train::From(v)->lookahead != nullptr) {
/* Starting train from stationary with a lookahead, refresh it */
Train::From(v)->lookahead.reset();
FillTrainReservationLookAhead(Train::From(v));
}
/* Unbunching data is no longer valid. */
v->ResetDepotUnbunching();
v->MarkDirty();
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
DirtyVehicleListWindowForVehicle(v);
InvalidateWindowData(WC_VEHICLE_VIEW, v->index);
}
return CommandCost();
}
/**
* Starts or stops a lot of vehicles
* @param tile Tile of the depot where the vehicles are started/stopped (only used for depots)
* @param flags type of operation
* @param p1 bitmask
* - bit 0 set = start vehicles, unset = stop vehicles
* - bit 1 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case
* - bit 8-15 Cargo filter
* @param p2 packed VehicleListIdentifier
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
VehicleList list;
bool do_start = HasBit(p1, 0);
bool vehicle_list_window = HasBit(p1, 1);
VehicleListIdentifier vli;
if (!vli.UnpackIfValid(p2)) return CMD_ERROR;
if (!IsCompanyBuildableVehicleType(vli.vtype)) return CMD_ERROR;
if (vehicle_list_window) {
if (!GenerateVehicleSortList(&list, vli, GB(p1, 8, 8))) return CMD_ERROR;
} else {
if (!IsDepotTile(tile)) return CMD_ERROR;
/* Get the list of vehicles in the depot */
BuildDepotVehicleList(vli.vtype, tile, &list, nullptr);
}
for (const Vehicle *v : list) {
if (!!(v->vehstatus & VS_STOPPED) != do_start) continue;
if (!vehicle_list_window && !v->IsChainInDepot()) continue;
/* Just try and don't care if some vehicle's can't be stopped. */
DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
}
return CommandCost();
}
/**
* Sells all vehicles in a depot
* @param tile Tile of the depot where the depot is
* @param flags type of operation
* @param p1 Vehicle type
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
VehicleList list;
CommandCost cost(EXPENSES_NEW_VEHICLES);
VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
uint sell_command = GetCmdSellVeh(vehicle_type);
/* Get the list of vehicles in the depot */
BuildDepotVehicleList(vehicle_type, tile, &list, &list);
CommandCost last_error = CMD_ERROR;
bool had_success = false;
for (const Vehicle *v : list) {
CommandCost ret = DoCommand(tile, v->index | (1 << 20), 0, flags, sell_command);
if (ret.Succeeded()) {
cost.AddCost(ret);
had_success = true;
} else {
last_error = ret;
}
}
return had_success ? cost : last_error;
}
/**
* Autoreplace all vehicles in the depot
* @param tile Tile of the depot where the vehicles are
* @param flags type of operation
* @param p1 Type of vehicle
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
VehicleList list;
CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
VehicleType vehicle_type = Extract<VehicleType, 0, 3>(p1);
if (!IsCompanyBuildableVehicleType(vehicle_type)) return CMD_ERROR;
if (!IsDepotTile(tile) || !IsInfraUsageAllowed(vehicle_type, _current_company, GetTileOwner(tile))) return CMD_ERROR;
/* Get the list of vehicles in the depot */
BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
for (const Vehicle *v : list) {
/* Ensure that the vehicle completely in the depot */
if (!v->IsChainInDepot()) continue;
if (v->type == VEH_TRAIN) {
_new_vehicle_id = INVALID_VEHICLE;
CommandCost ret = DoCommand(v->tile, v->index, 0, flags, CMD_TEMPLATE_REPLACE_VEHICLE);
if (ret.Succeeded()) cost.AddCost(ret);
if (_new_vehicle_id != INVALID_VEHICLE) {
v = Vehicle::Get(_new_vehicle_id);
}
}
CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
if (ret.Succeeded()) cost.AddCost(ret);
}
return cost;
}
/**
* Test if a name is unique among vehicle names.
* @param name Name to test.
* @return True if the name is unique.
*/
bool IsUniqueVehicleName(const char *name)
{
for (const Vehicle *v : Vehicle::Iterate()) {
if (!v->name.empty() && v->name == name) return false;
}
return true;
}
/**
* Clone the custom name of a vehicle, adding or incrementing a number.
* @param src Source vehicle, with a custom name.
* @param dst Destination vehicle.
*/
static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
{
std::string new_name = src->name.c_str();
if (!std::isdigit(*new_name.rbegin())) {
// No digit at the end, so start at number 1 (this will get incremented to 2)
new_name += " 1";
}
int max_iterations = 1000;
do {
size_t pos = new_name.length() - 1;
// Handle any carrying
for (; pos != std::string::npos && new_name[pos] == '9'; --pos) {
new_name[pos] = '0';
}
if (pos != std::string::npos && std::isdigit(new_name[pos])) {
++new_name[pos];
} else {
new_name[++pos] = '1';
new_name.push_back('0');
}
--max_iterations;
} while(max_iterations > 0 && !IsUniqueVehicleName(new_name.c_str()));
if (max_iterations > 0) {
dst->name = new_name;
}
/* All done. If we didn't find a name, it'll just use its default. */
}
/**
* Toggles 'reuse depot vehicles' on a template vehicle.
* @param tile unused
* @param flags type of operation
* @param p1 the template vehicle's index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdToggleReuseDepotVehicles(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
// Identify template to toggle
TemplateVehicle *template_vehicle = TemplateVehicle::GetIfValid(p1);
if (template_vehicle == nullptr) return CMD_ERROR;
CommandCost ret = CheckOwnership(template_vehicle->owner);
if (ret.Failed()) return ret;
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
template_vehicle->ToggleReuseDepotVehicles();
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Toggles 'keep remaining vehicles' on a template vehicle.
* @param tile unused
* @param flags type of operation
* @param p1 the template vehicle's index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdToggleKeepRemainingVehicles(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
// Identify template to toggle
TemplateVehicle *template_vehicle = TemplateVehicle::GetIfValid(p1);
if (template_vehicle == nullptr) return CMD_ERROR;
CommandCost ret = CheckOwnership(template_vehicle->owner);
if (ret.Failed()) return ret;
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
template_vehicle->ToggleKeepRemainingVehicles();
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Set/unset 'refit as template' on a template vehicle.
* @param tile unused
* @param flags type of operation
* @param p1 the template vehicle's index
* @param p2 whether 'refit as template' should be set
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdSetRefitAsTemplate(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
// Identify template to toggle
TemplateVehicle *template_vehicle = TemplateVehicle::GetIfValid(p1);
if (template_vehicle == nullptr) return CMD_ERROR;
CommandCost ret = CheckOwnership(template_vehicle->owner);
if (ret.Failed()) return ret;
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
template_vehicle->SetRefitAsTemplate(p2 != 0);
MarkTrainsUsingTemplateAsPendingTemplateReplacement(template_vehicle);
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Toggles replace old only on a template vehicle.
* @param tile unused
* @param flags type of operation
* @param p1 the template vehicle's index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdToggleTemplateReplaceOldOnly(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
// Identify template to toggle
TemplateVehicle *template_vehicle = TemplateVehicle::GetIfValid(p1);
if (template_vehicle == nullptr) return CMD_ERROR;
CommandCost ret = CheckOwnership(template_vehicle->owner);
if (ret.Failed()) return ret;
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
template_vehicle->ToggleReplaceOldOnly();
MarkTrainsUsingTemplateAsPendingTemplateReplacement(template_vehicle);
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Rename a template vehicle.
* @param tile unused
* @param flags type of operation
* @param p1 the template vehicle's index
* @param p2 unused
* @param text new name
* @return the cost of this operation or an error
*/
CommandCost CmdRenameTemplateReplace(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
TemplateVehicle *template_vehicle = TemplateVehicle::GetIfValid(p1);
if (template_vehicle == nullptr) return CMD_ERROR;
CommandCost ret = CheckOwnership(template_vehicle->owner);
if (ret.Failed()) return ret;
bool reset = StrEmpty(text);
if (!reset) {
if (Utf8StringLength(text) >= MAX_LENGTH_GROUP_NAME_CHARS) return CMD_ERROR;
}
if (flags & DC_EXEC) {
/* Assign the new one */
if (reset) {
template_vehicle->name.clear();
} else {
template_vehicle->name = text;
}
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Create a virtual train from a template vehicle.
* @param tile unused
* @param flags type of operation
* @param p1 the original vehicle's index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdVirtualTrainFromTemplateVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
VehicleID template_vehicle_id = p1;
TemplateVehicle* tv = TemplateVehicle::GetIfValid(template_vehicle_id);
if (tv == nullptr) return CMD_ERROR;
CommandCost ret = CheckOwnership(tv->owner);
if (ret.Failed()) return ret;
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
StringID err = INVALID_STRING_ID;
Train* train = VirtualTrainFromTemplateVehicle(tv, err, p2);
if (train == nullptr) {
return_cmd_error(err);
}
}
return CommandCost();
}
CommandCost CmdDeleteVirtualTrain(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text);
template <typename T>
void UpdateNewVirtualTrainFromSource(Train *v, const T *src)
{
struct helper {
static bool IsTrainPartReversed(const Train *src) { return HasBit(src->flags, VRF_REVERSE_DIRECTION); }
static bool IsTrainPartReversed(const TemplateVehicle *src) { return HasBit(src->ctrl_flags, TVCF_REVERSED); }
static const Train *GetTrainMultiheadOtherPart(const Train *src) { return src->other_multiheaded_part; }
static const TemplateVehicle *GetTrainMultiheadOtherPart(const TemplateVehicle *src) { return src; }
};
SB(v->flags, VRF_REVERSE_DIRECTION, 1, helper::IsTrainPartReversed(src) ? 1 : 0);
if (v->IsMultiheaded()) {
const T *other = helper::GetTrainMultiheadOtherPart(src);
/* For template vehicles, just use the front part, fix any discrepancy later */
v->other_multiheaded_part->cargo_type = other->cargo_type;
v->other_multiheaded_part->cargo_subtype = other->cargo_subtype;
}
while (true) {
v->cargo_type = src->cargo_type;
v->cargo_subtype = src->cargo_subtype;
if (v->HasArticulatedPart()) {
v = v->Next();
} else {
break;
}
if (src->HasArticulatedPart()) {
src = src->Next();
} else {
break;
}
}
v->First()->ConsistChanged(CCF_ARRANGE);
InvalidateVehicleTickCaches();
}
Train* VirtualTrainFromTemplateVehicle(const TemplateVehicle* tv, StringID &err, uint32_t user)
{
CommandCost c;
Train *tmp, *head, *tail;
const TemplateVehicle* tv_head = tv;
assert(tv->owner == _current_company);
head = BuildVirtualRailVehicle(tv->engine_type, err, user, true);
if (!head) return nullptr;
UpdateNewVirtualTrainFromSource(head, tv);
tail = head;
tv = tv->GetNextUnit();
while (tv) {
tmp = BuildVirtualRailVehicle(tv->engine_type, err, user, true);
if (!tmp) {
CmdDeleteVirtualTrain(INVALID_TILE, DC_EXEC, head->index, 0, nullptr);
return nullptr;
}
UpdateNewVirtualTrainFromSource(tmp, tv);
CmdMoveRailVehicle(INVALID_TILE, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0);
tail = tmp;
tv = tv->GetNextUnit();
}
for (tv = tv_head, tmp = head; tv != nullptr && tmp != nullptr; tv = tv->Next(), tmp = tmp->Next()) {
tmp->cargo_type = tv->cargo_type;
tmp->cargo_subtype = tv->cargo_subtype;
}
_new_vehicle_id = head->index;
return head;
}
/**
* Create a virtual train from a regular train.
* @param tile unused
* @param flags type of operation
* @param p1 the train index
* @param p2 user
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdVirtualTrainFromTrain(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
VehicleID vehicle_id = p1;
Vehicle* vehicle = Vehicle::GetIfValid(vehicle_id);
if (vehicle == nullptr || vehicle->type != VEH_TRAIN) {
return CMD_ERROR;
}
Train* train = Train::From(vehicle);
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
CommandCost c;
Train *tmp, *head, *tail;
StringID err = INVALID_STRING_ID;
head = BuildVirtualRailVehicle(train->engine_type, err, p2, true);
if (!head) return_cmd_error(err);
UpdateNewVirtualTrainFromSource(head, train);
tail = head;
train = train->GetNextUnit();
while (train) {
tmp = BuildVirtualRailVehicle(train->engine_type, err, p2, true);
if (!tmp) {
CmdDeleteVirtualTrain(tile, flags, head->index, 0, nullptr);
return_cmd_error(err);
}
UpdateNewVirtualTrainFromSource(tmp, train);
CmdMoveRailVehicle(0, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0);
tail = tmp;
train = train->GetNextUnit();
}
_new_vehicle_id = head->index;
}
return CommandCost();
}
/**
* Delete a virtual train
* @param tile unused
* @param flags type of operation
* @param p1 the vehicle's index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdDeleteVirtualTrain(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
VehicleID vehicle_id = p1;
Vehicle* vehicle = Vehicle::GetIfValid(vehicle_id);
if (vehicle == nullptr || vehicle->type != VEH_TRAIN) {
return CMD_ERROR;
}
CommandCost ret = CheckOwnership(vehicle->owner);
if (ret.Failed()) return ret;
vehicle = vehicle->First();
Train* train = Train::From(vehicle);
if (!train->IsVirtual()) {
return CMD_ERROR;
}
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
delete train;
}
return CommandCost();
}
/**
* Replace a template vehicle with another one based on a virtual train.
* @param tile unused
* @param flags type of operation
* @param p1 the template vehicle's index
* @param p2 the virtual train's index
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdReplaceTemplateVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
VehicleID template_vehicle_id = p1;
VehicleID virtual_train_id = p2;
TemplateVehicle *template_vehicle = TemplateVehicle::GetIfValid(template_vehicle_id);
Vehicle *vehicle = Vehicle::GetIfValid(virtual_train_id);
if (vehicle == nullptr || vehicle->type != VEH_TRAIN) {
return CMD_ERROR;
}
CommandCost ret = CheckOwnership(vehicle->owner);
if (ret.Failed()) return ret;
if (template_vehicle != nullptr) {
ret = CheckOwnership(template_vehicle->owner);
if (ret.Failed()) return ret;
}
vehicle = vehicle->First();
Train* train = Train::From(vehicle);
if (!train->IsVirtual()) {
return CMD_ERROR;
}
if (!TemplateVehicle::CanAllocateItem(CountVehiclesInChain(train))) {
return CMD_ERROR;
}
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
VehicleID old_ID = INVALID_VEHICLE;
bool restore_flags = false;
bool reuse_depot_vehicles = false;
bool keep_remaining_vehicles = false;
bool refit_as_template = true;
bool replace_old_only = false;
std::string name;
if (template_vehicle != nullptr) {
old_ID = template_vehicle->index;
restore_flags = true;
reuse_depot_vehicles = template_vehicle->reuse_depot_vehicles;
keep_remaining_vehicles = template_vehicle->keep_remaining_vehicles;
refit_as_template = template_vehicle->refit_as_template;
replace_old_only = template_vehicle->replace_old_only;
name = std::move(template_vehicle->name);
delete template_vehicle;
template_vehicle = nullptr;
}
template_vehicle = TemplateVehicleFromVirtualTrain(train);
if (restore_flags) {
template_vehicle->reuse_depot_vehicles = reuse_depot_vehicles;
template_vehicle->keep_remaining_vehicles = keep_remaining_vehicles;
template_vehicle->refit_as_template = refit_as_template;
template_vehicle->replace_old_only = replace_old_only;
template_vehicle->name = std::move(name);
}
// Make sure our replacements still point to the correct thing.
if (old_ID != INVALID_VEHICLE && old_ID != template_vehicle->index) {
bool reindex = false;
for (TemplateReplacement *tr : TemplateReplacement::Iterate()) {
if (tr->GetTemplateVehicleID() == old_ID) {
tr->SetTemplate(template_vehicle->index);
reindex = true;
}
}
if (reindex) {
ReindexTemplateReplacements();
MarkTrainsUsingTemplateAsPendingTemplateReplacement(template_vehicle);
}
} else if (template_vehicle->NumGroupsUsingTemplate() > 0) {
MarkTrainsUsingTemplateAsPendingTemplateReplacement(template_vehicle);
}
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Clone a vehicle to create a template vehicle.
* @param tile unused
* @param flags type of operation
* @param p1 the original vehicle's index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdTemplateVehicleFromTrain(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
// create a new template from the clicked vehicle
TemplateVehicle *tv;
Vehicle *t = Vehicle::GetIfValid(p1);
Train *clicked = Train::GetIfValid(t->index);
if (!clicked) return CMD_ERROR;
Train *init_clicked = clicked;
int len = CountVehiclesInChain(clicked);
if (!TemplateVehicle::CanAllocateItem(len)) {
return CMD_ERROR;
}
for (Train *v = clicked; v != nullptr; v = v->GetNextUnit()) {
const Engine *e = Engine::GetIfValid(v->engine_type);
if (e == nullptr || e->type != VEH_TRAIN) {
return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + VEH_TRAIN);
}
}
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
TemplateVehicle *tmp = nullptr;
TemplateVehicle *prev = nullptr;
for (; clicked != nullptr; clicked = clicked->Next()) {
tmp = new TemplateVehicle(clicked->engine_type);
SetupTemplateVehicleFromVirtual(tmp, prev, clicked);
prev = tmp;
}
tmp->First()->SetRealLength(CeilDiv(init_clicked->gcache.cached_total_length * 10, TILE_SIZE));
tv = tmp->First();
if (!tv) return CMD_ERROR;
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Delete a template vehicle.
* @param tile unused
* @param flags type of operation
* @param p1 the template vehicle's index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdDeleteTemplateVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
// Identify template to delete
TemplateVehicle *del = TemplateVehicle::GetIfValid(p1);
if (del == nullptr) return CMD_ERROR;
CommandCost ret = CheckOwnership(del->owner);
if (ret.Failed()) return ret;
bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) {
// Remove corresponding template replacements if existing
for (TemplateReplacement *tr : TemplateReplacement::Iterate()) {
if (tr->Template() == del->index) {
delete tr;
}
}
delete del;
InvalidateWindowClassesData(WC_CREATE_TEMPLATE, 0);
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Issues a template replacement for a vehicle group
* @param tile unused
* @param flags type of operation
* @param p1 the group index
* @param p2 the template vehicle's index
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdIssueTemplateReplacement(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
bool should_execute = (flags & DC_EXEC) != 0;
GroupID group_id = p1;
TemplateID template_id = p2;
if (should_execute) {
bool succeeded = IssueTemplateReplacement(group_id, template_id);
if (!succeeded) {
return CMD_ERROR;
}
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Deletes a template replacement from a vehicle group
* @param tile unused
* @param flags type of operation
* @param p1 the group index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdDeleteTemplateReplacement(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
bool should_execute = (flags & DC_EXEC) != 0;
GroupID group_id = p1;
if (should_execute) {
TemplateReplacement* tr = GetTemplateReplacementByGroupID(group_id);
if (tr != nullptr) {
delete tr;
}
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN, 0);
}
return CommandCost();
}
/**
* Clone a vehicle. If it is a train, it will clone all the cars too
* @param tile tile of the depot where the cloned vehicle is build
* @param flags type of operation
* @param p1 the original vehicle's index
* @param p2 1 = shared orders, else copied orders
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
CommandCost total_cost(EXPENSES_NEW_VEHICLES);
Vehicle *v = Vehicle::GetIfValid(p1);
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
Vehicle *v_front = v;
Vehicle *w = nullptr;
Vehicle *w_front = nullptr;
Vehicle *w_rear = nullptr;
/*
* v_front is the front engine in the original vehicle
* v is the car/vehicle of the original vehicle that is currently being copied
* w_front is the front engine of the cloned vehicle
* w is the car/vehicle currently being cloned
* w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
*/
CommandCost ret = CheckOwnership(v->owner);
if (ret.Failed()) return ret;
if (v->type == VEH_TRAIN && (!v->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
/* check that we can allocate enough vehicles */
if (!(flags & DC_EXEC)) {
int veh_counter = 0;
do {
veh_counter++;
} while ((v = v->Next()) != nullptr);
if (!Vehicle::CanAllocateItem(veh_counter)) {
return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
}
}
v = v_front;
do {
if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
/* we build the rear ends of multiheaded trains with the front ones */
continue;
}
/* In case we're building a multi headed vehicle and the maximum number of
* vehicles is almost reached (e.g. max trains - 1) not all vehicles would
* be cloned. When the non-primary engines were build they were seen as
* 'new' vehicles whereas they would immediately be joined with a primary
* engine. This caused the vehicle to be not build as 'the limit' had been
* reached, resulting in partially build vehicles and such. */
DoCommandFlag build_flags = flags;
if ((flags & DC_EXEC) && !v->IsPrimaryVehicle()) build_flags |= DC_AUTOREPLACE;
CommandCost cost = DoCommand(tile, v->engine_type | (1 << 16) | (INVALID_CARGO << 24), 0, build_flags, GetCmdBuildVeh(v));
if (cost.Failed()) {
/* Can't build a part, then sell the stuff we already made; clear up the mess */
if (w_front != nullptr) DoCommand(w_front->tile, w_front->index | (1 << 20), 0, flags, GetCmdSellVeh(w_front));
return cost;
}
total_cost.AddCost(cost);
if (flags & DC_EXEC) {
w = Vehicle::Get(_new_vehicle_id);
if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
}
if (v->type == VEH_TRAIN && !v->IsFrontEngine()) {
/* this s a train car
* add this unit to the end of the train */
CommandCost result = DoCommand(0, w->index | 1 << 20, w_rear->index, flags, CMD_MOVE_RAIL_VEHICLE);
if (result.Failed()) {
/* The train can't be joined to make the same consist as the original.
* Sell what we already made (clean up) and return an error. */
DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
DoCommand(w_front->tile, w->index | 1 << 20, 0, flags, GetCmdSellVeh(w));
return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
}
} else {
/* this is a front engine or not a train. */
w_front = w;
w->service_interval = v->service_interval;
w->SetServiceIntervalIsCustom(v->ServiceIntervalIsCustom());
w->SetServiceIntervalIsPercent(v->ServiceIntervalIsPercent());
}
w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
}
} while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != nullptr);
if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
/* for trains this needs to be the front engine due to the callback function */
_new_vehicle_id = w_front->index;
}
const Company *owner = Company::GetIfValid(_current_company);
if ((flags & DC_EXEC) && ((p2 & 1) || owner == nullptr || owner->settings.copy_clone_add_to_group)) {
/* Cloned vehicles belong to the same group */
DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
}
/* Take care of refitting. */
w = w_front;
v = v_front;
/* Both building and refitting are influenced by newgrf callbacks, which
* makes it impossible to accurately estimate the cloning costs. In
* particular, it is possible for engines of the same type to be built with
* different numbers of articulated parts, so when refitting we have to
* loop over real vehicles first, and then the articulated parts of those
* vehicles in a different loop. */
do {
do {
if (flags & DC_EXEC) {
assert(w != nullptr);
/* Find out what's the best sub type */
uint8_t subtype = GetBestFittingSubType(v, w, v->cargo_type);
if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
CommandCost cost = DoCommand(0, w->index, v->cargo_type | 1U << 25 | (subtype << 8), flags, GetCmdRefitVeh(v));
if (cost.Succeeded()) total_cost.AddCost(cost);
}
if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
w = w->GetNextArticulatedPart();
} else {
break;
}
} else {
const Engine *e = v->GetEngine();
CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : INVALID_CARGO);
if (v->cargo_type != initial_cargo && initial_cargo != INVALID_CARGO) {
bool dummy;
total_cost.AddCost(GetRefitCost(nullptr, v->engine_type, v->cargo_type, v->cargo_subtype, &dummy));
}
}
if (v->IsGroundVehicle() && v->HasArticulatedPart()) {
v = v->GetNextArticulatedPart();
} else {
break;
}
} while (v != nullptr);
if ((flags & DC_EXEC) && (v->type == VEH_TRAIN || v->type == VEH_SHIP)) w = w->GetNextVehicle();
} while ((v->type == VEH_TRAIN || v->type == VEH_SHIP) && (v = v->GetNextVehicle()) != nullptr);
if (flags & DC_EXEC) {
/*
* Set the orders of the vehicle. Cannot do it earlier as we need
* the vehicle refitted before doing this, otherwise the moved
* cargo types might not match (passenger vs non-passenger)
*/
CommandCost result = DoCommand(0, w_front->index | (p2 & 1 ? CO_SHARE : CO_COPY) << 30, v_front->index, flags, CMD_CLONE_ORDER);
if (result.Failed()) {
/* The vehicle has already been bought, so now it must be sold again. */
DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
return result;
}
/* Now clone the vehicle's name, if it has one. */
if (!v_front->name.empty()) CloneVehicleName(v_front, w_front);
/* Since we can't estimate the cost of cloning a vehicle accurately we must
* check whether the company has enough money manually. */
if (!CheckCompanyHasMoney(total_cost)) {
/* The vehicle has already been bought, so now it must be sold again. */
DoCommand(w_front->tile, w_front->index | 1 << 20, 0, flags, GetCmdSellVeh(w_front));
return total_cost;
}
}
return total_cost;
}
/**
* Clone a vehicle from a template.
* @param tile tile of the depot where the cloned vehicle is build
* @param flags type of operation
* @param p1 the original template vehicle's index
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdCloneVehicleFromTemplate(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
TemplateVehicle* tv = TemplateVehicle::GetIfValid(p1);
if (tv == nullptr) {
return CMD_ERROR;
}
CommandCost ret = CheckOwnership(tv->owner);
if (ret.Failed()) return ret;
/* Vehicle construction needs random bits, so we have to save the random
* seeds to prevent desyncs. */
SavedRandomSeeds saved_seeds;
SaveRandomSeeds(&saved_seeds);
auto guard = scope_guard([&]() {
if (!(flags & DC_EXEC)) RestoreRandomSeeds(saved_seeds);
});
ret = DoCommand(0, tv->index, 0, DC_EXEC, CMD_VIRTUAL_TRAIN_FROM_TEMPLATE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN));
if (ret.Failed()) return ret;
Train* virt = Train::From(Vehicle::Get(_new_vehicle_id));
ret = DoCommand(tile, _new_vehicle_id, 0, flags, CMD_CLONE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN));
delete virt;
return ret;
}
/**
* Send all vehicles of type to depots
* @param flags the flags used for DoCommand()
* @param depot_flags depot command flags
* @param vli identifier of the vehicle list
* @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
*/
static CommandCost SendAllVehiclesToDepot(DoCommandFlag flags, DepotCommand depot_flags, const VehicleListIdentifier &vli, const CargoID cid)
{
VehicleList list;
if (!GenerateVehicleSortList(&list, vli, cid)) return CMD_ERROR;
/* Send all the vehicles to a depot */
bool had_success = false;
for (uint i = 0; i < list.size(); i++) {
const Vehicle *v = list[i];
CommandCost ret = DoCommand(v->tile, v->index | depot_flags, 0, flags, GetCmdSendToDepot(vli.vtype));
if (ret.Succeeded()) {
had_success = true;
/* Return 0 if DC_EXEC is not set this is a valid goto depot command)
* In this case we know that at least one vehicle can be sent to a depot
* and we will issue the command. We can now safely quit the loop, knowing
* it will succeed at least once. With DC_EXEC we really need to send them to the depot */
if (!(flags & DC_EXEC)) break;
}
}
return had_success ? CommandCost() : CMD_ERROR;
}
/**
* Send a vehicle to the depot.
* @param tile unused
* @param flags for command type
* @param p1 bitmask
* - p1 0-19: bitvehicle ID to send to the depot
* - p1 0- 7: cargo filter for DEPOT_MASS_SEND mode
* - p1 bits 27-31 - DEPOT_ flags (see vehicle_type.h)
* @param p2 packed VehicleListIdentifier, or specific depot tile
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdSendVehicleToDepot(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
if (p1 & DEPOT_MASS_SEND) {
/* Mass goto depot requested */
VehicleListIdentifier vli;
if (!vli.UnpackIfValid(p2)) return CMD_ERROR;
uint32_t depot_flags = (p1 & (DEPOT_SERVICE | DEPOT_CANCEL | DEPOT_SELL));
if (!(p1 & DEPOT_CANCEL)) depot_flags |= DEPOT_DONT_CANCEL;
return SendAllVehiclesToDepot(flags, (DepotCommand) depot_flags, vli, GB(p1, 0, 8));
}
Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
if (v == nullptr) return CMD_ERROR;
if (!v->IsPrimaryVehicle()) return CMD_ERROR;
return v->SendToDepot(flags, (DepotCommand)(p1 & DEPOT_COMMAND_MASK), p2);
}
/**
* Give a custom name to your vehicle
* @param tile unused
* @param flags type of operation
* @param p1 vehicle ID to name
* @param p2 unused
* @param text the new name or an empty string when resetting to the default
* @return the cost of this operation or an error
*/
CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
Vehicle *v = Vehicle::GetIfValid(p1);
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
CommandCost ret = CheckOwnership(v->owner);
if (ret.Failed()) return ret;
bool reset = StrEmpty(text);
if (!reset) {
if (Utf8StringLength(text) >= MAX_LENGTH_VEHICLE_NAME_CHARS) return CMD_ERROR;
if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
}
if (flags & DC_EXEC) {
if (reset) {
v->name.clear();
} else {
v->name = text;
}
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 1);
InvalidateWindowClassesData(WC_DEPARTURES_BOARD, 0);
MarkWholeScreenDirty();
}
return CommandCost();
}
/**
* Change the service interval of a vehicle
* @param tile unused
* @param flags type of operation
* @param p1 vehicle ID that is being service-interval-changed
* @param p2 bitmask
* - p2 = (bit 0-15) - new service interval
* - p2 = (bit 16) - service interval is custom flag
* - p2 = (bit 17) - service interval is percentage flag
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
{
Vehicle *v = Vehicle::GetIfValid(p1);
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
CommandCost ret = CheckOwnership(v->owner);
if (ret.Failed()) return ret;
const Company *company = Company::Get(v->owner);
bool iscustom = HasBit(p2, 16);
bool ispercent = iscustom ? HasBit(p2, 17) : company->settings.vehicle.servint_ispercent;
uint16_t serv_int;
if (iscustom) {
serv_int = GB(p2, 0, 16);
if (serv_int != GetServiceIntervalClamped(serv_int, ispercent)) return CMD_ERROR;
} else {
serv_int = CompanyServiceInterval(company, v->type);
}
if (flags & DC_EXEC) {
v->SetServiceInterval(serv_int);
v->SetServiceIntervalIsCustom(iscustom);
v->SetServiceIntervalIsPercent(ispercent);
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
}
return CommandCost();
}