eliminate MAX/MIN macros completely

This commit is contained in:
Enno Rehling 2019-01-24 17:50:58 +01:00
parent 9b113c050e
commit 20e0e8400b
12 changed files with 82 additions and 82 deletions

View File

@ -27,7 +27,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "study.h" #include "study.h"
void academy_teaching_bonus(struct unit *u, skill_t sk, int students) { 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 */ /* actually students * EXPERIENCEDAYS / MAX_STUDENTS */
learn_skill(u, sk, students); learn_skill(u, sk, students);
} }

View File

@ -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. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/ **/
#ifdef _MSC_VER
#include <platform.h> #include <platform.h>
#endif
#include <kernel/config.h> #include <kernel/config.h>
#include "battle.h" #include "battle.h"
#include "alchemy.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. /* every entry in the size[] array means someone trying to defend us.
* 'retreat' is the number of rows falling. * '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; 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. */ * are taken into account, e.g. no horses, magic, etc. */
{ {
int skill; int skill;
const race * rc = u_race(u);
if (wtype == NULL) { if (wtype == NULL) {
skill = effskill(u, SK_WEAPONLESS, 0); skill = effskill(u, SK_WEAPONLESS, 0);
int def = attacking ? rc->at_default : rc->df_default;
if (skill <= 0) { if (skill <= 0) {
/* wenn kein waffenloser kampf, dann den rassen-defaultwert */ /* 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, 0); int sword = effskill(u, SK_MELEE, 0);
int spear = effskill(u, SK_SPEAR, 0); int spear = effskill(u, SK_SPEAR, 0);
skill = MAX(sword, spear) - 3; skill = ((sword > spear) ? 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;
} }
} }
if (def > skill) skill = def;
if (attacking) { if (attacking) {
skill += u_race(u)->at_bonus; skill += rc->at_bonus;
if (fval(u->region->terrain, SEA_REGION) && u->ship) if (fval(u->region->terrain, SEA_REGION) && u->ship)
skill += u->ship->type->at_bonus; skill += u->ship->type->at_bonus;
} }
else { else {
skill += u_race(u)->df_bonus; skill += rc->df_bonus;
if (fval(u->region->terrain, SEA_REGION) && u->ship) if (fval(u->region->terrain, SEA_REGION) && u->ship)
skill += u->ship->type->df_bonus; 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, 0); skill = effskill(u, wtype->skill, 0);
if (skill > 0) { if (skill > 0) {
if (attacking) { if (attacking) {
skill += u_race(u)->at_bonus; skill += rc->at_bonus;
} }
else { else {
skill += u_race(u)->df_bonus; skill += rc->df_bonus;
} }
} }
if (attacking) { 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, static int apply_magicshield(int reduced_damage, fighter *df,
const weapon_type *awtype, battle *b, bool magic) { const weapon_type *awtype, battle *b, bool magic) {
side *ds = df->side; side *ds = df->side;
selist *ql; selist *ql;
int qi; int qi;
if (reduced_damage <= 0) if (reduced_damage <= 0)
return 0; return 0;
/* Schilde */ /* Schilde */
for (qi = 0, ql = b->meffects; ql; selist_advance(&ql, &qi, 1)) { for (qi = 0, ql = b->meffects; ql; selist_advance(&ql, &qi, 1)) {
meffect *me = (meffect *) selist_get(ql, qi); meffect *me = (meffect *)selist_get(ql, qi);
if (meffect_protection(b, me, ds) != 0) { if (meffect_protection(b, me, ds) != 0) {
assert(0 <= reduced_damage); /* rda sollte hier immer mindestens 0 sein */ assert(0 <= reduced_damage); /* rda sollte hier immer mindestens 0 sein */
/* jeder Schaden wird um effect% reduziert bis der Schild duration /* jeder Schaden wird um effect% reduziert bis der Schild duration
* Trefferpunkte aufgefangen hat */ * Trefferpunkte aufgefangen hat */
if (me->typ == SHIELD_REDUCE) { if (me->typ == SHIELD_REDUCE) {
int hp = reduced_damage * (me->effect / 100); int hp = reduced_damage * (me->effect / 100);
reduced_damage -= hp; reduced_damage -= hp;
me->duration -= hp; me->duration -= hp;
} }
/* gibt R<>stung +effect f<>r duration Treffer */ /* gibt R<>stung +effect f<>r duration Treffer */
if (me->typ == SHIELD_ARMOR) { if (me->typ == SHIELD_ARMOR) {
reduced_damage = MAX(reduced_damage - me->effect, 0); reduced_damage -= me->effect;
me->duration--; if (reduced_damage < 0) reduced_damage = 0;
} me->duration--;
}
}
} }
}
return reduced_damage; return reduced_damage;
} }
@ -1347,11 +1329,13 @@ terminate(troop dt, troop at, int type, const char *damage_formula, bool missile
/* Skilldifferenzbonus */ /* Skilldifferenzbonus */
if (rule_damage & DAMAGE_SKILL_BONUS) { 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_race_resistance(reduced_damage, df, awtype, magic);
reduced_damage = apply_magicshield(reduced_damage, df, awtype, b, 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; minrow = FIGHT_ROW;
maxrow = BEHIND_ROW; maxrow = BEHIND_ROW;
} }
minrow = MAX(minrow, FIGHT_ROW);
if (minrow < FIGHT_ROW) minrow = FIGHT_ROW;
enemies = count_enemies(b, af, minrow, maxrow, select); 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); dt = select_enemy(at.fighter, FIGHT_ROW, BEHIND_ROW, SELECT_ADVANCE);
} }
else { else {
mindist = MAX(mindist, FIGHT_ROW); if (mindist < FIGHT_ROW) mindist = FIGHT_ROW;
dt = select_enemy(at.fighter, mindist, maxdist, SELECT_ADVANCE); 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) void damage_building(battle * b, building * bldg, int damage_abs)
{ {
assert(bldg); 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<65>ude passen. */ /* Wenn Burg, dann gucken, ob die Leute alle noch in das Geb<65>ude passen. */
@ -3020,7 +3006,9 @@ static void print_stats(battle * b)
for (s = b->sides; s != b->sides + b->nsides; ++s) { for (s = b->sides; s != b->sides + b->nsides; ++s) {
if (!selist_empty(s->leader.fighters)) { 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 else
p_bonus += 3; p_bonus += 3;
} while (rnd >= 97); } while (rnd >= 97);
bonus = MAX(p_bonus, bonus); if (p_bonus > bonus) p_bonus = bonus;
} }
tactics += bonus; tactics += bonus;
} }

View File

@ -75,8 +75,9 @@ use_manacrystal(struct unit *u, const struct item_type *itype, int amount,
} }
msp = max_spellpoints_depr(u->region, u) / 2; msp = max_spellpoints_depr(u->region, u) / 2;
if (msp < 25) msp = 25;
for (i = 0; i != amount; ++i) { for (i = 0; i != amount; ++i) {
sp += MAX(25, msp); sp += msp;
change_spellpoints(u, sp); change_spellpoints(u, sp);
} }

View File

@ -60,7 +60,7 @@ group *new_group(faction * f, const char *name, int gid)
gp = &(*gp)->next; gp = &(*gp)->next;
*gp = g; *gp = g;
maxgid = MAX(gid, maxgid); if (gid > maxgid) maxgid = gid;
g->name = str_strdup(name); g->name = str_strdup(name);
g->gid = gid; g->gid = gid;

View File

@ -10,7 +10,10 @@
without prior permission by the authors of Eressea. without prior permission by the authors of Eressea.
*/ */
#ifdef _MSC_VER
#include <platform.h> #include <platform.h>
#endif
#include <kernel/config.h> #include <kernel/config.h>
#include "order.h" #include "order.h"
@ -398,6 +401,7 @@ order *parse_order(const char *s, const struct locale * lang)
} }
if (kwd != NOKEYWORD) { if (kwd != NOKEYWORD) {
order *ord = (order *)malloc(sizeof(order)); order *ord = (order *)malloc(sizeof(order));
if (ord == NULL) abort();
create_order_i(ord, kwd, sptr, persistent, noerror, lang); create_order_i(ord, kwd, sptr, persistent, noerror, lang);
return ord; return ord;
} }

