From 200b4c0e08504a7c8df898d77a9d72b3fa573c04 Mon Sep 17 00:00:00 2001 From: j-p-e-g Date: Sun, 6 Jul 2008 16:47:06 +0000 Subject: Add a function x_chance_in_y(x,y) to replace the various random2(y) < x checks, e.g. x_chance_in_y(weight, totalweight). This should make things a bit more readable. Apply it to a number of files. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@6428 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/abyss.cc | 2 +- crawl-ref/source/cloud.cc | 2 +- crawl-ref/source/clua.cc | 2 +- crawl-ref/source/decks.cc | 39 +++++++++++---------- crawl-ref/source/dungeon.cc | 80 +++++++++++++++++++++++--------------------- crawl-ref/source/effects.cc | 30 +++++++++-------- crawl-ref/source/fight.cc | 16 +++++---- crawl-ref/source/it_use2.cc | 25 +++++++------- crawl-ref/source/it_use3.cc | 61 ++++++++++++++++----------------- crawl-ref/source/item_use.cc | 13 ++++--- crawl-ref/source/maps.cc | 6 ++-- crawl-ref/source/mgrow.cc | 2 +- crawl-ref/source/monplace.cc | 4 +-- crawl-ref/source/mstuff2.cc | 17 ++++++---- crawl-ref/source/mutation.cc | 10 +++--- crawl-ref/source/ouch.cc | 8 ++--- crawl-ref/source/player.cc | 10 +++--- crawl-ref/source/randart.cc | 14 ++++---- crawl-ref/source/religion.cc | 19 +++++------ crawl-ref/source/spells1.cc | 15 ++++----- crawl-ref/source/spells2.cc | 16 +++++---- crawl-ref/source/spells3.cc | 19 +++++------ crawl-ref/source/spells4.cc | 6 ++-- crawl-ref/source/spl-cast.cc | 2 +- crawl-ref/source/spl-util.cc | 21 +++--------- crawl-ref/source/stuff.cc | 23 +++++++++---- crawl-ref/source/stuff.h | 1 + crawl-ref/source/xom.cc | 22 ++++++------ 28 files changed, 248 insertions(+), 237 deletions(-) (limited to 'crawl-ref/source') diff --git a/crawl-ref/source/abyss.cc b/crawl-ref/source/abyss.cc index bf8167dcab..7d9a427909 100644 --- a/crawl-ref/source/abyss.cc +++ b/crawl-ref/source/abyss.cc @@ -735,7 +735,7 @@ static void _apply_corruption_effect( map_marker *marker, int duration) for (int i = 0; i < neffects; ++i) { - if (random2(4000) < cmark->duration + if (x_chance_in_y(cmark->duration, 4000) && !_spawn_corrupted_servant_near(cmark->pos)) { break; diff --git a/crawl-ref/source/cloud.cc b/crawl-ref/source/cloud.cc index aca3710b42..24b62e6977 100644 --- a/crawl-ref/source/cloud.cc +++ b/crawl-ref/source/cloud.cc @@ -120,7 +120,7 @@ static void _dissipate_cloud(int cc, cloud_struct &cloud, int dissipate) // Apply calculated rate to the actual cloud. cloud.decay -= dissipate; - if (random2(100) < cloud.spread_rate) + if (x_chance_in_y(cloud.spread_rate, 100)) { cloud.spread_rate -= div_rand_round(cloud.spread_rate, 10); cloud.decay -= _spread_cloud(cloud); diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc index 7107dc6355..8ada60d847 100644 --- a/crawl-ref/source/clua.cc +++ b/crawl-ref/source/clua.cc @@ -2010,7 +2010,7 @@ static int crawl_random_element(lua_State *ls) if (rollsize > 0) { rollsize += this_weight; - if (random2(rollsize) < this_weight) + if (x_chance_in_y(this_weight, rollsize)) { lua_pushvalue(ls, key_idx); lua_replace(ls, value_idx); diff --git a/crawl-ref/source/decks.cc b/crawl-ref/source/decks.cc index 17e6c65210..de5e5c5a62 100644 --- a/crawl-ref/source/decks.cc +++ b/crawl-ref/source/decks.cc @@ -374,7 +374,7 @@ static card_type _choose_from_archetype(const deck_archetype* pdeck, { const card_with_weights& cww = pdeck[i]; totalweight += cww.weight[rarity]; - if (random2(totalweight) < cww.weight[rarity]) + if (x_chance_in_y(cww.weight[rarity], totalweight)) result = cww.card; } return result; @@ -820,7 +820,7 @@ bool deck_peek() already_seen++; // Always increase if seen 2, 50% increase if seen 1. - if (already_seen && random2(2) < already_seen) + if (already_seen && x_chance_in_y(already_seen, 2)) deck.props["non_brownie_draws"]++; mprf("You draw two cards from the deck. They are: %s and %s.", @@ -1197,12 +1197,13 @@ void evoke_deck( item_def& deck ) if (you.penance[GOD_NEMELEX_XOBEH]) { int c = 1; - if ( (flags & (CFLAG_MARKED | CFLAG_SEEN)) - || props["num_marked"].get_byte() > 0 ) + if ((flags & (CFLAG_MARKED | CFLAG_SEEN)) + || props["num_marked"].get_byte() > 0) { c = 3; } - if (random2(3000) < c * you.penance[GOD_NEMELEX_XOBEH]) + + if (x_chance_in_y(c * you.penance[GOD_NEMELEX_XOBEH], 3000)) { card_type old_card = card; card = _choose_from_archetype(deck_of_punishment, rarity); @@ -1268,7 +1269,7 @@ void evoke_deck( item_def& deck ) } if (!deck_gone && allow_id - && (you.skills[SK_EVOCATIONS] > 5 + random2(35))) + && you.skills[SK_EVOCATIONS] > 5 + random2(35)) { mpr("Your skill with magical items lets you identify the deck."); set_ident_flags( deck, ISFLAG_KNOW_TYPE ); @@ -1292,11 +1293,11 @@ int get_power_level(int power, deck_rarity_type rarity) case DECK_RARITY_COMMON: break; case DECK_RARITY_LEGENDARY: - if (random2(500) < power) + if (x_chance_in_y(power, 500)) ++power_level; // deliberate fall-through case DECK_RARITY_RARE: - if (random2(700) < power) + if (x_chance_in_y(power, 700)) ++power_level; break; } @@ -1434,8 +1435,8 @@ static void _damnation_card(int power, deck_rarity_type rarity) if (you.religion == GOD_NEMELEX_XOBEH && !player_under_penance()) nemelex_bonus = you.piety / 20; - int extra_targets = power_level + random2(you.skills[SK_EVOCATIONS] + - nemelex_bonus) / 12; + int extra_targets = power_level + random2(you.skills[SK_EVOCATIONS] + + nemelex_bonus) / 12; for (int i = 0; i < 1 + extra_targets; ++i) { @@ -1588,7 +1589,7 @@ static int _drain_monsters(int x, int y, int pow, int garbage) { simple_monster_message(&mon, " is drained."); - if (random2(20) < pow/60) + if (x_chance_in_y(pow / 60, 20)) { mon.hit_dice--; mon.experience = 0; @@ -1805,7 +1806,7 @@ static void _helm_card(int power, deck_rarity_type rarity) { // If there are n left, of which we need to choose // k, we have chance k/n of selecting the next item. - if ( random2(4-i) < num_resists ) + if (x_chance_in_y(num_resists, 4-i)) { // Add a temporary resistance. you.duration[possible_resists[i]] += random2(power/7) + 1; @@ -2171,9 +2172,10 @@ static void _sage_card(int power, deck_rarity_type rarity) if (you.skills[i] < MAX_SKILL_LEVEL) { - const int curweight = 1 + you.skills[i] * (40-you.skills[i]) * c; + // Choosing a skill is likelier if you are little skilled in it. + const int curweight = 1 + you.skills[i] * (40 - you.skills[i]) * c; totalweight += curweight; - if (random2(totalweight) < curweight) + if (x_chance_in_y(curweight, totalweight)) result = i; } } @@ -2183,8 +2185,8 @@ static void _sage_card(int power, deck_rarity_type rarity) else { you.duration[DUR_SAGE] = random2(1800) + 200; - you.sage_bonus_skill = static_cast(result); - you.sage_bonus_degree = power / 25; + you.sage_bonus_skill = static_cast(result); + you.sage_bonus_degree = power / 25; mprf(MSGCH_PLAIN, "You feel studious about %s.", skill_name(result)); } } @@ -2275,7 +2277,8 @@ static void _water_card(int power, deck_rarity_type rarity) static void _glass_card(int power, deck_rarity_type rarity) { const int power_level = get_power_level(power, rarity); - const int radius = (power_level == 2) ? 1000 : random2(power/40) + 2; + const int radius = (power_level == 2) ? 1000 + : random2(power/40) + 2; vitrify_area(radius); } @@ -2825,7 +2828,7 @@ bool card_effect(card_type which_card, deck_rarity_type rarity, if (coinflip()) { mprf("You have drawn %s.", card_name(which_card)); - your_spells(SPELL_OLGREBS_TOXIC_RADIANCE,random2(power/4), false); + your_spells(SPELL_OLGREBS_TOXIC_RADIANCE, random2(power/4), false); } else rc = _damaging_card(which_card, power, rarity); diff --git a/crawl-ref/source/dungeon.cc b/crawl-ref/source/dungeon.cc index 96c521479f..927e8fc600 100644 --- a/crawl-ref/source/dungeon.cc +++ b/crawl-ref/source/dungeon.cc @@ -1530,8 +1530,8 @@ static void _place_base_islands(int margin, int num_islands, int estradius, { centre_ok = true; - centres[i].x = a + random2(GXM-2*a-1); - centres[i].y = b + random2(GYM-2*b-1); + centres[i].x = a + random2(GXM - 2*a - 1); + centres[i].y = b + random2(GYM - 2*b - 1); for (int j = 0; j < i; ++j) { @@ -1544,7 +1544,7 @@ static void _place_base_islands(int margin, int num_islands, int estradius, break; } } - if (random2(num_islands) && island_distance) + if (island_distance && !one_chance_in(num_islands)) --island_distance; } while (!centre_ok); @@ -1589,27 +1589,27 @@ static void _prepare_shoals(int level_number) // Adding shallow water at deep water adjacent to floor. // Randomisation: place shallow water if at least 1d(1d3) floor neighbours - for ( int i = margin; i < GXM - margin; ++i) - for ( int j = margin; j < GYM - margin; ++j) + for (int i = margin; i < GXM - margin; ++i) + for (int j = margin; j < GYM - margin; ++j) if (grd[i][j] == DNGN_DEEP_WATER && count_neighbours(i, j, DNGN_FLOOR) > random2(random2(3)+1)) { grd[i][j] = DNGN_SHALLOW_WATER; } - // Placing sandbanks + // Placing sandbanks. for (int banks = 0; banks < 8; ++banks) { - int xsize = 3+random2(3); // random rectangle - int ysize = 3+random2(3); + int xsize = 3 + random2(3); // random rectangle + int ysize = 3 + random2(3); int xb = random2(GXM - 2 * margin - 10) + margin + 2; int yb = random2(GYM - 2 * margin - 10) + margin + 2; bool ok_place = true; - for ( int i = xb; i < xb + xsize; ++i ) - for ( int j = yb; j < yb + ysize; ++j ) + for (int i = xb; i < xb + xsize; ++i) + for (int j = yb; j < yb + ysize; ++j) { - if ( grd[i][j] != DNGN_DEEP_WATER ) + if (grd[i][j] != DNGN_DEEP_WATER) ok_place = false; } @@ -1639,7 +1639,7 @@ static void _prepare_shoals(int level_number) if ( grd[x][y] == DNGN_DEEP_WATER ) { int badness = count_neighbours(x, y, DNGN_WATER_STUCK); - if ( random2(badness) >= 2 && coinflip() ) + if (random2(badness) >= 2 && coinflip()) grd[x][y] = DNGN_LAVA; } @@ -1770,7 +1770,8 @@ static void _prepare_water( int level_number ) grd[i][j] = DNGN_SHALLOW_WATER; } else if (which_grid >= DNGN_FLOOR - && random2(100) < 80 - level_number * 4) + && x_chance_in_y(80 - level_number * 4, + 100)) { grd[i][j] = DNGN_SHALLOW_WATER; } @@ -2085,7 +2086,7 @@ static void _place_minivaults(const std::string &tag, int lo, int hi, bool force } int chance = you.your_level == 0? 50 : 100; - while (chance && random2(100) < chance || nvaults-- > 0) + while (chance && x_chance_in_y(chance, 100) || nvaults-- > 0) { const int vault = _dgn_random_map_for_place(true); if (vault == -1) @@ -2223,7 +2224,8 @@ static builder_rc_type _builder_basic(int level_number) int doorlevel = random2(11); int corrlength = 2 + random2(14); int roomsize = 4 + random2(5) + random2(6); - int no_corr = (one_chance_in(100) ? 500 + random2(500) : 30 + random2(200)); + int no_corr = (one_chance_in(100) ? 500 + random2(500) + : 30 + random2(200)); int intersect_chance = (one_chance_in(20) ? 400 : random2(20)); _make_trail( 35, 30, 35, 20, corrlength, intersect_chance, no_corr, @@ -2287,7 +2289,7 @@ static builder_rc_type _builder_basic(int level_number) } } - // make some more rooms: + // Make some more rooms. no_rooms = 1 + random2(3); max_doors = 1; @@ -2798,7 +2800,7 @@ static bool _make_room(int sx,int sy,int ex,int ey,int max_doors, int doorlevel) && grid_is_solid(grd[sx-1][ry-1]) && grid_is_solid(grd[sx-1][ry+1])) { - if (random2(10) < doorlevel) + if (x_chance_in_y(doorlevel, 10)) grd[sx-1][ry] = DNGN_CLOSED_DOOR; } @@ -2807,7 +2809,7 @@ static bool _make_room(int sx,int sy,int ex,int ey,int max_doors, int doorlevel) && grid_is_solid(grd[ex+1][ry-1]) && grid_is_solid(grd[ex+1][ry+1])) { - if (random2(10) < doorlevel) + if (x_chance_in_y(doorlevel, 10)) grd[ex+1][ry] = DNGN_CLOSED_DOOR; } } @@ -2820,7 +2822,7 @@ static bool _make_room(int sx,int sy,int ex,int ey,int max_doors, int doorlevel) && grid_is_solid(grd[rx-1][sy-1]) && grid_is_solid(grd[rx+1][sy-1])) { - if (random2(10) < doorlevel) + if (x_chance_in_y(doorlevel, 10)) grd[rx][sy-1] = DNGN_CLOSED_DOOR; } @@ -2829,7 +2831,7 @@ static bool _make_room(int sx,int sy,int ex,int ey,int max_doors, int doorlevel) && grid_is_solid(grd[rx-1][ey+1]) && grid_is_solid(grd[rx+1][ey+1])) { - if (random2(10) < doorlevel) + if (x_chance_in_y(doorlevel, 10)) grd[rx][ey+1] = DNGN_CLOSED_DOOR; } } @@ -5212,7 +5214,7 @@ static void _place_pool(dungeon_feature_type pool_type, unsigned char pool_x1, if (pool_x1 >= pool_x2 - 4 || pool_y1 >= pool_y2 - 4) return; - left_edge = pool_x1 + 2 + random2(pool_x2 - pool_x1); + left_edge = pool_x1 + 2 + random2(pool_x2 - pool_x1); right_edge = pool_x2 - 2 - random2(pool_x2 - pool_x1); for (j = pool_y1 + 1; j < pool_y2 - 1; j++) @@ -5980,7 +5982,7 @@ static char _plan_3() const int prev_ry2 = romy2[which_room - 1]; join_the_dots( coord_def(rx1 + random2( rx2 - rx1 ), - ry1 + random2( ry2 - ry1 )), + ry1 + random2( ry2 - ry1 )), coord_def(prev_rx1 + random2(prev_rx2 - prev_rx1), prev_ry1 + random2(prev_ry2 - prev_ry1)), MMT_VAULT ); @@ -6010,7 +6012,7 @@ static char _plan_3() const int prev_ry2 = romy2[i - 1]; join_the_dots( coord_def( rx1 + random2( rx2 - rx1 ), - ry1 + random2( ry2 - ry1 ) ), + ry1 + random2( ry2 - ry1 ) ), coord_def( prev_rx1 + random2( prev_rx2 - prev_rx1 ), prev_ry1 + random2( prev_ry2 - prev_ry1 ) ), @@ -6904,8 +6906,8 @@ static void _big_room(int level_number) if (level_number > 7) { - type_floor = ((random2(level_number) < 14) ? DNGN_DEEP_WATER - : DNGN_LAVA); + type_floor = (x_chance_in_y(14, level_number) ? DNGN_DEEP_WATER + : DNGN_LAVA); } octa_room(sr, oblique, type_floor); @@ -6923,8 +6925,8 @@ static void _big_room(int level_number) if (level_number > 7 && one_chance_in(4)) { - type_floor = ((random2(level_number) < 14) ? DNGN_DEEP_WATER - : DNGN_LAVA); + type_floor = (x_chance_in_y(14, level_number) ? DNGN_DEEP_WATER + : DNGN_LAVA); } // Make the big room. @@ -7032,15 +7034,15 @@ static void _roguey_level(int level_number, spec_room &sr, bool make_stairs) _replace_area( rox1[i], roy1[i], rox2[i], roy2[i], DNGN_ROCK_WALL, DNGN_FLOOR ); - // inner room? + // Inner room? if (rox2[i] - rox1[i] > 5 && roy2[i] - roy1[i] > 5 - && random2(100 - level_number) < 3) + && x_chance_in_y(3, 100 - level_number)) { if (!one_chance_in(4)) { _box_room( rox1[i] + 2, rox2[i] - 2, roy1[i] + 2, roy2[i] - 2, (coinflip() ? DNGN_STONE_WALL - : DNGN_ROCK_WALL) ); + : DNGN_ROCK_WALL) ); } else { @@ -7078,17 +7080,17 @@ static void _roguey_level(int level_number, spec_room &sr, bool make_stairs) { case 0: last_room = i - 1; - pos[0] = rox1[i]; // - 1; - pos[1] = roy1[i] + random2(roy2[i] - roy1[i]); + pos[0] = rox1[i]; // - 1; + pos[1] = roy1[i] + random2(roy2[i] - roy1[i]); jpos[0] = rox2[last_room]; // + 1; jpos[1] = roy1[last_room] - + random2(roy2[last_room] - roy1[last_room]); + + random2(roy2[last_room] - roy1[last_room]); break; case 1: last_room = i - 5; - pos[1] = roy1[i]; // - 1; - pos[0] = rox1[i] + random2(rox2[i] - rox1[i]); + pos[1] = roy1[i]; // - 1; + pos[0] = rox1[i] + random2(rox2[i] - rox1[i]); jpos[1] = roy2[last_room]; // + 1; jpos[0] = rox1[last_room] + random2(rox2[last_room] - rox1[last_room]); @@ -7697,10 +7699,10 @@ bool dgn_region::overlaps(const map_mask &mask) const coord_def dgn_region::random_edge_point() const { return random2(size.x + size.y) < size.x ? - coord_def( pos.x + random2(size.x), - coinflip()? pos.y : pos.y + size.y - 1 ) - : coord_def( coinflip()? pos.x : pos.x + size.x - 1, - pos.y + random2(size.y) ); + coord_def( pos.x + random2(size.x), + coinflip()? pos.y : pos.y + size.y - 1 ) + : coord_def( coinflip()? pos.x : pos.x + size.x - 1, + pos.y + random2(size.y) ); } coord_def dgn_region::random_point() const diff --git a/crawl-ref/source/effects.cc b/crawl-ref/source/effects.cc index 3b0c21128e..26d2847bef 100644 --- a/crawl-ref/source/effects.cc +++ b/crawl-ref/source/effects.cc @@ -888,7 +888,7 @@ static int _find_acquirement_subtype(object_class_type class_wanted, const int weight = you.skills[i] + 1; count += weight; - if (random2(count) < weight) + if (x_chance_in_y(weight, count)) skill = i; } @@ -909,7 +909,7 @@ static int _find_acquirement_subtype(object_class_type class_wanted, if (wskill == SK_THROWING) wskill = weapon_skill(OBJ_WEAPONS, i); - if (wskill == skill && random2(count += acqweight) < acqweight) + if (wskill == skill && x_chance_in_y(acqweight, count += acqweight)) type_wanted = i; } } @@ -923,7 +923,7 @@ static int _find_acquirement_subtype(object_class_type class_wanted, if (you.skills[i]) { count += you.skills[i]; - if (random2(count) < you.skills[i]) + if (x_chance_in_y(you.skills[i], count)) skill = i; } } @@ -1309,10 +1309,10 @@ static int _find_acquirement_subtype(object_class_type class_wanted, while (you.had_book[type_wanted]); } - // if the book is invalid find any valid one. + // If the book is invalid find any valid one. while (book_rarity(type_wanted) == 100 - || type_wanted == BOOK_DESTRUCTION - || type_wanted == BOOK_MANUAL) + || type_wanted == BOOK_DESTRUCTION + || type_wanted == BOOK_MANUAL) { type_wanted = random2(NUM_BOOKS); } @@ -1436,11 +1436,13 @@ static int _find_acquirement_subtype(object_class_type class_wanted, case OBJ_MISCELLANY: do + { type_wanted = random2(NUM_MISCELLANY); + } while (type_wanted == MISC_HORN_OF_GERYON - || type_wanted == MISC_RUNE_OF_ZOT - || type_wanted == MISC_CRYSTAL_BALL_OF_FIXATION - || type_wanted == MISC_EMPTY_EBONY_CASKET); + || type_wanted == MISC_RUNE_OF_ZOT + || type_wanted == MISC_CRYSTAL_BALL_OF_FIXATION + || type_wanted == MISC_EMPTY_EBONY_CASKET); break; default: break; @@ -2210,7 +2212,7 @@ static void _hell_effects() else if (temp_rand > 7) // 10 in 27 odds {dlb} { // 60:40 miscast:summon split {dlb} - summon_instead = (random2(5) > 2); + summon_instead = x_chance_in_y(2, 5); switch (you.where_are_you) { @@ -2486,11 +2488,11 @@ void handle_time(long time_delta) // about 1.5 points on average, so they can corrupt the player // quite quickly. Wielding one for a short battle is OK, which is // as things should be. -- GDL - if (you.duration[DUR_INVIS] && random2(10) < 6) + if (you.duration[DUR_INVIS] && x_chance_in_y(6, 10)) added_contamination++; if (you.duration[DUR_HASTE] && !you.duration[DUR_BERSERKER] - && random2(10) < 6) + && x_chance_in_y(6, 10)) { added_contamination++; } @@ -2637,7 +2639,7 @@ void handle_time(long time_delta) break; } - if (random2(100) < total_skill) + if (x_chance_in_y(total_skill, 100)) { item_def& item = you.inv[you.equip[EQ_WEAPON]]; @@ -3016,7 +3018,7 @@ void update_corpses(double elapsedTime) } int fountain_checks = static_cast(elapsedTime / 1000.0); - if (random2(1000) < static_cast(elapsedTime) % 1000) + if (x_chance_in_y(static_cast(elapsedTime) % 1000, 1000)) fountain_checks += 1; // dry fountains may start flowing again diff --git a/crawl-ref/source/fight.cc b/crawl-ref/source/fight.cc index 0864f666bc..18be80e265 100644 --- a/crawl-ref/source/fight.cc +++ b/crawl-ref/source/fight.cc @@ -112,7 +112,7 @@ bool test_melee_hit(int to_hit, int ev) if (to_hit >= AUTOMATIC_HIT) return (true); - else if (random2(100) < MIN_HIT_MISS_PERCENTAGE) + else if (x_chance_in_y(MIN_HIT_MISS_PERCENTAGE, 100)) margin = (coinflip() ? 1 : -1) * AUTOMATIC_HIT; else { @@ -247,15 +247,17 @@ int calc_heavy_armour_penalty( bool random_factor ) } } - // heavy armour modifiers for PARM_EVASION + // Heavy armour modifiers for PARM_EVASION. if (you.equip[EQ_BODY_ARMOUR] != -1) { const int ev_pen = property( you.inv[you.equip[EQ_BODY_ARMOUR]], PARM_EVASION ); - if (ev_pen < 0 && - maybe_random2(you.skills[SK_ARMOUR], random_factor) < abs(ev_pen)) + if (ev_pen < 0 && maybe_random2(you.skills[SK_ARMOUR], + random_factor) < abs(ev_pen)) + { heavy_armour += maybe_random2( abs(ev_pen), random_factor ); + } } // ??? what is the reasoning behind this ??? {dlb} @@ -273,14 +275,14 @@ int calc_heavy_armour_penalty( bool random_factor ) heavy_armour /= 2; } } - return heavy_armour; + return (heavy_armour); } static bool player_fights_well_unarmed(int heavy_armour_penalty) { return (you.burden_state == BS_UNENCUMBERED - && random2(20) < you.skills[SK_UNARMED_COMBAT] - && random2(1 + heavy_armour_penalty) < 2); + && x_chance_in_y(you.skills[SK_UNARMED_COMBAT], 20) + && x_chance_in_y(2, 1 + heavy_armour_penalty)); } unchivalric_attack_type is_unchivalric_attack(const actor *attacker, diff --git a/crawl-ref/source/it_use2.cc b/crawl-ref/source/it_use2.cc index 8953c4eb1c..3f6e8df4b3 100644 --- a/crawl-ref/source/it_use2.cc +++ b/crawl-ref/source/it_use2.cc @@ -81,7 +81,7 @@ bool potion_effect( potion_type pot_eff, int pow, bool was_known ) mpr("You feel much better."); // only fix rot when healed to full - if ( you.hp == you.hp_max) + if (you.hp == you.hp_max) { unrot_hp( (2 + random2avg(5, 2)) / factor ); set_hp(you.hp_max, false); @@ -148,7 +148,7 @@ bool potion_effect( potion_type pot_eff, int pow, bool was_known ) mprf(MSGCH_DURATION, "You feel %s all of a sudden.", were_mighty ? "mightier" : "very mighty"); - if ( were_mighty ) + if (were_mighty) contaminate_player(1, was_known); else modify_stat(STAT_STRENGTH, 5, true, ""); @@ -180,8 +180,8 @@ bool potion_effect( potion_type pot_eff, int pow, bool was_known ) break; case POT_LEVITATION: - mprf("You feel %s buoyant.", - (!player_is_airborne()) ? "very" : "more"); + mprf("You feel %s buoyant.", !player_is_airborne() ? "very" + : "more"); if (!player_is_airborne()) mpr("You gently float upwards from the floor."); @@ -228,8 +228,8 @@ bool potion_effect( potion_type pot_eff, int pow, bool was_known ) break; case POT_INVISIBILITY: - mpr( (!you.duration[DUR_INVIS]) ? "You fade into invisibility!" - : "You fade further into invisibility." ); + mpr(!you.duration[DUR_INVIS] ? "You fade into invisibility!" + : "You fade further into invisibility."); // Invisibility cancels backlight. you.duration[DUR_BACKLIGHT] = 0; @@ -260,11 +260,14 @@ bool potion_effect( potion_type pot_eff, int pow, bool was_known ) break; case POT_DEGENERATION: - if ( pow == 40 ) + if (pow == 40) mpr("There was something very wrong with that liquid!"); + if (lose_stat(STAT_RANDOM, (1 + random2avg(4, 2)) / factor, false, "drinking a potion of degeneration")) + { xom_is_stimulated(64 / factor); + } break; // Don't generate randomly - should be rare and interesting. @@ -320,7 +323,7 @@ bool potion_effect( potion_type pot_eff, int pow, bool was_known ) nothing_happens = false; } - // give a message if no message otherwise + // Give a message if no message otherwise. if (!restore_stat(STAT_ALL, 0, false) && nothing_happens) mpr( "You feel refreshed." ); break; @@ -331,7 +334,7 @@ bool potion_effect( potion_type pot_eff, int pow, bool was_known ) mpr("You feel slightly irritated."); make_hungry(100, false); } - else if ( you.duration[DUR_BUILDING_RAGE] ) + else if (you.duration[DUR_BUILDING_RAGE]) { you.duration[DUR_BUILDING_RAGE] = 0; mpr("Your blood cools."); @@ -346,7 +349,7 @@ bool potion_effect( potion_type pot_eff, int pow, bool was_known ) case POT_CURE_MUTATION: mpr("It has a very clean taste."); for (int i = 0; i < 7; i++) - if (random2(10) > i) + if (x_chance_in_y(i, 10)) delete_mutation(RANDOM_MUTATION, false); break; @@ -614,8 +617,6 @@ void unwear_armour(char unw) if (is_random_artefact( you.inv[unw] )) unuse_randart(unw); - - return; } void unuse_randart(unsigned char unw) diff --git a/crawl-ref/source/it_use3.cc b/crawl-ref/source/it_use3.cc index 58949f87a8..a247b568f5 100644 --- a/crawl-ref/source/it_use3.cc +++ b/crawl-ref/source/it_use3.cc @@ -170,10 +170,10 @@ void special_wielded() case SPWLD_VARIABLE: do_uncurse_item( you.inv[wpn] ); - if (random2(5) < 2) // 40% chance {dlb} + if (x_chance_in_y(2, 5)) // 40% chance {dlb} you.inv[wpn].plus += (coinflip() ? +1 : -1); - if (random2(5) < 2) // 40% chance {dlb} + if (x_chance_in_y(2, 5)) // 40% chance {dlb} you.inv[wpn].plus2 += (coinflip() ? +1 : -1); if (you.inv[wpn].plus < -4) @@ -206,14 +206,14 @@ void special_wielded() break; case SPWLD_POWER: - you.inv[wpn].plus = stepdown_value( -4 + (you.hp / 5), 4, 4, 4, 20 ); + you.inv[wpn].plus = stepdown_value( -4 + (you.hp / 5), 4, 4, 4, 20 ); you.inv[wpn].plus2 = you.inv[wpn].plus; break; case SPWLD_OLGREB: // Giving Olgreb's staff a little lift since staves of poison have // been made better. -- bwr - you.inv[wpn].plus = you.skills[SK_POISON_MAGIC] / 3; + you.inv[wpn].plus = you.skills[SK_POISON_MAGIC] / 3; you.inv[wpn].plus2 = you.inv[wpn].plus; break; @@ -248,8 +248,6 @@ void special_wielded() { you.wield_change = true; } - - return; } // end special_wielded() static bool _reaching_weapon_attack(const item_def& wpn) @@ -310,7 +308,7 @@ static bool _reaching_weapon_attack(const item_def& wpn) { const int skill = weapon_skill( wpn.base_type, wpn.sub_type ); - if ((5 + (3 * skill)) > random2(40)) + if (x_chance_in_y(5 + (3 * skill), 40)) { mpr("You reach to attack!"); success = you_attack(mgrd[beam.tx][beam.ty], false); @@ -326,6 +324,7 @@ static bool _reaching_weapon_attack(const item_def& wpn) mpr("You reach to attack!"); success = you_attack(mgrd[beam.tx][beam.ty], false); } + if (success) { int mid = mgrd[beam.tx][beam.ty]; @@ -353,7 +352,6 @@ static bool evoke_horn_of_geryon() mpr("You produce a weird and mournful sound."); for (int count_x = 0; count_x < GXM; count_x++) - { for (int count_y = 0; count_y < GYM; count_y++) { if (grd[count_x][count_y] == DNGN_STONE_ARCH) @@ -373,7 +371,6 @@ static bool evoke_horn_of_geryon() } } } - } if (rc) mpr("Your way has been unbarred."); @@ -385,17 +382,17 @@ static bool evoke_horn_of_geryon() mgen_data(MONS_BEAST, BEH_HOSTILE, 4, you.pos(), MHITYOU)); } - return rc; + return (rc); } static bool evoke_sceptre_of_asmodeus() { bool rc = true; - if ( one_chance_in(21) ) + if (one_chance_in(21)) rc = false; - else if (one_chance_in(2)) + else if (coinflip()) { - // summon devils, maybe a Fiend + // Summon devils, maybe a Fiend. const monster_type mtype = (one_chance_in(4) ? MONS_FIEND : summon_any_demon(DEMON_COMMON)); const bool good_summon = @@ -427,13 +424,13 @@ static bool evoke_sceptre_of_asmodeus() return (true); } -// returns true if item successfully evoked. +// Returns true if item successfully evoked. bool evoke_wielded() { int power = 0; - int pract = 0; - bool did_work = false; // used for default "nothing happens" message + int pract = 0; // By how much Evocations is practised. + bool did_work = false; // Used for default "nothing happens" message. const int wield = you.equip[EQ_WEAPON]; @@ -493,7 +490,7 @@ bool evoke_wielded() break; case SPWPN_SCEPTRE_OF_ASMODEUS: - if ( evoke_sceptre_of_asmodeus() ) + if (evoke_sceptre_of_asmodeus()) { make_hungry(200, false, true); did_work = true; @@ -597,7 +594,7 @@ bool evoke_wielded() case OBJ_MISCELLANY: did_work = true; // easier to do it this way for misc items - if ( is_deck(wpn) ) + if (is_deck(wpn)) { evoke_deck(wpn); pract = 1; @@ -647,7 +644,7 @@ bool evoke_wielded() break; case MISC_HORN_OF_GERYON: - if ( evoke_horn_of_geryon() ) + if (evoke_horn_of_geryon()) pract = 1; break; @@ -735,8 +732,8 @@ static bool ball_of_seeing(void) mpr("You gaze into the crystal ball."); - use = ((!you.duration[DUR_CONF]) ? - random2(you.skills[SK_EVOCATIONS] * 6) : random2(5)); + use = (!you.duration[DUR_CONF] ? random2(you.skills[SK_EVOCATIONS] * 6) + : random2(5)); if (use < 2) { @@ -778,11 +775,11 @@ static bool disc_of_storms(void) const int fail_rate = (30 - you.skills[SK_EVOCATIONS]); bool ret = false; - if (player_res_electricity() || (random2(100) < fail_rate)) + if (player_res_electricity() || x_chance_in_y(fail_rate, 100)) canned_msg(MSG_NOTHING_HAPPENS); - else if (random2(100) < fail_rate) + else if (x_chance_in_y(fail_rate, 100)) mpr("The disc glows for a moment, then fades."); - else if (random2(100) < fail_rate) + else if (x_chance_in_y(fail_rate, 100)) mpr("Little bolts of electricity crackle over the disc."); else { @@ -805,7 +802,7 @@ static bool disc_of_storms(void) beam.target_x = you.x_pos + random2(13) - 6; beam.target_y = you.y_pos + random2(13) - 6; - // non-controlleable + // Non-controlleable, so no player tracer. zapping( which_zap, 30 + you.skills[SK_EVOCATIONS] * 2, beam ); disc_count--; @@ -815,7 +812,7 @@ static bool disc_of_storms(void) } return (ret); -} // end disc_of_storms() +} void tome_of_power(int slot) { @@ -833,7 +830,7 @@ void tome_of_power(int slot) set_ident_flags( you.inv[slot], ISFLAG_KNOW_TYPE ); if (player_mutation_level(MUT_BLURRY_VISION) > 0 - && random2(4) < player_mutation_level(MUT_BLURRY_VISION)) + && x_chance_in_y(player_mutation_level(MUT_BLURRY_VISION), 4)) { mpr("The page is too blurry for you to read."); return; @@ -842,27 +839,27 @@ void tome_of_power(int slot) mpr("You find yourself reciting the magical words!"); exercise( SK_EVOCATIONS, 1 ); - if ( random2(50) < 7 ) + if (x_chance_in_y(7, 50)) { mpr("A cloud of weird smoke pours from the book's pages!"); big_cloud( random_smoke_type(), KC_YOU, you.x_pos, you.y_pos, 20, 10 + random2(8) ); xom_is_stimulated(16); } - else if ( random2(43) < 2 ) + else if (x_chance_in_y(2, 43)) { mpr("A cloud of choking fumes pours from the book's pages!"); big_cloud(CLOUD_POISON, KC_YOU, you.x_pos, you.y_pos, 20, 7 + random2(5)); xom_is_stimulated(64); } - else if ( random2(41) < 2 ) + else if (x_chance_in_y(2, 41)) { mpr("A cloud of freezing gas pours from the book's pages!"); big_cloud(CLOUD_COLD, KC_YOU, you.x_pos, you.y_pos, 20, 8 + random2(5)); xom_is_stimulated(64); } - else if ( random2(39) < 3 ) + else if (x_chance_in_y(3, 39)) { if (one_chance_in(5)) { @@ -1035,7 +1032,7 @@ static bool ball_of_energy(void) { lose_stat(STAT_INTELLIGENCE, 1, false, "using a ball of energy"); } - else if ((use < 4 && enough_mp(1, true)) + else if (use < 4 && enough_mp(1, true) || you.magic_points == you.max_magic_points) { mpr( "You feel your power drain away!" ); diff --git a/crawl-ref/source/item_use.cc b/crawl-ref/source/item_use.cc index 49855a0b41..568645fc89 100644 --- a/crawl-ref/source/item_use.cc +++ b/crawl-ref/source/item_use.cc @@ -135,8 +135,7 @@ bool can_wield(const item_def *weapon, bool say_reason, if (weapon->base_type != OBJ_WEAPONS) return (true); - if ((you.species < SP_OGRE || you.species > SP_OGRE_MAGE) - && item_mass( *weapon ) >= 300) + if (player_size(PSIZE_TORSO) < SIZE_LARGE && item_mass( *weapon ) >= 300) { SAY(mpr("That's too large and heavy for you to wield.")); return (false); @@ -164,8 +163,7 @@ bool can_wield(const item_def *weapon, bool say_reason, int weap_brand = get_weapon_brand( *weapon ); if ((you.is_undead || you.species == SP_DEMONSPAWN) - && (weap_brand == SPWPN_HOLY_WRATH - || is_blessed_blade(*weapon))) + && (weap_brand == SPWPN_HOLY_WRATH || is_blessed_blade(*weapon))) { SAY(mpr("This weapon will not allow you to wield it.")); return (false); @@ -376,10 +374,12 @@ static void warn_rod_shield_interference(const item_def &) // Any way to avoid the double entendre? :-) if (leak_degree) + { mprf(MSGCH_WARN, "Your %s %sreduces the effectiveness of your rod.", shield_base_name(player_shield()), leak_degree); + } } static void warn_launcher_shield_slowdown(const item_def &launcher) @@ -394,11 +394,14 @@ static void warn_launcher_shield_slowdown(const item_def &launcher) { const char *slow_degree = shield_impact_degree(slowspeed * 100 / normspeed); + if (slow_degree) + { mprf(MSGCH_WARN, "Your %s %sslows your rate of fire.", shield_base_name(player_shield()), slow_degree); + } } } @@ -4285,7 +4288,7 @@ void read_scroll( int slot ) // Imperfect vision prevents players from reading actual content {dlb}: if (player_mutation_level(MUT_BLURRY_VISION) - && random2(5) < player_mutation_level(MUT_BLURRY_VISION)) + && x_chance_in_y(player_mutation_level(MUT_BLURRY_VISION), 5)) { mpr((player_mutation_level(MUT_BLURRY_VISION) == 3 && one_chance_in(3)) ? "This scroll appears to be blank." diff --git a/crawl-ref/source/maps.cc b/crawl-ref/source/maps.cc index 426136969b..1eb3685d0a 100644 --- a/crawl-ref/source/maps.cc +++ b/crawl-ref/source/maps.cc @@ -376,7 +376,7 @@ int random_map_for_place(const level_id &place, bool want_minivault) { rollsize += vdefs[i].chance; - if (rollsize && random2(rollsize) < vdefs[i].chance) + if (rollsize && x_chance_in_y(vdefs[i].chance, rollsize)) mapindex = i; } } @@ -417,7 +417,7 @@ int random_map_in_depth(const level_id &place, bool want_minivault) { rollsize += vdefs[i].chance; - if (rollsize && random2(rollsize) < vdefs[i].chance) + if (rollsize && x_chance_in_y(vdefs[i].chance, rollsize)) mapindex = i; } @@ -444,7 +444,7 @@ int random_map_for_tag(const std::string &tag, { rollsize += vdefs[i].chance; - if (rollsize && random2(rollsize) < vdefs[i].chance) + if (rollsize && x_chance_in_y(vdefs[i].chance, rollsize)) mapindex = i; } } diff --git a/crawl-ref/source/mgrow.cc b/crawl-ref/source/mgrow.cc index bdaf066465..40025ee814 100644 --- a/crawl-ref/source/mgrow.cc +++ b/crawl-ref/source/mgrow.cc @@ -89,7 +89,7 @@ static const monster_level_up *_monster_level_up_target( monster_type type, { const monsterentry *me = get_monster_data(mlup.after); if (static_cast(me->hpdice[0]) == hit_dice - && random2(1000) < mlup.chance) + && x_chance_in_y(mlup.chance, 1000)) { return (&mlup); } diff --git a/crawl-ref/source/monplace.cc b/crawl-ref/source/monplace.cc index c03517a98d..4d6a8b2ba7 100644 --- a/crawl-ref/source/monplace.cc +++ b/crawl-ref/source/monplace.cc @@ -205,7 +205,7 @@ static void _hell_spawn_random_monsters() if (env.turns_on_level > taper_off_turn) { genodds += (env.turns_on_level - taper_off_turn); - genodds = (genodds < 0 ? 20000 : std::min(genodds, 20000)); + genodds = (genodds < 0 ? 20000 : std::min(genodds, 20000)); } if (one_chance_in(genodds)) @@ -538,7 +538,7 @@ static int _is_near_stairs(coord_def &p) return (result); } -static bool _valid_monster_location(const mgen_data &mg, +static bool _valid_monster_location(const mgen_data &mg, const coord_def &mg_pos) { const int htype = (mons_class_is_zombified(mg.cls) ? mg.base_type diff --git a/crawl-ref/source/mstuff2.cc b/crawl-ref/source/mstuff2.cc index 9bf6cde740..cc0303ea60 100644 --- a/crawl-ref/source/mstuff2.cc +++ b/crawl-ref/source/mstuff2.cc @@ -187,7 +187,8 @@ void mons_trap(monsters *monster) } if (random2(monster->ev) > 8 - || trapKnown && intelligent_ally(monster) && random2(monster->ev) > 8) + || trapKnown && intelligent_ally(monster) + && random2(monster->ev) > 8) { if (monsterNearby && !simple_monster_message(monster, " avoids a huge, swinging blade.")) @@ -241,7 +242,8 @@ void mons_trap(monsters *monster) } if (random2(monster->ev) > 8 - || trapKnown && intelligent_ally(monster) && random2(monster->ev) > 8) + || trapKnown && intelligent_ally(monster) + && random2(monster->ev) > 8) { if (monsterNearby && !simple_monster_message(monster, " nimbly jumps out of the way of a falling net.")) @@ -287,6 +289,7 @@ void mons_trap(monsters *monster) mpr("The power of Zot is invoked against you!"); miscast_effect( SPTYP_RANDOM, 10 + random2(30), 75 + random2(100), 0, "the power of Zot" ); + return; // early return {dlb} } } @@ -301,8 +304,8 @@ void mons_trap(monsters *monster) mpr("You hear a distant \"Zot\"!", MSGCH_SOUND); } - // determine trap effects upon monster, based upon - // whether it is naughty or nice to the player: {dlb} + // Determine trap effects upon monster, based upon + // whether it is naughty or nice to the player. {dlb} // NB: beem[].colour values are mislabeled as colours (?) - // cf. mons_ench_f2() [which are also mislabeled] {dlb} @@ -378,7 +381,7 @@ void mons_trap(monsters *monster) if (beem.colour == OBJ_MISSILES && beem.type == MI_NEEDLE - && random2(100) < 50 - (3*monster->ac/2)) + && x_chance_in_y(50 - (3 * monster->ac)/ 2, 100)) { apply_poison = true; } @@ -400,8 +403,8 @@ void mons_trap(monsters *monster) if (apply_poison) poison_monster( monster, KC_OTHER ); - // generate "fallen" projectile, where appropriate: {dlb} - if (random2(10) < 7) + // Generate "fallen" projectile, where appropriate. {dlb} + if (x_chance_in_y(7, 10)) { beem.target_x = monster->x; beem.target_y = monster->y; diff --git a/crawl-ref/source/mutation.cc b/crawl-ref/source/mutation.cc index af0d204581..ff05e986dd 100644 --- a/crawl-ref/source/mutation.cc +++ b/crawl-ref/source/mutation.cc @@ -1633,8 +1633,9 @@ static bool accept_mutation(mutation_type mutat, bool ignore_rarity = false) return (true); const int rarity = mutation_defs[mutat].rarity + you.demon_pow[mutat]; + // Low rarity means unlikely to choose it. - return (rarity > random2(10)); + return (x_chance_in_y(rarity, 10)); } static mutation_type get_random_xom_mutation() @@ -1687,7 +1688,7 @@ static mutation_type get_random_mutation(bool prefer_good, cweight += weight; - if (random2(cweight) < weight) + if (x_chance_in_y(weight, cweight)) chosen = curr; } return (chosen); @@ -1718,7 +1719,7 @@ bool mutate(mutation_type which_mutation, bool failMsg, } // Zin's protection. - if (you.religion == GOD_ZIN && you.piety > random2(MAX_PIETY)) + if (you.religion == GOD_ZIN && x_chance_in_y(you.piety, MAX_PIETY)) { simple_god_message(" protects your body from chaos!"); return (false); @@ -1781,7 +1782,8 @@ bool mutate(mutation_type which_mutation, bool failMsg, if (which_mutation == RANDOM_MUTATION || which_mutation == RANDOM_XOM_MUTATION) { - if (random2(15) < how_mutated(false, true)) + // If already heavily mutated, remove a mutation instead. + if (x_chance_in_y(how_mutated(false, true), 15)) { // God gifts override mutation loss due to being heavily // mutated. diff --git a/crawl-ref/source/ouch.cc b/crawl-ref/source/ouch.cc index 4ff2e61713..e60da7c57b 100644 --- a/crawl-ref/source/ouch.cc +++ b/crawl-ref/source/ouch.cc @@ -233,7 +233,7 @@ int check_your_resists(int hurted, beam_type flavour) break; case BEAM_MIASMA: - if (player_prot_life() > random2(3)) + if (x_chance_in_y(player_prot_life(), 3)) { canned_msg(MSG_YOU_RESIST); hurted = 0; @@ -398,8 +398,8 @@ void item_corrode( int itco ) // {dlb} if (chance_corr >= 0 && chance_corr <= 4) { - it_resists = (random2(100) < - 2 + (4 << chance_corr) + (chance_corr * 8)); + it_resists + = x_chance_in_y(2 + (4 << chance_corr) + chance_corr * 8, 100); } else it_resists = true; // no idea how often this occurs {dlb} @@ -497,7 +497,7 @@ static void _expose_invent_to_element(beam_type flavour, int strength) for (int j = 0; j < you.inv[i].quantity; ++j) { - if (random2(100) < strength) + if (x_chance_in_y(strength, 100)) { num_dest++; diff --git a/crawl-ref/source/player.cc b/crawl-ref/source/player.cc index 3057aedbbf..4ded8c08f8 100644 --- a/crawl-ref/source/player.cc +++ b/crawl-ref/source/player.cc @@ -5954,10 +5954,10 @@ bool player::alive() const bool player::fumbles_attack(bool verbose) { - // fumbling in shallow water : + // Fumbling in shallow water. if (floundering()) { - if (random2(dex) < 4 || one_chance_in(5)) + if (x_chance_in_y(4, dex) || one_chance_in(5)) { if (verbose) mpr("Unstable footing causes you to fumble your attack."); @@ -6008,9 +6008,9 @@ void player::attacking(actor *other) pet_target = monster_index(mon); } - if (mutation[MUT_BERSERK] - && (random2(100) < (mutation[MUT_BERSERK] * 10) - 5) - || _equipment_make_berserk()) + if (player_mutation_level(MUT_BERSERK) + && x_chance_in_y(player_mutation_level(MUT_BERSERK) * 10 - 5, 100) + || _equipment_make_berserk()) { go_berserk(false); } diff --git a/crawl-ref/source/randart.cc b/crawl-ref/source/randart.cc index 15396bfd7c..63a85194a6 100644 --- a/crawl-ref/source/randart.cc +++ b/crawl-ref/source/randart.cc @@ -194,7 +194,7 @@ static std::string _replace_name_parts(const std::string name_in, std::string place; if (one_chance_in(5)) { - switch(random2(8)) + switch (random2(8)) { case 0: case 1: @@ -804,7 +804,7 @@ void static _get_randart_properties(const item_def &item, if (random2(15) >= power_level && aclass != OBJ_WEAPONS && (aclass != OBJ_JEWELLERY || atype != RING_SLAYING)) { - // Weapons and rings of slaying can't get these + // Weapons and rings of slaying can't get these. if (one_chance_in(4 + power_level)) // to-hit { proprt[RAP_ACCURACY] = 1 + random2(3) + random2(2); @@ -867,7 +867,7 @@ void static _get_randart_properties(const item_def &item, power_level++; } - if (random2(12) < power_level || power_level > 7) + if (x_chance_in_y(power_level, 12) || power_level > 7) done_powers = true; // res_elec @@ -923,7 +923,7 @@ void static _get_randart_properties(const item_def &item, power_level++; } - if (random2(12) < power_level || power_level > 10) + if (x_chance_in_y(power_level, 12) || power_level > 10) done_powers = true; // turn invis @@ -981,7 +981,7 @@ void static _get_randart_properties(const item_def &item, if (aclass == OBJ_ARMOUR) power_level -= 4; - if (power_level >= 2 && random2(17) < power_level) + if (power_level >= 2 && x_chance_in_y(power_level, 17)) { switch (random2(9)) { @@ -1488,8 +1488,8 @@ bool make_item_fixed_artefact( item_def &item, bool in_abyss, int which ) if (!force) { - which = SPWPN_START_FIXEDARTS + - random2(SPWPN_START_NOGEN_FIXEDARTS - SPWPN_START_FIXEDARTS); + which = SPWPN_START_FIXEDARTS + + random2(SPWPN_START_NOGEN_FIXEDARTS - SPWPN_START_FIXEDARTS); } const unique_item_status_type status = diff --git a/crawl-ref/source/religion.cc b/crawl-ref/source/religion.cc index 28861f3f46..094dc87a02 100644 --- a/crawl-ref/source/religion.cc +++ b/crawl-ref/source/religion.cc @@ -2652,7 +2652,7 @@ bool did_god_conduct(conduct_type thing_done, int level, bool known, // use up the deck, and 80% on a card which does use up the // deck. int chance = 0; - switch(level) + switch (level) { case 0: chance = 0; break; case 1: chance = 40; break; @@ -2661,7 +2661,8 @@ bool did_god_conduct(conduct_type thing_done, int level, bool known, case 3: chance = 100; break; } - if (random2(100) < chance && you.attribute[ATTR_CARD_COUNTDOWN]) + if (x_chance_in_y(chance, 100) + && you.attribute[ATTR_CARD_COUNTDOWN]) { you.attribute[ATTR_CARD_COUNTDOWN]--; #if DEBUG_DIAGNOSTICS || DEBUG_CARDS || DEBUG_GIFTS @@ -3446,10 +3447,11 @@ static bool _zin_retribution() int punishment = random2(10); // if little mutated or can't unmutate, do something else instead - if (punishment < 2 && (how_mutated() <= random2(3) - || player_mutation_level(MUT_MUTATION_RESISTANCE) == 3)) + if (punishment < 2 + && (how_mutated() <= random2(3) + || player_mutation_level(MUT_MUTATION_RESISTANCE) == 3)) { - punishment = random2(8)+2; + punishment = random2(8) + 2; } switch (punishment) @@ -3460,16 +3462,13 @@ static bool _zin_retribution() simple_god_message(" draws some chaos from your body!", god); bool success = false; for (int i = 0; i < 7; ++i) - if (random2(10) > i - && delete_mutation(RANDOM_GOOD_MUTATION)) - { + if (x_chance_in_y(i, 10) && delete_mutation(RANDOM_GOOD_MUTATION)) success = true; - } if (success && !how_mutated()) { simple_god_message(" rids your body of chaos!", god); - // lower penance a bit more for being particularly successful + // Lower penance a bit more for being particularly successful. dec_penance(god, 1); } break; diff --git a/crawl-ref/source/spells1.cc b/crawl-ref/source/spells1.cc index 9df1997998..6c8be52d3c 100644 --- a/crawl-ref/source/spells1.cc +++ b/crawl-ref/source/spells1.cc @@ -1181,10 +1181,8 @@ bool cast_revivification(int pow) loss = 2; for (loopy = 0; loopy < 9; loopy++) - { - if (random2(pow) < 8) + if (x_chance_in_y(8, pow)) loss++; - } dec_max_hp(loss); set_hp(you.hp_max, false); @@ -1242,7 +1240,8 @@ void cast_deaths_door(int pow) set_hp( allowed_deaths_door_hp(), false ); deflate_hp( you.hp_max, false ); - you.duration[DUR_DEATHS_DOOR] = 10 + random2avg(13, 3) + (random2(pow) / 10); + you.duration[DUR_DEATHS_DOOR] = 10 + random2avg(13, 3) + + (random2(pow) / 10); if (you.duration[DUR_DEATHS_DOOR] > 25) you.duration[DUR_DEATHS_DOOR] = 23 + random2(5); @@ -1334,7 +1333,7 @@ void antimagic() if ( you.duration[dur_list[i]] > 1 ) you.duration[dur_list[i]] = 1; - contaminate_player( -1 * (1+random2(5))); + contaminate_player( -1 * (1 + random2(5))); } // end antimagic() void extension(int pow) @@ -1691,7 +1690,7 @@ bool cast_sure_blade(int power) } return (success); -} // end cast_sure_blade() +} void manage_fire_shield(void) { @@ -1703,7 +1702,6 @@ void manage_fire_shield(void) char stx = 0, sty = 0; for (stx = -1; stx < 2; stx++) - { for (sty = -1; sty < 2; sty++) { if (sty == 0 && stx == 0) @@ -1718,5 +1716,4 @@ void manage_fire_shield(void) 1 + random2(6), KC_YOU ); } } - } -} // end manage_fire_shield() +} diff --git a/crawl-ref/source/spells2.cc b/crawl-ref/source/spells2.cc index 797e233c43..3955799d24 100644 --- a/crawl-ref/source/spells2.cc +++ b/crawl-ref/source/spells2.cc @@ -159,7 +159,7 @@ static bool _mark_detected_creature(int gridx, int gridy, const monsters *mon, bool found_good = false; - if (fuzz_radius && fuzz_chance > random2(100)) + if (fuzz_radius && x_chance_in_y(fuzz_chance, 100)) { const int fuzz_diam = 2 * fuzz_radius + 1; @@ -1154,7 +1154,8 @@ bool cast_sticks_to_snakes(int pow, god_type god) if (one_chance_in(5 - std::min(4, div_rand_round(pow * 2, 25))) || get_ammo_brand(you.inv[wpn]) == SPMSL_POISONED) { - mon = random2(100) < pow / 3 ? MONS_BROWN_SNAKE : MONS_SNAKE; + mon = x_chance_in_y(pow / 3, 100) ? MONS_BROWN_SNAKE + : MONS_SNAKE; } else mon = MONS_SMALL_SNAKE; @@ -1518,17 +1519,18 @@ bool cast_summon_elemental(int pow, god_type god, // - Earth elementals are more static and easy to tame (as before). // - Fire elementals fall in between the two (10 is still fairly easy). const bool friendly = ((mon != MONS_FIRE_ELEMENTAL - || random2(10) < you.skills[SK_FIRE_MAGIC]) + || x_chance_in_y(you.skills[SK_FIRE_MAGIC], 10)) && (mon != MONS_WATER_ELEMENTAL - || random2((you.species == SP_MERFOLK) ? 5 : 15) - < you.skills[SK_ICE_MAGIC]) + || x_chance_in_y(you.skills[SK_ICE_MAGIC], + (you.species == SP_MERFOLK) ? 5 + : 15)) && (mon != MONS_AIR_ELEMENTAL - || random2(15) < you.skills[SK_AIR_MAGIC]) + || x_chance_in_y(you.skills[SK_AIR_MAGIC], 15)) && (mon != MONS_EARTH_ELEMENTAL - || random2(5) < you.skills[SK_EARTH_MAGIC]) + || x_chance_in_y(you.skills[SK_EARTH_MAGIC], 5)) && random2(100) >= unfriendly); diff --git a/crawl-ref/source/spells3.cc b/crawl-ref/source/spells3.cc index d866db7181..aabfc95a42 100644 --- a/crawl-ref/source/spells3.cc +++ b/crawl-ref/source/spells3.cc @@ -97,10 +97,9 @@ bool cast_selective_amnesia(bool force) mpr( "You don't know that spell." ); else { - if (!force - && (you.religion != GOD_SIF_MUNA - && random2(you.skills[SK_SPELLCASTING]) - < random2(spell_difficulty( spell )))) + if (!force && you.religion != GOD_SIF_MUNA + && random2(you.skills[SK_SPELLCASTING]) + < random2(spell_difficulty( spell ))) { mpr("Oops! This spell sure is a blunt instrument."); forget_map(20 + random2(50)); @@ -397,12 +396,10 @@ bool cast_sublimation_of_blood(int pow) food += 15; for (int loopy = 0; loopy < (you.hp > 1 ? 3 : 0); ++loopy) - { - if (random2(pow) < 6) + if (x_chance_in_y(6, pow)) dec_hp(1, false); - } - if (random2(pow) < 6) + if (x_chance_in_y(6, pow)) break; } @@ -1196,9 +1193,9 @@ bool cast_summon_wraiths(int pow, god_type god) canned_msg(MSG_NOTHING_HAPPENS); //jmf: Kiku sometimes deflects this - if (!(you.religion == GOD_KIKUBAAQUDGHA - && (!player_under_penance() && you.piety >= piety_breakpoint(3) - && you.piety > random2(MAX_PIETY)))) + if (you.religion != GOD_KIKUBAAQUDGHA + || player_under_penance() || you.piety < piety_breakpoint(3) + || !x_chance_in_y(you.piety, MAX_PIETY)) { disease_player(25 + random2(50)); } diff --git a/crawl-ref/source/spells4.cc b/crawl-ref/source/spells4.cc index 4384a84ced..5d62772b28 100644 --- a/crawl-ref/source/spells4.cc +++ b/crawl-ref/source/spells4.cc @@ -291,7 +291,7 @@ static int _shatter_walls(int x, int y, int pow, int garbage) break; } - if (random2(100) < chance) + if (x_chance_in_y(chance, 100)) { noisy(30, x, y); @@ -1699,7 +1699,7 @@ void do_monster_rot(int mon) { int damage = 1 + random2(3); - if (mons_holiness(&menv[mon]) == MH_UNDEAD && random2(5)) + if (mons_holiness(&menv[mon]) == MH_UNDEAD && !one_chance_in(5)) { apply_area_cloud(make_a_normal_cloud, menv[mon].x, menv[mon].y, 10, 1, CLOUD_MIASMA, KC_YOU); @@ -1815,7 +1815,7 @@ void cast_fragmentation(int pow) // jmf: ripped idea from airstrike blast.colour = LIGHTGREY; - if (random2(50) < (pow / 5)) // potential insta-kill + if (x_chance_in_y(pow / 5, 50)) // potential insta-kill { monster_die(&menv[mon], KILL_YOU, 0); blast.damage.num = 4; diff --git a/crawl-ref/source/spl-cast.cc b/crawl-ref/source/spl-cast.cc index 42d8de5c5f..08e9890a99 100644 --- a/crawl-ref/source/spl-cast.cc +++ b/crawl-ref/source/spl-cast.cc @@ -795,7 +795,7 @@ bool maybe_identify_staff(item_def &item, spell_type spell) if (you.skills[SK_SPELLCASTING] > relevant_skill) relevant_skill = you.skills[SK_SPELLCASTING]; - if (random2(100) < relevant_skill) + if (x_chance_in_y(relevant_skill, 100)) id_staff = true; } else if (relevant_skill >= 4) diff --git a/crawl-ref/source/spl-util.cc b/crawl-ref/source/spl-util.cc index 364ab2461a..6edc0d33a8 100644 --- a/crawl-ref/source/spl-util.cc +++ b/crawl-ref/source/spl-util.cc @@ -330,10 +330,8 @@ int count_bits(unsigned int bits) int c = 0; for (n = 1; n < INT_MAX; n <<= 1) - { if (n & bits) c++; - } return (c); } @@ -370,7 +368,7 @@ int apply_area_visible( int (*func) (int, int, int, int), int power, } return (rv); -} // end apply_area_visible() +} // Applies the effect to all nine squares around/including the target. // Returns summation of return values from passed in function. @@ -381,15 +379,11 @@ int apply_area_square( int (*func) (int, int, int, int), int cx, int cy, int rv = 0; for (x = cx - 1; x <= cx + 1; x++) - { for (y = cy - 1; y <= cy + 1; y++) - { rv += func(x, y, power, 0); - } - } return (rv); -} // end apply_area_square() +} // Applies the effect to the eight squares beside the target. @@ -401,7 +395,6 @@ int apply_area_around_square( int (*func) (int, int, int, int), int rv = 0; for (x = targ_x - 1; x <= targ_x + 1; x++) - { for (y = targ_y - 1; y <= targ_y + 1; y++) { if (x == targ_x && y == targ_y) @@ -409,9 +402,9 @@ int apply_area_around_square( int (*func) (int, int, int, int), else rv += func(x, y, power, 0); } - } + return (rv); -} // end apply_area_around_square() +} // Effect up to max_targs monsters around a point, chosen randomly // Return varies with the function called; return values will be added up. @@ -425,14 +418,10 @@ int apply_random_around_square( int (*func) (int, int, int, int), return 0; if (max_targs >= 9 && !hole_in_middle) - { return (apply_area_square( func, targ_x, targ_y, power )); - } if (max_targs >= 8 && hole_in_middle) - { return (apply_area_around_square( func, targ_x, targ_y, power )); - } FixedVector< coord_def, 8 > targs; int count = 0; @@ -525,7 +514,7 @@ int apply_random_around_square( int (*func) (int, int, int, int), targs[ count - 1 ].x = x; targs[ count - 1 ].y = y; } - else if (random2( count ) < max_targs) + else if (x_chance_in_y(max_targs, count)) { const int pick = random2( max_targs ); targs[ pick ].x = x; diff --git a/crawl-ref/source/stuff.cc b/crawl-ref/source/stuff.cc index 159b09ae48..6fef267f58 100644 --- a/crawl-ref/source/stuff.cc +++ b/crawl-ref/source/stuff.cc @@ -866,30 +866,41 @@ bool one_chance_in(int a_million) return (random2(a_million) == 0); } -// simple little function to quickly modify all three stats +bool x_chance_in_y(int x, int y) +{ + if (x <= 0 || y <= 0) + return (false); + + if (x >= y) + return (true); + + return (random2(y) < x); +} + +// Simple little function to quickly modify all three stats // at once - does check for '0' modifiers to prevent needless // adding .. could use checking for sums less than zero, I guess. -// used in conjunction with newgame::species_stat_init() and -// newgame::job_stat_init() routines 24jan2000 {dlb} +// Used in conjunction with newgame::species_stat_init() and +// newgame::job_stat_init() routines 24jan2000. {dlb} void modify_all_stats(int STmod, int IQmod, int DXmod) { if (STmod) { - you.strength += STmod; + you.strength += STmod; you.max_strength += STmod; you.redraw_strength = true; } if (IQmod) { - you.intel += IQmod; + you.intel += IQmod; you.max_intel += IQmod; you.redraw_intelligence = true; } if (DXmod) { - you.dex += DXmod; + you.dex += DXmod; you.max_dex += DXmod; you.redraw_dexterity = true; } diff --git a/crawl-ref/source/stuff.h b/crawl-ref/source/stuff.h index 2883e3cacf..60c2ae4305 100644 --- a/crawl-ref/source/stuff.h +++ b/crawl-ref/source/stuff.h @@ -34,6 +34,7 @@ bool coinflip(); int div_rand_round( int num, int den ); int div_round_up( int num, int den ); bool one_chance_in(int a_million); +bool x_chance_in_y(int x, int y); int random2(int randmax); int random_range(int low, int high); int random_range(int low, int high, int nrolls); diff --git a/crawl-ref/source/xom.cc b/crawl-ref/source/xom.cc index fc40cb3750..6af82176bb 100644 --- a/crawl-ref/source/xom.cc +++ b/crawl-ref/source/xom.cc @@ -227,17 +227,17 @@ static object_class_type _get_unrelated_wield_class(object_class_type ref) object_class_type objtype = OBJ_WEAPONS; if (ref == OBJ_WEAPONS) { - if (random2(10)) - objtype = OBJ_STAVES; - else + if (one_chance_in(10)) objtype = OBJ_MISCELLANY; + else + objtype = OBJ_STAVES; } else if (ref == OBJ_STAVES) { - if (random2(10)) - objtype = OBJ_WEAPONS; - else + if (one_chance_in(10)) objtype = OBJ_MISCELLANY; + else + objtype = OBJ_WEAPONS; } else { @@ -313,7 +313,7 @@ static bool _xom_annoyance_gift(int power) const object_class_type objtype = _get_unrelated_wield_class(weapon->base_type); - if (power > random2(256)) + if (x_chance_in_y(power, 256)) acquirement(objtype, GOD_XOM); else _xom_make_item(objtype, OBJ_RANDOM, power * 3); @@ -336,9 +336,8 @@ static bool _xom_gives_item(int power) // cloak or body armour. Ha ha! god_speaks(GOD_XOM, _get_xom_speech("armour gift").c_str()); _xom_make_item(OBJ_ARMOUR, - random2(10) ? - get_random_body_armour_type(you.your_level * 2) - : ARM_CLOAK, + one_chance_in(10) ? ARM_CLOAK : + get_random_body_armour_type(you.your_level * 2), power * 3); return (true); } @@ -350,7 +349,7 @@ static bool _xom_gives_item(int power) // better than random object), and it is sometimes tuned to the // player's skills and nature. Being tuned to the player's skills // and nature is not very Xomlike... - if (power > random2(256)) + if (x_chance_in_y(power, 256)) { // Random-type acquirement. const int r = random2(7); @@ -465,6 +464,7 @@ static bool _xom_is_good(int sever) } else if (random2(sever) <= 3) { + // XXX: Can we clean up this ugliness, please? const int numdemons = std::min(random2(random2(random2(sever+1)+1)+1)+2, 16); int numdifferent = 0; -- cgit v1.2.3-54-g00ecf