fix curse-vigour calculation math.

also fix a ton of small other niggles from pvs-studio.
This commit is contained in:
Enno Rehling 2016-11-20 19:08:39 +01:00
parent a8f3cad143
commit ec4060cb9a
22 changed files with 139 additions and 145 deletions

View File

@ -765,7 +765,7 @@ bool missile)
skill += CavalryBonus(tu, enemy, BONUS_SKILL);
if (wtype)
skill =
skillmod(urace(tu)->attribs, tu, tu->region, wtype->skill, skill,
skillmod(u_race(tu)->attribs, tu, tu->region, wtype->skill, skill,
SMF_RIDING);
}

View File

@ -97,7 +97,7 @@ static request *nextentertainer;
static int entertaining;
static unsigned int norders;
static request *oa;
static request *g_requests;
#define RECRUIT_MERGE 1
static int rules_recruit = -1;
@ -154,21 +154,21 @@ static void expandorders(region * r, request * requests)
if (norders > 0) {
int i = 0;
oa = (request *)calloc(norders, sizeof(request));
g_requests = (request *)calloc(norders, sizeof(request));
for (o = requests; o; o = o->next) {
if (o->qty > 0) {
unsigned int j;
for (j = o->qty; j; j--) {
oa[i] = *o;
oa[i].unit->n = 0;
g_requests[i] = *o;
g_requests[i].unit->n = 0;
i++;
}
}
}
scramble(oa, norders, sizeof(request));
scramble(g_requests, norders, sizeof(request));
}
else {
oa = NULL;
g_requests = NULL;
}
while (requests) {
request *o = requests->next;
@ -1458,16 +1458,16 @@ static void expandbuying(region * r, request * buyorders)
for (j = 0; j != norders; j++) {
int price, multi;
ltype = oa[j].type.ltype;
ltype = g_requests[j].type.ltype;
trade = trades;
while (trade->type != ltype)
++trade;
multi = trade->multi;
price = ltype->price * multi;
if (get_pooled(oa[j].unit, rsilver, GET_DEFAULT,
if (get_pooled(g_requests[j].unit, rsilver, GET_DEFAULT,
price) >= price) {
unit *u = oa[j].unit;
unit *u = g_requests[j].unit;
item *items;
/* litems zählt die Güter, die verkauft wurden, u->n das Geld, das
@ -1481,7 +1481,7 @@ static void expandbuying(region * r, request * buyorders)
items = a->data.v;
i_change(&items, ltype->itype, 1);
a->data.v = items;
i_change(&oa[j].unit->items, ltype->itype, 1);
i_change(&g_requests[j].unit->items, ltype->itype, 1);
use_pooled(u, rsilver, GET_DEFAULT, price);
if (u->n < 0)
u->n = 0;
@ -1499,7 +1499,7 @@ static void expandbuying(region * r, request * buyorders)
fset(u, UFL_LONGACTION | UFL_NOTMOVING);
}
}
free(oa);
free(g_requests);
/* Ausgabe an Einheiten */
@ -1736,7 +1736,7 @@ static void expandselling(region * r, request * sellorders, int limit)
for (j = 0; j != norders; j++) {
const luxury_type *search = NULL;
const luxury_type *ltype = oa[j].type.ltype;
const luxury_type *ltype = g_requests[j].type.ltype;
int multi = r_demand(r, ltype);
int i;
int use = 0;
@ -1753,7 +1753,7 @@ static void expandselling(region * r, request * sellorders, int limit)
if (money >= price) {
int abgezogenhafen = 0;
int abgezogensteuer = 0;
unit *u = oa[j].unit;
unit *u = g_requests[j].unit;
item *itm;
attrib *a = a_find(u->attribs, &at_luxuries);
if (a == NULL)
@ -1801,10 +1801,10 @@ static void expandselling(region * r, request * sellorders, int limit)
}
}
if (use > 0) {
use_pooled(oa[j].unit, ltype->itype->rtype, GET_DEFAULT, use);
use_pooled(g_requests[j].unit, ltype->itype->rtype, GET_DEFAULT, use);
}
}
free(oa);
free(g_requests);
/* Steuern. Hier werden die Steuern dem Besitzer der größten Burg gegeben. */
if (maxowner) {
@ -1928,7 +1928,7 @@ static bool sell(unit * u, request ** sellorders, struct order *ord)
s = gettoken(token, sizeof(token));
itype = s ? finditemtype(s, u->faction->locale) : 0;
ltype = itype ? resource2luxury(itype->rtype) : 0;
if (ltype == NULL) {
if (ltype == NULL || itype == NULL) {
cmistake(u, ord, 126, MSG_COMMERCE);
return false;
}
@ -2010,8 +2010,8 @@ static void expandstealing(region * r, request * stealorders)
* u ist die beklaute unit. oa.unit ist die klauende unit.
*/
for (j = 0; j != norders && oa[j].unit->n <= oa[j].unit->wants; j++) {
unit *u = findunitg(oa[j].no, r);
for (j = 0; j != norders && g_requests[j].unit->n <= g_requests[j].unit->wants; j++) {
unit *u = findunitg(g_requests[j].no, r);
int n = 0;
if (u && u->region == r) {
n = get_pooled(u, rsilver, GET_ALL, INT_MAX);
@ -2035,17 +2035,17 @@ static void expandstealing(region * r, request * stealorders)
n = 10;
}
if (n > 0) {
n = _min(n, oa[j].unit->wants);
n = _min(n, g_requests[j].unit->wants);
use_pooled(u, rsilver, GET_ALL, n);
oa[j].unit->n = n;
change_money(oa[j].unit, n);
g_requests[j].unit->n = n;
change_money(g_requests[j].unit, n);
ADDMSG(&u->faction->msgs, msg_message("stealeffect", "unit region amount",
u, u->region, n));
}
add_income(oa[j].unit, IC_STEAL, oa[j].unit->wants, oa[j].unit->n);
fset(oa[j].unit, UFL_LONGACTION | UFL_NOTMOVING);
add_income(g_requests[j].unit, IC_STEAL, g_requests[j].unit->wants, g_requests[j].unit->n);
fset(g_requests[j].unit, UFL_LONGACTION | UFL_NOTMOVING);
}
free(oa);
free(g_requests);
}
/* ------------------------------------------------------------- */
@ -2060,14 +2060,14 @@ static void plant(unit * u, int raw)
if (!fval(r->terrain, LAND_REGION)) {
return;
}
if (rherbtype(r) == NULL) {
itype = rherbtype(r);
if (itype == NULL) {
cmistake(u, u->thisorder, 108, MSG_PRODUCE);
return;
}
/* Skill prüfen */
skill = effskill(u, SK_HERBALISM, 0);
itype = rherbtype(r);
if (skill < 6) {
ADDMSG(&u->faction->msgs,
msg_feedback(u, u->thisorder, "plant_skills",
@ -2702,13 +2702,13 @@ static void expandloot(region * r, request * lootorders)
return;
for (i = 0; i != norders && startmoney > looted + TAXFRACTION * 2; i++) {
change_money(oa[i].unit, TAXFRACTION);
oa[i].unit->n += TAXFRACTION;
change_money(g_requests[i].unit, TAXFRACTION);
g_requests[i].unit->n += TAXFRACTION;
/*Looting destroys double the money*/
looted += TAXFRACTION * 2;
}
rsetmoney(r, startmoney - looted);
free(oa);
free(g_requests);
/* Lowering morale by 1 depending on the looted money (+20%) */
if (rng_int() % 100 < 20 + (looted * 80) / startmoney) {
@ -2737,11 +2737,11 @@ void expandtax(region * r, request * taxorders)
return;
for (i = 0; i != norders && rmoney(r) > TAXFRACTION; i++) {
change_money(oa[i].unit, TAXFRACTION);
oa[i].unit->n += TAXFRACTION;
change_money(g_requests[i].unit, TAXFRACTION);
g_requests[i].unit->n += TAXFRACTION;
rsetmoney(r, rmoney(r) - TAXFRACTION);
}
free(oa);
free(g_requests);
for (u = r->units; u; u = u->next) {
if (u->n >= 0) {
@ -2939,9 +2939,10 @@ static void peasant_taxes(region * r)
maxsize = buildingeffsize(b, false);
if (maxsize > 0) {
double taxfactor = money * b->type->taxes(b, maxsize);
double morale = money * region_get_morale(r) * MORALE_TAX_FACTOR;
if (taxfactor > morale)
double morale = MORALE_TAX_FACTOR * money * region_get_morale(r);
if (taxfactor > morale) {
taxfactor = morale;
}
if (taxfactor > 0) {
int taxmoney = (int)taxfactor;
change_money(u, taxmoney);

View File

@ -137,7 +137,7 @@ int give_quota(const unit * src, const unit * dst, const item_type * type,
}
if (dst && src && src->faction != dst->faction) {
divisor = config_get_flt("rules.items.give_divisor", 1);
assert(divisor == 0 || divisor >= 1);
assert(divisor <= 0 || divisor >= 1);
if (divisor >= 1) {
/* predictable > correct: */
int x = (int)(n / divisor);
@ -781,7 +781,7 @@ void give_cmd(unit * u, order * ord)
}
if (u2 != NULL) {
if ((u_race(u)->ec_flags & ECF_KEEP_ITEM) && u2 != NULL) {
if ((u_race(u)->ec_flags & ECF_KEEP_ITEM)) {
ADDMSG(&u->faction->msgs,
msg_feedback(u, ord, "race_nogive", "race", u_race(u)));
return;

View File

@ -114,10 +114,9 @@ struct order *ord)
const resource_type *rt_crystal = NULL;
int i;
if (rt_crystal == NULL) {
rt_crystal = rt_find("antimagic");
assert(rt_crystal != NULL);
}
for (i = 0; i != amount; ++i) {
int effect, duration = 2;
double force;

View File

@ -405,8 +405,8 @@ static void test_buildingtype_exists(CuTest * tc)
r = findregion(-1, 0);
b = new_building(btype, r, default_locale);
b->size = 10;
CuAssertPtrNotNull(tc, b);
b->size = 10;
CuAssertTrue(tc, !buildingtype_exists(r, NULL, false));
CuAssertTrue(tc, !buildingtype_exists(r, btype2, false));

View File

@ -741,10 +741,15 @@ int cmp_current_owner(const building * b, const building * a)
int oldsize = buildingeffsize(a, false);
double oldtaxes = a->type->taxes(a, oldsize);
if (newtaxes != oldtaxes)
return (newtaxes > oldtaxes) ? 1 : -1;
if (newsize != oldsize)
if (newtaxes > oldtaxes) {
return 1;
}
if (newtaxes < oldtaxes) {
return -1;
}
if (newsize != oldsize) {
return newsize - oldsize;
}
return (b->size - a->size);
}
else {
@ -944,7 +949,7 @@ int entertainmoney(const region * r)
return 0;
}
n = rmoney(r) / ENTERTAINFRACTION;
n = rmoney(r) / (double)ENTERTAINFRACTION;
if (is_cursed(r->attribs, C_GENEROUS, 0)) {
n *= get_curseeffect(r->attribs, C_GENEROUS, 0);

View File

@ -825,7 +825,7 @@ double destr_curse(curse * c, int cast_level, double force)
c->type->change_vigour(c, -(cast_level + 1) / 2);
}
else {
c->vigour -= cast_level + 1 / 2;
c->vigour -= (cast_level + 1) / 2.0;
}
}
}

View File

@ -827,7 +827,7 @@ int writepasswd(void)
log_info("writing passwords...");
for (f = factions; f; f = f->next) {
fprintf(F, "%s:%s:%s:%u\n",
fprintf(F, "%s:%s:%s:%d\n",
itoa36(f->no), f->email, f->_password, f->subscription);
}
fclose(F);

View File

@ -973,7 +973,7 @@ void init_resources(void)
{
resource_type *rtype;
rtype = rt_get_or_create(resourcenames[R_PERSON]); // lousy hack
rt_get_or_create(resourcenames[R_PERSON]); // lousy hack
rtype = rt_get_or_create(resourcenames[R_PEASANT]);
rtype->uchange = res_changepeasants;
@ -1204,7 +1204,7 @@ void read_items(struct storage *store, item ** ilist)
itype = it_find(ibuf);
READ_INT(store, &i);
if (i <= 0) {
log_error("data contains an entry with %d %s", i, resourcename(itype->rtype, NMF_PLURAL));
log_error("data contains an entry with %d %s", i, ibuf);
}
else {
if (itype && itype->rtype) {

View File

@ -179,7 +179,7 @@ int count)
if (u != v && (v->items || rtype->uget)) {
int mask;
if ((urace(v)->ec_flags & ECF_KEEP_ITEM))
if ((u_race(v)->ec_flags & ECF_KEEP_ITEM))
continue;
if (v->faction == f) {
@ -232,7 +232,7 @@ use_pooled(unit * u, const resource_type * rtype, unsigned int mode, int count)
for (v = r->units; use > 0 && v != NULL; v = v->next) {
if (u != v) {
int mask;
if ((urace(v)->ec_flags & ECF_KEEP_ITEM))
if ((u_race(v)->ec_flags & ECF_KEEP_ITEM))
continue;
if (v->items == NULL && rtype->uget == NULL)
continue;

View File

@ -46,7 +46,7 @@ extern int dice_rand(const char *s);
static void update_resource(struct rawmaterial *res, double modifier)
{
double amount = 1 + (res->level - res->startlevel) * res->divisor / 100.0;
double amount = (res->level - res->startlevel) / 100.0 * res->divisor + 1;
amount = ResourceFactor() * res->base * amount * modifier;
if (amount < 1.0)
res->amount = 1;

View File

@ -917,11 +917,6 @@ bool leave(unit * u, bool force)
return true;
}
const struct race *urace(const struct unit *u)
{
return u->_race;
}
bool can_survive(const unit * u, const region * r)
{
if ((fval(r->terrain, WALK_INTO) && (u_race(u)->flags & RCF_WALK))

View File

@ -141,8 +141,6 @@ extern "C" {
struct unit *utarget(const struct unit *u);
void usettarget(struct unit *u, const struct unit *b);
extern const struct race *urace(const struct unit *u);
const char *uprivate(const struct unit *u);
void usetprivate(struct unit *u, const char *c);

View File

@ -648,7 +648,7 @@ growing_trees(region * r, const int current_season, const int last_weeks_season)
* verfügbaren Fläche ab. In Gletschern gibt es weniger
* Möglichkeiten als in Ebenen. */
sprout = 0;
seedchance = (1000 * region_maxworkers(r2)) / r2->terrain->size;
seedchance = (1000 * region_maxworkers(r2)) / (double)r2->terrain->size;
for (i = 0; i < seeds / MAXDIRECTIONS; i++) {
if (rng_int() % 10000 < seedchance)
sprout++;
@ -3636,7 +3636,7 @@ int pay_cmd(unit * u, struct order *ord)
static int reserve_i(unit * u, struct order *ord, int flags)
{
char token[128];
if (u->number > 0 && (urace(u)->ec_flags & GETITEM)) {
if (u->number > 0 && (u_race(u)->ec_flags & GETITEM)) {
int use, count, para;
const item_type *itype;
const char *s;
@ -3945,7 +3945,7 @@ int armedmen(const unit * u, bool siege_weapons)
{
item *itm;
int n = 0;
if (!(urace(u)->flags & RCF_NOWEAPONS)) {
if (!(u_race(u)->flags & RCF_NOWEAPONS)) {
if (effskill(u, SK_WEAPONLESS, 0) >= 1) {
/* kann ohne waffen bewachen: fuer drachen */
n = u->number;

View File

@ -76,17 +76,13 @@ bool monster_is_waiting(const unit * u)
static void eaten_by_monster(unit * u)
{
/* adjustment for smaller worlds */
static double multi = 0.0;
double multi = RESOURCE_QUANTITY * newterrain(T_PLAIN)->size / 10000.0;
int n = 0;
int horse = -1;
const resource_type *rhorse = get_resourcetype(R_HORSE);
const race *rc = u_race(u);
attrib *a;
if (multi == 0.0) {
multi = RESOURCE_QUANTITY * newterrain(T_PLAIN)->size / 10000.0;
}
a = a_find(rc->attribs, &at_scare);
if (a) {
n = rng_int() & a->data.i * u->number;

View File

@ -695,7 +695,7 @@ static order *plan_dragon(unit * u)
int attempts = 0;
skill_t sk = SK_PERCEPTION;
/* study perception (or a random useful skill) */
while ((!skill_enabled(sk) || (attempts < MAXSKILLS && u_race(u)->bonus[sk] < (++attempts < 10?1:-5 )))) {
while (!skill_enabled(sk) || (attempts < MAXSKILLS && u_race(u)->bonus[sk] < (++attempts < 10?1:-5 ))) {
sk = (skill_t)(rng_int() % MAXSKILLS);
}
long_order = create_order(K_STUDY, u->faction->locale, "'%s'",

View File

@ -1730,7 +1730,7 @@ static void rpline(struct stream *out)
memset(line, '-', sizeof(line));
line[REPORTWIDTH] = '\n';
}
swrite(line, sizeof(char), sizeof(line), out);
swrite(line, sizeof(line), 1, out);
}
static void list_address(struct stream *out, const faction * uf, quicklist * seenfactions)

View File

@ -220,7 +220,7 @@ void report_summary(summary * s, summary * o, bool full)
fprintf(F, " Helden: %s\n", pcomp(s->heroes, o->heroes));
if (full) {
fprintf(F, "Regionen: %d\n", listlen(regions));
fprintf(F, "Regionen: %d\n", (int)listlen(regions));
fprintf(F, "Bewohnte Regionen: %d\n", s->inhabitedregions);
fprintf(F, "Landregionen: %d\n", s->landregionen);
fprintf(F, "Spielerregionen: %d\n", s->regionen_mit_spielern);

View File

@ -29,7 +29,7 @@ static void test_unicode_trim(CuTest * tc)
CuAssertIntEquals(tc, 3, unicode_utf8_trim(buffer));
CuAssertStrEquals(tc, "Hello Word", buffer);
buffer[9] = (char)0xc3;
buffer[9] = -61;
CuAssertIntEquals(tc, 1, unicode_utf8_trim(buffer));
CuAssertStrEquals(tc, "Hello Wor?", buffer);
}