From 3b81b7797f1bb471439ee0d99e3ee9e0f5b3c13d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 24 Jan 2019 17:50:58 +0100 Subject: [PATCH] eliminate MAX/MIN macros completely --- src/academy.c | 2 +- src/battle.c | 114 +++++++++++++++++++----------------------- src/items/xerewards.c | 3 +- src/kernel/group.c | 2 +- src/kernel/order.c | 4 ++ src/kernel/pool.c | 4 +- src/kernel/pool.h | 4 +- src/kernel/save.c | 5 +- src/morale.c | 3 +- src/platform.h | 3 -- src/races/dragons.c | 5 +- src/races/zombies.c | 15 ++++-- 12 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/academy.c b/src/academy.c index 538d36e06..01ff9fd1c 100644 --- a/src/academy.c +++ b/src/academy.c @@ -27,7 +27,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "study.h" void academy_teaching_bonus(struct unit *u, skill_t sk, int students) { - if (students && sk != NOSKILL) { + if (students > 0 && sk != NOSKILL) { /* actually students * EXPERIENCEDAYS / MAX_STUDENTS */ learn_skill(u, sk, students); } diff --git a/src/battle.c b/src/battle.c index f65a8d5d2..df937698f 100644 --- a/src/battle.c +++ b/src/battle.c @@ -16,7 +16,10 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. **/ +#ifdef _MSC_VER #include +#endif + #include #include "battle.h" #include "alchemy.h" @@ -436,7 +439,8 @@ static int get_row(const side * s, int row, const side * vs) /* every entry in the size[] array means someone trying to defend us. * 'retreat' is the number of rows falling. */ - result = MAX(FIRST_ROW, row - retreat); + result = row - retreat; + if (result < FIRST_ROW) result = FIRST_ROW; return result; } @@ -587,50 +591,27 @@ weapon_skill(const weapon_type * wtype, const unit * u, bool attacking) * are taken into account, e.g. no horses, magic, etc. */ { int skill; + const race * rc = u_race(u); if (wtype == NULL) { skill = effskill(u, SK_WEAPONLESS, NULL); + int def = attacking ? rc->at_default : rc->df_default; if (skill <= 0) { /* wenn kein waffenloser kampf, dann den rassen-defaultwert */ - if (u_race(u) == get_race(RC_ORC)) { + if (rc == get_race(RC_ORC)) { int sword = effskill(u, SK_MELEE, NULL); int spear = effskill(u, SK_SPEAR, NULL); - skill = MAX(sword, spear) - 3; - if (attacking) { - skill = MAX(skill, u_race(u)->at_default); - } - else { - skill = MAX(skill, u_race(u)->df_default); - } - } - else { - if (attacking) { - skill = u_race(u)->at_default; - } - else { - skill = u_race(u)->df_default; - } - } - } - else { - /* der rassen-defaultwert kann h�her sein als der Talentwert von - * waffenloser kampf */ - if (attacking) { - if (skill < u_race(u)->at_default) - skill = u_race(u)->at_default; - } - else { - if (skill < u_race(u)->df_default) - skill = u_race(u)->df_default; + skill = ((sword > spear) ? sword : spear) - 3; } } + if (def > skill) skill = def; if (attacking) { - skill += u_race(u)->at_bonus; + skill += rc->at_bonus; if (fval(u->region->terrain, SEA_REGION) && u->ship) skill += u->ship->type->at_bonus; } else { - skill += u_race(u)->df_bonus; + skill += rc->df_bonus; if (fval(u->region->terrain, SEA_REGION) && u->ship) skill += u->ship->type->df_bonus; } @@ -642,10 +623,10 @@ weapon_skill(const weapon_type * wtype, const unit * u, bool attacking) skill = effskill(u, wtype->skill, NULL); if (skill > 0) { if (attacking) { - skill += u_race(u)->at_bonus; + skill += rc->at_bonus; } else { - skill += u_race(u)->df_bonus; + skill += rc->df_bonus; } } if (attacking) { @@ -1261,32 +1242,33 @@ static int apply_race_resistance(int reduced_damage, fighter *df, static int apply_magicshield(int reduced_damage, fighter *df, const weapon_type *awtype, battle *b, bool magic) { - side *ds = df->side; - selist *ql; - int qi; + side *ds = df->side; + selist *ql; + int qi; - if (reduced_damage <= 0) - return 0; + if (reduced_damage <= 0) + return 0; - /* Schilde */ - for (qi = 0, ql = b->meffects; ql; selist_advance(&ql, &qi, 1)) { - meffect *me = (meffect *) selist_get(ql, qi); - if (meffect_protection(b, me, ds) != 0) { - assert(0 <= reduced_damage); /* rda sollte hier immer mindestens 0 sein */ - /* jeder Schaden wird um effect% reduziert bis der Schild duration - * Trefferpunkte aufgefangen hat */ - if (me->typ == SHIELD_REDUCE) { - int hp = reduced_damage * (me->effect / 100); - reduced_damage -= hp; - me->duration -= hp; - } - /* gibt R�stung +effect f�r duration Treffer */ - if (me->typ == SHIELD_ARMOR) { - reduced_damage = MAX(reduced_damage - me->effect, 0); - me->duration--; - } + /* Schilde */ + for (qi = 0, ql = b->meffects; ql; selist_advance(&ql, &qi, 1)) { + meffect *me = (meffect *)selist_get(ql, qi); + if (meffect_protection(b, me, ds) != 0) { + assert(0 <= reduced_damage); /* rda sollte hier immer mindestens 0 sein */ + /* jeder Schaden wird um effect% reduziert bis der Schild duration + * Trefferpunkte aufgefangen hat */ + if (me->typ == SHIELD_REDUCE) { + int hp = reduced_damage * (me->effect / 100); + reduced_damage -= hp; + me->duration -= hp; + } + /* gibt R�stung +effect f�r duration Treffer */ + if (me->typ == SHIELD_ARMOR) { + reduced_damage -= me->effect; + if (reduced_damage < 0) reduced_damage = 0; + me->duration--; + } + } } - } return reduced_damage; } @@ -1347,11 +1329,13 @@ terminate(troop dt, troop at, int type, const char *damage_formula, bool missile /* Skilldifferenzbonus */ if (rule_damage & DAMAGE_SKILL_BONUS) { - damage += MAX(0, (attskill - defskill) / DAMAGE_QUOTIENT); + int b = (attskill - defskill) / DAMAGE_QUOTIENT; + if (b > 0) damage += b; } } - reduced_damage = MAX(damage - armor_value, 0); + reduced_damage = damage - armor_value; + if (reduced_damage < 0) reduced_damage = 0; reduced_damage = apply_race_resistance(reduced_damage, df, awtype, magic); reduced_damage = apply_magicshield(reduced_damage, df, awtype, b, magic); @@ -1500,7 +1484,8 @@ troop select_enemy(fighter * af, int minrow, int maxrow, int select) minrow = FIGHT_ROW; maxrow = BEHIND_ROW; } - minrow = MAX(minrow, FIGHT_ROW); + + if (minrow < FIGHT_ROW) minrow = FIGHT_ROW; enemies = count_enemies(b, af, minrow, maxrow, select); @@ -1611,7 +1596,7 @@ static troop select_opponent(battle * b, troop at, int mindist, int maxdist) dt = select_enemy(at.fighter, FIGHT_ROW, BEHIND_ROW, SELECT_ADVANCE); } else { - mindist = MAX(mindist, FIGHT_ROW); + if (mindist < FIGHT_ROW) mindist = FIGHT_ROW; dt = select_enemy(at.fighter, mindist, maxdist, SELECT_ADVANCE); } @@ -2078,7 +2063,8 @@ void dazzle(battle * b, troop * td) void damage_building(battle * b, building * bldg, int damage_abs) { assert(bldg); - bldg->size = MAX(1, bldg->size - damage_abs); + bldg->size -= damage_abs; + if (bldg->size < 1) bldg->size = 1; /* Wenn Burg, dann gucken, ob die Leute alle noch in das Geb�ude passen. */ @@ -3020,7 +3006,9 @@ static void print_stats(battle * b) for (s = b->sides; s != b->sides + b->nsides; ++s) { if (!selist_empty(s->leader.fighters)) { - b->max_tactics = MAX(b->max_tactics, s->leader.value); + if (s->leader.value > b->max_tactics) { + b->max_tactics = s->leader.value; + } } } @@ -3371,7 +3359,7 @@ fighter *make_fighter(battle * b, unit * u, side * s1, bool attack) else p_bonus += 3; } while (rnd >= 97); - bonus = MAX(p_bonus, bonus); + if (p_bonus > bonus) p_bonus = bonus; } tactics += bonus; } diff --git a/src/items/xerewards.c b/src/items/xerewards.c index d039a19a1..c49787ec7 100644 --- a/src/items/xerewards.c +++ b/src/items/xerewards.c @@ -75,8 +75,9 @@ use_manacrystal(struct unit *u, const struct item_type *itype, int amount, } msp = max_spellpoints_depr(u->region, u) / 2; + if (msp < 25) msp = 25; for (i = 0; i != amount; ++i) { - sp += MAX(25, msp); + sp += msp; change_spellpoints(u, sp); } diff --git a/src/kernel/group.c b/src/kernel/group.c index b01e10a9a..bfa654cdf 100755 --- a/src/kernel/group.c +++ b/src/kernel/group.c @@ -60,7 +60,7 @@ group *new_group(faction * f, const char *name, int gid) gp = &(*gp)->next; *gp = g; - maxgid = MAX(gid, maxgid); + if (gid > maxgid) maxgid = gid; g->name = str_strdup(name); g->gid = gid; diff --git a/src/kernel/order.c b/src/kernel/order.c index 44332d630..b352a1650 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -10,7 +10,10 @@ without prior permission by the authors of Eressea. */ +#ifdef _MSC_VER #include +#endif + #include #include "order.h" @@ -398,6 +401,7 @@ order *parse_order(const char *s, const struct locale * lang) } if (kwd != NOKEYWORD) { order *ord = (order *)malloc(sizeof(order)); + if (ord == NULL) abort(); create_order_i(ord, kwd, sptr, persistent, noerror, lang); return ord; } diff --git a/src/kernel/pool.c b/src/kernel/pool.c index 6805e1c0e..4ff2ce6f4 100644 --- a/src/kernel/pool.c +++ b/src/kernel/pool.c @@ -156,7 +156,7 @@ int set_resvalue(unit * u, const item_type * itype, int value) } int -get_pooled(const unit * u, const resource_type * rtype, unsigned int mode, +get_pooled(const unit * u, const resource_type * rtype, int mode, int count) { const faction *f = u->faction; @@ -199,7 +199,7 @@ int count) } int -use_pooled(unit * u, const resource_type * rtype, unsigned int mode, int count) +use_pooled(unit * u, const resource_type * rtype, int mode, int count) { const faction *f = u->faction; unit *v; diff --git a/src/kernel/pool.h b/src/kernel/pool.h index ecd1a5364..df7638689 100644 --- a/src/kernel/pool.h +++ b/src/kernel/pool.h @@ -42,9 +42,9 @@ extern "C" { #define GET_ALL (GET_SLACK|GET_RESERVE|GET_POOLED_SLACK|GET_POOLED_RESERVE|GET_POOLED_FORCE) int get_pooled(const struct unit *u, const struct resource_type *res, - unsigned int mode, int count); + int mode, int count); int use_pooled(struct unit *u, const struct resource_type *res, - unsigned int mode, int count); + int mode, int count); /** use_pooled * verbraucht 'count' Objekte der resource 'itm' * unter zuhilfenahme des Pools der struct region und Aufbrauch des diff --git a/src/kernel/save.c b/src/kernel/save.c index 4c8e02fe0..a914b1c32 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -448,7 +448,7 @@ unit *read_unit(gamedata *data) set_number(u, number); READ_INT(data->store, &n); - u->age = (short)n; + u->age = n; READ_TOK(data->store, rname, sizeof(rname)); rc = rc_find(rname); @@ -775,7 +775,8 @@ static region *readregion(gamedata *data, int x, int y) } if (data->version >= REGIONOWNER_VERSION) { READ_INT(data->store, &n); - region_set_morale(r, MAX(0, (short)n), -1); + if (n < 0) n = 0; + region_set_morale(r, n, -1); read_owner(data, &r->land->ownership); } } diff --git a/src/morale.c b/src/morale.c index 6155426ac..7d76db6b9 100644 --- a/src/morale.c +++ b/src/morale.c @@ -76,7 +76,8 @@ void morale_update(region *r) { void morale_change(region *r, int value) { int morale = region_get_morale(r); if (morale > 0) { - morale = MAX(0, morale - value); + morale = morale - value; + if (morale < 0) morale = 0; region_set_morale(r, morale, turn); } } diff --git a/src/platform.h b/src/platform.h index 7b52782e3..4305c379b 100644 --- a/src/platform.h +++ b/src/platform.h @@ -14,6 +14,3 @@ #pragma warning(disable: 4224) // formal parameter was previously defined as a type #pragma warning(disable: 4214) // bit field types other than int #endif - -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) diff --git a/src/races/dragons.c b/src/races/dragons.c index aa79a783a..ec16f78dc 100644 --- a/src/races/dragons.c +++ b/src/races/dragons.c @@ -22,7 +22,10 @@ /* util includes */ #include -#define age_chance(a,b,p) (MAX(0,a-b)*p) +static int age_chance(int a, int b, int p) { + int r = (a - b) * p; + return (r < 0) ? 0 : r; +} #define DRAGONAGE 27 #define WYRMAGE 68 diff --git a/src/races/zombies.c b/src/races/zombies.c index 3c880d819..652ccc1e2 100644 --- a/src/races/zombies.c +++ b/src/races/zombies.c @@ -31,7 +31,10 @@ #define UNDEAD_BREAKUP 25 /* chance dafuer */ #define UNDEAD_BREAKUP_FRACTION (25+rng_int()%70) /* anteil der weg geht */ -#define age_chance(a,b,p) (MAX(0,a-b)*p) +static int age_chance(int a, int b, int p) { + int r = (a - b) * p; + return (r < 0) ? 0 : r; +} void make_undead_unit(unit * u) { @@ -43,8 +46,9 @@ void make_undead_unit(unit * u) void age_skeleton(unit * u) { if (is_monsters(u->faction) && rng_int() % 100 < age_chance(u->age, 27, 1)) { - int n = MAX(1, u->number / 2); + int n = u->number / 2; double q = (double)u->hp / (double)(unit_max_hp(u) * u->number); + if (n < 1) n = 1; u_setrace(u, get_race(RC_SKELETON_LORD)); u->irace = NULL; scale_number(u, n); @@ -55,8 +59,9 @@ void age_skeleton(unit * u) void age_zombie(unit * u) { if (is_monsters(u->faction) && rng_int() % 100 < age_chance(u->age, 27, 1)) { - int n = MAX(1, u->number / 2); + int n = u->number / 2; double q = (double)u->hp / (double)(unit_max_hp(u) * u->number); + if (n < 1) n = 1; u_setrace(u, get_race(RC_ZOMBIE_LORD)); u->irace = NULL; scale_number(u, n); @@ -67,11 +72,11 @@ void age_zombie(unit * u) void age_ghoul(unit * u) { if (is_monsters(u->faction) && rng_int() % 100 < age_chance(u->age, 27, 1)) { - int n = MAX(1, u->number / 2); + int n = u->number / 2; double q = (double)u->hp / (double)(unit_max_hp(u) * u->number); u_setrace(u, get_race(RC_GHOUL_LORD)); u->irace = NULL; - scale_number(u, n); + scale_number(u, (n > 0) ? n : 1); u->hp = (int)(unit_max_hp(u) * u->number * q); } }