forked from github/server
Merge pull request #47 from badgerman/master
refactoring, tests, old datafile compatibility.
This commit is contained in:
commit
2a77f77745
|
@ -68,6 +68,7 @@ without prior permission by the authors of Eressea.
|
||||||
#include <quicklist.h>
|
#include <quicklist.h>
|
||||||
#include <util/rand.h>
|
#include <util/rand.h>
|
||||||
#include <util/rng.h>
|
#include <util/rng.h>
|
||||||
|
#include <util/xml.h>
|
||||||
|
|
||||||
#include <storage.h>
|
#include <storage.h>
|
||||||
|
|
||||||
|
@ -1001,6 +1002,21 @@ static int tolua_get_spells(lua_State * L)
|
||||||
return tolua_quicklist_push(L, "spell_list", "spell", spells);
|
return tolua_quicklist_push(L, "spell_list", "spell", spells);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int init_data(const char *filename, const char *catalog)
|
||||||
|
{
|
||||||
|
int l;
|
||||||
|
l = read_xml(filename, catalog);
|
||||||
|
reset_locales();
|
||||||
|
if (l) {
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
if (turn < 0) {
|
||||||
|
turn = first_turn;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int tolua_read_xml(lua_State * L)
|
int tolua_read_xml(lua_State * L)
|
||||||
{
|
{
|
||||||
const char *filename = tolua_tostring(L, 1, "config.xml");
|
const char *filename = tolua_tostring(L, 1, "config.xml");
|
||||||
|
|
|
@ -4,36 +4,36 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
ally * ally_find(ally *al, const struct faction *f) {
|
ally * ally_find(ally *al, const struct faction *f) {
|
||||||
for (;al;al=al->next) {
|
for (; al; al = al->next) {
|
||||||
if (al->faction==f) return al;
|
if (al->faction == f) return al;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ally * ally_add(ally **al_p, struct faction *f) {
|
ally * ally_add(ally **al_p, struct faction *f) {
|
||||||
ally * al;
|
ally * al;
|
||||||
while (*al_p) {
|
while (*al_p) {
|
||||||
al = *al_p;
|
al = *al_p;
|
||||||
if (al->faction==f) return al;
|
if (f && al->faction == f) return al;
|
||||||
al_p = &al->next;
|
al_p = &al->next;
|
||||||
}
|
}
|
||||||
al = (ally *)malloc(sizeof(ally));
|
al = (ally *)malloc(sizeof(ally));
|
||||||
al->faction = f;
|
al->faction = f;
|
||||||
al->status = 0;
|
al->status = 0;
|
||||||
al->next = 0;
|
al->next = 0;
|
||||||
*al_p = al;
|
*al_p = al;
|
||||||
return al;
|
return al;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ally_remove(ally **al_p, struct faction *f) {
|
void ally_remove(ally **al_p, struct faction *f) {
|
||||||
ally * al;
|
ally * al;
|
||||||
while (*al_p) {
|
while (*al_p) {
|
||||||
al = *al_p;
|
al = *al_p;
|
||||||
if (al->faction==f) {
|
if (al->faction == f) {
|
||||||
*al_p = al->next;
|
*al_p = al->next;
|
||||||
free(al);
|
free(al);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
al_p = &al->next;
|
||||||
}
|
}
|
||||||
al_p = &al->next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,22 +7,37 @@
|
||||||
|
|
||||||
static void test_ally(CuTest * tc)
|
static void test_ally(CuTest * tc)
|
||||||
{
|
{
|
||||||
ally * al = 0;
|
ally * al = 0;
|
||||||
struct faction * f1 = test_create_faction(0);
|
struct faction * f1 = test_create_faction(0);
|
||||||
|
|
||||||
ally_add(&al, f1);
|
ally_add(&al, f1);
|
||||||
CuAssertPtrNotNull(tc, al);
|
CuAssertPtrNotNull(tc, al);
|
||||||
CuAssertPtrEquals(tc, f1, ally_find(al, f1)->faction);
|
CuAssertPtrEquals(tc, f1, ally_find(al, f1)->faction);
|
||||||
|
|
||||||
ally_remove(&al, f1);
|
ally_remove(&al, f1);
|
||||||
CuAssertPtrEquals(tc, 0, al);
|
CuAssertPtrEquals(tc, 0, al);
|
||||||
CuAssertPtrEquals(tc, 0, ally_find(al, f1));
|
CuAssertPtrEquals(tc, 0, ally_find(al, f1));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_ally_null(CuTest * tc)
|
||||||
|
{
|
||||||
|
ally *a1 = 0, *a2 = 0;
|
||||||
|
|
||||||
|
a1 = ally_add(&a1, 0);
|
||||||
|
a2 = ally_add(&a1, 0);
|
||||||
|
CuAssertPtrNotNull(tc, a1);
|
||||||
|
CuAssertPtrNotNull(tc, a2);
|
||||||
|
CuAssertPtrEquals(tc, a2, a1->next);
|
||||||
|
CuAssertPtrEquals(tc, 0, a2->next);
|
||||||
|
free(a1);
|
||||||
|
free(a2);
|
||||||
}
|
}
|
||||||
|
|
||||||
CuSuite *get_ally_suite(void)
|
CuSuite *get_ally_suite(void)
|
||||||
{
|
{
|
||||||
CuSuite *suite = CuSuiteNew();
|
CuSuite *suite = CuSuiteNew();
|
||||||
SUITE_ADD_TEST(suite, test_ally);
|
SUITE_ADD_TEST(suite, test_ally);
|
||||||
return suite;
|
SUITE_ADD_TEST(suite, test_ally_null);
|
||||||
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -430,10 +430,8 @@ void set_alliance(faction * a, faction * b, int status)
|
||||||
sfp = &sf->next;
|
sfp = &sf->next;
|
||||||
}
|
}
|
||||||
if (*sfp == NULL) {
|
if (*sfp == NULL) {
|
||||||
ally *sf = *sfp = malloc(sizeof(ally));
|
ally *sf = ally_add(sfp, b);
|
||||||
sf->next = NULL;
|
|
||||||
sf->status = status;
|
sf->status = status;
|
||||||
sf->faction = b;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
(*sfp)->status |= status;
|
(*sfp)->status |= status;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
||||||
Katja Zedel <katze@felidae.kn-bremen.de
|
Katja Zedel <katze@felidae.kn-bremen.de
|
||||||
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
||||||
|
|
||||||
Permission to use, copy, modify, and/or distribute this software for any
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -50,203 +50,200 @@ static int maxgid;
|
||||||
|
|
||||||
static group *new_group(faction * f, const char *name, int gid)
|
static group *new_group(faction * f, const char *name, int gid)
|
||||||
{
|
{
|
||||||
group **gp = &f->groups;
|
group **gp = &f->groups;
|
||||||
int index = gid % GMAXHASH;
|
int index = gid % GMAXHASH;
|
||||||
group *g = calloc(sizeof(group), 1);
|
group *g = calloc(sizeof(group), 1);
|
||||||
|
|
||||||
while (*gp)
|
while (*gp)
|
||||||
gp = &(*gp)->next;
|
gp = &(*gp)->next;
|
||||||
*gp = g;
|
*gp = g;
|
||||||
|
|
||||||
maxgid = _max(gid, maxgid);
|
maxgid = _max(gid, maxgid);
|
||||||
g->name = _strdup(name);
|
g->name = _strdup(name);
|
||||||
g->gid = gid;
|
g->gid = gid;
|
||||||
|
|
||||||
g->nexthash = ghash[index];
|
g->nexthash = ghash[index];
|
||||||
return ghash[index] = g;
|
return ghash[index] = g;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_group(faction * f, group * g)
|
static void init_group(faction * f, group * g)
|
||||||
{
|
{
|
||||||
ally *a, **an;
|
ally *a, **an;
|
||||||
|
|
||||||
an = &g->allies;
|
an = &g->allies;
|
||||||
for (a = f->allies; a; a = a->next)
|
for (a = f->allies; a; a = a->next)
|
||||||
if (a->faction) {
|
if (a->faction) {
|
||||||
ally *ga = calloc(sizeof(ally), 1);
|
ally *ga = ally_add(an, a->faction);
|
||||||
*ga = *a;
|
ga->status = a->status;
|
||||||
*an = ga;
|
an = &ga->next;
|
||||||
an = &ga->next;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static group *find_groupbyname(group * g, const char *name)
|
static group *find_groupbyname(group * g, const char *name)
|
||||||
{
|
{
|
||||||
while (g && unicode_utf8_strcasecmp(name, g->name) != 0)
|
while (g && unicode_utf8_strcasecmp(name, g->name) != 0)
|
||||||
g = g->next;
|
g = g->next;
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
static group *find_group(int gid)
|
static group *find_group(int gid)
|
||||||
{
|
{
|
||||||
int index = gid % GMAXHASH;
|
int index = gid % GMAXHASH;
|
||||||
group *g = ghash[index];
|
group *g = ghash[index];
|
||||||
while (g && g->gid != gid)
|
while (g && g->gid != gid)
|
||||||
g = g->nexthash;
|
g = g->nexthash;
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_group(attrib * a, void *owner, struct storage *store)
|
static int read_group(attrib * a, void *owner, struct storage *store)
|
||||||
{
|
{
|
||||||
group *g;
|
group *g;
|
||||||
int gid;
|
int gid;
|
||||||
|
|
||||||
READ_INT(store, &gid);
|
READ_INT(store, &gid);
|
||||||
a->data.v = g = find_group(gid);
|
a->data.v = g = find_group(gid);
|
||||||
if (g != 0) {
|
if (g != 0) {
|
||||||
g->members++;
|
g->members++;
|
||||||
return AT_READ_OK;
|
return AT_READ_OK;
|
||||||
}
|
}
|
||||||
return AT_READ_FAIL;
|
return AT_READ_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
write_group(const attrib * a, const void *owner, struct storage *store)
|
write_group(const attrib * a, const void *owner, struct storage *store)
|
||||||
{
|
{
|
||||||
group *g = (group *) a->data.v;
|
group *g = (group *)a->data.v;
|
||||||
WRITE_INT(store, g->gid);
|
WRITE_INT(store, g->gid);
|
||||||
}
|
}
|
||||||
|
|
||||||
attrib_type at_group = { /* attribute for units assigned to a group */
|
attrib_type at_group = { /* attribute for units assigned to a group */
|
||||||
"grp",
|
"grp",
|
||||||
DEFAULT_INIT,
|
DEFAULT_INIT,
|
||||||
DEFAULT_FINALIZE, DEFAULT_AGE, write_group, read_group, ATF_UNIQUE};
|
DEFAULT_FINALIZE, DEFAULT_AGE, write_group, read_group, ATF_UNIQUE };
|
||||||
|
|
||||||
void free_group(group * g)
|
void free_group(group * g)
|
||||||
{
|
{
|
||||||
int index = g->gid % GMAXHASH;
|
int index = g->gid % GMAXHASH;
|
||||||
group **g_ptr = ghash + index;
|
group **g_ptr = ghash + index;
|
||||||
while (*g_ptr && (*g_ptr)->gid != g->gid)
|
while (*g_ptr && (*g_ptr)->gid != g->gid)
|
||||||
g_ptr = &(*g_ptr)->nexthash;
|
g_ptr = &(*g_ptr)->nexthash;
|
||||||
assert(*g_ptr == g);
|
assert(*g_ptr == g);
|
||||||
*g_ptr = g->nexthash;
|
*g_ptr = g->nexthash;
|
||||||
|
|
||||||
while (g->allies) {
|
while (g->allies) {
|
||||||
ally *a = g->allies;
|
ally *a = g->allies;
|
||||||
g->allies = a->next;
|
g->allies = a->next;
|
||||||
free(a);
|
free(a);
|
||||||
}
|
}
|
||||||
free(g->name);
|
free(g->name);
|
||||||
free(g);
|
free(g);
|
||||||
}
|
}
|
||||||
|
|
||||||
group * get_group(const struct unit *u)
|
group * get_group(const struct unit *u)
|
||||||
{
|
{
|
||||||
if (fval(u, UFL_GROUP)) {
|
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) {
|
||||||
return (group *) a->data.v;
|
return (group *)a->data.v;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_group(struct unit *u, struct group *g)
|
void set_group(struct unit *u, struct group *g)
|
||||||
{
|
{
|
||||||
attrib *a = NULL;
|
attrib *a = NULL;
|
||||||
|
|
||||||
if (fval(u, UFL_GROUP)) {
|
if (fval(u, UFL_GROUP)) {
|
||||||
a = a_find(u->attribs, &at_group);
|
a = a_find(u->attribs, &at_group);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a) {
|
if (a) {
|
||||||
group *og = (group *) a->data.v;
|
group *og = (group *)a->data.v;
|
||||||
if (og == g)
|
if (og == g)
|
||||||
return;
|
return;
|
||||||
--og->members;
|
--og->members;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g) {
|
if (g) {
|
||||||
if (!a) {
|
if (!a) {
|
||||||
a = a_add(&u->attribs, a_new(&at_group));
|
a = a_add(&u->attribs, a_new(&at_group));
|
||||||
fset(u, UFL_GROUP);
|
fset(u, UFL_GROUP);
|
||||||
|
}
|
||||||
|
a->data.v = g;
|
||||||
|
g->members++;
|
||||||
|
}
|
||||||
|
else if (a) {
|
||||||
|
a_remove(&u->attribs, a);
|
||||||
|
freset(u, UFL_GROUP);
|
||||||
}
|
}
|
||||||
a->data.v = g;
|
|
||||||
g->members++;
|
|
||||||
} else if (a) {
|
|
||||||
a_remove(&u->attribs, a);
|
|
||||||
freset(u, UFL_GROUP);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool join_group(unit * u, const char *name)
|
bool join_group(unit * u, const char *name)
|
||||||
{
|
{
|
||||||
group *g = NULL;
|
group *g = NULL;
|
||||||
|
|
||||||
if (name && name[0]) {
|
if (name && name[0]) {
|
||||||
g = find_groupbyname(u->faction->groups, name);
|
g = find_groupbyname(u->faction->groups, name);
|
||||||
if (g == NULL) {
|
if (g == NULL) {
|
||||||
g = new_group(u->faction, name, ++maxgid);
|
g = new_group(u->faction, name, ++maxgid);
|
||||||
init_group(u->faction, g);
|
init_group(u->faction, g);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
set_group(u, g);
|
set_group(u, g);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_groups(struct storage *store, group * g)
|
void write_groups(struct storage *store, group * g)
|
||||||
{
|
{
|
||||||
while (g) {
|
while (g) {
|
||||||
ally *a;
|
ally *a;
|
||||||
WRITE_INT(store, g->gid);
|
WRITE_INT(store, g->gid);
|
||||||
WRITE_STR(store, g->name);
|
WRITE_STR(store, g->name);
|
||||||
for (a = g->allies; a; a = a->next) {
|
for (a = g->allies; a; a = a->next) {
|
||||||
if (a->faction) {
|
if (a->faction) {
|
||||||
write_faction_reference(a->faction, store);
|
write_faction_reference(a->faction, store);
|
||||||
WRITE_INT(store, a->status);
|
WRITE_INT(store, a->status);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
WRITE_INT(store, 0);
|
||||||
|
a_write(store, g->attribs, g);
|
||||||
|
WRITE_SECTION(store);
|
||||||
|
g = g->next;
|
||||||
}
|
}
|
||||||
WRITE_INT(store, 0);
|
WRITE_INT(store, 0);
|
||||||
a_write(store, g->attribs, g);
|
|
||||||
WRITE_SECTION(store);
|
|
||||||
g = g->next;
|
|
||||||
}
|
|
||||||
WRITE_INT(store, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_groups(struct storage *store, faction * f)
|
void read_groups(struct storage *store, faction * f)
|
||||||
{
|
{
|
||||||
for (;;) {
|
|
||||||
ally **pa;
|
|
||||||
group *g;
|
|
||||||
int gid;
|
|
||||||
char buf[1024];
|
|
||||||
|
|
||||||
READ_INT(store, &gid);
|
|
||||||
if (gid == 0)
|
|
||||||
break;
|
|
||||||
READ_STR(store, buf, sizeof(buf));
|
|
||||||
g = new_group(f, buf, gid);
|
|
||||||
pa = &g->allies;
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ally *a;
|
ally **pa;
|
||||||
variant fid;
|
group *g;
|
||||||
READ_INT(store, &fid.i);
|
int gid;
|
||||||
if (fid.i <= 0)
|
char buf[1024];
|
||||||
break;
|
|
||||||
if (global.data_version < STORAGE_VERSION && fid.i == 0)
|
|
||||||
break;
|
|
||||||
a = malloc(sizeof(ally));
|
|
||||||
*pa = a;
|
|
||||||
pa = &a->next;
|
|
||||||
READ_INT(store, &a->status);
|
|
||||||
|
|
||||||
a->faction = findfaction(fid.i);
|
READ_INT(store, &gid);
|
||||||
if (!a->faction)
|
if (gid == 0)
|
||||||
ur_add(fid, &a->faction, resolve_faction);
|
break;
|
||||||
|
READ_STR(store, buf, sizeof(buf));
|
||||||
|
g = new_group(f, buf, gid);
|
||||||
|
pa = &g->allies;
|
||||||
|
for (;;) {
|
||||||
|
ally *a;
|
||||||
|
variant fid;
|
||||||
|
|
||||||
|
READ_INT(store, &fid.i);
|
||||||
|
if (fid.i <= 0)
|
||||||
|
break;
|
||||||
|
if (global.data_version < STORAGE_VERSION && fid.i == 0)
|
||||||
|
break;
|
||||||
|
a = ally_add(pa, findfaction(fid.i));
|
||||||
|
READ_INT(store, &a->status);
|
||||||
|
if (!a->faction)
|
||||||
|
ur_add(fid, &a->faction, resolve_faction);
|
||||||
|
}
|
||||||
|
*pa = 0;
|
||||||
|
a_read(store, &g->attribs, g);
|
||||||
}
|
}
|
||||||
*pa = 0;
|
|
||||||
a_read(store, &g->attribs, g);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,7 +167,7 @@ int count)
|
||||||
|
|
||||||
if ((mode & GET_SLACK) && (mode & GET_RESERVE))
|
if ((mode & GET_SLACK) && (mode & GET_RESERVE))
|
||||||
use = have;
|
use = have;
|
||||||
else {
|
else if (mode & (GET_SLACK|GET_RESERVE)) {
|
||||||
int reserve = get_reservation(u, rtype);
|
int reserve = get_reservation(u, rtype);
|
||||||
int slack = _max(0, have - reserve);
|
int slack = _max(0, have - reserve);
|
||||||
if (mode & GET_RESERVE)
|
if (mode & GET_RESERVE)
|
||||||
|
@ -177,11 +177,9 @@ int count)
|
||||||
}
|
}
|
||||||
if (rtype->flags & RTF_POOLED && mode & ~(GET_SLACK | GET_RESERVE)) {
|
if (rtype->flags & RTF_POOLED && mode & ~(GET_SLACK | GET_RESERVE)) {
|
||||||
for (v = r->units; v && use < count; v = v->next)
|
for (v = r->units; v && use < count; v = v->next)
|
||||||
if (u != v) {
|
if (u != v && (u->items || rtype->uget)) {
|
||||||
int mask;
|
int mask;
|
||||||
|
|
||||||
if (v->items == NULL && rtype->uget == NULL)
|
|
||||||
continue;
|
|
||||||
if ((urace(v)->ec_flags & GIVEITEM) == 0)
|
if ((urace(v)->ec_flags & GIVEITEM) == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
|
@ -29,8 +29,8 @@ extern "C" {
|
||||||
#define GET_POOLED_SLACK 0x08
|
#define GET_POOLED_SLACK 0x08
|
||||||
#define GET_POOLED_RESERVE 0x10
|
#define GET_POOLED_RESERVE 0x10
|
||||||
#define GET_POOLED_FORCE 0x20 /* ignore f->options pools */
|
#define GET_POOLED_FORCE 0x20 /* ignore f->options pools */
|
||||||
#define GET_ALLIED_SLACK 0x30
|
#define GET_ALLIED_SLACK 0x40
|
||||||
#define GET_ALLIED_RESERVE 0x40
|
#define GET_ALLIED_RESERVE 0x80
|
||||||
|
|
||||||
/* for convenience: */
|
/* for convenience: */
|
||||||
#define GET_DEFAULT (GET_RESERVE|GET_SLACK|GET_POOLED_SLACK)
|
#define GET_DEFAULT (GET_RESERVE|GET_SLACK|GET_POOLED_SLACK)
|
||||||
|
|
|
@ -1,47 +1,96 @@
|
||||||
#include <platform.h>
|
#include <platform.h>
|
||||||
#include <kernel/types.h>
|
#include <kernel/types.h>
|
||||||
|
|
||||||
|
#include "ally.h"
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
#include "magic.h"
|
#include "magic.h"
|
||||||
#include "unit.h"
|
#include "unit.h"
|
||||||
#include "item.h"
|
#include "item.h"
|
||||||
|
#include "faction.h"
|
||||||
#include "region.h"
|
#include "region.h"
|
||||||
#include "skill.h"
|
#include "skill.h"
|
||||||
|
|
||||||
#include <CuTest.h>
|
#include <CuTest.h>
|
||||||
#include <tests.h>
|
#include <tests.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
void test_pool(CuTest *tc) {
|
||||||
|
unit *u1, *u2, *u3;
|
||||||
|
faction *f;
|
||||||
|
region *r;
|
||||||
|
struct resource_type *rtype;
|
||||||
|
ally *al;
|
||||||
|
|
||||||
|
test_cleanup();
|
||||||
|
test_create_world();
|
||||||
|
rtype = rt_get_or_create("money");
|
||||||
|
it_get_or_create(rtype);
|
||||||
|
f = test_create_faction(0);
|
||||||
|
r = findregion(0, 0);
|
||||||
|
assert(r && f && rtype && rtype->itype);
|
||||||
|
u1 = test_create_unit(f, r);
|
||||||
|
u2 = test_create_unit(f, r);
|
||||||
|
u3 = test_create_unit(test_create_faction(0), r);
|
||||||
|
assert(u1 && u2);
|
||||||
|
i_change(&u1->items, rtype->itype, 100);
|
||||||
|
set_resvalue(u1, rtype, 50);
|
||||||
|
i_change(&u2->items, rtype->itype, 200);
|
||||||
|
set_resvalue(u2, rtype, 100);
|
||||||
|
i_change(&u3->items, rtype->itype, 400);
|
||||||
|
set_resvalue(u3, rtype, 200);
|
||||||
|
|
||||||
|
CuAssertIntEquals(tc, 50, get_pooled(u1, rtype, GET_SLACK, 40));
|
||||||
|
CuAssertIntEquals(tc, 50, get_pooled(u1, rtype, GET_SLACK, INT_MAX));
|
||||||
|
CuAssertIntEquals(tc, 100, get_pooled(u1, rtype, GET_SLACK | GET_RESERVE, INT_MAX));
|
||||||
|
CuAssertIntEquals(tc, 150, get_pooled(u1, rtype, GET_SLACK | GET_POOLED_SLACK, INT_MAX));
|
||||||
|
CuAssertIntEquals(tc, 100, get_pooled(u1, rtype, GET_POOLED_SLACK, INT_MAX));
|
||||||
|
CuAssertIntEquals(tc, 200, get_pooled(u1, rtype, GET_POOLED_SLACK | GET_POOLED_RESERVE, INT_MAX));
|
||||||
|
|
||||||
|
al = ally_add(&u3->faction->allies, f);
|
||||||
|
al->status = HELP_GUARD;
|
||||||
|
CuAssertIntEquals(tc, 0, get_pooled(u1, rtype, GET_ALLIED_SLACK | GET_ALLIED_RESERVE, INT_MAX));
|
||||||
|
al->status = HELP_MONEY;
|
||||||
|
CuAssertIntEquals(tc, 200, get_pooled(u1, rtype, GET_ALLIED_SLACK, INT_MAX));
|
||||||
|
CuAssertIntEquals(tc, 200, get_pooled(u1, rtype, GET_ALLIED_RESERVE, INT_MAX));
|
||||||
|
CuAssertIntEquals(tc, 400, get_pooled(u1, rtype, GET_ALLIED_SLACK | GET_ALLIED_RESERVE, INT_MAX));
|
||||||
|
|
||||||
|
CuAssertIntEquals(tc, 100, get_pooled(u1, rtype, GET_ALL, 50));
|
||||||
|
CuAssertIntEquals(tc, 300, get_pooled(u1, rtype, GET_ALL, 150));
|
||||||
|
CuAssertIntEquals(tc, 300, get_pooled(u1, rtype, GET_ALL, INT_MAX));
|
||||||
|
}
|
||||||
|
|
||||||
void test_change_resource(CuTest * tc)
|
void test_change_resource(CuTest * tc)
|
||||||
{
|
{
|
||||||
struct unit * u;
|
struct unit * u;
|
||||||
struct faction * f;
|
struct faction * f;
|
||||||
struct region * r;
|
struct region * r;
|
||||||
const char * names[] = { "money", "aura", "permaura", "horse", "hp", 0 };
|
const char * names[] = { "money", "aura", "permaura", "horse", "hp", 0 };
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
test_create_world();
|
test_create_world();
|
||||||
enable_skill(SK_MAGIC, true);
|
enable_skill(SK_MAGIC, true);
|
||||||
|
|
||||||
r = findregion(0, 0);
|
r = findregion(0, 0);
|
||||||
f = test_create_faction(0);
|
f = test_create_faction(0);
|
||||||
u = test_create_unit(f, r);
|
u = test_create_unit(f, r);
|
||||||
CuAssertPtrNotNull(tc, u);
|
CuAssertPtrNotNull(tc, u);
|
||||||
set_level(u, SK_MAGIC, 5);
|
set_level(u, SK_MAGIC, 5);
|
||||||
create_mage(u, M_DRAIG);
|
create_mage(u, M_DRAIG);
|
||||||
|
|
||||||
for (i=0;names[i];++i) {
|
for (i = 0; names[i]; ++i) {
|
||||||
const struct resource_type *rtype = rt_find(names[i]);
|
const struct resource_type *rtype = rt_find(names[i]);
|
||||||
int have = get_resource(u, rtype);
|
int have = get_resource(u, rtype);
|
||||||
CuAssertIntEquals(tc, have+1, change_resource(u, rtype, 1));
|
CuAssertIntEquals(tc, have + 1, change_resource(u, rtype, 1));
|
||||||
CuAssertIntEquals(tc, have+1, get_resource(u, rtype));
|
CuAssertIntEquals(tc, have + 1, get_resource(u, rtype));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CuSuite *get_pool_suite(void)
|
CuSuite *get_pool_suite(void)
|
||||||
{
|
{
|
||||||
CuSuite *suite = CuSuiteNew();
|
CuSuite *suite = CuSuiteNew();
|
||||||
/* SUITE_ADD_TEST(suite, test_pool); */
|
SUITE_ADD_TEST(suite, test_pool);
|
||||||
SUITE_ADD_TEST(suite, test_change_resource);
|
SUITE_ADD_TEST(suite, test_change_resource);
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1119,8 +1119,11 @@ static ally **addally(const faction * f, ally ** sfp, int aid, int state)
|
||||||
if (state == 0)
|
if (state == 0)
|
||||||
return sfp;
|
return sfp;
|
||||||
|
|
||||||
sf = calloc(1, sizeof(ally));
|
while (*sfp) {
|
||||||
sf->faction = af;
|
sfp = &(*sfp)->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
sf = ally_add(sfp, af);
|
||||||
if (!sf->faction) {
|
if (!sf->faction) {
|
||||||
variant id;
|
variant id;
|
||||||
id.i = aid;
|
id.i = aid;
|
||||||
|
@ -1128,9 +1131,6 @@ static ally **addally(const faction * f, ally ** sfp, int aid, int state)
|
||||||
}
|
}
|
||||||
sf->status = state & HELP_ALL;
|
sf->status = state & HELP_ALL;
|
||||||
|
|
||||||
while (*sfp)
|
|
||||||
sfp = &(*sfp)->next;
|
|
||||||
*sfp = sf;
|
|
||||||
return &sf->next;
|
return &sf->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1161,10 +1161,13 @@ skill *add_skill(unit * u, skill_t id)
|
||||||
++u->skill_size;
|
++u->skill_size;
|
||||||
u->skills = realloc(u->skills, u->skill_size * sizeof(skill));
|
u->skills = realloc(u->skills, u->skill_size * sizeof(skill));
|
||||||
sv = (u->skills + u->skill_size - 1);
|
sv = (u->skills + u->skill_size - 1);
|
||||||
sv->level = (unsigned char)0;
|
sv->level = 0;
|
||||||
sv->weeks = (unsigned char)1;
|
sv->weeks = 1;
|
||||||
sv->old = (unsigned char)0;
|
sv->old = 0;
|
||||||
sv->id = (unsigned char)id;
|
sv->id = id;
|
||||||
|
if (id == SK_MAGIC && u->faction) {
|
||||||
|
assert(max_magicians(u->faction) >= u->number);
|
||||||
|
}
|
||||||
return sv;
|
return sv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
src/laws.c
19
src/laws.c
|
@ -83,7 +83,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
#include <util/umlaut.h>
|
#include <util/umlaut.h>
|
||||||
#include <util/message.h>
|
#include <util/message.h>
|
||||||
#include <util/rng.h>
|
#include <util/rng.h>
|
||||||
#include <util/xml.h>
|
|
||||||
|
|
||||||
#include <attributes/otherfaction.h>
|
#include <attributes/otherfaction.h>
|
||||||
|
|
||||||
|
@ -1349,10 +1348,8 @@ int ally_cmd(unit * u, struct order *ord)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
sf = calloc(1, sizeof(ally));
|
sf = ally_add(sfp, f);
|
||||||
sf->faction = f;
|
|
||||||
sf->status = 0;
|
sf->status = 0;
|
||||||
addlist(sfp, sf);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (keyword) {
|
switch (keyword) {
|
||||||
|
@ -4557,17 +4554,3 @@ void update_subscriptions(void)
|
||||||
}
|
}
|
||||||
fclose(F);
|
fclose(F);
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_data(const char *filename, const char *catalog)
|
|
||||||
{
|
|
||||||
int l;
|
|
||||||
l = read_xml(filename, catalog);
|
|
||||||
reset_locales();
|
|
||||||
if (l) {
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
if (turn < 0) {
|
|
||||||
turn = first_turn;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
|
@ -24,13 +24,10 @@ extern "C" {
|
||||||
|
|
||||||
extern int writepasswd(void);
|
extern int writepasswd(void);
|
||||||
void demographics(void);
|
void demographics(void);
|
||||||
void last_orders(void);
|
|
||||||
void find_address(void);
|
|
||||||
void update_guards(void);
|
void update_guards(void);
|
||||||
void update_subscriptions(void);
|
void update_subscriptions(void);
|
||||||
void deliverMail(struct faction *f, struct region *r, struct unit *u,
|
void deliverMail(struct faction *f, struct region *r, struct unit *u,
|
||||||
const char *s, struct unit *receiver);
|
const char *s, struct unit *receiver);
|
||||||
int init_data(const char *filename, const char *catalog);
|
|
||||||
|
|
||||||
bool renamed_building(const struct building * b);
|
bool renamed_building(const struct building * b);
|
||||||
int rename_building(struct unit * u, struct order * ord, struct building * b, const char *name);
|
int rename_building(struct unit * u, struct order * ord, struct building * b, const char *name);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <kernel/types.h>
|
#include <kernel/types.h>
|
||||||
#include "laws.h"
|
#include "laws.h"
|
||||||
|
|
||||||
|
#include <kernel/ally.h>
|
||||||
#include <kernel/config.h>
|
#include <kernel/config.h>
|
||||||
#include <kernel/building.h>
|
#include <kernel/building.h>
|
||||||
#include <kernel/faction.h>
|
#include <kernel/faction.h>
|
||||||
|
@ -57,14 +58,15 @@ static void test_rename_building(CuTest * tc)
|
||||||
|
|
||||||
rename_building(u, NULL, b, "Villa Nagel");
|
rename_building(u, NULL, b, "Villa Nagel");
|
||||||
CuAssertStrEquals(tc, "Villa Nagel", b->name);
|
CuAssertStrEquals(tc, "Villa Nagel", b->name);
|
||||||
|
CuAssertTrue(tc, renamed_building(b));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_rename_building_twice(CuTest * tc)
|
static void test_rename_building_twice(CuTest * tc)
|
||||||
{
|
{
|
||||||
region *r;
|
region *r;
|
||||||
building *b;
|
|
||||||
unit *u;
|
unit *u;
|
||||||
faction *f;
|
faction *f;
|
||||||
|
building *b;
|
||||||
building_type *btype;
|
building_type *btype;
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
|
@ -85,6 +87,37 @@ static void test_rename_building_twice(CuTest * tc)
|
||||||
CuAssertStrEquals(tc, "Villa Kunterbunt", b->name);
|
CuAssertStrEquals(tc, "Villa Kunterbunt", b->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_contact(CuTest * tc)
|
||||||
|
{
|
||||||
|
region *r;
|
||||||
|
unit *u1, *u2, *u3;
|
||||||
|
building *b;
|
||||||
|
building_type *btype;
|
||||||
|
ally *al;
|
||||||
|
|
||||||
|
test_cleanup();
|
||||||
|
test_create_world();
|
||||||
|
|
||||||
|
btype = bt_get_or_create("castle");
|
||||||
|
r = findregion(0, 0);
|
||||||
|
b = new_building(btype, r, default_locale);
|
||||||
|
u1 = test_create_unit(test_create_faction(0), r);
|
||||||
|
u2 = test_create_unit(test_create_faction(0), r);
|
||||||
|
u3 = test_create_unit(test_create_faction(0), r);
|
||||||
|
set_level(u3, SK_PERCEPTION, 2);
|
||||||
|
usetsiege(u3, b);
|
||||||
|
b->besieged = 1;
|
||||||
|
CuAssertIntEquals(tc, 1, can_contact(r, u1, u2));
|
||||||
|
|
||||||
|
u_set_building(u1, b);
|
||||||
|
CuAssertIntEquals(tc, 0, can_contact(r, u1, u2));
|
||||||
|
al = ally_add(&u1->faction->allies, u2->faction);
|
||||||
|
al->status = HELP_ALL;
|
||||||
|
CuAssertIntEquals(tc, HELP_GIVE, can_contact(r, u1, u2));
|
||||||
|
u_set_building(u2, b);
|
||||||
|
CuAssertIntEquals(tc, 1, can_contact(r, u1, u2));
|
||||||
|
}
|
||||||
|
|
||||||
static void test_fishing_feeds_2_people(CuTest * tc)
|
static void test_fishing_feeds_2_people(CuTest * tc)
|
||||||
{
|
{
|
||||||
const resource_type *rtype;
|
const resource_type *rtype;
|
||||||
|
@ -412,5 +445,6 @@ CuSuite *get_laws_suite(void)
|
||||||
SUITE_ADD_TEST(suite, test_reserve_cmd);
|
SUITE_ADD_TEST(suite, test_reserve_cmd);
|
||||||
SUITE_ADD_TEST(suite, test_new_units);
|
SUITE_ADD_TEST(suite, test_new_units);
|
||||||
SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit);
|
SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit);
|
||||||
|
SUITE_ADD_TEST(suite, test_contact);
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,9 +45,9 @@
|
||||||
|
|
||||||
static int read_permissions(attrib * a, void *owner, struct storage *store)
|
static int read_permissions(attrib * a, void *owner, struct storage *store)
|
||||||
{
|
{
|
||||||
attrib *attr = NULL;
|
assert(!a);
|
||||||
a_read(store, &attr, owner);
|
a_read(store, &a, owner);
|
||||||
a_remove(&attr, a);
|
a_remove(&a, a);
|
||||||
return AT_READ_OK;
|
return AT_READ_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1183,7 +1183,7 @@ link_seen(seen_region * seehash[], const region * first, const region * last)
|
||||||
}
|
}
|
||||||
r = r->next;
|
r = r->next;
|
||||||
}
|
}
|
||||||
sr->next = 0;
|
if (sr) sr->next = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
seen_region *find_seen(struct seen_region *seehash[], const region * r)
|
seen_region *find_seen(struct seen_region *seehash[], const region * r)
|
||||||
|
|
Loading…
Reference in New Issue