From 5dc94f3d83f0a9009e3355403e5b31a6cae8d421 Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Tue, 5 Dec 2023 10:06:27 +0200 Subject: [PATCH] Macro for picking random entry from array --- include/hack.h | 3 +++ src/artifact.c | 4 ++-- src/do_name.c | 4 ++-- src/dokick.c | 2 +- src/engrave.c | 2 +- src/invent.c | 2 +- src/makemon.c | 4 ++-- src/minion.c | 4 ++-- src/mklev.c | 4 ++-- src/mkmaze.c | 2 +- src/mkobj.c | 2 +- src/mkroom.c | 2 +- src/mondata.c | 2 +- src/mthrowu.c | 2 +- src/music.c | 2 +- src/potion.c | 4 ++-- src/pray.c | 2 +- src/shk.c | 8 ++++---- src/sounds.c | 8 ++++---- src/sp_lev.c | 4 ++-- src/steal.c | 2 +- src/trap.c | 2 +- src/u_init.c | 2 +- src/wizard.c | 14 +++++++------- 24 files changed, 45 insertions(+), 42 deletions(-) diff --git a/include/hack.h b/include/hack.h index 69645f086..d973ac6d6 100644 --- a/include/hack.h +++ b/include/hack.h @@ -1404,6 +1404,9 @@ typedef uint32_t mmflags_nht; /* makemon MM_ flags */ /* monster shooting a wand; note: not -9 to -0 because -0 is ambiguous */ #define BZ_M_WAND(bztyp) (-30 - (bztyp)) /* -39..-30 */ +/* pick a random entry from array */ +#define ROLL_FROM(array) array[rn2(SIZE(array))] + #define FEATURE_NOTICE_VER(major, minor, patch) \ (((unsigned long) major << 24) | ((unsigned long) minor << 16) \ | ((unsigned long) patch << 8) | ((unsigned long) 0)) diff --git a/src/artifact.c b/src/artifact.c index f15efdc5c..a79d828bd 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -1505,7 +1505,7 @@ artifact_hit( return TRUE; } *dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER; - pline(behead_msg[rn2(SIZE(behead_msg))], wepdesc, + pline(ROLL_FROM(behead_msg), wepdesc, mon_nam(mdef)); if (Hallucination && !flags.female) pline("Good job Henry, but that wasn't Anne."); @@ -1525,7 +1525,7 @@ artifact_hit( return TRUE; } *dmgptr = 2 * (Upolyd ? u.mh : u.uhp) + FATAL_DAMAGE_MODIFIER; - pline(behead_msg[rn2(SIZE(behead_msg))], wepdesc, "you"); + pline(ROLL_FROM(behead_msg), wepdesc, "you"); otmp->dknown = TRUE; /* Should amulets fall off? */ return TRUE; diff --git a/src/do_name.c b/src/do_name.c index 3d0ddbec2..e6c4ec459 100644 --- a/src/do_name.c +++ b/src/do_name.c @@ -1899,7 +1899,7 @@ static const char *const ghostnames[] = { const char * rndghostname(void) { - return rn2(7) ? ghostnames[rn2(SIZE(ghostnames))] + return rn2(7) ? ROLL_FROM(ghostnames) : (const char *) gp.plname; } @@ -2635,7 +2635,7 @@ rndorcname(char *s) for (i = 0; i < iend; ++i) { vstart = 1 - vstart; /* 0 -> 1, 1 -> 0 */ Sprintf(eos(s), "%s%s", (i > 0 && !rn2(30)) ? "-" : "", - vstart ? v[rn2(SIZE(v))] : snd[rn2(SIZE(snd))]); + vstart ? ROLL_FROM(v) : ROLL_FROM(snd)); } } return s; diff --git a/src/dokick.c b/src/dokick.c index 45004a8ef..d6927d116 100644 --- a/src/dokick.c +++ b/src/dokick.c @@ -689,7 +689,7 @@ really_kick_object(coordxy x, coordxy y) if (!Deaf) pline1("Thwwpingg!"); - You("%s!", flyingcoinmsg[rn2(SIZE(flyingcoinmsg))]); + You("%s!", ROLL_FROM(flyingcoinmsg)); (void) scatter(x, y, rnd(3), VIS_EFFECTS | MAY_HIT, gk.kickedobj); newsym(x, y); diff --git a/src/engrave.c b/src/engrave.c index 9b57f1411..7772d82bb 100644 --- a/src/engrave.c +++ b/src/engrave.c @@ -1549,7 +1549,7 @@ static const char blind_writing[][21] = { static const char * blengr(void) { - return blind_writing[rn2(SIZE(blind_writing))]; + return ROLL_FROM(blind_writing); } /*engrave.c*/ diff --git a/src/invent.c b/src/invent.c index a6dc1a460..c4b1b4962 100644 --- a/src/invent.c +++ b/src/invent.c @@ -1501,7 +1501,7 @@ currency(long amount) { const char *res; - res = Hallucination ? currencies[rn2(SIZE(currencies))] : "zorkmid"; + res = Hallucination ? ROLL_FROM(currencies) : "zorkmid"; if (amount != 1L) res = makeplural(res); return res; diff --git a/src/makemon.c b/src/makemon.c index 95f5411b5..1253a1314 100644 --- a/src/makemon.c +++ b/src/makemon.c @@ -2317,7 +2317,7 @@ set_mimic_sym(register struct monst *mtmp) goto assign_sym; } } else { - s_sym = syms[rn2(SIZE(syms))]; + s_sym = ROLL_FROM(syms); assign_sym: if (s_sym == MAXOCLASSES) { static const int furnsyms[] = { @@ -2326,7 +2326,7 @@ set_mimic_sym(register struct monst *mtmp) }; ap_type = M_AP_FURNITURE; - appear = furnsyms[rn2(SIZE(furnsyms))]; + appear = ROLL_FROM(furnsyms); } else { ap_type = M_AP_OBJECT; if (s_sym == S_MIMIC_DEF) { diff --git a/src/minion.c b/src/minion.c index f7b99bb91..85494a110 100644 --- a/src/minion.c +++ b/src/minion.c @@ -109,7 +109,7 @@ msummon(struct monst *mon) if (!rn2(6)) { switch (atyp) { /* see summon_minion */ case A_NEUTRAL: - dtype = elementals[rn2(SIZE(elementals))]; + dtype = ROLL_FROM(elementals); break; case A_CHAOTIC: case A_NONE: @@ -204,7 +204,7 @@ summon_minion(aligntyp alignment, boolean talk) mnum = lminion(); break; case A_NEUTRAL: - mnum = elementals[rn2(SIZE(elementals))]; + mnum = ROLL_FROM(elementals); break; case A_CHAOTIC: case A_NONE: diff --git a/src/mklev.c b/src/mklev.c index a5fea6c8a..4c2746291 100644 --- a/src/mklev.c +++ b/src/mklev.c @@ -956,7 +956,7 @@ fill_ordinary_room(struct mkroom *croom, boolean bonus_items) WAN_DIGGING, SPE_HEALING, }; - otyp = supply_items[rn2(SIZE(supply_items))]; + otyp = ROLL_FROM(supply_items); } otmp = mksobj(otyp, TRUE, FALSE); if (otyp == POT_HEALING && rn2(2)) @@ -991,7 +991,7 @@ fill_ordinary_room(struct mkroom *croom, boolean bonus_items) SPBOOK_no_NOVEL, SPBOOK_no_NOVEL }; - int oclass = extra_classes[rn2(SIZE(extra_classes))]; + int oclass = ROLL_FROM(extra_classes); otmp = mkobj(oclass, FALSE); if (oclass == SPBOOK_no_NOVEL) { /* bias towards lower level by generating again diff --git a/src/mkmaze.c b/src/mkmaze.c index 1a3aec851..ef7d5d8d9 100644 --- a/src/mkmaze.c +++ b/src/mkmaze.c @@ -756,7 +756,7 @@ migr_booty_item(int otyp, const char* gang) Strcpy(ONAME(otmp), gang); if (objects[otyp].oc_class == FOOD_CLASS) { if (otyp == SLIME_MOLD) - otmp->spe = fruitadd((char *) orcfruit[rn2(SIZE(orcfruit))], + otmp->spe = fruitadd((char *) ROLL_FROM(orcfruit), (struct fruit *) 0); otmp->quan += (long) rn2(3); otmp->owt = weight(otmp); diff --git a/src/mkobj.c b/src/mkobj.c index cd1e95765..f3affc40a 100644 --- a/src/mkobj.c +++ b/src/mkobj.c @@ -1910,7 +1910,7 @@ static const int treefruits[] = { struct obj * rnd_treefruit_at(coordxy x, coordxy y) { - return mksobj_at(treefruits[rn2(SIZE(treefruits))], x, y, TRUE, FALSE); + return mksobj_at(ROLL_FROM(treefruits), x, y, TRUE, FALSE); } /* create a stack of N gold pieces; never returns Null */ diff --git a/src/mkroom.c b/src/mkroom.c index 53828b12e..3ca05a7d0 100644 --- a/src/mkroom.c +++ b/src/mkroom.c @@ -819,7 +819,7 @@ squadmon(void) goto gotone; } } - mndx = squadprob[rn2(SIZE(squadprob))].pm; + mndx = ROLL_FROM(squadprob).pm; gotone: if (!(gm.mvitals[mndx].mvflags & G_GONE)) return &mons[mndx]; diff --git a/src/mondata.c b/src/mondata.c index 04e4b1c8d..da9fdfcc2 100644 --- a/src/mondata.c +++ b/src/mondata.c @@ -1546,7 +1546,7 @@ get_atkdam_type(int adtyp) static const int rnd_breath_typ[] = { AD_MAGM, AD_FIRE, AD_COLD, AD_SLEE, AD_DISN, AD_ELEC, AD_DRST, AD_ACID }; - return rnd_breath_typ[rn2(SIZE(rnd_breath_typ))]; + return ROLL_FROM(rnd_breath_typ); } return adtyp; } diff --git a/src/mthrowu.c b/src/mthrowu.c index bafb3c8c6..5829f05e0 100644 --- a/src/mthrowu.c +++ b/src/mthrowu.c @@ -48,7 +48,7 @@ const char *const hallublasts[] = { const char * rnd_hallublast(void) { - return hallublasts[rn2(SIZE(hallublasts))]; + return ROLL_FROM(hallublasts); } boolean diff --git a/src/music.c b/src/music.c index 045e6e839..2703f2f0a 100644 --- a/src/music.c +++ b/src/music.c @@ -695,7 +695,7 @@ do_improvisation(struct obj* instr) /* TODO maybe: sound effects for these riffs */ You("%s %s.", rn2(2) ? "butcher" : rn2(2) ? "manage" : "pull off", - an(beats[rn2(SIZE(beats))])); + an(ROLL_FROM(beats))); Hero_playnotes(obj_to_instr(&itmp), improvisation, 50); } awaken_monsters(u.ulevel * (mundane ? 5 : 40)); diff --git a/src/potion.c b/src/potion.c index 91e59ad02..93eed9dfe 100644 --- a/src/potion.c +++ b/src/potion.c @@ -1468,9 +1468,9 @@ const char * bottlename(void) { if (Hallucination) - return hbottlenames[rn2(SIZE(hbottlenames))]; + return ROLL_FROM(hbottlenames); else - return bottlenames[rn2(SIZE(bottlenames))]; + return ROLL_FROM(bottlenames); } /* handle item dipped into water potion or steed saddle splashed by same */ diff --git a/src/pray.c b/src/pray.c index 4884f686f..387e5a434 100644 --- a/src/pray.c +++ b/src/pray.c @@ -1398,7 +1398,7 @@ godvoice(aligntyp g_align, const char *words) words = ""; pline_The("voice of %s %s: %s%s%s", align_gname(g_align), - godvoices[rn2(SIZE(godvoices))], quot, words, quot); + ROLL_FROM(godvoices), quot, words, quot); } static void diff --git a/src/shk.c b/src/shk.c index 07c778050..7968bcd02 100644 --- a/src/shk.c +++ b/src/shk.c @@ -697,7 +697,7 @@ u_entered_shop(char *enterstring) s_suffix(shkname(shkp)), shtypes[rt - SHOPBASE].name); } else { pline("%s seems %s over your return to %s %s!", - Shknam(shkp), angrytexts[rn2(SIZE(angrytexts))], + Shknam(shkp), ROLL_FROM(angrytexts), noit_mhis(shkp), shtypes[rt - SHOPBASE].name); } } else if (eshkp->surcharge) { @@ -4613,7 +4613,7 @@ pay_for_damage(const char *dmgstr, boolean cant_mollify) dugwall ? "shop" : "door"); } else { pline("%s is %s that you decided to %s %s %s!", - Shknam(shkp), angrytexts[rn2(SIZE(angrytexts))], + Shknam(shkp), ROLL_FROM(angrytexts), dmgstr, noit_mhis(shkp), dugwall ? "shop" : "door"); } } else { @@ -4624,7 +4624,7 @@ pay_for_damage(const char *dmgstr, boolean cant_mollify) dugwall ? "shop" : "door"); } else { pline("%s is %s that someone decided to %s %s %s!", - Shknam(shkp), angrytexts[rn2(SIZE(angrytexts))], + Shknam(shkp), ROLL_FROM(angrytexts), dmgstr, noit_mhis(shkp), dugwall ? "shop" : "door"); } } @@ -4926,7 +4926,7 @@ shk_chat(struct monst* shkp) (!Deaf && !muteshk(shkp)) ? "says" : "indicates"); } else if (is_izchak(shkp, FALSE)) { if (!Deaf && !muteshk(shkp)) - pline(Izchak_speaks[rn2(SIZE(Izchak_speaks))], shkname(shkp)); + pline(ROLL_FROM(Izchak_speaks), shkname(shkp)); } else { if (!Deaf && !muteshk(shkp)) pline("%s talks about the problem of shoplifters.", Shknam(shkp)); diff --git a/src/sounds.c b/src/sounds.c index 34245860f..cfeea58a8 100644 --- a/src/sounds.c +++ b/src/sounds.c @@ -406,7 +406,7 @@ growl(register struct monst* mtmp) /* presumably nearness and soundok checks have already been made */ if (Hallucination) - growl_verb = h_sounds[rn2(SIZE(h_sounds))]; + growl_verb = ROLL_FROM(h_sounds); else growl_verb = growl_sound(mtmp); if (growl_verb) { @@ -432,7 +432,7 @@ yelp(register struct monst* mtmp) /* presumably nearness and soundok checks have already been made */ if (Hallucination) - yelp_verb = h_sounds[rn2(SIZE(h_sounds))]; + yelp_verb = ROLL_FROM(h_sounds); else switch (mtmp->data->msound) { case MS_MEW: @@ -483,7 +483,7 @@ whimper(register struct monst* mtmp) /* presumably nearness and soundok checks have already been made */ if (Hallucination) - whimper_verb = h_sounds[rn2(SIZE(h_sounds))]; + whimper_verb = ROLL_FROM(h_sounds); else switch (mtmp->data->msound) { case MS_MEW: @@ -602,7 +602,7 @@ maybe_gasp(struct monst* mon) break; } if (dogasp) { - return Exclam[rn2(SIZE(Exclam))]; /* [mon->m_id % SIZE(Exclam)]; */ + return ROLL_FROM(Exclam); /* [mon->m_id % SIZE(Exclam)]; */ } return (const char *) 0; } diff --git a/src/sp_lev.c b/src/sp_lev.c index 066cd7f1d..bbeb5930a 100644 --- a/src/sp_lev.c +++ b/src/sp_lev.c @@ -1125,7 +1125,7 @@ rnddoor(void) { static int state[] = { D_NODOOR, D_BROKEN, D_ISOPEN, D_CLOSED, D_LOCKED }; - return state[rn2(SIZE(state))]; + return ROLL_FROM(state); } /* @@ -5789,7 +5789,7 @@ generate_way_out_method( /* generate one of the escape items */ if (selection_rndcoord(ov2, &x, &y, FALSE)) { - mksobj_at(escapeitems[rn2(SIZE(escapeitems))], x, y, TRUE, FALSE); + mksobj_at(ROLL_FROM(escapeitems), x, y, TRUE, FALSE); goto gotitdone; } diff --git a/src/steal.c b/src/steal.c index 1f4b24a93..eff28499d 100644 --- a/src/steal.c +++ b/src/steal.c @@ -419,7 +419,7 @@ steal(struct monst* mtmp, char* objnambuf) "take" }; cant_take: pline("%s tries to %s %s%s but gives up.", Monnam(mtmp), - how[rn2(SIZE(how))], + ROLL_FROM(how), (otmp->owornmask & W_ARMOR) ? "your " : "", (otmp->owornmask & W_ARMOR) ? equipname(otmp) : yname(otmp)); diff --git a/src/trap.c b/src/trap.c index 4f41c2d0e..3bd83449c 100644 --- a/src/trap.c +++ b/src/trap.c @@ -6097,7 +6097,7 @@ chest_trap( case 1: case 0: pline("A cloud of %s gas billows from %s.", - Blind ? blindgas[rn2(SIZE(blindgas))] : rndcolor(), + Blind ? ROLL_FROM(blindgas) : rndcolor(), the(xname(obj))); if (!Stunned) { if (Hallucination) diff --git a/src/u_init.c b/src/u_init.c index 001be68ce..d6afd9a34 100644 --- a/src/u_init.c +++ b/src/u_init.c @@ -849,7 +849,7 @@ u_init(void) if (Role_if(PM_CLERIC) || Role_if(PM_WIZARD)) { static int trotyp[] = { WOODEN_FLUTE, TOOLED_HORN, WOODEN_HARP, BELL, BUGLE, LEATHER_DRUM }; - Instrument[0].trotyp = trotyp[rn2(SIZE(trotyp))]; + Instrument[0].trotyp = ROLL_FROM(trotyp); ini_inv(Instrument); } diff --git a/src/wizard.c b/src/wizard.c index c17966916..16473254e 100644 --- a/src/wizard.c +++ b/src/wizard.c @@ -502,7 +502,7 @@ clonewiz(void) } if (!Protection_from_shape_changers) { mtmp2->m_ap_type = M_AP_MONSTER; - mtmp2->mappearance = wizapp[rn2(SIZE(wizapp))]; + mtmp2->mappearance = ROLL_FROM(wizapp); } newsym(mtmp2->mx, mtmp2->my); } @@ -513,7 +513,7 @@ int pick_nasty( int difcap) /* if non-zero, try to make difficulty be lower than this */ { - int alt, res = nasties[rn2(SIZE(nasties))]; + int alt, res = ROLL_FROM(nasties); /* To do? Possibly should filter for appropriate forms when * in the elemental planes or surrounded by water or lava. @@ -523,7 +523,7 @@ pick_nasty( */ if (Is_rogue_level(&u.uz) && !('A' <= mons[res].mlet && mons[res].mlet <= 'Z')) - res = nasties[rn2(SIZE(nasties))]; + res = ROLL_FROM(nasties); /* if genocided or too difficult or out of place, try a substitute when a suitable one exists @@ -822,20 +822,20 @@ cuss(struct monst *mtmp) } else if (u.uhave.amulet && !rn2(SIZE(random_insult))) { SetVoice(mtmp, 0, 80, 0); verbalize("Relinquish the amulet, %s!", - random_insult[rn2(SIZE(random_insult))]); + ROLL_FROM(random_insult)); } else if (u.uhp < 5 && !rn2(2)) { /* Panic */ SetVoice(mtmp, 0, 80, 0); verbalize(rn2(2) ? "Even now thy life force ebbs, %s!" : "Savor thy breath, %s, it be thy last!", - random_insult[rn2(SIZE(random_insult))]); + ROLL_FROM(random_insult)); } else if (mtmp->mhp < 5 && !rn2(2)) { /* Parthian shot */ SetVoice(mtmp, 0, 80, 0); verbalize(rn2(2) ? "I shall return." : "I'll be back."); } else { SetVoice(mtmp, 0, 80, 0); verbalize("%s %s!", - random_malediction[rn2(SIZE(random_malediction))], - random_insult[rn2(SIZE(random_insult))]); + ROLL_FROM(random_malediction), + ROLL_FROM(random_insult)); } } else if (is_lminion(mtmp) && !(mtmp->isminion && EMIN(mtmp)->renegade)) {