(svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
authorrubidium
Sun, 08 Jul 2007 17:40:04 +0000
changeset 7199 e65a28455ee3
parent 7198 27559e21e9f5
child 7200 54afe2891d7b
(svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
src/economy.cpp
src/industry_cmd.cpp
src/newgrf_callbacks.h
src/newgrf_cargo.cpp
src/newgrf_cargo.h
--- 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) {
--- 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];
 	}
 }
 
--- 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
--- 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;;
+}
--- 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 */