]> git.pond.sub.org Git - empserver/commitdiff
New pct_chance(), for clarity, and symmetry with chance()
authorMarkus Armbruster <armbru@pond.sub.org>
Thu, 16 Aug 2012 19:05:42 +0000 (21:05 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Wed, 8 May 2013 04:55:20 +0000 (06:55 +0200)
include/chance.h
src/lib/commands/bomb.c
src/lib/commands/sona.c
src/lib/gen/chance.c
src/lib/subs/detonate.c
src/lib/subs/list.c
src/lib/subs/lndsub.c
src/lib/subs/mission.c
src/lib/subs/mslsub.c
src/lib/subs/shpsub.c

index da5d18852b8047bbc0ecf2e9b09a3e1e41576f6b..fc56ea02e9e8706f18354d5265b49feccf596eca 100644 (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);
index 9ebd780cb308abb9c8500ae4a236c26103e1230f..b97bbed27ae487aa366265196315131733b60733 100644 (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");
index 0f27f300bc621b8c45311644926e07dbd485c3e2..6305a94b9058d1c86f1df8925679fe2f655f8344 100644 (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
index 1738ab280b03efc9b033b763de909cdd9882390c..73e24f286b0d7793475288f58b43f004eab68fa1 100644 (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].
  */
index 4204d2629e2f32d7058020f9b74a80ff044d52cd..08f1978c7c2716533db4ae98161502bc9ab8e08e 100644 (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) {
index 32f1997e180ff37082c03165466a421291df64d4..af87027b12354f4aa3a52509ac6c5db45a87b253 100644 (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);
index 8c4f6c3f2b443484e0f5de984f3173501650e9fc..e74bab17de650a5a93987ed5d87d9cc7094f9a85 100644 (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)
index 08debd42a30b0c55f0270219092d205303535267..182c5f52aec5bd7030e779dbdde8aabf97af4288 100644 (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",
index f2aaf01c7ea94d4d6b5d65725d3b591a2dee4986..b93a04de7cdf1a7f8ad64cf2b1835fc41fa8183b 100644 (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");
     }
index 364e54870c40cf0813ff57852020b1db46e393a7..e96da87834da6f412b4044aa30636b970939be6b 100644 (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),