Skip to content

Commit

Permalink
Merge pull request #1094 from eressea/roads
Browse files Browse the repository at this point in the history
testing roads
  • Loading branch information
ennorehling authored Oct 25, 2024
2 parents 1f5a78a + 045753c commit db3062b
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 36 deletions.
11 changes: 11 additions & 0 deletions scripts/roadtest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'config'
eressea.read_game(get_turn() .. '.dat')
r = get_region(-55, -22)
print(r)
roads = r.roads
for d = 1, 6 do
print(roads[d])
end
for b in r.buildings do
print(b)
end
41 changes: 41 additions & 0 deletions scripts/tests/e2/buildings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,44 @@ function test_build_harbour()
assert_equal(25, u.building.size)
end

function test_caravan()
local r = region.create(0,0, "desert")
local r2 = region.create(1,0, "plain")
local f = faction.create("human")
local u = unit.create(f, r, 10)
u:set_skill("building", 3)
u:set_skill("roadwork", 10)
u:add_item("money", 8000)
u:add_item("horse", 2)
u:add_item("stone", 210)
u:add_item("log", 50)
u:add_item("iron", 10)
u:set_orders("MACHE 100 STRASSE OST")
process_orders()
assert_equal(0, r.roads[3]) -- keine Arme, keine Kekse
u:set_orders("MACHE 1 Karawanserei\nBEZAHLE NICHT")
process_orders()
assert_not_nil(u.building)
assert_equal(1, u.building.size)
u:set_orders("MACHE 100 STRASSE OST\nBEZAHLE NICHT")
process_orders()
assert_equal(0, r.roads[3]) -- Karawane ist noch nicht fertig
u:set_orders("MACHE 9 Karawanserei\nBEZAHLE NICHT")
process_orders()
assert_equal(10, u.building.size)
u:set_orders("MACHE 100 STRASSE OST\nBEZAHLE NICHT")
process_orders()
assert_equal(0, r.roads[3]) -- Karawane ist noch nicht fertig
u:set_orders("MACHE 200 STRASSE OST")
process_orders()
assert_equal(100, r.roads[3]) -- maximaler Ausbau Strasse in Wüsten
assert_equal(100, u:get_item('stone'))
u:set_orders("ZERSTOERE 1")
process_orders()
assert_equal(9, u.building.size)
assert_equal(100, r.roads[3])
u:set_orders("ZERSTOERE 9")
process_orders()
assert_nil(u.building)
assert_equal(50, r.roads[3])
end
1 change: 0 additions & 1 deletion scripts/tests/e2/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ function test_speedsail()
local u = unit.create(f, r, 1)

turn_begin()
u.name = 'Xolgrim'
u.ship = ship.create(r, "boat")
u:add_item("speedsail", 2)
u:clear_orders()
Expand Down
1 change: 0 additions & 1 deletion scripts/tests/harbor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ function test_leave_harbour()
local u = unit.create(f, r1, 1)
local u2 = unit.create(f, r, 1)
u2.building = b
u.name = 'Xolgrim'
u.ship = sh
u:set_skill('sailing', 10)

Expand Down
2 changes: 0 additions & 2 deletions scripts/tests/laws.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ function test_give_and_forget()
local f = faction.create("human")
local u1 = unit.create(f, r, 1)
local u2 = unit.create(f, r, 1)
u1.name = 'Xolgrim'
u1:set_skill('alchemy', 1)
u1:set_skill('crossbow', 1)
u2:set_skill('alchemy', 1)
Expand All @@ -194,7 +193,6 @@ function test_steal_from_pool()
local u = unit.create(faction.create("human"), r)
u:set_skill('stealth', 1)
u:set_orders("BEKLAUE " .. itoa36(u1.id))
u.name = 'Xolgrim'
process_orders()
assert_equal(50, u:get_item('money'))
assert_equal(50, u2:get_item('money'))
Expand Down
2 changes: 1 addition & 1 deletion src/bind_region.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ static int tolua_region_get_next(lua_State * L)
direction_t dir = (direction_t)tolua_tonumber(L, 2, 0);

