2005-07-24 14:12:37 +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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file engine.cpp Base for all engine handling. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2007-01-27 12:29:55 +00:00
|
|
|
#include "aircraft.h"
|
2009-06-24 21:33:11 +00:00
|
|
|
#include "newgrf.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "newgrf_engine.h"
|
2009-10-31 17:48:09 +00:00
|
|
|
#include "newgrf_cargo.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
#include "group.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2008-01-09 09:57:48 +00:00
|
|
|
#include "gfx_func.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "core/random_func.hpp"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_gui.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai/ai.hpp"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2009-08-30 17:54:11 +00:00
|
|
|
#include "engine_gui.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/engines.h"
|
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
EnginePool _engine_pool("Engine");
|
|
|
|
INSTANTIATE_POOL_METHODS(Engine)
|
2005-12-15 17:55:59 +00:00
|
|
|
|
2009-03-08 16:51:08 +00:00
|
|
|
EngineOverrideManager _engine_mngr;
|
|
|
|
|
2009-01-08 21:48:59 +00:00
|
|
|
/** Year that engine aging stops. Engines will not reduce in reliability
|
|
|
|
* and no more engines will be introduced */
|
|
|
|
Year _year_engine_aging_stops;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
/** Number of engines of each vehicle type in original engine data */
|
|
|
|
const uint8 _engine_counts[4] = {
|
|
|
|
lengthof(_orig_rail_vehicle_info),
|
|
|
|
lengthof(_orig_road_vehicle_info),
|
|
|
|
lengthof(_orig_ship_vehicle_info),
|
|
|
|
lengthof(_orig_aircraft_vehicle_info),
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Offset of the first engine of each vehicle type in original engine data */
|
|
|
|
const uint8 _engine_offsets[4] = {
|
|
|
|
0,
|
|
|
|
lengthof(_orig_rail_vehicle_info),
|
|
|
|
lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
|
|
|
|
lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
|
|
|
|
};
|
|
|
|
|
2009-10-10 18:48:13 +00:00
|
|
|
assert_compile(lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info) + lengthof(_orig_aircraft_vehicle_info) == lengthof(_orig_engine_info));
|
|
|
|
|
2009-03-08 16:51:08 +00:00
|
|
|
const uint EngineOverrideManager::NUM_DEFAULT_ENGINES = _engine_counts[VEH_TRAIN] + _engine_counts[VEH_ROAD] + _engine_counts[VEH_SHIP] + _engine_counts[VEH_AIRCRAFT];
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
Engine::Engine() :
|
|
|
|
name(NULL),
|
|
|
|
overrides_count(0),
|
|
|
|
overrides(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine::Engine(VehicleType type, EngineID base)
|
2008-01-08 18:25:51 +00:00
|
|
|
{
|
2008-04-29 21:31:29 +00:00
|
|
|
this->type = type;
|
|
|
|
this->internal_id = base;
|
|
|
|
this->list_position = base;
|
|
|
|
|
|
|
|
/* Check if this base engine is within the original engine data range */
|
|
|
|
if (base >= _engine_counts[type]) {
|
|
|
|
/* Mark engine as valid anyway */
|
|
|
|
this->info.climates = 0x80;
|
2008-12-16 18:09:40 +00:00
|
|
|
/* Set model life to maximum to make wagons available */
|
|
|
|
this->info.base_life = 0xFF;
|
2008-04-29 21:31:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the original engine info for this slot */
|
|
|
|
this->info = _orig_engine_info[_engine_offsets[type] + base];
|
|
|
|
|
|
|
|
/* Copy the original engine data for this slot */
|
|
|
|
switch (type) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
|
|
|
|
case VEH_TRAIN:
|
|
|
|
this->u.rail = _orig_rail_vehicle_info[base];
|
2009-06-16 13:31:06 +00:00
|
|
|
this->original_image_index = this->u.rail.image_index;
|
2009-04-21 23:40:56 +00:00
|
|
|
this->info.string_id = STR_VEHICLE_NAME_TRAIN_ENGINE_RAIL_KIRBY_PAUL_TANK_STEAM + base;
|
2008-12-16 18:09:40 +00:00
|
|
|
|
|
|
|
/* Set the default model life of original wagons to "infinite" */
|
|
|
|
if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
|
this->u.road = _orig_road_vehicle_info[base];
|
2009-06-16 13:31:06 +00:00
|
|
|
this->original_image_index = this->u.road.image_index;
|
2009-07-22 22:44:56 +00:00
|
|
|
this->info.string_id = STR_VEHICLE_NAME_ROAD_VEHICLE_MPS_REGAL_BUS + base;
|
2008-04-29 21:31:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
|
this->u.ship = _orig_ship_vehicle_info[base];
|
2009-06-16 13:31:06 +00:00
|
|
|
this->original_image_index = this->u.ship.image_index;
|
2009-04-21 23:40:56 +00:00
|
|
|
this->info.string_id = STR_VEHICLE_NAME_SHIP_MPS_OIL_TANKER + base;
|
2008-04-29 21:31:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
this->u.air = _orig_aircraft_vehicle_info[base];
|
2009-06-16 13:31:06 +00:00
|
|
|
this->original_image_index = this->u.air.image_index;
|
2009-04-21 23:40:56 +00:00
|
|
|
this->info.string_id = STR_VEHICLE_NAME_AIRCRAFT_SAMPSON_U52 + base;
|
2008-04-29 21:31:29 +00:00
|
|
|
break;
|
2008-01-12 19:58:06 +00:00
|
|
|
}
|
2008-01-08 18:25:51 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
Engine::~Engine()
|
|
|
|
{
|
|
|
|
UnloadWagonOverrides(this);
|
|
|
|
free(this->name);
|
|
|
|
}
|
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
/**
|
|
|
|
* Determines whether an engine can carry something.
|
|
|
|
* A vehicle cannot carry anything if its capacity is zero, or none of the possible cargos is available in the climate.
|
|
|
|
* @return true if the vehicle can carry something.
|
|
|
|
*/
|
|
|
|
bool Engine::CanCarryCargo() const
|
|
|
|
{
|
|
|
|
/* For engines that can appear in a consist (i.e. rail vehicles and (articulated) road vehicles), a capacity
|
|
|
|
* of zero is a special case, to define the vehicle to not carry anything. The default cargotype is still used
|
|
|
|
* for livery selection etc.
|
|
|
|
* Note: Only the property is tested. A capacity callback returning 0 does not have the same effect.
|
|
|
|
*/
|
|
|
|
switch (this->type) {
|
|
|
|
case VEH_TRAIN:
|
|
|
|
if (this->u.rail.capacity == 0) return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
|
if (this->u.road.capacity == 0) return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
return this->GetDefaultCargoType() != CT_INVALID;
|
|
|
|
}
|
|
|
|
|
2009-03-18 19:32:13 +00:00
|
|
|
/**
|
|
|
|
* Determines the default cargo capacity of an engine for display purposes.
|
|
|
|
*
|
|
|
|
* For planes carrying both passenger and mail this is the passenger capacity.
|
|
|
|
* For multiheaded engines this is the capacity of both heads.
|
|
|
|
* For articulated engines use GetCapacityOfArticulatedParts
|
|
|
|
*
|
2009-10-28 18:31:16 +00:00
|
|
|
* @note Keep this function consistent with GetVehicleCapacity().
|
2009-10-31 17:48:09 +00:00
|
|
|
* @param mail_capacity returns secondary cargo (mail) capacity of aircraft
|
2009-03-18 19:32:13 +00:00
|
|
|
* @return The default capacity
|
|
|
|
* @see GetDefaultCargoType
|
|
|
|
*/
|
2009-10-31 17:48:09 +00:00
|
|
|
uint Engine::GetDisplayDefaultCapacity(uint16 *mail_capacity) const
|
2009-03-18 19:32:13 +00:00
|
|
|
{
|
2009-10-31 17:48:09 +00:00
|
|
|
if (mail_capacity != NULL) *mail_capacity = 0;
|
2009-03-18 19:32:13 +00:00
|
|
|
if (!this->CanCarryCargo()) return 0;
|
|
|
|
switch (type) {
|
|
|
|
case VEH_TRAIN:
|
2009-09-22 19:28:57 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY, this->u.rail.capacity) + (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? this->u.rail.capacity : 0);
|
2009-03-18 19:32:13 +00:00
|
|
|
|
|
|
|
case VEH_ROAD:
|
2009-09-22 19:28:57 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY, this->u.road.capacity);
|
2009-03-18 19:32:13 +00:00
|
|
|
|
|
|
|
case VEH_SHIP:
|
2009-09-22 19:28:57 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY, this->u.ship.capacity);
|
2009-03-18 19:32:13 +00:00
|
|
|
|
2009-10-31 17:48:09 +00:00
|
|
|
case VEH_AIRCRAFT: {
|
|
|
|
uint capacity = this->u.air.passenger_capacity;
|
|
|
|
CargoID cargo = this->GetDefaultCargoType();
|
|
|
|
if (IsCargoInClass(cargo, CC_PASSENGERS)) {
|
|
|
|
if (mail_capacity != NULL) *mail_capacity = this->u.air.mail_capacity;
|
|
|
|
} else {
|
|
|
|
capacity += this->u.air.mail_capacity;
|
|
|
|
}
|
|
|
|
switch (cargo) {
|
|
|
|
case CT_PASSENGERS:
|
|
|
|
case CT_MAIL: return capacity;
|
|
|
|
case CT_GOODS: return capacity / 2;
|
|
|
|
default: return capacity / 4;
|
2009-10-28 18:31:16 +00:00
|
|
|
}
|
2009-10-31 17:48:09 +00:00
|
|
|
}
|
2009-03-18 19:32:13 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:14:15 +00:00
|
|
|
Money Engine::GetRunningCost() const
|
|
|
|
{
|
|
|
|
switch (this->type) {
|
2009-11-08 13:02:05 +00:00
|
|
|
case VEH_ROAD: {
|
|
|
|
if (this->u.road.running_cost_class == INVALID_PRICE) return 0;
|
|
|
|
return GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost) * GetPriceByIndex(this->u.road.running_cost_class) >> 8;
|
|
|
|
}
|
2009-01-24 20:14:15 +00:00
|
|
|
|
2009-11-08 13:02:05 +00:00
|
|
|
case VEH_TRAIN: {
|
|
|
|
if (this->u.rail.running_cost_class == INVALID_PRICE) return 0;
|
2009-09-22 19:28:57 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8;
|
2009-11-08 13:02:05 +00:00
|
|
|
}
|
2009-01-24 20:14:15 +00:00
|
|
|
|
|
|
|
case VEH_SHIP:
|
2009-11-07 22:47:54 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_SHIP_RUNNING_COST_FACTOR, this->u.ship.running_cost) * _price[PR_RUNNING_SHIP] >> 8;
|
2009-01-24 20:14:15 +00:00
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
2009-11-07 22:47:54 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_AIRCRAFT_RUNNING_COST_FACTOR, this->u.air.running_cost) * _price[PR_RUNNING_AIRCRAFT] >> 8;
|
2009-01-24 20:14:15 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-25 00:57:03 +00:00
|
|
|
Money Engine::GetCost() const
|
|
|
|
{
|
|
|
|
switch (this->type) {
|
|
|
|
case VEH_ROAD:
|
2009-11-07 22:47:54 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_ROADVEH_COST_FACTOR, this->u.road.cost_factor) * (_price[PR_BUILD_VEHICLE_ROAD] >> 3) >> 5;
|
2009-01-25 00:57:03 +00:00
|
|
|
|
|
|
|
case VEH_TRAIN:
|
|
|
|
if (this->u.rail.railveh_type == RAILVEH_WAGON) {
|
2009-11-07 22:47:54 +00:00
|
|
|
return (GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor) * _price[PR_BUILD_VEHICLE_WAGON]) >> 8;
|
2009-01-25 00:57:03 +00:00
|
|
|
} else {
|
2009-11-07 22:47:54 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor) * (_price[PR_BUILD_VEHICLE_TRAIN] >> 3) >> 5;
|
2009-01-25 00:57:03 +00:00
|
|
|
}
|
2009-11-08 13:02:05 +00:00
|
|
|
|
2009-01-25 00:57:03 +00:00
|
|
|
case VEH_SHIP:
|
2009-11-07 22:47:54 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_SHIP_COST_FACTOR, this->u.ship.cost_factor) * (_price[PR_BUILD_VEHICLE_SHIP] >> 3) >> 5;
|
2009-01-25 00:57:03 +00:00
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
2009-11-07 22:47:54 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_AIRCRAFT_COST_FACTOR, this->u.air.cost_factor) * (_price[PR_BUILD_VEHICLE_AIRCRAFT] >> 3) >> 5;
|
2009-01-25 00:57:03 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-01 16:10:06 +00:00
|
|
|
/**
|
|
|
|
* Returns max speed for display purposes
|
2009-02-01 17:14:39 +00:00
|
|
|
* @return max speed in km-ish/h
|
2009-02-01 16:10:06 +00:00
|
|
|
*/
|
|
|
|
uint Engine::GetDisplayMaxSpeed() const
|
|
|
|
{
|
|
|
|
switch (this->type) {
|
|
|
|
case VEH_TRAIN:
|
2009-09-22 19:28:57 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_TRAIN_SPEED, this->u.rail.max_speed);
|
2009-02-01 16:10:06 +00:00
|
|
|
|
|
|
|
case VEH_ROAD:
|
2009-02-01 17:14:39 +00:00
|
|
|
return this->u.road.max_speed / 2;
|
2009-02-01 16:10:06 +00:00
|
|
|
|
|
|
|
case VEH_SHIP:
|
2009-09-22 19:28:57 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_SHIP_SPEED, this->u.ship.max_speed) / 2;
|
2009-02-01 16:10:06 +00:00
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
2009-02-01 17:14:39 +00:00
|
|
|
return this->u.air.max_speed;
|
2009-02-01 16:10:06 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint Engine::GetPower() const
|
|
|
|
{
|
|
|
|
/* Currently only trains have 'power' */
|
|
|
|
switch (this->type) {
|
|
|
|
case VEH_TRAIN:
|
2009-09-22 19:28:57 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_TRAIN_POWER, this->u.rail.power);
|
2009-02-01 16:10:06 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the weight for display purposes.
|
|
|
|
* For dual-headed train-engines this is the weight of both heads
|
|
|
|
* @return weight in display units metric tons
|
|
|
|
*/
|
|
|
|
uint Engine::GetDisplayWeight() const
|
|
|
|
{
|
|
|
|
/* Currently only trains have 'weight' */
|
|
|
|
switch (this->type) {
|
|
|
|
case VEH_TRAIN:
|
2009-09-22 19:28:57 +00:00
|
|
|
return GetEngineProperty(this->index, PROP_TRAIN_WEIGHT, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
|
2009-02-01 16:10:06 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-19 09:45:44 +00:00
|
|
|
/**
|
|
|
|
* Returns the tractive effort for display purposes.
|
|
|
|
* For dual-headed train-engines this is the tractive effort of both heads
|
|
|
|
* @return tractive effort in display units kN
|
|
|
|
*/
|
|
|
|
uint Engine::GetDisplayMaxTractiveEffort() const
|
|
|
|
{
|
|
|
|
/* Currently only trains have 'tractive effort' */
|
|
|
|
switch (this->type) {
|
|
|
|
case VEH_TRAIN:
|
2009-09-22 19:28:57 +00:00
|
|
|
return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_TRAIN_TRACTIVE_EFFORT, this->u.rail.tractive_effort)) / 256;
|
2009-02-19 09:45:44 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-16 13:52:18 +00:00
|
|
|
/**
|
|
|
|
* Returns the vehicle's life length in days.
|
|
|
|
* @return the life length
|
|
|
|
*/
|
|
|
|
Date Engine::GetLifeLengthInDays() const
|
|
|
|
{
|
|
|
|
/* Assume leap years; this gives the player a bit more than the given amount of years, but never less. */
|
2009-06-17 17:13:30 +00:00
|
|
|
return (this->info.lifelength + _settings_game.vehicle.extend_vehicle_life) * DAYS_IN_LEAP_YEAR;
|
2009-06-16 13:52:18 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 16:51:08 +00:00
|
|
|
/**
|
|
|
|
* Initializes the EngineOverrideManager with the default engines.
|
|
|
|
*/
|
|
|
|
void EngineOverrideManager::ResetToDefaultMapping()
|
|
|
|
{
|
|
|
|
this->Clear();
|
|
|
|
for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
|
|
|
|
for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
|
|
|
|
EngineIDMapping *eid = this->Append();
|
|
|
|
eid->type = type;
|
|
|
|
eid->grfid = INVALID_GRFID;
|
|
|
|
eid->internal_id = internal_id;
|
|
|
|
eid->substitute_id = internal_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks up an EngineID in the EngineOverrideManager
|
|
|
|
* @param type Vehicle type
|
|
|
|
* @param grf_local_id The local id in the newgrf
|
|
|
|
* @param grfid The GrfID that defines the scope of grf_local_id.
|
|
|
|
* If a newgrf overrides the engines of another newgrf, the "scope grfid" is the ID of the overridden newgrf.
|
|
|
|
* If dynnamic_engines is disabled, all newgrf share the same ID scope identified by INVALID_GRFID.
|
|
|
|
* @return The engine ID if present, or INVALID_ENGINE if not.
|
|
|
|
*/
|
|
|
|
EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
|
|
|
|
{
|
|
|
|
const EngineIDMapping *end = this->End();
|
|
|
|
EngineID index = 0;
|
|
|
|
for (const EngineIDMapping *eid = this->Begin(); eid != end; eid++, index++) {
|
|
|
|
if (eid->type == type && eid->grfid == grfid && eid->internal_id == grf_local_id) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return INVALID_ENGINE;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/** Sets cached values in Company::num_vehicles and Group::num_vehicles
|
2008-05-14 15:29:48 +00:00
|
|
|
*/
|
2008-05-13 21:36:09 +00:00
|
|
|
void SetCachedEngineCounts()
|
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
size_t engines = Engine::GetPoolSize();
|
2008-05-13 21:36:09 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Set up the engine count for all companies */
|
|
|
|
Company *c;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
free(c->num_engines);
|
|
|
|
c->num_engines = CallocT<EngineID>(engines);
|
2008-05-13 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Recalculate */
|
|
|
|
Group *g;
|
|
|
|
FOR_ALL_GROUPS(g) {
|
|
|
|
free(g->num_engines);
|
|
|
|
g->num_engines = CallocT<EngineID>(engines);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2009-07-13 16:35:22 +00:00
|
|
|
if (!v->IsEngineCountable()) continue;
|
2008-05-13 21:36:09 +00:00
|
|
|
|
|
|
|
assert(v->engine_type < engines);
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
Company::Get(v->owner)->num_engines[v->engine_type]++;
|
2008-05-13 21:36:09 +00:00
|
|
|
|
|
|
|
if (v->group_id == DEFAULT_GROUP) continue;
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
g = Group::Get(v->group_id);
|
2008-05-13 21:36:09 +00:00
|
|
|
assert(v->type == g->vehicle_type);
|
|
|
|
assert(v->owner == g->owner);
|
|
|
|
|
|
|
|
g->num_engines[v->engine_type]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
void SetupEngines()
|
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
_engine_pool.CleanPool();
|
2008-04-29 21:31:29 +00:00
|
|
|
|
2009-03-08 16:51:08 +00:00
|
|
|
assert(_engine_mngr.Length() >= _engine_mngr.NUM_DEFAULT_ENGINES);
|
|
|
|
const EngineIDMapping *end = _engine_mngr.End();
|
|
|
|
uint index = 0;
|
|
|
|
for (const EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
|
|
|
|
const Engine *e = new Engine(eid->type, eid->internal_id);
|
|
|
|
assert(e->index == index);
|
|
|
|
}
|
2008-04-29 21:31:29 +00:00
|
|
|
}
|
|
|
|
|
2008-01-08 18:25:51 +00:00
|
|
|
|
2005-10-01 12:43:34 +00:00
|
|
|
void ShowEnginePreviewWindow(EngineID engine);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-05 12:08:20 +00:00
|
|
|
/* Determine if an engine type is a wagon (and not a loco) */
|
|
|
|
static bool IsWagon(EngineID index)
|
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
const Engine *e = Engine::Get(index);
|
2008-06-05 12:08:20 +00:00
|
|
|
return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void CalcEngineReliability(Engine *e)
|
|
|
|
{
|
|
|
|
uint age = e->age;
|
|
|
|
|
2007-05-10 23:10:23 +00:00
|
|
|
/* Check for early retirement */
|
2008-12-16 18:09:40 +00:00
|
|
|
if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
|
2008-04-29 21:31:29 +00:00
|
|
|
int retire_early = e->info.retire_early;
|
2008-02-08 15:34:54 +00:00
|
|
|
uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
|
|
|
|
if (retire_early != 0 && age >= retire_early_max_age) {
|
2007-05-10 23:10:23 +00:00
|
|
|
/* Early retirement is enabled and we're past the date... */
|
2008-09-30 20:39:50 +00:00
|
|
|
e->company_avail = 0;
|
2007-05-10 23:10:23 +00:00
|
|
|
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (age < e->duration_phase_1) {
|
|
|
|
uint start = e->reliability_start;
|
|
|
|
e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
|
2008-12-16 18:09:40 +00:00
|
|
|
} else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
|
2006-10-07 15:04:22 +00:00
|
|
|
/* We are at the peak of this engines life. It will have max reliability.
|
|
|
|
* This is also true if the engines never expire. They will not go bad over time */
|
2004-08-09 17:04:08 +00:00
|
|
|
e->reliability = e->reliability_max;
|
|
|
|
} else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
|
|
|
|
uint max = e->reliability_max;
|
|
|
|
e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
|
|
|
|
} else {
|
2006-10-07 15:04:22 +00:00
|
|
|
/* time's up for this engine.
|
|
|
|
* We will now completely retire this design */
|
2008-09-30 20:39:50 +00:00
|
|
|
e->company_avail = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
e->reliability = e->reliability_final;
|
2007-02-06 11:11:12 +00:00
|
|
|
/* Kick this engine out of the lists */
|
|
|
|
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(WC_BUILD_VEHICLE); // Update to show the new reliability
|
|
|
|
SetWindowClassesDirty(WC_REPLACE_VEHICLE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-01-08 21:48:59 +00:00
|
|
|
void SetYearEngineAgingStops()
|
|
|
|
{
|
|
|
|
/* Determine last engine aging year, default to 2050 as previously. */
|
|
|
|
_year_engine_aging_stops = 2050;
|
|
|
|
|
|
|
|
const Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
const EngineInfo *ei = &e->info;
|
|
|
|
|
|
|
|
/* Exclude certain engines */
|
|
|
|
if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue;
|
|
|
|
if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
|
|
|
|
|
|
|
|
/* Base year ending date on half the model life */
|
|
|
|
YearMonthDay ymd;
|
2009-01-13 22:58:03 +00:00
|
|
|
ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
|
2009-01-08 21:48:59 +00:00
|
|
|
|
|
|
|
_year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-23 02:35:17 +00:00
|
|
|
void StartupOneEngine(Engine *e, Date aging_date)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-23 02:35:17 +00:00
|
|
|
const EngineInfo *ei = &e->info;
|
|
|
|
uint32 r;
|
|
|
|
|
|
|
|
e->age = 0;
|
|
|
|
e->flags = 0;
|
|
|
|
e->company_avail = 0;
|
|
|
|
|
2009-07-23 18:45:53 +00:00
|
|
|
/* Don't randomise the start-date in the first two years after gamestart to ensure availability
|
|
|
|
* of engines in early starting games.
|
|
|
|
* Note: TTDP uses fixed 1922 */
|
2009-01-23 02:35:17 +00:00
|
|
|
r = Random();
|
2009-07-23 18:45:53 +00:00
|
|
|
e->intro_date = ei->base_intro <= ConvertYMDToDate(_settings_game.game_creation.starting_year + 2, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
|
2009-01-23 02:35:17 +00:00
|
|
|
if (e->intro_date <= _date) {
|
|
|
|
e->age = (aging_date - e->intro_date) >> 5;
|
|
|
|
e->company_avail = (CompanyMask)-1;
|
|
|
|
e->flags |= ENGINE_AVAILABLE;
|
|
|
|
}
|
2005-01-02 17:23:04 +00:00
|
|
|
|
2009-01-23 02:35:17 +00:00
|
|
|
e->reliability_start = GB(r, 16, 14) + 0x7AE0;
|
|
|
|
r = Random();
|
|
|
|
e->reliability_max = GB(r, 0, 14) + 0xBFFF;
|
|
|
|
e->reliability_final = GB(r, 16, 14) + 0x3FFF;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-23 02:35:17 +00:00
|
|
|
r = Random();
|
|
|
|
e->duration_phase_1 = GB(r, 0, 5) + 7;
|
|
|
|
e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
|
|
|
|
e->duration_phase_3 = GB(r, 9, 7) + 120;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-23 02:35:17 +00:00
|
|
|
e->reliability_spd_dec = ei->decay_speed << 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-23 02:35:17 +00:00
|
|
|
CalcEngineReliability(e);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-23 02:35:17 +00:00
|
|
|
/* prevent certain engines from ever appearing. */
|
|
|
|
if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
|
|
|
|
e->flags |= ENGINE_AVAILABLE;
|
|
|
|
e->company_avail = 0;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-23 02:35:17 +00:00
|
|
|
void StartupEngines()
|
|
|
|
{
|
|
|
|
Engine *e;
|
|
|
|
/* Aging of vehicles stops, so account for that when starting late */
|
|
|
|
const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-23 02:35:17 +00:00
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
StartupOneEngine(e, aging_date);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-08-28 19:53:25 +00:00
|
|
|
|
|
|
|
/* Update the bitmasks for the vehicle lists */
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *c;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
c->avail_railtypes = GetCompanyRailtypes(c->index);
|
|
|
|
c->avail_roadtypes = GetCompanyRoadtypes(c->index);
|
2008-08-28 19:53:25 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void AcceptEnginePreview(EngineID eid, CompanyID company)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
Engine *e = Engine::Get(eid);
|
|
|
|
Company *c = Company::Get(company);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SetBit(e->company_avail, company);
|
2007-03-08 16:27:54 +00:00
|
|
|
if (e->type == VEH_TRAIN) {
|
2009-10-06 19:17:07 +00:00
|
|
|
assert(e->u.rail.railtype < RAILTYPE_END);
|
|
|
|
SetBit(c->avail_railtypes, e->u.rail.railtype);
|
2007-05-25 08:13:01 +00:00
|
|
|
} else if (e->type == VEH_ROAD) {
|
2009-10-06 19:17:07 +00:00
|
|
|
SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
|
2007-01-24 07:14:09 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
e->preview_company_rank = 0xFF;
|
|
|
|
if (company == _local_company) {
|
2007-02-06 11:11:12 +00:00
|
|
|
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
|
2006-10-07 14:30:13 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static CompanyID GetBestCompany(uint8 pp)
|
2005-05-11 00:00:27 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
const Company *c;
|
2005-05-11 00:00:27 +00:00
|
|
|
int32 best_hist;
|
2008-09-30 20:39:50 +00:00
|
|
|
CompanyID best_company;
|
2008-12-23 21:03:43 +00:00
|
|
|
CompanyMask mask = 0;
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2008-02-15 21:05:46 +00:00
|
|
|
do {
|
2005-05-11 00:00:27 +00:00
|
|
|
best_hist = -1;
|
2008-09-30 20:39:50 +00:00
|
|
|
best_company = INVALID_COMPANY;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
if (c->block_preview == 0 && !HasBit(mask, c->index) &&
|
|
|
|
c->old_economy[0].performance_history > best_hist) {
|
|
|
|
best_hist = c->old_economy[0].performance_history;
|
|
|
|
best_company = c->index;
|
2005-05-11 00:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SetBit(mask, best_company);
|
2008-02-25 02:09:04 +00:00
|
|
|
} while (--pp != 0);
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
return best_company;
|
2005-05-11 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void EnginesDailyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-08 21:48:59 +00:00
|
|
|
if (_cur_year >= _year_engine_aging_stops) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
EngineID i = e->index;
|
2007-02-28 17:06:22 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
|
|
|
|
if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
|
2007-02-28 17:06:22 +00:00
|
|
|
e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_ENGINE_PREVIEW, i);
|
2008-09-30 20:39:50 +00:00
|
|
|
e->preview_company_rank++;
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2008-09-30 20:39:50 +00:00
|
|
|
} else if (e->preview_company_rank != 0xFF) {
|
|
|
|
CompanyID best_company = GetBestCompany(e->preview_company_rank);
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (best_company == INVALID_COMPANY) {
|
|
|
|
e->preview_company_rank = 0xFF;
|
2005-05-11 00:00:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
e->flags |= ENGINE_OFFER_WINDOW_OPEN;
|
|
|
|
e->preview_wait = 20;
|
|
|
|
AI::NewEvent(best_company, new AIEventEnginePreview(i));
|
|
|
|
if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/** Accept an engine prototype. XXX - it is possible that the top-company
|
2005-05-11 00:00:27 +00:00
|
|
|
* changes while you are waiting to accept the offer? Then it becomes invalid
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perfom
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 engine-prototype offered
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-11 00:00:27 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-18 19:32:16 +00:00
|
|
|
Engine *e = Engine::GetIfValid(p1);
|
|
|
|
if (e == NULL || GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static void NewVehicleAvailable(Engine *e)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *c;
|
2008-04-29 21:31:29 +00:00
|
|
|
EngineID index = e->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* In case the company didn't build the vehicle during the intro period,
|
|
|
|
* prevent that company from getting future intro periods for a while. */
|
2007-02-28 17:06:22 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
|
2008-09-30 20:39:50 +00:00
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
uint block_preview = c->block_preview;
|
2005-01-06 22:31:58 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!HasBit(e->company_avail, c->index)) continue;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
/* We assume the user did NOT build it.. prove me wrong ;) */
|
2008-09-30 20:39:50 +00:00
|
|
|
c->block_preview = 20;
|
2005-01-06 22:31:58 +00:00
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
|
2009-07-13 16:37:27 +00:00
|
|
|
(v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft())) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (v->owner == c->index && v->engine_type == index) {
|
2005-01-06 22:31:58 +00:00
|
|
|
/* The user did prove me wrong, so restore old value */
|
2008-09-30 20:39:50 +00:00
|
|
|
c->block_preview = block_preview;
|
2005-01-06 22:31:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-28 17:06:22 +00:00
|
|
|
e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
|
2007-02-06 11:11:12 +00:00
|
|
|
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
|
2004-08-23 07:50:01 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Now available for all companies */
|
|
|
|
e->company_avail = (CompanyMask)-1;
|
2004-08-23 07:50:01 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Do not introduce new rail wagons */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (IsWagon(index)) return;
|
2004-08-23 07:50:01 +00:00
|
|
|
|
2008-01-08 18:16:26 +00:00
|
|
|
if (e->type == VEH_TRAIN) {
|
2007-02-23 18:55:07 +00:00
|
|
|
/* maybe make another rail type available */
|
2008-04-29 21:31:29 +00:00
|
|
|
RailType railtype = e->u.rail.railtype;
|
2007-01-24 07:14:09 +00:00
|
|
|
assert(railtype < RAILTYPE_END);
|
2008-09-30 20:39:50 +00:00
|
|
|
FOR_ALL_COMPANIES(c) SetBit(c->avail_railtypes, railtype);
|
2008-01-08 18:16:26 +00:00
|
|
|
} else if (e->type == VEH_ROAD) {
|
2007-05-25 08:47:40 +00:00
|
|
|
/* maybe make another road type available */
|
2008-09-30 20:39:50 +00:00
|
|
|
FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
|
2007-05-25 08:47:40 +00:00
|
|
|
}
|
2008-05-13 10:17:04 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::BroadcastNewEvent(new AIEventEngineAvailable(index));
|
|
|
|
|
2008-05-13 10:17:04 +00:00
|
|
|
SetDParam(0, GetEngineCategoryName(index));
|
|
|
|
SetDParam(1, index);
|
2009-08-04 18:04:33 +00:00
|
|
|
AddNewsItem(STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, NR_ENGINE, index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void EnginesMonthlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-08 21:48:59 +00:00
|
|
|
if (_cur_year < _year_engine_aging_stops) {
|
2008-01-24 10:47:44 +00:00
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Age the vehicle */
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((e->flags & ENGINE_AVAILABLE) && e->age != 0xFFFF) {
|
2004-08-09 17:04:08 +00:00
|
|
|
e->age++;
|
|
|
|
CalcEngineReliability(e);
|
|
|
|
}
|
|
|
|
|
2009-01-13 22:58:03 +00:00
|
|
|
if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Introduce it to all companies */
|
2004-08-09 17:04:08 +00:00
|
|
|
NewVehicleAvailable(e);
|
2009-03-14 18:16:29 +00:00
|
|
|
} else if (!(e->flags & (ENGINE_AVAILABLE | ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Introduction date has passed.. show introducing dialog to one companies. */
|
2007-02-28 17:06:22 +00:00
|
|
|
e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
|
2004-09-15 06:51:23 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Do not introduce new rail wagons */
|
2008-04-29 21:31:29 +00:00
|
|
|
if (!IsWagon(e->index))
|
2008-09-30 20:39:50 +00:00
|
|
|
e->preview_company_rank = 1; // Give to the company with the highest rating.
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-27 20:53:25 +00:00
|
|
|
static bool IsUniqueEngineName(const char *name)
|
|
|
|
{
|
2008-04-29 21:31:29 +00:00
|
|
|
const Engine *e;
|
2009-01-10 15:54:07 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
FOR_ALL_ENGINES(e) {
|
2009-01-10 15:54:07 +00:00
|
|
|
if (e->name != NULL && strcmp(e->name, name) == 0) return false;
|
2007-06-27 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/** Rename an engine.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perfom
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 engine ID to rename
|
|
|
|
* @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
|
2005-05-11 00:00:27 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-18 19:32:16 +00:00
|
|
|
Engine *e = Engine::GetIfValid(p1);
|
|
|
|
if (e == NULL) return CMD_ERROR;
|
2007-06-27 20:53:25 +00:00
|
|
|
|
2008-12-28 14:37:19 +00:00
|
|
|
bool reset = StrEmpty(text);
|
2008-09-15 19:02:50 +00:00
|
|
|
|
|
|
|
if (!reset) {
|
2008-12-28 14:37:19 +00:00
|
|
|
if (strlen(text) >= MAX_LENGTH_ENGINE_NAME_BYTES) return CMD_ERROR;
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!IsUniqueEngineName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
2008-09-15 19:02:50 +00:00
|
|
|
}
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-01-12 19:58:06 +00:00
|
|
|
free(e->name);
|
2008-09-15 19:02:50 +00:00
|
|
|
|
|
|
|
if (reset) {
|
|
|
|
e->name = NULL;
|
|
|
|
} else {
|
2008-12-28 14:37:19 +00:00
|
|
|
e->name = strdup(text);
|
2008-09-15 19:02:50 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-04 11:28:12 +00:00
|
|
|
/** Check if an engine is buildable.
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param engine index of the engine to check.
|
|
|
|
* @param type the type the engine should be.
|
|
|
|
* @param company index of the company.
|
2008-02-04 11:28:12 +00:00
|
|
|
* @return True if an engine is valid, of the specified type, and buildable by
|
2008-09-30 20:39:50 +00:00
|
|
|
* the given company.
|
2006-01-12 15:52:18 +00:00
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
2009-05-18 19:32:16 +00:00
|
|
|
const Engine *e = Engine::GetIfValid(engine);
|
2006-01-12 15:52:18 +00:00
|
|
|
|
2009-05-18 19:32:16 +00:00
|
|
|
/* check if it's an engine that is in the engine array */
|
|
|
|
if (e == NULL) return false;
|
2006-01-12 15:52:18 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* check if it's an engine of specified type */
|
2006-01-12 15:52:18 +00:00
|
|
|
if (e->type != type) return false;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* check if it's available */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!HasBit(e->company_avail, company)) return false;
|
2006-01-12 15:52:18 +00:00
|
|
|
|
2007-04-26 07:24:19 +00:00
|
|
|
if (type == VEH_TRAIN) {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Check if the rail type is available to this company */
|
2009-05-16 23:34:14 +00:00
|
|
|
const Company *c = Company::Get(company);
|
2009-10-06 19:17:07 +00:00
|
|
|
if (((GetRailTypeInfo(e->u.rail.railtype))->compatible_railtypes & c->avail_railtypes) == 0) return false;
|
2007-04-26 07:24:19 +00:00
|
|
|
}
|
|
|
|
|
2006-01-12 15:52:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-16 22:02:12 +00:00
|
|
|
/**
|
|
|
|
* Check if an engine is refittable.
|
2009-02-27 20:40:39 +00:00
|
|
|
* Note: Likely you want to use IsArticulatedVehicleRefittable().
|
2008-12-16 22:02:12 +00:00
|
|
|
* @param engine index of the engine to check.
|
|
|
|
* @return true if the engine is refittable.
|
|
|
|
*/
|
|
|
|
bool IsEngineRefittable(EngineID engine)
|
|
|
|
{
|
2009-05-18 19:32:16 +00:00
|
|
|
const Engine *e = Engine::GetIfValid(engine);
|
2008-12-16 22:02:12 +00:00
|
|
|
|
2009-05-18 19:32:16 +00:00
|
|
|
/* check if it's an engine that is in the engine array */
|
|
|
|
if (e == NULL) return false;
|
2008-12-16 22:02:12 +00:00
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
if (!e->CanCarryCargo()) return false;
|
|
|
|
|
2008-12-16 22:02:12 +00:00
|
|
|
const EngineInfo *ei = &e->info;
|
|
|
|
if (ei->refit_mask == 0) return false;
|
|
|
|
|
|
|
|
/* Are there suffixes?
|
|
|
|
* Note: This does not mean the suffixes are actually available for every consist at any time. */
|
2009-09-14 12:22:57 +00:00
|
|
|
if (HasBit(ei->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
|
2008-12-16 22:02:12 +00:00
|
|
|
|
|
|
|
/* Is there any cargo except the default cargo? */
|
2009-02-21 12:52:41 +00:00
|
|
|
CargoID default_cargo = e->GetDefaultCargoType();
|
2008-12-16 22:02:12 +00:00
|
|
|
return default_cargo != CT_INVALID && ei->refit_mask != 1U << default_cargo;
|
|
|
|
}
|