make old_race a little bit less awful.

This commit is contained in:
Enno Rehling 2016-10-04 10:34:18 +02:00
parent 3338f00fcb
commit 61f76e6722
3 changed files with 60 additions and 10 deletions

View File

@ -137,16 +137,6 @@ int NMRTimeout(void)
return config_get_int("nmr.timeout", 0); return config_get_int("nmr.timeout", 0);
} }
race_t old_race(const struct race * rc)
{
race_t i;
// TODO: this sucks so bad!
for (i = 0; i != MAXRACES; ++i) {
if (get_race(i) == rc) return i;
}
return NORACE;
}
helpmode helpmodes[] = { helpmode helpmodes[] = {
{ "all", HELP_ALL } { "all", HELP_ALL }
, ,

View File

@ -86,6 +86,50 @@ const struct race *get_race(race_t rt) {
return rc_find(name); return rc_find(name);
} }
typedef struct xref {
race_t id;
const race *rc;
} rc_xref;
int cmp_xref(const void *a, const void *b)
{
const rc_xref *l = (const rc_xref *)a;
const rc_xref *r = (const rc_xref *)b;
if (l->rc<r->rc) return -1;
if (l->rc>r->rc) return 1;
return 0;
}
race_t old_race(const struct race * rc)
{
static int cache;
static rc_xref *xrefs;
int i, l, r;
if (rc_changed(&cache)) {
if (!xrefs) {
xrefs = malloc(sizeof(rc_xref) * MAXRACES);
}
for (i = 0; i != MAXRACES; ++i) {
xrefs[i].rc = get_race(i);
xrefs[i].id = (race_t)i;
}
qsort(xrefs, MAXRACES, sizeof(rc_xref), cmp_xref);
}
l=0; r=MAXRACES-1;
while (l<=r) {
int m = (l+r)/2;
if (rc<xrefs[m].rc) {
r = m-1;
} else if (rc>xrefs[m].rc) {
l = m+1;
} else {
return (race_t)xrefs[m].id;
}
}
return NORACE;
}
race_list *get_familiarraces(void) race_list *get_familiarraces(void)
{ {
static int init = 0; static int init = 0;

View File

@ -66,10 +66,26 @@ static void test_race_get(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_old_race(CuTest *tc)
{
race * rc1, *rc2;
test_setup();
test_create_race("dwarf");
rc1 = test_create_race("elf");
rc2 = test_create_race("onkel");
CuAssertIntEquals(tc, RC_ELF, old_race(rc1));
CuAssertIntEquals(tc, NORACE, old_race(rc2));
rc2 = test_create_race("human");
CuAssertIntEquals(tc, RC_ELF, old_race(rc1));
CuAssertIntEquals(tc, RC_HUMAN, old_race(rc2));
test_cleanup();
}
CuSuite *get_race_suite(void) CuSuite *get_race_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_race_get); SUITE_ADD_TEST(suite, test_race_get);
SUITE_ADD_TEST(suite, test_old_race);
SUITE_ADD_TEST(suite, test_rc_name); SUITE_ADD_TEST(suite, test_rc_name);
SUITE_ADD_TEST(suite, test_rc_defaults); SUITE_ADD_TEST(suite, test_rc_defaults);
SUITE_ADD_TEST(suite, test_rc_find); SUITE_ADD_TEST(suite, test_rc_find);