1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/**
* @file
* @brief Functions used to help determine which monsters should appear.
**/
#include "AppHdr.h"
#include "mon-pick.h"
#include "mon-pick-data.h"
#include "branch.h"
#include "coord.h"
#include "env.h"
#include "errors.h"
#include "libutil.h"
#include "mon-place.h"
#include "place.h"
#include "stringutil.h"
int branch_ood_cap(branch_type branch)
{
ASSERT(branch < NUM_BRANCHES);
switch (branch)
{
case BRANCH_DUNGEON:
return 27;
case BRANCH_COCYTUS:
case BRANCH_GEHENNA:
case BRANCH_TARTARUS:
case BRANCH_DIS:
return 14;
case BRANCH_DEPTHS:
return 13;
case BRANCH_VAULTS:
case BRANCH_LAIR:
return 12;
case BRANCH_ELF:
case BRANCH_SWAMP:
case BRANCH_SPIDER:
case BRANCH_ZOT:
return 7;
case BRANCH_CRYPT:
case BRANCH_TOMB:
return 5;
default:
return branches[branch].numlevels;
}
}
// only Pan currently
monster_type pick_monster_no_rarity(branch_type branch)
{
if (!population[branch].size())
return MONS_0;
return population[branch][random2(population[branch].size())].value;
}
monster_type pick_monster_by_hash(branch_type branch, uint32_t hash)
{
if (!population[branch].size())
return MONS_0;
return population[branch][hash % population[branch].size()].value;
}
monster_type pick_monster(level_id place, mon_pick_vetoer veto)
{
#ifdef ASSERTS
if (!place.is_valid())
die("trying to pick a monster from %s", place.describe().c_str());
#endif
return pick_monster_from(population[place.branch], place.depth, veto);
}
monster_type pick_monster(level_id place, monster_picker &picker, mon_pick_vetoer veto)
{
ASSERT(place.is_valid());
return picker.pick_with_veto(population[place.branch], place.depth, MONS_0, veto);
}
monster_type pick_monster_from(const vector<pop_entry>& fpop, int depth,
mon_pick_vetoer veto)
{
// XXX: If creating/destroying instances has performance issues, cache a
// static instance
monster_picker picker = monster_picker();
return picker.pick_with_veto(fpop, depth, MONS_0, veto);
}
monster_type monster_picker::pick_with_veto(const vector<pop_entry>& weights,
int level, monster_type none,
mon_pick_vetoer vetoer)
{
_veto = vetoer;
return pick(weights, level, none);
}
// Veto specialisation for the monster_picker class; this simply calls the
// stored veto function. Can subclass further for more complex veto behaviour.
bool monster_picker::veto(monster_type mon)
{
return _veto && (invalid_monster_type(mon) || _veto(mon));
}
bool positioned_monster_picker::veto(monster_type mon)
{
// Actually pick a monster that is happy where we want to put it.
// Fish zombies on land are helpless and uncool.
if (in_bounds(pos) && !monster_habitable_grid(mon, pos))
return true;
// Optional positional veto
if (posveto && posveto(mon, pos))
return true;
return monster_picker::veto(mon);
}
monster_type pick_monster_all_branches(int absdepth0, mon_pick_vetoer veto)
{
monster_picker picker = monster_picker();
return pick_monster_all_branches(absdepth0, picker, veto);
}
// Used for picking zombies when there's nothing native.
// TODO: cache potential zombifiables for the given level/size, with a
// second pass to select ones that have a skeleton/etc and can be placed in
// a given spot.
monster_type pick_monster_all_branches(int absdepth0, monster_picker &picker,
mon_pick_vetoer veto)
{
monster_type valid[NUM_MONSTERS];
int rarities[NUM_MONSTERS];
memset(rarities, 0, sizeof(rarities));
int nvalid = 0;
for (branch_iterator it; it; ++it)
{
int depth = absdepth0 - absdungeon_depth(it->id, 0);
if (depth < 1 || depth > branch_ood_cap(it->id))
continue;
for (const pop_entry& pop : population[it->id])
{
if (depth < pop.minr || depth > pop.maxr)
continue;
if (veto ? (*veto)(pop.value) : picker.veto(pop.value))
continue;
int rar = picker.rarity_at(pop, depth);
ASSERT(rar > 0);
monster_type mons = pop.value;
if (!rarities[mons])
valid[nvalid++] = pop.value;
if (rarities[mons] < rar)
rarities[mons] = rar;
}
}
if (!nvalid)
return MONS_0;
int totalrar = 0;
for (int i = 0; i < nvalid; i++)
totalrar += rarities[valid[i]];
totalrar = random2(totalrar); // the roll!
for (int i = 0; i < nvalid; i++)
if ((totalrar -= rarities[valid[i]]) < 0)
return valid[i];
die("mon-pick roll out of range");
}
bool branch_has_monsters(branch_type branch)
{
ASSERT_RANGE(branch, 0, NUM_BRANCHES);
return population[branch].size();
}
const vector<pop_entry>& fish_population(branch_type br, bool lava)
{
ASSERT_RANGE(br, 0, NUM_BRANCHES);
return (lava ? population_lava : population_water)[br];
}
static bool _branch_uses_generic_late_zombies(branch_type br)
{
ASSERT_RANGE(br, 0, NUM_BRANCHES);
return is_hell_branch(br) || br == BRANCH_CRYPT || br == BRANCH_TOMB
|| br == BRANCH_ABYSS || br == BRANCH_PANDEMONIUM;
}
int branch_zombie_cap(branch_type br)
{
ASSERT_RANGE(br, 0, NUM_BRANCHES);
if (_branch_uses_generic_late_zombies(br))
return 15;
else
return branch_ood_cap(br);
}
const vector<pop_entry>& zombie_population(branch_type br)
{
ASSERT_RANGE(br, 0, NUM_BRANCHES);
if (_branch_uses_generic_late_zombies(br))
return pop_generic_late_zombie;
return population[br];
}
bool monster_in_population(branch_type branch, monster_type m, bool check_noncore)
{
for (const pop_entry& pop : population[branch])
if (pop.value == m)
return true;
if (check_noncore)
{
for (const pop_entry& pop : population_water[branch])
if (pop.value == m)
return true;
for (const pop_entry& pop : population_lava[branch])
if (pop.value == m)
return true;
if (_branch_uses_generic_late_zombies(branch))
for (const pop_entry& pop : pop_generic_late_zombie)
if (pop.value == m)
return true;
}
return false;
}
int monster_probability(level_id place, monster_type m)
{
// TODO: does not check fish, zombies etc...
if (!monster_in_population(place.branch, m, false))
return -1;
monster_picker picker = monster_picker();
return picker.probability_at(m, population[place.branch], place.depth);
}
int monster_pop_depth_avg(branch_type branch, monster_type m)
{
for (const pop_entry& pop : population[branch])
if (pop.value == m)
return max(0, (pop.minr + pop.maxr) / 2);
// negative values *are* sometimes used in the data, but the above max will
// prevent them from ever being naturally returned as an average.
return -1;
}
// if a monster is ood, how far ood is it? If it's not ood but in the branch
// data, return 0. If it's not in the branch data, return -1.
int monster_how_ood(branch_type branch, int depth, monster_type m)
{
for (const pop_entry& pop : population[branch])
if (pop.value == m)
return max(0, pop.minr - depth);
return -1;
}
#if defined(DEBUG_DIAGNOSTICS) || defined(DEBUG_TESTS)
static bool _not_skeletonable(monster_type mt)
{
// Zombifiability in general.
if (mons_species(mt) != mt)
return true;
if (!mons_zombie_size(mt) || mons_is_unique(mt))
return true;
if (!(mons_class_holiness(mt) & MH_NATURAL))
return true;
return !mons_has_skeleton(mt);
}
void debug_monpick()
{
string fails;
for (branch_iterator it; it; ++it)
{
branch_type br = it->id;
for (int d = 1; d <= branch_ood_cap(br); d++)
{
if (!pick_monster_all_branches(absdungeon_depth(br, d),
_not_skeletonable)
&& br != BRANCH_ZIGGURAT) // order is ok, check the loop
{
fails += make_stringf(
"%s: no valid skeletons in any parallel branch\n",
level_id(br, d).describe().c_str());
}
}
}
dump_test_fails(fails, "mon-pick");
}
#endif
|