From e80f3390863aa4a722b0d7516cca7420b12df53a Mon Sep 17 00:00:00 2001 From: yexo Date: Sat, 7 Aug 2010 20:11:27 +0000 Subject: (svn r20396) -Codechange: introduce a helper function to test whether an industry temporarily refues to accept some cargo --- src/economy.cpp | 7 +------ src/industry_cmd.cpp | 12 ++---------- src/newgrf_industries.cpp | 22 ++++++++++++++++++++++ src/newgrf_industries.h | 1 + 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/economy.cpp b/src/economy.cpp index dcf7d3990..73b288763 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -909,8 +909,6 @@ static uint DeliverGoodsToIndustry(const Station *st, CargoID cargo_type, uint n Industry *ind = st->industries_near[i]; if (ind->index == source) continue; - const IndustrySpec *indspec = GetIndustrySpec(ind->type); - uint cargo_index; for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) { if (cargo_type == ind->accepts_cargo[cargo_index]) break; @@ -919,10 +917,7 @@ static uint DeliverGoodsToIndustry(const Station *st, CargoID cargo_type, uint n if (cargo_index >= lengthof(ind->accepts_cargo)) continue; /* Check if industry temporarily refuses acceptance */ - if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) { - uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), ind, ind->type, ind->location.tile); - if (res == 0) continue; - } + if (IndustryTemporarilyRefusesCargo(ind, cargo_type)) continue; /* Insert the industry into _cargo_delivery_destinations, if not yet contained */ _cargo_delivery_destinations.Include(ind); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index fea6e5d7e..8e893d6fe 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -2065,18 +2065,11 @@ static bool CheckIndustryCloseDownProtection(IndustryType type) */ static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces) { - const IndustrySpec *indspec = GetIndustrySpec(ind->type); + if (cargo == CT_INVALID) return; /* Check for acceptance of cargo */ for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) { - if (ind->accepts_cargo[j] == CT_INVALID) continue; - if (cargo == ind->accepts_cargo[j]) { - if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) { - uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, - 0, GetReverseCargoTranslation(cargo, indspec->grf_prop.grffile), - ind, ind->type, ind->location.tile); - if (res == 0) continue; - } + if (cargo == ind->accepts_cargo[j] && !IndustryTemporarilyRefusesCargo(ind, cargo)) { *c_accepts = true; break; } @@ -2084,7 +2077,6 @@ static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accept /* Check for produced cargo */ for (byte j = 0; j < lengthof(ind->produced_cargo); j++) { - if (ind->produced_cargo[j] == CT_INVALID) continue; if (cargo == ind->produced_cargo[j]) { *c_produces = true; break; diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index a7d9606a3..fc8c8bbef 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -17,6 +17,7 @@ #include "newgrf_commons.h" #include "newgrf_text.h" #include "newgrf_town.h" +#include "newgrf_cargo.h" #include "window_func.h" #include "town.h" #include "company_base.h" @@ -583,3 +584,24 @@ void GetIndustryResolver(ResolverObject *ro, uint index) Industry *i = Industry::Get(index); NewIndustryResolver(ro, i->location.tile, i, i->type); } + +/** + * Check whether an industry temporarily refuses to accept a certain cargo. + * @param ind The industry to query. + * @param cargo_type The cargo to get information about. + * @pre cargo_type is in ind->accepts_cargo. + * @return Whether the given industry refuses to accept this cargo type. + */ +bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type) +{ + assert(cargo_type == ind->accepts_cargo[0] || cargo_type == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]); + + const IndustrySpec *indspec = GetIndustrySpec(ind->type); + if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) { + uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, + 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), + ind, ind->type, ind->location.tile); + return res == 0; + } + return false; +} diff --git a/src/newgrf_industries.h b/src/newgrf_industries.h index cf3418b83..3d90d65bb 100644 --- a/src/newgrf_industries.h +++ b/src/newgrf_industries.h @@ -39,6 +39,7 @@ uint32 GetIndustryIDAtOffset(TileIndex new_tile, const Industry *i, uint32 cur_g void IndustryProductionCallback(Industry *ind, int reason); CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder); bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type); +bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type); IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id); -- cgit v1.2.3-70-g09d2