diff --git a/src/creport.c b/src/creport.c index 475a04b1e..07d957258 100644 --- a/src/creport.c +++ b/src/creport.c @@ -1018,20 +1018,35 @@ static void cr_output_unit_compat(FILE * F, const faction * f, cr_output_unit(&strm, f, u, mode); } -/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */ +static void print_ally(const faction *f, faction *af, int status, + struct ally *sf, FILE *F) { + if (af) { + int mode = alliedgroup(NULL, f, af, sf, HELP_ALL); + if (mode != 0 && status > 0) { + fprintf(F, "ALLIANZ %d\n", af->no); + fprintf(F, "\"%s\";Parteiname\n", af->name); + fprintf(F, "%d;Status\n", status & HELP_ALL); + } + } +} + +struct print_ally_s { + const faction *f; + FILE *F; +}; + +static void print_ally_cb(struct ally *sf, faction *af, int status, void *udata) { + struct print_ally_s *data = (struct print_ally_s *)udata; + print_ally(data->f, af, status, sf, data->F); +} /* prints allies */ -static void show_allies_cr(FILE * F, const faction * f, const ally * sf) +static void show_allies_cr(FILE * F, const faction * f, struct ally * sf) { - for (; sf; sf = sf->next) - if (sf->faction) { - int mode = alliedgroup(NULL, f, sf->faction, sf, HELP_ALL); - if (mode != 0 && sf->status > 0) { - fprintf(F, "ALLIANZ %d\n", sf->faction->no); - fprintf(F, "\"%s\";Parteiname\n", sf->faction->name); - fprintf(F, "%d;Status\n", sf->status & HELP_ALL); - } - } + struct print_ally_s data; + data.F = F; + data.f = f; + allies_walk(sf, print_ally_cb, &data); } /* prints allies */ diff --git a/src/kernel/ally.c b/src/kernel/ally.c index 0660f148a..3abfc4c09 100644 --- a/src/kernel/ally.c +++ b/src/kernel/ally.c @@ -20,6 +20,20 @@ #include #include +typedef struct ally { + struct ally *next; + struct faction *faction; + int status; +} ally; + +void allies_walk(struct ally *allies, cb_allies_walk callback, void *udata); +{ + ally *al; + for (al = allies; al; al = al->next) { + callback(allies, al->faction, al->status, udata); + } +} + void read_allies(gamedata * data, faction *f) { ally **sfp = &f->allies; @@ -196,7 +210,8 @@ static int AllianceRestricted(void) int alliedgroup(const struct plane *pl, const struct faction *f, - const struct faction *f2, const struct ally *sf, int mode) + const struct faction *f2, + const struct ally *sf, int mode) { if (!(faction_alive(f) && faction_alive(f2))) { return 0; diff --git a/src/kernel/ally.h b/src/kernel/ally.h index d4c41d36c..16bd57aa7 100644 --- a/src/kernel/ally.h +++ b/src/kernel/ally.h @@ -28,19 +28,18 @@ extern "C" { struct faction; struct gamedata; struct unit; + struct ally; extern struct attrib_type at_npcfaction; - typedef struct ally { - struct ally *next; - struct faction *faction; - int status; - } ally; - void read_allies(struct gamedata * data, struct faction *f); - ally * ally_find(ally *al, const struct faction *f); - ally * ally_add(ally **al_p, struct faction *f); - void ally_remove(ally **al_p, struct faction *f); + typedef void (*cb_allies_walk)(struct ally *, struct faction *, int, void *); + void allies_walk(struct ally *allies, cb_allies_walk callback, void *udata); + struct ally* ally_find(struct ally*al, const struct faction *f); + void ally_set(struct ally**al_p, struct faction *f, int status); + int ally_get(struct ally *al, struct faction *f); + struct ally* ally_add(struct ally**al_p, struct faction *f); + void ally_remove(struct ally**al_p, struct faction *f); int AllianceAuto(void); /* flags that allied factions get automatically */ int HelpMask(void); /* flags restricted to allied factions */ @@ -49,7 +48,7 @@ extern "C" { int alliedfaction(const struct plane *pl, const struct faction *f, const struct faction *f2, int mode); int alliedgroup(const struct plane *pl, const struct faction *f, - const struct faction *f2, const struct ally *sf, int mode); + const struct faction *f2, const struct ally*sf, int mode); #ifdef __cplusplus } diff --git a/src/laws.c b/src/laws.c index 4c2a29e4e..035ed7088 100644 --- a/src/laws.c +++ b/src/laws.c @@ -1292,11 +1292,12 @@ void quit(void) int ally_cmd(unit * u, struct order *ord) { char token[128]; - ally *sf, **sfp; + struct ally **sfp; faction *f; int keyword, not_kw; const char *s; - + int sf_status; + init_order_depr(ord); f = getfaction(); @@ -1319,28 +1320,17 @@ int ally_cmd(unit * u, struct order *ord) sfp = &u->faction->allies; if (fval(u, UFL_GROUP)) { attrib *a = a_find(u->attribs, &at_group); - if (a) + if (a) { sfp = &((group *)a->data.v)->allies; + } } - for (sf = *sfp; sf; sf = sf->next) - if (sf->faction == f) - break; /* Gleich die passende raussuchen, wenn vorhanden */ not_kw = getparam(u->faction->locale); /* HELFE partei [modus] NICHT */ - if (!sf) { - if (keyword == P_NOT || not_kw == P_NOT) { - /* Wir helfen der Partei gar nicht... */ - return 0; - } - else { - sf = ally_add(sfp, f); - sf->status = 0; - } - } + sf_status = ally_get(*sfp, f); switch (keyword) { case P_NOT: - sf->status = 0; + sf_status = 0; break; case NOPARAM: @@ -1348,60 +1338,57 @@ int ally_cmd(unit * u, struct order *ord) return 0; case P_ANY: - if (not_kw == P_NOT) - sf->status = 0; - else - sf->status = HELP_ALL; + sf_status = (not_kw == P_NOT) ? 0 : HELP_ALL; break; case P_TRAVEL: - if (not_kw == P_NOT) - sf->status = sf->status & (HELP_ALL - HELP_TRAVEL); - else - sf->status = sf->status | HELP_TRAVEL; + if (not_kw == P_NOT) { + sf_status = sf_status & (HELP_ALL - HELP_TRAVEL); + } + else { + sf_status |= HELP_TRAVEL; + } break; case P_GIVE: if (not_kw == P_NOT) - sf->status = sf->status & (HELP_ALL - HELP_GIVE); + sf_status &= (HELP_ALL - HELP_GIVE); else - sf->status = sf->status | HELP_GIVE; + sf_status |= HELP_GIVE; break; case P_MONEY: if (not_kw == P_NOT) - sf->status = sf->status & (HELP_ALL - HELP_MONEY); + sf_status &= (HELP_ALL - HELP_MONEY); else - sf->status = sf->status | HELP_MONEY; + sf_status |= HELP_MONEY; break; case P_FIGHT: if (not_kw == P_NOT) - sf->status = sf->status & (HELP_ALL - HELP_FIGHT); + sf_status &= (HELP_ALL - HELP_FIGHT); else - sf->status = sf->status | HELP_FIGHT; + sf_status |= HELP_FIGHT; break; case P_FACTIONSTEALTH: if (not_kw == P_NOT) - sf->status = sf->status & (HELP_ALL - HELP_FSTEALTH); + sf_status &= (HELP_ALL - HELP_FSTEALTH); else - sf->status = sf->status | HELP_FSTEALTH; + sf_status |= HELP_FSTEALTH; break; case P_GUARD: if (not_kw == P_NOT) - sf->status = sf->status & (HELP_ALL - HELP_GUARD); + sf_status &= (HELP_ALL - HELP_GUARD); else - sf->status = sf->status | HELP_GUARD; + sf_status |= HELP_GUARD; break; } - sf->status &= HelpMask(); + sf_status &= HelpMask(); + ally_set(sfp, f, sf_status); - if (sf->status == 0) { /* Alle HELPs geloescht */ - removelist(sfp, sf); - } return 0; }