2009-08-21 21:21:05 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
2011-12-15 21:56:00 +00:00
|
|
|
/** @file cargotype.cpp Implementation of cargoes. */
|
2007-02-23 11:50:43 +00:00
|
|
|
|
2007-02-20 22:09:21 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "cargotype.h"
|
2023-11-25 20:20:35 +00:00
|
|
|
#include "core/geometry_func.hpp"
|
2009-12-05 16:00:58 +00:00
|
|
|
#include "newgrf_cargo.h"
|
2011-04-30 15:24:23 +01:00
|
|
|
#include "string_func.h"
|
2010-04-01 20:48:28 +01:00
|
|
|
#include "strings_func.h"
|
2022-11-02 17:31:10 +00:00
|
|
|
#include "settings_type.h"
|
2007-02-20 22:09:21 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/strings.h"
|
2007-02-20 22:09:21 +00:00
|
|
|
#include "table/cargo_const.h"
|
|
|
|
|
2014-04-23 21:13:33 +01:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-07-16 21:40:06 +01:00
|
|
|
CargoSpec CargoSpec::array[NUM_CARGO];
|
2024-01-07 19:32:39 +00:00
|
|
|
std::array<std::vector<const CargoSpec *>, NUM_TPE> CargoSpec::town_production_cargoes{};
|
2007-02-20 22:09:21 +00:00
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
2012-12-23 22:12:52 +00:00
|
|
|
* Bitmask of cargo types available. This includes phony cargoes like regearing cargoes.
|
2009-12-05 21:39:28 +00:00
|
|
|
* Initialized during a call to #SetupCargoForClimate.
|
|
|
|
*/
|
2018-05-21 22:08:39 +01:00
|
|
|
CargoTypes _cargo_mask;
|
2007-02-22 22:09:51 +00:00
|
|
|
|
2012-12-23 22:12:52 +00:00
|
|
|
/**
|
|
|
|
* Bitmask of real cargo types available. Phony cargoes like regearing cargoes are excluded.
|
|
|
|
*/
|
2018-05-21 22:08:39 +01:00
|
|
|
CargoTypes _standard_cargo_mask;
|
2012-12-23 22:12:52 +00:00
|
|
|
|
2024-02-04 10:16:08 +00:00
|
|
|
/**
|
|
|
|
* List of default cargo labels, used when setting up cargo types for default vehicles.
|
|
|
|
* This is done by label so that a cargo label can be redefined in a different slot.
|
|
|
|
*/
|
|
|
|
static std::vector<CargoLabel> _default_cargo_labels;
|
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
|
|
|
* Set up the default cargo types for the given landscape type.
|
2009-12-05 21:39:28 +00:00
|
|
|
* @param l Landscape
|
|
|
|
*/
|
2007-02-20 22:09:21 +00:00
|
|
|
void SetupCargoForClimate(LandscapeID l)
|
|
|
|
{
|
|
|
|
assert(l < lengthof(_default_climate_cargo));
|
|
|
|
|
2007-02-22 22:09:51 +00:00
|
|
|
_cargo_mask = 0;
|
2024-02-04 10:16:08 +00:00
|
|
|
_default_cargo_labels.clear();
|
2007-02-22 22:09:51 +00:00
|
|
|
|
2023-09-17 17:53:13 +01:00
|
|
|
/* Copy from default cargo by label or index. */
|
|
|
|
auto insert = std::begin(CargoSpec::array);
|
2024-02-04 10:16:08 +00:00
|
|
|
for (const auto &cl : _default_climate_cargo[l]) {
|
2007-02-20 22:09:21 +00:00
|
|
|
|
2023-09-17 17:53:13 +01:00
|
|
|
/* Check if value is an index into the cargo table */
|
2024-02-04 10:16:08 +00:00
|
|
|
if (std::holds_alternative<int>(cl)) {
|
2023-09-17 17:53:13 +01:00
|
|
|
/* Copy the default cargo by index. */
|
2024-02-04 10:16:08 +00:00
|
|
|
*insert = _default_cargo[std::get<int>(cl)];
|
2023-09-17 17:53:13 +01:00
|
|
|
} else {
|
|
|
|
/* Search for label in default cargo types and copy if found. */
|
2024-02-04 10:16:08 +00:00
|
|
|
CargoLabel label = std::get<CargoLabel>(cl);
|
|
|
|
auto found = std::find_if(std::begin(_default_cargo), std::end(_default_cargo), [&label](const CargoSpec &cs) { return cs.label == label; });
|
2023-09-17 17:53:13 +01:00
|
|
|
if (found != std::end(_default_cargo)) {
|
|
|
|
*insert = *found;
|
|
|
|
} else {
|
|
|
|
/* Index or label is invalid, this should not happen. */
|
|
|
|
NOT_REACHED();
|
2007-02-20 22:09:21 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-17 17:53:13 +01:00
|
|
|
|
2024-02-04 10:16:08 +00:00
|
|
|
if (insert->IsValid()) {
|
|
|
|
SetBit(_cargo_mask, insert->Index());
|
|
|
|
_default_cargo_labels.push_back(insert->label);
|
|
|
|
}
|
2023-09-17 17:53:13 +01:00
|
|
|
++insert;
|
2007-02-20 22:09:21 +00:00
|
|
|
}
|
2023-09-17 17:53:13 +01:00
|
|
|
|
|
|
|
/* Reset and disable remaining cargo types. */
|
|
|
|
std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
|
2024-02-04 10:16:08 +00:00
|
|
|
|
|
|
|
BuildCargoLabelMap();
|
2007-02-20 22:09:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 20:20:35 +00:00
|
|
|
/**
|
2024-02-04 10:16:08 +00:00
|
|
|
* Build cargo label map.
|
|
|
|
* This is called multiple times during NewGRF initialization as cargos are defined, so that TranslateRefitMask() and
|
|
|
|
* GetCargoTranslation(), also used during initialization, get the correct information.
|
2023-11-25 20:20:35 +00:00
|
|
|
*/
|
2024-02-04 10:16:08 +00:00
|
|
|
void BuildCargoLabelMap()
|
2023-11-25 20:20:35 +00:00
|
|
|
{
|
2024-02-04 10:16:08 +00:00
|
|
|
CargoSpec::label_map.clear();
|
|
|
|
for (const CargoSpec &cs : CargoSpec::array) {
|
|
|
|
/* During initialization, CargoSpec can be marked valid before the label has been set. */
|
2024-03-23 20:18:31 +00:00
|
|
|
if (!cs.IsValid() || cs.label == CargoLabel{0} || cs.label == CT_INVALID) continue;
|
2024-02-04 10:16:08 +00:00
|
|
|
/* Label already exists, don't addd again. */
|
|
|
|
if (CargoSpec::label_map.count(cs.label) != 0) continue;
|
|
|
|
|
2024-04-20 02:46:39 +01:00
|
|
|
CargoSpec::label_map.emplace(cs.label, cs.Index());
|
2023-11-25 20:20:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 16:35:32 +01:00
|
|
|
/**
|
2024-02-04 10:16:08 +00:00
|
|
|
* Test if a cargo is a default cargo type.
|
|
|
|
* @param cid Cargo ID.
|
|
|
|
* @returns true iff the cargo type is a default cargo type.
|
2021-04-30 16:35:32 +01:00
|
|
|
*/
|
2024-02-04 10:16:08 +00:00
|
|
|
bool IsDefaultCargo(CargoID cid)
|
2021-04-30 16:35:32 +01:00
|
|
|
{
|
2024-02-04 10:16:08 +00:00
|
|
|
auto cs = CargoSpec::Get(cid);
|
|
|
|
if (!cs->IsValid()) return false;
|
2021-04-30 16:35:32 +01:00
|
|
|
|
2024-02-04 10:16:08 +00:00
|
|
|
CargoLabel label = cs->label;
|
|
|
|
return std::any_of(std::begin(_default_cargo_labels), std::end(_default_cargo_labels), [&label](const CargoLabel &cl) { return cl == label; });
|
2021-04-30 16:35:32 +01:00
|
|
|
}
|
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
2024-02-04 10:16:08 +00:00
|
|
|
* Get dimensions of largest cargo icon.
|
|
|
|
* @return Dimensions of largest cargo icon.
|
2009-12-05 21:39:28 +00:00
|
|
|
*/
|
2024-02-04 10:16:08 +00:00
|
|
|
Dimension GetLargestCargoIconSize()
|
2007-02-24 19:36:47 +00:00
|
|
|
{
|
2024-02-04 10:16:08 +00:00
|
|
|
Dimension size = {0, 0};
|
|
|
|
for (const CargoSpec *cs : _sorted_cargo_specs) {
|
|
|
|
size = maxdim(size, GetSpriteSize(cs->GetCargoIcon()));
|
2007-02-24 19:36:47 +00:00
|
|
|
}
|
2024-02-04 10:16:08 +00:00
|
|
|
return size;
|
2007-02-24 19:36:47 +00:00
|
|
|
}
|
2007-04-13 20:32:18 +01:00
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
|
|
|
* Find the CargoID of a 'bitnum' value.
|
2007-04-13 20:32:18 +01:00
|
|
|
* @param bitnum 'bitnum' to find.
|
2024-01-06 15:15:37 +00:00
|
|
|
* @return First CargoID with the given bitnum, or #INVALID_CARGO if not found or if the provided \a bitnum is invalid.
|
2007-04-13 20:32:18 +01:00
|
|
|
*/
|
2023-05-08 18:01:06 +01:00
|
|
|
CargoID GetCargoIDByBitnum(uint8_t bitnum)
|
2007-04-13 20:32:18 +01:00
|
|
|
{
|
2024-01-06 15:15:37 +00:00
|
|
|
if (bitnum == INVALID_CARGO_BITNUM) return INVALID_CARGO;
|
2007-04-13 20:32:18 +01:00
|
|
|
|
2021-04-29 16:51:05 +01:00
|
|
|
for (const CargoSpec *cs : CargoSpec::Iterate()) {
|
2009-07-16 21:40:06 +01:00
|
|
|
if (cs->bitnum == bitnum) return cs->Index();
|
2007-04-13 20:32:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No matching label was found, so it is invalid */
|
2024-01-06 15:15:37 +00:00
|
|
|
return INVALID_CARGO;
|
2007-04-13 20:32:18 +01:00
|
|
|
}
|
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
|
|
|
* Get sprite for showing cargo of this type.
|
2009-12-05 16:00:58 +00:00
|
|
|
* @return Sprite number to use.
|
|
|
|
*/
|
|
|
|
SpriteID CargoSpec::GetCargoIcon() const
|
|
|
|
{
|
|
|
|
SpriteID sprite = this->sprite;
|
|
|
|
if (sprite == 0xFFFF) {
|
|
|
|
/* A value of 0xFFFF indicates we should draw a custom icon */
|
|
|
|
sprite = GetCustomCargoSprite(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sprite == 0) sprite = SPR_CARGO_GOODS;
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2023-09-18 22:34:55 +01:00
|
|
|
std::array<uint8_t, NUM_CARGO> _sorted_cargo_types; ///< Sort order of cargoes by cargo ID.
|
2021-06-13 01:41:41 +01:00
|
|
|
std::vector<const CargoSpec *> _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name.
|
2024-01-16 21:46:00 +00:00
|
|
|
std::span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
|
2010-04-07 15:17:29 +01:00
|
|
|
|
2010-04-01 20:48:28 +01:00
|
|
|
/** Sort cargo specifications by their name. */
|
2019-04-12 16:47:13 +01:00
|
|
|
static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
|
2010-04-01 20:48:28 +01:00
|
|
|
{
|
2023-04-27 14:39:10 +01:00
|
|
|
std::string a_name = GetString(a->name);
|
|
|
|
std::string b_name = GetString(b->name);
|
2010-04-01 20:48:28 +01:00
|
|
|
|
2023-04-27 14:39:10 +01:00
|
|
|
int res = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
|
2010-04-01 20:48:28 +01:00
|
|
|
|
|
|
|
/* If the names are equal, sort by cargo bitnum. */
|
2019-04-12 16:47:13 +01:00
|
|
|
return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
|
2010-04-01 20:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort cargo specifications by their cargo class. */
|
2019-04-12 16:47:13 +01:00
|
|
|
static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
|
2010-04-01 20:48:28 +01:00
|
|
|
{
|
2019-04-12 16:47:13 +01:00
|
|
|
int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
|
2010-04-01 20:48:28 +01:00
|
|
|
if (res == 0) {
|
2019-04-12 16:47:13 +01:00
|
|
|
res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
|
2010-04-01 20:48:28 +01:00
|
|
|
if (res == 0) {
|
2019-04-12 16:47:13 +01:00
|
|
|
res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
|
2010-04-07 15:17:29 +01:00
|
|
|
if (res == 0) {
|
|
|
|
return CargoSpecNameSorter(a, b);
|
|
|
|
}
|
2010-04-01 20:48:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:47:13 +01:00
|
|
|
return res < 0;
|
2010-04-01 20:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize the list of sorted cargo specifications. */
|
|
|
|
void InitializeSortedCargoSpecs()
|
|
|
|
{
|
2024-01-07 19:32:39 +00:00
|
|
|
for (auto &tpc : CargoSpec::town_production_cargoes) tpc.clear();
|
2019-04-12 16:47:13 +01:00
|
|
|
_sorted_cargo_specs.clear();
|
2023-11-25 20:20:35 +00:00
|
|
|
/* Add each cargo spec to the list, and determine the largest cargo icon size. */
|
2021-04-29 16:51:05 +01:00
|
|
|
for (const CargoSpec *cargo : CargoSpec::Iterate()) {
|
2019-04-12 16:47:13 +01:00
|
|
|
_sorted_cargo_specs.push_back(cargo);
|
2010-04-01 20:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort cargo specifications by cargo class and name. */
|
2019-04-12 16:47:13 +01:00
|
|
|
std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
|
2010-04-07 15:17:29 +01:00
|
|
|
|
2023-09-18 22:34:55 +01:00
|
|
|
/* Populate */
|
|
|
|
for (auto it = std::begin(_sorted_cargo_specs); it != std::end(_sorted_cargo_specs); ++it) {
|
|
|
|
_sorted_cargo_types[(*it)->Index()] = static_cast<uint8_t>(it - std::begin(_sorted_cargo_specs));
|
|
|
|
}
|
|
|
|
|
2021-06-13 01:41:41 +01:00
|
|
|
/* Count the number of standard cargos and fill the mask. */
|
2012-12-23 22:12:52 +00:00
|
|
|
_standard_cargo_mask = 0;
|
2023-05-08 18:01:06 +01:00
|
|
|
uint8_t nb_standard_cargo = 0;
|
2021-04-28 21:50:46 +01:00
|
|
|
for (const auto &cargo : _sorted_cargo_specs) {
|
2024-01-07 19:32:39 +00:00
|
|
|
assert(cargo->town_production_effect != INVALID_TPE);
|
|
|
|
CargoSpec::town_production_cargoes[cargo->town_production_effect].push_back(cargo);
|
2010-04-07 15:17:29 +01:00
|
|
|
if (cargo->classes & CC_SPECIAL) break;
|
2021-06-13 01:41:41 +01:00
|
|
|
nb_standard_cargo++;
|
2012-12-23 22:12:52 +00:00
|
|
|
SetBit(_standard_cargo_mask, cargo->Index());
|
2010-04-07 15:17:29 +01:00
|
|
|
}
|
2021-06-13 01:41:41 +01:00
|
|
|
|
|
|
|
/* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
|
|
|
|
_sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
|
2010-04-01 20:48:28 +01:00
|
|
|
}
|
|
|
|
|
2023-05-08 18:01:06 +01:00
|
|
|
uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
|
2022-11-02 17:31:10 +00:00
|
|
|
{
|
|
|
|
if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
|
|
|
|
return this->WeightOfNUnits(n);
|
|
|
|
}
|