WIP: refactor allies api

This commit is contained in:
Enno Rehling 2018-10-26 10:54:09 +02:00
parent 5ab7c8cc65
commit 0ac3dc5ead
4 changed files with 77 additions and 61 deletions

View file

@ -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 */

View file

@ -20,6 +20,20 @@
#include <string.h>
#include <stdlib.h>
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;

View file

@ -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 */

View file

@ -1292,10 +1292,11 @@ 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;
}