summaryrefslogtreecommitdiffstats
path: root/crawl-ref
diff options
context:
space:
mode:
authorharanp <haranp@c06c8d41-db1a-0410-9941-cceddc491573>2007-04-14 08:42:55 +0000
committerharanp <haranp@c06c8d41-db1a-0410-9941-cceddc491573>2007-04-14 08:42:55 +0000
commita645ea7c241525d80c84cf3a843cd19dc29e8d17 (patch)
tree5f434cb49ab04806d92b6f88b3b2f85fc8b58f63 /crawl-ref
parent30653817169d9b0e2b517ba9c3e6586962b0edee (diff)
downloadcrawl-ref-a645ea7c241525d80c84cf3a843cd19dc29e8d17.tar.gz
crawl-ref-a645ea7c241525d80c84cf3a843cd19dc29e8d17.zip
More code cleanups and type safety.
Fixed a semi-bug (undefined behaviour) in undead_cannot_memorise(). git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1302 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref')
-rw-r--r--crawl-ref/source/dungeon.cc41
-rw-r--r--crawl-ref/source/spl-book.cc10
-rw-r--r--crawl-ref/source/spl-book.h6
-rw-r--r--crawl-ref/source/spl-cast.cc6
-rw-r--r--crawl-ref/source/spl-util.cc42
-rw-r--r--crawl-ref/source/spl-util.h4
6 files changed, 39 insertions, 70 deletions
diff --git a/crawl-ref/source/dungeon.cc b/crawl-ref/source/dungeon.cc
index b967b4e6ed..701abecc29 100644
--- a/crawl-ref/source/dungeon.cc
+++ b/crawl-ref/source/dungeon.cc
@@ -2462,42 +2462,27 @@ int items( int allow_uniques, // not just true-false,
break;
case OBJ_BOOKS:
- create_book:
do
{
mitm[p].sub_type = random2(NUM_BOOKS);
- if (book_rarity(mitm[p].sub_type) == 100)
- continue;
-
- if (mitm[p].sub_type != BOOK_DESTRUCTION
- && mitm[p].sub_type != BOOK_MANUAL)
+ if (mitm[p].sub_type != BOOK_DESTRUCTION &&
+ mitm[p].sub_type != BOOK_MANUAL &&
+ book_rarity(mitm[p].sub_type) != 100 &&
+ one_chance_in(10))
{
- if (one_chance_in(10))
- {
- if (coinflip())
- mitm[p].sub_type = BOOK_WIZARDRY;
- else
- mitm[p].sub_type = BOOK_POWER;
- }
+ mitm[p].sub_type = coinflip() ? BOOK_WIZARDRY : BOOK_POWER;
+ }
- if (random2(item_level + 1) + 1 >= book_rarity(mitm[p].sub_type)
- || one_chance_in(100))
- {
- break;
- }
- else
- {
- mitm[p].sub_type = BOOK_DESTRUCTION;
- continue;
- }
+ if (!one_chance_in(100) &&
+ random2(item_level+1) + 1 < book_rarity(mitm[p].sub_type))
+ {
+ mitm[p].sub_type = BOOK_DESTRUCTION; // continue trying
}
}
- while (mitm[p].sub_type == BOOK_DESTRUCTION
- || mitm[p].sub_type == BOOK_MANUAL);
-
- if (book_rarity(mitm[p].sub_type) == 100)
- goto create_book;
+ while (mitm[p].sub_type == BOOK_DESTRUCTION ||
+ mitm[p].sub_type == BOOK_MANUAL ||
+ book_rarity(mitm[p].sub_type) == 100);
mitm[p].special = random2(5);
diff --git a/crawl-ref/source/spl-book.cc b/crawl-ref/source/spl-book.cc
index af290ce1e8..12cdc1c8a3 100644
--- a/crawl-ref/source/spl-book.cc
+++ b/crawl-ref/source/spl-book.cc
@@ -849,7 +849,7 @@ unsigned char spellbook_contents( item_def &book, int action,
}
//jmf: was in shopping.cc
-char book_rarity(unsigned char which_book)
+int book_rarity(unsigned char which_book)
{
switch (which_book)
{
@@ -940,7 +940,7 @@ char book_rarity(unsigned char which_book)
}
} // end book_rarity()
-bool is_valid_spell_in_book( unsigned int splbook, int spell )
+bool is_valid_spell_in_book( int splbook, int spell )
{
return which_spell_in_book(splbook, spell) != SPELL_NO_SPELL;
}
@@ -1074,7 +1074,7 @@ unsigned char read_book( item_def &book, int action )
// the living by setting up an US_ALIVE case returning
// a value of false for a set of spells ... might be
// an idea worth further consideration - 12mar2000 {dlb}
-bool undead_cannot_memorise(unsigned char spell, unsigned char being)
+bool undead_cannot_memorise(spell_type spell, char being)
{
switch (being)
{
@@ -1091,6 +1091,8 @@ bool undead_cannot_memorise(unsigned char spell, unsigned char being)
case SPELL_TAME_BEASTS:
case SPELL_BERSERKER_RAGE:
return true;
+ default:
+ return false;
}
break;
@@ -1118,6 +1120,8 @@ bool undead_cannot_memorise(unsigned char spell, unsigned char being)
case SPELL_TAME_BEASTS:
case SPELL_BERSERKER_RAGE:
return true;
+ default:
+ return false;
}
break;
}
diff --git a/crawl-ref/source/spl-book.h b/crawl-ref/source/spl-book.h
index a577b97f80..d06172cc8f 100644
--- a/crawl-ref/source/spl-book.h
+++ b/crawl-ref/source/spl-book.h
@@ -22,10 +22,10 @@
/* ***********************************************************************
* called from: dungeon - effects - shopping
* *********************************************************************** */
-char book_rarity(unsigned char which_book);
+int book_rarity(unsigned char which_book);
-bool is_valid_spell_in_book( unsigned int splbook, int spell );
+bool is_valid_spell_in_book( int splbook, int spell );
// updated 24may2000 {dlb}
@@ -46,7 +46,7 @@ spell_type which_spell_in_book(int sbook_type, int spl);
// returns amount practised (or -1 for abort)
int staff_spell( int zap_device_2 );
-bool undead_cannot_memorise(unsigned char spell, unsigned char being);
+bool undead_cannot_memorise(spell_type spell, char being);
unsigned char spellbook_contents( item_def &book, int action,
formatted_string *fs = NULL );
diff --git a/crawl-ref/source/spl-cast.cc b/crawl-ref/source/spl-cast.cc
index 6b08002f17..125443d3fe 100644
--- a/crawl-ref/source/spl-cast.cc
+++ b/crawl-ref/source/spl-cast.cc
@@ -245,7 +245,7 @@ char list_spells(void)
return (ki);
} // end list_spells()
-static int apply_vehumet_wizardry_boost(int spell, int chance)
+static int apply_vehumet_wizardry_boost(spell_type spell, int chance)
{
int wizardry = player_mag_abil(false);
int fail_reduce = 100;
@@ -823,9 +823,9 @@ void spellcasting_side_effects(spell_type spc2, bool idonly = false)
alert_nearby_monsters();
}
-static bool spell_is_uncastable(int spell)
+static bool spell_is_uncastable(spell_type spell)
{
- if (you.is_undead && spell_typematch( spell, SPTYP_HOLY ))
+ if (you.is_undead && spell_typematch(spell, SPTYP_HOLY))
{
mpr( "You can't use this type of magic!" );
return (true);
diff --git a/crawl-ref/source/spl-util.cc b/crawl-ref/source/spl-util.cc
index 8a8a291e9b..f3be88e194 100644
--- a/crawl-ref/source/spl-util.cc
+++ b/crawl-ref/source/spl-util.cc
@@ -227,13 +227,13 @@ const char *get_spell_target_prompt( spell_type which_spell )
return (seekspell(which_spell)->target_prompt);
}
-bool spell_typematch(int which_spell, unsigned int which_discipline)
+bool spell_typematch(spell_type which_spell, unsigned int which_discipline)
{
return (seekspell(which_spell)->disciplines & which_discipline);
}
//jmf: next two for simple bit handling
-unsigned int get_spell_type(int spell)
+unsigned int get_spell_type(spell_type spell)
{
return (seekspell(spell)->disciplines);
}
@@ -252,20 +252,6 @@ int count_bits(unsigned int bits)
return (c);
}
-// this will probably be used often, so rather than use malloc/free
-// (which may lead to memory fragmentation) I'll just use a static
-// array of characters -- if/when the String changeover takes place,
-// this will all shift, no doubt {dlb}
-/*
- const char *spell_title( int which_spell )
- {
- static char this_title[41] = ""; // this is generous, to say the least {dlb}
- strncpy(this_title, seekspell(which_spell)->title, 41);
- // truncation better than overrun {dlb}
- return ( this_title );
- } // end spell_title()
-*/
-
const char *spell_title(spell_type spell)
{
return (seekspell(spell)->title);
@@ -379,7 +365,7 @@ int apply_random_around_square( int (*func) (int, int, int, int),
// Found target
count++;
- // Slight differece here over the basic algorithm...
+ // Slight difference here over the basic algorithm...
//
// For cases where the number of choices <= max_targs it's
// obvious (all available choices will be selected).
@@ -423,7 +409,7 @@ int apply_random_around_square( int (*func) (int, int, int, int),
// probability is that the chosen elements have already
// passed tests which verify that they *don't* belong
// in slots m+1...m+k, so the only positions an already
- // chosen element can end up in are it's original
+ // chosen element can end up in are its original
// position (in one of the chosen slots), or in the
// new slot.
//
@@ -714,7 +700,8 @@ char spell_direction( struct dist &spelld, struct bolt &pbolt,
return 1;
} // end spell_direction()
-const char* spelltype_short_name( int which_spelltype ) {
+const char* spelltype_short_name( int which_spelltype )
+{
switch (which_spelltype)
{
case SPTYP_CONJURATION:
@@ -750,8 +737,6 @@ const char* spelltype_short_name( int which_spelltype ) {
const char *spelltype_name(unsigned int which_spelltype)
{
- static char bug_string[80];
-
switch (which_spelltype)
{
case SPTYP_CONJURATION:
@@ -781,17 +766,12 @@ const char *spelltype_name(unsigned int which_spelltype)
case SPTYP_AIR:
return ("Air");
default:
- snprintf( bug_string, sizeof(bug_string),
- "invalid(%d)", which_spelltype );
-
- return (bug_string);
+ return "Buggy";
}
} // end spelltype_name()
int spell_type2skill(unsigned int spelltype)
{
- char buffer[80];
-
switch (spelltype)
{
case SPTYP_CONJURATION: return (SK_CONJURATIONS);
@@ -809,10 +789,10 @@ int spell_type2skill(unsigned int spelltype)
default:
case SPTYP_HOLY:
- snprintf( buffer, sizeof(buffer),
- "spell_type2skill: called with spelltype %d", spelltype );
-
- mpr( buffer );
+#ifdef DEBUG_DIAGNOSTICS
+ mprf(MSGCH_DIAGNOSTICS, "spell_type2skill: called with spelltype %u",
+ spelltype );
+#endif
return (-1);
}
} // end spell_type2skill()
diff --git a/crawl-ref/source/spl-util.h b/crawl-ref/source/spl-util.h
index 2f181470aa..676eef40e6 100644
--- a/crawl-ref/source/spl-util.h
+++ b/crawl-ref/source/spl-util.h
@@ -62,8 +62,8 @@ int spell_levels_required(spell_type which_spell);
unsigned int get_spell_flags( spell_type which_spell );
// * called from: chardump - spell - spl-book - spells0
-bool spell_typematch(int which_spell, unsigned int which_discipline);
-unsigned int get_spell_type( int which_spell ); //jmf: simplification of above
+bool spell_typematch(spell_type which_spell, unsigned int which_discipline);
+unsigned int get_spell_type( spell_type which_spell );
int count_bits( unsigned int bits );
// * called from: chardump - command - debug - spl-book - spells0