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>
|
2015-08-19 14:37:51 +02:00
|
|
|
|
#include <quicklist.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) {
|
|
|
|
|
quicklist *ql = (quicklist *)a->data.v;
|
|
|
|
|
ql_free(ql);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
quicklist *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));
|
|
|
|
|
}
|
|
|
|
|
ql = (quicklist *)a->data.v;
|
2015-08-18 18:57:04 +02:00
|
|
|
|
|
2015-08-19 14:37:51 +02:00
|
|
|
|
fset(r, RF_TRAVELUNIT);
|
|
|
|
|
ql_push(&ql, u);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void travelthru_map(region * r, void(*cb)(region *, struct unit *, void *), void *cbdata)
|
|
|
|
|
{
|
|
|
|
|
attrib *a;
|
|
|
|
|
assert(r);
|
|
|
|
|
a = a_find(r->attribs, &at_travelunit);
|
|
|
|
|
if (a) {
|
|
|
|
|
quicklist *ql;
|
|
|
|
|
ql_iter qi;
|
|
|
|
|
ql = (quicklist *)a->data.v;
|
|
|
|
|
for (qi = qli_init(&ql); qli_more(qi);) {
|
|
|
|
|
unit *u = (unit *)qli_next(&qi);
|
|
|
|
|
cb(r, u, cbdata);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|