nexus/flag.c

446 lines
9.7 KiB
C
Raw Normal View History

2010-12-02 12:17:54 +11:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "flag.h"
2011-02-01 06:16:13 +11:00
#include "io.h"
#include "lf.h"
#include "objects.h"
2010-12-02 12:17:54 +11:00
#include "text.h"
2011-02-01 06:16:13 +11:00
extern int gamestarted;
extern int needredraw;
extern int statdirty;
2011-02-01 06:16:13 +11:00
2010-12-02 12:17:54 +11:00
flag_t *addflag(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3, char *text) {
return addflag_real(fp, id, val1, val2, val3, text, PERMENANT, B_KNOWN, -1);
2011-02-01 06:16:13 +11:00
}
flag_t *addtempflag(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3, char *text, int timeleft) {
return addflag_real(fp, id, val1, val2, val3, text, timeleft, B_KNOWN, -1);
2011-02-01 06:16:13 +11:00
}
flag_t *addflag_real(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3, char *text, int lifetime, int known, long obfromid) {
2010-12-02 12:17:54 +11:00
flag_t *f;
int i;
int doredraw = B_FALSE;
2010-12-02 12:17:54 +11:00
2011-02-01 06:16:13 +11:00
// identified things mean all new flags are autmaticlaly known.
if (hasflag(fp, F_IDENTIFIED)) {
known = B_KNOWN;
}
2011-02-01 06:16:13 +11:00
// certain flags stack...
if (flagstacks(id)) {
f = hasflag(fp, id);
if (f) {
// add values!
f->val[0] += val1;
f->val[1] += val2;
f->val[2] += val3;
// TODO: how to handle text??
return f;
}
}
2010-12-02 12:17:54 +11:00
if (fp->first == NULL) {
fp->first = malloc(sizeof(flag_t));
f = fp->first;
f->prev = NULL;
} else {
// go to end of list
f = fp->last;
f->next = malloc(sizeof(flag_t));
f->next->prev = f;
f = f->next;
}
fp->last = f;
f->next = NULL;
// fill in props
2011-02-01 06:16:13 +11:00
f->id = id;
f->lifetime = lifetime;
f->known = known;
f->obfrom = obfromid;
2010-12-02 12:17:54 +11:00
// first blank values
for (i = 0; i < 3; i++) {
2010-12-07 18:34:26 +11:00
f->val[i] = NA;
2010-12-02 12:17:54 +11:00
}
f->val[0] = val1;
f->nvals = 1;
if (val2 != NA) {
f->val[1] = val2;
f->nvals++;
}
if (val3 != NA) {
2010-12-07 18:34:26 +11:00
f->val[2] = val3;
2010-12-02 12:17:54 +11:00
f->nvals++;
}
if (text) {
f->text = strdup(text);
} else {
f->text = strdup("");
}
f->pile = fp;
2011-02-01 06:16:13 +11:00
// notify
if (gamestarted) {
if (f->pile->owner) {
if (announceflaggain(f->pile->owner, f)) {
2011-03-04 12:22:36 +11:00
interrupt(f->pile->owner);
2011-02-01 06:16:13 +11:00
f->known = B_TRUE;
if (f->obfrom) { // ooooooo it's getting -1 ?!?!?!?
object_t *ob;
ob = findobbyid(f->pile->owner->pack, f->obfrom);
if (ob) {
flag_t *f2;
switch (f->lifetime) {
case FROMOBEQUIP:
f2 = hasflagval(ob->flags, F_EQUIPCONFER, f->id, NA, NA, NULL);
break;
case FROMOBHOLD:
f2 = hasflagval(ob->flags, F_HOLDCONFER, f->id, NA, NA, NULL);
break;
case FROMOBACTIVATE:
f2 = hasflagval(ob->flags, F_ACTIVATECONFER, f->id, NA, NA, NULL);
break;
}
if (f2) {
f2->known = B_TRUE;
}
}
}
2011-02-01 06:16:13 +11:00
}
if (isplayer(f->pile->owner)) {
switch (f->id) {
case F_BLIND:
case F_SEEINDARK:
case F_SPRINTING:
case F_TIRED:
case F_FASTMOVE:
case F_SLOWMOVE:
doredraw = B_TRUE;
2011-02-01 06:16:13 +11:00
break;
default:
break;
}
}
} else if (f->pile->ob) {
if (announceobflaggain(f->pile->ob, f)) {
f->known = B_TRUE;
}
}
}
if (gamestarted && doredraw) {
statdirty = B_TRUE;
needredraw = B_TRUE;
drawscreen();
}
2010-12-02 12:17:54 +11:00
return f;
}
2011-02-01 06:16:13 +11:00
flagpile_t *addflagpile(lifeform_t *owner, object_t *ob) {
2010-12-02 12:17:54 +11:00
flagpile_t *fp;
fp = malloc(sizeof(flagpile_t));
fp->first = NULL;
fp->last = NULL;
2011-02-01 06:16:13 +11:00
fp->owner = owner;
fp->ob = ob;
2010-12-02 12:17:54 +11:00
return fp;
}
2011-02-01 06:16:13 +11:00
void copyflags(flagpile_t *dst, flagpile_t *src, int lifetime) {
flag_t *f;
for (f = src->first ; f ; f = f->next) {
addflag_real(dst, f->id, f->val[0], f->val[1], f->val[2], f->text,
(lifetime == NA) ? f->lifetime : lifetime, f->known, -1);
2011-02-01 06:16:13 +11:00
}
}
int flagstacks(enum FLAG fid) {
int res = B_FALSE;
switch (fid) {
case F_EVASION:
res = B_TRUE;
break;
default:
res = B_FALSE;
break;
}
return res;
}
2010-12-02 12:17:54 +11:00
flag_t *hasflag(flagpile_t *fp, int id) {
2011-02-01 06:16:13 +11:00
return hasflag_real(fp, id, NA);
}
flag_t *hasflagknown(flagpile_t *fp, int id) {
return hasflag_real(fp, id, B_TRUE);
}
flag_t *hasflag_real(flagpile_t *fp, int id, int wantknown) {
2010-12-02 12:17:54 +11:00
flag_t *f;
2011-02-01 06:16:13 +11:00
lifeform_t *owner;
owner = fp->owner;
2010-12-02 12:17:54 +11:00
for (f = fp->first ; f ; f = f->next) {
2011-02-01 06:16:13 +11:00
if (f->id == id) {
int valid = B_TRUE;
if ((wantknown != NA) && (f->known != wantknown)) valid = B_FALSE;
if (owner && (f->lifetime == FROMJOB) && !getjob(owner)) {
valid = B_FALSE;
}
if (valid) {
return f;
}
}
2010-12-02 12:17:54 +11:00
}
return NULL;
}
2011-02-01 06:16:13 +11:00
2010-12-02 12:17:54 +11:00
flag_t *hasflagval(flagpile_t *fp, int id, int val1, int val2, int val3, char *text) {
2011-02-01 06:16:13 +11:00
return hasflagval_real(fp, id, val1, val2, val3, text, B_FALSE); // doesn't have to be known
}
flag_t *hasflagvalknown(flagpile_t *fp, int id, int val1, int val2, int val3, char *text) {
return hasflagval_real(fp, id, val1, val2, val3, text, B_TRUE); // must be known
}
flag_t *hasflagval_real(flagpile_t *fp, int id, int val1, int val2, int val3, char *text, int wantknown) {
2010-12-02 12:17:54 +11:00
flag_t *f;
2011-02-01 06:16:13 +11:00
lifeform_t *owner;
owner = fp->owner;
2010-12-02 12:17:54 +11:00
for (f = fp->first ; f ; f = f->next) {
if (f->id == id) {
2011-02-01 06:16:13 +11:00
if (owner && (f->lifetime == FROMJOB) && !getjob(owner)) {
// invalid
} else {
if ( ((val1 == NA) || (f->val[0] == val1)) &&
((val2 == NA) || (f->val[1] == val2)) &&
((val3 == NA) || (f->val[2] == val3)) &&
((text == NULL) || strstr(f->text, text))) {
if (!wantknown || f->known) {
return f;
}
}
2010-12-02 12:17:54 +11:00
}
}
}
return NULL;
}
2011-03-04 12:22:36 +11:00
// returns true if we did something
int killflagsofid(flagpile_t *fp, enum FLAG fid) {
2011-02-01 06:16:13 +11:00
flag_t *f,*nextf;
2011-03-04 12:22:36 +11:00
int donesomething = B_FALSE;
2011-02-01 06:16:13 +11:00
for (f = fp->first ; f ; f = nextf) {
nextf = f->next;
if (f->id == fid) {
killflag(f);
2011-03-04 12:22:36 +11:00
donesomething = B_TRUE;
2011-02-01 06:16:13 +11:00
}
}
2011-03-04 12:22:36 +11:00
return donesomething;
2011-02-01 06:16:13 +11:00
}
2010-12-02 12:17:54 +11:00
void killflag(flag_t *f) {
flag_t *nextone, *lastone;
2011-02-01 06:16:13 +11:00
lifeform_t *lf;
int doredraw = B_FALSE;
2011-02-01 06:16:13 +11:00
lf = f->pile->owner;
if (isplayer(lf)) {
switch (f->id) {
case F_BLIND:
case F_SEEINDARK:
case F_SPRINTING:
case F_TIRED:
case F_FASTMOVE:
case F_SLOWMOVE:
doredraw = B_TRUE;
2011-02-01 06:16:13 +11:00
break;
default:
break;
}
}
// notify
if (gamestarted) {
if (f->pile->owner) {
2011-03-04 12:22:36 +11:00
if (announceflagloss(f->pile->owner, f)) {
interrupt(f->pile->owner);
}
2011-02-01 06:16:13 +11:00
} else if (f->pile->ob) {
announceobflagloss(f->pile->ob, f);
}
}
// we will revert to our original form at the end of timeeffectslf().
if (lf && (f->id == F_POLYMORPHED)) {
lf->polyrevert = B_TRUE;
}
2010-12-02 12:17:54 +11:00
if (lf && (f->id == F_SPRINTING)) {
int howlong;
// you get tired when you finish sprinting
// TODO: change this based on
// constitution
// running skill
howlong = 5;
addtempflag(f->pile, F_TIRED, B_TRUE, NA, NA, NULL, howlong);
}
2010-12-02 12:17:54 +11:00
// free mem
// remove from list
nextone = f->next;
if (nextone != NULL) {
nextone->prev = f->prev;
} else { /* last */
f->pile->last = f->prev;
}
if (f->prev == NULL) {
/* first */
nextone = f->next;
f->pile->first = nextone;
free(f);
} else {
lastone = f->prev;
free (lastone->next );
lastone->next = nextone;
}
2011-02-01 06:16:13 +11:00
if (gamestarted && doredraw) {
statdirty = B_TRUE;
needredraw = B_TRUE;
2011-02-01 06:16:13 +11:00
drawscreen();
}
2010-12-02 12:17:54 +11:00
}
void killflagpile(flagpile_t *fp) {
while (fp->first) {
killflag(fp->first);
}
free(fp);
}
2011-02-01 06:16:13 +11:00
void timeeffectsflag(flag_t *f) {
if ((f->lifetime != PERMENANT) && (f->lifetime > 0)) {
f->lifetime--;
if (f->lifetime <= 0) {
killflag(f);
return;
} else if (f->lifetime == 5) {
// warn about certain flags......
if (isplayer(f->pile->owner)) {
switch (f->id) {
case F_CANWILL:
switch (f->val[0]) {
case OT_A_JUMP:
warn("Your ability to jump is starting to run out...");;
break;
default:
break;
}
break;
case F_DTIMMUNE:
warn("Your %s immunity is starting to run out...", getdamname(f->val[0]));
break;
case F_DTRESIST:
warn("Your %s resistance is starting to run out...", getdamname(f->val[0]));
break;
case F_DTVULN:
warn("You feel a little less vulnerable to %s...", getdamname(f->val[0]));
break;
case F_MAGSHIELD:
warn("Your magnetic shield is weakening...");
break;
case F_POLYMORPHED:
warn("You are starting to revert to your original form...");
break;
default: // no message
break;
}
}
} else if (f->lifetime == 2) {
if (isplayer(f->pile->owner)) {
switch (f->id) {
case F_NONCORPOREAL:
warn("You feel your body solidifying...");
break;
default:
break;
}
}
2011-02-01 06:16:13 +11:00
} else if (f->lifetime == 1) {
// warn about certain flags......
if (isplayer(f->pile->owner)) {
switch (f->id) {
case F_CANWILL:
switch (f->val[0]) {
case OT_A_JUMP:
warn("Your ability to jump is about to expire!");;
break;
default:
break;
}
break;
case F_DTIMMUNE:
warn("Your %s immunity is about to expire!", getdamname(f->val[0]));
break;
case F_DTRESIST:
warn("Your %s resistance is about to expire!", getdamname(f->val[0]));
break;
case F_DTVULN:
warn("You feel a little less vulnerable to %s...", getdamname(f->val[0]));
break;
case F_MAGSHIELD:
warn("Your magnetic shield is about to expire!");
break;
case F_POLYMORPHED:
warn("You are about to revert to your original form!");
break;
default: // no message
break;
}
}
}
}
}
void sumflags(flagpile_t *fp, int id, int *val0, int *val1, int *val2) {
flag_t *f;
if (val0) *val0 = 0;
if (val1) *val1 = 0;
if (val2) *val2 = 0;
for (f = fp->first ; f ; f = f->next) {
if (f->id == id) {
if (val0) *val0 = *val0 + f->val[0];
if (val1) *val1 = *val1 + f->val[1];
if (val2) *val2 = *val2 + f->val[2];
}
}
}
void timeeffectsflags(flagpile_t *fp) {
flag_t *f,*nextf;
for (f = fp->first ; f ; f = nextf) {
nextf = f->next;
timeeffectsflag(f);
}
}