From 36b8e007280262287665a9c68d15533a09f33329 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 May 2005 01:26:59 +0000 Subject: [PATCH] pool: ohne old_race() calls geht es schneller. reports: spskill() kann schneller sein. --- src/common/kernel/pool.c | 38 +++++++++++++++++++++++++++---------- src/common/kernel/reports.c | 31 ++++++++++++++++-------------- src/common/kernel/reports.h | 2 +- src/common/kernel/unit.c | 5 ++--- src/mapper/map_units.c | 2 +- 5 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/common/kernel/pool.c b/src/common/kernel/pool.c index 78a6f042a..928966f1f 100644 --- a/src/common/kernel/pool.c +++ b/src/common/kernel/pool.c @@ -40,6 +40,22 @@ static int want_mp = 1 << O_MATERIALPOOL; static int want_sp = 1 << O_SILBERPOOL; +static const race * rc_stonegolem; +static const race * rc_irongolem; + +static void +init_static(void) +{ + static boolean init = false; + if (!init) { + init = true; + rc_stonegolem = rc_find("stone golem"); + if (rc_stonegolem==NULL) log_error(("Could not find race: stone golem\n")); + rc_irongolem = rc_find("irongolem"); + if (rc_irongolem==NULL) log_error(("Could not find race: irongolem\n")); + } +} + int new_get_resource(const unit * u, const resource_type * rtype) { @@ -51,11 +67,10 @@ new_get_resource(const unit * u, const resource_type * rtype) if (i>=0) return i; } if (itype!=NULL) { - race_t urc = old_race(u->race); - /* resouce is an item */ - if (urc==RC_STONEGOLEM && itype == olditemtype[R_STONE]) { + if (!rc_stonegolem) init_static(); + if (itype == olditemtype[R_STONE] && u->race==rc_stonegolem) { return u->number*GOLEM_STONE; - } else if (urc==RC_IRONGOLEM && itype == olditemtype[R_IRON]) { + } else if (itype==olditemtype[R_IRON] && u->race==rc_irongolem) { return u->number*GOLEM_IRON; } else { const item * i = *i_find((item**)&u->items, itype); @@ -103,11 +118,13 @@ new_change_resource(unit * u, const resource_type * rtype, int change) int new_get_resvalue(const unit * u, const resource_type * rtype) { - race_t urc = old_race(u->race); struct reservation * res = u->reservations; - if (rtype==oldresourcetype[R_STONE] && urc==RC_STONEGOLEM) + + if (!rc_stonegolem) init_static(); + + if (rtype==oldresourcetype[R_STONE] && u->race==rc_stonegolem) return (u->number * GOLEM_STONE); - if (rtype==oldresourcetype[R_IRON] && urc==RC_IRONGOLEM) + if (rtype==oldresourcetype[R_IRON] && u->race==rc_irongolem) return (u->number * GOLEM_IRON); while (res && res->type!=rtype) res=res->next; if (res) return res->value; @@ -256,13 +273,14 @@ change_resource(unit * u, resource_t res, int change) { int i = 0; const item_type * itype = resource2item(oldresourcetype[res]); - race_t urc = old_race(u->race); - if (res==R_STONE && urc==RC_STONEGOLEM) { + if (!rc_stonegolem) init_static(); + + if (res==R_STONE && u->race==rc_stonegolem) { i = u->number - (change+GOLEM_STONE-1)/GOLEM_STONE; scale_number(u, i); } - else if (res==R_IRON && urc==RC_IRONGOLEM) { + else if (res==R_IRON && u->race==rc_irongolem) { i = u->number - (change+GOLEM_IRON-1)/GOLEM_IRON; scale_number(u, i); } diff --git a/src/common/kernel/reports.c b/src/common/kernel/reports.c index 27063ff67..f8b0ff2fa 100644 --- a/src/common/kernel/reports.c +++ b/src/common/kernel/reports.c @@ -307,8 +307,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode) dh = 0; if (u->faction == f || telepath_see) { for (sk = 0; sk != MAXSKILLS; sk++) { - spskill(f->locale, u, sk, &dh, 1); - bufp += strlen(bufp); + bufp += spskill(bufp, f->locale, u, sk, &dh, 1); } } @@ -602,47 +601,50 @@ bufunit_ugroupleader(const faction * f, const unit * u, int indent, int mode) } #endif -void -spskill(const struct locale * lang, const struct unit * u, skill_t sk, int *dh, int days) +size_t +spskill(char * buffer, const struct locale * lang, const struct unit * u, skill_t sk, int *dh, int days) { - char * sbuf = buf+strlen(buf); + char * pbuf = buffer; int i, effsk; if (!u->number) - return; + return 0; if (!has_skill(u, sk)) return; - strcat(sbuf, ", "); sbuf+=2; + pbuf += strlcpy(pbuf, ", "); sbuf+=2; if (!*dh) { - sbuf += sprintf(sbuf, "%s: ", LOC(lang, "nr_skills")); + pbuf += strlcpy(pbuf, LOC(lang, "nr_skills")); + strcpy(pbuf++, " "); *dh = 1; } - sbuf += sprintf(sbuf, "%s ", skillname(sk, lang)); + pbuf += strlcpy(pbuf, skillname(sk, lang)); + strcpy(pbuf++, " "); if (sk == SK_MAGIC){ if (find_magetype(u) != M_GRAU){ - sbuf += sprintf(sbuf, "%s ", LOC(lang, mkname("school", magietypen[find_magetype(u)]))); + pbuf += strlcpy(pbuf, LOC(lang, mkname("school", magietypen[find_magetype(u)]))); + strcpy(pbuf++, " "); } } if (sk == SK_STEALTH) { i = u_geteffstealth(u); if(i>=0) { - sbuf += sprintf(sbuf, "%d/", i); + pbuf += dprintf(pbuf, "%d/", i); } } effsk = effskill(u, sk); - sbuf += sprintf(sbuf, "%d", effsk); + pbuf += sprintf(pbuf, "%d", effsk); if(u->faction->options & Pow(O_SHOWSKCHANGE)) { skill *skill = get_skill(u, sk); int oldeff = 0; int diff; - if(skill->old > 0) { + if (skill->old > 0) { oldeff = skill->old + get_modifier(u, sk, skill->old, u->region, false); } @@ -650,9 +652,10 @@ spskill(const struct locale * lang, const struct unit * u, skill_t sk, int *dh, diff = effsk - oldeff; if(diff != 0) { - sbuf += sprintf(sbuf, " (%s%d)", (diff>0)?"+":"", diff); + pbuf += sprintf(pbuf, " (%s%d)", (diff>0)?"+":"", diff); } } + return pbuf-buffer; } void diff --git a/src/common/kernel/reports.h b/src/common/kernel/reports.h index 2d3219b9c..a5cf28f7a 100644 --- a/src/common/kernel/reports.h +++ b/src/common/kernel/reports.h @@ -43,7 +43,7 @@ extern int read_datenames(const char *filename); void sparagraph(struct strlist ** SP, const char *s, int indent, char mark); void lparagraph(struct strlist ** SP, char *s, int indent, char mark); const char *hp_status(const struct unit * u); -extern void spskill(const struct locale * lang, const struct unit * u, skill_t sk, int *dh, int days); /* mapper */ +extern size_t spskill(char * pbuf, const struct locale * lang, const struct unit * u, skill_t sk, int *dh, int days); /* mapper */ extern void spunit(struct strlist ** SP, const struct faction * f, const struct unit * u, int indent, int mode); extern int reports(void); diff --git a/src/common/kernel/unit.c b/src/common/kernel/unit.c index 14443e386..c7d8c0c46 100644 --- a/src/common/kernel/unit.c +++ b/src/common/kernel/unit.c @@ -808,9 +808,10 @@ void u_setfaction(unit * u, faction * f) { int cnt = u->number; - unit ** iunit; + if (u->faction==f) return; if (u->faction) { + unit ** iunit; set_number(u, 0); if (playerrace(u->race)) { --u->faction->no_units; @@ -821,9 +822,7 @@ u_setfaction(unit * u, faction * f) #ifdef LASTORDER set_order(&u->lastorder, NULL); #endif - } - if (u->faction!=NULL) { iunit = &u->faction->units; while (*iunit && *iunit!=u) { iunit=&(*iunit)->nextF; diff --git a/src/mapper/map_units.c b/src/mapper/map_units.c index 8ca3cdf44..214851718 100644 --- a/src/mapper/map_units.c +++ b/src/mapper/map_units.c @@ -896,7 +896,7 @@ mapper_spunit(dbllist ** SP, unit * u, int indent) dh = 0; for (sk = 0; sk != MAXSKILLS; sk++) - spskill(find_locale("de"), u, sk, &dh, 1); + spskill(buf, find_locale("de"), u, sk, &dh, 1); dh = 0; for (itm = u->items;itm;itm=itm->next) {