summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorharanp <haranp@c06c8d41-db1a-0410-9941-cceddc491573>2007-07-03 12:02:02 +0000
committerharanp <haranp@c06c8d41-db1a-0410-9941-cceddc491573>2007-07-03 12:02:02 +0000
commitda7b14eeeda044dc523653b4c0b365e15495282e (patch)
tree2cec3f9fe140b3f6b129ec65eb22a2fcb4c72de1
parenteedaf28e2ab8dfae0eaae2fc308bdf683bf57021 (diff)
downloadcrawl-ref-da7b14eeeda044dc523653b4c0b365e15495282e.tar.gz
crawl-ref-da7b14eeeda044dc523653b4c0b365e15495282e.zip
Minor cleanups (especially of Xom code.)
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1728 c06c8d41-db1a-0410-9941-cceddc491573
-rw-r--r--crawl-ref/source/decks.cc2
-rw-r--r--crawl-ref/source/defines.h1
-rw-r--r--crawl-ref/source/fight.cc8
-rw-r--r--crawl-ref/source/misc.cc2
-rw-r--r--crawl-ref/source/randart.cc6
-rw-r--r--crawl-ref/source/religion.cc8
-rw-r--r--crawl-ref/source/spells2.cc2
-rw-r--r--crawl-ref/source/xom.cc121
8 files changed, 53 insertions, 97 deletions
diff --git a/crawl-ref/source/decks.cc b/crawl-ref/source/decks.cc
index 2cc06286cb..6e72756b13 100644
--- a/crawl-ref/source/decks.cc
+++ b/crawl-ref/source/decks.cc
@@ -816,7 +816,7 @@ static void potion_card(int power, deck_rarity_type rarity)
POT_POISON, POT_CONFUSION, POT_DEGENERATION
};
- potion_type pot = pot_effects[random2(ARRAYSIZE(pot_effects))];
+ potion_type pot = RANDOM_ELEMENT(pot_effects);
if ( power_level >= 1 && coinflip() )
pot = (coinflip() ? POT_CURE_MUTATION : POT_MUTATION);
diff --git a/crawl-ref/source/defines.h b/crawl-ref/source/defines.h
index a3e2b3b63f..348c275080 100644
--- a/crawl-ref/source/defines.h
+++ b/crawl-ref/source/defines.h
@@ -290,6 +290,7 @@
#define CONTROL( xxx ) ((xxx) - 'A' + 1)
#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))
+#define RANDOM_ELEMENT(x) (x[random2(ARRAYSIZE(x))])
#define MIN(x, y) MINIMUM(x, y)
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
diff --git a/crawl-ref/source/fight.cc b/crawl-ref/source/fight.cc
index f938562d72..a8ba7d74a3 100644
--- a/crawl-ref/source/fight.cc
+++ b/crawl-ref/source/fight.cc
@@ -1761,9 +1761,7 @@ bool melee_attack::chop_hydra_head( int dam,
if (dam_type == DVORP_CLAWING)
{
static const char *claw_verbs[] = { "rip", "tear", "claw" };
- verb =
- claw_verbs[
- random2( sizeof(claw_verbs) / sizeof(*claw_verbs) ) ];
+ verb = RANDOM_ELEMENT(claw_verbs);
}
else
{
@@ -1771,9 +1769,7 @@ bool melee_attack::chop_hydra_head( int dam,
{
"slice", "lop", "chop", "hack"
};
- verb =
- slice_verbs[
- random2( sizeof(slice_verbs) / sizeof(*slice_verbs) ) ];
+ verb = RANDOM_ELEMENT(slice_verbs);
}
if (def->number < 1)
diff --git a/crawl-ref/source/misc.cc b/crawl-ref/source/misc.cc
index a9feae9cc0..52637882e1 100644
--- a/crawl-ref/source/misc.cc
+++ b/crawl-ref/source/misc.cc
@@ -1911,7 +1911,7 @@ bool i_feel_safe(bool announce)
const unsigned char targ_monst = mgrd[x][y];
if ( targ_monst != NON_MONSTER )
{
- struct monsters *mon = &menv[targ_monst];
+ const monsters *mon = &menv[targ_monst];
if ( !mons_friendly(mon) &&
player_monster_visible(mon) &&
!mons_is_submerged(mon) &&
diff --git a/crawl-ref/source/randart.cc b/crawl-ref/source/randart.cc
index d719501136..e32d71ce00 100644
--- a/crawl-ref/source/randart.cc
+++ b/crawl-ref/source/randart.cc
@@ -1382,7 +1382,7 @@ std::string randart_name( const item_def &item )
if (coinflip())
{
result += item_base_name(item);
- result += rand_wpn_names[random2(ARRAYSIZE(rand_wpn_names))];
+ result += RANDOM_ELEMENT(rand_wpn_names);
}
else
{
@@ -1457,7 +1457,7 @@ std::string randart_armour_name( const item_def &item )
if (coinflip())
{
result += item_base_name(item);
- result += rand_armour_names[random2(ARRAYSIZE(rand_armour_names))];
+ result += RANDOM_ELEMENT(rand_armour_names);
}
else
{
@@ -1538,7 +1538,7 @@ std::string randart_jewellery_name( const item_def &item )
if (one_chance_in(5))
{
result += (jewellery_is_amulet(item) ? "amulet" : "ring");
- result += rand_armour_names[random2(ARRAYSIZE(rand_armour_names))];
+ result += RANDOM_ELEMENT(rand_armour_names);
}
else
{
diff --git a/crawl-ref/source/religion.cc b/crawl-ref/source/religion.cc
index 78a37976f9..46a3a4053a 100644
--- a/crawl-ref/source/religion.cc
+++ b/crawl-ref/source/religion.cc
@@ -455,24 +455,24 @@ static void do_god_gift()
if ( random2(200) <= you.piety )
{
// make a pure deck
- misc_item_type pure_decks[] = {
+ const misc_item_type pure_decks[] = {
MISC_DECK_OF_ESCAPE,
MISC_DECK_OF_DESTRUCTION,
MISC_DECK_OF_DUNGEONS,
MISC_DECK_OF_SUMMONING,
MISC_DECK_OF_WONDERS
};
- gift_type = pure_decks[random2(ARRAYSIZE(pure_decks))];
+ gift_type = RANDOM_ELEMENT(pure_decks);
}
else
{
// make a mixed deck
- misc_item_type mixed_decks[] = {
+ const misc_item_type mixed_decks[] = {
MISC_DECK_OF_WAR,
MISC_DECK_OF_CHANGES,
MISC_DECK_OF_DEFENSE
};
- gift_type = mixed_decks[random2(ARRAYSIZE(mixed_decks))];
+ gift_type = RANDOM_ELEMENT(mixed_decks);
}
int thing_created = items( 1, OBJ_MISCELLANY, gift_type,
diff --git a/crawl-ref/source/spells2.cc b/crawl-ref/source/spells2.cc
index 746b2b7788..1752ae6405 100644
--- a/crawl-ref/source/spells2.cc
+++ b/crawl-ref/source/spells2.cc
@@ -1320,7 +1320,7 @@ void summon_animals(int pow)
{
// pick a random monster and subtract its cost
if ( varied || num_so_far == 0 )
- mon_chosen = animals[random2(ARRAYSIZE(animals))];
+ mon_chosen = RANDOM_ELEMENT(animals);
const int power_cost = mons_power(mon_chosen) * 3;
diff --git a/crawl-ref/source/xom.cc b/crawl-ref/source/xom.cc
index 22de3219b4..be3754a4c8 100644
--- a/crawl-ref/source/xom.cc
+++ b/crawl-ref/source/xom.cc
@@ -169,18 +169,6 @@ static void xom_make_item(object_class_type base,
origin_acquired(mitm[thing_created], GOD_XOM);
}
-static void xom_suggest(const char **messages, int nmsgs)
-{
- if (nmsgs && messages)
- {
- const char *sel = messages[random2(nmsgs)];
- if (sel)
- god_speaks(GOD_XOM, sel);
- }
-}
-
-#define XOM_SAY(x) xom_suggest(x, ARRAYSIZE(x))
-
static object_class_type get_unrelated_wield_class(object_class_type ref)
{
object_class_type objtype = OBJ_WEAPONS;
@@ -217,9 +205,9 @@ static bool xom_annoyance_gift(int power)
// Xom has a sense of humour.
if (coinflip() && weapon && weapon->cursed())
{
- // If you are wielding a cursed item on then Xom will give
+ // If you are wielding a cursed item then Xom will give
// you an item of that same type. Ha ha!
- XOM_SAY(xom_try_this);
+ god_speaks(GOD_XOM, RANDOM_ELEMENT(xom_try_this));
if (coinflip())
// For added humour, give the same sub-type.
xom_make_item(weapon->base_type, weapon->sub_type, power * 3);
@@ -235,7 +223,7 @@ static bool xom_annoyance_gift(int power)
// a ring. Ha ha!
//
// A random ring. (Not necessarily a good one.)
- XOM_SAY(xom_try_this);
+ god_speaks(GOD_XOM, RANDOM_ELEMENT(xom_try_this));
xom_make_item(OBJ_JEWELLERY, get_random_ring_type(), power * 3);
return (true);
};
@@ -245,7 +233,7 @@ static bool xom_annoyance_gift(int power)
{
// If you are wearing a cursed amulet then Xom will give
// you an amulet. Ha ha!
- XOM_SAY(xom_try_this);
+ god_speaks(GOD_XOM, RANDOM_ELEMENT(xom_try_this));
xom_make_item(OBJ_JEWELLERY, get_random_amulet_type(), power * 3);
return (true);
};
@@ -257,7 +245,7 @@ static bool xom_annoyance_gift(int power)
{
// If you are wearing a cursed ring then Xom will give you
// a ring. Ha ha!
- XOM_SAY(xom_try_this_ring);
+ god_speaks(GOD_XOM, RANDOM_ELEMENT(xom_try_this_ring));
xom_make_item(OBJ_JEWELLERY, get_random_ring_type(), power * 3);
return (true);
}
@@ -266,7 +254,7 @@ static bool xom_annoyance_gift(int power)
{
// Xom will give you a wielded item of a type different
// than what you are currently wielding.
- XOM_SAY(xom_try_this_other_thing);
+ god_speaks(GOD_XOM, RANDOM_ELEMENT(xom_try_this_other_thing));
const object_class_type objtype =
get_unrelated_wield_class(weapon->base_type);
@@ -291,7 +279,7 @@ bool xom_gives_item(int power)
{
// If you are wearing a cursed cloak then Xom will give you a
// cloak or body armour . Ha ha!
- XOM_SAY(xom_try_these_duds);
+ god_speaks(GOD_XOM, RANDOM_ELEMENT(xom_try_these_duds));
xom_make_item(OBJ_ARMOUR,
random2(10)?
get_random_body_armour_type(you.your_level * 2)
@@ -300,7 +288,7 @@ bool xom_gives_item(int power)
return (true);
}
- XOM_SAY(xom_generic_beneficence);
+ god_speaks(GOD_XOM, RANDOM_ELEMENT(xom_generic_beneficence));
// There are two kinds of Xom gifts: acquirement and random
// object. The result from acquirement is very good (usually as
@@ -379,6 +367,23 @@ monsters *get_random_nearby_monster()
return (monster);
}
+static int xom_random_demon(int sever)
+{
+ int demontype;
+
+ do
+ {
+ // XXX Change the 20 if we add/remove demons!
+ // XXX Maybe we should use summon_any_demon() instead?
+ demontype = MONS_WHITE_IMP +
+ std::min(random2(random2(random2(sever))), 20);
+
+ // Don't make Green Deaths for non-poison-resistant characters.
+ } while ( demontype == MONS_GREEN_DEATH && !player_res_poison());
+
+ return demontype;
+}
+
static bool xom_is_good(int sever)
{
// niceness = false - bad, true - nice
@@ -447,26 +452,11 @@ static bool xom_is_good(int sever)
(temp_rand == 1) ? "Xom grants you some temporary aid."
: "Xom momentarily opens a gate.");
- int i;
- int r = random2(random2(random2(sever+1)+1)+1)+2;
- int numdemons = MIN(r, 24);
- for (i=0; i < numdemons; i++)
+ int numdemons = std::min(random2(random2(random2(sever+1)+1)+1)+2, 24);
+ for (int i = 0; i < numdemons; i++)
{
- r = random2(random2(random2(sever)));
- int demonnum = MONS_WHITE_IMP + MIN(r, 20);
- if (!player_res_poison())
- {
- while (demonnum == MONS_GREEN_DEATH)
- {
- r = random2(random2(random2(sever)));
- demonnum = MONS_WHITE_IMP + MIN(r, 20);
- }
- }
- ASSERT (demonnum >= MONS_WHITE_IMP);
- ASSERT (demonnum <= MONS_WHITE_IMP+20);
- create_monster(demonnum, 3,
- BEH_GOD_GIFT, you.x_pos, you.y_pos,
- you.pet_target, 250 );
+ create_monster(xom_random_demon(sever), 3, BEH_GOD_GIFT,
+ you.x_pos, you.y_pos, you.pet_target, 250 );
}
done = true;
@@ -478,21 +468,8 @@ static bool xom_is_good(int sever)
}
else if (random2(sever) <= 5)
{
- int r = random2(random2(random2(sever)));
- int demonnum = MONS_WHITE_IMP + MIN(r, 20);
- ASSERT (demonnum >= MONS_WHITE_IMP);
- ASSERT (demonnum <= MONS_WHITE_IMP+20);
- if (!player_res_poison())
- {
- while (demonnum == MONS_GREEN_DEATH)
- {
- r = random2(random2(random2(sever)));
- demonnum = MONS_WHITE_IMP + MIN(r, 20);
- }
- }
- if (create_monster( demonnum, 6,
- BEH_GOD_GIFT, you.x_pos, you.y_pos,
- you.pet_target, 250 ) != -1)
+ if (create_monster(xom_random_demon(sever), 6, BEH_GOD_GIFT,
+ you.x_pos, you.y_pos, you.pet_target, 250) != -1)
{
temp_rand = random2(3);
@@ -557,17 +534,7 @@ static bool xom_is_good(int sever)
}
else if (random2(sever) <= 9)
{
- int r = random2(random2(random2(sever)));
- int demonnum = MONS_WHITE_IMP + MIN(r, 20);
- if (!player_res_poison())
- {
- while (demonnum == MONS_GREEN_DEATH)
- {
- r = random2(random2(random2(sever)));
- demonnum = MONS_WHITE_IMP + MIN(r, 20);
- }
- }
- if (create_monster( demonnum, 0, BEH_GOD_GIFT,
+ if (create_monster( xom_random_demon(sever), 0, BEH_GOD_GIFT,
you.x_pos, you.y_pos, you.pet_target, 250 ) != -1)
{
temp_rand = random2(3);
@@ -580,15 +547,9 @@ static bool xom_is_good(int sever)
}
else if ((random2(sever) <= 10) && player_in_a_dangerous_place())
{
- bool lightningprot = false;
- if (you.hp <= random2(201)
- && !you.attribute[ATTR_DIVINE_LIGHTNING_PROTECTION])
- {
+ if (you.hp <= random2(201))
you.attribute[ATTR_DIVINE_LIGHTNING_PROTECTION] = 1;
- lightningprot = true;
- }
- else
- lightningprot = false;
+
mpr("The area is suffused with divine lightning!");
beam.beam_source = NON_MONSTER;
@@ -606,7 +567,7 @@ static bool xom_is_good(int sever)
beam.is_explosion = true;
explosion(beam);
- if (lightningprot)
+ if (you.attribute[ATTR_DIVINE_LIGHTNING_PROTECTION])
{
mpr("Your divine protection wanes.");
you.attribute[ATTR_DIVINE_LIGHTNING_PROTECTION] = 0;
@@ -771,14 +732,12 @@ static bool xom_is_bad(int sever)
dancing_weapon(100, true); // nasty, but fun
else
{
- int r = random2(random2(random2(sever+1)+1)+1)+1;
- int numdemons = MIN(r, 24);
+ const int numdemons =
+ std::min(random2(random2(random2(sever+1)+1)+1)+1, 24);
for (int i = 0; i < numdemons; i++)
{
- create_monster(MONS_WHITE_IMP +
- random2(random2(random2(MIN(sever, 22)))),
- 4,
- BEH_HOSTILE, you.x_pos, you.y_pos,
+ create_monster(MONS_WHITE_IMP + random2(random2(random2(std::min(sever,22)))),
+ 4, BEH_HOSTILE, you.x_pos, you.y_pos,
MHITNOT, 250);
}
}
@@ -800,7 +759,7 @@ static bool xom_is_bad(int sever)
done = true;
}
- else if ((random2(sever) == 0) && (you.level_type != LEVEL_ABYSS))
+ else if (one_chance_in(sever) && (you.level_type != LEVEL_ABYSS))
{
temp_rand = random2(3);