Changeset - r28189:ef345222bebe
[Not reviewed]
master
0 3 0
Peter Nelson - 12 months ago 2023-11-27 23:17:55
peter1138@openttd.org
Codechange: Use std::array for industry tile cargo acceptance. (#11498)

This avoids use of memcpy/memset to copy or fill.
3 files changed with 14 insertions and 16 deletions:
0 comments (0 inline, 0 general)
src/industry_cmd.cpp
Show inline comments
 
@@ -415,22 +415,20 @@ static void AddAcceptedCargo_Industry(Ti
 
	const Industry *ind = Industry::GetByTile(tile);
 

	
 
	/* Starting point for acceptance */
 
	CargoID accepts_cargo[lengthof(itspec->accepts_cargo)];
 
	int8_t cargo_acceptance[lengthof(itspec->acceptance)];
 
	MemCpyT(accepts_cargo, itspec->accepts_cargo, lengthof(accepts_cargo));
 
	MemCpyT(cargo_acceptance, itspec->acceptance, lengthof(cargo_acceptance));
 
	auto accepts_cargo = itspec->accepts_cargo;
 
	auto cargo_acceptance = itspec->acceptance;
 

	
 
	if (itspec->special_flags & INDTILE_SPECIAL_ACCEPTS_ALL_CARGO) {
 
		/* Copy all accepted cargoes from industry itself */
 
		for (const auto &a : ind->accepted) {
 
			CargoID *pos = std::find(accepts_cargo, endof(accepts_cargo), a.cargo);
 
			if (pos == endof(accepts_cargo)) {
 
			auto pos = std::find(std::begin(accepts_cargo), std::end(accepts_cargo), a.cargo);
 
			if (pos == std::end(accepts_cargo)) {
 
				/* Not found, insert */
 
				pos = std::find(accepts_cargo, endof(accepts_cargo), CT_INVALID);
 
				if (pos == endof(accepts_cargo)) continue; // nowhere to place, give up on this one
 
				pos = std::find(std::begin(accepts_cargo), std::end(accepts_cargo), CT_INVALID);
 
				if (pos == std::end(accepts_cargo)) continue; // nowhere to place, give up on this one
 
				*pos = a.cargo;
 
			}
 
			cargo_acceptance[pos - accepts_cargo] += 8;
 
			cargo_acceptance[std::distance(std::begin(accepts_cargo), pos)] += 8;
 
		}
 
	}
 

	
 
@@ -438,7 +436,7 @@ static void AddAcceptedCargo_Industry(Ti
 
		/* Try callback for accepts list, if success override all existing accepts */
 
		uint16_t res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, Industry::GetByTile(tile), tile);
 
		if (res != CALLBACK_FAILED) {
 
			MemSetT(accepts_cargo, CT_INVALID, lengthof(accepts_cargo));
 
			accepts_cargo.fill(CT_INVALID);
 
			for (uint i = 0; i < 3; i++) accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile);
 
		}
 
	}
 
@@ -447,12 +445,12 @@ static void AddAcceptedCargo_Industry(Ti
 
		/* Try callback for acceptance list, if success override all existing acceptance */
 
		uint16_t res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, Industry::GetByTile(tile), tile);
 
		if (res != CALLBACK_FAILED) {
 
			MemSetT(cargo_acceptance, 0, lengthof(cargo_acceptance));
 
			cargo_acceptance.fill(0);
 
			for (uint i = 0; i < 3; i++) cargo_acceptance[i] = GB(res, i * 4, 4);
 
		}
 
	}
 

	
 
	for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
 
	for (byte i = 0; i < std::size(itspec->accepts_cargo); i++) {
 
		CargoID a = accepts_cargo[i];
 
		if (!IsValidCargoID(a) || cargo_acceptance[i] <= 0) continue; // work only with valid cargoes
 

	
src/industrytype.h
Show inline comments
 
@@ -152,8 +152,8 @@ struct IndustrySpec {
 
 * @note A tile can at most accept 3 types of cargo, even if an industry as a whole can accept more types.
 
 */
 
struct IndustryTileSpec {
 
	CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]; ///< Cargo accepted by this tile
 
	int8_t acceptance[INDUSTRY_NUM_INPUTS]; ///< Level of acceptance per cargo type (signed, may be negative!)
 
	std::array<CargoID, INDUSTRY_NUM_INPUTS> accepts_cargo; ///< Cargo accepted by this tile
 
	std::array<int8_t, INDUSTRY_NUM_INPUTS> acceptance; ///< Level of acceptance per cargo type (signed, may be negative!)
 
	Slope slopes_refused;                 ///< slope pattern on which this tile cannot be built
 
	byte anim_production;                 ///< Animation frame to start when goods are produced
 
	byte anim_next;                       ///< Next frame in an animation
src/newgrf.cpp
Show inline comments
 
@@ -3295,12 +3295,12 @@ static ChangeInfoResult IndustrytilesCha
 

	
 
			case 0x13: { // variable length cargo acceptance
 
				byte num_cargoes = buf->ReadByte();
 
				if (num_cargoes > lengthof(tsp->acceptance)) {
 
				if (num_cargoes > std::size(tsp->acceptance)) {
 
					GRFError *error = DisableGrf(STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG);
 
					error->param_value[1] = prop;
 
					return CIR_DISABLED;
 
				}
 
				for (uint i = 0; i < lengthof(tsp->acceptance); i++) {
 
				for (uint i = 0; i < std::size(tsp->acceptance); i++) {
 
					if (i < num_cargoes) {
 
						tsp->accepts_cargo[i] = GetCargoTranslation(buf->ReadByte(), _cur.grffile);
 
						/* Tile acceptance can be negative to counteract the INDTILE_SPECIAL_ACCEPTS_ALL_CARGO flag */
0 comments (0 inline, 0 general)