From 6189a6686626c7055a50478411d0e175579bf9c4 Mon Sep 17 00:00:00 2001 From: onpon4 Date: Tue, 5 Jan 2016 06:59:37 -0500 Subject: [PATCH] Added CHANCE macro to replace difficult-to-read special rand() uses. I found myself confused by the interception chances, because it looked like the chance of interceptions was decreasing slowly over time. Turns out, it was the denominator in a fraction; a "chance" of 300 meant that there was a 1 / 300 chance. --- src/defs.h | 5 +++++ src/intermission.cpp | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/defs.h b/src/defs.h index 81d2768..c1bc414 100644 --- a/src/defs.h +++ b/src/defs.h @@ -20,6 +20,10 @@ along with this program. If not, see . #ifndef DEFS_H #define DEFS_H +#include + + +// Macros #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define LIMIT(x, a, b) x = ((x) < (b) ? ((x) > (a) ? (x) : (a)) : (b)) @@ -29,6 +33,7 @@ along with this program. If not, see . #define WRAP_ADD(x, y, a, b) x = (((x) + (y)) + \ ((x) + (y) < (a) ? ((b) - (a)) : 0) + \ ((x) + (y) > (b) ? ((a) - (b)) : 0)) +#define CHANCE(x) ((rand() % RAND_MAX) < ((x) * RAND_MAX)) #define RANDRANGE(x, y) (((x) < (y)) ? ((x) + (rand() % (1 + (y) - (x)))) : (x)) // ALL diff --git a/src/intermission.cpp b/src/intermission.cpp index f2a1ef4..af6dae1 100644 --- a/src/intermission.cpp +++ b/src/intermission.cpp @@ -632,7 +632,7 @@ int intermission() SDL_Rect r; SDL_Rect destRect; int distance = 0; - int interceptionChance; + double interceptionChance; intermission_setStatusLines(); initShop(); @@ -670,17 +670,17 @@ int intermission() switch (game.system) { case SYSTEM_EYANANTH: - interceptionChance = 300; + interceptionChance = 1. / 300.; break; case SYSTEM_MORDOR: - interceptionChance = 150; + interceptionChance = 1. / 150.; break; case SYSTEM_SOL: // There is no chance of being interceptted after the final attack on Earth if ((game.system == SYSTEM_SOL) && (systemPlanet[2].missionCompleted)) interceptionChance = 0; else - interceptionChance = 100; + interceptionChance = 1. / 100.; break; default: interceptionChance = 0; @@ -894,7 +894,7 @@ int intermission() } else if (interceptionChance > 0) { - if ((rand() % interceptionChance) == 0) + if (CHANCE(interceptionChance)) { game.area = MISN_INTERCEPTION; rtn = 2;