forked from github/server
eliminate struct ally, use struct allies
This commit is contained in:
parent
0ca2523ea7
commit
b0485ec57f
9 changed files with 69 additions and 180 deletions
|
@ -1029,7 +1029,7 @@ struct print_ally_s {
|
|||
FILE *F;
|
||||
};
|
||||
|
||||
static int print_ally_cb(struct ally *al, faction *af, int status, void *udata) {
|
||||
static int print_ally_cb(struct allies *al, faction *af, int status, void *udata) {
|
||||
struct print_ally_s *data = (struct print_ally_s *)udata;
|
||||
|
||||
UNUSED_ARG(al);
|
||||
|
@ -1046,8 +1046,8 @@ static void show_allies_cr(FILE * F, const faction * f, const group *g)
|
|||
struct print_ally_s data;
|
||||
data.F = F;
|
||||
data.f = f;
|
||||
struct ally *sf = g ? g->allies : f->allies;
|
||||
ally_walk(sf, print_ally_cb, &data);
|
||||
struct allies *sf = g ? g->allies : f->allies;
|
||||
allies_walk(sf, print_ally_cb, &data);
|
||||
}
|
||||
|
||||
/* prints allies */
|
||||
|
|
|
@ -23,11 +23,11 @@
|
|||
typedef struct allies {
|
||||
struct allies *next;
|
||||
int num;
|
||||
const struct faction *factions[BLOCKSIZE];
|
||||
struct faction *factions[BLOCKSIZE];
|
||||
int status[BLOCKSIZE];
|
||||
} allies;
|
||||
|
||||
static void block_insert(allies *al, const struct faction *f, int status) {
|
||||
static void block_insert(allies *al, struct faction *f, int status) {
|
||||
int i = al->num++;
|
||||
al->status[i] = status;
|
||||
al->factions[i] = f;
|
||||
|
@ -45,7 +45,22 @@ static int block_search(allies *al, const struct faction *f) {
|
|||
return BLOCKSIZE;
|
||||
}
|
||||
|
||||
int allies_get(allies *al, const struct faction *f)
|
||||
int allies_walk(struct allies *all, cb_allies_walk callback, void *udata)
|
||||
{
|
||||
allies *al;
|
||||
for (al = all; al; al = al->next) {
|
||||
int i;
|
||||
for (i = 0; i != al->num; ++i) {
|
||||
int e = callback(all, al->factions[i], al->status[i], udata);
|
||||
if (e != 0) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ally_get(allies *al, const struct faction *f)
|
||||
{
|
||||
for (; al; al = al->next) {
|
||||
int i = block_search(al, f);
|
||||
|
@ -56,7 +71,7 @@ int allies_get(allies *al, const struct faction *f)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void allies_set(allies **p_al, const struct faction *f, int status)
|
||||
void ally_set(allies **p_al, struct faction *f, int status)
|
||||
{
|
||||
while (*p_al) {
|
||||
allies *al = *p_al;
|
||||
|
@ -89,7 +104,7 @@ void allies_set(allies **p_al, const struct faction *f, int status)
|
|||
block_insert(*p_al, f, status);
|
||||
}
|
||||
|
||||
void allies_write(gamedata * data, const allies *alist)
|
||||
void write_allies(gamedata * data, const allies *alist)
|
||||
{
|
||||
const allies *al;
|
||||
for (al = alist; al; al = al->next) {
|
||||
|
@ -105,7 +120,7 @@ void allies_write(gamedata * data, const allies *alist)
|
|||
write_faction_reference(NULL, data->store);
|
||||
}
|
||||
|
||||
void allies_read(gamedata * data, allies **p_al)
|
||||
void read_allies(gamedata * data, allies **p_al)
|
||||
{
|
||||
for (;;) {
|
||||
faction *f;
|
||||
|
@ -118,103 +133,32 @@ void allies_read(gamedata * data, allies **p_al)
|
|||
f = findfaction(aid);
|
||||
if (!f) f = faction_create(aid);
|
||||
READ_INT(data->store, &status);
|
||||
allies_set(p_al, f, status);
|
||||
ally_set(p_al, f, status);
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct ally {
|
||||
struct ally *next;
|
||||
struct faction *faction;
|
||||
int status;
|
||||
} ally;
|
||||
|
||||
static ally * ally_add(ally **al_p, struct faction *f) {
|
||||
ally * al;
|
||||
while (*al_p) {
|
||||
al = *al_p;
|
||||
if (f && al->faction == f) return al;
|
||||
al_p = &al->next;
|
||||
}
|
||||
al = (ally *)calloc(1, sizeof(ally));
|
||||
al->faction = f;
|
||||
*al_p = al;
|
||||
return al;
|
||||
}
|
||||
|
||||
void allies_free(ally *al)
|
||||
void allies_free(allies *al)
|
||||
{
|
||||
while (al) {
|
||||
ally * an = al->next;
|
||||
allies * an = al->next;
|
||||
free(al);
|
||||
al = an;
|
||||
}
|
||||
}
|
||||
|
||||
ally *ally_clone(const ally *al) {
|
||||
ally *al_clone = NULL, **al_end = &al_clone;
|
||||
allies *allies_clone(const allies *al) {
|
||||
allies *al_clone = NULL, **al_end = &al_clone;
|
||||
|
||||
for (; al; al = al->next) {
|
||||
if (al->faction) {
|
||||
ally * al_new = ally_add(al_end, al->faction);
|
||||
al_new->status = al->status;
|
||||
al_end = &al_new->next;
|
||||
}
|
||||
allies *al_new = calloc(1, sizeof(allies));
|
||||
memcpy(al_new, al, sizeof(allies));
|
||||
*al_end = al_new;
|
||||
al_end = &al_new->next;
|
||||
}
|
||||
*al_end = NULL;
|
||||
return al_clone;
|
||||
}
|
||||
|
||||
int ally_walk(ally *allies, cb_ally_walk callback, void *udata)
|
||||
{
|
||||
ally *al;
|
||||
for (al = allies; al; al = al->next) {
|
||||
int err = callback(allies, al->faction, al->status, udata);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void write_allies(gamedata * data, const ally *alist)
|
||||
{
|
||||
const ally *a;
|
||||
for (a = alist; a; a = a->next) {
|
||||
if (a->faction && a->faction->_alive) {
|
||||
write_faction_reference(a->faction, data->store);
|
||||
WRITE_INT(data->store, a->status);
|
||||
}
|
||||
}
|
||||
write_faction_reference(NULL, data->store);
|
||||
}
|
||||
|
||||
void read_allies(gamedata * data, ally **sfp)
|
||||
{
|
||||
for (;;) {
|
||||
int aid;
|
||||
READ_INT(data->store, &aid);
|
||||
if (aid > 0) {
|
||||
ally * al = ally_add(sfp, NULL);
|
||||
int state;
|
||||
if ((al->faction = findfaction(aid)) == NULL) {
|
||||
al->faction = faction_create(aid);
|
||||
}
|
||||
READ_INT(data->store, &state);
|
||||
al->status = state & HELP_ALL;
|
||||
sfp = &al->next;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ally * ally_find(ally *al, const struct faction *f) {
|
||||
for (; al; al = al->next) {
|
||||
if (al->faction == f) return al;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ally_flag(const char *s, int help_mask)
|
||||
{
|
||||
if ((help_mask & HELP_MONEY) && strcmp(s, "money") == 0)
|
||||
|
@ -349,7 +293,7 @@ int
|
|||
alliedgroup(const struct faction *f,
|
||||
const struct faction *f2, const struct group *g, int mask)
|
||||
{
|
||||
ally *all = g ? g->allies : f->allies;
|
||||
allies *all = g ? g->allies : f->allies;
|
||||
int status;
|
||||
|
||||
if (!(faction_alive(f) && faction_alive(f2))) {
|
||||
|
@ -399,32 +343,3 @@ int alliedunit(const unit * u, const faction * f2, int mask)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void ally_set(ally **allies, struct faction *f, int status) {
|
||||
ally *al;
|
||||
while (*allies) {
|
||||
al = *allies;
|
||||
if (al->faction == f) {
|
||||
if (status != 0) {
|
||||
al->status = status;
|
||||
}
|
||||
else {
|
||||
*allies = al->next;
|
||||
free(al);
|
||||
}
|
||||
return;
|
||||
}
|
||||
allies = &al->next;
|
||||
}
|
||||
al = ally_add(allies, f);
|
||||
al->status = status;
|
||||
}
|
||||
|
||||
int ally_get(ally *allies, const struct faction *f) {
|
||||
ally *al;
|
||||
for (al = allies; al; al = al->next) {
|
||||
if (al->faction == f) {
|
||||
return al->status;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -23,31 +23,24 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct attrib_type;
|
||||
struct faction;
|
||||
struct group;
|
||||
struct gamedata;
|
||||
struct unit;
|
||||
struct ally;
|
||||
struct allies;
|
||||
struct attrib_type;
|
||||
struct faction;
|
||||
struct group;
|
||||
struct gamedata;
|
||||
struct unit;
|
||||
struct allies;
|
||||
|
||||
extern struct attrib_type at_npcfaction;
|
||||
extern struct attrib_type at_npcfaction;
|
||||
|
||||
int allies_get(struct allies *al, const struct faction *f);
|
||||
void allies_set(struct allies **p_al, const struct faction *f, int status);
|
||||
void allies_write(struct gamedata * data, const struct allies *alist);
|
||||
void allies_read(struct gamedata * data, struct allies **sfp);
|
||||
int ally_get(struct allies *al, const struct faction *f);
|
||||
void ally_set(struct allies **p_al, struct faction *f, int status);
|
||||
void write_allies(struct gamedata * data, const struct allies *alist);
|
||||
void read_allies(struct gamedata * data, struct allies **sfp);
|
||||
typedef int (*cb_allies_walk)(struct allies *, struct faction *, int, void *);
|
||||
int allies_walk(struct allies *allies, cb_allies_walk callback, void *udata);
|
||||
struct allies *allies_clone(const struct allies *al);
|
||||
|
||||
void read_allies(struct gamedata * data, struct ally **alist);
|
||||
void write_allies(struct gamedata * data, const struct ally *alist);
|
||||
typedef int (*cb_ally_walk)(struct ally *, struct faction *, int, void *);
|
||||
int ally_walk(struct ally *allies, cb_ally_walk callback, void *udata);
|
||||
struct ally *ally_clone(const struct ally *al);
|
||||
void allies_free(struct ally *al);
|
||||
|
||||
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, const struct faction *f);
|
||||
void allies_free(struct allies *al);
|
||||
|
||||
int AllianceAuto(void); /* flags that allied factions get automatically */
|
||||
int HelpMask(void); /* flags restricted to allied factions */
|
||||
|
|
|
@ -5,35 +5,17 @@
|
|||
#include <CuTest.h>
|
||||
#include <tests.h>
|
||||
|
||||
static void test_ally(CuTest * tc)
|
||||
static void test_allies_clone(CuTest * tc)
|
||||
{
|
||||
struct ally * al = NULL;
|
||||
struct allies * al = NULL, *ac;
|
||||
struct faction * f;
|
||||
|
||||
test_setup();
|
||||
f = test_create_faction(NULL);
|
||||
ally_set(&al, f, HELP_GUARD);
|
||||
CuAssertPtrNotNull(tc, al);
|
||||
CuAssertIntEquals(tc, HELP_GUARD, ally_get(al, f));
|
||||
|
||||
ally_set(&al, f, 0);
|
||||
CuAssertPtrEquals(tc, NULL, al);
|
||||
CuAssertIntEquals(tc, 0, ally_get(al, f));
|
||||
allies_free(al);
|
||||
test_teardown();
|
||||
}
|
||||
|
||||
static void test_ally_clone(CuTest * tc)
|
||||
{
|
||||
struct ally * al = NULL, *ac;
|
||||
struct faction * f;
|
||||
|
||||
test_setup();
|
||||
f = test_create_faction(NULL);
|
||||
CuAssertPtrEquals(tc, NULL, ally_clone(NULL));
|
||||
CuAssertPtrEquals(tc, NULL, allies_clone(NULL));
|
||||
|
||||
ally_set(&al, f, HELP_GUARD);
|
||||
ac = ally_clone(al);
|
||||
ac = allies_clone(al);
|
||||
CuAssertPtrNotNull(tc, ac);
|
||||
CuAssertTrue(tc, al != ac);
|
||||
CuAssertIntEquals(tc, HELP_GUARD, ally_get(ac, f));
|
||||
|
@ -50,11 +32,11 @@ static void test_allies(CuTest *tc) {
|
|||
test_setup();
|
||||
f = test_create_faction(NULL);
|
||||
|
||||
CuAssertIntEquals(tc, 0, allies_get(al, f));
|
||||
allies_set(&al, f, 42);
|
||||
CuAssertIntEquals(tc, 42, allies_get(al, f));
|
||||
allies_set(&al, f, 0);
|
||||
CuAssertIntEquals(tc, 0, allies_get(al, f));
|
||||
CuAssertIntEquals(tc, 0, ally_get(al, f));
|
||||
ally_set(&al, f, 42);
|
||||
CuAssertIntEquals(tc, 42, ally_get(al, f));
|
||||
ally_set(&al, f, 0);
|
||||
CuAssertIntEquals(tc, 0, ally_get(al, f));
|
||||
CuAssertPtrEquals(tc, NULL, al);
|
||||
test_teardown();
|
||||
}
|
||||
|
@ -62,9 +44,8 @@ static void test_allies(CuTest *tc) {
|
|||
CuSuite *get_ally_suite(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_ally);
|
||||
SUITE_ADD_TEST(suite, test_ally_clone);
|
||||
SUITE_ADD_TEST(suite, test_allies);
|
||||
SUITE_ADD_TEST(suite, test_allies_clone);
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
|
|
@ -86,7 +86,7 @@ extern "C" {
|
|||
int num_people; /* Anzahl Personen ohne Monster */
|
||||
int num_units;
|
||||
int options;
|
||||
struct ally *allies; /* alliedgroup and others should check sf.faction.alive before using a faction from f.allies */
|
||||
struct allies *allies; /* alliedgroup and others should check sf.faction.alive before using a faction from f.allies */
|
||||
struct group *groups; /* alliedgroup and others should check sf.faction.alive before using a faction from f.groups */
|
||||
score_t score;
|
||||
struct alliance *alliance;
|
||||
|
|
|
@ -69,7 +69,7 @@ group *new_group(faction * f, const char *name, int gid)
|
|||
|
||||
static void init_group(faction * f, group * g)
|
||||
{
|
||||
g->allies = ally_clone(f->allies);
|
||||
g->allies = allies_clone(f->allies);
|
||||
}
|
||||
|
||||
static group *find_groupbyname(group * g, const char *name)
|
||||
|
|
|
@ -30,7 +30,7 @@ extern "C" {
|
|||
struct faction *f;
|
||||
struct attrib *attribs;
|
||||
char *name;
|
||||
struct ally *allies;
|
||||
struct allies *allies;
|
||||
int gid;
|
||||
int members;
|
||||
} group;
|
||||
|
|
|
@ -1290,7 +1290,7 @@ void quit(void)
|
|||
int ally_cmd(unit * u, struct order *ord)
|
||||
{
|
||||
char token[128];
|
||||
struct ally **sfp;
|
||||
struct allies **sfp;
|
||||
faction *f;
|
||||
int keyword, not_kw;
|
||||
const char *s;
|
||||
|
|
10
src/report.c
10
src/report.c
|
@ -1511,7 +1511,7 @@ report_template(const char *filename, report_context * ctx, const char *bom)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int count_allies_cb(struct ally *all, faction *af, int status, void *udata) {
|
||||
static int count_allies_cb(struct allies *all, faction *af, int status, void *udata) {
|
||||
int *num = (int *)udata;
|
||||
if (status > 0) {
|
||||
++*num;
|
||||
|
@ -1525,7 +1525,7 @@ struct show_s {
|
|||
int num_allies;
|
||||
};
|
||||
|
||||
static int show_allies_cb(struct ally *all, faction *af, int status, void *udata) {
|
||||
static int show_allies_cb(struct allies *all, faction *af, int status, void *udata) {
|
||||
struct show_s * show = (struct show_s *)udata;
|
||||
const faction * f = show->f;
|
||||
|
||||
|
@ -1585,10 +1585,10 @@ static int show_allies_cb(struct ally *all, faction *af, int status, void *udata
|
|||
}
|
||||
|
||||
static void
|
||||
show_allies(const faction * f, struct ally * allies, char *buf, size_t size)
|
||||
show_allies(const faction * f, struct allies * allies, char *buf, size_t size)
|
||||
{
|
||||
int num_allies = 0;
|
||||
ally_walk(allies, count_allies_cb, &num_allies);
|
||||
allies_walk(allies, count_allies_cb, &num_allies);
|
||||
|
||||
if (num_allies > 0) {
|
||||
struct show_s show;
|
||||
|
@ -1596,7 +1596,7 @@ show_allies(const faction * f, struct ally * allies, char *buf, size_t size)
|
|||
show.num_allies = num_allies;
|
||||
sbs_init(&show.sbs, buf, size);
|
||||
|
||||
ally_walk(allies, show_allies_cb, &show);
|
||||
allies_walk(allies, show_allies_cb, &show);
|
||||
sbs_strcat(&show.sbs, ".");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue