(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
pull/155/head
skidd13 17 years ago
parent e3092f6791
commit e36dba227b

@ -643,7 +643,7 @@ static bool AiCheckIfRouteIsGood(Player *p, FoundRoute *fr, byte bitmask)
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 @@ static void AiWantPassengerAircraftRoute(Player *p)
* 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;

@ -1255,7 +1255,7 @@ static void HandleCrashedAircraft(Vehicle *v)
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
};

@ -36,4 +36,63 @@ void SetRandomSeed(uint32 seed);
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 */

@ -715,7 +715,7 @@ static void DisasterTick_Submarine(Vehicle *v)
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 @@ static void Disaster_Airplane_Init()
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 @@ static void Disaster_Helicopter_Init()
FOR_ALL_INDUSTRIES(i) {
if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
(found == NULL || CHANCE16(1, 2))) {
(found == NULL || Chance16(1, 2))) {
found = i;
}
}

@ -1102,7 +1102,7 @@ static void SubsidyMonthlyHandler()
}
/* 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) {

@ -558,7 +558,7 @@ static void AnimateTile_Industry(TileIndex tile)
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 @@ static void TileLoop_Industry(TileIndex tile)
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 @@ static void TileLoop_Industry(TileIndex tile)
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 @@ static void TileLoop_Industry(TileIndex tile)
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 @@ static void TileLoop_Industry(TileIndex tile)
break;
case GFX_SUGAR_MINE_SIEVE:
if (CHANCE16(1, 3)) AddAnimatedTile(tile);
if (Chance16(1, 3)) AddAnimatedTile(tile);
break;
}
}
@ -863,7 +863,7 @@ static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direct
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 @@ static void ProduceIndustryGoods(Industry *i)
/* 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 @@ static void ProduceIndustryGoods(Industry *i)
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 @@ static void ChangeIndustryProduction(Industry *i, bool monthly)
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 @@ static void ChangeIndustryProduction(Industry *i, bool monthly)
}
}
} 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 @@ static void ChangeIndustryProduction(Industry *i, bool monthly)
}
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 @@ void IndustryMonthlyLoop()
}
/* 3% chance that we start a new industry */
if (CHANCE16(3, 100)) {
if (Chance16(3, 100)) {
MaybeNewIndustry();
} else {
i = GetRandomIndustry();

@ -261,55 +261,6 @@ template<typename T> static inline uint CountBits(T value)
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) \

@ -1243,7 +1243,7 @@ static void TileLoop_Road(TileIndex tile)
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);

@ -1989,7 +1989,7 @@ static void CheckIfRoadVehNeedsService(Vehicle *v)
if (v->current_order.type == OT_GOTO_DEPOT &&
v->current_order.flags & OF_NON_STOP &&
!CHANCE16(1, 20)) {
!Chance16(1, 20)) {
return;
}

@ -406,7 +406,7 @@ static void TileLoop_Town(TileIndex tile)
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 @@ no_slope:
* 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 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
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 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
/* 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 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
/* 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 @@ static void DoBuildTownHouse(Town *t, TileIndex tile)
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 @@ static void UpdateTownGrowRate(Town *t)
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) {

@ -2015,7 +2015,7 @@ static void HandleLocomotiveSmokeCloud(const Vehicle* v)
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 @@ static void HandleLocomotiveSmokeCloud(const Vehicle* v)
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 @@ static void HandleCrashedTrain(Vehicle *v)
}
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 @@ static void CheckIfTrainNeedsService(Vehicle *v)
if (v->current_order.type == OT_GOTO_DEPOT &&
v->current_order.dest != depot->index &&
!CHANCE16(3, 16)) {
!Chance16(3, 16)) {
return;
}

@ -543,7 +543,7 @@ static void TileLoopTreesDesert(TileIndex tile)
};
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 @@ static void TileLoopTreesAlps(TileIndex tile)
} 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);
}
}

@ -1264,7 +1264,7 @@ static void BubbleTick(Vehicle *v)
}
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 @@ void CheckVehicleBreakdown(Vehicle *v)
/* 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 */

Loading…
Cancel
Save