move travelthru logic to a separate module. todo: split off the report writing from the collection of units.

This commit is contained in:
Enno Rehling 2015-08-18 18:57:04 +02:00
parent 1f4c8d5858
commit ebe365fd6e
12 changed files with 197 additions and 133 deletions

View File

@ -108,6 +108,7 @@ set (ERESSEA_SRC
spy.c
study.c
summary.c
travelthru.c
monsters.c
wormhole.c
${SPELLS_SRC}

View File

@ -11,6 +11,7 @@ without prior permission by the authors of Eressea.
#include <kernel/config.h>
#include "buildno.h"
#include "creport.h"
#include "travelthru.h"
/* tweakable features */
#define RENDER_CRMESSAGES

View File

@ -525,18 +525,6 @@ attrib_type at_woodcount = {
ATF_UNIQUE
};
/*********************/
/* at_travelunit */
/*********************/
attrib_type at_travelunit = {
"travelunit",
DEFAULT_INIT,
DEFAULT_FINALIZE,
DEFAULT_AGE,
NO_WRITE,
NO_READ
};
void rsetroad(region * r, direction_t d, int val)
{
connection *b;

View File

@ -169,7 +169,6 @@ extern "C" {
extern struct attrib_type at_horseluck;
extern struct attrib_type at_woodcount;
extern struct attrib_type at_deathcount;
extern struct attrib_type at_travelunit;
void initrhash(void);
void rhash(struct region *r);

View File

@ -23,6 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "laws.h"
#include "reports.h"
#include "alchemy.h"
#include "travelthru.h"
#include "vortex.h"
#include "monster.h"
#include "lighthouse.h"
@ -501,29 +502,6 @@ static ship *do_maelstrom(region * r, unit * u)
return u->ship;
}
/** 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
*/
void travelthru(const unit * u, region * r)
{
attrib *ru = a_add(&r->attribs, a_new(&at_travelunit));
fset(r, RF_TRAVELUNIT);
ru->data.v = (void *)u;
/* 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 */
#ifdef SMART_INTERVALS
update_interval(u->faction, r);
#endif
}
static direction_t
koor_reldirection(int ax, int ay, int bx, int by, const struct plane *pl)
{

View File

@ -64,7 +64,6 @@ extern "C" {
int enoughsailors(const struct ship *sh, int sumskill);
bool canswim(struct unit *u);
bool canfly(struct unit *u);
void travelthru(const struct unit *u, struct region *r);
struct ship *move_ship(struct ship *sh, struct region *from,
struct region *to, struct region_list *route);
int walkingcapacity(const struct unit *u);

View File

@ -23,6 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "reports.h"
#include "laws.h"
#include "travelthru.h"
#include "monster.h"
/* modules includes */
@ -163,7 +164,7 @@ static void centre(stream *out, const char *s, bool breaking)
}
}
static void
void
paragraph(stream *out, const char *str, ptrdiff_t indent, int hanging_indent,
char marker)
{
@ -1345,100 +1346,6 @@ static void statistics(stream *out, const region * r, const faction * f)
}
static int count_travelthru(const region *r, const faction *f, attrib *alist) {
int maxtravel = 0;
attrib *a;
for (a = alist; a && a->type == &at_travelunit; a = a->next) {
unit *u = (unit *)a->data.v;
if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) {
if (cansee_durchgezogen(f, r, u, 0)) {
++maxtravel;
}
}
}
return maxtravel;
}
void write_travelthru(stream *out, const region * r, const faction * f)
{
attrib *abegin, *a;
int counter = 0, maxtravel = 0;
char buf[8192];
char *bufp = buf;
int bytes;
size_t size = sizeof(buf) - 1;
assert(r);
assert(f);
if (!fval(r, RF_TRAVELUNIT)) {
return;
}
CHECK_ERRNO();
abegin = a_find(r->attribs, &at_travelunit);
/* How many are we listing? For grammar. */
maxtravel = count_travelthru(r, f, abegin);
if (maxtravel == 0) {
return;
}
/* Auflisten. */
newline(out);
for (a = abegin; a && a->type == &at_travelunit; a = a->next) {
unit *u = (unit *)a->data.v;
if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) {
if (cansee_durchgezogen(f, r, u, 0)) {
++counter;
if (u->ship != NULL) {
bytes = (int)strlcpy(bufp, shipname(u->ship), size);
}
else {
bytes = (int)strlcpy(bufp, unitname(u), size);
}
CHECK_ERRNO();
if (wrptr(&bufp, &size, bytes) != 0) {
INFO_STATIC_BUFFER();
break;
}
if (counter + 1 < maxtravel) {
bytes = (int)strlcpy(bufp, ", ", size);
CHECK_ERRNO();
if (wrptr(&bufp, &size, bytes) != 0) {
INFO_STATIC_BUFFER();
break;
}
}
else if (counter + 1 == maxtravel) {
bytes = (int)strlcpy(bufp, LOC(f->locale, "list_and"), size);
CHECK_ERRNO();
if (wrptr(&bufp, &size, bytes) != 0) {
INFO_STATIC_BUFFER();
break;
}
}
}
}
}
if (size > 0) {
CHECK_ERRNO();
if (maxtravel == 1) {
bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_one"));
}
else {
bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_many"));
}
CHECK_ERRNO();
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER_EX("write_travelthru");
CHECK_ERRNO();
}
*bufp = 0;
paragraph(out, buf, 0, 0, 0);
}
static int buildingmaintenance(const building * b, const resource_type * rtype)
{
const building_type *bt = b->type;
@ -2340,20 +2247,25 @@ const char *charset)
}
}
guards(out, r, f);
newline(out);
write_travelthru(out, r, f);
}
else {
if (sr->mode == see_far) {
describe(out, sr, f);
newline(out);
guards(out, r, f);
newline(out);
write_travelthru(out, r, f);
}
else if (sr->mode == see_lighthouse) {
describe(out, sr, f);
newline(out);
write_travelthru(out, r, f);
}
else {
describe(out, sr, f);
newline(out);
write_travelthru(out, r, f);
}
}

