#include #include #include #include "ai.h" #include "attack.h" #include "defs.h" #include "flag.h" #include "god.h" #include "io.h" #include "lf.h" #include "map.h" #include "move.h" #include "nexus.h" #include "objects.h" #include "spell.h" #include "text.h" extern lifeform_t *player; extern enum ERROR reason; int wantdb = B_TRUE; void addignorecell(lifeform_t *lf, cell_t *c) { if (c) { addflag(lf->flags, F_IGNORECELL, c->x, c->y, NA, NULL); } } // returns true on failure int aiattack(lifeform_t *lf, lifeform_t *victim, int timelimit) { int db = B_FALSE; int innocentattack = 0; flag_t *f; if (lfhasflag(lf, F_DEBUG)) { db = B_TRUE; } if (!canattack(lf)) { dblog(".oO { canattack() is false }"); return B_FALSE; } // mindless? /* if (getattrbracket(getattr(lf, A_IQ), A_IQ, NULL) == IQ_MINDLESS) { if (!isundead(lf) && (lf->race->raceclass->id != RC_PLANT)) { dblog(".oO { i am mindless and not undead }"); return B_TRUE; } } */ // already targetting this lf? f = lfhasflagval(lf, F_TARGETLF, victim->id, NA, NA, NULL); if (f) { dblog(".oO { i am already targetting this lf }"); if ((f->lifetime > 0) && (f->lifetime < timelimit)) { f->lifetime = timelimit; dblog(".oO { (flag lifetime updated) }"); } return B_TRUE; } // feigning death? if (lfhasflag(victim, F_FEIGNINGDEATH)) { int penalty; penalty = (getcelldist(lf->cell, victim->cell)-1); if (penalty < 0) penalty = 0; penalty *= 3; if (!skillcheckvs(lf, SC_IQ, -penalty, victim, SC_WILL, 5+gethitdice(victim))) { dblog(".oO { attempted target fooled me with feign death. ignoring. }"); return B_TRUE; } } if (db) { char lfname[BUFLEN],vicname[BUFLEN]; getlfname(lf, lfname); getlfname(victim, vicname); dblog(".oO { %s setting new target: %s }", lfname, vicname); } killflagsofid(lf->flags, F_TARGETLF); killflagsofid(lf->flags, F_TARGETCELL); if ((timelimit == PERMENANT) || (timelimit == UNLIMITED)) { addflag(lf->flags, F_TARGETLF, victim->id, victim->cell->x, victim->cell->y, NULL); } else { addtempflag(lf->flags, F_TARGETLF, victim->id , victim->cell->x , victim->cell->y, NULL,timelimit); } // tell the player if (areallies(player, lf) && cantalk(lf)) { char text[BUFLEN]; if (cansee(lf, victim)) { real_getlfname(victim, text, B_FALSE); } else { strcpy(text, "something"); } if (cansee(player, victim)) { sayphrase(lf, SP_ALLY_ATTACK, SV_SHOUT, NA, text); } else { sayphrase(lf, SP_ALLY_ATTACKUNSEEN, SV_SHOUT, NA, text); } } else { makenoise(lf, N_GETANGRY); } // become hostile? if (isplayer(victim) && !hasflag(lf->flags, F_HOSTILE) ) { addflag(lf->flags, F_HOSTILE, B_TRUE, NA, NA, NULL); //innocentattack = 1; } // change allegience ? if (!areenemies(lf, victim)) { if (getallegiance(victim) == AL_FRIENDLY) { killflagsofid(lf->flags, F_FRIENDLY); innocentattack = 3; } } // no longer a pet f = lfhasflagval(lf, F_PETOF, victim->id, NA, NA, NULL); if (f) { if (isplayer(victim)) innocentattack = 3; killflag(f); } return B_FALSE; } enum OBTYPE aigetattackspell(lifeform_t *lf, lifeform_t *victim) { flag_t *retflag[MAXCANDIDATES]; int nretflags = 0; flag_t *f; enum OBTYPE poss[MAXPILEOBS]; int nposs = 0,i; int db = B_FALSE; if (lfhasflag(lf, F_DEBUG)) { db = B_TRUE; } if (db) { dblog(".oO { looking for attack spells/abils }"); } f = lfhasflag(lf, F_NEEDOBFORSPELLS); if (f && !hasob(lf->pack, f->val[0])) { if (db) dblog(".oO { Cannot cast spell, I don't have my spellcast object }"); return OT_NONE; } f = lfhasflag(lf, F_CASTTYPE); if (f) { switch (f->val[0]) { case CT_GAZE: // gaze into victim's eyes: they must be able to see you and vice versa if (!cansee(victim, lf) || !cansee(lf, victim)) { if (db) dblog(".oO { Cannot cast spell, no 2way los for gaze attack or victims eyes covered }"); return OT_NONE; } break; case CT_EYESPIT: // spit into the victim's eyes. lof is checked in aispellok if (!cansee(victim, lf) ) { if (db) dblog(".oO { Cannot cast spell, no 2way los for gaze attack or victims eyes covered }"); return OT_NONE; } break; } } getflags(lf->flags, retflag, &nretflags, F_CANCAST, F_CANWILL, F_NONE); for (i = 0; i < nretflags; i++) { f = retflag[i]; if (aispellok(lf, f->val[0], victim, F_AICASTTOATTACK)) { poss[nposs] = f->val[0]; nposs++; } } // select a random one if (nposs > 0) { int sel; if (db) { char lfname[BUFLEN]; getlfname(lf,lfname); dblog(".oO { %s i have %d valid spells/abils. using one. }", lfname, nposs); } sel = rnd(0,nposs-1); return poss[sel]; } return OT_NONE; } enum OBTYPE aigetfleespell(lifeform_t *lf) { flag_t *f; enum OBTYPE poss[MAXPILEOBS]; int nposs = 0,i; int db = B_FALSE; lifeform_t *fleefrom = NULL; flag_t *retflag[MAXCANDIDATES]; int nretflags = 0; if (lfhasflag(lf, F_DEBUG)) { db = B_TRUE; } f = lfhasflag(lf, F_FLEEFROM); if (f) { fleefrom = findlf(lf->cell->map, f->val[0]); } getflags(lf->flags, retflag, &nretflags, F_CANCAST, F_CANWILL, F_NONE); for (i = 0; i < nretflags; i++) { f = retflag[i]; if (aispellok(lf, f->val[0], fleefrom, F_AICASTTOFLEE)) { poss[nposs] = f->val[0]; nposs++; } } // select a random one if (nposs > 0) { int sel; sel = rnd(0,nposs-1); return poss[sel]; } return OT_NONE; } // this function assumes that you can't just SEE the target! cell_t *aigetlastknownpos(lifeform_t *lf, lifeform_t *target, int *lastx, int *lasty, int *lastdir) { flag_t *f, *tflag, *bestflag = NULL; cell_t *c = NULL; int besttime = -1; int i; // defaults if (lastx) *lastx = NA; if (lasty) *lasty = NA; if (lastdir) *lastdir = D_NONE; // check scent/footprints first for (i = 0; i < lf->nlos; i++) { if (hastrailof(lf->los[i]->obpile, target, NA, &tflag, lf)) { if (tflag->lifetime > besttime) { besttime = tflag->lifetime; bestflag = tflag; c = lf->los[i]; } } } if (bestflag && c) { if (lastx) *lastx = c->x; if (lasty) *lasty = c->y; if (lastdir) { // can only obtain direction from footprints if your // tracking skill is high enough. if (bestflag->val[2] == S_SIGHT) { if (getskill(lf, SK_PERCEPTION) >= PR_SKILLED) { *lastdir = bestflag->val[1]; } else { *lastdir = D_NONE; } } else { *lastdir = bestflag->val[1]; } } return c; } f = ispetortarget(lf, target); if (f) { c = getcellat(lf->cell->map, f->val[1], f->val[2]); if (c) { if (lastx) *lastx = c->x; if (lasty) *lasty = c->y; if (lastdir && strlen(f->text)) { *lastdir = atoi(f->text); } } return c; } return NULL; } object_t *aigetrangedattack(lifeform_t *lf, lifeform_t *target, enum RANGEATTACK *ra, int *range) { int db = B_FALSE; enum ATTRBRACKET iqb; object_t *o; if (lfhasflag(lf, F_DEBUG)) db = B_TRUE; if (lfhasflag(lf, F_RAGE)) { if (db) dblog(".oO { no ranged attack because i am enraged }"); if (ra) *ra = RA_NONE; return NULL; } iqb = getattrbracket(getattr(lf, A_IQ), A_IQ, NULL); if (iqb <= IQ_ANIMAL) { // animal and lower intelligence won't use ranged // attacks. if (db) dblog(".oO { no ranged attack due to low iq. }"); if (ra) *ra = RA_NONE; return NULL; } o = getfirearm(lf); if (o && getammo(o)) { if (db) { char gunname[BUFLEN]; getobname(o, gunname, o->amt); if (db) dblog(".oO { will fire my gun (%s) at target. }",gunname); } if (ra) *ra = RA_GUN; if (range) *range = getfirearmrange(o); return o; } // do we have a wand we can zap? if (lfhasflag(lf, F_HUMANOID) || hasbp(lf, BP_HANDS)) { if (lfhasflag(lf, F_FLEEFROM)) { o = aigetwand(lf, F_AICASTTOFLEE); } else { o = aigetwand(lf, F_AICASTTOATTACK); } if (o) { if (db) dblog(".oO { will zap %s instead of moving }", o->type->name); if (ra) *ra = RA_WAND; if (range) *range = getvisrange(lf, B_TRUE); // ie unlimited return o; } } // can we attack by throwing something? if (hasbp(lf, BP_HANDS)) { o = getbestthrowmissile(lf, target); if (o) { if (db) dblog(".oO { will throw %s at my target }", o->type->name); if (ra) *ra = RA_THROW; if (range) { if (hasflag(o->flags, F_POWDER)) { *range = 1; } else { *range = getmaxthrowrange(lf, o); } } return o; } } if (db) dblog(".oO { found no ranged attack. }"); if (ra) *ra = RA_NONE; if (range) *range = 0; return NULL; } void aigetspelltarget(lifeform_t *lf, objecttype_t *spelltype, lifeform_t *victim, lifeform_t **spelllf, cell_t **spellcell, object_t **spellob, enum FLAG purpose) { int specialcase = B_FALSE; flag_t *f; // default - at victim. if (spelllf) *spelllf = victim; if (spellcell) *spellcell = victim->cell; if (spellob) *spellob = NULL; f = hasflag(spelltype->flags, purpose); if (f) { switch (f->val[0]) { case ST_VICTIM: // at victim. if (spelllf) *spelllf = victim; if (spellcell) *spellcell = victim->cell; if (spellob) *spellob = NULL; break; case ST_ADJSELF: // cast at myself when next to victim if (getcelldist(lf->cell, victim->cell) <= 1) { if (spelllf) *spelllf = lf; if (spellcell) *spellcell = lf->cell; if (spellob) *spellob = NULL; } break; case ST_ADJVICTIM: // cast at victim when next to victim if (getcelldist(lf->cell, victim->cell) <= 1) { if (spelllf) *spelllf = victim; if (spellcell) *spellcell = victim->cell; if (spellob) *spellob = NULL; } break; case ST_SELF: if (spelllf) *spelllf = lf; if (spellcell) *spellcell = lf->cell; if (spellob) *spellob = NULL; break; case ST_ANYWHERE: if (spelllf) *spelllf = NULL; if (spellcell) *spellcell = NULL; if (spellob) *spellob = NULL; break; case ST_SPECIAL: specialcase = B_TRUE; break; } if (specialcase) { if (spelltype->id == OT_S_TELEKINESIS) { float maxweight; object_t *poss[MAXPILEOBS]; int nposs; int i; // find nearest object which can be picked up // this is copied out of the telekenesis spell code! maxweight = getlfweight(lf, B_NOOBS) + (getlfweight(lf, B_NOOBS) * (getstatmod(lf, A_IQ) / 100)); nposs = 0; for (i = 0; i < lf->nlos; i++) { if (lf->los[i] != lf->cell) { object_t *o; for (o = lf->los[i]->obpile->first ; o ; o = o->next) { if (!hasflag(o->flags, F_NOPICKUP) && getobweight(o) <= maxweight) { poss[nposs] = o; nposs++; if (nposs >= MAXPILEOBS) break; } } if (nposs >= MAXPILEOBS) break; } } // should always be true since we check this in aispellok if (nposs > 0) { if (spellob) *spellob = poss[rnd(0,nposs-1)]; } // cast spell at the victim if (spelllf) *spelllf = victim; if (spellcell) *spellcell = victim->cell; } else if (spelltype->id == OT_S_CHARM) { lifeform_t *l; l = getnearbypeaceful(lf); if (l) { if (spelllf) *spelllf = l; if (spellcell) *spellcell = l->cell; if (spellob) *spellob = NULL; } } } } } object_t *aigetwand(lifeform_t *lf, enum FLAG purpose) { object_t *o; object_t *poss[MAXPILEOBS]; int nposs = 0; for (o = lf->pack->first ; o ; o = o->next) { // wand with charges left? if ((o->type->obclass->id == OC_WAND) && (getcharges(o) > 0)) { // do we know how to use it? if (hasflag(o->flags, purpose)) { // TODO: if castatself, check whether we actually need to (ie. healing, invis, etc) poss[nposs] = o; nposs++; } } } if (nposs > 0) { return poss[rnd(0,nposs-1)]; } return NULL; } // returns targetcell flag on success flag_t *aigoto(lifeform_t *lf, cell_t *c, enum MOVEREASON why, void *data, int timelimit) { int db = B_FALSE; char whybuf[BUFLEN]; flag_t *f = NULL; if (lfhasflag(lf, F_DEBUG)) { db = B_TRUE; } if (lfhasflagval(lf, F_IGNORECELL, c->x, c->y, NA, NULL)) { char lfname[BUFLEN]; getlfname(lf, lfname); if (db) dblog(".oO { %s cannot go to targecell %d,%d due to f_ignorecell flag }", lfname, c->x, c->y); return NULL; } if (db) { char lfname[BUFLEN]; getlfname(lf, lfname); dblog(".oO { %s going to targecell: %d, %d }", lfname, c->x, c->y); } // kill previous target flags. killflagsofid(lf->flags, F_TARGETLF); killflagsofid(lf->flags, F_TARGETCELL); if (why == MR_LF) { snprintf(whybuf, BUFLEN, "%d", ((lifeform_t *)data)->id); } else if (why == MR_OB) { snprintf(whybuf, BUFLEN, "%ld", ((object_t *)data)->id); } else { strcpy(whybuf, ""); } if ((timelimit == PERMENANT) || (timelimit == UNLIMITED)) { f = addflag(lf->flags, F_TARGETCELL, c->x, c->y, why, whybuf); } else { f = addtempflag(lf->flags, F_TARGETCELL, c->x, c->y, why, whybuf,timelimit); } return f; } flag_t *aihastarget(lifeform_t *lf) { flag_t *f; f = lfhasflag(lf, F_TARGETLF); if (f) return f; f = lfhasflag(lf, F_TARGETCELL); if (f) return f; return NULL; } // returns B_FALSE if we did something. // returns B_TRUE if we failed (ie. did nothing) int aimovetolf(lifeform_t *lf, lifeform_t *target, int wantattack) { int db = B_FALSE; int ismaster = B_FALSE; flag_t *targetflag = NULL; if (lfhasflag(lf, F_DEBUG)) db = B_TRUE; if (db) { dblog(".oO { starting aimovetolf }"); } targetflag = lfhasflagval(lf, F_PETOF, target->id, NA, NA, NULL); if (targetflag) { ismaster = B_TRUE; } else { targetflag = lfhasflagval(lf, F_TARGETLF, target->id, NA, NA, NULL); } if (cansee(lf, target)) { int dist,wantdistmin,wantdistmax; int attackok; enum OBTYPE spell; object_t *rangedob = NULL; enum RANGEATTACK rangedattack = RA_NONE; int shootrange = 0; int movefailed = B_FALSE; int closethrowok = B_FALSE; // pet movement if (ismaster) { if (isresting(target)) { // rest as well. rest(lf, B_TRUE); return B_FALSE; } else if (isadjacent(lf->cell, target->cell)) { if (db) dblog(".oO { i can see my master adjacent - moving randomly }"); // move randomly. TODO: just return ?? dorandommove(lf, B_NOBADMOVES, B_TRUE); return B_FALSE; } // otherwise fall through to below movement code. } // how far away is my target ? dist = getcelldist(lf->cell, target->cell); // how far away do i _want_ to be? getwantdistance(lf,target, &wantdistmin,&wantdistmax, wantattack); // reset F_TARGET lifetime to full. if (targetflag) { if (targetflag->id == F_TARGETLF) { targetflag->lifetime = DEF_AIFOLLOWTIME; } if (db) dblog(".oO { i can see my target (at %d,%d). might move towards it. }",target->cell->x,target->cell->y); // remember their location targetflag->val[1] = target->cell->x; targetflag->val[2] = target->cell->y; } // is an attack possible? attackok = B_FALSE; if (wantattack) { if (dist == 1) { attackok = B_TRUE; } else if (!lfhasflag(lf, F_HIDING)) { attackok = B_TRUE; } else if (!lfhasflag(lf, F_FEIGNINGDEATH)) { attackok = B_TRUE; } } if (attackok) { objecttype_t *st; flag_t *f; int spellchance = 0; // drink boost potions if (!useitemwithflag(lf, F_AIBOOSTITEM)) { return B_FALSE; } // try spells first. // can we attack with spells (ie. ones which target the victim)? // if target is adjacent, we will normally just attack rather than try a spell. // random chance of casting a spell f = lfhasflag(lf, F_CASTCHANCE); if (f) spellchance = f->val[0]; else spellchance = 30; // some attacks can always happen if (cancast(lf, OT_A_THRUST, NULL) && (dist == 2)) { spellchance = 100; } if (pctchance(spellchance)) { spell = aigetattackspell(lf, target); } else { spell = OT_NONE; } st = findot(spell); if ( (spell != OT_NONE) && // found a valid spell/ability to use // AND one of these: ((dist != 1) || // there is distance between us and target (getspellrange(lf, st->id, 1) == 1) || // OR this works from adjacent (st->obclass->id == OC_ABILITY) || // OR this is an ability !countinnateattacks(lf) || // OR we have no melee attack (rnd(1,3) == 1)) // OR random chance of using anyway... ) { int spellfailed = B_FALSE; lifeform_t *spelllf = NULL; cell_t *spellcell = NULL; object_t *spellob = NULL; if (db) { dblog(".oO { will cast attack spell: %s }", st->name); } // special cases: eg. spells like telekenesis if (spell == OT_S_TELEKINESIS) { float maxweight; object_t *poss[MAXPILEOBS]; int nposs; int i; // find nearest object which can be picked up // this is copied out of the telekenesis spell code! maxweight = getlfweight(lf, B_NOOBS) + (getlfweight(lf, B_NOOBS) * (getstatmod(lf, A_IQ) / 100)); nposs = 0; for (i = 0; i < lf->nlos; i++) { if (lf->los[i] != lf->cell) { object_t *o; for (o = lf->los[i]->obpile->first ; o ; o = o->next) { if (!hasflag(o->flags, F_NOPICKUP) && getobweight(o) <= maxweight) { poss[nposs] = o; nposs++; if (nposs >= MAXPILEOBS) break; } } if (nposs >= MAXPILEOBS) break; } } if (nposs > 0) { spellob = poss[rnd(0,nposs-1)]; } else { spellfailed = B_TRUE; } // cast spell at the player spelllf = target; spellcell = target->cell; } else { // pick targets based on spell flags aigetspelltarget(lf, st, target, &spelllf, &spellcell, &spellob, F_AICASTTOATTACK); } if (spellfailed) { if (db) dblog(".oO { cast spell/ability failed (1)! }"); } else { if (getschool(spell) == SS_ABILITY) { spellfailed = useability(lf, spell, spelllf, spellcell); } else { spellfailed = castspell(lf, spell, spelllf, spellob, spellcell, NULL, NULL); } } if (spellfailed) { if (db) dblog(".oO { cast spell/ability tried but failed (2)! reason = %d }", reason); // spell failed. we will keep going through aiturn. } else { // spell succesful if ((spell == OT_A_STEAL) && !lfhasflag(lf, F_NOFLEE)) { // run away for a while fleefrom(lf, spelllf, rnd(10,20), B_TRUE); } return B_FALSE; } } // see if we have a ranged attack. if so, adjust wantdist // to maintain distance. rangedob = aigetrangedattack(lf, target, &rangedattack, &shootrange); // for firearms, chance to fire depends on accuracy. if (rangedattack == RA_GUN) { int acc,chance; acc = getmissileaccuracy(lf, target->cell, getammo(rangedob), rangedob, NULL); switch (getpctletter(acc,100)) { case 'S': case 'A': chance = 100; break; case 'B': chance = 75; break; case 'C': chance = 50; break; default: chance = 25; break; } if (!pctchance(chance)) { rangedattack = RA_NONE; rangedob = NULL; } } if (rangedattack != RA_NONE) { // ie if we found a ranged attack if ((rangedattack == RA_THROW) && rangedob && hasflag(rangedob->flags, F_POWDER)) { // don't have to maintain distance to throw powder closethrowok = B_TRUE; if (wantdistmin < 1) wantdistmin = 1; if (wantdistmax < wantdistmin) wantdistmax = shootrange; } else { // stay out of target's attack range if (wantdistmin < 2) wantdistmin = 2; // and stay within range for our ranged attack if (wantdistmax < wantdistmin) wantdistmax = shootrange; } } } // end if attackok // move towards the target lf. // try to get to our ideal range from them. if (db) dblog(".oO { i am at distance %d, want to be at %d-%d }", dist, wantdistmin, wantdistmax); if (dist > wantdistmax) { if (db) { dblog(".oO { moving towards target. }"); } // do we need to sprint got catch up? if (lfhasflag(target, F_SPRINTING) && !lfhasflag(lf, F_SPRINTING) && cancast(lf, OT_A_SPRINT, NULL)) { useability(lf, OT_A_SPRINT, NULL,NULL); // doesn't matter if it fails } if (!movetowards(lf, target->cell, DT_ORTH, B_FALSE)) { turntoface(lf, target->cell); // success return B_FALSE; } else { if (db) dblog(".oO { move towards failed! - reason = %d }",reason); movefailed = B_TRUE; } } else if (dist < wantdistmin) { if (db) dblog(".oO { moving away from target to maintain mindist %d. }", wantdistmin); if (!moveawayfrom(lf, target->cell, DT_ORTH, B_KEEPLOF, B_TRUE)) { // maintain LOF, and keep facing the target // success return B_FALSE; } else { if (db) dblog(".oO { move towards failed! - reason = %d }",reason); movefailed = B_TRUE; } } // if we got here, we're either at the correct distance or couldn't // move. if (attackok) { // if not adjacent, check for guns, wands, throwing if ( (rangedattack != RA_NONE) && // we have a ranged attack haslof(lf->cell, target->cell, LOF_NEED, NULL) && // and we have line of fire to them (closethrowok || onein(2) || (getcelldist(lf->cell, target->cell) > 1) )) { // and we're not adjacent to target OR random if (rangedattack == RA_GUN) { setguntarget(lf, target); if (!shoot(lf)) { // succesful return B_FALSE; } else { if (db) dblog(".oO { shoot gun failed! reason = %d }", reason); } } else if (rangedattack == RA_THROW) { // try to throw it! if (!throwat(lf, rangedob, target->cell)) { // succesful return B_FALSE; } else { if (db) dblog(".oO { throw failed! }"); } } else if (rangedattack == RA_WAND) { objecttype_t *st; cell_t *zapcell = NULL; st = getlinkspell(rangedob); if (st) { enum FLAG purpose; if (lfhasflag(lf, F_FLEEFROM)) { purpose = F_AICASTTOFLEE; } else { purpose = F_AICASTTOATTACK; } aigetspelltarget(lf, st, target, NULL, &zapcell, NULL, purpose); } else { // no linkspell - just zap it. zapcell = NULL; } // zap it if (!operate(lf, rangedob, zapcell)) { // succesful return B_FALSE; } else { if (db) dblog(".oO { zap failed! }"); } } } // end if rangedattackok } // end if attackok // if we could see our traget, but everything we tried failed (spells, moving and ranged attack), // either rest or move randomly. if (movefailed) { makenoise(lf, N_FRUSTRATED); if (onein(2)) { rest(lf, B_TRUE); } else { if (dorandommove(lf, B_NOBADMOVES, B_FALSE)) { rest(lf, B_TRUE); } } return B_FALSE; } } else { // can't see target. // move towards their last known location instead cell_t *targcell; int lastx,lasty; int lastdir = D_NONE; targcell = aigetlastknownpos(lf, target, &lastx, &lasty, &lastdir); if (targcell) { // are we already AT their last known location? if (targcell == lf->cell) { if (db) dblog(".oO { cannot see target. i am already at their last known loc %d/%d }",lastx, lasty); // go in their last known direction. if (lastdir == D_NONE) { if (db) dblog(".oO { i don't know my target's last known movement dir. }"); } else { // try going in last known dir if (db) dblog(".oO { trying target's last known move dir (%s) }",getdirname(lastdir)); if (!trymove(lf, lastdir, B_TRUE, B_FALSE)) { if (db) dblog(".oO { ...successfully }"); // we now don't know their last known dir. if (targetflag) { free(targetflag->text); targetflag->text = strdup(""); } return B_FALSE; } } } else { // not already at their last known cell. try to go there. if (db) dblog(".oO { i cannot see my target. moving to last known loc %d/%d }",lastx,lasty); if (aigoto(lf, targcell, MR_LF, target, PERMENANT)) { if (db) dblog(".oO { successfuly moved towards LKL. }"); // success return B_FALSE; } else { if (db) dblog(".oO { aigoto target's last known loc failed! }"); } } } else { // we don't know their last known location.... if (db) dblog(".oO { go to target's last known loc failed! }"); } } if (db) dblog(".oO { aimovetolf failed. }"); // FAILED. return B_TRUE; } void aimovetotargetcell(lifeform_t *lf, flag_t *f) { int x,y; cell_t *c; int db = B_FALSE; if (lfhasflag(lf, F_DEBUG)) { db = B_TRUE; } x = f->val[0]; y = f->val[1]; if (db) dblog(".oO { walking from %d,%d towards f_targetcell (%d,%d) ... }", lf->cell->x, lf->cell->y, x, y); c = getcellat(lf->cell->map, x, y); if (c) { // try to move towards the cell if (movetowards(lf, c, DT_ORTH, B_FALSE )) { // couldn't move towards it for some reason. // so stop trying. if (db) dblog(".oO { couldn't walk towards f_targetcell. abandoning it. }"); killflag(f); // remember NOT to target this one. addignorecell(lf, c); } else { if (db) dblog(".oO { successfully walked towards f_targetcell. arrived at %d,%d }",lf->cell->x, lf->cell->y); // moved towards it. // reset lifetime f->lifetime = DEF_AIFOLLOWTIME; // are we there yet? if (lf->cell == c) { // yes. remove target cell if (db) dblog(".oO { arrived at f_targetcell. removing. }"); if (f->val[2] == MR_LF) { lifeform_t *targlf; // if we were chasing someone, keep looking // for them. targlf = findlf(lf->cell->map, atoi(f->text)); if (targlf) { if (db) dblog(".oO { resuming pursuit of %s }", targlf->race->name); addflag(lf->flags, F_TARGETLF, targlf->id, NA, NA, NULL); } } killflag(f); } else { turntoface(lf, c); } } } else { if (db) dblog(".oO { f_targetcell doesn't exist. abandoning. }"); // destination doesn't exist! killflag(f); // remember NOT to target this one. addignorecell(lf, c); } } int aipickup(lifeform_t *lf, object_t *o) { // special case if ((o->type->id == OT_COFFIN) && (lf->race->id == R_GASCLOUD) && lfhasflagval(lf, F_ORIGRACE, R_VAMPIRE, NA, NA, NULL)) { return rest(lf, B_TRUE); } if (isedible(o)) { return eat(lf, o); } else { return pickup(lf, o, o->amt, B_TRUE, B_TRUE); } return B_FALSE; } int aipickupok(lifeform_t *lf, object_t *o) { int ok = B_FALSE; // special case if ((o->type->id == OT_COFFIN) && (lf->race->id == R_GASCLOUD) && lfhasflagval(lf, F_ORIGRACE, R_VAMPIRE, NA, NA, NULL)) { return B_TRUE; } if (hasflag(o->flags, F_SHOPITEM)) { return B_FALSE; } if (isedible(o)) { if (caneat(lf, o) && !isinbattle(lf)) { ok = B_TRUE; } } else if (canpickup(lf, o, 1)) { ok = B_TRUE; } return ok; } int aiobok(lifeform_t *lf, object_t *o, lifeform_t *target) { // non-humanoids can only use food. if (!lfhasflag(lf, F_HUMANOID)) { if (o->type->obclass->id != OC_FOOD) { return B_FALSE; } } switch (o->type->id) { case OT_POT_INVIS: case OT_WAND_INVIS: if (lfhasflag(target, F_INVISIBLE)) { return B_FALSE; } break; case OT_POT_INVULN: if (lfhasflag(target, F_INVULNERABLE)) { return B_FALSE; } break; default: break; } return B_TRUE; } void aiturn(lifeform_t *lf) { int db = B_FALSE; object_t *curwep,*bestwep, *o; int icanattack = B_FALSE; object_t *curgun,*bestgun; flag_t *f; //flag_t *nextf; // lifeform_t *fleefrom = NULL; lifeform_t *target,*newtarget; enum BODYPART bp; //cell_t *c; lifeform_t *master = NULL; enum ATTRBRACKET iqb; int n,i; flag_t *retflag[MAXCANDIDATES]; int nretflags = 0; /* if (wantdb && haslos(player, lf->cell)) { db = B_TRUE; } else { db = B_FALSE; } */ if (wantdb && lfhasflag(lf, F_DEBUG)) { db = B_TRUE; } else { db = B_FALSE; } if (db) { char lfname[BUFLEN]; real_getlfname(lf, lfname, B_FALSE); dblog("AIMOVE: %s, facing %s", lfname, getdirnameshort(lf->facing)); } // if lifeform isn't alive, skip turn if (isdead(lf)) { if (db) dblog(".oO { i am not alive, skipping turn. }"); taketime(lf, SPEED_DEAD); return; } /////////////////////////////////////////// // info gathering /////////////////////////////////////////// // remember our intelligence iqb = getattrbracket(getattr(lf, A_IQ), A_IQ, NULL); // are we a pet? f = lfhasflagval(lf, F_PETOF, NA, NA, NA, NULL); if (f && (getallegiance(lf) == AL_FRIENDLY)) { master = findlf(lf->cell->map, f->val[0]); } /////////////////////////////////////////////// // emergencies / fixing up /////////////////////////////////////////////// if (!lfhasflag(lf, F_RAGE)) { if (iqb >= AT_AVERAGE) { if (celldangerous(lf, lf->cell, B_TRUE, NULL)) { // if our cell is dangerous, move away! if (!dorandommove(lf, B_NOBADMOVES, B_FALSE)) { return; } } } // flying monsters not flying? if (!isprone(lf)) { if (hasflag(lf->race->flags, F_FLYING) && !lfhasflag(lf, F_FLYING)) { copyflag(lf->flags, lf->race->flags, F_FLYING); taketime(lf, getmovespeed(lf)); return; } if (hasflag(lf->race->flags, F_LEVITATING) && !lfhasflag(lf, F_LEVITATING)) { copyflag(lf->flags, lf->race->flags, F_LEVITATING); taketime(lf, getmovespeed(lf)); return; } } } /////////////////////////////////////////////// // housekeeping - weapon changes, drop/pickup, // use items, talk,etc /////////////////////////////////////////////// if (!lfhasflag(lf, F_RAGE)) { if (lfhasflag(lf, F_ISPRISONER) && master && isplayer(master) && cansee(lf, master)) { if (isoutdoors(lf->cell->map) && pctchance(20)) { object_t *o; say(lf, "Thanks for getting me out!", SV_TALK); o = addobfast(master->pack, OT_MANUAL); if (o) { char obname[BUFLEN]; say(lf, "Here, let me teach you something as a reward.", SV_TALK); getobname(o, obname, o->amt); msgnocap("%c - %s", o->letter, obname); } // no longer an ally killflagsofid(lf->flags, F_PETOF); killflagsofid(lf->flags, F_ISPRISONER); } } // too many objects? i = countobs(lf->pack, B_FALSE); if (i >= (MAXPILEOBS - 5)) { object_t *container; // get largest container with space container = getbestcontainer(lf->pack); if (container) { object_t *o; // find object which will fit for (o = lf->pack->first ; o ; o = o->next) { if ((o != container) && !isequipped(o) && obfits(o, container->contents)) { // put it in. moveob(o, container->contents, ALL); if (cansee(player, lf)) { char obname[BUFLEN]; char lfname[BUFLEN]; char containername[BUFLEN]; // announce getobname(o, obname, o->amt); getobname(container, containername, 1); getlfname(lf, lfname); msg("%s puts %s into %s.", lfname, obname, containername); } // timetime taketime(lf, getactspeed(lf)); return; } } } } // talking f = lfhasflag(lf, F_RANDOMTALKPCT); if (f) { if (pctchance(f->val[0])) { flag_t *poss[MAXCANDIDATES]; int nposs = 0,i; getflags(lf->flags, retflag, &nretflags, F_RANDOMTALK, F_NONE); for (i = 0; i < nretflags; i++) { poss[nposs++] = retflag[i]; } if (nposs) { int vol; f = poss[rnd(0,nposs-1)]; vol = rnd(f->val[1], f->val[2]); if (strlen(f->text)) { say(lf, f->text, vol); } else { sayphrase(lf, f->val[0], vol, NA, NULL); } } } } } /////////////////////////////////////////////// // healing /////////////////////////////////////////////// // special cases if ((lf->race->id == R_STIRGE) || (lf->race->id == R_LEECH)) { if (ispeaceful(lf)) { int sleepval = 18; if (modcounter(lf->flags, 1) >= sleepval) { // we say that this ISNT on purpose, because otherwise // we'll wake up as soon as we're healed. in this case // we actually want to sleep forever (or until woken). if (!gotosleep(lf, B_FALSE)) { // force this since when not on purpose, gotosleep wont // take time. taketime(lf, getactspeed(lf)); return; // success } } } } if (!lfhasflag(lf, F_RAGE)) { // feigning death with enemies in sight, and hurt? if (lfhasflag(lf, F_FEIGNINGDEATH) && !safetorest(lf)) { if (isbleeding(lf)) { if (db) dblog(".oO { i am feigning death and bleeding (hp=%d/%d), skipping turn. }",lf->hp,lf->maxhp); // just wait... rest(lf, B_TRUE); return; } } // hurt gods planeshift away if (lf->race->raceclass->id == RC_GOD) { if (gethppct(lf) <= 10) { if (!castspell(lf, OT_S_PLANESHIFT, lf, NULL, lf->cell, NULL, NULL)) { return; } } } // need to heal? if (lf->hp < (lf->maxhp/2)) { if (!useitemwithflag(lf, F_AIHEALITEM)) { return; } else { // don't have or can't use our healing items // no enemies in sight? if (safetorest(lf)) { // if it's "night time" for us, sleep forever. // otehrwise just sleep until we're healed if (!gotosleep(lf, issleepingtimefor(lf) ? B_TRUE : B_FALSE)) { taketime(lf, getactspeed(lf)); // to make sure our turn ends return; // success } } } } // burdened? if (isburdened(lf)) { object_t *o,*heaviest = NULL; float hevweight = 0; if (db) dblog(".oO { i am burdened }"); // drop our heaviest non-equipped object for (o = lf->pack->first ; o ; o = o->next) { if (!isequipped(o)) { float thisweight; thisweight = getobweight(o); if (thisweight > hevweight) { hevweight = thisweight; heaviest = o; } } } if (heaviest) { if (db) { char obname[BUFLEN]; getobname(o, obname, ALL); dblog(".oO { i will drop %s to lower my burden }", obname); } if (!drop(heaviest, ALL)) { return; } if (db) dblog(".oO { drop failed! }"); } if (db) dblog(".oO { couldn't drop anything }"); } // do we have a better weapon we could use? curwep = getweapon(lf); bestwep = getbestweapon(lf); if ((curwep != bestwep) && !isfirearm(curwep)) { if (db) dblog(".oO { i have a better weapon than my current one (%s > %s) }",bestwep->type->name, curwep ? curwep->type->name : "nothing"); // weild better one if (!weild(lf, bestwep)) return; } // do we have a better firearm ? curgun = getfirearm(lf); if (curwep && hasflag(curwep->flags, F_TWOHANDED)) { // we are using a two handed weapon. don't // check for guns. } else { bestgun = getbestfirearm(lf); if (curgun != bestgun) { if (db) dblog(".oO { i have a better gun than my current one (%s > %s) }",bestgun->type->name, curgun ? curgun->type->name : "nothing"); // weild better one if (!weild(lf, bestgun)) return; } } // do we have ammo for an empty gun? if (curgun) { object_t *curammo; curammo = getammo(curgun); if (!curammo) { o = getrandomammo(lf); if (o && !loadfirearm(lf, curgun, o)) { // success return; } } } // do we have better armour? for (bp = BP_RIGHTFINGER ; bp < MAXBODYPARTS; bp++) { object_t *curarm; curarm = getarmour(lf, bp); // do we have a better one? for (o = lf->pack->first ; o ; o = o->next) { if (canwear(lf, o, BP_NONE) && isbetterarmourthan(o, curarm)) { // wear this armour instead if (!wear(lf, o)) return; } } } // now check whetehr we have ANY weapon if (curwep || lfhasflag(lf, F_HASATTACK)) { icanattack = B_TRUE; } // before attacking targets, // look for any object which we _covet_. // ie. if we covet something, we will pick it up // instead of attacking our target. if (!lfhasflag(lf, F_HIDING) && !lfhasflag(lf, F_FEIGNINGDEATH)) { if (db) dblog(".oO { looking for covetted objects... }"); if (lookforobs(lf)) { if (db) dblog(".oO { found covetted object. returning. }"); return; } } } // end if not enraged /////////////////////////////////////////////// // attacks /////////////////////////////////////////////// target = gettargetlf(lf); // do we already have a target we are attacking? if (target) { if (db) dblog(".oO { i have a target: lfid %d (%s). }", target->id, target->race->name); // target dead or unconscious? if (isdead(target) || isunconscious(target)) { if (db) dblog(".oO { my target is dead/ko'd }", target->id, target->race->name); loseaitargets(lf); if (areallies(lf, player) && cantalk(lf)) { char text[BUFLEN]; real_getlfname(target, text, B_FALSE); sayphrase(lf, SP_ALLY_TARGETKILL, SV_SHOUT, NA, text); } } else { // aquatic grabbers will try to drag their prey into the water if (lfhasflagval(lf, F_GRABBING, target->id, NA, NA, NULL) && isaquatic(lf) ) { if ( hasobwithflag(lf->cell->obpile, F_DEEPWATER) && !hasobwithflag(target->cell->obpile, F_DEEPWATER)) { // move away! if (!moveawayfrom(lf, target->cell, DT_ORTH, B_FALSE, B_TRUE)) { return; } } } // try to move towards them. if (!aimovetolf(lf, target, B_TRUE)) { // success return; } } } /////////////////////////////////////////////// // generic pre-movement actions. /////////////////////////////////////////////// // need light? if (!haslos(lf, lf->cell)) { object_t *lamp; lamp = hasobwithflagval(lf->pack, F_ACTIVATECONFER, F_PRODUCESLIGHT, NA, NA, NULL); if (lamp && !isactivated(lamp)) { if (db) dblog(".oO { it's dark and i have an inactive light source (%s) }", lamp->type->name); if (!operate(lf, lamp, NULL)) { if (db) dblog(".oO { successfully turned it on. }"); return; } else { if (db) dblog(".oO { failed to turn it on. }"); } } } // ally needs ammo? for (i = 0; i < lf->nlos; i++) { cell_t *c; c = lf->los[i]; if (c->lf && (c->lf != lf) && areallies(lf, c->lf)) { object_t *gun; gun = getfirearm(c->lf); if (gun && !getammo(gun)) { object_t *o; for (o = lf->pack->first ; o ; o = o->next) { if (isammofor(o->type, gun) ) { if (getcelldist(lf->cell, c) <= getmaxthrowrange(lf, o)) { // throw it to them! if (!throwat(lf, o, c)) { // success return; } } } } } } } /////////////////////////////////////////////// // movement /////////////////////////////////////////////// // do we have a target cell? f = hasflag(lf->flags, F_TARGETCELL); if (f) { int valid = B_TRUE; cell_t *c; // is it still valid? c = getcellat(lf->cell->map, f->val[0], f->val[1]); if (!c) { valid = B_FALSE; } else if (f->val[2] == MR_LF) { lifeform_t *who; who = findlf(lf->cell->map, atoi(f->text)); // lf doesn't exist? if (!who) { valid = B_FALSE; } else if (cansee(lf, who) && (lf->cell != c)) { // can see them and they're not where we are going? valid = B_FALSE; } } else if (f->val[2] == MR_OB) { object_t *what; what = findobidinmap(lf->cell->map, atol(f->text)); if (!what) { valid = B_FALSE; } else if (haslos(lf, c) && (what->pile->where != c)) { // if you can see the cell and object isn't there anymore valid = B_FALSE; } else if (c->lf && !areenemies(lf, c->lf) && haslos(lf, c) && (getcelldist(lf->cell, c) == 1)) { // can see a non-enemy on top of the object, and we are adjacent valid = B_FALSE; } } if (valid) { aimovetotargetcell(lf, f); } else { killflag(f); } return; } /////////////////////////////////////////////// // look for something to do (objects, things // to attack, etc) /////////////////////////////////////////////// // not attacking anyone in particular if (db) dblog(".oO { i do not have a target or can't move towards it. }"); // shopkeepers will return to their shops if (hasjob(lf, J_SHOPKEEPER)) { f = lfhasflag(lf, F_OWNSSHOP); if (f) { int myshop; cell_t *where; myshop = f->val[0]; // find the closest cell of my shop where = getclosestroomcell(lf, myshop); // move towards my shop. note that if the player leaves then // re-enters this map, they will find that we have instantly // teleported there (see mapentereffects). if (aigoto(lf, where, MR_OTHER, NULL, PERMENANT)) { // success return; } } } if (!lfhasflag(lf, F_STUNNED)) { lifeform_t *hateposs[MAXCANDIDATES],*poss[MAXCANDIDATES]; int nposs = 0, nhateposs = 0; if (db) dblog(".oO { looking for a target . }"); // look for any hated lfs or enemies newtarget = NULL; for (n = 0; n < lf->nlos; n++) { lifeform_t *who; if (lf->los[n] != lf->cell) { // not ourself who = lf->los[n]->lf; if (who && !isdead(who) && !isunconscious(who) && cansee(lf, who)) { int chance = 100; // chance that we ('lf') will attack 'who' int reachpenalty; // will usually ignore targets who we can't reach if (!canreach(lf, who, &reachpenalty)) { if (!aigetrangedattack(lf, who, NULL, NULL)) { // no ranged attack? // 1 size too small = 53% chance to attack // 1 size too small = 6% chance to attack chance = 100 - (reachpenalty*47); if (db) dblog(".oO { target %d (%s) is %d sizes out of reach. %d%% chance to ignore }",who->id, who->race->name, reachpenalty, reachpenalty*47); } } else if (isresting(who)) { // targets sleeping in a tent will probably be ignored object_t *restob; restob = getrestob(who); if (restob) { switch (restob->type->id) { case OT_TENT: chance = 5; break; case OT_MOTEL: chance = 0; break; default: break; } } } if (pctchance(chance)) { if (lfhasflagval(lf, F_HATESRACE, who->race->id, NA, NA, NULL) || lfhasflagval(lf, F_HATESRACE, who->race->baseid, NA, NA, NULL) ) { if (nhateposs < MAXCANDIDATES) { if (db) dblog(".oO { found a hated target - lfid %d (%s) ! }",who->id, who->race->name); hateposs[nhateposs++] = who; } break; } else if (!nhateposs && areenemies(lf, who)) { // dont check if we've already found a hated target if (nposs < MAXCANDIDATES) { if (db) dblog(".oO { found an enemy target - lfid %d (%s) ! }",who->id, who->race->name); poss[nposs++] = who; } } else { getflags(lf->flags, retflag, &nretflags, F_HATESRACEWITHFLAG, F_NONE); for (i = 0; i < nretflags; i++) { if (lfhasflag(who, retflag[i]->id)) { if (db) dblog(".oO { found a target with hated flags - lfid %d (%s) ! }",who->id, who->race->name); hateposs[nhateposs++] = who; } } } } } } } if (nhateposs) { newtarget = hateposs[rnd(0,nhateposs-1)]; } else if (nposs) { newtarget = poss[rnd(0,nposs-1)]; } } if (newtarget) { if (aiattack(lf, newtarget, DEF_AIFOLLOWTIME)) { // failed for some reason. maybe target was feigning // death? if (db) dblog(".oO { setting a new target via aiattack failed! }"); } else { // then move towards them... if (db) dblog(".oO { moving towards my new target (%d,%d) -> (%d,%d) }", lf->cell->x, lf->cell->y, newtarget->cell->x, newtarget->cell->y); if (icanattack) { if (!movetowards(lf, newtarget->cell, DT_ORTH, B_FALSE)) { turntoface(lf, newtarget->cell); return; } } else { if (db) dblog(".oO { won't move towards target - i have no weapon. }"); } } } else { // god with no targets? if (lf->race->raceclass->id == RC_GOD) { if (onein(6)) { // gods will planeshift away if (!castspell(lf, OT_S_PLANESHIFT, lf, NULL, lf->cell, NULL, NULL)) { return; } } } } /////////////////////////////////////////////// // training /////////////////////////////////////////////// // need to train skills? if (readytotrain(lf)) { if (safetorest(lf)) { // special case - monsters don't need to actually rest to gain // skills, although they DO still need to wait until the player // is out of sight. enhanceskills(lf); } } // do we have armour which needs repairing? if (getskill(lf, SK_ARMOUR) >= PR_SKILLED) { if (db) dblog(".oO { do i have any armour to repair? }"); // just try to use the ability - it'll fail if we have nothing // which we can repair. if (!useability(lf, OT_A_REPAIR, NULL, NULL)) { if (db) dblog(".oO { yes - done. }"); // success return; } else { if (db) dblog(".oO { no armour to repair. }"); } } // pet movement - note that pets will only rest if their // master is resting. the normal rest code underneath this section // will never be called. if (master) { //lifeform_t *master; //master = findlf(lf->cell->map, mf->val[0]); if (!aimovetolf(lf, master, B_FALSE)) { // success return; } } /////////////////////////////////////////////// // resting / healing /////////////////////////////////////////////// // need to heal? if (needstorest(lf, NULL) && safetorest(lf)) { if (db) dblog(".oO { resting to heal }"); rest(lf, B_TRUE); return; } // DEFAULT - try to move in a random direction if (db) dblog(".oO { default - moving randomly }"); dorandommove(lf, B_NOBADMOVES, B_TRUE); // this function will call rest() if we cant move } // is the spell 'spellid' okay for AI lifeform 'lf' to cast at 'victim', for given purpose. // purpose could be F_AICASTTOFLEE or F_ATCASTTOATTACK int aispellok(lifeform_t *lf, enum OBTYPE spellid, lifeform_t *victim, enum FLAG purpose) { objecttype_t *ot; flag_t *f; int db = B_FALSE; int ok = B_FALSE; int specialcase = B_FALSE; int specificcheckok = B_FALSE; int needlos = B_TRUE; enum LOFTYPE needlof = LOF_NEED; if (lfhasflag(lf, F_DEBUG)) { db = B_TRUE; } if (lfhasflag(lf, F_RAGE)) { if (db) dblog(".oO { can't cast spells, i am enraged }"); return B_FALSE; } ot = findot(spellid); if (ot) { flag_t *f; f = hasflag(ot->flags, F_LOSLOF); if (f) { needlos = f->val[0]; needlof = f->val[1]; } } // override needlos/lof based on cast type f = lfhasflag(lf, F_CASTTYPE); if (f) { switch (f->val[0]) { case CT_GAZE: needlos = B_TRUE; break; case CT_EYESPIT: needlof = LOF_NEED; break; } } // enough mp etc? if (!cancast(lf, spellid, NULL)) { if (db) { char why[BUFLEN]; if (reason == E_NOMP) { strcpy(why, "not enough mp"); } else if (reason == E_NOSTAM) { strcpy(why, "not enough stamina"); } else if (reason == E_TOOPOWERFUL) { strcpy(why, "spell too powerful"); } else if (reason == E_NOTREADY) { strcpy(why, "abil not ready"); } else if (reason == E_NEEDGRAB) { strcpy(why, "needs grab"); } else { strcpy(why, "unknown reason"); } if (db) { if (ot) { dblog(".oO { can't cast %s right now (%s) (mpcost=%d, i have %d) }", ot ? ot->name : "?unkownspell?", why, getmpcost(lf, ot->id), lf->mp); } else { dblog(".oO { can't cast ?unknownspell? right now }"); } } } return B_FALSE; } // boost spell already active? if (hasflag(ot->flags, F_ONGOING)) { if (lfhasflagval(lf, F_BOOSTSPELL, ot->id, NA, NA, NULL)) { if (db) { dblog(".oO { can't cast %s - it is already active.", ot ? ot->name : "?unkownspell?"); } return B_FALSE; } } f = hasflag(ot->flags, purpose); if (f) { if ((f->val[1] == NA) || pctchance(f->val[1])) { int range; switch (f->val[0]) { case ST_VICTIM: range = getspellrange(lf, spellid, getspellpower(lf, spellid)); if ((range == UNLIMITED) || (getcelldist(lf->cell, victim->cell) <= range)) { if (db) { dblog(".oO { spell possibility: %s }", ot ? ot->name : "?unkownspell?"); } ok = B_TRUE; } break; case ST_SELF: case ST_ANYWHERE: if (db) { dblog(".oO { spell possibility: %s }", ot ? ot->name : "?unkownspell?"); } ok = B_TRUE; break; case ST_ADJVICTIM: if (getcelldist(lf->cell,victim->cell) == 1) { if (ot->id == OT_A_GRAB) { if (lfhasflag(lf, F_GRABBING) || lfhasflag(lf, F_GRABBEDBY) || lfhasflag(victim, F_GRABBING) || lfhasflag(victim, F_GRABBEDBY)) { } else { ok = B_TRUE; } } else if (ot->id == OT_A_CRUSH) { // can only crush if you first grab something if (lfhasflag(lf, F_GRABBING)) { ok = B_TRUE; } } else if (ot->id == OT_A_SUCKBLOOD) { // must attach first if (lfhasflag(lf, F_ATTACHEDTO)) { ok = B_TRUE; } } else { ok = B_TRUE; } } break; case ST_ADJSELF: if (getcelldist(lf->cell,victim->cell) == 1) { ok = B_TRUE; } break; case ST_SPECIAL: specialcase = B_TRUE; break; } // now check for line of sight / fire switch (f->val[0]) { case ST_VICTIM: case ST_ADJVICTIM: if (needlos && (!victim || !cansee(lf, victim)) ) { if (db) dblog(".oO { cant cast %s - no LOS to victim }", ot ? ot->name : "?unkownspell?"); return B_FALSE; } if (needlof && !haslof(lf->cell, victim->cell, needlof, NULL) ) { if (db) dblog(".oO { cant cast %s - no LOF to victim }", ot ? ot->name : "?unkownspell?"); return B_FALSE; } break; default: break; } } else { // failed pctchance for spell if (db) dblog(".oO { failed pct check for casting %s }", ot ? ot->name : "?unkownspell?"); return B_FALSE; } } else { // invalid spell for this purpose if (db) dblog(".oO { cant cast %s - not valid for given purpose }", ot ? ot->name : "?unkownspell?"); return B_FALSE; } if (specialcase) { if (ot->id == OT_S_PYROMANIA) { int i; for (i = 0; i < lf->nlos; i++) { if ((lf->los[i] != lf->cell) && getflamingobs(lf->los[i]->obpile, NULL, NULL)) { ok = B_TRUE; } } } else if (ot->id == OT_S_TELEKINESIS) { int i,nposs; float maxweight; maxweight = getlfweight(lf, B_NOOBS) + (getlfweight(lf, B_NOOBS) * (getstatmod(lf, A_IQ) / 100)); nposs = 0; for (i = 0; i < lf->nlos; i++) { if (lf->los[i] != lf->cell) { object_t *o; for (o = lf->los[i]->obpile->first ; o ; o = o->next) { if (!hasflag(o->flags, F_NOPICKUP) && getobweight(o) <= maxweight) { ok = B_TRUE; break; } if (ok) break; } } } } else if (ot->id == OT_S_CHARM) { if (getnearbypeaceful(lf)) { ok = B_TRUE; } } else { if (db) dblog(".oO { cant cast %s - specialcase conditions not yet coded }", ot ? ot->name : "?unkownspell?"); return B_FALSE; } } if (!ok) { if (db) dblog(".oO { cant cast %s - targetting conditions cannot be met }", ot ? ot->name : "?unkownspell?"); return B_FALSE; } // now check whether it meets specific spell conditions specificcheckok = B_TRUE; if (ot->id == OT_S_AIRBLAST) { // target must be in a straight compass dir from us if ((victim->x != lf->x) || (victim->y != lf->y) || (abs(victim->x - lf->x) != abs(victim->y - lf->y)) ) { } else { specificcheckok = B_FALSE; } } if (ot->id == OT_S_ANIMATEMETAL) { object_t *wep; wep = getweapon(lf); if (!wep || !ismetal(wep->material->id)) { specificcheckok = B_TRUE; } } if ((ot->id == OT_S_BLINDNESS) && isblind(victim)) { specificcheckok = B_FALSE; } if (ot->id == OT_A_CLIMB) { if (!canclimb(lf, NULL)) { specificcheckok = B_FALSE; } } if (ot->id == OT_A_DISARM) { if (!getweapon(victim)) { specificcheckok = B_FALSE; } } if ((ot->id == OT_S_DISPERSAL) && (lf->race->raceclass->id == RC_GOD)) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_DRAINLIFE) && isimmuneto(victim->flags, DT_NECROTIC)) { specificcheckok = B_FALSE; } if (ot->id == OT_A_FLIP) { if (getlfsize(victim) > getlfsize(lf)) { specificcheckok = B_FALSE; } } if (ot->id == OT_A_FLURRY) { if (!isdualweilding(lf)) { specificcheckok = B_FALSE; } } if ((ot->id == OT_S_FREEZEOB) && lfhasflag(lf, F_FREEZINGTOUCH)) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_HASTE) && (lfhasflag(victim, F_FASTACT) || lfhasflag(victim, F_FASTACTMOVE)) ) { specificcheckok = B_FALSE; } if (ot->id == OT_A_HEAVYBLOW) { object_t *w; w = getweapon(lf); if (!w || !isheavyweapon(w)) { specificcheckok = B_FALSE; } } if (ot->id == OT_A_HIDE) { if (lfhasflag(victim, F_HIDING)) { specificcheckok = B_FALSE; } else if (lfhasflag(victim, F_FEIGNINGDEATH)) { specificcheckok = B_FALSE; } else if (!safetorest(victim)) { specificcheckok = B_FALSE; } } if ((ot->id == OT_S_INVISIBILITY) && lfhasflag(victim, F_INVISIBLE)) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_PAIN) && lfhasflag(victim, F_PAIN)) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_HEALING) && (lf->hp >= lf->maxhp)) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_HEALINGMIN) && (lf->hp >= lf->maxhp)) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_PARALYZE) && lfhasflag(victim, F_PARALYZED)) { specificcheckok = B_FALSE; } if (ot->id == OT_A_SHIELDBASH) { if (!getshield(lf)) { specificcheckok = B_FALSE; } } if ((ot->id == OT_S_SLEEP) && lfhasflag(victim, F_ASLEEP)) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_SLOW) && (lfhasflag(victim, F_SLOWACT) || lfhasflag(victim, F_SLOWACTMOVE)) ) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_SMITEGOOD) && (getalignment(victim) != AL_GOOD)) { specificcheckok = B_FALSE; } if ((ot->id == OT_S_SMITEEVIL) && (getalignment(victim) != AL_EVIL)) { specificcheckok = B_FALSE; } if (ot->id == OT_A_SPRINT) { if (lfhasflag(lf, F_SPRINTING) || !getstamina(lf) || (getstamina(lf) <= (getmaxstamina(lf)/2))) { specificcheckok = B_FALSE; } if (isairborne(lf)) { specificcheckok = B_FALSE; } } if ((ot->id == OT_A_STEAL) || (ot->id == OT_S_CONFISCATE)) { if (!countobs(victim->pack, B_FALSE)) { specificcheckok = B_FALSE; } } if ((ot->id == OT_A_SWOOP) || (ot->id == OT_A_CHARGE)) { cell_t *adjcell; flag_t *willflag, *srflag; int srange = 5; willflag = lfhasflagval(lf, F_CANWILL, ot->id, NA, NA, NULL); if (willflag) { texttospellopts(f->text, "range:", &srange, NULL); if (!srange) srange = 5; } // override... srflag = lfhasflag(lf, F_SWOOPRANGE); if (srflag) { srange = srflag->val[0]; } adjcell = get_closest_adjcell(lf->cell, victim->cell); if (!adjcell || celldangerous(lf, adjcell, B_TRUE, NULL)) { // don't charge into dangerous cells specificcheckok = B_FALSE; } else if (!haslof(lf->cell, victim->cell, LOF_NEED,NULL)) { specificcheckok = B_FALSE; } else if (isimmobile(lf)) { specificcheckok = B_FALSE; } else if ((ot->id == OT_A_SWOOP) && !lfhasflag(lf, F_FLYING)) { specificcheckok = B_FALSE; } else if (getcelldist(lf->cell, victim->cell) > srange) { specificcheckok = B_FALSE; } else if (getcelldist(lf->cell, victim->cell) == 1) { // ie already adjacent specificcheckok = B_FALSE; } } if ((ot->id == OT_A_TUMBLE) || (ot->id == OT_A_JUMP)) { if (lfhasflag(lf, F_GRABBING) || lfhasflag(lf, F_GRABBEDBY) || isairborne(lf)) { specificcheckok = B_FALSE; } } if ((ot->id == OT_S_WARPWOOD)) { specificcheckok = B_FALSE; if (victim) { object_t *oo; for (oo = victim->pack->first ; oo ; oo = oo->next) { if ((oo->type->material->id == MT_WOOD) && isequipped(oo)) { specificcheckok = B_TRUE; break; } } } } 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; } } } if (!specificcheckok) { if (db) dblog(".oO { cant cast %s - specific spell check failed }", ot ? ot->name : "?unkownspell?"); return B_FALSE; } return B_TRUE; } int aiwants(lifeform_t *lf, object_t *o, int *covets) { enum OBTYPE oid[MAXPILEOBS]; int oidcovet[MAXPILEOBS]; int noids = 0; enum FLAG wantflag[MAXPILEOBS]; int wantflagcovet[MAXPILEOBS]; int nwantflags = 0; makewantedoblist(lf, &noids, oid, oidcovet, &nwantflags, wantflag, wantflagcovet); return aiwants_real(lf, o, covets, &noids, oid, oidcovet, &nwantflags, wantflag, wantflagcovet); } int aiwants_real(lifeform_t *lf, object_t *o, int *covets, int *noids, enum OBTYPE *oid, int *oidcovet,int *nwantflags, enum FLAG *wantflag, int *wantflagcovet) { int i; for (i = 0; i < *noids; i++) { if (oid[i] == o->type->id) { if (covets) { *covets = oidcovet[i]; } return B_TRUE; } } for (i = 0; i < *nwantflags; i++) { if (hasflag(o->flags, wantflag[i])) { if ((wantflag[i] == F_EDIBLE) && !caneat(lf, o)) { // special case } else { if (covets) { *covets = wantflagcovet[i]; } return B_TRUE; } } } return B_FALSE; } lifeform_t *gettargetlf(lifeform_t *lf) { flag_t *f; lifeform_t *target = NULL; f = hasflag(lf->flags, F_TARGETLF); if (f) { target = findlf(lf->cell->map, f->val[0]); } return target; } object_t *hasbetterarmour(lifeform_t *lf, obpile_t *op) { object_t *o; for (o = op->first ; o ; o = o->next) { if (isarmour(o)) { object_t *curarm; enum BODYPART bp; flag_t *f; // where does it go? f = hasflag(o->flags, F_GOESON); bp = f->val[0]; // is it better than what we have in that position? curarm = getarmour(lf, bp); if (isbetterwepthan(o, curarm, lf)) { return o; } } } return NULL; } object_t *hasbetterweapon(lifeform_t *lf, obpile_t *op) { object_t *bestwep, *o; bestwep = getbestweapon(lf); for (o = op->first ; o ; o = o->next) { if (isweapon(o) && isbetterwepthan(o, bestwep, lf) && canweild(lf, o)) { return o; } } return NULL; } // returns B_TRUE if we did something int lookforobs(lifeform_t *lf) { object_t *o,*nexto; enum OBTYPE oid[MAXPILEOBS]; int oidcovet[MAXPILEOBS]; int noids = 0; enum FLAG wantflag[MAXPILEOBS]; int wantflagcovet[MAXPILEOBS]; int nwantflags = 0; flag_t *f; cell_t *c; int i; int db = B_FALSE; lifeform_t *target; int targdist = 999; int covets = B_FALSE; target = gettargetlf(lf); if (target) { targdist = getcelldist(lf->cell, target->cell); } if (wantdb && lfhasflag(lf, F_DEBUG)) { db = B_TRUE; } else { db = B_FALSE; } makewantedoblist(lf, &noids, oid, oidcovet, &nwantflags, wantflag, wantflagcovet); // current cell has an object we want? for (o = lf->cell->obpile->first ; o ; o = nexto) { nexto = o->next; if (aiwants_real(lf, o, &covets, &noids, oid, oidcovet, &nwantflags, wantflag, wantflagcovet)) { int getit = B_TRUE; // if we are in battle only go for it if we covet it if (target && !covets) getit = B_FALSE; if (isdangerousob(o, lf, B_TRUE) || !aipickupok(lf, o)) getit = B_FALSE; if (getit) { if (db) dblog(".oO { current cell has ob i want (%s) }",o->type->name); // try to pick it up if (!aipickup(lf, o)) return B_TRUE; if (db) dblog(".oO { pickup of %s failed, trying to eat! }",o->type->name); if (!eat(lf, o)) return B_TRUE; if (db) dblog(".oO { eating %s failed }",o->type->name); } } } // current cell has better weapon? if (lfhasflag(lf, F_HUMANOID) && hasbp(lf, BP_WEAPON)) { f = hasflag(lf->flags, F_WANTSBETTERWEP); if (f) { // if we are in battle only go for it if we covet it if (!target || (f->val[1] == B_COVETS)) { o = hasbetterweapon(lf, lf->cell->obpile); if (o && !isdangerousob(o, lf, B_TRUE) && aipickupok(lf, o) && canpickup(lf, o, 1)) { if (db) dblog(".oO { current cell has better weapon (%s) }",o->type->name); // try to pick it up if (!aipickup(lf, o)) return B_TRUE; if (db) dblog(".oO { pickup of better wep %s failed! }",o->type->name); } } } } if (lfhasflag(lf, F_HUMANOID)) { // current cell has better armour? f = hasflag(lf->flags, F_WANTSBETTERARM); if (f ) { // if we are in battle only go for it if we covet it if (!target || (f->val[1] == B_COVETS)) { o = hasbetterarmour(lf, lf->cell->obpile); if (o && !isdangerousob(o, lf, B_TRUE) && aipickupok(lf, o) && canpickup(lf, o, 1)) { if (db) dblog(".oO { current cell has better armour (%s) }",o->type->name); // try to pick it up if (!aipickup(lf, o)) return B_TRUE; if (db) dblog(".oO { pickup of better armour %s failed! }",o->type->name); } } } } // look around for objects which we want, if we don't already have a targetcell. if (!hasflag(lf->flags, F_TARGETCELL)) { if (db) dblog(".oO { no targetcell, so looking for remote objects }"); for (i = 0 ; i < lf->nlos; i++) { int gothere = B_FALSE; c = lf->los[i]; if ((c != lf->cell) && !c->lf && !lfhasflagval(lf, F_IGNORECELL, c->x, c->y, NA, NULL)) { int celldist; celldist = getcelldist(lf->cell, c); for (o = c->obpile->first ; o ; o = nexto) { nexto = o->next; if (aiwants_real(lf, o, &covets, &noids, oid, oidcovet, &nwantflags, wantflag, wantflagcovet)) { gothere = B_TRUE; // if we are in battle only go for it if we covet it and // it's closer than our target if (target) { if (!covets || (celldist > targdist)) { gothere = B_FALSE; } } if (gothere) { if (db) dblog(".oO { remote cell has ob i want (%s). setting f_targetcell. }",o->type->name); break; } } // end if aiwantsthisob } // end foreach ob in cell if (!gothere) { if (lfhasflag(lf, F_HUMANOID) && hasbp(lf, BP_WEAPON)) { // remote cell has better weapon? f = hasflag(lf->flags, F_WANTSBETTERWEP); if (f) { // if we are in battle only go for it if we covet it if (!target || ((f->val[1] != B_COVETS) && (celldist <= targdist)) ) { o = hasbetterweapon(lf, c->obpile); if (o && !isdangerousob(o, lf, B_TRUE) && aipickupok(lf, o) && canpickup(lf, o, 1)) { if (db) dblog(".oO { remote cell has better weapon (%s). setting f_targetcell }",o->type->name); gothere = B_TRUE; } } } } } if (!gothere) { if (lfhasflag(lf, F_HUMANOID)) { // remote cell has better armour? f = hasflag(lf->flags, F_WANTSBETTERARM); if (f) { if (!target || ((f->val[1] != B_COVETS) && (celldist <= targdist)) ) { o = hasbetterarmour(lf, c->obpile); if (o && !isdangerousob(o, lf, B_TRUE) && aipickupok(lf, o) && canpickup(lf, o, 1)) { if (db) dblog(".oO { remote cell has better armour (%s). setting f_targetcell }",o->type->name); gothere = B_TRUE; } } } } } if (gothere) { // cast a spell? if (cancast(lf, OT_S_CALLWIND, NULL) && haslof(lf->cell, c, LOF_NEED, NULL)) { if (!castspell(lf, OT_S_CALLWIND, NULL, o, c, NULL, NULL)) { // successful return B_TRUE; } } // start walking towards target cell if (aigoto(lf, c, MR_OB, o, DEF_AIFOLLOWTIME)) { return B_FALSE; } } } } } if (db) dblog(".oO { didn't find any obs i want }"); return B_FALSE; } int loseaitargets(lifeform_t *lf) { int donesomething = B_FALSE; if (killflagsofid(lf->flags, F_TARGETLF)) donesomething = B_TRUE; if (killflagsofid(lf->flags, F_TARGETCELL)) donesomething = B_TRUE; return donesomething; } void makewantedoblist(lifeform_t *lf, int *noids, enum OBTYPE *oid, int *oidcovet,int *nwantflags, enum FLAG *wantflag, int *wantflagcovet) { int i; flag_t *f; flag_t *retflag[MAXCANDIDATES]; int nretflags = 0; // construct a list of objects which we want *noids = 0; getflags(lf->flags, retflag, &nretflags, F_WANTS, F_WANTSOBFLAG, F_NONE); for (i = 0; i < nretflags; i++) { f = retflag[i]; if (f->id == F_WANTS) { oid[*noids] = f->val[0]; oidcovet[*noids] = (f->val[1] == B_COVETS) ? B_TRUE : B_FALSE; (*noids)++; } else if (f->id == F_WANTSOBFLAG) { wantflag[*nwantflags] = f->val[0]; wantflagcovet[*nwantflags] = (f->val[1] == B_COVETS) ? B_TRUE : B_FALSE; (*nwantflags)++; } } if (hasflag(lf->flags, F_HUNGER)) { // monsters dont normaly need to eat. if they have hunger, it's due // to a spell. wantflag[*nwantflags] = F_EDIBLE; wantflagcovet[*nwantflags] = B_TRUE; (*nwantflags)++; } } // try to use an item with the given flag on ourself. // returns B_FALSE if successful int useitemwithflag(lifeform_t *lf, enum FLAG whichflag) { object_t *o; for (o = lf->pack->first ; o ; o = o->next) { if (hasflag(o->flags, whichflag)) { if (aiobok(lf, o, lf)) { if (o->type->obclass->id == OC_POTION) { if (canquaff(lf, o)) { quaff(lf, o); return B_FALSE; } } else if (o->type->obclass->id == OC_SCROLL) { if (!readsomething(lf, o)) { return B_FALSE; } } else if ((o->type->obclass->id == OC_WAND) && getcharges(o)) { // if wand, use it on ourself if (!operate(lf, o, lf->cell)) { return B_FALSE; } // here on are special cases } else if (o->type->id == OT_ASHCONCEAL) { throwat(lf, o, lf->cell); } } } } // failed to use an item return B_TRUE; }