if (dir >= 0 && dir < MAXDIRECTIONS) {
tolua_pushusertype(L, (void *)r_connect(self, dir), "region");
tolua_pushusertype(L, (void *)rconnect(self, dir), "region");
return 1;
}
return 0;
Expand Down
26 changes: 26 additions & 0 deletions src/economy.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#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 @@ -1698,6 +1699,30 @@ static void test_destroy_cmd(CuTest* tc) {
test_teardown();
}

static void test_make_road(CuTest *tc) {
region *r, *r2;
unit *u;
faction *f;
struct item_type *itype;
terrain_type *t_plain;

test_setup();
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);
set_level(u, SK_ROAD_BUILDING, 10);
scale_number(u, 10);
i_change(&u->items, itype = test_create_itemtype("stone"), 100);
u->thisorder = create_order(K_MAKE, f->locale,
"50 %s %s", param_name(P_ROAD, f->locale), LOC(f->locale, directions[D_EAST]));
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();
}

static void test_make_zero(CuTest* tc) {
unit* u;
faction* f;
Expand Down Expand Up @@ -1784,6 +1809,7 @@ CuSuite *get_economy_suite(void)
SUITE_ADD_TEST(suite, test_destroy_road);
SUITE_ADD_TEST(suite, test_destroy_road_limit);
SUITE_ADD_TEST(suite, test_destroy_road_guard);
SUITE_ADD_TEST(suite, test_make_road);
SUITE_ADD_TEST(suite, test_make_zero);
SUITE_ADD_TEST(suite, test_entertain_fair);
return suite;
Expand Down
11 changes: 3 additions & 8 deletions src/kernel/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <stdio.h>
#include <stdlib.h>

int nextborder = 0;

#define BORDER_MAXHASH 8191
connection *borders[BORDER_MAXHASH];
border_type *bordertypes;
Expand Down Expand Up @@ -69,7 +67,7 @@ void walk_connections(region *r, void(*cb)(connection *, void *), void *data) {

walk_i(r, borders[key], cb, data);
for (d = 0; d != MAXDIRECTIONS; ++d) {
region *rn = r_connect(r, d);
region *rn = rconnect(r, d);
if (rn) {
int k = reg_hashkey(rn);
if (k < key) {
Expand Down Expand Up @@ -103,7 +101,7 @@ connection *get_borders(const region * r1, const region * r2)
return *bp;
}

connection *new_border(border_type * type, region * from, region * to, int id)
connection *create_border(border_type * type, region * from, region * to)
{
connection *b, **bp;

Expand All @@ -117,7 +115,6 @@ connection *new_border(border_type * type, region * from, region * to, int id)
b->type = type;
b->from = from;
b->to = to;
b->id = (id > 0) ? id : ++nextborder;

if (type->init) {
type->init(b);
Expand Down Expand Up @@ -547,7 +544,6 @@ void write_borders(struct storage *store)
if (b->type->valid && !b->type->valid(b))
continue;
WRITE_TOK(store, b->type->_name);
WRITE_INT(store, b->id);
WRITE_INT(store, b->from->uid);
WRITE_INT(store, b->to->uid);

Expand Down Expand Up @@ -612,8 +608,7 @@ int read_borders(gamedata *data)
}
}
if (b == NULL) {
b = new_border(type, from, to, bid);
assert(bid <= nextborder);
b = create_border(type, from, to);
}
type->read(b, data);
if (!type->write) {
Expand Down
5 changes: 1 addition & 4 deletions src/kernel/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ extern "C" {
struct gamedata;
struct unit;

extern int nextborder;

typedef struct connection {
struct border_type *type; /* the type of this connection */
struct connection *next; /* next connection between these regions */
struct connection *nexthash; /* next connection between these regions */
struct region *from, *to; /* borders can be directed edges */
variant data;
int id; /* unique id */
} connection;

typedef struct border_type {
Expand Down Expand Up @@ -86,7 +83,7 @@ extern "C" {
connection *get_borders(const struct region *r1,
const struct region *r2);
/* returns the list of borders between r1 and r2 or r2 and r1 */
connection *new_border(border_type *type, struct region *from, struct region *to, int id);
connection *create_border(border_type *type, struct region *from, struct region *to);
/* creates a connection of the specified type */
void erase_border(connection * b);
/* remove the connection from memory */
Expand Down
3 changes: 2 additions & 1 deletion src/kernel/gamedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
#define FIX_SHADOWS_VERSION 379 /* shadowdemon/-master skills, bug 3011 */
#define FIX_SHAPESHIFT_IRACE_VERSION 380 /* shapeshift spell, bug 2991 */
#define SKILL_DAYS_VERSION 381 /* skills are stored as days, not weeks */
#define BORDER_ID_VERSION 382 /* borders no longer have an id, from turn 1376 */

#define RELEASE_VERSION SKILL_DAYS_VERSION /* use for new datafiles */
#define RELEASE_VERSION BORDER_ID_VERSION /* use for new datafiles */
#define MIN_VERSION UIDHASH_VERSION /* minimal datafile we support */
#define MAX_VERSION RELEASE_VERSION /* change this if we can need to read the future datafile, and we can do so */

Expand Down
6 changes: 3 additions & 3 deletions src/kernel/region.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,10 @@ void runhash(region * r)
regionhash[key] = DELMARKER;
}

region *r_connect(const region * r, direction_t dir)
region *rconnect(const region * r, direction_t dir)
{
region *result;
int x, y;
region *rmodify = (region *)r;
if (dir < 0 || dir >= MAXDIRECTIONS) {
return NULL;
}
Expand All @@ -344,6 +343,7 @@ region *r_connect(const region * r, direction_t dir)
pnormalize(&x, &y, rplane(r));
result = rfindhash(x, y);
if (result) {
region *rmodify = (region *)r;
rmodify->connect[dir] = result;
result->connect[back[dir]] = rmodify;
}
Expand Down Expand Up @@ -544,7 +544,7 @@ void rsetroad(region * r, direction_t d, int val)
}
if (!b) {
if (!val) return;
b = new_border(&bt_road, r, r2, 0);
b = create_border(&bt_road, r, r2);
}
if (r == b->from) {
b->data.sa[0] = (short)val;
Expand Down
3 changes: 1 addition & 2 deletions src/kernel/region.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ void region_set_owner(struct region *r, struct faction *owner, int turn);
struct faction *region_get_owner(const struct region *r);
struct alliance *region_get_alliance(const struct region *r);

struct region *r_connect(const struct region *, direction_t dir);
#define rconnect(r, dir) ((r)->connect[dir]?(r)->connect[dir]:r_connect(r, (direction_t)dir))
struct region *rconnect(const struct region *, direction_t dir);

void free_regions(void);
void free_land(struct land_region * lr);
Expand Down
40 changes: 40 additions & 0 deletions src/kernel/region.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "building.h"
#include "calendar.h"
#include "config.h"
#include "connection.h"
#include "resources.h"
#include "unit.h"
#include "terrain.h"
Expand Down Expand Up @@ -95,6 +96,43 @@ static void test_region_getset_resource(CuTest *tc) {
test_teardown();
}

static void test_roads(CuTest *tc) {
region *r, *r2;

test_setup();
r = test_create_plain(0, 0);
r2 = test_create_plain(1, 0);
rsetroad(r, D_EAST, 100);
rsetroad(r2, D_WEST, 50);
CuAssertIntEquals(tc, 100, rroad(r, D_EAST));
CuAssertIntEquals(tc, 50, rroad(r2, D_WEST));
test_teardown();
}

static void cb_connection(struct connection *c, void *data)
{
++*(int *)data;
}

static void test_borders(CuTest *tc) {
region *r, *r2;
connection *c;
int n = 0;

test_setup();
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);
CuAssertPtrEquals(tc, c, get_borders(r, r2));
CuAssertPtrEquals(tc, c, get_borders(r2, r));

create_border(&bt_road, r, test_create_plain(-1, 0));
walk_connections(r, cb_connection, &n);
CuAssertIntEquals(tc, 2, n);
test_teardown();
}

static void test_trees(CuTest *tc) {
region *r;

Expand Down Expand Up @@ -138,6 +176,8 @@ CuSuite *get_region_suite(void)
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_newterrain);
SUITE_ADD_TEST(suite, test_terraform);
SUITE_ADD_TEST(suite, test_roads);
SUITE_ADD_TEST(suite, test_borders);
SUITE_ADD_TEST(suite, test_trees);
SUITE_ADD_TEST(suite, test_mourning);
SUITE_ADD_TEST(suite, test_region_getset_resource);
Expand Down
Loading

0 comments on commit db3062b

Please sign in to comment.