I am now calling get_race far too often. Fix this a bit.

This commit is contained in:
Enno Rehling 2016-09-20 20:27:41 +02:00
parent 69b420ae2f
commit a3ad9a0222
15 changed files with 73 additions and 48 deletions

View File

@ -1039,7 +1039,7 @@ static int rc_specialdamage(const unit *au, const unit *du, const struct weapon_
if (wtype != NULL) {
if (fval(u_race(du), RCF_DRAGON)) {
static int cache;
static race *rc_halfling;
static const race *rc_halfling;
if (rc_changed(&cache)) {
rc_halfling = get_race(RC_HALFLING);
}

View File

@ -1480,6 +1480,8 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
const char *mailto = LOC(f->locale, "mailto");
const attrib *a;
FILE *F = fopen(filename, "w");
static const race *rc_human;
static int rc_cache;
if (era < 0) {
era = config_get_int("world.era", 1);
@ -1546,7 +1548,10 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
fprintf(F, "%d;Anzahl Personen\n", count_all(f));
fprintf(F, "\"%s\";Magiegebiet\n", magic_school[f->magiegebiet]);
if (f->race == get_race(RC_HUMAN)) {
if (rc_changed(&rc_cache)) {
rc_human = rc_find("human");
}
if (f->race == rc_human) {
fprintf(F, "%d;Anzahl Immigranten\n", count_migrants(f));
fprintf(F, "%d;Max. Immigranten\n", count_maxmigrants(f));
}

View File

@ -69,7 +69,7 @@ unsigned int guard_flags(const unit * u)
{
// TODO: this should be a property of the race, like race.guard_flags
static int rc_cache;
static race *rc_elf, *rc_ent, *rc_ironkeeper;
static const race *rc_elf, *rc_ent, *rc_ironkeeper;
const race *rc = u_race(u);
unsigned int flags =
GUARD_CREWS | GUARD_LANDING | GUARD_TRAVELTHRU | GUARD_TAX;

View File

@ -301,7 +301,7 @@ unit *addplayer(region * r, faction * f)
fset(u, UFL_ISNEW);
if (f->race == get_race(RC_DAEMON)) {
race_t urc;
race *rc;
const race *rc;
do {
urc = (race_t)(rng_int() % MAXRACES);
rc = get_race(urc);

View File

@ -75,7 +75,7 @@ static const char *racenames[MAXRACES] = {
"clone"
};
struct race *get_race(race_t rt) {
const struct race *get_race(race_t rt) {
const char * name;
assert(rt < MAXRACES);
@ -83,7 +83,7 @@ struct race *get_race(race_t rt) {
if (!name) {
return NULL;
}
return rc_get_or_create(name);
return rc_find(name);
}
race_list *get_familiarraces(void)
@ -163,38 +163,43 @@ bool rc_changed(int *cache) {
return false;
}
race *rc_get_or_create(const char *zName)
race *rc_create(const char *zName)
{
race *rc;
int i;
assert(zName);
rc = rc_find_i(zName);
if (!rc) {
rc = (race *)calloc(sizeof(race), 1);
rc->hitpoints = 1;
rc->weight = PERSON_WEIGHT;
rc->capacity = 540;
rc->recruit_multi = 1.0F;
rc->regaura = 1.0F;
rc->speed = 1.0F;
rc->battle_flags = 0;
if (strchr(zName, ' ') != NULL) {
log_error("race '%s' has an invalid name. remove spaces\n", zName);
assert(strchr(zName, ' ') == NULL);
}
rc->_name = _strdup(zName);
rc->precombatspell = NULL;
rc->attack[0].type = AT_COMBATSPELL;
for (i = 1; i < RACE_ATTACKS; ++i)
rc->attack[i].type = AT_NONE;
rc->index = num_races++;
++rc_changes;
rc->next = races;
return races = rc;
rc = (race *)calloc(sizeof(race), 1);
rc->hitpoints = 1;
rc->weight = PERSON_WEIGHT;
rc->capacity = 540;
rc->recruit_multi = 1.0F;
rc->regaura = 1.0F;
rc->speed = 1.0F;
rc->battle_flags = 0;
if (strchr(zName, ' ') != NULL) {
log_error("race '%s' has an invalid name. remove spaces\n", zName);
assert(strchr(zName, ' ') == NULL);
}
return rc;
rc->_name = _strdup(zName);
rc->precombatspell = NULL;
rc->attack[0].type = AT_COMBATSPELL;
for (i = 1; i < RACE_ATTACKS; ++i)
rc->attack[i].type = AT_NONE;
rc->index = num_races++;
++rc_changes;
rc->next = races;
return races = rc;
}
race *rc_get_or_create(const char *zName)
{
race *rc;
assert(zName);
rc = rc_find_i(zName);
return rc ? rc : rc_create(zName);
}
/** dragon movement **/

View File

@ -175,10 +175,11 @@ extern "C" {
struct race_list *get_familiarraces(void);
struct race *races;
struct race *get_race(race_t rt);
const struct race *get_race(race_t rt);
/** TODO: compatibility hacks: **/
race_t old_race(const struct race *);
race *rc_create(const char *zName);
race *rc_get_or_create(const char *name);
bool rc_changed(int *cache);
const race *rc_find(const char *);
@ -241,8 +242,6 @@ extern "C" {
const char *racename(const struct locale *lang, const struct unit *u,
const race * rc);
#define omniscient(f) ((f)->race==get_race(RC_ILLUSION) || (f)->race==get_race(RC_TEMPLATE))
#define playerrace(rc) (!fval((rc), RCF_NPC))
#define dragonrace(rc) (fval(rc, RCF_DRAGON))
#define humanoidrace(rc) (fval((rc), RCF_UNDEAD) || (rc)==get_race(RC_DRACOID) || playerrace(rc))

View File

@ -51,15 +51,15 @@ static void test_rc_find(CuTest *tc) {
static void test_race_get(CuTest *tc) {
int cache = 0;
race *rc;
const race *rc;
test_setup();
CuAssertTrue(tc, rc_changed(&cache));
CuAssertTrue(tc, !rc_changed(&cache));
rc = get_race(RC_ELF);
CuAssertPtrEquals(tc, rc, (void *)rc_get_or_create("elf"));
rc = rc_get_or_create("elf");
CuAssertPtrEquals(tc, (void *)rc, (void *)get_race(RC_ELF));
CuAssertTrue(tc, rc_changed(&cache));
CuAssertTrue(tc, !rc_changed(&cache));
CuAssertPtrEquals(tc, rc, (void *)rc_find("elf"));
CuAssertPtrEquals(tc, (void *)rc, (void *)rc_find("elf"));
free_races();
CuAssertTrue(tc, rc_changed(&cache));
test_cleanup();

View File

@ -105,7 +105,7 @@ int skill_mod(const race * rc, skill_t sk, const struct terrain_type *terrain)
{
int result = 0;
static int rc_cache;
static race *rc_dwarf, *rc_insect;
static const race *rc_dwarf, *rc_insect;
if (rc_changed(&rc_cache)) {
rc_dwarf = get_race(RC_DWARF);
@ -140,7 +140,7 @@ int rc_skillmod(const struct race *rc, const region * r, skill_t sk)
}
if (r && r_isforest(r)) {
static int rc_cache;
static race * rc_elf;
static const race * rc_elf;
if (rc_changed(&rc_cache)) {
rc_elf = get_race(RC_ELF);
}

View File

@ -102,7 +102,7 @@ static void test_remove_units_ignores_spells(CuTest *tc) {
test_cleanup();
test_create_world();
u = create_unit(findregion(0, 0), test_create_faction(test_create_race("human")), 1, get_race(RC_SPELL), 0, 0, 0);
u = create_unit(findregion(0, 0), test_create_faction(test_create_race("human")), 1, test_create_race("spell"), 0, 0, 0);
uid = u->no;
u->number = 0;
u->age = 1;

View File

@ -36,6 +36,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "study.h"
#include "wormhole.h"
#include "prefix.h"
#include "reports.h"
#include "teleport.h"
#include "calendar.h"
#include "guard.h"

View File

@ -104,6 +104,19 @@ const char *coasts[MAXDIRECTIONS] = {
"coast::w"
};
bool omniscient(const faction *f)
{
static const race *rc_template, *rc_illusion;
static int cache;
if (rc_changed(&cache)) {
rc_illusion = get_race(RC_ILLUSION);
rc_template = get_race(RC_TEMPLATE);
}
return (f->race == rc_template || f->race == rc_illusion);
}
static char *groupid(const struct group *g, const struct faction *f)
{
typedef char name[OBJECTIDSIZE + 1];

View File

@ -48,6 +48,8 @@ extern "C" {
struct unit *can_find(struct faction *, struct faction *);
bool omniscient(const struct faction *f);
/* funktionen zum schreiben eines reports */
void sparagraph(struct strlist **SP, const char *s, unsigned int indent, char mark);
void lparagraph(struct strlist **SP, char *s, unsigned int indent, char mark);

View File

@ -2730,9 +2730,9 @@ static int sp_firewall(castorder * co)
* (SPELLLEVEL | TESTCANSEE)
*/
static race *unholy_race(const race *rc) {
static const race *unholy_race(const race *rc) {
static int cache;
static race * rc_skeleton, *rc_zombie, *rc_ghoul;
static const race * rc_skeleton, *rc_zombie, *rc_ghoul;
if (rc_changed(&cache)) {
rc_skeleton = get_race(RC_SKELETON);
rc_zombie = get_race(RC_ZOMBIE);

View File

@ -95,7 +95,7 @@ magic_t getmagicskill(const struct locale * lang)
bool is_migrant(unit * u)
{
static int cache;
static race *toad_rc;
static const race *toad_rc;
if (u_race(u) == u->faction->race)
return false;
@ -113,7 +113,7 @@ bool is_migrant(unit * u)
/* ------------------------------------------------------------- */
bool magic_lowskill(unit * u)
{
static race *toad_rc;
static const race *toad_rc;
static int cache;
if (rc_changed(&cache)) {
toad_rc = get_race(RC_TOAD);

View File

@ -291,10 +291,10 @@ void test_learn_skill_multi(CuTest *tc) {
static void test_demon_skillchanges(CuTest *tc) {
unit * u;
race * rc;
const race * rc;
test_setup();
rc = test_create_race("demon");
CuAssertPtrEquals(tc, rc, get_race(RC_DAEMON));
CuAssertPtrEquals(tc, (void *)rc, (void *)get_race(RC_DAEMON));
u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0));
CuAssertPtrNotNull(tc, u);
set_level(u, SK_CROSSBOW, 1);