(svn r24277) -Codechange: Store cargo and railtype translation tables in a SmallVector.

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
frosch 12 years ago
parent b8f6b300d6
commit 522e5ec86b

@ -953,7 +953,7 @@ static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop
case 0x05: { // Track type
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];
break;
}
@ -1089,7 +1089,7 @@ static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop
break;
}
if (_cur.grffile->railtype_max == 0) {
if (_cur.grffile->railtype_list.Length() == 0) {
/* Use traction type to select between normal and electrified
* 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;
@ -2449,9 +2449,8 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By
return CIR_INVALID_ID;
}
free(_cur.grffile->cargo_list);
_cur.grffile->cargo_max = numinfo;
_cur.grffile->cargo_list = MallocT<CargoLabel>(numinfo);
_cur.grffile->cargo_list.Clear();
_cur.grffile->cargo_list.Append(numinfo);
}
CargoLabel cl = buf->ReadDWord();
@ -2578,9 +2577,8 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By
return CIR_INVALID_ID;
}
free(_cur.grffile->railtype_list);
_cur.grffile->railtype_max = numinfo;
_cur.grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
_cur.grffile->railtype_list.Clear();
_cur.grffile->railtype_list.Append(numinfo);
}
RailTypeLabel rtl = buf->ReadDWord();
@ -2679,9 +2677,8 @@ static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, B
return CIR_INVALID_ID;
}
free(_cur.grffile->cargo_list);
_cur.grffile->cargo_max = numinfo;
_cur.grffile->cargo_list = MallocT<CargoLabel>(numinfo);
_cur.grffile->cargo_list.Clear();
_cur.grffile->cargo_list.Append(numinfo);
}
CargoLabel cl = buf->ReadDWord();
@ -2719,9 +2716,8 @@ static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, B
return CIR_INVALID_ID;
}
free(_cur.grffile->railtype_list);
_cur.grffile->railtype_max = numinfo;
_cur.grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
_cur.grffile->railtype_list.Clear();
_cur.grffile->railtype_list.Append(numinfo);
}
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 (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 */
if (ctype >= 32) {
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 */
if (ctype >= _cur.grffile->cargo_max) {
grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur.grffile->cargo_max - 1);
if (ctype >= _cur.grffile->cargo_list.Length()) {
grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur.grffile->cargo_list.Length() - 1);
return CT_INVALID;
}
@ -8036,17 +8032,13 @@ static void BuildCargoTranslationMap()
const CargoSpec *cs = CargoSpec::Get(c);
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 */
_cur.grffile->cargo_map[c] = cs->bitnum;
} else {
/* Check the translation table for this cargo's label */
for (uint i = 0; i < _cur.grffile->cargo_max; i++) {
if (cs->label == _cur.grffile->cargo_list[i]) {
_cur.grffile->cargo_map[c] = i;
break;
}
}
int index = _cur.grffile->cargo_list.FindIndex(cs->label);
if (index >= 0) _cur.grffile->cargo_map[c] = index;
}
}
}
@ -8107,8 +8099,6 @@ GRFFile::GRFFile(const GRFConfig *config)
GRFFile::~GRFFile()
{
free(this->filename);
free(this->cargo_list);
free(this->railtype_list);
delete[] this->language_map;
}
@ -8219,7 +8209,7 @@ static void CalculateRefitMasks()
{
const GRFFile *file = _gted[engine].defaultcargo_grf;
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;
}
}

@ -17,6 +17,7 @@
#include "fileio_type.h"
#include "core/bitmath_func.hpp"
#include "core/alloc_type.hpp"
#include "core/smallvec_type.hpp"
/**
* 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.
uint8 cargo_max;
CargoLabel *cargo_list;
uint8 cargo_map[NUM_CARGO];
SmallVector<CargoLabel, 4> cargo_list; ///< Cargo translation table (local ID -> label)
uint8 cargo_map[NUM_CARGO]; ///< Inverse cargo translation table (CargoID -> local ID)
uint8 railtype_max;
RailTypeLabel *railtype_list;
SmallVector<RailTypeLabel, 4> railtype_list; ///< Railtype translation table
RailType railtype_map[RAILTYPE_END];
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 */
if (grffile->cargo_max > 0) {
if (grffile->cargo_list.Length() > 0) {
/* ...and the cargo is in bounds, then get the cargo ID for
* 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 the cargo value is a 'climate independent' 'bitnum' */
return GetCargoIDByBitnum(cargo);

@ -130,13 +130,12 @@ SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSp
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
{
/* 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 */
RailTypeLabel label = GetRailTypeInfo(railtype)->label;
for (uint i = 0; i < grffile->railtype_max; i++) {
if (label == grffile->railtype_list[i]) return i;
}
int index = grffile->railtype_list.FindIndex(label);
if (index >= 0) return index;
/* If not found, return as invalid */
return 0xFF;

Loading…
Cancel
Save