(svn r16678) -Codechange: Turn CargoArray into a class, so one does not have to deal with sizeof() wrt. typedef-ed arrays.

This commit is contained in:
frosch 2009-06-27 21:06:58 +00:00
parent 9b070b5405
commit a288e4d82f
14 changed files with 59 additions and 53 deletions

View File

@ -179,8 +179,7 @@
{
if (!::IsValidTile(tile) || width <= 0 || height <= 0 || radius <= 0) return -1;
CargoArray acceptance;
::GetAcceptanceAroundTiles(acceptance, tile, width, height, _settings_game.station.modified_catchment ? radius : (int)CA_UNMODIFIED);
CargoArray acceptance = ::GetAcceptanceAroundTiles(tile, width, height, _settings_game.station.modified_catchment ? radius : (int)CA_UNMODIFIED);
return acceptance[cargo_type];
}
@ -188,8 +187,7 @@
{
if (!::IsValidTile(tile) || width <= 0 || height <= 0 || radius <= 0) return -1;
CargoArray produced;
::GetProductionAroundTiles(produced, tile, width, height, _settings_game.station.modified_catchment ? radius : (int)CA_UNMODIFIED);
CargoArray produced = ::GetProductionAroundTiles(tile, width, height, _settings_game.station.modified_catchment ? radius : (int)CA_UNMODIFIED);
return produced[cargo_type];
}

View File

