server/src/attributes/otherfaction.c

69 lines
1.5 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
#include <platform.h>
#include <kernel/config.h>
#include "otherfaction.h"
#include <kernel/ally.h>
2010-08-08 10:06:34 +02:00
#include <kernel/faction.h>
#include <kernel/unit.h>
#include <kernel/attrib.h>
2018-09-29 13:21:46 +02:00
#include <kernel/gamedata.h>
#include <storage.h>
2010-08-08 10:06:34 +02:00
#include <assert.h>
/*
* simple attributes that do not yet have their own file
2010-08-08 10:06:34 +02:00
*/
void write_of(const variant *var, const void *owner, struct storage *store)
2010-08-08 10:06:34 +02:00
{
const faction *f = (faction *)var->v;
WRITE_INT(store, f->no);
2010-08-08 10:06:34 +02:00
}
int read_of(variant *var, void *owner, gamedata *data)
2011-03-07 08:02:35 +01:00
{ /* return 1 on success, 0 if attrib needs removal */
int of;
READ_INT(data->store, &of);
if (rule_stealth_other()) {
var->v = findfaction(of);
if (var->v) {
return AT_READ_OK;
}
}
return AT_READ_FAIL;
2010-08-08 10:06:34 +02:00
}
attrib_type at_otherfaction = {
"otherfaction", NULL, NULL, NULL, write_of, read_of, NULL, ATF_UNIQUE
2010-08-08 10:06:34 +02:00
};
faction *get_otherfaction(const unit * u)
2010-08-08 10:06:34 +02:00
{
attrib *a = a_find(u->attribs, &at_otherfaction);
if (a) {
faction * f = (faction *)(a->data.v);
if (f && f->_alive) {
return f;
}
}
return NULL;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
struct attrib *make_otherfaction(struct faction *f)
2010-08-08 10:06:34 +02:00
{
attrib *a = a_new(&at_otherfaction);
a->data.v = (void *)f;
return a;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
faction *visible_faction(const faction * f, const unit * u)
2010-08-08 10:06:34 +02:00
{
if (f == NULL || !alliedunit(u, f, HELP_FSTEALTH)) {
faction *fv = get_otherfaction(u);
if (fv) return fv;
2010-08-08 10:06:34 +02:00
}
return u->faction;
2010-08-08 10:06:34 +02:00
}