2016-11-01 17:25:52 +01:00
|
|
|
/*
|
2015-08-18 18:57:04 +02:00
|
|
|
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
|
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
|
|
|
|
#include "travelthru.h"
|
|
|
|
#include "laws.h"
|
|
|
|
#include "report.h"
|
|
|
|
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
#include <kernel/ship.h>
|
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <util/attrib.h>
|
|
|
|
#include <util/bsdstring.h>
|
|
|
|
#include <util/log.h>
|
|
|
|
#include <util/language.h>
|
2015-08-19 14:37:51 +02:00
|
|
|
|
2015-08-18 18:57:04 +02:00
|
|
|
#include <stream.h>
|
2017-01-26 17:41:21 +01:00
|
|
|
#include <selist.h>
|
2015-08-18 18:57:04 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-13 22:09:41 +02:00
|
|
|
static void travel_done(attrib *a) {
|
2017-01-26 17:41:21 +01:00
|
|
|
selist *ql = (selist *)a->data.v;
|
|
|
|
selist_free(ql);
|
2015-10-13 22:09:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 18:57:04 +02:00
|
|
|
/*********************/
|
|
|
|
/* at_travelunit */
|
|
|
|
/*********************/
|
|
|
|
attrib_type at_travelunit = {
|
|
|
|
"travelunit",
|
|
|
|
DEFAULT_INIT,
|
2015-10-13 22:09:41 +02:00
|
|
|
travel_done,
|
2015-08-18 18:57:04 +02:00
|
|
|
DEFAULT_AGE,
|
|
|
|
NO_WRITE,
|
|
|
|
NO_READ
|
|
|
|
};
|
|
|
|
|
|
|
|
/** sets a marker in the region telling that the unit has travelled through it
|
|
|
|
* this is used for two distinctly different purposes:
|
|
|
|
* - to report that a unit has travelled through. the report function
|
|
|
|
* makes sure to only report the ships of travellers, not the travellers
|
|
|
|
* themselves
|
|
|
|
* - to report the region to the traveller
|
|
|
|
*/
|
2015-08-19 14:37:51 +02:00
|
|
|
void travelthru_add(region * r, unit * u)
|
2015-08-18 18:57:04 +02:00
|
|
|
{
|
2016-09-20 15:53:55 +02:00
|
|
|
region *next[MAXDIRECTIONS];
|
|
|
|
int d;
|
2015-08-19 14:37:51 +02:00
|
|
|
attrib *a;
|
2017-01-26 17:41:21 +01:00
|
|
|
selist *ql;
|
2015-08-18 18:57:04 +02:00
|
|
|
|
2015-08-19 14:37:51 +02:00
|
|
|
assert(r);
|
|
|
|
assert(u);
|
2015-08-18 18:57:04 +02:00
|
|
|
|
2015-08-19 14:37:51 +02:00
|
|
|
a = a_find(r->attribs, &at_travelunit);
|
|
|
|
if (!a) {
|
|
|
|
a = a_add(&r->attribs, a_new(&at_travelunit));
|
|
|
|
}
|
2017-01-26 17:41:21 +01:00
|
|
|
ql = (selist *)a->data.v;
|
2015-08-18 18:57:04 +02:00
|
|
|
|
2015-08-19 14:37:51 +02:00
|
|
|
fset(r, RF_TRAVELUNIT);
|
2017-01-26 17:41:21 +01:00
|
|
|
selist_push(&ql, u);
|
2015-08-19 14:37:51 +02:00
|
|
|
a->data.v = ql;
|
|
|
|
|
2015-08-18 18:57:04 +02:00
|
|
|
/* the first and last region of the faction gets reset, because travelthrough
|
|
|
|
* could be in regions that are located before the [first, last] interval,
|
|
|
|
* and recalculation is needed */
|
|
|
|
update_interval(u->faction, r);
|
2016-09-20 15:53:55 +02:00
|
|
|
get_neighbours(r, next);
|
|
|
|
for (d=0;d!=MAXDIRECTIONS;++d) {
|
|
|
|
update_interval(u->faction, next[d]);
|
|
|
|
}
|
2015-08-18 18:57:04 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 14:37:51 +02:00
|
|
|
bool travelthru_cansee(const struct region *r, const struct faction *f, const struct unit *u) {
|
|
|
|
if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) {
|
|
|
|
return cansee_durchgezogen(f, r, u, 0);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-26 10:12:12 +01:00
|
|
|
struct cb_data {
|
|
|
|
void(*call)(region *, struct unit *, void *);
|
|
|
|
void *data;
|
|
|
|
struct region *r;
|
|
|
|
};
|
|
|
|
|
2017-01-26 18:58:29 +01:00
|
|
|
static bool cb_map(void *data, void *ex) {
|
2017-01-26 10:12:12 +01:00
|
|
|
struct cb_data *cb = (struct cb_data *)ex;
|
|
|
|
struct unit *u = (struct unit *)data;
|
|
|
|
cb->call(cb->r, u, cb->data);
|
2017-01-26 18:58:29 +01:00
|
|
|
return true;
|
2017-01-26 10:12:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void travelthru_map(region * r, void(*cb)(region *, struct unit *, void *), void *data)
|
2015-08-19 14:37:51 +02:00
|
|
|
{
|
|
|
|
attrib *a;
|
2017-01-26 10:12:12 +01:00
|
|
|
struct cb_data cbdata;
|
|
|
|
|
2015-08-19 14:37:51 +02:00
|
|
|
assert(r);
|
2017-01-26 10:12:12 +01:00
|
|
|
cbdata.call = cb;
|
|
|
|
cbdata.data = data;
|
|
|
|
cbdata.r = r;
|
2015-08-19 14:37:51 +02:00
|
|
|
a = a_find(r->attribs, &at_travelunit);
|
|
|
|
if (a) {
|
2017-01-26 17:41:21 +01:00
|
|
|
selist *ql = (selist *)a->data.v;
|
2017-01-26 10:12:12 +01:00
|
|
|
selist_foreach_ex(ql, cb_map, &cbdata);
|
2015-08-19 14:37:51 +02:00
|
|
|
}
|
|
|
|
}
|