View File

@ -156,7 +156,7 @@ int set_resvalue(unit * u, const item_type * itype, int value)
} }
int 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) int count)
{ {
const faction *f = u->faction; const faction *f = u->faction;
@ -199,7 +199,7 @@ int count)
} }
int 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; const faction *f = u->faction;
unit *v; unit *v;

View File

@ -42,9 +42,9 @@ extern "C" {
#define GET_ALL (GET_SLACK|GET_RESERVE|GET_POOLED_SLACK|GET_POOLED_RESERVE|GET_POOLED_FORCE) #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, 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, int use_pooled(struct unit *u, const struct resource_type *res,
unsigned int mode, int count); int mode, int count);
/** use_pooled /** use_pooled
* verbraucht 'count' Objekte der resource 'itm' * verbraucht 'count' Objekte der resource 'itm'
* unter zuhilfenahme des Pools der struct region und Aufbrauch des * unter zuhilfenahme des Pools der struct region und Aufbrauch des

View File

@ -448,7 +448,7 @@ unit *read_unit(gamedata *data)
set_number(u, number); set_number(u, number);
READ_INT(data->store, &n); READ_INT(data->store, &n);
u->age = (short)n; u->age = n;
READ_TOK(data->store, rname, sizeof(rname)); READ_TOK(data->store, rname, sizeof(rname));
rc = rc_find(rname); rc = rc_find(rname);
@ -775,7 +775,8 @@ static region *readregion(gamedata *data, int x, int y)
} }
if (data->version >= REGIONOWNER_VERSION) { if (data->version >= REGIONOWNER_VERSION) {
READ_INT(data->store, &n); 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); read_owner(data, &r->land->ownership);
} }
} }

View File

@ -76,7 +76,8 @@ void morale_update(region *r) {
void morale_change(region *r, int value) { void morale_change(region *r, int value) {
int morale = region_get_morale(r); int morale = region_get_morale(r);
if (morale > 0) { if (morale > 0) {
morale = MAX(0, morale - value); morale = morale - value;
if (morale < 0) morale = 0;
region_set_morale(r, morale, turn); region_set_morale(r, morale, turn);
} }
} }

View File

@ -14,6 +14,3 @@
#pragma warning(disable: 4224) // formal parameter was previously defined as a type #pragma warning(disable: 4224) // formal parameter was previously defined as a type
#pragma warning(disable: 4214) // bit field types other than int #pragma warning(disable: 4214) // bit field types other than int
#endif #endif
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

View File

@ -22,7 +22,10 @@
/* util includes */ /* util includes */
#include <util/rng.h> #include <util/rng.h>
#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 DRAGONAGE 27
#define WYRMAGE 68 #define WYRMAGE 68

