/** * True if the square can hold a trap. */ bool square_istrappable(struct chunk *c, int y, int x) { return feat_is_trap_holding(c->squares[y][x].feat); }
/** * Instantiate a player trap */ static int pick_trap(struct chunk *c, int feat, int trap_level) { int i, pick; int *trap_probs = NULL; int trap_prob_max = 0; /* Paranoia */ if (!feat_is_trap_holding(feat)) return -1; /* No traps in town */ if (c->depth == 0) return -1; /* Get trap probabilities */ trap_probs = mem_zalloc(z_info->trap_max * sizeof(int)); for (i = 0; i < z_info->trap_max; i++) { /* Get this trap */ struct trap_kind *kind = &trap_info[i]; trap_probs[i] = trap_prob_max; /* Ensure that this is a valid player trap */ if (!kind->name) continue; if (!kind->rarity) continue; if (!trf_has(kind->flags, TRF_TRAP)) continue; /* Require that trap_level not be too low */ if (kind->min_depth > trap_level) continue; /* Floor? */ if (feat_is_floor(feat) && !trf_has(kind->flags, TRF_FLOOR)) continue; /* Check legality of trapdoors. */ if (trf_has(kind->flags, TRF_DOWN)) { /* No trap doors on quest levels */ if (is_quest(player->depth)) continue; /* No trap doors on the deepest level */ if (player->depth >= z_info->max_depth - 1) continue; /* No trap doors with persistent levels (for now) */ if (OPT(player, birth_levels_persist)) continue; } /* Trap is okay, store the cumulative probability */ trap_probs[i] += (100 / kind->rarity); trap_prob_max = trap_probs[i]; } /* No valid trap */ if (trap_prob_max == 0) { mem_free(trap_probs); return -1; } /* Pick at random. */ pick = randint0(trap_prob_max); for (i = 0; i < z_info->trap_max; i++) { if (pick < trap_probs[i]) { break; } } mem_free(trap_probs); /* Return our chosen trap */ return i < z_info->trap_max ? i : -1; }