introduce an rcoption struct.

used for pretty rare options that have no property in the race struct.
This commit is contained in:
Enno Rehling 2017-02-06 09:03:08 +01:00
parent de10a8ad65
commit 998dcffab2
5 changed files with 75 additions and 12 deletions

2
clibs

@ -1 +1 @@
Subproject commit f91ef37f08c5244bf616f1836c0aa9caaf36805c
Subproject commit 27c8b3202b52766465743c3324fc0b52c5ba4b11

2
cmake

@ -1 +1 @@
Subproject commit f1fb3943ace59994d90d71a891b80033dc2700a2
Subproject commit d88983c7ff4bc3a4884a7c3f74e8190bac5eab23

View File

@ -558,20 +558,12 @@ void faction_setpassword(faction * f, const char *pwhash)
f->_password = strdup(pwhash);
}
const race *other_race(const race *rc) {
if (rc->parameters) {
const char *str = get_param(rc->parameters, "other_race");
return str ? rc_find(str) : NULL;
}
return NULL;
}
bool valid_race(const struct faction *f, const struct race *rc)
{
if (f->race == rc)
return true;
else {
return other_race(f->race) == rc;
return rc_otherrace(f->race) == rc;
}
}

View File

@ -44,6 +44,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/language.h>
#include <util/log.h>
#include <util/rng.h>
#include <util/variant.h>
#include <storage.h>
@ -77,6 +78,59 @@ static const char *racenames[MAXRACES] = {
"clone"
};
#define MAXOPTIONS 4
typedef struct rcoption {
unsigned char key[MAXOPTIONS];
variant value[MAXOPTIONS];
} rcoption;
enum {
RCO_NONE,
RCO_SCARE,
RCO_OTHER
};
static void rc_setoption(race *rc, int key, const char *value) {
int i;
variant *v = NULL;
if (!rc->options) {
rc->options = malloc(sizeof(rcoption));
rc->options->key[0] = key;
rc->options->key[1] = RCO_NONE;
v = rc->options->value;
} else {
for (i=0;!v && i < MAXOPTIONS && rc->options->key[i]!=RCO_NONE;++i) {
if (rc->options->key[i]==key) {
v = rc->options->value+i;
}
}
if (!v) {
assert(i<MAXOPTIONS || !"MAXOPTIONS too small for race");
v = rc->options->value+i;
rc->options->key[i] = key;
}
}
assert(v);
if (key == RCO_SCARE) {
v->i = atoi(value);
}
else if (key == RCO_OTHER) {
v->v = rc_get_or_create(value);
}
}
static variant *rc_getoption(const race *rc, int key) {
if (rc->options) {
int i;
for (i=0;i!=MAXOPTIONS && rc->options->key[i]!=RCO_NONE;++i) {
if (rc->options->key[i]==key) {
return rc->options->value+i;
}
}
}
return NULL;
}
const struct race *findrace(const char *s, const struct locale *lang)
{
void **tokens = get_translations(lang, UT_RACES);
@ -297,7 +351,14 @@ int rc_armor_bonus(const race *rc)
int rc_scare(const struct race *rc)
{
return get_param_int(rc->parameters, "ai.scare", 0);
variant *v = rc_getoption(rc, RCO_SCARE);
return v ? v->i : 0;
}
const race *rc_otherrace(const race *rc)
{
variant *v = rc_getoption(rc, RCO_OTHER);
return v ? (const race *)v->v : NULL;
}
int rc_migrants_formula(const race *rc)
@ -314,6 +375,12 @@ void rc_set_param(struct race *rc, const char *key, const char *value) {
rc->flags |= RCF_MIGRANTS;
}
}
else if (strcmp(key, "other_race")==0) {
rc_setoption(rc, RCO_OTHER, value);
}
else if (strcmp(key, "ai.scare")==0) {
rc_setoption(rc, RCO_SCARE, value);
}
else {
set_param(&rc->parameters, key, value);
}

View File

@ -47,6 +47,7 @@ extern "C" {
struct spell;
struct spellref;
struct locale;
struct rcoption;
extern int num_races;
@ -153,6 +154,8 @@ extern "C" {
struct item *(*itemdrop) (const struct race *, int size);
void(*init_familiar) (struct unit *);
struct rcoption *options; // rarely used properties
const struct race *familiars[MAXMAGIETYP];
struct race *next;
} race;
@ -189,6 +192,7 @@ extern "C" {
double rc_maxaura(const struct race *rc);
int rc_armor_bonus(const struct race *rc);
int rc_scare(const struct race *rc);
const race *rc_otherrace(const race *rc);
#define MIGRANTS_NONE 0
#define MIGRANTS_LOG10 1