View File

@ -1,3 +1,4 @@
#pragma once
/*
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea.de>
@ -11,6 +12,9 @@
*/
#ifndef H_GC_REPORT
#define H_GC_REPORT
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -21,7 +25,8 @@ extern "C" {
void register_nr(void);
void report_cleanup(void);
void write_spaces(struct stream *out, size_t num);
void write_travelthru(struct stream *out, const struct region * r, const struct faction * f);
void paragraph(struct stream *out, const char *str, ptrdiff_t indent, int hanging_indent, char marker);
#ifdef __cplusplus
}
#endif

View File

@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/config.h>
#include "reports.h"
#include "laws.h"
#include "travelthru.h"
#include "lighthouse.h"
/* kernel includes */

View File

@ -4,6 +4,7 @@
#include "report.h"
#include "creport.h"
#include "move.h"
#include "travelthru.h"
#include <kernel/building.h>
#include <kernel/faction.h>

159
src/travelthru.c Normal file
View File

@ -0,0 +1,159 @@
/*
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>
#include <stream.h>
#include <assert.h>
#include <string.h>
/*********************/
/* at_travelunit */
/*********************/
attrib_type at_travelunit = {
"travelunit",
DEFAULT_INIT,
DEFAULT_FINALIZE,
DEFAULT_AGE,
NO_WRITE,
NO_READ
};
static int count_travelthru(const struct region *r, const struct faction *f, attrib *alist) {
int maxtravel = 0;
attrib *a;
for (a = alist; a && a->type == &at_travelunit; a = a->next) {
unit *u = (unit *)a->data.v;
if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) {
if (cansee_durchgezogen(f, r, u, 0)) {
++maxtravel;
}
}
}
return maxtravel;
}
void write_travelthru(stream *out, const region * r, const faction * f)
{
attrib *abegin, *a;
int counter = 0, maxtravel = 0;
char buf[8192];
char *bufp = buf;
int bytes;
size_t size = sizeof(buf) - 1;
assert(r);
assert(f);
if (!fval(r, RF_TRAVELUNIT)) {
return;
}
abegin = a_find(r->attribs, &at_travelunit);
/* How many are we listing? For grammar. */
maxtravel = count_travelthru(r, f, abegin);
if (maxtravel == 0) {
return;
}
/* Auflisten. */
for (a = abegin; a && a->type == &at_travelunit; a = a->next) {
unit *u = (unit *)a->data.v;
if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) {
if (cansee_durchgezogen(f, r, u, 0)) {
++counter;
if (u->ship != NULL) {
bytes = (int)strlcpy(bufp, shipname(u->ship), size);
}
else {
bytes = (int)strlcpy(bufp, unitname(u), size);
}
if (wrptr(&bufp, &size, bytes) != 0) {
INFO_STATIC_BUFFER();
break;
}
if (counter + 1 < maxtravel) {
bytes = (int)strlcpy(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) {
INFO_STATIC_BUFFER();
break;
}
}
else if (counter + 1 == maxtravel) {
bytes = (int)strlcpy(bufp, LOC(f->locale, "list_and"), size);
if (wrptr(&bufp, &size, bytes) != 0) {
INFO_STATIC_BUFFER();
break;
}
}
}
}
}
if (size > 0) {
if (maxtravel == 1) {
bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_one"));
}
else {
bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_many"));
}
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER_EX("write_travelthru");
}
*bufp = 0;
paragraph(out, buf, 0, 0, 0);
}
/** 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
*/
void travelthru(const unit * u, region * r)
{
attrib *ru = a_add(&r->attribs, a_new(&at_travelunit));
fset(r, RF_TRAVELUNIT);
ru->data.v = (void *)u;
/* 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 */
#ifdef SMART_INTERVALS
update_interval(u->faction, r);
#endif
}

20
src/travelthru.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#ifndef H_TRAVELTHRU
#define H_TRAVELTHRU
#ifdef __cplusplus
extern "C" {
#endif
extern struct attrib_type at_travelunit;
struct stream;
struct region;
struct faction;
void write_travelthru(struct stream *out, const struct region * r, const struct faction * f);
void travelthru(const struct unit * u, struct region * r);
#ifdef __cplusplus
}
#endif
#endif