Skip to content

Commit

Permalink
Rewrite of roads (#1105)
Browse files Browse the repository at this point in the history
* replace bt_road connections with land_region.roads (WIP)

* convert bt_road to land_region.roads
roads are now okay in CR, but not in NR

* ungenutze Funktionen weg

* recreated the complex NR display of roads.

* unused variable, short road values
  • Loading branch information
ennorehling authored Jan 3, 2025
1 parent 045ccaf commit 2617d95
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 147 deletions.
37 changes: 28 additions & 9 deletions src/creport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,11 +1124,34 @@ static char *cr_output_resource(char *buf, const resource_type *rtype,
return buf;
}

static void
cr_borders(stream *out, const region * r, const faction * f)
static int
cr_roads(stream *out, const region *r, const faction *f, int offset)
{
int g = offset;
static const char *road_type = NULL;
if (!road_type) {
road_type = LOC(f->locale, mkname("border", "road"));
}
if (r->land && r->terrain->max_road) {
direction_t d;
for (d = 0; d != MAXDIRECTIONS; d++) { /* Nachbarregionen, die gesehen werden, ermitteln */
if (r->land->roads[d]) {
int p = rroad(r, d) * 100 / r->terrain->max_road;
creport_block_1(out, "GRENZE", ++g);
creport_tag(out, "typ", road_type);
creport_tag_int(out, "richtung", d);
creport_tag_int(out, "prozent", p);
}
}
}
return g;
}

static int
cr_borders(stream *out, const region * r, const faction * f, int offset)
{
direction_t d;
int g = 0;
int g = offset;
for (d = 0; d != MAXDIRECTIONS; d++) { /* Nachbarregionen, die gesehen werden, ermitteln */
const region *r2 = rconnect(r, d);
const connection *b;
Expand Down Expand Up @@ -1158,15 +1181,11 @@ cr_borders(stream *out, const region * r, const faction * f)
creport_tag_int(out, "richtung", d);
if (!b->type->transparent(b, f))
sputs("1;opaque", out);
/* hack: */
if (b->type == &bt_road && r->terrain->max_road) {
int p = rroad(r, d) * 100 / r->terrain->max_road;
creport_tag_int(out, "prozent", p);
}
}
b = b->next;
}
}
return g;
}

