# HG changeset patch # User rubidium # Date 1183916404 0 # Node ID e65a28455ee30d637e20a47bb25912eced5db44a # Parent 27559e21e9f511b680e380185b776d20805e9049 (svn r10477) -Codechange: add some callbacks to customise the acceptance of industries. diff -r 27559e21e9f5 -r e65a28455ee3 src/economy.cpp --- a/src/economy.cpp Sun Jul 08 14:23:15 2007 +0000 +++ b/src/economy.cpp Sun Jul 08 17:40:04 2007 +0000 @@ -1211,18 +1211,18 @@ indspec = GetIndustrySpec(ind->type); uint i; - if (indspec->produced_cargo[0] == CT_INVALID) continue; - for (i = 0; i < lengthof(indspec->accepts_cargo); i++) { - if (cargo_type == indspec->accepts_cargo[i] && - (indspec->input_cargo_multiplier[i][0] != 0 || indspec->input_cargo_multiplier[i][1] != 0)) { - break; - } + if (cargo_type == indspec->accepts_cargo[i]) break; } /* Check if matching cargo has been found */ if (i == lengthof(indspec->accepts_cargo)) continue; + if (HASBIT(indspec->callback_flags, CBM_IND_REFUSE_CARGO)) { + uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), ind, ind->type, ind->xy); + if (res == 0) continue; + } + uint dist = DistanceManhattan(ind->xy, xy); if (dist < best_dist) { diff -r 27559e21e9f5 -r e65a28455ee3 src/industry_cmd.cpp --- a/src/industry_cmd.cpp Sun Jul 08 14:23:15 2007 +0000 +++ b/src/industry_cmd.cpp Sun Jul 08 17:40:04 2007 +0000 @@ -330,12 +330,36 @@ static void GetAcceptedCargo_Industry(TileIndex tile, AcceptedCargo ac) { - const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile)); - CargoID a; + IndustryGfx gfx = GetIndustryGfx(tile); + const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx); + + /* When we have to use a callback, we put our data in the next two variables */ + CargoID raw_accepts_cargo[lengthof(itspec->accepts_cargo)]; + uint8 raw_acceptance[lengthof(itspec->acceptance)]; + + /* And then these will always point to a same sized array with the required data */ + const CargoID *accepts_cargo = itspec->accepts_cargo; + const uint8 *acceptance = itspec->acceptance; + + if (HASBIT(itspec->callback_flags, CBM_INDT_ACCEPT_CARGO)) { + uint16 res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, GetIndustryByTile(tile), tile); + if (res != CALLBACK_FAILED) { + accepts_cargo = raw_accepts_cargo; + for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile); + } + } + + if (HASBIT(itspec->callback_flags, CBM_INDT_CARGO_ACCEPTANCE)) { + uint16 res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, GetIndustryByTile(tile), tile); + if (res != CALLBACK_FAILED) { + acceptance = raw_acceptance; + for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_acceptance[i] = GB(res, i * 4, 4); + } + } for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) { - a = itspec->accepts_cargo[i]; - if (a != CT_INVALID) ac[a] = itspec->acceptance[i]; + CargoID a = accepts_cargo[i]; + if (a != CT_INVALID) ac[a] = acceptance[i]; } } diff -r 27559e21e9f5 -r e65a28455ee3 src/newgrf_callbacks.h --- a/src/newgrf_callbacks.h Sun Jul 08 14:23:15 2007 +0000 +++ b/src/newgrf_callbacks.h Sun Jul 08 17:40:04 2007 +0000 @@ -107,10 +107,10 @@ CBID_HOUSE_ACCEPT_CARGO = 0x2A, /* Called to query the cargo acceptance of the industry tile */ - CBID_INDTILE_ACCEPT_CARGO = 0x2B, // not yet implemented + CBID_INDTILE_CARGO_ACCEPTANCE = 0x2B, /* Called to determine which cargoes an industry should accept. */ - CBID_INDUSTRY_ACCEPT_CARGO = 0x2C, // not yet implemented + CBID_INDTILE_ACCEPT_CARGO = 0x2C, /* Called to determine if a specific colour map should be used for a vehicle * instead of the default livery */ @@ -158,8 +158,8 @@ /* Called to determine if industry can alter the ground below industry tile */ CBID_INDUSTRY_AUTOSLOPE = 0x3C, // not yet implemented - /* Called to determine if the industry can still accept or refuse more cargo arrival */ - CBID_INDUSTRY_REFUSE_CARGO = 0x3D, // not yet implemented + /* Called to determine if the industry can still accept or refuse more cargo arrival */ + CBID_INDUSTRY_REFUSE_CARGO = 0x3D, /* Called (if appropriate bit in callback mask set) to determine whether a * town building can be destroyed. */ @@ -240,7 +240,7 @@ enum IndustryTileCallbackMask { CBM_INDT_ANIM_NEXT_FRAME = 0, ///< decides next animation frame CBM_INDT_ANIM_SPEED = 1, ///< decides animation speed - CBM_INDT_ACCEPTANCE_CARGO = 2, ///< decides amount of cargo acceptance + CBM_INDT_CARGO_ACCEPTANCE = 2, ///< decides amount of cargo acceptance CBM_INDT_ACCEPT_CARGO = 3, ///< decides accepted types CBM_INDT_SHAPE_CHECK = 4, ///< decides slope suitability CBM_INDT_DRAW_FOUNDATIONS = 5, ///< decides if default foundations need to be drawn diff -r 27559e21e9f5 -r e65a28455ee3 src/newgrf_cargo.cpp --- a/src/newgrf_cargo.cpp Sun Jul 08 14:23:15 2007 +0000 +++ b/src/newgrf_cargo.cpp Sun Jul 08 17:40:04 2007 +0000 @@ -110,3 +110,20 @@ /* Else the cargo value is a 'climate independent' 'bitnum' */ return GetCargoIDByBitnum(cargo); } + +uint8 GetReverseCargoTranslation(CargoID cargo, const GRFFile *grffile) +{ + /* Pre-version 7 uses the 'climate dependent' ID, i.e. cargo is the cargo ID */ + if (grffile->grf_version < 7) return cargo; + + const CargoSpec *cs = GetCargo(cargo); + + /* If the GRF contains a translation table (and the cargo is in the table) + * then get the cargo ID for the label */ + for (uint i = 0; i < grffile->cargo_max; i++) { + if (cs->label == grffile->cargo_list[i]) return i; + } + + /* No matching label was found, so we return the 'climate independent' 'bitnum' */ + return cs->bitnum;; +} diff -r 27559e21e9f5 -r e65a28455ee3 src/newgrf_cargo.h --- a/src/newgrf_cargo.h Sun Jul 08 14:23:15 2007 +0000 +++ b/src/newgrf_cargo.h Sun Jul 08 17:40:04 2007 +0000 @@ -28,5 +28,6 @@ SpriteID GetCustomCargoSprite(const CargoSpec *cs); uint16 GetCargoCallback(uint16 callback, uint32 param1, uint32 param2, const CargoSpec *cs); CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile); +uint8 GetReverseCargoTranslation(CargoID cargo, const GRFFile *grffile); #endif /* NEWGRF_CARGO_H */