Update to 14.0-beta1

This commit is contained in:
dP
2024-02-04 02:18:17 +05:30
parent 79037e2c65
commit 33ef333b57
1325 changed files with 138465 additions and 70987 deletions

View File

@@ -9,6 +9,7 @@
#include "stdafx.h"
#include "cargotype.h"
#include "core/geometry_func.hpp"
#include "newgrf_cargo.h"
#include "string_func.h"
#include "strings_func.h"
@@ -21,6 +22,7 @@
#include "safeguards.h"
CargoSpec CargoSpec::array[NUM_CARGO];
std::array<std::vector<const CargoSpec *>, NUM_TPE> CargoSpec::town_production_cargoes{};
/**
* Bitmask of cargo types available. This includes phony cargoes like regearing cargoes.
@@ -41,54 +43,59 @@ void SetupCargoForClimate(LandscapeID l)
{
assert(l < lengthof(_default_climate_cargo));
/* Reset and disable all cargo types */
for (CargoID i = 0; i < lengthof(CargoSpec::array); i++) {
*CargoSpec::Get(i) = {};
CargoSpec::Get(i)->bitnum = INVALID_CARGO;
/* Set defaults for newer properties, which old GRFs do not know */
CargoSpec::Get(i)->multiplier = 0x100;
}
_cargo_mask = 0;
for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
CargoLabel cl = _default_climate_cargo[l][i];
/* Copy from default cargo by label or index. */
auto insert = std::begin(CargoSpec::array);
for (const CargoLabel &cl : _default_climate_cargo[l]) {
/* Bzzt: check if cl is just an index into the cargo table */
/* Check if value is an index into the cargo table */
if (cl < lengthof(_default_cargo)) {
/* Copy the indexed cargo */
CargoSpec *cargo = CargoSpec::Get(i);
*cargo = _default_cargo[cl];
if (cargo->bitnum != INVALID_CARGO) SetBit(_cargo_mask, i);
continue;
}
/* Loop through each of the default cargo types to see if
* the label matches */
for (uint j = 0; j < lengthof(_default_cargo); j++) {
if (_default_cargo[j].label == cl) {
*CargoSpec::Get(i) = _default_cargo[j];
/* Populate the available cargo mask */
SetBit(_cargo_mask, i);
break;
/* Copy the default cargo by index. */
*insert = _default_cargo[cl];
} else {
/* Search for label in default cargo types and copy if found. */
auto found = std::find_if(std::begin(_default_cargo), std::end(_default_cargo), [&cl](const CargoSpec &cs) { return cs.label == cl; });
if (found != std::end(_default_cargo)) {
*insert = *found;
} else {
/* Index or label is invalid, this should not happen. */
NOT_REACHED();
}
}
if (insert->IsValid()) SetBit(_cargo_mask, insert->Index());
++insert;
}
/* Reset and disable remaining cargo types. */
std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
}
/**
* Get dimensions of largest cargo icon.
* @return Dimensions of largest cargo icon.
*/
Dimension GetLargestCargoIconSize()
{
Dimension size = {0, 0};
for (const CargoSpec *cs : _sorted_cargo_specs) {
size = maxdim(size, GetSpriteSize(cs->GetCargoIcon()));
}
return size;
}
/**
* Get the cargo ID of a default cargo, if present.
* @param l Landscape
* @param ct Default cargo type.
* @return ID number if the cargo exists, else #CT_INVALID
* @return ID number if the cargo exists, else #INVALID_CARGO
*/
CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
{
assert(l < lengthof(_default_climate_cargo));
if (ct == CT_INVALID) return CT_INVALID;
if (!IsValidCargoType(ct)) return INVALID_CARGO;
assert(ct < lengthof(_default_climate_cargo[0]));
CargoLabel cl = _default_climate_cargo[l][ct];
@@ -103,7 +110,7 @@ CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
/**
* Get the cargo ID by cargo label.
* @param cl Cargo type to get.
* @return ID number if the cargo exists, else #CT_INVALID
* @return ID number if the cargo exists, else #INVALID_CARGO
*/
CargoID GetCargoIDByLabel(CargoLabel cl)
{
@@ -112,25 +119,25 @@ CargoID GetCargoIDByLabel(CargoLabel cl)
}
/* No matching label was found, so it is invalid */
return CT_INVALID;
return INVALID_CARGO;
}
/**
* Find the CargoID of a 'bitnum' value.
* @param bitnum 'bitnum' to find.
* @return First CargoID with the given bitnum, or #CT_INVALID if not found or if the provided \a bitnum is invalid.
* @return First CargoID with the given bitnum, or #INVALID_CARGO if not found or if the provided \a bitnum is invalid.
*/
CargoID GetCargoIDByBitnum(uint8 bitnum)
CargoID GetCargoIDByBitnum(uint8_t bitnum)
{
if (bitnum == INVALID_CARGO) return CT_INVALID;
if (bitnum == INVALID_CARGO_BITNUM) return INVALID_CARGO;
for (const CargoSpec *cs : CargoSpec::Iterate()) {
if (cs->bitnum == bitnum) return cs->Index();
}
/* No matching label was found, so it is invalid */
return CT_INVALID;
return INVALID_CARGO;
}
/**
@@ -150,19 +157,17 @@ SpriteID CargoSpec::GetCargoIcon() const
return sprite;
}
std::array<uint8_t, NUM_CARGO> _sorted_cargo_types; ///< Sort order of cargoes by cargo ID.
std::vector<const CargoSpec *> _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name.
span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
std::span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
/** Sort cargo specifications by their name. */
static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
{
static char a_name[64];
static char b_name[64];
std::string a_name = GetString(a->name);
std::string b_name = GetString(b->name);
GetString(a_name, a->name, lastof(a_name));
GetString(b_name, b->name, lastof(b_name));
int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
int res = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
/* If the names are equal, sort by cargo bitnum. */
return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
@@ -188,8 +193,9 @@ static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * c
/** Initialize the list of sorted cargo specifications. */
void InitializeSortedCargoSpecs()
{
for (auto &tpc : CargoSpec::town_production_cargoes) tpc.clear();
_sorted_cargo_specs.clear();
/* Add each cargo spec to the list. */
/* Add each cargo spec to the list, and determine the largest cargo icon size. */
for (const CargoSpec *cargo : CargoSpec::Iterate()) {
_sorted_cargo_specs.push_back(cargo);
}
@@ -197,10 +203,17 @@ void InitializeSortedCargoSpecs()
/* Sort cargo specifications by cargo class and name. */
std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
/* 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));
}
/* Count the number of standard cargos and fill the mask. */
_standard_cargo_mask = 0;
uint8 nb_standard_cargo = 0;
uint8_t nb_standard_cargo = 0;
for (const auto &cargo : _sorted_cargo_specs) {
assert(cargo->town_production_effect != INVALID_TPE);
CargoSpec::town_production_cargoes[cargo->town_production_effect].push_back(cargo);
if (cargo->classes & CC_SPECIAL) break;
nb_standard_cargo++;
SetBit(_standard_cargo_mask, cargo->Index());
@@ -210,7 +223,7 @@ void InitializeSortedCargoSpecs()
_sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
}
uint64 CargoSpec::WeightOfNUnitsInTrain(uint32 n) const
uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
{
if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
return this->WeightOfNUnits(n);