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 (wtype != NULL) {
if (fval(u_race(du), RCF_DRAGON)) { if (fval(u_race(du), RCF_DRAGON)) {
static int cache; static int cache;
static race *rc_halfling; static const race *rc_halfling;
if (rc_changed(&cache)) { if (rc_changed(&cache)) {
rc_halfling = get_race(RC_HALFLING); 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 char *mailto = LOC(f->locale, "mailto");
const attrib *a; const attrib *a;
FILE *F = fopen(filename, "w"); FILE *F = fopen(filename, "w");
static const race *rc_human;
static int rc_cache;
if (era < 0) { if (era < 0) {
era = config_get_int("world.era", 1); 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, "%d;Anzahl Personen\n", count_all(f));
fprintf(F, "\"%s\";Magiegebiet\n", magic_school[f->magiegebiet]); 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;Anzahl Immigranten\n", count_migrants(f));
fprintf(F, "%d;Max. Immigranten\n", count_maxmigrants(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 // TODO: this should be a property of the race, like race.guard_flags
static int rc_cache; 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); const race *rc = u_race(u);
unsigned int flags = unsigned int flags =
GUARD_CREWS | GUARD_LANDING | GUARD_TRAVELTHRU | GUARD_TAX; GUARD_CREWS | GUARD_LANDING | GUARD_TRAVELTHRU | GUARD_TAX;

View file

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

View file

@ -75,7 +75,7 @@ static const char *racenames[MAXRACES] = {
"clone" "clone"
}; };
struct race *get_race(race_t rt) { const struct race *get_race(race_t rt) {
const char * name; const char * name;
assert(rt < MAXRACES); assert(rt < MAXRACES);
@ -83,7 +83,7 @@ struct race *get_race(race_t rt) {
if (!name) { if (!name) {
return NULL; return NULL;
} }
return rc_get_or_create(name); return rc_find(name);
} }
race_list *get_familiarraces(void) race_list *get_familiarraces(void)
@ -163,14 +163,12 @@ bool rc_changed(int *cache) {
return false; return false;
} }
race *rc_get_or_create(const char *zName) race *rc_create(const char *zName)
{ {
race *rc; race *rc;
int i; int i;
assert(zName); assert(zName);
rc = rc_find_i(zName);
if (!rc) {
rc = (race *)calloc(sizeof(race), 1); rc = (race *)calloc(sizeof(race), 1);
rc->hitpoints = 1; rc->hitpoints = 1;
rc->weight = PERSON_WEIGHT; rc->weight = PERSON_WEIGHT;
@ -193,8 +191,15 @@ race *rc_get_or_create(const char *zName)
++rc_changes; ++rc_changes;
rc->next = races; rc->next = races;
return races = rc; return races = rc;
} }
return 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 **/ /** dragon movement **/

View file

@ -175,10 +175,11 @@ extern "C" {
struct race_list *get_familiarraces(void); struct race_list *get_familiarraces(void);
struct race *races; struct race *races;
struct race *get_race(race_t rt); const struct race *get_race(race_t rt);
/** TODO: compatibility hacks: **/ /** TODO: compatibility hacks: **/
race_t old_race(const struct race *); race_t old_race(const struct race *);
race *rc_create(const char *zName);
race *rc_get_or_create(const char *name); race *rc_get_or_create(const char *name);
bool rc_changed(int *cache); bool rc_changed(int *cache);
const race *rc_find(const char *); const race *rc_find(const char *);
@ -241,8 +242,6 @@ extern "C" {
const char *racename(const struct locale *lang, const struct unit *u, const char *racename(const struct locale *lang, const struct unit *u,
const race * rc); 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 playerrace(rc) (!fval((rc), RCF_NPC))
#define dragonrace(rc) (fval(rc, RCF_DRAGON)) #define dragonrace(rc) (fval(rc, RCF_DRAGON))
#define humanoidrace(rc) (fval((rc), RCF_UNDEAD) || (rc)==get_race(RC_DRACOID) || playerrace(rc)) #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) { static void test_race_get(CuTest *tc) {
int cache = 0; int cache = 0;
race *rc; const race *rc;
test_setup(); test_setup();
CuAssertTrue(tc, rc_changed(&cache)); CuAssertTrue(tc, rc_changed(&cache));
CuAssertTrue(tc, !rc_changed(&cache)); CuAssertTrue(tc, !rc_changed(&cache));
rc = get_race(RC_ELF); rc = rc_get_or_create("elf");
CuAssertPtrEquals(tc, rc, (void *)rc_get_or_create("elf")); CuAssertPtrEquals(tc, (void *)rc, (void *)get_race(RC_ELF));
CuAssertTrue(tc, rc_changed(&cache)); CuAssertTrue(tc, rc_changed(&cache));
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(); free_races();
CuAssertTrue(tc, rc_changed(&cache)); CuAssertTrue(tc, rc_changed(&cache));
test_cleanup(); 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; int result = 0;
static int rc_cache; static int rc_cache;
static race *rc_dwarf, *rc_insect; static const race *rc_dwarf, *rc_insect;
if (rc_changed(&rc_cache)) { if (rc_changed(&rc_cache)) {
rc_dwarf = get_race(RC_DWARF); 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)) { if (r && r_isforest(r)) {
static int rc_cache; static int rc_cache;
static race * rc_elf; static const race * rc_elf;
if (rc_changed(&rc_cache)) { if (rc_changed(&rc_cache)) {
rc_elf = get_race(RC_ELF); rc_elf = get_race(RC_ELF);
} }

View file

@ -102,7 +102,7 @@ static void test_remove_units_ignores_spells(CuTest *tc) {
test_cleanup(); test_cleanup();
test_create_world(); 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; uid = u->no;
u->number = 0; u->number = 0;
u->age = 1; u->age = 1;

View file

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

View file

@ -104,6 +104,19 @@ const char *coasts[MAXDIRECTIONS] = {
"coast::w" "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) static char *groupid(const struct group *g, const struct faction *f)
{ {
typedef char name[OBJECTIDSIZE + 1]; typedef char name[OBJECTIDSIZE + 1];

View file

@ -48,6 +48,8 @@ extern "C" {
struct unit *can_find(struct faction *, struct faction *); struct unit *can_find(struct faction *, struct faction *);
bool omniscient(const struct faction *f);
/* funktionen zum schreiben eines reports */ /* funktionen zum schreiben eines reports */
void sparagraph(struct strlist **SP, const char *s, unsigned int indent, char mark); 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); 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) * (SPELLLEVEL | TESTCANSEE)
*/ */
static race *unholy_race(const race *rc) { static const race *unholy_race(const race *rc) {
static int cache; static int cache;
static race * rc_skeleton, *rc_zombie, *rc_ghoul; static const race * rc_skeleton, *rc_zombie, *rc_ghoul;
if (rc_changed(&cache)) { if (rc_changed(&cache)) {
rc_skeleton = get_race(RC_SKELETON); rc_skeleton = get_race(RC_SKELETON);
rc_zombie = get_race(RC_ZOMBIE); rc_zombie = get_race(RC_ZOMBIE);

View file

@ -95,7 +95,7 @@ magic_t getmagicskill(const struct locale * lang)
bool is_migrant(unit * u) bool is_migrant(unit * u)
{ {
static int cache; static int cache;
static race *toad_rc; static const race *toad_rc;
if (u_race(u) == u->faction->race) if (u_race(u) == u->faction->race)
return false; return false;
@ -113,7 +113,7 @@ bool is_migrant(unit * u)
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
bool magic_lowskill(unit * u) bool magic_lowskill(unit * u)
{ {
static race *toad_rc; static const race *toad_rc;
static int cache; static int cache;
if (rc_changed(&cache)) { if (rc_changed(&cache)) {
toad_rc = get_race(RC_TOAD); 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) { static void test_demon_skillchanges(CuTest *tc) {
unit * u; unit * u;
race * rc; const race * rc;
test_setup(); test_setup();
rc = test_create_race("demon"); 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)); u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0));
CuAssertPtrNotNull(tc, u); CuAssertPtrNotNull(tc, u);
set_level(u, SK_CROSSBOW, 1); set_level(u, SK_CROSSBOW, 1);