diff --git a/src/bindings.c b/src/bindings.c index 35657b5be..2364775fb 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -458,8 +458,7 @@ static int tolua_write_report(lua_State * L) { faction *f = (faction *)tolua_tousertype(L, 1, 0); if (f) { - time_t ltime = time(0); - int result = write_reports(f, ltime); + int result = write_reports(f); lua_pushinteger(L, result); } else { diff --git a/src/economy.h b/src/economy.h index 6d58a1f9f..8d022ebbc 100644 --- a/src/economy.h +++ b/src/economy.h @@ -18,6 +18,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #ifndef H_GC_ECONOMY #define H_GC_ECONOMY + +#include + #ifdef __cplusplus extern "C" { #endif diff --git a/src/kernel/pool.c b/src/kernel/pool.c index e58bc131e..52e445c94 100644 --- a/src/kernel/pool.c +++ b/src/kernel/pool.c @@ -16,11 +16,14 @@ 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 -#include +#endif + #include "pool.h" #include "ally.h" +#include "config.h" #include "faction.h" #include "item.h" #include "order.h" @@ -168,7 +171,8 @@ int count) use = have; else if (rtype->itype && mode & (GET_SLACK | GET_RESERVE)) { int reserve = get_reservation(u, rtype->itype); - int slack = MAX(0, have - reserve); + int slack = have - reserve; + if (slack < 0) slack = 0; if (mode & GET_RESERVE) use = have - slack; else if (mode & GET_SLACK) @@ -206,18 +210,20 @@ use_pooled(unit * u, const resource_type * rtype, unsigned int mode, int count) } if ((mode & GET_SLACK) && (mode & GET_RESERVE)) { - n = MIN(use, have); + n = (use < have) ? use : have; } else if (rtype->itype) { int reserve = get_reservation(u, rtype->itype); - int slack = MAX(0, have - reserve); + int slack = have - reserve; + if (slack < 0) slack = 0; if (mode & GET_RESERVE) { n = have - slack; - n = MIN(use, n); + if (n > use) n = use; change_reservation(u, rtype->itype, -n); } else if (mode & GET_SLACK) { - n = MIN(use, slack); + n = slack; + if (n > use) n = use; } } if (n > 0) { diff --git a/src/monsters.c b/src/monsters.c index 99f32cedb..59664480a 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -17,7 +17,9 @@ * permission from the authors. */ +#ifdef _MSC_VER #include +#endif #include "monsters.h" @@ -132,7 +134,10 @@ static void reduce_weight(unit * u) if (itype->weight >= 10 && itype->rtype->wtype == 0 && itype->rtype->atype == 0) { if (itype->capacity < itype->weight) { - int reduce = MIN(itm->number, -((capacity - weight) / itype->weight)); + int reduce = (weight - capacity) / itype->weight; + if (reduce > itm->number) { + reduce = itm->number; + } give_peasants(u, itm->type, reduce); weight -= reduce * itype->weight; } @@ -147,7 +152,10 @@ static void reduce_weight(unit * u) const item_type *itype = itm->type; weight += itm->number * itype->weight; if (itype->capacity < itype->weight) { - int reduce = MIN(itm->number, -((capacity - weight) / itype->weight)); + int reduce = (weight - capacity) / itype->weight; + if (reduce > itm->number) { + reduce = itm->number; + } give_peasants(u, itm->type, reduce); weight -= reduce * itype->weight; } @@ -1027,10 +1035,12 @@ static void eaten_by_monster(unit * u) n = (int)(n * multi); if (n > 0) { + n = lovar(n); - n = MIN(rpeasants(u->region), n); if (n > 0) { + int p = rpeasants(u->region); + if (p < n) n = p; deathcounts(u->region, n); rsetpeasants(u->region, rpeasants(u->region) - n); ADDMSG(&u->region->msgs, msg_message("eatpeasants", "unit amount", u, n)); @@ -1048,8 +1058,9 @@ static void absorbed_by_monster(unit * u) if (n > 0) { n = lovar(n); - n = MIN(rpeasants(u->region), n); if (n > 0) { + int p = rpeasants(u->region); + if (p < n) n = p; rsetpeasants(u->region, rpeasants(u->region) - n); scale_number(u, u->number + n); ADDMSG(&u->region->msgs, msg_message("absorbpeasants", @@ -1063,7 +1074,10 @@ static int scareaway(region * r, int anzahl) int n, p, diff = 0, emigrants[MAXDIRECTIONS]; direction_t d; - anzahl = MIN(MAX(1, anzahl), rpeasants(r)); + p = rpeasants(r); + if (anzahl < 1) anzahl = 1; + if (anzahl > p) anzahl = p; + assert(p >= 0 && anzahl >= 0); /* Wandern am Ende der Woche (normal) oder wegen Monster. Die * Wanderung wird erst am Ende von demographics () ausgefuehrt. @@ -1073,9 +1087,7 @@ static int scareaway(region * r, int anzahl) for (d = 0; d != MAXDIRECTIONS; d++) emigrants[d] = 0; - p = rpeasants(r); - assert(p >= 0 && anzahl >= 0); - for (n = MIN(p, anzahl); n; n--) { + for (n = anzahl; n; n--) { direction_t dir = (direction_t)(rng_int() % MAXDIRECTIONS); region *rc = rconnect(r, dir); @@ -1104,8 +1116,9 @@ static void scared_by_monster(unit * u) } if (n > 0) { n = lovar(n); - n = MIN(rpeasants(u->region), n); if (n > 0) { + int p = rpeasants(u->region); + if (p < n) n = p; n = scareaway(u->region, n); if (n > 0) { ADDMSG(&u->region->msgs, msg_message("fleescared", diff --git a/src/randenc.c b/src/randenc.c index 499591294..7a65ce378 100644 --- a/src/randenc.c +++ b/src/randenc.c @@ -16,49 +16,52 @@ 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 -#include +#endif + #include "randenc.h" -#include "volcano.h" +#include "chaos.h" #include "economy.h" #include "monsters.h" #include "move.h" -#include "chaos.h" #include "study.h" +#include "volcano.h" -#include -#include +#include "spells/unitcurse.h" +#include "spells/regioncurse.h" /* attributes includes */ -#include -#include +#include "attributes/racename.h" +#include "attributes/reduceproduction.h" /* kernel includes */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "kernel/building.h" +#include "kernel/config.h" +#include "kernel/curse.h" +#include "kernel/equipment.h" +#include "kernel/faction.h" +#include "kernel/item.h" +#include "kernel/messages.h" +#include "kernel/order.h" +#include "kernel/plane.h" +#include "kernel/pool.h" +#include "kernel/race.h" +#include "kernel/region.h" +#include "kernel/ship.h" +#include "kernel/terrain.h" +#include "kernel/terrainid.h" +#include "kernel/unit.h" /* util includes */ -#include -#include -#include -#include -#include -#include -#include +#include "util/attrib.h" +#include "util/language.h" +#include "util/lists.h" +#include "util/log.h" +#include "util/rand.h" +#include "util/message.h" +#include "util/rng.h" /* libc includes */ #include @@ -785,9 +788,8 @@ static void rotting_herbs(void) int n = itm->number; double k = n * rot_chance / 100.0; if (fval(itm->type, ITF_HERB)) { - double nv = normalvariate(k, k / 4); - int inv = (int)nv; - int delta = MIN(n, inv); + int delta = (int)normalvariate(k, k / 4); + if (n < delta) delta = n; if (!i_change(itmp, itm->type, -delta)) { continue; } @@ -823,7 +825,9 @@ void randomevents(void) while (*blist) { building *b = *blist; if (fval(b->type, BTF_DECAY) && !building_owner(b)) { - b->size -= MAX(1, (b->size * 20) / 100); + int delta = (b->size * 20) / 100; + if (delta < 1) delta = 1; + b->size -= delta; if (b->size == 0) { remove_building(blist, r->buildings); } diff --git a/src/reports.c b/src/reports.c index 69e7a3555..862dd165a 100644 --- a/src/reports.c +++ b/src/reports.c @@ -16,12 +16,12 @@ 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 -#include +#endif #include "reports.h" #include "battle.h" -#include "kernel/calendar.h" #include "guard.h" #include "laws.h" #include "spells.h" @@ -30,48 +30,50 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "donations.h" /* attributes includes */ -#include -#include -#include -#include -#include +#include "attributes/attributes.h" +#include "attributes/follow.h" +#include "attributes/otherfaction.h" +#include "attributes/racename.h" +#include "attributes/stealth.h" -#include +#include "spells/unitcurse.h" /* kernel includes */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "kernel/config.h" +#include "kernel/calendar.h" +#include "kernel/ally.h" +#include "kernel/alliance.h" +#include "kernel/connection.h" +#include "kernel/building.h" +#include "kernel/curse.h" +#include "kernel/faction.h" +#include "kernel/group.h" +#include "kernel/item.h" +#include "kernel/messages.h" +#include "kernel/order.h" +#include "kernel/plane.h" +#include "kernel/race.h" +#include "kernel/region.h" +#include "kernel/resources.h" +#include "kernel/ship.h" +#include "kernel/spell.h" +#include "kernel/spellbook.h" +#include "kernel/terrain.h" +#include "kernel/unit.h" /* util includes */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "util/attrib.h" +#include "util/base36.h" +#include "util/bsdstring.h" +#include "util/functions.h" +#include "util/goodies.h" +#include "util/language.h" +#include "util/lists.h" +#include "util/log.h" +#include "util/macros.h" +#include "util/path.h" +#include "util/strings.h" +#include "util/translation.h" #include #include @@ -610,6 +612,70 @@ report_resources(const region * r, resource_report * result, int size, return n; } +static size_t spskill(char *buffer, size_t size, const struct locale * lang, + const struct unit * u, struct skill * sv, int *dh) +{ + char *bufp = buffer; + int effsk; + + if (!u->number) + return 0; + if (sv->level <= 0) { + if (sv->old <= 0 || (u->faction->options & WANT_OPTION(O_SHOWSKCHANGE)) == 0) { + return 0; + } + } + + bufp = STRLCPY(bufp, ", ", size); + + if (!*dh) { + bufp = STRLCPY(bufp, LOC(lang, "nr_skills"), size); + bufp = STRLCPY(bufp, ": ", size); + *dh = 1; + } + bufp = STRLCPY(bufp, skillname(sv->id, lang), size); + bufp = STRLCPY(bufp, " ", size); + + if (sv->id == SK_MAGIC) { + sc_mage *mage = get_mage(u); + if (mage && mage->magietyp != M_GRAY) { + bufp = STRLCPY(bufp, LOC(lang, mkname("school", + magic_school[mage->magietyp])), size); + bufp = STRLCPY(bufp, " ", size); + } + } + + if (sv->id == SK_STEALTH && fval(u, UFL_STEALTH)) { + int i = u_geteffstealth(u); + if (i >= 0) { + if (wrptr(&bufp, &size, snprintf(bufp, size, "%d/", i)) != 0) + WARN_STATIC_BUFFER(); + } + } + + effsk = eff_skill(u, sv, 0); + if (wrptr(&bufp, &size, snprintf(bufp, size, "%d", effsk)) != 0) + WARN_STATIC_BUFFER(); + + if (u->faction->options & WANT_OPTION(O_SHOWSKCHANGE)) { + int oldeff = 0; + int diff; + + if (sv->old > 0) { + oldeff = sv->old + get_modifier(u, sv->id, sv->old, u->region, false); + if (oldeff < 0) oldeff = 0; + } + + diff = effsk - oldeff; + + if (diff != 0) { + if (wrptr(&bufp, &size, snprintf(bufp, size, " (%s%d)", (diff > 0) ? "+" : "", diff)) != 0) + WARN_STATIC_BUFFER(); + } + } + return bufp - buffer; +} + int bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, char *buf, size_t size) @@ -750,7 +816,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (u->faction == f) { skill *sv; for (sv = u->skills; sv != u->skills + u->skill_size; ++sv) { - size_t bytes = spskill(bufp, size, lang, u, sv, &dh, 1); + size_t bytes = spskill(bufp, size, lang, u, sv, &dh); assert(bytes <= INT_MAX); if (wrptr(&bufp, &size, (int)bytes) != 0) WARN_STATIC_BUFFER(); @@ -918,71 +984,6 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, return dh; } -size_t -spskill(char *buffer, size_t size, const struct locale * lang, - const struct unit * u, struct skill * sv, int *dh, int days) -{ - char *bufp = buffer; - int effsk; - - if (!u->number) - return 0; - if (sv->level <= 0) { - if (sv->old <= 0 || (u->faction->options & WANT_OPTION(O_SHOWSKCHANGE)) == 0) { - return 0; - } - } - - bufp = STRLCPY(bufp, ", ", size); - - if (!*dh) { - bufp = STRLCPY(bufp, LOC(lang, "nr_skills"), size); - bufp = STRLCPY(bufp, ": ", size); - *dh = 1; - } - bufp = STRLCPY(bufp, skillname(sv->id, lang), size); - bufp = STRLCPY(bufp, " ", size); - - if (sv->id == SK_MAGIC) { - sc_mage *mage = get_mage(u); - if (mage && mage->magietyp != M_GRAY) { - bufp = STRLCPY(bufp, LOC(lang, mkname("school", - magic_school[mage->magietyp])), size); - bufp = STRLCPY(bufp, " ", size); - } - } - - if (sv->id == SK_STEALTH && fval(u, UFL_STEALTH)) { - int i = u_geteffstealth(u); - if (i >= 0) { - if (wrptr(&bufp, &size, snprintf(bufp, size, "%d/", i)) != 0) - WARN_STATIC_BUFFER(); - } - } - - effsk = eff_skill(u, sv, 0); - if (wrptr(&bufp, &size, snprintf(bufp, size, "%d", effsk)) != 0) - WARN_STATIC_BUFFER(); - - if (u->faction->options & WANT_OPTION(O_SHOWSKCHANGE)) { - int oldeff = 0; - int diff; - - if (sv->old > 0) { - oldeff = sv->old + get_modifier(u, sv->id, sv->old, u->region, false); - } - - oldeff = MAX(0, oldeff); - diff = effsk - oldeff; - - if (diff != 0) { - if (wrptr(&bufp, &size, snprintf(bufp, size, " (%s%d)", (diff > 0) ? "+" : "", diff)) != 0) - WARN_STATIC_BUFFER(); - } - } - return bufp - buffer; -} - void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned int width, char mark) { bool firstline; @@ -1013,7 +1014,7 @@ void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned firstline = false; } if (!cut) { - cut = s + MIN(len, REPORTWIDTH); + cut = s + ((len < REPORTWIDTH) ? len : REPORTWIDTH); } memcpy(buf + indent, s, cut - s); buf[indent + (cut - s)] = 0; @@ -1669,7 +1670,7 @@ void finish_reports(report_context *ctx) { } } -int write_reports(faction * f, time_t ltime) +int write_reports(faction * f) { bool gotit = false; struct report_context ctx; @@ -1758,7 +1759,6 @@ int reports(void) { faction *f; FILE *mailit; - time_t ltime = time(NULL); int retval = 0; char path[4096]; const char * rpath = reportpath(); @@ -1775,7 +1775,7 @@ int reports(void) for (f = factions; f; f = f->next) { if (f->email && !fval(f, FFL_NPC)) { - int error = write_reports(f, ltime); + int error = write_reports(f); if (error) retval = error; if (mailit) @@ -1998,6 +1998,7 @@ static void eval_unitsize(struct opstack **stack, const void *userdata) const struct unit *u = (const struct unit *)opop(stack).v; variant var; + UNUSED_ARG(userdata); var.i = u->number; opush(stack, var); } @@ -2009,6 +2010,7 @@ static void eval_faction(struct opstack **stack, const void *userdata) size_t len = strlen(c); variant var; + UNUSED_ARG(userdata); var.v = strcpy(balloc(len + 1), c); opush(stack, var); } @@ -2018,6 +2020,8 @@ static void eval_alliance(struct opstack **stack, const void *userdata) const struct alliance *al = (const struct alliance *)opop(stack).v; const char *c = alliancename(al); variant var; + + UNUSED_ARG(userdata); if (c != NULL) { size_t len = strlen(c); var.v = strcpy(balloc(len + 1), c); diff --git a/src/reports.h b/src/reports.h index 0c285257e..d09cce992 100644 --- a/src/reports.h +++ b/src/reports.h @@ -60,12 +60,11 @@ extern "C" { void sparagraph(struct strlist **SP, const char *s, unsigned int indent, char mark); void lparagraph(struct strlist **SP, char *s, unsigned int indent, char mark); const char *hp_status(const struct unit *u); - size_t spskill(char *pbuf, size_t siz, const struct locale *lang, const struct unit *u, struct skill *sv, int *dh, int days); /* mapper */ void spunit(struct strlist **SP, const struct faction *f, const struct unit *u, unsigned int indent, seen_mode mode); int reports(void); - int write_reports(struct faction *f, time_t ltime); + int write_reports(struct faction *f); int init_reports(void); void reorder_units(struct region * r);