View File

@ -31,7 +31,10 @@
#define UNDEAD_BREAKUP 25 /* chance dafuer */ #define UNDEAD_BREAKUP 25 /* chance dafuer */
#define UNDEAD_BREAKUP_FRACTION (25+rng_int()%70) /* anteil der weg geht */ #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) void make_undead_unit(unit * u)
{ {
@ -43,8 +46,9 @@ void make_undead_unit(unit * u)
void age_skeleton(unit * u) void age_skeleton(unit * u)
{ {
if (is_monsters(u->faction) && rng_int() % 100 < age_chance(u->age, 27, 1)) { 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); 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_setrace(u, get_race(RC_SKELETON_LORD));
u->irace = NULL; u->irace = NULL;
scale_number(u, n); scale_number(u, n);
@ -55,8 +59,9 @@ void age_skeleton(unit * u)
void age_zombie(unit * u) void age_zombie(unit * u)
{ {
if (is_monsters(u->faction) && rng_int() % 100 < age_chance(u->age, 27, 1)) { 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); 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_setrace(u, get_race(RC_ZOMBIE_LORD));
u->irace = NULL; u->irace = NULL;
scale_number(u, n); scale_number(u, n);
@ -67,11 +72,11 @@ void age_zombie(unit * u)
void age_ghoul(unit * u) void age_ghoul(unit * u)
{ {
if (is_monsters(u->faction) && rng_int() % 100 < age_chance(u->age, 27, 1)) { 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); double q = (double)u->hp / (double)(unit_max_hp(u) * u->number);
u_setrace(u, get_race(RC_GHOUL_LORD)); u_setrace(u, get_race(RC_GHOUL_LORD));
u->irace = NULL; u->irace = NULL;
scale_number(u, n); scale_number(u, (n > 0) ? n : 1);
u->hp = (int)(unit_max_hp(u) * u->number * q); u->hp = (int)(unit_max_hp(u) * u->number * q);
} }
} }