From: nhkeni Date: Wed, 16 Mar 2022 21:59:23 +0000 (-0400) Subject: LIMIT_TO_RANGE_INT macro and various casts. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e51026aee18007eb507db2a27ca56aa078d73fc5;p=nethack LIMIT_TO_RANGE_INT macro and various casts. --- diff --git a/include/hack.h b/include/hack.h index f30e7394d..7be2672a2 100644 --- a/include/hack.h +++ b/include/hack.h @@ -642,6 +642,15 @@ enum optset_restrictions { #endif #define plur(x) (((x) == 1) ? "" : "s") +/* Cast to int, but limit value to range. */ +#define LIMIT_TO_RANGE_INT(lo, hi, var) \ + (int) ( \ + (var) < (lo) ? (lo) : ( \ + (var) > (hi) ? (hi) : \ + (var) \ + ) \ + ) + #define ARM_BONUS(obj) \ (objects[(obj)->otyp].a_ac + (obj)->spe \ - min((int) greatest_erosion(obj), objects[(obj)->otyp].a_ac)) diff --git a/src/do_wear.c b/src/do_wear.c index 925512448..7f01697c7 100644 --- a/src/do_wear.c +++ b/src/do_wear.c @@ -1533,7 +1533,7 @@ stop_donning(struct obj *stolenobj) /* no message if stolenobj is already thesimpleoname(otmp)); } else { buf[0] = '\0'; /* silently stop doffing stolenobj */ - result = -g.multi; /* remember this before calling unmul() */ + result = (int) -g.multi; /* remember this before calling unmul() */ } unmul(buf); /* while putting on, item becomes worn immediately but side-effects are diff --git a/src/dothrow.c b/src/dothrow.c index bdd35db79..f171b2989 100644 --- a/src/dothrow.c +++ b/src/dothrow.c @@ -257,7 +257,7 @@ throw_obj(struct obj *obj, int shotlimit) static boolean ok_to_throw(int *shotlimit_p) /* (see dothrow()) */ { - *shotlimit_p = g.command_count; + *shotlimit_p = LIMIT_TO_RANGE_INT(0, LARGEST_INT, g.command_count); g.multi = 0; /* reset; it's been used up */ if (notake(g.youmonst.data)) { diff --git a/src/hack.c b/src/hack.c index 603f0a05f..68650075e 100644 --- a/src/hack.c +++ b/src/hack.c @@ -3176,7 +3176,7 @@ dopickup(void) { int count, tmpcount, ret; - count = g.command_count; + count = (int) g.command_count; g.multi = 0; /* always reset */ if ((ret = pickup_checks()) >= 0) { diff --git a/src/read.c b/src/read.c index db8800b86..f19252181 100644 --- a/src/read.c +++ b/src/read.c @@ -2894,7 +2894,7 @@ create_particular_parse(char* str, struct _create_particular_data* d) char *bufp = str; char *tmpp; - d->quan = 1 + ((g.multi > 0) ? g.multi : 0); + d->quan = 1 + ((g.multi > 0) ? (int) g.multi : 0); d->monclass = MAXMCLASSES; d->which = g.urole.mnum; /* an arbitrary index into mons[] */ d->fem = -1; /* gender not specified */