2009-03-13 20:09:35 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-03-13 20:29:35 +00:00
|
|
|
/** @file vehicle_cmd.cpp Commands for vehicles. */
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "roadveh.h"
|
|
|
|
#include "gfx_func.h"
|
|
|
|
#include "news_func.h"
|
2009-06-24 17:39:54 +00:00
|
|
|
#include "airport.h"
|
2009-03-13 20:09:35 +00:00
|
|
|
#include "command_func.h"
|
|
|
|
#include "company_func.h"
|
|
|
|
#include "vehicle_gui.h"
|
|
|
|
#include "train.h"
|
2009-05-22 20:07:26 +00:00
|
|
|
#include "aircraft.h"
|
2009-03-13 20:09:35 +00:00
|
|
|
#include "newgrf_engine.h"
|
|
|
|
#include "newgrf_text.h"
|
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
|
|
|
#include "vehicle_func.h"
|
|
|
|
#include "string_func.h"
|
|
|
|
#include "depot_map.h"
|
|
|
|
#include "vehiclelist.h"
|
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
|
|
|
/* Tables used in vehicle.h to find the right command for a certain vehicle type */
|
|
|
|
const uint32 _veh_build_proc_table[] = {
|
2009-09-07 08:59:43 +00:00
|
|
|
CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
|
|
|
|
CMD_BUILD_ROAD_VEH | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
|
|
|
|
CMD_BUILD_SHIP | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
|
|
|
|
CMD_BUILD_AIRCRAFT | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
|
2009-03-13 20:09:35 +00:00
|
|
|
};
|
2009-07-22 23:39:35 +00:00
|
|
|
|
2009-03-13 20:09:35 +00:00
|
|
|
const uint32 _veh_sell_proc_table[] = {
|
2009-07-22 23:39:35 +00:00
|
|
|
CMD_SELL_RAIL_WAGON | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
|
|
|
|
CMD_SELL_ROAD_VEH | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
|
|
|
|
CMD_SELL_SHIP | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
|
|
|
|
CMD_SELL_AIRCRAFT | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
|
2009-03-13 20:09:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint32 _veh_refit_proc_table[] = {
|
2009-07-22 23:39:35 +00:00
|
|
|
CMD_REFIT_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
|
|
|
|
CMD_REFIT_ROAD_VEH | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
|
|
|
|
CMD_REFIT_SHIP | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
|
|
|
|
CMD_REFIT_AIRCRAFT | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
|
2009-03-13 20:09:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint32 _send_to_depot_proc_table[] = {
|
2009-07-22 23:39:35 +00:00
|
|
|
/* TrainGotoDepot has a nice randomizer in the pathfinder, which causes desyncs... */
|
|
|
|
CMD_SEND_TRAIN_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT) | CMD_NO_TEST_IF_IN_NETWORK,
|
|
|
|
CMD_SEND_ROADVEH_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
|
|
|
|
CMD_SEND_SHIP_TO_DEPOT | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
|
|
|
|
CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
|
2009-03-13 20:09:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Start/Stop a vehicle
|
|
|
|
* @param tile unused
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 vehicle to start/stop
|
|
|
|
* @param p2 bit 0: Shall the start/stop newgrf callback be evaluated (only valid with DC_AUTOREPLACE for network safety)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:09:35 +00:00
|
|
|
*/
|
|
|
|
CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 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);
|
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(p1);
|
|
|
|
if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
switch (v->type) {
|
|
|
|
case VEH_TRAIN:
|
2009-08-05 17:59:21 +00:00
|
|
|
if ((v->vehstatus & VS_STOPPED) && Train::From(v)->tcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_CATENARY);
|
2009-03-13 20:09:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
|
case VEH_ROAD:
|
|
|
|
break;
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
case VEH_AIRCRAFT: {
|
2009-06-06 16:54:22 +00:00
|
|
|
Aircraft *a = Aircraft::From(v);
|
2009-03-13 20:09:35 +00:00
|
|
|
/* cannot stop airplane when in flight, or when taking off / landing */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT);
|
|
|
|
} break;
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
default: return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this vehicle can be started/stopped. The callback will fail or
|
|
|
|
* return 0xFF if it can. */
|
|
|
|
uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
|
|
|
|
if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF && HasBit(p2, 0)) {
|
|
|
|
StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
|
|
|
|
return_cmd_error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-07-22 20:17:07 +00:00
|
|
|
if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, STR_NEWS_TRAIN_IS_WAITING + v->type);
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
v->vehstatus ^= VS_STOPPED;
|
|
|
|
if (v->type != VEH_TRAIN) v->cur_speed = 0; // trains can stop 'slowly'
|
2009-09-20 19:36:27 +00:00
|
|
|
v->MarkDirty();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
|
2009-03-13 20:09:35 +00:00
|
|
|
}
|
|
|
|
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 Station/Order/Depot ID (only used for vehicle list windows)
|
|
|
|
* @param p2 bitmask
|
|
|
|
* - bit 0-4 Vehicle type
|
|
|
|
* - bit 5 false = start vehicles, true = stop vehicles
|
|
|
|
* - bit 6 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case
|
|
|
|
* - bit 8-11 Vehicle List Window type (ignored unless bit 1 is set)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:09:35 +00:00
|
|
|
*/
|
|
|
|
CommandCost CmdMassStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
VehicleList list;
|
|
|
|
CommandCost return_value = CMD_ERROR;
|
|
|
|
VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
|
|
|
|
bool start_stop = HasBit(p2, 5);
|
|
|
|
bool vehicle_list_window = HasBit(p2, 6);
|
|
|
|
|
|
|
|
if (vehicle_list_window) {
|
|
|
|
uint32 id = p1;
|
|
|
|
uint16 window_type = p2 & VLW_MASK;
|
|
|
|
|
|
|
|
GenerateVehicleSortList(&list, vehicle_type, _current_company, id, window_type);
|
|
|
|
} else {
|
|
|
|
/* Get the list of vehicles in the depot */
|
|
|
|
BuildDepotVehicleList(vehicle_type, tile, &list, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < list.Length(); i++) {
|
|
|
|
const Vehicle *v = list[i];
|
|
|
|
|
|
|
|
if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
|
|
|
|
|
|
|
|
if (!vehicle_list_window) {
|
|
|
|
if (vehicle_type == VEH_TRAIN) {
|
2009-06-06 16:54:22 +00:00
|
|
|
if (CheckTrainInDepot(Train::From(v), false) == -1) continue;
|
2009-03-13 20:09:35 +00:00
|
|
|
} else {
|
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandCost ret = DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
|
|
|
|
|
|
|
|
if (CmdSucceeded(ret)) {
|
|
|
|
return_value = CommandCost();
|
|
|
|
/* We know that the command is valid for at least one vehicle.
|
|
|
|
* If we haven't set DC_EXEC, then there is no point in continueing because it will be valid */
|
|
|
|
if (!(flags & DC_EXEC)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:09:35 +00:00
|
|
|
*/
|
|
|
|
CommandCost CmdDepotSellAllVehicles(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
VehicleList list;
|
|
|
|
|
|
|
|
CommandCost cost(EXPENSES_NEW_VEHICLES);
|
|
|
|
VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
|
2009-07-23 22:01:25 +00:00
|
|
|
uint sell_command = GetCmdSellVeh(vehicle_type);
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
/* Get the list of vehicles in the depot */
|
|
|
|
BuildDepotVehicleList(vehicle_type, tile, &list, &list);
|
|
|
|
|
|
|
|
for (uint i = 0; i < list.Length(); i++) {
|
|
|
|
CommandCost ret = DoCommand(tile, list[i]->index, 1, flags, sell_command);
|
|
|
|
if (CmdSucceeded(ret)) cost.AddCost(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cost.GetCost() == 0) return CMD_ERROR; // no vehicles to sell
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Autoreplace all vehicles in the depot
|
|
|
|
* Note: this command can make incorrect cost estimations
|
|
|
|
* Luckily the final price can only drop, not increase. This is due to the fact that
|
|
|
|
* estimation can't predict wagon removal so it presumes worst case which is no income from selling wagons.
|
|
|
|
* @param tile Tile of the depot where the vehicles are
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 Type of vehicle
|
|
|
|
* @param p2 If bit 0 is set, then either replace all or nothing (instead of replacing until money runs out)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:09:35 +00:00
|
|
|
*/
|
|
|
|
CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
VehicleList list;
|
|
|
|
CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
|
|
|
|
VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
|
|
|
|
bool all_or_nothing = HasBit(p2, 0);
|
|
|
|
|
|
|
|
if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_company)) return CMD_ERROR;
|
|
|
|
|
|
|
|
/* Get the list of vehicles in the depot */
|
|
|
|
BuildDepotVehicleList(vehicle_type, tile, &list, &list, true);
|
|
|
|
|
|
|
|
bool did_something = false;
|
|
|
|
|
|
|
|
for (uint i = 0; i < list.Length(); i++) {
|
2009-05-24 20:29:04 +00:00
|
|
|
const Vehicle *v = list[i];
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
/* Ensure that the vehicle completely in the depot */
|
|
|
|
if (!v->IsInDepot()) continue;
|
|
|
|
|
|
|
|
CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
|
|
|
|
|
|
|
|
if (CmdSucceeded(ret)) {
|
|
|
|
did_something = true;
|
|
|
|
cost.AddCost(ret);
|
|
|
|
} else {
|
2009-08-04 18:04:33 +00:00
|
|
|
if (ret.GetErrorMessage() != STR_ERROR_AUTOREPLACE_NOTHING_TO_DO && all_or_nothing) {
|
2009-03-13 20:09:35 +00:00
|
|
|
/* We failed to replace a vehicle even though we set all or nothing.
|
|
|
|
* We should never reach this if DC_EXEC is set since then it should
|
|
|
|
* have failed the estimation guess. */
|
|
|
|
assert(!(flags & DC_EXEC));
|
|
|
|
/* Now we will have to return an error. */
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!did_something) {
|
|
|
|
/* Either we didn't replace anything or something went wrong.
|
|
|
|
* Either way we want to return an error and not execute this command. */
|
|
|
|
cost = CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2009-10-28 21:09:37 +00:00
|
|
|
/** Learn the price of refitting a certain engine
|
|
|
|
* @param engine_type Which engine to refit
|
|
|
|
* @return Price for refitting
|
|
|
|
*/
|
|
|
|
static CommandCost GetRefitCost(EngineID engine_type)
|
|
|
|
{
|
|
|
|
Money base_cost;
|
|
|
|
ExpensesType expense_type;
|
|
|
|
const Engine *e = Engine::Get(engine_type);
|
|
|
|
switch (e->type) {
|
|
|
|
case VEH_SHIP:
|
2009-11-07 22:47:54 +00:00
|
|
|
base_cost = _price[PR_BUILD_VEHICLE_SHIP];
|
2009-10-28 21:09:37 +00:00
|
|
|
expense_type = EXPENSES_SHIP_RUN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
2009-11-07 22:47:54 +00:00
|
|
|
base_cost = _price[PR_BUILD_VEHICLE_ROAD];
|
2009-10-28 21:09:37 +00:00
|
|
|
expense_type = EXPENSES_ROADVEH_RUN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
2009-11-07 22:47:54 +00:00
|
|
|
base_cost = _price[PR_BUILD_VEHICLE_AIRCRAFT];
|
2009-10-28 21:09:37 +00:00
|
|
|
expense_type = EXPENSES_AIRCRAFT_RUN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_TRAIN:
|
2009-11-07 22:47:54 +00:00
|
|
|
base_cost = 2 * _price[(e->u.rail.railveh_type == RAILVEH_WAGON) ? PR_BUILD_VEHICLE_WAGON : PR_BUILD_VEHICLE_TRAIN];
|
2009-10-28 21:09:37 +00:00
|
|
|
expense_type = EXPENSES_TRAIN_RUN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
return CommandCost(expense_type, (e->info.refit_cost * base_cost) >> 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 the whole chain.
|
|
|
|
* @param new_cid Cargotype to refit to
|
|
|
|
* @param new_subtype Cargo subtype to refit to
|
|
|
|
* @param flags Command flags
|
|
|
|
* @return refit cost; or CMD_ERROR if no vehicle was actually refitable to the cargo
|
|
|
|
*/
|
|
|
|
CommandCost RefitVehicle(Vehicle *v, bool only_this, CargoID new_cid, byte new_subtype, DoCommandFlag flags)
|
|
|
|
{
|
|
|
|
CommandCost cost(v->GetExpenseType(false));
|
|
|
|
uint total_capacity = 0;
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
for (; v != NULL; v = (only_this ? NULL : v->Next())) {
|
|
|
|
const Engine *e = Engine::Get(v->engine_type);
|
|
|
|
if (!e->CanCarryCargo() || !HasBit(e->info.refit_mask, new_cid)) continue;
|
|
|
|
success = true;
|
|
|
|
|
|
|
|
/* Back up the vehicle's cargo type */
|
|
|
|
CargoID temp_cid = v->cargo_type;
|
|
|
|
byte temp_subtype = v->cargo_subtype;
|
|
|
|
v->cargo_type = new_cid;
|
|
|
|
v->cargo_subtype = new_subtype;
|
|
|
|
|
2009-10-31 17:48:09 +00:00
|
|
|
uint16 mail_capacity;
|
|
|
|
uint amount = GetVehicleCapacity(v, &mail_capacity);
|
2009-10-28 21:09:37 +00:00
|
|
|
total_capacity += amount;
|
|
|
|
|
|
|
|
/* Restore the original cargo type */
|
|
|
|
v->cargo_type = temp_cid;
|
|
|
|
v->cargo_subtype = temp_subtype;
|
|
|
|
|
|
|
|
if (new_cid != v->cargo_type) {
|
|
|
|
cost.AddCost(GetRefitCost(v->engine_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
|
|
|
|
v->cargo_type = new_cid;
|
|
|
|
v->cargo_cap = amount;
|
|
|
|
v->cargo_subtype = new_subtype;
|
2009-10-31 17:48:09 +00:00
|
|
|
if (v->type == VEH_AIRCRAFT) {
|
|
|
|
Vehicle *u = v->Next();
|
|
|
|
u->cargo_cap = mail_capacity;
|
|
|
|
u->cargo.Truncate(mail_capacity);
|
|
|
|
}
|
2009-10-28 21:09:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_returned_refit_capacity = total_capacity;
|
|
|
|
return success ? cost : CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-03-13 20:09:35 +00:00
|
|
|
/** Test if a name is unique among vehicle names.
|
|
|
|
* @param name Name to test.
|
|
|
|
* @return True ifffffff the name is unique.
|
|
|
|
*/
|
|
|
|
static bool IsUniqueVehicleName(const char *name)
|
|
|
|
{
|
|
|
|
const Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->name != NULL && strcmp(v->name, name) == 0) 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)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
/* Find the position of the first digit in the last group of digits. */
|
|
|
|
size_t number_position;
|
|
|
|
for (number_position = strlen(src->name); number_position > 0; number_position--) {
|
|
|
|
/* The design of UTF-8 lets this work simply without having to check
|
|
|
|
* for UTF-8 sequences. */
|
|
|
|
if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Format buffer and determine starting number. */
|
|
|
|
int num;
|
|
|
|
if (number_position == strlen(src->name)) {
|
|
|
|
/* No digit at the end, so start at number 2. */
|
|
|
|
strecpy(buf, src->name, lastof(buf));
|
|
|
|
strecat(buf, " ", lastof(buf));
|
|
|
|
number_position = strlen(buf);
|
|
|
|
num = 2;
|
|
|
|
} else {
|
|
|
|
/* Found digits, parse them and start at the next number. */
|
|
|
|
strecpy(buf, src->name, lastof(buf));
|
|
|
|
buf[number_position] = '\0';
|
|
|
|
num = strtol(&src->name[number_position], NULL, 10) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this name is already taken. */
|
|
|
|
for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
|
|
|
|
/* Attach the number to the temporary name. */
|
|
|
|
seprintf(&buf[number_position], lastof(buf), "%d", num);
|
|
|
|
|
|
|
|
/* Check the name is unique. */
|
|
|
|
if (IsUniqueVehicleName(buf)) {
|
|
|
|
dst->name = strdup(buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done. If we didn't find a name, it'll just use its default. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:09:35 +00:00
|
|
|
*/
|
|
|
|
CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
CommandCost total_cost(EXPENSES_NEW_VEHICLES);
|
|
|
|
uint32 build_argument = 2;
|
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(p1);
|
|
|
|
if (v == NULL) return CMD_ERROR;
|
2009-03-13 20:09:35 +00:00
|
|
|
Vehicle *v_front = v;
|
|
|
|
Vehicle *w = NULL;
|
|
|
|
Vehicle *w_front = NULL;
|
|
|
|
Vehicle *w_rear = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!CheckOwnership(v->owner)) return CMD_ERROR;
|
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->type == VEH_TRAIN && (!Train::From(v)->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
/* check that we can allocate enough vehicles */
|
|
|
|
if (!(flags & DC_EXEC)) {
|
|
|
|
int veh_counter = 0;
|
|
|
|
do {
|
|
|
|
veh_counter++;
|
|
|
|
} while ((v = v->Next()) != NULL);
|
|
|
|
|
2009-05-13 19:26:47 +00:00
|
|
|
if (!Vehicle::CanAllocateItem(veh_counter)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
|
2009-03-13 20:09:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v = v_front;
|
|
|
|
|
|
|
|
do {
|
2009-07-01 23:57:20 +00:00
|
|
|
if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
|
2009-03-13 20:09:35 +00:00
|
|
|
/* we build the rear ends of multiheaded trains with the front ones */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-12 14:37:29 +00:00
|
|
|
/* 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, build_argument, build_flags, GetCmdBuildVeh(v));
|
2009-03-13 20:09:35 +00:00
|
|
|
build_argument = 3; // ensure that we only assign a number to the first engine
|
|
|
|
|
2009-09-12 14:37:29 +00:00
|
|
|
if (CmdFailed(cost)) {
|
|
|
|
/* Can't build a part, then sell the stuff we already made; clear up the mess */
|
|
|
|
if (w_front != NULL) DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
|
|
|
|
return cost;
|
|
|
|
}
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
total_cost.AddCost(cost);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-05-16 23:34:14 +00:00
|
|
|
w = Vehicle::Get(_new_vehicle_id);
|
2009-03-13 20:09:35 +00:00
|
|
|
|
2009-06-06 16:54:22 +00:00
|
|
|
if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
|
|
|
|
SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
|
2009-03-13 20:09:35 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->type == VEH_TRAIN && !Train::From(v)->IsFrontEngine()) {
|
2009-03-13 20:09:35 +00:00
|
|
|
/* this s a train car
|
|
|
|
* add this unit to the end of the train */
|
|
|
|
CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
|
|
|
|
if (CmdFailed(result)) {
|
|
|
|
/* 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, flags, GetCmdSellVeh(w_front));
|
|
|
|
DoCommand(w_front->tile, w->index, 1, 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_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
|
|
|
|
}
|
2009-07-02 12:10:39 +00:00
|
|
|
} while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
|
2009-03-13 20:09:35 +00:00
|
|
|
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
|
2009-03-13 20:09:35 +00:00
|
|
|
/* for trains this needs to be the front engine due to the callback function */
|
|
|
|
_new_vehicle_id = w_front->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* 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 != NULL);
|
|
|
|
|
|
|
|
if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_subtype) {
|
2009-10-02 15:13:15 +00:00
|
|
|
CommandCost cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16, flags, GetCmdRefitVeh(v));
|
2009-03-13 20:09:35 +00:00
|
|
|
if (CmdSucceeded(cost)) total_cost.AddCost(cost);
|
|
|
|
}
|
|
|
|
|
2009-07-02 09:06:15 +00:00
|
|
|
if (w->type == VEH_TRAIN && Train::From(w)->HasArticulatedPart()) {
|
2009-07-02 12:10:39 +00:00
|
|
|
w = Train::From(w)->GetNextArticPart();
|
2009-07-02 09:06:15 +00:00
|
|
|
} else if (w->type == VEH_ROAD && RoadVehicle::From(w)->HasArticulatedPart()) {
|
2009-03-13 20:09:35 +00:00
|
|
|
w = w->Next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-16 23:34:14 +00:00
|
|
|
const Engine *e = Engine::Get(v->engine_type);
|
2009-03-13 20:09:35 +00:00
|
|
|
CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
|
|
|
|
|
|
|
|
if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
|
|
|
|
total_cost.AddCost(GetRefitCost(v->engine_type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-02 09:06:15 +00:00
|
|
|
if (v->type == VEH_TRAIN && Train::From(v)->HasArticulatedPart()) {
|
2009-07-02 12:10:39 +00:00
|
|
|
v = Train::From(v)->GetNextArticPart();
|
2009-07-02 09:06:15 +00:00
|
|
|
} else if (v->type == VEH_ROAD && RoadVehicle::From(v)->HasArticulatedPart()) {
|
2009-03-13 20:09:35 +00:00
|
|
|
v = v->Next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (v != NULL);
|
|
|
|
|
2009-07-02 12:10:39 +00:00
|
|
|
if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = Train::From(w)->GetNextVehicle();
|
|
|
|
} while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
|
|
|
|
|
|
|
|
/* Now clone the vehicle's name, if it has one. */
|
|
|
|
if (v_front->name != NULL) 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)) {
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* The vehicle has already been bought, so now it must be sold again. */
|
|
|
|
DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
|
|
|
|
}
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send all vehicles of type to depots
|
|
|
|
* @param type type of vehicle
|
|
|
|
* @param flags the flags used for DoCommand()
|
|
|
|
* @param service should the vehicles only get service in the depots
|
|
|
|
* @param owner owner of the vehicles to send
|
|
|
|
* @param vlw_flag tells what kind of list requested the goto depot
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param id general purpose id whoms meaning is given by @c vlw_flag; e.g. StationID for station lists
|
2009-03-13 20:09:35 +00:00
|
|
|
* @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
|
|
|
|
*/
|
|
|
|
CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
|
|
|
|
{
|
|
|
|
VehicleList list;
|
|
|
|
|
|
|
|
GenerateVehicleSortList(&list, type, owner, id, vlw_flag);
|
|
|
|
|
|
|
|
/* Send all the vehicles to a depot */
|
|
|
|
for (uint i = 0; i < list.Length(); i++) {
|
|
|
|
const Vehicle *v = list[i];
|
|
|
|
CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
|
|
|
|
|
|
|
|
/* Return 0 if DC_EXEC is not set this is a valid goto depot command)
|
2009-03-14 18:16:29 +00:00
|
|
|
* 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 */
|
2009-03-13 20:09:35 +00:00
|
|
|
if (CmdSucceeded(ret) && !(flags & DC_EXEC)) {
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (flags & DC_EXEC) ? CommandCost() : CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Give a custom name to your vehicle
|
|
|
|
* @param tile unused
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 vehicle ID to name
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text the new name or an empty string when resetting to the default
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:09:35 +00:00
|
|
|
*/
|
|
|
|
CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(p1);
|
|
|
|
if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
bool reset = StrEmpty(text);
|
|
|
|
|
|
|
|
if (!reset) {
|
|
|
|
if (strlen(text) >= MAX_LENGTH_VEHICLE_NAME_BYTES) return CMD_ERROR;
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!(flags & DC_AUTOREPLACE) && !IsUniqueVehicleName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
2009-03-13 20:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
free(v->name);
|
|
|
|
v->name = reset ? NULL : strdup(text);
|
|
|
|
InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
|
|
|
|
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 new service interval
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:09:35 +00:00
|
|
|
*/
|
|
|
|
CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(p1);
|
2009-05-26 21:59:49 +00:00
|
|
|
if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2009-03-13 20:09:35 +00:00
|
|
|
|
2009-05-26 21:59:49 +00:00
|
|
|
uint16 serv_int = GetServiceIntervalClamped(p2, v->owner); // Double check the service interval from the user-input
|
|
|
|
if (serv_int != p2) return CMD_ERROR;
|
2009-03-13 20:09:35 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->service_interval = serv_int;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
|
2009-03-13 20:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|