void cr_output_resources(struct stream *out, const struct faction * f, const struct region *r, enum seen_mode mode)
Expand Down Expand Up @@ -1412,7 +1431,7 @@ void cr_output_region(struct stream* out, const struct faction* f,
}
}
cr_output_curses(out, f, r, TYP_REGION);
cr_borders(out, r, f);
cr_borders(out, r, f, cr_roads(out, r, f, 0));
}
if (see_schemes(r, mode)) {
/* Sonderbehandlung Teleport-Ebene */
Expand Down
6 changes: 2 additions & 4 deletions src/economy.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <kernel/building.h>
#include <kernel/calendar.h>
#include <kernel/config.h>
#include "kernel/connection.h"
#include <kernel/curse.h>
#include <kernel/faction.h>
#include "kernel/direction.h" // for D_EAST, directions
Expand Down Expand Up @@ -1700,7 +1699,7 @@ static void test_destroy_cmd(CuTest* tc) {
}

static void test_make_road(CuTest *tc) {
region *r, *r2;
region *r;
unit *u;
faction *f;
struct item_type *itype;
Expand All @@ -1710,7 +1709,7 @@ static void test_make_road(CuTest *tc) {
t_plain = test_create_terrain("plain", LAND_REGION);
t_plain->max_road = 100;
u = test_create_unit(f = test_create_faction(), r = test_create_region(0, 0, t_plain));
r2 = test_create_region(1, 0, t_plain);
test_create_region(1, 0, t_plain);
set_level(u, SK_ROAD_BUILDING, 10);
scale_number(u, 10);
i_change(&u->items, itype = test_create_itemtype("stone"), 100);
Expand All @@ -1719,7 +1718,6 @@ static void test_make_road(CuTest *tc) {
make_cmd(u, u->thisorder);
CuAssertIntEquals(tc, 50, i_get(u->items, itype));
CuAssertIntEquals(tc, 50, rroad(r, D_EAST));
CuAssertPtrNotNull(tc, get_borders(r, r2));
test_teardown();
}

Expand Down
89 changes: 10 additions & 79 deletions src/kernel/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,51 +413,6 @@ border_type bt_fogwall = {
b_uvisible, /* uvisible */
};

/***
* roads. meant to replace the old at_road or r->road attribute
***/

static const char *b_nameroad(const connection * b, const region * r,
const struct faction *f, int gflags)
{
region *r2 = (r == b->to) ? b->from : b->to;
int local = (r == b->from) ? b->data.sa[0] : b->data.sa[1];
static char buffer[64];

UNUSED_ARG(f);
if (gflags & GF_PURE)
return "road";
if (gflags & GF_ARTICLE) {
if (!(gflags & GF_DETAILED))
return LOC(f->locale, mkname("border", "a_road"));
else if (r->terrain->max_road <= local) {
int remote = (r2 == b->from) ? b->data.sa[0] : b->data.sa[1];
if (r2->terrain->max_road <= remote) {
return LOC(f->locale, mkname("border", "a_road"));
}
else {
return LOC(f->locale, mkname("border", "an_incomplete_road"));
}
}
else {
if (local) {
const char *temp = LOC(f->locale, mkname("border", "a_road_percent"));
int percent = 100 * local / r->terrain->max_road;
if (percent < 1) percent = 1;
str_replace(buffer, sizeof(buffer), temp, "$percent", itoa10(percent));
}
else {
return LOC(f->locale, mkname("border", "a_road_connection"));
}
}
}
else if (gflags & GF_PLURAL)
return LOC(f->locale, mkname("border", "roads"));
else
return LOC(f->locale, mkname("border", "road"));
return buffer;
}

static void b_readroad(connection * b, gamedata * data)
{
storage * store = data->store;
Expand All @@ -472,42 +427,12 @@ static void b_readroad(connection * b, gamedata * data)
rsetroad(b->to, dir, n);
}

static void b_writeroad(const connection * b, storage * store)
{
WRITE_INT(store, b->data.sa[0]);
WRITE_INT(store, b->data.sa[1]);
}

static bool b_validroad(const connection * b)
{
return (b->data.sa[0] != SHRT_MAX);
}

static bool b_rvisibleroad(const connection * b, const region * r)
{
int x = (r == b->from) ? b->data.sa[0] : b->data.sa[1];
if (x == 0) {
return false;
}
if (b->to != r && b->from != r) {
return false;
}
return true;
}

border_type bt_road = {
"road", VAR_INT, LAND_REGION,
b_transparent,
"road", VAR_NONE, LAND_REGION,
NULL,
NULL, /* init */
NULL, /* destroy */
b_readroad, /* read */
b_writeroad, /* write */
b_blocknone, /* block */
b_nameroad, /* name */
b_rvisibleroad, /* rvisible */
b_finvisible, /* fvisible */
b_uinvisible, /* uvisible */
b_validroad /* valid */
};

void write_borders(struct storage *store)
Expand Down Expand Up @@ -587,10 +512,16 @@ int read_borders(gamedata *data)
}
}
if (b == NULL) {
b = create_border(type, from, to);
if (type == &bt_road) {
/* delete old bt_road instances */
b = &dummy;
}
else {
b = create_border(type, from, to);
}
}
type->read(b, data);
if (!type->write) {
if (type->datatype != VAR_NONE && !type->write) {
log_warning("invalid border '%s' between '%s' and '%s'\n", zText, regionname(from, 0), regionname(to, 0));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/kernel/direction.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ direction_t finddirection(const char *str) {
return NODIRECTION;
}

direction_t d_reverse(direction_t d)
{
return (d + MAXDIRECTIONS / 2) % MAXDIRECTIONS;
}

const char * directions[MAXDIRECTIONS + 2] = {
"northwest", "northeast", "east", "southeast", "southwest", "west", 0, "pause"
};
Expand Down
1 change: 1 addition & 0 deletions src/kernel/direction.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C"
void init_direction(const struct locale *lang, direction_t dir, const char *str);

direction_t finddirection(const char *str);
direction_t d_reverse(direction_t d);

extern const char * directions[];

Expand Down
12 changes: 12 additions & 0 deletions src/kernel/direction.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,24 @@ static void test_finddirection(CuTest *tc) {
test_teardown();
}

static void test_direction_reverse(CuTest *tc) {
test_setup();
CuAssertIntEquals(tc, D_SOUTHWEST, d_reverse(D_NORTHEAST));
CuAssertIntEquals(tc, D_SOUTHEAST, d_reverse(D_NORTHWEST));
CuAssertIntEquals(tc, D_NORTHEAST, d_reverse(D_SOUTHWEST));
CuAssertIntEquals(tc, D_NORTHWEST, d_reverse(D_SOUTHEAST));
CuAssertIntEquals(tc, D_EAST, d_reverse(D_WEST));
CuAssertIntEquals(tc, D_WEST, d_reverse(D_EAST));
test_teardown();
}

CuSuite *get_direction_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_init_direction);
SUITE_ADD_TEST(suite, test_init_directions);
SUITE_ADD_TEST(suite, test_finddirection);
SUITE_ADD_TEST(suite, test_direction_reverse);
return suite;
}

43 changes: 5 additions & 38 deletions src/kernel/region.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,48 +529,15 @@ attrib_type at_woodcount = {
ATF_UNIQUE
};

void rsetroad(region * r, direction_t d, int val)
void rsetroad(region * r, direction_t d, short val)
{
connection *b;
region *r2 = rconnect(r, d);

assert(val>=SHRT_MIN && val<=SHRT_MAX);
if (!r2) {
return;
}
b = get_borders(r, r2);
while (b && b->type != &bt_road) {
b = b->next;
}
if (!b) {
if (!val) return;
b = create_border(&bt_road, r, r2);
}
if (r == b->from) {
b->data.sa[0] = (short)val;
}
else {
b->data.sa[1] = (short)val;
}
if (!r->land) return;
r->land->roads[d] = val;
}

int rroad(const region * r, direction_t d)
short rroad(const region * r, direction_t d)
{
connection *b;
region *r2 = rconnect(r, d);

if (!r2) {
return 0;
}
b = get_borders(r, r2);
while (b && b->type != &bt_road) {
b = b->next;
}
if (!b) {
return 0;
}

return (r == b->from) ? b->data.sa[0] : b->data.sa[1];
return r->land ? r->land->roads[d] : 0;
}

void r_foreach_demand(const struct region *r, void (*callback)(struct demand *, int, void *), void *data)
Expand Down
5 changes: 3 additions & 2 deletions src/kernel/region.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef struct land_region {
int trees[3]; /* 0 -> seeds, 1 -> shoots, 2 -> trees */
int money;
struct region_owner *ownership;
short roads[MAXDIRECTIONS];
} land_region;

/* seen_mode: visibility in the report */
Expand Down Expand Up @@ -172,8 +173,8 @@ void deathcounts(struct region *r, int delta);
void setluxuries(struct region *r, const struct luxury_type *sale);
int get_maxluxuries(void);

int rroad(const struct region *r, direction_t d);
void rsetroad(struct region *r, direction_t d, int value);
short rroad(const struct region *r, direction_t d);
void rsetroad(struct region *r, direction_t d, short value);

bool is_coastregion(struct region *r);

Expand Down
4 changes: 2 additions & 2 deletions src/kernel/region.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ static void test_borders(CuTest *tc) {
r = test_create_plain(0, 0);
r2 = test_create_plain(1, 0);
CuAssertPtrEquals(tc, NULL, get_borders(r, r2));
c = create_border(&bt_road, r, r2);
c = create_border(&bt_wall, r, r2);
CuAssertPtrEquals(tc, c, get_borders(r, r2));
CuAssertPtrEquals(tc, c, get_borders(r2, r));

create_border(&bt_road, r, test_create_plain(-1, 0));
create_border(&bt_wall, r, test_create_plain(-1, 0));
walk_connections(r, cb_connection, &n);
CuAssertIntEquals(tc, 2, n);
test_teardown();
Expand Down
Loading

0 comments on commit 2617d95

Please sign in to comment.