diff --git a/src/economy.c b/src/economy.c index 1b6a4136b..5bbeb4106 100644 --- a/src/economy.c +++ b/src/economy.c @@ -2747,6 +2747,24 @@ static int max_skill(region * r, faction * f, skill_t sk) return w; } +message * can_steal(const unit * u, struct order *ord) { + plane *pl; + + if (fval(u_race(u), RCF_NOSTEAL)) { + return msg_feedback(u, ord, "race_nosteal", "race", u_race(u)); + } + + if (fval(u->region->terrain, SEA_REGION) && u_race(u) != get_race(RC_AQUARIAN)) { + return msg_feedback(u, ord, "error_onlandonly", ""); + } + + pl = rplane(u->region); + if (pl && fval(pl, PFL_NOATTACK)) { + return msg_feedback(u, ord, "error270", ""); + } + return 0; +} + static void steal_cmd(unit * u, struct order *ord, request ** stealorders) { const resource_type *rring = get_resourcetype(R_RING_OF_NIMBLEFINGER); @@ -2756,27 +2774,15 @@ static void steal_cmd(unit * u, struct order *ord, request ** stealorders) unit *u2 = NULL; region *r = u->region; faction *f = NULL; - plane *pl; + message * msg; assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH)); - if (fval(u_race(u), RCF_NOSTEAL)) { - ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "race_nosteal", "race", - u_race(u))); - return; + msg = can_steal(u, ord); + if (msg) { + ADDMSG(&u->faction->msgs, msg); + return; } - - if (fval(r->terrain, SEA_REGION) && u_race(u) != get_race(RC_AQUARIAN)) { - ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_onlandonly", "")); - return; - } - - pl = rplane(r); - if (pl && fval(pl, PFL_NOATTACK)) { - cmistake(u, ord, 270, MSG_INCOME); - return; - } - init_tokens(ord); skip_token(); id = read_unitid(u->faction, r); diff --git a/src/economy.h b/src/economy.h index 8b44df380..63e5cbeaf 100644 --- a/src/economy.h +++ b/src/economy.h @@ -57,6 +57,8 @@ extern "C" { extern int give_control_cmd(struct unit *u, struct order *ord); extern void give_control(struct unit * u, struct unit * u2); + struct message * can_steal(const struct unit * u, struct order *ord); + #ifdef __cplusplus } #endif diff --git a/src/economy.test.c b/src/economy.test.c index 61294b1da..e0b9e16b0 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -2,60 +2,122 @@ #include #include "economy.h" +#include #include +#include #include #include #include +#include #include #include static void test_give_control_building(CuTest * tc) { - unit *u1, *u2; - building *b; - struct faction *f; - region *r; + unit *u1, *u2; + building *b; + struct faction *f; + region *r; - test_cleanup(); - test_create_world(); - f = test_create_faction(0); - r = findregion(0, 0); - b = test_create_building(r, 0); - u1 = test_create_unit(f, r); - u_set_building(u1, b); - u2 = test_create_unit(f, r); - u_set_building(u2, b); - CuAssertPtrEquals(tc, u1, building_owner(b)); - give_control(u1, u2); - CuAssertPtrEquals(tc, u2, building_owner(b)); + test_cleanup(); + test_create_world(); + f = test_create_faction(0); + r = findregion(0, 0); + b = test_create_building(r, 0); + u1 = test_create_unit(f, r); + u_set_building(u1, b); + u2 = test_create_unit(f, r); + u_set_building(u2, b); + CuAssertPtrEquals(tc, u1, building_owner(b)); + give_control(u1, u2); + CuAssertPtrEquals(tc, u2, building_owner(b)); + test_cleanup(); } static void test_give_control_ship(CuTest * tc) { - unit *u1, *u2; - ship *sh; - struct faction *f; - region *r; + unit *u1, *u2; + ship *sh; + struct faction *f; + region *r; - test_cleanup(); - test_create_world(); - f = test_create_faction(0); - r = findregion(0, 0); - sh = test_create_ship(r, 0); - u1 = test_create_unit(f, r); - u_set_ship(u1, sh); - u2 = test_create_unit(f, r); - u_set_ship(u2, sh); - CuAssertPtrEquals(tc, u1, ship_owner(sh)); - give_control(u1, u2); - CuAssertPtrEquals(tc, u2, ship_owner(sh)); + test_cleanup(); + test_create_world(); + f = test_create_faction(0); + r = findregion(0, 0); + sh = test_create_ship(r, 0); + u1 = test_create_unit(f, r); + u_set_ship(u1, sh); + u2 = test_create_unit(f, r); + u_set_ship(u2, sh); + CuAssertPtrEquals(tc, u1, ship_owner(sh)); + give_control(u1, u2); + CuAssertPtrEquals(tc, u2, ship_owner(sh)); + test_cleanup(); +} + +static struct { + struct unit *u; + struct region *r; + struct faction *f; +} steal; + +static void setup_steal(terrain_type *ter, race *rc) { + steal.r = test_create_region(0, 0, ter); + steal.f = test_create_faction(0); + steal.u = test_create_unit(steal.f, steal.r); +} + +static void test_steal_okay(CuTest * tc) { + race *rc; + terrain_type *ter; + + test_cleanup(); + ter = test_create_terrain("plain", LAND_REGION); + rc = test_create_race("human"); + rc->flags = 0; + setup_steal(ter, rc); + CuAssertPtrEquals(tc, 0, can_steal(steal.u, 0)); + test_cleanup(); +} + +static void test_steal_nosteal(CuTest * tc) { + race *rc; + terrain_type *ter; + message *msg; + + test_cleanup(); + ter = test_create_terrain("plain", LAND_REGION); + rc = test_create_race("human"); + rc->flags = RCF_NOSTEAL; + setup_steal(ter, rc); + CuAssertPtrNotNull(tc, msg=can_steal(steal.u, 0)); + msg_release(msg); + test_cleanup(); +} + +static void test_steal_ocean(CuTest * tc) { + race *rc; + terrain_type *ter; + message *msg; + + test_cleanup(); + ter = test_create_terrain("ocean", SEA_REGION); + rc = test_create_race("human"); + setup_steal(ter, rc); + CuAssertPtrNotNull(tc, msg = can_steal(steal.u, 0)); + msg_release(msg); + test_cleanup(); } CuSuite *get_economy_suite(void) { - CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_give_control_building); - SUITE_ADD_TEST(suite, test_give_control_ship); - return suite; + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_give_control_building); + SUITE_ADD_TEST(suite, test_give_control_ship); + SUITE_ADD_TEST(suite, test_steal_okay); + SUITE_ADD_TEST(suite, test_steal_ocean); + SUITE_ADD_TEST(suite, test_steal_nosteal); + return suite; } diff --git a/src/eressea.c b/src/eressea.c index 16e9763f3..4900693a3 100755 --- a/src/eressea.c +++ b/src/eressea.c @@ -49,11 +49,10 @@ void game_done(void) void game_init(void) { - kernel_init(); + kernel_init(); register_triggers(); register_xmas(); - register_reports(); register_nr(); register_cr(); diff --git a/src/kernel/config.c b/src/kernel/config.c index 4d0fcacfa..61f02a211 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -46,6 +46,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "plane.h" #include "pool.h" #include "race.h" +#include "reports.h" #include "region.h" #include "save.h" #include "ship.h" @@ -2744,8 +2745,12 @@ void attrib_init(void) void kernel_init(void) { - attrib_init(); - translation_init(); + register_reports(); + if (!mt_find("missing_message")) { + mt_register(mt_new_va("missing_message", "name:string", 0)); + } + attrib_init(); + translation_init(); } static order * defaults[MAXLOCALES]; diff --git a/src/test_eressea.c b/src/test_eressea.c index 2b0fbee53..f93f88e48 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -1,7 +1,10 @@ +#include +#include #include #include #include + CuSuite *get_tests_suite(void); CuSuite *get_callback_suite(void); CuSuite *get_jsonconf_suite(void); @@ -39,6 +42,7 @@ int RunAllTests(void) int flags = log_flags; log_flags = LOG_FLUSH | LOG_CPERROR; + kernel_init(); /* self-test */ CuSuiteAddSuite(suite, get_tests_suite());