#define CHANCE_H
extern int chance(double);
+extern int pct_chance(int);
extern int roll0(int);
extern int roll(int);
extern int roundavg(double);
* Dave Pare, 1986
* Ken Stevens, 1995
* Steve McClure, 1998-2000
- * Markus Armbruster, 2004-2011
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
shp_hardtarget(&ship), EF_SHIP);
pr("%d%% hitchance...", hitchance);
}
- if (roll(100) <= hitchance) {
+ if (pct_chance(hitchance)) {
/* pinbombing is more accurate than normal bombing */
dam = 2 * pln_damage(&plp->plane, 'p', 1);
} else {
hitchance = pln_hitchance(&plp->plane, 0, EF_PLANE);
pr("%d%% hitchance...", hitchance);
}
- if (roll(100) <= hitchance) {
+ if (pct_chance(hitchance)) {
/* pinbombing is more accurate than normal bombing */
dam = 2 * pln_damage(&plp->plane, 'p', 1);
} else {
lnd_hardtarget(&land), EF_LAND);
pr("%d%% hitchance...", hitchance);
}
- if (roll(100) <= hitchance) {
+ if (pct_chance(hitchance)) {
dam = 2 * pln_damage(&plp->plane, 'p', 1);
} else {
pr("thud\n");
* Known contributors to this file:
* Jim Griffith, 1989
* Ken Stevens, 1995
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
tmcp = &mchr[(int)targ->shp_type];
if (!(tmcp->m_flags & M_SUB))
continue;
- if (roll(100) > pln_identchance(pp, shp_hardtarget(targ),
- EF_SHIP))
+ if (!pct_chance(pln_identchance(pp, shp_hardtarget(targ),
+ EF_SHIP)))
continue;
vis = shp_visib(targ);
pingrange = MAX(vis, 10) * range / 10;
found = 1;
}
if (relations_with(targ->shp_own, pp->pln_own) < FRIENDLY &&
- roll(100) > pln_identchance(pp, shp_hardtarget(targ),
- EF_SHIP))
- if (roll(100) > pln_identchance(pp, shp_hardtarget(targ),
- EF_SHIP))
+ !pct_chance(pln_identchance(pp, shp_hardtarget(targ),
+ EF_SHIP)))
+ if (!pct_chance(pln_identchance(pp, shp_hardtarget(targ),
+ EF_SHIP)))
pr("sub #%d %s\n", targ->shp_uid,
xyas(targ->shp_x, targ->shp_y, player->cnum));
else
* chance.c: Roll dice
*
* Known contributors to this file:
- *
+ * Markus Armbruster, 2006-2012
*/
#include <config.h>
return d > (random() % 32768) / 32768.0;
}
+/*
+ * Return non-zero with probability PCT%.
+ */
+int
+pct_chance(int pct)
+{
+ return roll(100) <= pct;
+}
+
/*
* Return a random number in [0..N-1].
*/
continue;
if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
continue;
- if (roll(100) > damage)
+ if (!pct_chance(damage))
continue;
nuke.nuk_effic = 0;
if (own == player->cnum) {
*
* Known contributors to this file:
* Dave Pare, 1986
+ * Markus Armbruster, 2003-2012
*/
#include <config.h>
continue;
}
if (mp->m_flags & M_SUB) {
- if (roll(100) > pln_hitchance(pp,
- shp_hardtarget(&ship), EF_SHIP))
+ if (!pct_chance(pln_hitchance(pp,
+ shp_hardtarget(&ship), EF_SHIP)))
continue;
}
add_shiplist(ship.shp_uid, head);
/* Have to make a retreat check */
ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
- if (roll(100) <= ret_chance) {
+ if (pct_chance(ret_chance)) {
pr("\n");
lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
llp->unit.land.lnd_mission = 0;
nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
else
nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
- if (roll(100) < lnd_acc(&land))
+ if (pct_chance(lnd_acc(&land) - 1))
dam2 /= 2;
dam += dam2;
if (land.lnd_own != attacker)
}
if (pcp->pl_flags & P_A) {
- if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
+ if (!pct_chance(pln_identchance(pp, hardtarget, EF_SHIP))) {
emp_remque(qp);
free(qp);
continue;
continue;
if (plp->pcp->pl_flags & P_A) {
- if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
+ if (!pct_chance(pln_identchance(pp, hardtarget, EF_SHIP))) {
wu(0, pp->pln_own,
"\t%s detects sub movement in %s\n",
prplane(pp), xyas(x, y, pp->pln_own));
hitchance = 100;
else if (hardtarget != SECT_HARDTARGET)
wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
- if (roll(100) <= hitchance) {
+ if (pct_chance(hitchance)) {
newdam = pln_damage(&plp->plane, 'p', 1);
wu(0, pp->pln_own,
"\t\thit %s %s for %d damage\n",
hit = 1;
} else {
hitchance = pln_hitchance(pp, hardtarget, type);
- hit = (roll(100) <= hitchance);
+ hit = pct_chance(hitchance);
mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
hit ? "HIT!" : "miss");
}
* Known contributors to this file:
* Ken Stevens, 1995
* Steve McClure, 1996-2000
- * Markus Armbruster, 2006-2010
+ * Markus Armbruster, 2006-2012
*/
#include <config.h>
hitchance = 0;
if (hitchance > 100)
hitchance = 100;
- hit = roll(100) <= hitchance;
+ hit = pct_chance(hitchance);
mpr(bombown, "%s anti-missile system activated...%s\n",
cname(ship.shp_own),