New pct_chance(), for clarity, and symmetry with chance()

This commit is contained in:
Markus Armbruster 2012-08-16 21:05:42 +02:00
parent 1cf6b5e6bb
commit 54ddcd0f5a
10 changed files with 34 additions and 22 deletions

View file

@ -34,6 +34,7 @@
#define CHANCE_H
extern int chance(double);
extern int pct_chance(int);
extern int roll0(int);
extern int roll(int);
extern int roundavg(double);

View file

@ -30,7 +30,7 @@
* Dave Pare, 1986
* Ken Stevens, 1995
* Steve McClure, 1998-2000
* Markus Armbruster, 2004-2011
* Markus Armbruster, 2004-2012
*/
#include <config.h>
@ -455,7 +455,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
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 {
@ -555,7 +555,7 @@ plane_bomb(struct emp_qelem *list, struct sctstr *target)
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 {
@ -664,7 +664,7 @@ land_bomb(struct emp_qelem *list, struct sctstr *target)
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");

View file

@ -29,6 +29,7 @@
* Known contributors to this file:
* Jim Griffith, 1989
* Ken Stevens, 1995
* Markus Armbruster, 2004-2012
*/
#include <config.h>
@ -250,8 +251,8 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
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;
@ -276,10 +277,10 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
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

View file

@ -27,7 +27,7 @@
* chance.c: Roll dice
*
* Known contributors to this file:
*
* Markus Armbruster, 2006-2012
*/
#include <config.h>
@ -45,6 +45,15 @@ chance(double d)
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].
*/

View file

@ -280,7 +280,7 @@ detonate(struct nukstr *np, coord x, coord y, int airburst)
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) {

View file

@ -28,6 +28,7 @@
*
* Known contributors to this file:
* Dave Pare, 1986
* Markus Armbruster, 2003-2012
*/
#include <config.h>
@ -229,8 +230,8 @@ asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
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);

View file

@ -212,7 +212,7 @@ lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
/* 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;
@ -1099,7 +1099,7 @@ lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
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)

View file

@ -835,7 +835,7 @@ mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
}
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;
@ -1040,7 +1040,7 @@ air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
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));
@ -1065,7 +1065,7 @@ air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
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",

View file

@ -163,7 +163,7 @@ msl_hit(struct plnstr *pp, int hardtarget, int type,
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");
}

View file

@ -29,7 +29,7 @@
* Known contributors to this file:
* Ken Stevens, 1995
* Steve McClure, 1996-2000
* Markus Armbruster, 2006-2010
* Markus Armbruster, 2006-2012
*/
#include <config.h>
@ -875,7 +875,7 @@ shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
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),