/** * Chooses a monster race that seems "appropriate" to the given level * * This function uses the "prob2" field of the "monster allocation table", * and various local information, to calculate the "prob3" field of the * same table, which is then used to choose an "appropriate" monster, in * a relatively efficient manner. * * Note that "town" monsters will *only* be created in the town, and * "normal" monsters will *never* be created in the town, unless the * "level" is "modified", for example, by polymorph or summoning. * * There is a small chance (1/50) of "boosting" the given depth by * a small amount (up to four levels), except in the town. * * It is (slightly) more likely to acquire a monster of the given level * than one of a lower level. This is done by choosing several monsters * appropriate to the given level and keeping the "hardest" one. * * Note that if no monsters are "appropriate", then this function will * fail, and return zero, but this should *almost* never happen. */ struct monster_race *get_mon_num(int level) { int i, p; long total; struct monster_race *race; alloc_entry *table = alloc_race_table; /* Occasionally produce a nastier monster in the dungeon */ if (level > 0 && one_in_(z_info->ood_monster_chance)) level += MIN(level / 4 + 2, z_info->ood_monster_amount); total = 0L; /* Process probabilities */ for (i = 0; i < alloc_race_size; i++) { time_t cur_time = time(NULL); struct tm *date = localtime(&cur_time); /* Monsters are sorted by depth */ if (table[i].level > level) break; /* Default */ table[i].prob3 = 0; /* No town monsters in dungeon */ if ((level > 0) && (table[i].level <= 0)) continue; /* Get the chosen monster */ race = &r_info[table[i].index]; /* No seasonal monsters outside of Christmas */ if (rf_has(race->flags, RF_SEASONAL) && !(date->tm_mon == 11 && date->tm_mday >= 24 && date->tm_mday <= 26)) continue; /* Only one copy of a a unique must be around at the same time */ if (rf_has(race->flags, RF_UNIQUE) && race->cur_num >= race->max_num) continue; /* Some monsters never appear out of depth */ if (rf_has(race->flags, RF_FORCE_DEPTH) && race->level > player->depth) continue; /* Accept */ table[i].prob3 = table[i].prob2; /* Total */ total += table[i].prob3; } /* No legal monsters */ if (total <= 0) return NULL; /* Pick a monster */ race = get_mon_race_aux(total, table); /* Try for a "harder" monster once (50%) or twice (10%) */ p = randint0(100); if (p < 60) { struct monster_race *old = race; /* Pick a new monster */ race = get_mon_race_aux(total, table); /* Keep the deepest one */ if (race->level < old->level) race = old; } /* Try for a "harder" monster twice (10%) */ if (p < 10) { struct monster_race *old = race; /* Pick a monster */ race = get_mon_race_aux(total, table); /* Keep the deepest one */ if (race->level < old->level) race = old; } /* Result */ return race; }
/** * Chooses a monster race that seems "appropriate" to the given level * * This function uses the "prob2" field of the "monster allocation table", * and various local information, to calculate the "prob3" field of the * same table, which is then used to choose an "appropriate" monster, in * a relatively efficient manner. * * Note that "town" monsters will *only* be created in the town, and * "normal" monsters will *never* be created in the town, unless the * "level" is "modified", for example, by polymorph or summoning. * * There is a small chance (1/50) of "boosting" the given depth by * a small amount (up to four levels), except in the town. * * It is (slightly) more likely to acquire a monster of the given level * than one of a lower level. This is done by choosing several monsters * appropriate to the given level and keeping the "hardest" one. * * Note that if no monsters are "appropriate", then this function will * fail, and return zero, but this should *almost* never happen. */ monster_race *get_mon_num(int level) { int i, p; long total; monster_race *race; alloc_entry *table = alloc_race_table; /* Occasionally produce a nastier monster in the dungeon */ if (level > 0 && one_in_(NASTY_MON)) level += MIN(level / 4 + 2, MON_OOD_MAX); total = 0L; /* Process probabilities */ for (i = 0; i < alloc_race_size; i++) { /* Monsters are sorted by depth */ if (table[i].level > level) break; /* Default */ table[i].prob3 = 0; /* No town monsters in dungeon */ if ((level > 0) && (table[i].level <= 0)) continue; /* Get the chosen monster */ race = &r_info[table[i].index]; /* Only one copy of a a unique must be around at the same time */ if (rf_has(race->flags, RF_UNIQUE) && race->cur_num >= race->max_num) continue; /* Some monsters never appear out of depth */ if (rf_has(race->flags, RF_FORCE_DEPTH) && race->level > p_ptr->depth) continue; /* Accept */ table[i].prob3 = table[i].prob2; /* Total */ total += table[i].prob3; } /* No legal monsters */ if (total <= 0) return NULL; /* Pick a monster */ race = get_mon_race_aux(total, table); /* Try for a "harder" monster once (50%) or twice (10%) */ p = randint0(100); if (p < 60) { monster_race *old = race; /* Pick a new monster */ race = get_mon_race_aux(total, table); /* Keep the deepest one */ if (race->level < old->level) race = old; } /* Try for a "harder" monster twice (10%) */ if (p < 10) { monster_race *old = race; /* Pick a monster */ race = get_mon_race_aux(total, table); /* Keep the deepest one */ if (race->level < old->level) race = old; } /* Result */ return race; }