remove cfindhash, again.

curse and unit ids can collide, there is no reason why they should be from the same pool.
This commit is contained in:
Enno Rehling 2016-10-23 10:03:33 +02:00
parent ac4e32a8f5
commit f12eda8bdf
5 changed files with 42 additions and 40 deletions

View File

@ -97,15 +97,6 @@ static void cunhash(curse * c)
} }
} }
curse *cfindhash(int i)
{
curse *old;
for (old = cursehash[i % MAXENTITYHASH]; old; old = old->nexthash)
if (old->no == i)
return old;
return NULL;
}
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
/* at_curse */ /* at_curse */
void curse_init(attrib * a) void curse_init(attrib * a)
@ -531,6 +522,24 @@ static void set_cursedmen(curse * c, int cursedmen)
} }
} }
static int newcurseid(void) {
int random_no;
int start_random_no;
random_no = 1 + (rng_int() % MAX_UNIT_NR);
start_random_no = random_no;
while (findcurse(random_no)) {
random_no++;
if (random_no == MAX_UNIT_NR + 1) {
random_no = 1;
}
if (random_no == start_random_no) {
random_no = (int)MAX_UNIT_NR + 1;
}
}
return random_no;
}
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
/* Legt eine neue Verzauberung an. Sollte es schon einen Zauber /* Legt eine neue Verzauberung an. Sollte es schon einen Zauber
* dieses Typs geben, gibt es den bestehenden zurück. * dieses Typs geben, gibt es den bestehenden zurück.
@ -552,7 +561,7 @@ static curse *make_curse(unit * mage, attrib ** ap, const curse_type * ct,
c->effect = effect; c->effect = effect;
c->magician = mage; c->magician = mage;
c->no = newunitid(); c->no = newcurseid();
chash(c); chash(c);
switch (c->type->typ) { switch (c->type->typ) {

View File

@ -280,14 +280,12 @@ extern "C" {
* */ * */
struct curse *get_curse(struct attrib *ap, const curse_type * ctype); struct curse *get_curse(struct attrib *ap, const curse_type * ctype);
int find_cursebyname(const char *c);
const curse_type *ct_find(const char *c); const curse_type *ct_find(const char *c);
bool ct_changed(int *cache); bool ct_changed(int *cache);
void ct_register(const curse_type *); void ct_register(const curse_type *);
void ct_remove(const char *c); void ct_remove(const char *c);
void ct_checknames(void); void ct_checknames(void);
curse *cfindhash(int i);
curse *findcurse(int curseid); curse *findcurse(int curseid);
void curse_init(struct attrib *a); void curse_init(struct attrib *a);

View File

@ -1437,6 +1437,26 @@ void free_unit(unit * u)
} }
} }
static int newunitid(void)
{
int random_unit_no;
int start_random_no;
random_unit_no = 1 + (rng_int() % MAX_UNIT_NR);
start_random_no = random_unit_no;
while (ufindhash(random_unit_no) || dfindhash(random_unit_no)
|| forbiddenid(random_unit_no)) {
random_unit_no++;
if (random_unit_no == MAX_UNIT_NR + 1) {
random_unit_no = 1;
}
if (random_unit_no == start_random_no) {
random_unit_no = (int)MAX_UNIT_NR + 1;
}
}
return random_unit_no;
}
static void createunitid(unit * u, int id) static void createunitid(unit * u, int id)
{ {
if (id <= 0 || id > MAX_UNIT_NR || ufindhash(id) || dfindhash(id) if (id <= 0 || id > MAX_UNIT_NR || ufindhash(id) || dfindhash(id)
@ -1699,6 +1719,7 @@ int unit_getcapacity(const unit * u)
} }
void renumber_unit(unit *u, int no) { void renumber_unit(unit *u, int no) {
if (no == 0) no = newunitid();
uunhash(u); uunhash(u);
if (!ualias(u)) { if (!ualias(u)) {
attrib *a = a_add(&u->attribs, a_new(&at_alias)); attrib *a = a_add(&u->attribs, a_new(&at_alias));
@ -1929,28 +1950,6 @@ bool unit_can_study(const unit *u) {
return !((u_race(u)->flags & RCF_NOLEARN) || fval(u, UFL_WERE)); return !((u_race(u)->flags & RCF_NOLEARN) || fval(u, UFL_WERE));
} }
/* ID's für Einheiten und Zauber */
int newunitid(void)
{
int random_unit_no;
int start_random_no;
random_unit_no = 1 + (rng_int() % MAX_UNIT_NR);
start_random_no = random_unit_no;
while (ufindhash(random_unit_no) || dfindhash(random_unit_no)
|| cfindhash(random_unit_no)
|| forbiddenid(random_unit_no)) {
random_unit_no++;
if (random_unit_no == MAX_UNIT_NR + 1) {
random_unit_no = 1;
}
if (random_unit_no == start_random_no) {
random_unit_no = (int)MAX_UNIT_NR + 1;
}
}
return random_unit_no;
}
static int read_newunitid(const faction * f, const region * r) static int read_newunitid(const faction * f, const region * r)
{ {
int n; int n;

View File

@ -258,7 +258,6 @@ extern "C" {
#define GET_PEASANTS 2 #define GET_PEASANTS 2
int getunit(const struct region * r, const struct faction * f, struct unit **uresult); int getunit(const struct region * r, const struct faction * f, struct unit **uresult);
int newunitid(void);
int read_unitid(const struct faction *f, const struct region *r); int read_unitid(const struct faction *f, const struct region *r);
void setstatus(struct unit *u, int status); void setstatus(struct unit *u, int status);

View File

@ -2916,7 +2916,7 @@ int renumber_cmd(unit * u, order * ord)
{ {
char token[128]; char token[128];
const char *s; const char *s;
int i; int i = 0;
faction *f = u->faction; faction *f = u->faction;
init_order(ord); init_order(ord);
@ -2936,10 +2936,7 @@ int renumber_cmd(unit * u, order * ord)
case P_UNIT: case P_UNIT:
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
if (s == NULL || *s == 0) { if (s && *s) {
i = newunitid();
}
else {
i = atoi36((const char *)s); i = atoi36((const char *)s);
if (i <= 0 || i > MAX_UNIT_NR) { if (i <= 0 || i > MAX_UNIT_NR) {
cmistake(u, ord, 114, MSG_EVENT); cmistake(u, ord, 114, MSG_EVENT);