WIP: implementing hash tables for flagpiles. flag code done, need to wrap all walks of flagpiles with foreach_bucket()

This commit is contained in:
rob 2022-08-28 15:13:30 +10:00
parent 10fd3bc440
commit 9fcbceb8c6
7 changed files with 1007 additions and 896 deletions

17
ai.c
View File

@ -3387,13 +3387,16 @@ int aispellok(lifeform_t *lf, enum OBTYPE spellid, lifeform_t *victim, enum FLAG
}
}
if (ot->id == OT_S_WEAKEN) {
flag_t *lff;
for (lff = lf->flags->first; lff ; lff = lff->next) {
if ((lff->id == F_ATTRMOD) && (lff->val[0] == A_STR) && (lff->obfrom == OT_S_WEAKEN)) {
specificcheckok = B_FALSE;
break;
}
}
int nretflags,i;
flag_t *retflag[MAXCANDIDATES];
getflags(lf->flags, retflag, &nretflags, F_ATTRMOD, F_NONE);
for (i=0; i< nretflags; i++) {
if ((retflag[i]->id == F_ATTRMOD) && (retflag[i]->val[0] == A_STR) && (retflag[i]->obfrom == OT_S_WEAKEN)) {
specificcheckok = B_FALSE;
break;
}
}
}
if (!specificcheckok) {
if (db) dblog(".oO { cant cast %s - specific spell check failed }", ot ? ot->name : "?unkownspell?");

13
data.c
View File

@ -46,13 +46,14 @@ extern objecttype_t *lastot;
extern condset_t ccwalkable;
void addbonustext(flagpile_t *fp, enum FLAG fid, char *text) {
int idx = -1;
flag_t *f;
int idx = -1, i;
flag_t *retflag[MAXCANDIDATES];
int nretflags = 0;
getflags(fp, retflag, &nretflags, fid, F_NONE);
// find last index
for (f = fp->first ; f; f = f->next) {
if (f->id == fid) {
if (f->val[0] > idx) idx = f->val[0];
}
for (i=0; i< nretflags; i++) {
if (retflag[i]->val[0] > idx) idx = retflag[i]->val[0];
}
addflag(fp, fid, idx+1, NA, NA, text);
}

6
defs.h
View File

@ -11,10 +11,14 @@
#define OBNOS1(o) (o->amt == 1) ? "" : "s"
#define ISINRANGE(a,min,max) ((a >= min) && (a <= max))
#define foreach_bucket(a) for (a=0; a<NHASHBUCKETS; a++)
// #define PRACTICETIME 15 // #attempts it takes to learn new weapon skill
// for flags hashtable
#define NHASHBUCKETS (5)
// how much your str/agi/etc goes up every few levels
#define STATAMTPERLEVEL 5
@ -5313,7 +5317,7 @@ typedef struct obpile_s {
typedef struct flagpile_s {
lifeform_t *owner;
struct object_s *ob;
struct flag_s *first,*last;
struct flag_s *first[NHASHBUCKETS],*last[NHASHBUCKETS];
struct flag_s *item[MAXFLAGS];
int nitems;

354
flag.c
View File

@ -59,6 +59,14 @@ flag_t *addtempflag(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3,
return addflag_real(fp, id, val1, val2, val3, text, timeleft, B_KNOWN, -1);
}
int getflaghash(enum FLAG fid) {
int h;
h = (int) fid % NHASHBUCKETS;
assert(h >= 0);
assert(h < NHASHBUCKETS);
return h;
}
flag_t *addflag_real(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3, char *text, int lifetime, int known, long obfromid) {
lifeform_t *lf;
flag_t *f = NULL;
@ -67,6 +75,7 @@ flag_t *addflag_real(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3,
int redrawstatatend = B_FALSE;
int i;
int rv;
int hash;
enum { NONE, FIRST, FIRST2, MIDDLE, LAST } fml = NONE;
checkflagpile(fp);
@ -168,17 +177,21 @@ flag_t *addflag_real(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3,
*/
//if (gamemode == GM_GAMESTARTED) checkmapflags(player->cell->map); // debugging
// determine bucket
hash = getflaghash(id);
////////////////////////////////
// ACTUAL FLAG ADDITION IS HERE
////////////////////////////////
if (fp->first == NULL) {
fp->first = (flag_t *)calloc(1, sizeof(flag_t));
f = fp->first;
if (fp->first[hash] == NULL) {
fp->first[hash] = (flag_t *)calloc(1, sizeof(flag_t));
f = fp->first[hash];
f->prev = NULL;
// also the last
fp->last = f;
fp->last[hash] = f;
f->next = NULL;
fml = FIRST;
@ -186,7 +199,7 @@ flag_t *addflag_real(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3,
flag_t *ff,*insertbefore = NULL,*insertafter = NULL;
// we will keep flags sorted.
// find correct position in list...
ff = fp->first;
ff = fp->first[hash];
while (ff && (ff->id < id)) {
ff = ff->next;
}
@ -223,8 +236,8 @@ flag_t *addflag_real(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3,
} else {
// first one
//fp->first = (flag_t *)calloc(1, sizeof(flag_t));
fp->first = newone;
assert(fp->first != NULL);
fp->first[hash] = newone;
assert(fp->first[hash] != NULL);
//f = fp->first;
f = newone;
fml = FIRST2;
@ -238,11 +251,11 @@ flag_t *addflag_real(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3,
ff->prev = f;
} else {
// last one. insert at end of list.
f = fp->last;
f = fp->last[hash];
f->next = (flag_t *)malloc(sizeof(flag_t)); /// <- died here!
f->next->prev = f;
f = f->next;
fp->last = f;
fp->last[hash] = f;
f->next = NULL;
fml = LAST;
@ -463,13 +476,16 @@ flag_t *addflag_real(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3,
}
flagpile_t *addflagpile(lifeform_t *owner, object_t *ob) {
int i;
flagpile_t *fp;
fp = malloc(sizeof(flagpile_t));
fp->first = NULL;
fp->last = NULL;
foreach_bucket(i) {
fp->first[i] = NULL;
fp->last[i] = NULL;
fp->nitems = 0;
}
fp->owner = owner;
fp->ob = ob;
fp->nitems = 0;
return fp;
}
@ -552,22 +568,27 @@ void checkflagpile(flagpile_t *fp) {
}
}
/// TODO: remove this function
int fpisbad(flagpile_t *fp) {
flag_t *f;
int i;
if (!flagcheck) return B_FALSE;
for (f = fp->first ; f ; f = f->next) {
if (f->next) {
if(f->next->prev != f) {
dblog("flag after %d is bad! its prev = 0x%x, should be this flag (0x%x)",f->id, f->next->prev, f);
return B_TRUE;
}
}
if (fp->ob && fp->owner) {
dblog("flag %d has both ob and owner",f->id);
return B_TRUE;
}
foreach_bucket(i) {
for (f = fp->first[i] ; f ; f = f->next) {
if (f->next) {
if(f->next->prev != f) {
dblog("flag after %d is bad! its prev = 0x%x, should be this flag (0x%x)",f->id, f->next->prev, f);
return B_TRUE;
}
}
if (fp->ob && fp->owner) {
dblog("flag %d has both ob and owner",f->id);
return B_TRUE;
}
}
}
return B_FALSE;
}
@ -576,10 +597,11 @@ int fpisbad(flagpile_t *fp) {
// returns # of flags copied
int copyflag(flagpile_t *dst, flagpile_t *src, enum FLAG id) {
flag_t *f;
int ndone = 0;
for (f = src->first ; f ; f = f->next) {
int ndone = 0, i;
foreach_bucket(i) {
for (f = src->first[i] ; f ; f = f->next) {
// gone past the requrested id's number - ie. it's not there.
if (f->id > id) break;
//if (f->id > id) break;
if (f->id == id) {
flag_t *newf;
newf = addflag_real(dst, f->id, f->val[0], f->val[1], f->val[2], f->text,
@ -596,64 +618,75 @@ int copyflag(flagpile_t *dst, flagpile_t *src, enum FLAG id) {
}
ndone++;
}
}
}
return ndone;
}
void copyflags(flagpile_t *dst, flagpile_t *src, int lifetime) {
flag_t *f,*newf;
for (f = src->first ; f ; f = f->next) {
// omit certain flags
if (lifetime == FROMBRAND) {
if ((f->id == F_ONLYFOROBTYPE) ||
(f->id == F_ONLYFORDAMTYPE) ||
(f->id == F_ONLYFORWEPSKILL)) {
continue;
}
}
newf = addflag_real(dst, f->id, f->val[0], f->val[1], f->val[2], f->text,
(lifetime == NA) ? f->lifetime : lifetime, f->known, -1);
// newf might not be set, if (for example) we are coyping a duplicate
// f_linkrace flag.
if (newf) {
newf->condition = f->condition;
newf->origlifetime = f->origlifetime;
newf->chance = f->chance;
if (f->altval) {
addaltval(newf, f->altval->id,
f->altval->val[0],
f->altval->val[1],
f->altval->val[2],
f->altval->text);
}
}
}
int i;
foreach_bucket(i) {
for (f = src->first[i] ; f ; f = f->next) {
// omit certain flags
if (lifetime == FROMBRAND) {
if ((f->id == F_ONLYFOROBTYPE) ||
(f->id == F_ONLYFORDAMTYPE) ||
(f->id == F_ONLYFORWEPSKILL)) {
continue;
}
}
newf = addflag_real(dst, f->id, f->val[0], f->val[1], f->val[2], f->text,
(lifetime == NA) ? f->lifetime : lifetime, f->known, -1);
// newf might not be set, if (for example) we are coyping a duplicate
// f_linkrace flag.
if (newf) {
newf->condition = f->condition;
newf->origlifetime = f->origlifetime;
newf->chance = f->chance;
if (f->altval) {
addaltval(newf, f->altval->id,
f->altval->val[0],
f->altval->val[1],
f->altval->val[2],
f->altval->text);
}
}
}
}
}
int countflags(flagpile_t *fp) {
flag_t *f;
int count = 0;
for (f = fp->first ; f ; f = f->next) {
count++;
}
int count = 0, i;
foreach_bucket(i) {
for (f = fp->first[i] ; f ; f = f->next) {
count++;
}
}
return count;
}
int countflagsofid(flagpile_t *fp, enum FLAG fid) {
flag_t *f;
int count = 0;
for (f = fp->first ; f ; f = f->next) {
int count = 0, i;
foreach_bucket(i) {
for (f = fp->first[i] ; f ; f = f->next) {
if (f->id == fid) count++;
}
}
}
return count;
}
void dumpflags(flagpile_t *fp) {
flag_t *f;
dblog("START FLAGDUMP");
for (f = fp->first ; f ; f = f->next) {
dblog("fid=%d, v0=%d v1=%d v2=%d text=[%s]",
f->id, f->val[0], f->val[1], f->val[2], f->text);
int i;
foreach_bucket(i) {
for (f = fp->first[i] ; f ; f = f->next) {
dblog("[b=%d] fid=%d, v0=%d v1=%d v2=%d text=[%s]",
i, f->id, f->val[0], f->val[1], f->val[2], f->text);
}
}
dblog("END FLAGDUMP");
}
@ -817,11 +850,75 @@ flag_t *hasflagknown(flagpile_t *fp, int id) {
return hasflag_real(fp, id, B_TRUE, NULL, NA);
}
flag_t *flag_bsearch(flagpile_t *fp, int id, int *iterp) {
flag_t *f,*searchfrom = NULL;
int pos;
int iter = 0;
int l,r;
// binary search for this flag id.
l = 0;
r = fp->nitems-1;
while (!searchfrom) {
//if (db) dblog(" iter#%d: l=%d,r=%d", iter, l, r);
if (r < l) {
// NOT FOUND.
//if (db) dblog(" r < l: not found!");
break;
} else if (fp->item[l]->id > id) {
// NOT FOUND.
//if (db) dblog(" leftid %d > wantid %d. not found!",fp->item[l]->id, id);
break;
} else if (fp->item[r]->id < id) {
// NOT FOUND.
//if (db) dblog(" rightid %d > wantid %d. not found!",fp->item[r]->id, id);
break;
}
// half way inbetween l and r
pos = ((l+r)/2);
f = fp->item[pos];
//if (db) dblog(" iter#%d: pos=%d. item here is %d (looking for %d)", iter, pos, f->id, id);
if (f->id == id) {
// go back to first occurence
while (f->prev && (f->prev->id == id)) {
f = f->prev;
iter++;
}
searchfrom = f;
break;
} else if (f->id > id) {
// go left
r = pos-1;
} else {
// go right
l = pos+1;
}
iter++;
}
if (iterp) *iterp += iter;
return searchfrom;
}
flag_t *flag_hsearch(flagpile_t *fp, int id, int *iterp) {
flag_t *f,*searchfrom = NULL;
int hash;
int iter = 0;
hash = getflaghash(id);
for (f = fp->first[hash]; f ; f=f->next) {
iter++;
if (f->id == id) {
searchfrom = f;
break;
}
}
if (iterp) *iterp += iter;
return searchfrom;
}
flag_t *hasflag_real(flagpile_t *fp, int id, int wantknown, flag_t *exception, int lifetimeexception) {
flag_t *f,*foundflag = NULL, *searchfrom = NULL;
lifeform_t *owner;
int pos;
int l,r;
int iter = 0,subiter = 0;
int db = B_FALSE;
@ -829,52 +926,17 @@ flag_t *hasflag_real(flagpile_t *fp, int id, int wantknown, flag_t *exception, i
if (flagdb) db = B_TRUE;
if (db) dblog("hasflag %d in flagpile with %d entries", id, fp->nitems);
// binary search for this flag id.
l = 0;
r = fp->nitems-1;
while (!searchfrom) {
//if (db) dblog(" iter#%d: l=%d,r=%d", iter, l, r);
if (r < l) {
// NOT FOUND.
//if (db) dblog(" r < l: not found!");
break;
} else if (fp->item[l]->id > id) {
// NOT FOUND.
//if (db) dblog(" leftid %d > wantid %d. not found!",fp->item[l]->id, id);
break;
} else if (fp->item[r]->id < id) {
// NOT FOUND.
//if (db) dblog(" rightid %d > wantid %d. not found!",fp->item[r]->id, id);
break;
}
// half way inbetween l and r
pos = ((l+r)/2);
f = fp->item[pos];
//if (db) dblog(" iter#%d: pos=%d. item here is %d (looking for %d)", iter, pos, f->id, id);
if (f->id == id) {
// go back to first occurence
while (f->prev && (f->prev->id == id)) {
f = f->prev;
iter++;
}
searchfrom = f;
break;
} else if (f->id > id) {
// go left
r = pos-1;
} else {
// go right
l = pos+1;
}
iter++;
}
if (db) dblog("call to hasflag %d in flagpile with %d entries", id, fp->nitems);
// find first occurence of this id
//searchfrom = flag_bsearch(fp, id, &iter);
searchfrom = flag_hsearch(fp, id, &iter);
// now find a valid one
f = searchfrom;
while (f && (f->id == id)) {
//while (f && (f->id == id)) { for binary search
while (f) {
if (f->id == id) {
int valid = B_TRUE;
if (f == exception) valid = B_FALSE;
if ((lifetimeexception != NA) && (f->lifetime == lifetimeexception)) valid = B_FALSE;
@ -889,6 +951,7 @@ flag_t *hasflag_real(flagpile_t *fp, int id, int wantknown, flag_t *exception, i
f = f->next;
subiter++;
}
}
}
if (db) dblog("finished - %s - iter=%d, subiter=%d", foundflag ? "found it" : "NOT FOUND",iter,subiter);
@ -1131,7 +1194,7 @@ void real_killflag(flag_t *f, int wantannounce) {
cell_t *redolos = NULL;
int redostat = B_FALSE;
int redoscreen = B_FALSE;
int i,dopleasegod[MAXGODS];
int i,hash,dopleasegod[MAXGODS];
flagpile_t *newflags = NULL;
@ -1261,6 +1324,8 @@ void real_killflag(flag_t *f, int wantannounce) {
}
checkflagpile(pile);
// determine bucket
hash = getflaghash(f->id);
/////////////////////////////////////////////
// now we are actually removing the flag.
/////////////////////////////////////////////
@ -1284,13 +1349,13 @@ void real_killflag(flag_t *f, int wantannounce) {
if (nextone != NULL) {
nextone->prev = f->prev;
} else { /* last */
f->pile->last = f->prev;
f->pile->last[hash] = f->prev;
}
if (f->prev == NULL) {
/* first */
nextone = f->next;
f->pile->first = nextone;
f->pile->first[hash] = nextone;
free(f);
} else {
lastone = f->prev;
@ -1362,43 +1427,49 @@ void real_killflag(flag_t *f, int wantannounce) {
}
void killflagpile(flagpile_t *fp) {
if (fp->ob && !fp->ob->dying) {
assert(0 == "calling killflagpile on non-dying object");
}
int i;
if (fp->ob && !fp->ob->dying) {
assert(0 == "calling killflagpile on non-dying object");
}
//checkflagpile(fp);
while (fp->first) {
killflag(fp->first);
}
//checkflagpile(fp);
free(fp);
//checkflagpile(fp);
foreach_bucket(i) {
while (fp->first[i]) {
killflag(fp->first[i]);
}
}
//checkflagpile(fp);
free(fp);
}
int killtransitoryflags(flagpile_t *fp, enum FLAG fid) {
flag_t *f,*nextf;
int nkilled = 0;
for (f = fp->first ; f ; f = nextf) {
nextf = f->next;
flag_t *f,*nextf;
int nkilled = 0, i;
foreach_bucket(i) {
for (f = fp->first[i] ; f ; f = nextf) {
nextf = f->next;
// gone past the requrested id's number - ie. it's not there.
if (f->id > fid) break;
// gone past the requrested id's number - ie. it's not there.
//if (f->id > fid) break;
if (istransitoryflag(f) && (f->id == fid)) {
killflag(f);
nkilled++;
}
}
return nkilled;
if (istransitoryflag(f) && (f->id == fid)) {
killflag(f);
nkilled++;
}
}
}
return nkilled;
}
int killtransitoryflagvals(flagpile_t *fp, enum FLAG fid, int val1, int val2, int val3, char *text) {
flag_t *f, *nextf;
int nkilled = 0;
for (f = fp->first ; f ; f = nextf) {
int nkilled = 0, i;
foreach_bucket(i) {
for (f = fp->first[i] ; f ; f = nextf) {
nextf = f->next;
// gone past the requrested id's number - ie. it's not there.
if (f->id > fid) break;
//if (f->id > fid) break;
if (istransitoryflag(f) && (f->id == fid)) {
if ( ((val1 == NA) || (f->val[0] == val1)) &&
@ -1409,6 +1480,7 @@ int killtransitoryflagvals(flagpile_t *fp, enum FLAG fid, int val1, int val2, in
nkilled++;
}
}
}
}
return nkilled;
}
@ -1661,19 +1733,22 @@ char *getflagtext(flagpile_t *fp, enum FLAG fid) {
}
void getmaxflags(flagpile_t *fp, int id, int *max0, int *max1, int *max2) {
int i;
flag_t *f;
if (max0) *max0 = 0;
if (max1) *max1 = 0;
if (max2) *max2 = 0;
for (f = fp->first ; f ; f = f->next) {
foreach_bucket(i) {
for (f = fp->first[i] ; f ; f = f->next) {
// gone past the requrested id's number - ie. it's not there.
if (f->id > id) break;
//if (f->id > id) break;
if (f->id == id) {
if (max0 && (f->val[0] > *max0)) *max0 = f->val[0];
if (max1 && (f->val[1] > *max1)) *max1 = f->val[1];
if (max2 && (f->val[2] > *max2)) *max2 = f->val[2];
}
}
}
}
@ -1758,21 +1833,26 @@ void sumflags(flagpile_t *fp, enum FLAG id, int *val0, int *val1, int *val2) {
void timeeffectsflags(flagpile_t *fp) {
flag_t *f,*nextf;
enum FLAG thisid;
int i;
for (f = fp->first ; f ; f = nextf) {
foreach_bucket(i) {
for (f = fp->first[i] ; f ; f = nextf) {
nextf = f->next;
thisid = f->id;
timeeffectsflag(f, 1);
// if this checkflag() crashes, we know there we a bug during processing of
// flag 'thisid'
checkflagpile(fp);
}
}
//checkflagpile(fp);
}
// generate an index
void updatefpindex(flagpile_t *fp) {
return;
/*
flag_t *f;
fp->nitems = 0;
@ -1784,5 +1864,5 @@ void updatefpindex(flagpile_t *fp) {
fp->nitems++;
assert(fp->nitems < MAXFLAGS);
}
//checkflagpile(fp);
*/
}

3
flag.h
View File

@ -21,6 +21,8 @@ void copyflags(flagpile_t *dst, flagpile_t *src, int lifetime);
int countflags(flagpile_t *fp);
int countflagsofid(flagpile_t *fp, enum FLAG fid );
void dumpflags(flagpile_t *fp);
flag_t *flag_bsearch(flagpile_t *fp, int id, int *iterp);
flag_t *flag_hsearch(flagpile_t *fp, int id, int *iterp);
int flagcausesinterrupt(flag_t *f, enum GAINORLOSS gol);
int flagcausesloscalc(enum FLAG fid);
int flagcausesredraw(lifeform_t *lf, enum FLAG fid);
@ -30,6 +32,7 @@ int flagstacks(enum FLAG fid);
int flagtomaxhp(flag_t *f);
cell_t *getflagpilelocation(flagpile_t *fp);
int getflags(flagpile_t *fp, flag_t **retflag, int *nretflags, ... );
int getflaghash(enum FLAG fid);
char *getflagtext(flagpile_t *fp, enum FLAG fid);
void getmaxflags(flagpile_t *fp, int id, int *max0, int *max1, int *max2);
flag_t *getrandomflag(flagpile_t *fp, enum FLAG fid);

18
god.c
View File

@ -166,7 +166,7 @@ void angergod(enum RACE rid, int amt, enum GODANGERREASON why) {
object_t *o;
flag_t *f;
int isflag[MAXCANDIDATES];
int nposs = 0;
int nposs = 0, b;
enum ATTRIB a;
// lose at least one god gift
for (o = player->pack->first ; o ; o = o->next) {
@ -177,13 +177,15 @@ void angergod(enum RACE rid, int amt, enum GODANGERREASON why) {
}
}
for (f = player->flags->first ; f ; f = f->next) {
if ((f->lifetime == FROMGODGIFT) && (f->obfrom == god->race->id)) {
poss[nposs] = f;
isflag[nposs] = B_TRUE;
nposs++;
}
}
foreach_bucket(b) {
for (f = player->flags->first[b] ; f ; f = f->next) {
if ((f->lifetime == FROMGODGIFT) && (f->obfrom == god->race->id)) {
poss[nposs] = f;
isflag[nposs] = B_TRUE;
nposs++;
}
}
}
if (nposs) {
int sel;
msg("\"You are unworthy of my gifts, mortal!\"");

1492
io.c

File diff suppressed because it is too large Load Diff