Even more flags to reduce attribute-lookups.

double bookkeeping can be scary, but i think it should be rather safe in these cases.
This commit is contained in:
Enno Rehling 2005-06-12 17:57:14 +00:00
parent cbf2d8c679
commit 9dc06958e2
12 changed files with 128 additions and 58 deletions

View File

@ -613,11 +613,22 @@ cr_output_unit(FILE * F, const region * r,
const item_type * lasttype; const item_type * lasttype;
int pr; int pr;
item *itm, *show; item *itm, *show;
boolean itemcloak = is_cursed(u->attribs, C_ITEMCLOAK, 0);
building * b; building * b;
const char * pzTmp; const char * pzTmp;
skill * sv; skill * sv;
const attrib *a_fshidden = NULL; const attrib *a_fshidden = NULL;
boolean itemcloak = false;
static const curse_type * itemcloak_ct = 0;
static boolean init = false;
if (!init) {
init = true;
itemcloak_ct = ct_find("itemcloak");
}
if (itemcloak_ct!=NULL) {
itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct));
}
assert(u); assert(u);
@ -637,7 +648,8 @@ cr_output_unit(FILE * F, const region * r,
const attrib * a_otherfaction = a_find(u->attribs, &at_otherfaction); const attrib * a_otherfaction = a_find(u->attribs, &at_otherfaction);
const faction * otherfaction = a_otherfaction?get_otherfaction(a_otherfaction):NULL; const faction * otherfaction = a_otherfaction?get_otherfaction(a_otherfaction):NULL;
/* my own faction, full info */ /* my own faction, full info */
const attrib *a = a_find(u->attribs, &at_group); const attrib *a = NULL;
if (fval(u, UFL_GROUP)) a = a_find(u->attribs, &at_group);
if (a!=NULL) { if (a!=NULL) {
const group * g = (const group*)a->data.v; const group * g = (const group*)a->data.v;
fprintf(F, "%d;gruppe\n", g->gid); fprintf(F, "%d;gruppe\n", g->gid);
@ -731,13 +743,15 @@ cr_output_unit(FILE * F, const region * r,
fprintf(F, "%d;alias\n", -i); fprintf(F, "%d;alias\n", -i);
i = get_money(u); i = get_money(u);
fprintf(F, "%d;Kampfstatus\n", u->status); fprintf(F, "%d;Kampfstatus\n", u->status);
if(fval(u, UFL_NOAID)) { if (fval(u, UFL_NOAID)) {
fputs("1;unaided\n", F); fputs("1;unaided\n", F);
} }
if (fval(u, UFL_STEALTH)) {
i = u_geteffstealth(u); i = u_geteffstealth(u);
if (i >= 0) { if (i >= 0) {
fprintf(F, "%d;Tarnung\n", i); fprintf(F, "%d;Tarnung\n", i);
} }
}
c = uprivate(u); c = uprivate(u);
if (c) { if (c) {
fprintf(F, "\"%s\";privat\n", c); fprintf(F, "\"%s\";privat\n", c);

View File

@ -1237,7 +1237,7 @@ ally_cmd(unit * u, struct order * ord)
keyword = findparam(s, u->faction->locale); keyword = findparam(s, u->faction->locale);
sfp = &u->faction->allies; sfp = &u->faction->allies;
{ if (fval(u, UFL_GROUP)) {
attrib * a = a_find(u->attribs, &at_group); attrib * a = a_find(u->attribs, &at_group);
if (a) sfp = &((group*)a->data.v)->allies; if (a) sfp = &((group*)a->data.v)->allies;
} }
@ -1327,7 +1327,6 @@ static int
prefix_cmd(unit * u, struct order * ord) prefix_cmd(unit * u, struct order * ord)
{ {
attrib **ap; attrib **ap;
attrib *a;
int i; int i;
const char *s; const char *s;
@ -1336,9 +1335,13 @@ prefix_cmd(unit * u, struct order * ord)
s = getstrtoken(); s = getstrtoken();
if (!*s) { if (!*s) {
attrib *a = NULL;
if (fval(u, UFL_GROUP)) {
a = a_find(u->attribs, &at_group); a = a_find(u->attribs, &at_group);
}
if (a) { if (a) {
a_removeall(&((group*)a->data.v)->attribs, &at_raceprefix); group * g = (group*)a->data.v;
a_removeall(&g->attribs, &at_raceprefix);
} else { } else {
a_removeall(&u->faction->attribs, &at_raceprefix); a_removeall(&u->faction->attribs, &at_raceprefix);
} }
@ -1358,8 +1361,11 @@ prefix_cmd(unit * u, struct order * ord)
} }
ap = &u->faction->attribs; ap = &u->faction->attribs;
a = a_find(u->attribs, &at_group); if (fval(u, UFL_GROUP)) {
if (a) ap = &((group*)a->data.v)->attribs; attrib * a = a_find(u->attribs, &at_group);
group * g = (group*)a->data.v;
if (a) ap = &g->attribs;
}
set_prefix(ap, race_prefixes[i]); set_prefix(ap, race_prefixes[i]);
return 0; return 0;
@ -1735,10 +1741,11 @@ name_cmd(unit * u, struct order * ord)
case P_GROUP: case P_GROUP:
{ {
attrib * a = a_find(u->attribs, &at_group); attrib * a = NULL;
if (a){ if (fval(u, UFL_GROUP)) a = a_find(u->attribs, &at_group);
if (a) {
group * g = (group*)a->data.v; group * g = (group*)a->data.v;
s= &g->name; s = &g->name;
break; break;
} else { } else {
cmistake(u, ord, 109, MSG_EVENT); cmistake(u, ord, 109, MSG_EVENT);

View File

@ -2852,10 +2852,14 @@ make_fighter(battle * b, unit * u, side * s1, boolean attack)
boolean pr_aid = false; boolean pr_aid = false;
boolean stealth = (boolean)((fval(u, UFL_PARTEITARNUNG)!=0)?true:false); boolean stealth = (boolean)((fval(u, UFL_PARTEITARNUNG)!=0)?true:false);
int rest; int rest;
const attrib * a = a_find(u->attribs, &at_group); const group * g = NULL;
const group * g = a?(const group*)a->data.v:NULL; const attrib *a = a_find(u->attribs, &at_otherfaction);
const attrib *a2 = a_find(u->attribs, &at_otherfaction); const faction *stealthfaction = a?get_otherfaction(a):NULL;
const faction *stealthfaction = a2?get_otherfaction(a2):NULL;
if (fval(u, UFL_GROUP)) {
const attrib * agroup = a_find(u->attribs, &at_group);
if (agroup!=NULL) g = (const group*)agroup->data.v;
}
/* Illusionen und Zauber kaempfen nicht */ /* Illusionen und Zauber kaempfen nicht */
if (fval(u->race, RCF_ILLUSIONARY) || idle(u->faction)) if (fval(u->race, RCF_ILLUSIONARY) || idle(u->faction))

View File

@ -851,27 +851,32 @@ effskill(const unit * u, skill_t sk)
int int
effstealth(const unit * u) effstealth(const unit * u)
{ {
int e, es; int e;
/* Auf dem Ozean keine Tarnung! */ /* Auf dem Ozean keine Tarnung! */
if (u->region->terrain == T_OCEAN) return 0; if (u->region->terrain == T_OCEAN) return 0;
e = effskill(u, SK_STEALTH); e = effskill(u, SK_STEALTH);
es = u_geteffstealth(u); if (fval(u, UFL_STEALTH)) {
int es = u_geteffstealth(u);
if (es >=0 && es < e) return es; if (es >=0 && es < e) return es;
}
return e; return e;
} }
int int
eff_stealth (const unit * u, const region * r) eff_stealth (const unit * u, const region * r)
{ {
int e, es; int e;
if (r->terrain == T_OCEAN) return 0;
e = eff_skill (u, SK_STEALTH, r); e = eff_skill (u, SK_STEALTH, r);
es = u_geteffstealth(u); if (fval(u, UFL_STEALTH)) {
int es = u_geteffstealth(u);
if (es >=0 && es < e) return es; if (es >=0 && es < e) return es;
}
return e; return e;
} }
@ -1009,7 +1014,6 @@ int
alliedunit(const unit * u, const faction * f2, int mode) alliedunit(const unit * u, const faction * f2, int mode)
{ {
ally * sf; ally * sf;
const attrib * a;
const plane * pl = getplane(u->region); const plane * pl = getplane(u->region);
int automode; int automode;
@ -1022,8 +1026,10 @@ alliedunit(const unit * u, const faction * f2, int mode)
mode = (mode & automode) | (mode & HELP_GIVE); mode = (mode & automode) | (mode & HELP_GIVE);
sf = u->faction->allies; sf = u->faction->allies;
a = a_findc(u->attribs, &at_group); if (fval(u, UFL_GROUP)) {
const attrib * a = a_findc(u->attribs, &at_group);
if (a!=NULL) sf = ((group*)a->data.v)->allies; if (a!=NULL) sf = ((group*)a->data.v)->allies;
}
return alliedgroup(pl, u->faction, f2, sf, mode); return alliedgroup(pl, u->faction, f2, sf, mode);
} }
@ -1983,10 +1989,13 @@ create_unit(region * r, faction * f, int number, const struct race *urace, int i
} }
/* Gruppen */ /* Gruppen */
if (fval(creator, UFL_GROUP)) {
a = a_find(creator->attribs, &at_group); a = a_find(creator->attribs, &at_group);
if (a) { if (a) {
group * g = (group*)a->data.v; group * g = (group*)a->data.v;
a_add(&u->attribs, a_new(&at_group))->data.v = g; a_add(&u->attribs, a_new(&at_group))->data.v = g;
fset(u, UFL_GROUP);
}
} }
a = a_find(creator->attribs, &at_otherfaction); a = a_find(creator->attribs, &at_otherfaction);
if (a) { if (a) {

View File

@ -305,7 +305,7 @@ destroyfaction(faction * f)
/* units of other factions that were disguised as this faction /* units of other factions that were disguised as this faction
* have their disguise replaced by ordinary faction hiding. */ * have their disguise replaced by ordinary faction hiding. */
for(rc=regions; rc; rc=rc->next) { for (rc=regions; rc; rc=rc->next) {
for(u=rc->units; u; u=u->next) { for(u=rc->units; u; u=u->next) {
attrib *a = a_find(u->attribs, &at_otherfaction); attrib *a = a_find(u->attribs, &at_otherfaction);
if(!a) continue; if(!a) continue;

View File

@ -131,12 +131,18 @@ free_group(group * g)
boolean boolean
join_group(unit * u, const char * name) join_group(unit * u, const char * name)
{ {
attrib * a = a_find(u->attribs, &at_group); attrib * a = NULL;
group * g; group * g;
if (fval(u, UFL_GROUP)) {
a = a_find(u->attribs, &at_group);
}
if (a) ((group *)(a->data.v))->members--; if (a) ((group *)(a->data.v))->members--;
if (!name || !strlen(name)) { if (!name || !strlen(name)) {
if (a) a_remove(&u->attribs, a); if (a) {
a_remove(&u->attribs, a);
freset(u, UFL_GROUP);
}
return true; return true;
} }
g = find_groupbyname(u->faction->groups, name); g = find_groupbyname(u->faction->groups, name);
@ -144,7 +150,10 @@ join_group(unit * u, const char * name)
g = new_group(u->faction, name, ++maxgid); g = new_group(u->faction, name, ++maxgid);
init_group(u->faction, g); init_group(u->faction, g);
} }
if (!a) a = a_add(&u->attribs, a_new(&at_group)); if (!a) {
a = a_add(&u->attribs, a_new(&at_group));
fset(u, UFL_GROUP);
}
a->data.v = g; a->data.v = g;
g->members++; g->members++;
return true; return true;

View File

@ -559,7 +559,6 @@ jihad_attacks(void)
faction *f; faction *f;
region *r; region *r;
unit *u, *u2; unit *u, *u2;
attrib *a;
ally *sf, **sfp; ally *sf, **sfp;
for(f=factions; f; f=f->next) if(fspecial(f, FS_JIHAD)) { for(f=factions; f; f=f->next) if(fspecial(f, FS_JIHAD)) {
@ -580,8 +579,10 @@ jihad_attacks(void)
for(u=r->units; u; u=u->next) if(jihad(f, u->race)) { for(u=r->units; u; u=u->next) if(jihad(f, u->race)) {
/* Allianz auflösen */ /* Allianz auflösen */
sfp = &u2->faction->allies; sfp = &u2->faction->allies;
a = a_find(u2->attribs, &at_group); if (fval(u, UFL_GROUP)) {
attrib * a = a_find(u2->attribs, &at_group);
if (a) sfp = &((group*)a->data.v)->allies; if (a) sfp = &((group*)a->data.v)->allies;
}
for (sf=*sfp; sf; sf = sf->next) { for (sf=*sfp; sf; sf = sf->next) {
if(sf->faction == u->faction) break; if(sf->faction == u->faction) break;

View File

@ -314,10 +314,12 @@ rc_name(const race * rc, int n)
const char * const char *
raceprefix(const unit *u) raceprefix(const unit *u)
{ {
const attrib * agroup = a_findc(u->attribs, &at_group);
const attrib * asource = u->faction->attribs; const attrib * asource = u->faction->attribs;
if (fval(u, UFL_GROUP)) {
const attrib * agroup = agroup = a_findc(u->attribs, &at_group);
if (agroup!=NULL) asource = ((const group *)(agroup->data.v))->attribs; if (agroup!=NULL) asource = ((const group *)(agroup->data.v))->attribs;
}
return get_prefix(asource); return get_prefix(asource);
} }

View File

@ -207,12 +207,14 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
if (!isbattle) { if (!isbattle) {
attrib *a_otherfaction = a_find(u->attribs, &at_otherfaction); attrib *a_otherfaction = a_find(u->attribs, &at_otherfaction);
if (u->faction == f) { if (u->faction == f) {
if (fval(u, UFL_GROUP)) {
attrib *a = a_find(u->attribs, &at_group); attrib *a = a_find(u->attribs, &at_group);
if (a) { if (a) {
group * g = (group*)a->data.v; group * g = (group*)a->data.v;
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf)); bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, groupid(g, f), sizeof(buf)-(bufp-buf)); bufp += strlcpy(bufp, groupid(g, f), sizeof(buf)-(bufp-buf));
} }
}
if (getarnt) { if (getarnt) {
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf)); bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, LOC(f->locale, "anonymous"), sizeof(buf)-(bufp-buf)); bufp += strlcpy(bufp, LOC(f->locale, "anonymous"), sizeof(buf)-(bufp-buf));
@ -559,7 +561,7 @@ bufunit_ugroupleader(const faction * f, const unit * u, int indent, int mode)
show = NULL; show = NULL;
for(i = 0; i < ug->members; i++) { for(i = 0; i < ug->members; i++) {
unit *uc = ug->unit_array[i]; unit *uc = ug->unit_array[i];
if(!itemcloak && mode >= see_unit && !(a_fshidden if (!itemcloak && mode >= see_unit && !(a_fshidden
&& a_fshidden->data.ca[1] == 1 && effskill(u, SK_STEALTH) >= 3)) { && a_fshidden->data.ca[1] == 1 && effskill(u, SK_STEALTH) >= 3)) {
for (itm=uc->items;itm;itm=itm->next) { for (itm=uc->items;itm;itm=itm->next) {
item *ishow; item *ishow;
@ -656,7 +658,7 @@ spskill(char * buffer, size_t siz, const struct locale * lang, const struct unit
} }
} }
if (sk == SK_STEALTH) { if (sk == SK_STEALTH && fval(u, UFL_STEALTH)) {
i = u_geteffstealth(u); i = u_geteffstealth(u);
if(i>=0) { if(i>=0) {
bufp += sprintf(bufp, "%d/", i); bufp += sprintf(bufp, "%d/", i);

View File

@ -488,17 +488,32 @@ attrib_type at_stealth = {
void void
u_seteffstealth(unit * u, int value) u_seteffstealth(unit * u, int value)
{ {
attrib * a = a_find(u->attribs, &at_stealth); attrib * a = NULL;
if (!a && value<0) return; if (fval(u, UFL_STEALTH)) {
if (!a) a = a_add(&u->attribs, a_new(&at_stealth)); a = a_find(u->attribs, &at_stealth);
}
if (value<0) {
if (a!=NULL) {
freset(u, UFL_STEALTH);
a_remove(&u->attribs, a);
}
return;
}
if (a==NULL) {
a = a_add(&u->attribs, a_new(&at_stealth));
fset(u, UFL_STEALTH);
}
a->data.i = value; a->data.i = value;
} }
int int
u_geteffstealth(const struct unit * u) u_geteffstealth(const struct unit * u)
{ {
if (fval(u, UFL_STEALTH)) {
attrib * a = a_find(u->attribs, &at_stealth); attrib * a = a_find(u->attribs, &at_stealth);
return (a?a->data.i:-1); if (a!=NULL) return a->data.i;
}
return -1;
} }
int int

View File

@ -58,13 +58,15 @@ struct skill;
#define UFL_TAKEALL (1<<25) /* Einheit nimmt alle Gegenstände an */ #define UFL_TAKEALL (1<<25) /* Einheit nimmt alle Gegenstände an */
/* flags that speed up attribute access: */ /* flags that speed up attribute access: */
#define UFL_STEALTH (1<<26)
#define UFL_GUARD (1<<27) #define UFL_GUARD (1<<27)
#define UFL_GROUP (1<<28)
/* Flags, die gespeichert werden sollen: */ /* Flags, die gespeichert werden sollen: */
#ifndef HEROES #ifndef HEROES
# define UFL_SAVEMASK (UFL_MOVED | UFL_NOAID | UFL_OWNER | UFL_PARTEITARNUNG | UFL_LOCKED | UFL_HUNGER | UFL_TAKEALL | UFL_GUARD) # define UFL_SAVEMASK (UFL_MOVED | UFL_NOAID | UFL_OWNER | UFL_PARTEITARNUNG | UFL_LOCKED | UFL_HUNGER | UFL_TAKEALL | UFL_GUARD | UFL_STEALTH)
#else #else
# define UFL_SAVEMASK (UFL_MOVED | UFL_NOAID | UFL_OWNER | UFL_PARTEITARNUNG | UFL_LOCKED | UFL_HUNGER | UFL_TAKEALL | UFL_GUARD | UFL_HERO) # define UFL_SAVEMASK (UFL_MOVED | UFL_NOAID | UFL_OWNER | UFL_PARTEITARNUNG | UFL_LOCKED | UFL_HUNGER | UFL_TAKEALL | UFL_GUARD | UFL_STEALTH | UFL_HERO)
#endif #endif
#define UNIT_MAXSIZE 50000 #define UNIT_MAXSIZE 50000

View File

@ -1023,7 +1023,12 @@ fix_attribflags(void)
while (a) { while (a) {
if (a->type==&at_guard) { if (a->type==&at_guard) {
fset(u, UFL_GUARD); fset(u, UFL_GUARD);
break; }
else if (a->type==&at_group) {
fset(u, UFL_GROUP);
}
else if (a->type==&at_stealth) {
fset(u, UFL_STEALTH);
} }
a = a->next; a = a->next;
} }
@ -1071,7 +1076,7 @@ korrektur(void)
} }
do_once("chgt", fix_chaosgates()); do_once("chgt", fix_chaosgates());
do_once("attr", fix_attribflags()); do_once("atri", fix_attribflags());
fix_astralplane(); fix_astralplane();
fix_firewalls(); fix_firewalls();
fix_gates(); fix_gates();