mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-08 15:30:00 +00:00
(svn r24277) -Codechange: Store cargo and railtype translation tables in a SmallVector.
This commit is contained in:
parent
b8f6b300d6
commit
522e5ec86b
@ -953,7 +953,7 @@ static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop
|
|||||||
case 0x05: { // Track type
|
case 0x05: { // Track type
|
||||||
uint8 tracktype = buf->ReadByte();
|
uint8 tracktype = buf->ReadByte();
|
||||||
|
|
||||||
if (tracktype < _cur.grffile->railtype_max) {
|
if (tracktype < _cur.grffile->railtype_list.Length()) {
|
||||||
_gted[e->index].railtypelabel = _cur.grffile->railtype_list[tracktype];
|
_gted[e->index].railtypelabel = _cur.grffile->railtype_list[tracktype];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1089,7 +1089,7 @@ static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_cur.grffile->railtype_max == 0) {
|
if (_cur.grffile->railtype_list.Length() == 0) {
|
||||||
/* Use traction type to select between normal and electrified
|
/* Use traction type to select between normal and electrified
|
||||||
* rail only when no translation list is in place. */
|
* rail only when no translation list is in place. */
|
||||||
if (_gted[e->index].railtypelabel == RAILTYPE_RAIL_LABEL && engclass >= EC_ELECTRIC) _gted[e->index].railtypelabel = RAILTYPE_ELECTRIC_LABEL;
|
if (_gted[e->index].railtypelabel == RAILTYPE_RAIL_LABEL && engclass >= EC_ELECTRIC) _gted[e->index].railtypelabel = RAILTYPE_ELECTRIC_LABEL;
|
||||||
@ -2449,9 +2449,8 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By
|
|||||||
return CIR_INVALID_ID;
|
return CIR_INVALID_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(_cur.grffile->cargo_list);
|
_cur.grffile->cargo_list.Clear();
|
||||||
_cur.grffile->cargo_max = numinfo;
|
_cur.grffile->cargo_list.Append(numinfo);
|
||||||
_cur.grffile->cargo_list = MallocT<CargoLabel>(numinfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CargoLabel cl = buf->ReadDWord();
|
CargoLabel cl = buf->ReadDWord();
|
||||||
@ -2578,9 +2577,8 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By
|
|||||||
return CIR_INVALID_ID;
|
return CIR_INVALID_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(_cur.grffile->railtype_list);
|
_cur.grffile->railtype_list.Clear();
|
||||||
_cur.grffile->railtype_max = numinfo;
|
_cur.grffile->railtype_list.Append(numinfo);
|
||||||
_cur.grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RailTypeLabel rtl = buf->ReadDWord();
|
RailTypeLabel rtl = buf->ReadDWord();
|
||||||
@ -2679,9 +2677,8 @@ static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, B
|
|||||||
return CIR_INVALID_ID;
|
return CIR_INVALID_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(_cur.grffile->cargo_list);
|
_cur.grffile->cargo_list.Clear();
|
||||||
_cur.grffile->cargo_max = numinfo;
|
_cur.grffile->cargo_list.Append(numinfo);
|
||||||
_cur.grffile->cargo_list = MallocT<CargoLabel>(numinfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CargoLabel cl = buf->ReadDWord();
|
CargoLabel cl = buf->ReadDWord();
|
||||||
@ -2719,9 +2716,8 @@ static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, B
|
|||||||
return CIR_INVALID_ID;
|
return CIR_INVALID_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(_cur.grffile->railtype_list);
|
_cur.grffile->railtype_list.Clear();
|
||||||
_cur.grffile->railtype_max = numinfo;
|
_cur.grffile->railtype_list.Append(numinfo);
|
||||||
_cur.grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RailTypeLabel rtl = buf->ReadDWord();
|
RailTypeLabel rtl = buf->ReadDWord();
|
||||||
@ -4725,7 +4721,7 @@ static CargoID TranslateCargo(uint8 feature, uint8 ctype)
|
|||||||
if (feature == GSF_STATIONS && ctype == 0xFE) return CT_DEFAULT_NA;
|
if (feature == GSF_STATIONS && ctype == 0xFE) return CT_DEFAULT_NA;
|
||||||
if (ctype == 0xFF) return CT_PURCHASE;
|
if (ctype == 0xFF) return CT_PURCHASE;
|
||||||
|
|
||||||
if (_cur.grffile->cargo_max == 0) {
|
if (_cur.grffile->cargo_list.Length() == 0) {
|
||||||
/* No cargo table, so use bitnum values */
|
/* No cargo table, so use bitnum values */
|
||||||
if (ctype >= 32) {
|
if (ctype >= 32) {
|
||||||
grfmsg(1, "TranslateCargo: Cargo bitnum %d out of range (max 31), skipping.", ctype);
|
grfmsg(1, "TranslateCargo: Cargo bitnum %d out of range (max 31), skipping.", ctype);
|
||||||
@ -4745,8 +4741,8 @@ static CargoID TranslateCargo(uint8 feature, uint8 ctype)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check if the cargo type is out of bounds of the cargo translation table */
|
/* Check if the cargo type is out of bounds of the cargo translation table */
|
||||||
if (ctype >= _cur.grffile->cargo_max) {
|
if (ctype >= _cur.grffile->cargo_list.Length()) {
|
||||||
grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur.grffile->cargo_max - 1);
|
grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur.grffile->cargo_list.Length() - 1);
|
||||||
return CT_INVALID;
|
return CT_INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -8036,17 +8032,13 @@ static void BuildCargoTranslationMap()
|
|||||||
const CargoSpec *cs = CargoSpec::Get(c);
|
const CargoSpec *cs = CargoSpec::Get(c);
|
||||||
if (!cs->IsValid()) continue;
|
if (!cs->IsValid()) continue;
|
||||||
|
|
||||||
if (_cur.grffile->cargo_max == 0) {
|
if (_cur.grffile->cargo_list.Length() == 0) {
|
||||||
/* Default translation table, so just a straight mapping to bitnum */
|
/* Default translation table, so just a straight mapping to bitnum */
|
||||||
_cur.grffile->cargo_map[c] = cs->bitnum;
|
_cur.grffile->cargo_map[c] = cs->bitnum;
|
||||||
} else {
|
} else {
|
||||||
/* Check the translation table for this cargo's label */
|
/* Check the translation table for this cargo's label */
|
||||||
for (uint i = 0; i < _cur.grffile->cargo_max; i++) {
|
int index = _cur.grffile->cargo_list.FindIndex(cs->label);
|
||||||
if (cs->label == _cur.grffile->cargo_list[i]) {
|
if (index >= 0) _cur.grffile->cargo_map[c] = index;
|
||||||
_cur.grffile->cargo_map[c] = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -8107,8 +8099,6 @@ GRFFile::GRFFile(const GRFConfig *config)
|
|||||||
GRFFile::~GRFFile()
|
GRFFile::~GRFFile()
|
||||||
{
|
{
|
||||||
free(this->filename);
|
free(this->filename);
|
||||||
free(this->cargo_list);
|
|
||||||
free(this->railtype_list);
|
|
||||||
delete[] this->language_map;
|
delete[] this->language_map;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -8219,7 +8209,7 @@ static void CalculateRefitMasks()
|
|||||||
{
|
{
|
||||||
const GRFFile *file = _gted[engine].defaultcargo_grf;
|
const GRFFile *file = _gted[engine].defaultcargo_grf;
|
||||||
if (file == NULL) file = e->GetGRF();
|
if (file == NULL) file = e->GetGRF();
|
||||||
if (file != NULL && file->grf_version >= 8 && file->cargo_max != 0) {
|
if (file != NULL && file->grf_version >= 8 && file->cargo_list.Length() != 0) {
|
||||||
cargo_map_for_first_refittable = file->cargo_map;
|
cargo_map_for_first_refittable = file->cargo_map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
#include "fileio_type.h"
|
#include "fileio_type.h"
|
||||||
#include "core/bitmath_func.hpp"
|
#include "core/bitmath_func.hpp"
|
||||||
#include "core/alloc_type.hpp"
|
#include "core/alloc_type.hpp"
|
||||||
|
#include "core/smallvec_type.hpp"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of different canal 'features'.
|
* List of different canal 'features'.
|
||||||
@ -121,12 +122,10 @@ struct GRFFile : ZeroedMemoryAllocator {
|
|||||||
|
|
||||||
GRFLabel *label; ///< Pointer to the first label. This is a linked list, not an array.
|
GRFLabel *label; ///< Pointer to the first label. This is a linked list, not an array.
|
||||||
|
|
||||||
uint8 cargo_max;
|
SmallVector<CargoLabel, 4> cargo_list; ///< Cargo translation table (local ID -> label)
|
||||||
CargoLabel *cargo_list;
|
uint8 cargo_map[NUM_CARGO]; ///< Inverse cargo translation table (CargoID -> local ID)
|
||||||
uint8 cargo_map[NUM_CARGO];
|
|
||||||
|
|
||||||
uint8 railtype_max;
|
SmallVector<RailTypeLabel, 4> railtype_list; ///< Railtype translation table
|
||||||
RailTypeLabel *railtype_list;
|
|
||||||
RailType railtype_map[RAILTYPE_END];
|
RailType railtype_map[RAILTYPE_END];
|
||||||
|
|
||||||
CanalProperties canal_local_properties[CF_END]; ///< Canal properties as set by this NewGRF
|
CanalProperties canal_local_properties[CF_END]; ///< Canal properties as set by this NewGRF
|
||||||
|
@ -116,10 +116,10 @@ CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
|
|||||||
|
|
||||||
/* Other cases use (possibly translated) cargobits */
|
/* Other cases use (possibly translated) cargobits */
|
||||||
|
|
||||||
if (grffile->cargo_max > 0) {
|
if (grffile->cargo_list.Length() > 0) {
|
||||||
/* ...and the cargo is in bounds, then get the cargo ID for
|
/* ...and the cargo is in bounds, then get the cargo ID for
|
||||||
* the label */
|
* the label */
|
||||||
if (cargo < grffile->cargo_max) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
|
if (cargo < grffile->cargo_list.Length()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
|
||||||
} else {
|
} else {
|
||||||
/* Else the cargo value is a 'climate independent' 'bitnum' */
|
/* Else the cargo value is a 'climate independent' 'bitnum' */
|
||||||
return GetCargoIDByBitnum(cargo);
|
return GetCargoIDByBitnum(cargo);
|
||||||
|
@ -130,13 +130,12 @@ SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSp
|
|||||||
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
|
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
|
||||||
{
|
{
|
||||||
/* No rail type table present, return rail type as-is */
|
/* No rail type table present, return rail type as-is */
|
||||||
if (grffile == NULL || grffile->railtype_max == 0) return railtype;
|
if (grffile == NULL || grffile->railtype_list.Length() == 0) return railtype;
|
||||||
|
|
||||||
/* Look for a matching rail type label in the table */
|
/* Look for a matching rail type label in the table */
|
||||||
RailTypeLabel label = GetRailTypeInfo(railtype)->label;
|
RailTypeLabel label = GetRailTypeInfo(railtype)->label;
|
||||||
for (uint i = 0; i < grffile->railtype_max; i++) {
|
int index = grffile->railtype_list.FindIndex(label);
|
||||||
if (label == grffile->railtype_list[i]) return i;
|
if (index >= 0) return index;
|
||||||
}
|
|
||||||
|
|
||||||
/* If not found, return as invalid */
|
/* If not found, return as invalid */
|
||||||
return 0xFF;
|
return 0xFF;
|
||||||
|
Loading…
Reference in New Issue
Block a user