(svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
authorskidd13
Sun, 25 Nov 2007 15:35:25 +0000
changeset 7967 a230c063a672
parent 7966 0c6e2703634c
child 7968 45a32751abe5
(svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
-Codechange: Convert the CHANCE macros to functions and rename them fitting to the naming style
src/ai/default/default.cpp
src/aircraft_cmd.cpp
src/core/random_func.hpp
src/disaster_cmd.cpp
src/economy.cpp
src/industry_cmd.cpp
src/macros.h
src/road_cmd.cpp
src/roadveh_cmd.cpp
src/town_cmd.cpp
src/train_cmd.cpp
src/tree_cmd.cpp
src/vehicle.cpp
--- a/src/ai/default/default.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/ai/default/default.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -643,7 +643,7 @@
 
 	if (p->ai.route_type_mask != 0 &&
 			!(p->ai.route_type_mask & bitmask) &&
-			!CHANCE16(1, 5)) {
+			!Chance16(1, 5)) {
 		return false;
 	}
 
@@ -1452,7 +1452,7 @@
 	 * Also, non-full load is more resistant against starving (by building better stations
 	 * or using exclusive rights)
 	 */
-	p->ai.num_want_fullload = CHANCE16(1, 5); // 20% chance
+	p->ai.num_want_fullload = Chance16(1, 5); // 20% chance
 //	p->ai.loco_id = INVALID_VEHICLE;
 	p->ai.order_list_blocks[0] = 0;
 	p->ai.order_list_blocks[1] = 1;
--- a/src/aircraft_cmd.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/aircraft_cmd.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -1255,7 +1255,7 @@
 
 	if (v->u.air.crashed_counter < 650) {
 		uint32 r;
-		if (CHANCE16R(1,32,r)) {
+		if (Chance16R(1,32,r)) {
 			static const DirDiff delta[] = {
 				DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
 			};
--- a/src/core/random_func.hpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/core/random_func.hpp	Sun Nov 25 15:35:25 2007 +0000
@@ -36,4 +36,63 @@
 uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link
 uint InteractiveRandomRange(uint max);
 
+/**
+ * Checks if a given randomize-number is below a given probability.
+ *
+ * This function is used to check if the given probability by the fraction of (a/b)
+ * is greater than low 16 bits of the given randomize-number v.
+ *
+ * Do not use this function twice on the same random 16 bits as it will yield
+ * the same result. One can use a random number for two calls to Chance16I,
+ * where one call sends the low 16 bits and the other the high 16 bits.
+ *
+ * @param a The numerator of the fraction
+ * @param b The denominator of the fraction, must of course not be null
+ * @param r The given randomize-number
+ * @return True if v is less or equals (a/b)
+ */
+static inline bool Chance16I(const uint a, const uint b, const uint32 r)
+{
+	assert(b != 0);
+	return (uint16)r < (uint16)((a << 16) / b);
+}
+
+/**
+ * Flips a coin with a given probability.
+ *
+ * This macro can be used to get true or false randomized according to a
+ * given probability. The parameter a and b create a percent value with
+ * (a/b). The macro returns true in (a/b) percent.
+ *
+ * @see Chance16I()
+ * @param a The numerator of the fraction
+ * @param b The denominator of the fraction
+ * @return True in (a/b) percent
+ */
+static inline bool Chance16(const uint a, const uint b)
+{
+	return Chance16I(a, b, Random());
+}
+
+/**
+ * Flips a coin with a given probability and saves the randomize-number in a variable.
+ *
+ * This function uses the same parameters as Chance16. The third parameter
+ * must be a variable the randomize-number from Random() is saved in.
+ *
+ * The low 16 bits of r will already be used and can therefor not be passed to
+ * Chance16I. One can only send the high 16 bits to Chance16I.
+ *
+ * @see Chance16I()
+ * @param a The numerator of the fraction
+ * @param b The denominator of the fraction
+ * @param r The variable to save the randomize-number from Random()
+ * @return True in (a/b) percent
+ */
+static inline bool Chance16R(const uint a, const uint b, uint32 &r)
+{
+	r = Random();
+	return Chance16I(a, b, r);
+}
+
 #endif /* RANDOM_FUNC_HPP */
--- a/src/disaster_cmd.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/disaster_cmd.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -715,7 +715,7 @@
 	if (IsValidTile(tile)) {
 		TrackdirBits r = (TrackdirBits)GetTileTrackStatus(tile, TRANSPORT_WATER, 0);
 
-		if (TrackdirBitsToTrackBits(r) == TRACK_BIT_ALL && !CHANCE16(1, 90)) {
+		if (TrackdirBitsToTrackBits(r) == TRACK_BIT_ALL && !Chance16(1, 90)) {
 			GetNewVehiclePosResult gp = GetNewVehiclePos(v);
 			SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
 			return;
@@ -825,7 +825,7 @@
 
 	FOR_ALL_INDUSTRIES(i) {
 		if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
-				(found == NULL || CHANCE16(1, 2))) {
+				(found == NULL || Chance16(1, 2))) {
 			found = i;
 		}
 	}
@@ -861,7 +861,7 @@
 
 	FOR_ALL_INDUSTRIES(i) {
 		if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
-				(found == NULL || CHANCE16(1, 2))) {
+				(found == NULL || Chance16(1, 2))) {
 			found = i;
 		}
 	}
--- a/src/economy.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/economy.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -1102,7 +1102,7 @@
 	}
 
 	/* 25% chance to go on */
-	if (CHANCE16(1,4)) {
+	if (Chance16(1,4)) {
 		/*  Find a free slot*/
 		s = _subsidies;
 		while (s->cargo_type != CT_INVALID) {
--- a/src/industry_cmd.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/industry_cmd.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -558,7 +558,7 @@
 	case GFX_OILWELL_ANIMATED_2:
 	case GFX_OILWELL_ANIMATED_3:
 		if ((_tick_counter & 7) == 0) {
-			bool b = CHANCE16(1, 7);
+			bool b = Chance16(1, 7);
 			IndustryGfx gfx = GetIndustryGfx(tile);
 
 			m = GetIndustryAnimationState(tile) + 1;
@@ -738,7 +738,7 @@
 	case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
 	case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
 	case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
-		if (!(_tick_counter & 0x400) && CHANCE16(1, 2)) {
+		if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
 			switch (gfx) {
 				case GFX_COAL_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_ANIMATED;   break;
 				case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
@@ -751,7 +751,7 @@
 		break;
 
 	case GFX_OILWELL_NOT_ANIMATED:
-		if (CHANCE16(1, 6)) {
+		if (Chance16(1, 6)) {
 			SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
 			SetIndustryAnimationState(tile, 0);
 			AddAnimatedTile(tile);
@@ -775,7 +775,7 @@
 		break;
 
 	case GFX_POWERPLANT_SPARKS:
-		if (CHANCE16(1, 3)) {
+		if (Chance16(1, 3)) {
 			SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
 			AddAnimatedTile(tile);
 		}
@@ -805,7 +805,7 @@
 		break;
 
 	case GFX_SUGAR_MINE_SIEVE:
-		if (CHANCE16(1, 3)) AddAnimatedTile(tile);
+		if (Chance16(1, 3)) AddAnimatedTile(tile);
 		break;
 	}
 }
@@ -863,7 +863,7 @@
 		if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
 			byte or_ = type;
 
-			if (or_ == 1 && CHANCE16(1, 7)) or_ = 2;
+			if (or_ == 1 && Chance16(1, 7)) or_ = 2;
 
 			if (direction == AXIS_X) {
 				SetFenceSE(tile, or_);
@@ -990,7 +990,7 @@
 
 	/* play a sound? */
 	if ((i->counter & 0x3F) == 0) {
-		if (CHANCE16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
+		if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
 			SndPlayTileFx(
 				(SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
 				i->xy);
@@ -1012,7 +1012,7 @@
 			if (HasBit(indsp->callback_flags, CBM_IND_SPECIAL_EFFECT)) {
 				plant = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->xy) != 0);
 			} else {
-				plant = CHANCE16(1, 8);
+				plant = Chance16(1, 8);
 			}
 
 			if (plant) PlantRandomFarmField(i);
@@ -2052,9 +2052,9 @@
 
 				new_prod = old_prod = i->production_rate[j];
 
-				if (only_decrease || CHANCE16(1, 3)) mult *= -1;
+				if (only_decrease || Chance16(1, 3)) mult *= -1;
 
-				if (CHANCE16(1, 22)) {
+				if (Chance16(1, 22)) {
 					new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
 				}
 
@@ -2081,9 +2081,9 @@
 				}
 			}
 		} else {
-			if (only_decrease || CHANCE16(1, 3)) {
+			if (only_decrease || Chance16(1, 3)) {
 				/* If you transport > 60%, 66% chance we increase, else 33% chance we increase */
-				if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != CHANCE16(1, 3)) {
+				if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
 					mul = 1; // Increase production
 				} else {
 					div = 1; // Decrease production
@@ -2093,7 +2093,7 @@
 	}
 
 	if (standard && indspec->life_type & INDUSTRYLIFE_PROCESSING) {
-		if ( (byte)(_cur_year - i->last_prod_year) >= 5 && CHANCE16(1, smooth_economy ? 180 : 2)) {
+		if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
 			closeit = true;
 		}
 	}
@@ -2172,7 +2172,7 @@
 	}
 
 	/* 3% chance that we start a new industry */
-	if (CHANCE16(3, 100)) {
+	if (Chance16(3, 100)) {
 		MaybeNewIndustry();
 	} else {
 		i = GetRandomIndustry();
--- a/src/macros.h	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/macros.h	Sun Nov 25 15:35:25 2007 +0000
@@ -261,55 +261,6 @@
 	return num;
 }
 
-/**
- * Flips a coin with a given probability.
- *
- * This macro can be used to get true or false randomized according to a
- * given probability. The parameter a and b create a percent value with
- * (a/b). The macro returns true in (a/b) percent.
- *
- * @param a The numerator of the fraction
- * @param b The denominator of the fraction, must of course not be null
- * @return True in (a/b) percent
- */
-#define CHANCE16(a, b) CHANCE16I(a, b, Random())
-
-/**
- * Flips a coin with a given probability and saves the randomize-number in a variable.
- *
- * This macro uses the same parameters as the CHANCE16 marco. The third parameter
- * must be a variable the randomize-number from Random() is saved in.
- *
- * The low 16 bits of r will already be used and can therefor not be passed to
- * CHANCE16I. One can only send the high 16 bits to CHANCE16I.
- *
- * @param a The numerator of the fraction, see CHANCE16
- * @param b The denominator of the fraction, see CHANCE16
- * @param r The variable to save the randomize-number from Random()
- * @return True in (a/b) percent
- */
-#define CHANCE16R(a, b, r) CHANCE16I(a, b, r = Random())
-
-/**
- * Checks if a given randomize-number is below a given probability.
- *
- * This macro is used to check if the given probability by the fraction of (a/b)
- * is greater than low 16 bits of the given randomize-number v.
- *
- * Do not use this function twice on the same random 16 bits as it will yield
- * the same result. One can use a random number for two calls to CHANCE16I,
- * where one call sends the low 16 bits and the other the high 16 bits.
- *
- * @param a The numerator of the fraction, see CHANCE16
- * @param b The denominator of the fraction, see CHANCE16
- * @param r The given randomize-number
- * @return True if v is less or equals (a/b)
- */
-static inline bool CHANCE16I(const uint a, const uint b, const uint32 r)
-{
-	return (uint16)r < (uint16)((65536 * a) / b);
-}
-
 
 #define for_each_bit(_i, _b)            \
 	for (_i = 0; _b != 0; _i++, _b >>= 1) \
--- a/src/road_cmd.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/road_cmd.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -1243,7 +1243,7 @@
 			if (t->road_build_months != 0 &&
 					(DistanceManhattan(t->xy, tile) < 8 || grp != 0) &&
 					GetRoadTileType(tile) == ROAD_TILE_NORMAL && CountBits(GetAllRoadBits(tile)) > 1 ) {
-				if (GetTileSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && CHANCE16(1, 40)) {
+				if (GetTileSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) {
 					StartRoadWorks(tile);
 
 					SndPlayTileFx(SND_21_JACKHAMMER, tile);
--- a/src/roadveh_cmd.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/roadveh_cmd.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -1989,7 +1989,7 @@
 
 	if (v->current_order.type == OT_GOTO_DEPOT &&
 			v->current_order.flags & OF_NON_STOP &&
-			!CHANCE16(1, 20)) {
+			!Chance16(1, 20)) {
 		return;
 	}
 
--- a/src/town_cmd.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/town_cmd.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -406,7 +406,7 @@
 	if ((hs->building_flags & BUILDING_IS_ANIMATED) &&
 			house_id < NEW_HOUSE_OFFSET &&
 			!LiftHasDestination(tile) &&
-			CHANCE16(1, 2))
+			Chance16(1, 2))
 		AddAnimatedTile(tile);
 
 	t = GetTownByTile(tile);
@@ -731,14 +731,14 @@
 		 * maybe terraform some. */
 		desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NW : SLOPE_NE;
 		if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) {
-			if (CHANCE16(1, 8)) {
+			if (Chance16(1, 8)) {
 				CommandCost res = CMD_ERROR;
-				if (!_generating_world && CHANCE16(1, 10)) {
+				if (!_generating_world && Chance16(1, 10)) {
 					/* Note: Do not replace " ^ 0xF" with ComplementSlope(). The slope might be steep. */
-					res = DoCommand(tile, CHANCE16(1, 16) ? cur_slope : cur_slope ^ 0xF, 0,
+					res = DoCommand(tile, Chance16(1, 16) ? cur_slope : cur_slope ^ 0xF, 0,
 							DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
 				}
-				if (CmdFailed(res) && CHANCE16(1, 3)) {
+				if (CmdFailed(res) && Chance16(1, 3)) {
 					/* We can consider building on the slope, though. */
 					goto no_slope;
 				}
@@ -1002,7 +1002,7 @@
 
 				DiagDirection source_dir = ReverseDiagDir(target_dir);
 
-				if (CHANCE16(1, 4)) {
+				if (Chance16(1, 4)) {
 					/* Randomize a new target dir */
 					do target_dir = RandomDiagDir(); while (target_dir == source_dir);
 				}
@@ -1096,7 +1096,7 @@
 				 /* Allow a house at the edge. 60% chance or
 				  * always ok if no road allowed. */
 				rcmd = DiagDirToRoadBits(target_dir);
-				allow_house = (!IsRoadAllowedHere(house_tile, target_dir) || CHANCE16(6, 10));
+				allow_house = (!IsRoadAllowedHere(house_tile, target_dir) || Chance16(6, 10));
 				break;
 		}
 
@@ -1104,7 +1104,7 @@
 			/* Build a house, but not if there already is a house there. */
 			if (!IsTileType(house_tile, MP_HOUSE)) {
 				/* Level the land if possible */
-				if (CHANCE16(1, 6)) LevelTownLand(house_tile);
+				if (Chance16(1, 6)) LevelTownLand(house_tile);
 
 				/* And build a house.
 				 * Set result to -1 if we managed to build it. */
@@ -1778,7 +1778,7 @@
 			uint32 r = Random();
 
 			construction_stage = TOWN_HOUSE_COMPLETED;
-			if (CHANCE16(1, 7)) construction_stage = GB(r, 0, 2);
+			if (Chance16(1, 7)) construction_stage = GB(r, 0, 2);
 
 			if (construction_stage == TOWN_HOUSE_COMPLETED) {
 				ChangePopulation(t, hs->population);
@@ -2155,7 +2155,7 @@
 		t->fund_buildings_months--;
 	} else {
 		m = _grow_count_values[1][min(n, 5)];
-		if (n == 0 && !CHANCE16(1, 12)) return;
+		if (n == 0 && !Chance16(1, 12)) return;
 	}
 
 	if (_opt.landscape == LT_ARCTIC) {
--- a/src/train_cmd.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/train_cmd.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -2015,7 +2015,7 @@
 
 		case 1:
 			/* diesel smoke */
-			if (u->cur_speed <= 40 && CHANCE16(15, 128)) {
+			if (u->cur_speed <= 40 && Chance16(15, 128)) {
 				CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
 				sound = true;
 			}
@@ -2023,7 +2023,7 @@
 
 		case 2:
 			/* blue spark */
-			if (GB(v->tick_counter, 0, 2) == 0 && CHANCE16(1, 45)) {
+			if (GB(v->tick_counter, 0, 2) == 0 && Chance16(1, 45)) {
 				CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
 				sound = true;
 			}
@@ -3114,7 +3114,7 @@
 	}
 
 	uint32 r;
-	if (state <= 200 && CHANCE16R(1, 7, r)) {
+	if (state <= 200 && Chance16R(1, 7, r)) {
 		int index = (r * 10 >> 16);
 
 		Vehicle *u = v;
@@ -3392,7 +3392,7 @@
 
 	if (v->current_order.type == OT_GOTO_DEPOT &&
 			v->current_order.dest != depot->index &&
-			!CHANCE16(3, 16)) {
+			!Chance16(3, 16)) {
 		return;
 	}
 
--- a/src/tree_cmd.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/tree_cmd.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -543,7 +543,7 @@
 			};
 			uint32 r = Random();
 
-			if (CHANCE16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
+			if (Chance16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
 			break;
 		}
 
@@ -567,7 +567,7 @@
 		} else {
 			if (GetTreeDensity(tile) == 3) {
 				uint32 r = Random();
-				if (CHANCE16I(1, 200, r)) {
+				if (Chance16I(1, 200, r)) {
 					SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
 				}
 			}
--- a/src/vehicle.cpp	Sun Nov 25 15:17:16 2007 +0000
+++ b/src/vehicle.cpp	Sun Nov 25 15:35:25 2007 +0000
@@ -1264,7 +1264,7 @@
 	}
 
 	if (b->y == 4 && b->x == 1) {
-		if (v->z_pos > 180 || CHANCE16I(1, 96, InteractiveRandom())) {
+		if (v->z_pos > 180 || Chance16I(1, 96, InteractiveRandom())) {
 			v->spritenum = 5;
 			SndPlayVehicleFx(SND_2F_POP, v);
 		}
@@ -1443,7 +1443,7 @@
 
 	/* increase chance of failure */
 	chance = v->breakdown_chance + 1;
-	if (CHANCE16I(1,25,r)) chance += 25;
+	if (Chance16I(1,25,r)) chance += 25;
 	v->breakdown_chance = min(255, chance);
 
 	/* calculate reliability value to use in comparison */