From 54ddcd0f5a0723ed9901593cde8cd5b23ba0a8e3 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 16 Aug 2012 21:05:42 +0200 Subject: [PATCH] New pct_chance(), for clarity, and symmetry with chance() --- include/chance.h | 1 + src/lib/commands/bomb.c | 8 ++++---- src/lib/commands/sona.c | 13 +++++++------ src/lib/gen/chance.c | 11 ++++++++++- src/lib/subs/detonate.c | 2 +- src/lib/subs/list.c | 5 +++-- src/lib/subs/lndsub.c | 4 ++-- src/lib/subs/mission.c | 6 +++--- src/lib/subs/mslsub.c | 2 +- src/lib/subs/shpsub.c | 4 ++-- 10 files changed, 34 insertions(+), 22 deletions(-) diff --git a/include/chance.h b/include/chance.h index da5d1885..fc56ea02 100644 --- a/include/chance.h +++ b/include/chance.h @@ -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); diff --git a/src/lib/commands/bomb.c b/src/lib/commands/bomb.c index 9ebd780c..b97bbed2 100644 --- a/src/lib/commands/bomb.c +++ b/src/lib/commands/bomb.c @@ -30,7 +30,7 @@ * Dave Pare, 1986 * Ken Stevens, 1995 * Steve McClure, 1998-2000 - * Markus Armbruster, 2004-2011 + * Markus Armbruster, 2004-2012 */ #include @@ -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"); diff --git a/src/lib/commands/sona.c b/src/lib/commands/sona.c index 0f27f300..6305a94b 100644 --- a/src/lib/commands/sona.c +++ b/src/lib/commands/sona.c @@ -29,6 +29,7 @@ * Known contributors to this file: * Jim Griffith, 1989 * Ken Stevens, 1995 + * Markus Armbruster, 2004-2012 */ #include @@ -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 diff --git a/src/lib/gen/chance.c b/src/lib/gen/chance.c index 1738ab28..73e24f28 100644 --- a/src/lib/gen/chance.c +++ b/src/lib/gen/chance.c @@ -27,7 +27,7 @@ * chance.c: Roll dice * * Known contributors to this file: - * + * Markus Armbruster, 2006-2012 */ #include @@ -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]. */ diff --git a/src/lib/subs/detonate.c b/src/lib/subs/detonate.c index 4204d262..08f1978c 100644 --- a/src/lib/subs/detonate.c +++ b/src/lib/subs/detonate.c @@ -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) { diff --git a/src/lib/subs/list.c b/src/lib/subs/list.c index 32f1997e..af87027b 100644 --- a/src/lib/subs/list.c +++ b/src/lib/subs/list.c @@ -28,6 +28,7 @@ * * Known contributors to this file: * Dave Pare, 1986 + * Markus Armbruster, 2003-2012 */ #include @@ -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); diff --git a/src/lib/subs/lndsub.c b/src/lib/subs/lndsub.c index 8c4f6c3f..e74bab17 100644 --- a/src/lib/subs/lndsub.c +++ b/src/lib/subs/lndsub.c @@ -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) diff --git a/src/lib/subs/mission.c b/src/lib/subs/mission.c index 08debd42..182c5f52 100644 --- a/src/lib/subs/mission.c +++ b/src/lib/subs/mission.c @@ -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", diff --git a/src/lib/subs/mslsub.c b/src/lib/subs/mslsub.c index f2aaf01c..b93a04de 100644 --- a/src/lib/subs/mslsub.c +++ b/src/lib/subs/mslsub.c @@ -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"); } diff --git a/src/lib/subs/shpsub.c b/src/lib/subs/shpsub.c index 364e5487..e96da878 100644 --- a/src/lib/subs/shpsub.c +++ b/src/lib/subs/shpsub.c @@ -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 @@ -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),