From f12eda8bdfc945c000dd36dd168ad874079dbc9e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 23 Oct 2016 10:03:33 +0200 Subject: [PATCH] remove cfindhash, again. curse and unit ids can collide, there is no reason why they should be from the same pool. --- src/kernel/curse.c | 29 +++++++++++++++++++---------- src/kernel/curse.h | 2 -- src/kernel/unit.c | 43 +++++++++++++++++++++---------------------- src/kernel/unit.h | 1 - src/laws.c | 7 ++----- 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/kernel/curse.c b/src/kernel/curse.c index bc3cdbfba..3ecf48899 100644 --- a/src/kernel/curse.c +++ b/src/kernel/curse.c @@ -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 */ 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 * 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->magician = mage; - c->no = newunitid(); + c->no = newcurseid(); chash(c); switch (c->type->typ) { diff --git a/src/kernel/curse.h b/src/kernel/curse.h index 9f6688a4f..0de1211c7 100644 --- a/src/kernel/curse.h +++ b/src/kernel/curse.h @@ -280,14 +280,12 @@ extern "C" { * */ 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); bool ct_changed(int *cache); void ct_register(const curse_type *); void ct_remove(const char *c); void ct_checknames(void); - curse *cfindhash(int i); curse *findcurse(int curseid); void curse_init(struct attrib *a); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 2046a72c5..d95096484 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -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) { 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) { + if (no == 0) no = newunitid(); uunhash(u); if (!ualias(u)) { 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)); } -/* 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) { int n; diff --git a/src/kernel/unit.h b/src/kernel/unit.h index 32e48a950..a360321a2 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -258,7 +258,6 @@ extern "C" { #define GET_PEASANTS 2 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); void setstatus(struct unit *u, int status); diff --git a/src/laws.c b/src/laws.c index e243378dd..623a163d6 100755 --- a/src/laws.c +++ b/src/laws.c @@ -2916,7 +2916,7 @@ int renumber_cmd(unit * u, order * ord) { char token[128]; const char *s; - int i; + int i = 0; faction *f = u->faction; init_order(ord); @@ -2936,10 +2936,7 @@ int renumber_cmd(unit * u, order * ord) case P_UNIT: s = gettoken(token, sizeof(token)); - if (s == NULL || *s == 0) { - i = newunitid(); - } - else { + if (s && *s) { i = atoi36((const char *)s); if (i <= 0 || i > MAX_UNIT_NR) { cmistake(u, ord, 114, MSG_EVENT);