@ -92,8 +92,7 @@ AITileList_IndustryAccepting::AITileList_IndustryAccepting(IndustryID industry_i
/* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
* industry triggers the acceptance). */
CargoArray acceptance;
::GetAcceptanceAroundTiles(acceptance, cur_tile, 1, 1, radius);
CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius);
{
bool cargo_accepts = false;
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
@ -130,8 +129,7 @@ AITileList_IndustryProducing::AITileList_IndustryProducing(IndustryID industry_i
/* Only add the tile if it produces the cargo (a bug in OpenTTD makes this
* inconsitance). */
CargoArray produced;
::GetProductionAroundTiles(produced, cur_tile, 1, 1, radius);
CargoArray produced = ::GetProductionAroundTiles(cur_tile, 1, 1, radius);
{
bool cargo_produces = false;
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {

View File

@ -55,7 +55,31 @@ enum CargoTypes {
CT_INVALID = 0xFF
};
/** Array for storing amounts of cargo */
typedef uint CargoArray[NUM_CARGO];
/** Class for storing amounts of cargo */
struct CargoArray {
private:
uint amount[NUM_CARGO];
public:
FORCEINLINE CargoArray()
{
this->Clear();
}
FORCEINLINE void Clear()
{
memset(this->amount, 0, sizeof(this->amount));
}
FORCEINLINE uint &operator[](CargoID cargo)
{
return this->amount[cargo];
}
FORCEINLINE const uint &operator[](CargoID cargo) const
{
return this->amount[cargo];
}
};
#endif /* CARGO_TYPE_H */

View File

@ -848,8 +848,6 @@ struct DepotWindow : Window {
if (v != NULL && mode == MODE_DRAG_VEHICLE) {
CargoArray capacity, loaded;
memset(capacity, 0, sizeof(capacity));
memset(loaded, 0, sizeof(loaded));
/* Display info for single (articulated) vehicle, or for whole chain starting with selected vehicle */
bool whole_chain = (this->type == VEH_TRAIN && _ctrl_pressed);

View File

@ -380,7 +380,7 @@ static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh)
return FlatteningFoundation(tileh);
}
static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray acceptance)
static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance)
{
IndustryGfx gfx = GetIndustryGfx(tile);
const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
@ -868,7 +868,7 @@ static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mod
return 0;
}
static void AddProducedCargo_Industry(TileIndex tile, CargoArray produced)
static void AddProducedCargo_Industry(TileIndex tile, CargoArray &produced)
{
const Industry *i = GetIndustryByTile(tile);

View File

@ -130,7 +130,6 @@ public:
td.grf = NULL;
CargoArray acceptance;
memset(acceptance, 0, sizeof(CargoArray));
AddAcceptedCargo(tile, acceptance);
GetTileDesc(tile, &td);
@ -811,7 +810,7 @@ void GuiShowTooltips(StringID str, uint paramcount, const uint64 params[], bool
}
static int DrawStationCoverageText(const CargoArray cargos,
static int DrawStationCoverageText(const CargoArray &cargos,
int str_x, int str_y, StationCoverageType sct, bool supplies)
{
bool first = true;
@ -863,12 +862,12 @@ static int DrawStationCoverageText(const CargoArray cargos,
int DrawStationCoverageAreaText(int sx, int sy, StationCoverageType sct, int rad, bool supplies)
{
TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
CargoArray cargos;
if (tile < MapSize()) {
CargoArray cargos;
if (supplies) {
GetProductionAroundTiles(cargos, tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE , rad);
cargos = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
} else {
GetAcceptanceAroundTiles(cargos, tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE , rad);
cargos = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
}
return DrawStationCoverageText(cargos, sx, sy, sct, supplies);
}

View File

@ -38,7 +38,6 @@ void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y)
StringID subtype_text[NUM_CARGO];
char capacity[512];
memset(max_cargo, 0, sizeof(max_cargo));
memset(subtype_text, 0, sizeof(subtype_text));
for (const Vehicle *u = v; u != NULL; u = u->Next()) {

View File

@ -434,17 +434,15 @@ static void ShowRejectOrAcceptNews(const Station *st, uint num_items, CargoID *c
}
/**
* Get a list of the cargo types being produced around the tile (in a rectangle).
* @param produced Destination array of produced cargo
* Get the cargo types being produced around the tile (in a rectangle).
* @param tile Northtile of area
* @param w X extent of the area
* @param h Y extent of the area
* @param rad Search radius in addition to the given area
*/
void GetProductionAroundTiles(CargoArray produced, TileIndex tile,
int w, int h, int rad)
CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad)
{
memset(produced, 0, sizeof(CargoArray)); // sizeof(CargoArray) != sizeof(produced) (== sizeof(uint *))
CargoArray produced;
int x = TileX(tile);
int y = TileY(tile);
@ -468,20 +466,20 @@ void GetProductionAroundTiles(CargoArray produced, TileIndex tile,
AddProducedCargo(tile, produced);
}
}
return produced;
}
/**
* Get a list of the cargo types that are accepted around the tile.
* @param accepts Destination array of accepted cargo
* Get the acceptance of cargos around the tile in 1/8.
* @param tile Center of the search area
* @param w X extent of area
* @param h Y extent of area
* @param rad Search radius in addition to given area
*/
void GetAcceptanceAroundTiles(CargoArray acceptance, TileIndex tile,
int w, int h, int rad)
CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad)
{
memset(acceptance, 0, sizeof(CargoArray)); // sizeof(CargoArray) != sizeof(acceptance) (== sizeof(uint *))
CargoArray acceptance;
int x = TileX(tile);
int y = TileY(tile);
@ -504,6 +502,8 @@ void GetAcceptanceAroundTiles(CargoArray acceptance, TileIndex tile,
AddAcceptedCargo(tile, acceptance);
}
}
return acceptance;
}
/** Update the acceptance for a station.
@ -521,15 +521,12 @@ static void UpdateStationAcceptance(Station *st, bool show_msg)
/* And retrieve the acceptance. */
CargoArray acceptance;
if (!st->rect.IsEmpty()) {
GetAcceptanceAroundTiles(
acceptance,
acceptance = GetAcceptanceAroundTiles(
TileXY(st->rect.left, st->rect.top),
st->rect.right - st->rect.left + 1,
st->rect.bottom - st->rect.top + 1,
st->GetCatchmentRadius()
);
} else {
memset(acceptance, 0, sizeof(acceptance));
}
/* Adjust in case our station only accepts fewer kinds of goods */

View File

@ -22,8 +22,8 @@ void FindStationsAroundTiles(TileIndex tile, int w_prod, int h_prod, StationList
void ShowStationViewWindow(StationID station);
void UpdateAllStationVirtCoord();
void GetProductionAroundTiles(CargoArray produced, TileIndex tile, int w, int h, int rad);
void GetAcceptanceAroundTiles(CargoArray acceptance, TileIndex tile, int w, int h, int rad);
CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad);
CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad);
const DrawTileSprites *GetStationTileLayout(StationType st, byte gfx);
void StationPickerDrawSprite(int x, int y, StationType st, RailType railtype, RoadType roadtype, int image);

View File

@ -74,7 +74,7 @@ typedef CommandCost ClearTileProc(TileIndex tile, DoCommandFlag flags);
* @param tile Tile queried for its accepted cargo
* @param acceptance Storage destination of the cargo acceptance in 1/8
*/
typedef void AddAcceptedCargoProc(TileIndex tile, CargoArray acceptance);
typedef void AddAcceptedCargoProc(TileIndex tile, CargoArray &acceptance);
/**
* Tile callback function signature for obtaining a tile description
@ -103,7 +103,7 @@ typedef TrackStatus GetTileTrackStatusProc(TileIndex tile, TransportType mode, u
* @param tile Tile being queried
* @param produced Destination array for produced cargo
*/
typedef void AddProducedCargoProc(TileIndex tile, CargoArray produced);
typedef void AddProducedCargoProc(TileIndex tile, CargoArray &produced);
typedef bool ClickTileProc(TileIndex tile);
typedef void AnimateTileProc(TileIndex tile);
typedef void TileLoopProc(TileIndex tile);
@ -157,14 +157,14 @@ VehicleEnterTileStatus VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y
void ChangeTileOwner(TileIndex tile, Owner old_owner, Owner new_owner);
void GetTileDesc(TileIndex tile, TileDesc *td);
static inline void AddAcceptedCargo(TileIndex tile, CargoArray acceptance)
static inline void AddAcceptedCargo(TileIndex tile, CargoArray &acceptance)
{
AddAcceptedCargoProc *proc = _tile_type_procs[GetTileType(tile)]->add_accepted_cargo_proc;
if (proc == NULL) return;
proc(tile, acceptance);
}
static inline void AddProducedCargo(TileIndex tile, CargoArray produced)
static inline void AddProducedCargo(TileIndex tile, CargoArray &produced)
{
AddProducedCargoProc *proc = _tile_type_procs[GetTileType(tile)]->add_produced_cargo_proc;
if (proc == NULL) return;

View File

@ -555,7 +555,7 @@ static CommandCost ClearTile_Town(TileIndex tile, DoCommandFlag flags)
return cost;
}
static void AddProducedCargo_Town(TileIndex tile, CargoArray produced)
static void AddProducedCargo_Town(TileIndex tile, CargoArray &produced)
{
HouseID house_id = GetHouseType(tile);
const HouseSpec *hs = HouseSpec::Get(house_id);
@ -582,7 +582,7 @@ static void AddProducedCargo_Town(TileIndex tile, CargoArray produced)
}
}
static void AddAcceptedCargo_Town(TileIndex tile, CargoArray acceptance)
static void AddAcceptedCargo_Town(TileIndex tile, CargoArray &acceptance)
{
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
CargoID accepts[3];

View File

@ -182,14 +182,11 @@ static void TrainDetailsCapacityTab(const Vehicle *v, int left, int right, int y
*/
int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
{
CargoArray act_cargo;
CargoArray max_cargo;
int num = 0;
if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
memset(max_cargo, 0, sizeof(max_cargo));
memset(act_cargo, 0, sizeof(act_cargo));
CargoArray act_cargo;
CargoArray max_cargo;
for (const Vehicle *v = Vehicle::Get(veh_id) ; v != NULL ; v = v->Next()) {
act_cargo[v->cargo_type] += v->cargo.Count();
max_cargo[v->cargo_type] += v->cargo_cap;
@ -274,10 +271,7 @@ void DrawTrainDetails(const Vehicle *v, int left, int right, int y, int vscroll_
} else {
CargoArray act_cargo;
CargoArray max_cargo;
Money feeder_share = 0;
memset(max_cargo, 0, sizeof(max_cargo));
memset(act_cargo, 0, sizeof(act_cargo));
Money feeder_share = 0;
for (const Vehicle *u = v; u != NULL ; u = u->Next()) {
act_cargo[u->cargo_type] += u->cargo.Count();

View File

@ -291,7 +291,7 @@ static CommandCost ClearTile_Unmovable(TileIndex tile, DoCommandFlag flags)
return CommandCost();
}
static void AddAcceptedCargo_Unmovable(TileIndex tile, CargoArray acceptance)
static void AddAcceptedCargo_Unmovable(TileIndex tile, CargoArray &acceptance)
{
if (!IsCompanyHQ(tile)) return;

View File

@ -577,7 +577,6 @@ static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * co
{
const Vehicle *v;
CargoArray diff;
memset(diff, 0, sizeof(diff));
/* Append the cargo of the connected weagons */
for (v = *a; v != NULL; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;