2014-12-17 17:22:26 +01:00
|
|
|
#include <platform.h>
|
|
|
|
#include "lighthouse.h"
|
|
|
|
|
|
|
|
#include <kernel/config.h>
|
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/plane.h>
|
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/terrain.h>
|
|
|
|
#include <kernel/unit.h>
|
2016-09-11 11:02:04 +02:00
|
|
|
#include <util/log.h>
|
2018-09-29 11:37:17 +02:00
|
|
|
#include <kernel/attrib.h>
|
2014-12-17 17:22:26 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2016-12-16 17:16:10 +01:00
|
|
|
attrib_type at_lighthouse = {
|
2014-12-17 17:22:26 +01:00
|
|
|
"lighthouse"
|
2019-02-08 11:37:32 +01:00
|
|
|
/* Rest ist NULL; temporaeres, nicht alterndes Attribut */
|
2014-12-17 17:22:26 +01:00
|
|
|
};
|
|
|
|
|
2018-07-17 20:53:34 +02:00
|
|
|
bool is_lighthouse(const building_type *btype)
|
|
|
|
{
|
|
|
|
return is_building_type(btype, "lighthouse");
|
|
|
|
}
|
|
|
|
|
2014-12-17 17:22:26 +01:00
|
|
|
/* update_lighthouse: call this function whenever the size of a lighthouse changes
|
2018-07-17 21:34:22 +02:00
|
|
|
* it adds temporary markers to the surrounding regions.
|
|
|
|
* The existence of markers says nothing about the quality of the observer in
|
|
|
|
* the lighthouse, since this may change more frequently.
|
|
|
|
*/
|
2014-12-17 17:22:26 +01:00
|
|
|
void update_lighthouse(building * lh)
|
|
|
|
{
|
2018-07-17 20:53:34 +02:00
|
|
|
region *r = lh->region;
|
|
|
|
assert(is_lighthouse(lh->type));
|
2014-12-17 17:22:26 +01:00
|
|
|
|
2018-07-17 20:53:34 +02:00
|
|
|
r->flags |= RF_LIGHTHOUSE;
|
|
|
|
if (lh->size >= 10) {
|
2018-07-18 20:49:17 +02:00
|
|
|
int d = lighthouse_range(lh);
|
2018-07-17 20:53:34 +02:00
|
|
|
int x;
|
|
|
|
for (x = -d; x <= d; ++x) {
|
|
|
|
int y;
|
|
|
|
for (y = -d; y <= d; ++y) {
|
|
|
|
attrib *a;
|
|
|
|
region *r2;
|
|
|
|
int px = r->x + x, py = r->y + y;
|
2014-12-17 17:22:26 +01:00
|
|
|
|
2018-07-17 20:53:34 +02:00
|
|
|
pnormalize(&px, &py, rplane(r));
|
|
|
|
r2 = findregion(px, py);
|
|
|
|
if (!r2 || !fval(r2->terrain, SEA_REGION))
|
|
|
|
continue;
|
|
|
|
if (distance(r, r2) > d)
|
|
|
|
continue;
|
|
|
|
a = a_find(r2->attribs, &at_lighthouse);
|
|
|
|
while (a && a->type == &at_lighthouse) {
|
|
|
|
building *b = (building *)a->data.v;
|
|
|
|
if (b == lh)
|
|
|
|
break;
|
|
|
|
a = a->next;
|
|
|
|
}
|
|
|
|
if (!a) {
|
|
|
|
a = a_add(&r2->attribs, a_new(&at_lighthouse));
|
|
|
|
a->data.v = (void *)lh;
|
2014-12-17 17:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:53:34 +02:00
|
|
|
void remove_lighthouse(const building *lh) {
|
|
|
|
building *b;
|
|
|
|
region * r = lh->region;
|
|
|
|
|
|
|
|
r->flags &= ~RF_LIGHTHOUSE;
|
|
|
|
for (b = r->buildings; b; b = b->next) {
|
|
|
|
if (b != lh && is_lighthouse(b->type)) {
|
|
|
|
update_lighthouse(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:49:17 +02:00
|
|
|
int lighthouse_range(const building * b)
|
2014-12-17 17:22:26 +01:00
|
|
|
{
|
2018-07-17 20:53:34 +02:00
|
|
|
if (b->size >= 10 && (b->flags & BLD_MAINTAINED)) {
|
2018-07-18 20:49:17 +02:00
|
|
|
return (int)log10(b->size) + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lighthouse_view_distance(const building * b, const unit *u)
|
|
|
|
{
|
|
|
|
if (b->size >= 10 && (b->flags & BLD_MAINTAINED)) {
|
|
|
|
int maxd = lighthouse_range(b);
|
2014-12-17 17:22:26 +01:00
|
|
|
|
2018-07-18 20:49:17 +02:00
|
|
|
if (maxd > 0 && u && skill_enabled(SK_PERCEPTION)) {
|
2019-02-02 20:36:23 +01:00
|
|
|
int sk = effskill(u, SK_PERCEPTION, NULL) / 3;
|
2017-05-21 21:33:29 +02:00
|
|
|
assert(u->building == b);
|
2018-01-01 08:23:52 +01:00
|
|
|
if (maxd > sk) maxd = sk;
|
2014-12-17 17:22:26 +01:00
|
|
|
}
|
2017-05-21 21:33:29 +02:00
|
|
|
return maxd;
|
2014-12-17 17:22:26 +01:00
|
|
|
}
|
2017-05-21 21:33:29 +02:00
|
|
|
return 0;
|
2014-12-17 17:22:26 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 20:53:34 +02:00
|
|
|
bool lighthouse_guarded(const region * r)
|
2014-12-17 17:22:26 +01:00
|
|
|
{
|
|
|
|
attrib *a;
|
|
|
|
|
2018-07-17 20:53:34 +02:00
|
|
|
if (!r->attribs || !(r->terrain->flags & SEA_REGION)) {
|
2014-12-17 17:22:26 +01:00
|
|
|
return false;
|
2015-01-08 20:55:29 +01:00
|
|
|
}
|
2014-12-17 17:22:26 +01:00
|
|
|
for (a = a_find(r->attribs, &at_lighthouse); a && a->type == &at_lighthouse;
|
|
|
|
a = a->next) {
|
|
|
|
building *b = (building *)a->data.v;
|
|
|
|
|
2016-08-30 10:19:19 +02:00
|
|
|
assert(is_building_type(b->type, "lighthouse"));
|
2018-07-17 20:53:34 +02:00
|
|
|
if ((b->flags & BLD_MAINTAINED) && b->size >= 10) {
|
2014-12-17 17:22:26 +01:00
|
|
|
int maxd = (int)log10(b->size) + 1;
|
2018-07-17 20:53:34 +02:00
|
|
|
int d = distance(r, b->region);
|
|
|
|
assert(maxd >= d);
|
|
|
|
return true;
|
2014-12-17 17:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|