diff --git a/src/alchemy.c b/src/alchemy.c index a1dd86c0a..ace6a62dd 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -77,7 +77,7 @@ void herbsearch(unit * u, int max) herbsfound = ntimespprob(effsk * u->number, (double)rherbs(r) / 100.0F, -0.01F); herbsfound = _min(herbsfound, max); - rsetherbs(r, (short) (rherbs(r) - herbsfound)); + rsetherbs(r, rherbs(r) - herbsfound); if (herbsfound) { produceexp(u, SK_HERBALISM, u->number); diff --git a/src/economy.c b/src/economy.c index 6311d0c3a..12843bd58 100644 --- a/src/economy.c +++ b/src/economy.c @@ -2298,7 +2298,8 @@ static void plant(unit * u, int raw) /* Alles ok. Abziehen. */ use_pooled(u, rt_water, GET_DEFAULT, 1); use_pooled(u, itype->rtype, GET_DEFAULT, n); - rsetherbs(r, (short) (rherbs(r) + planted)); + /* TODO should there a region maximum for herbs? */ + rsetherbs(r, rherbs(r) + planted); ADDMSG(&u->faction->msgs, msg_message("plant", "unit region amount herb", u, r, planted, itype->rtype)); } diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index b1c3bcd0f..9c3b7d888 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -6,6 +6,7 @@ build.test.c config.test.c group.test.c faction.test.c +region.test.c unit.test.c save.test.c ship.test.c diff --git a/src/kernel/region.c b/src/kernel/region.c index ed56851ee..5add8f7b2 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -598,12 +598,13 @@ int rpeasants(const region * r) return r->land ? r->land->peasants : 0; } -void rsetpeasants(region * r, int value) +int rsetpeasants(const region * r, int value) { + assert(value >= 0); if (r->land) { - assert(value >= 0); - r->land->peasants = value; + return r->land->peasants = value; } + return 0; } int rmoney(const region * r) @@ -611,12 +612,13 @@ int rmoney(const region * r) return r->land ? r->land->money : 0; } -void rsethorses(const region * r, int value) +int rsethorses(const region * r, int value) { + assert(value >= 0); if (r->land) { - assert(value >= 0); - r->land->horses = value; + return r->land->horses = value; } + return 0; } int rhorses(const region * r) @@ -624,12 +626,13 @@ int rhorses(const region * r) return r->land ? r->land->horses : 0; } -void rsetmoney(region * r, int value) +int rsetmoney(const region * r, int value) { + assert(value >= 0); if (r->land) { - assert(value >= 0); - r->land->money = value; + return r->land->money = value; } + return 0; } int rherbs(const struct region *r) @@ -637,12 +640,13 @@ int rherbs(const struct region *r) return r->land?r->land->herbs:0; } -void rsetherbs(const struct region *r, int value) +int rsetherbs(const struct region *r, int value) { + assert(value >= 0 && value < (1 << 15)); if (r->land) { - assert(value >= 0); - r->land->herbs = (short)(value); + return r->land->herbs = (short)(value); } + return 0; } @@ -704,12 +708,11 @@ int rtrees(const region * r, int ageclass) int rsettrees(const region * r, int ageclass, int value) { - if (!r->land) - assert(value == 0); - else { + if (r->land) { assert(value >= 0); return r->land->trees[ageclass] = value; } + assert(value == 0); return 0; } @@ -1124,7 +1127,7 @@ void terraform_region(region * r, const terrain_type * terrain) } if (itype != NULL) { rsetherbtype(r, itype); - rsetherbs(r, (short)(50 + rng_int() % 31)); + rsetherbs(r, 50 + rng_int() % 31); } else { rsetherbtype(r, NULL); diff --git a/src/kernel/region.h b/src/kernel/region.h index 5974fd96c..b2d33e9f0 100644 --- a/src/kernel/region.h +++ b/src/kernel/region.h @@ -195,14 +195,14 @@ extern "C" { int rsettrees(const struct region *r, int ageclass, int value); int rpeasants(const struct region *r); - void rsetpeasants(struct region *r, int value); + int rsetpeasants(const struct region *r, int value); int rmoney(const struct region *r); - void rsetmoney(struct region *r, int value); + int rsetmoney(const struct region *r, int value); int rhorses(const struct region *r); - void rsethorses(const struct region *r, int value); + int rsethorses(const struct region *r, int value); int rherbs(const struct region *r); - void rsetherbs(const struct region *r, int value); + int rsetherbs(const struct region *r, int value); #define rbuildings(r) ((r)->buildings) diff --git a/src/kernel/region.test.c b/src/kernel/region.test.c new file mode 100644 index 000000000..da7c36780 --- /dev/null +++ b/src/kernel/region.test.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include "region.h" + +#include +#include + +#include + +static void assert_setter(CuTest *tc, int (*setter)(const region *, int), + int (*getter)(const region *), region *land, region *ocean, bool throwaway) { + CuAssertIntEquals(tc, 0, setter(land, 0)); + CuAssertIntEquals(tc, 0, getter(land)); + CuAssertIntEquals(tc, 42, setter(land, 42)); + CuAssertIntEquals(tc, 42, getter(land)); + CuAssertIntEquals(tc, 0, setter(ocean, 0)); + CuAssertIntEquals(tc, 0, getter(ocean)); + if (throwaway) { + CuAssertIntEquals(tc, 0, setter(ocean, 42)); + CuAssertIntEquals(tc, 0, getter(ocean)); + } +} + +static int t_settrees0(const region *r, int trees) { + return rsettrees(r, 0, trees); +} + +static int t_trees0(const region *r) { + return rtrees(r, 0); +} + +static void test_setters(CuTest *tc) { + region *land, *ocean; + + test_cleanup(); + test_create_world(); + land = findregion(0, 0); + ocean = findregion(-1, 0); + assert(fval(ocean->terrain, SEA_REGION) && fval(land->terrain, LAND_REGION)); + + assert_setter(tc, rsetherbs, rherbs, land, ocean, true); + assert_setter(tc, rsethorses, rhorses, land, ocean, true); + assert_setter(tc, rsetmoney, rmoney, land, ocean, true); + assert_setter(tc, rsetpeasants, rpeasants, land, ocean, true); + assert_setter(tc, t_settrees0, t_trees0, land, ocean, false); + + test_cleanup(); +} + +CuSuite *get_region_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_setters); + return suite; +} diff --git a/src/kernel/save.c b/src/kernel/save.c index d4df2b6f8..40894b3cd 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -946,7 +946,7 @@ static region *readregion(struct gamedata *data, int x, int y) rsetherbtype(r, NULL); } READ_INT(data->store, &n); - rsetherbs(r, (short)n); + rsetherbs(r, n); READ_INT(data->store, &n); rsetpeasants(r, n); READ_INT(data->store, &n); diff --git a/src/kernel/terrain.h b/src/kernel/terrain.h index 931a74861..75368b54e 100644 --- a/src/kernel/terrain.h +++ b/src/kernel/terrain.h @@ -36,6 +36,8 @@ extern "C" { #define SWIM_INTO (1<<8) /* man darf hierhin schwimmen */ #define WALK_INTO (1<<9) /* man darf hierhin laufen */ + struct region; + typedef struct production_rule { char *name; const struct resource_type *rtype; diff --git a/src/laws.c b/src/laws.c index 7dc6cd0af..bbb7ee342 100755 --- a/src/laws.c +++ b/src/laws.c @@ -692,7 +692,7 @@ growing_herbs(region * r, const int current_season, const int last_weeks_season) int i; for (i = rherbs(r); i > 0; i--) { if (rng_int() % 100 < (100 - rherbs(r))) - rsetherbs(r, (short)(rherbs(r) + 1)); + rsetherbs(r, rherbs(r) + 1); } } } diff --git a/src/test_eressea.c b/src/test_eressea.c index f3d4b71ae..adc8b243d 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -98,6 +98,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(item); ADD_SUITE(magic); ADD_SUITE(alchemy); + ADD_SUITE(region); ADD_SUITE(reports); ADD_SUITE(save); ADD_SUITE(ship);