From ef415c66d0625bc52872b9151751110ecfaf1584 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 11 Dec 2014 08:35:05 +0100 Subject: [PATCH] Fix GIVE 0 behavior on oceans. --- res/core/messages.xml | 9 ++++++++ src/give.c | 28 +++++++++++++++--------- src/give.test.c | 51 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 13 deletions(-) diff --git a/res/core/messages.xml b/res/core/messages.xml index dfbeecd07..e65d89d1e 100644 --- a/res/core/messages.xml +++ b/res/core/messages.xml @@ -7112,6 +7112,15 @@ "$unit($target) receives $int($amount) $resource($resource,$amount) from $unit($unit)." + + + + + + "$unit($unit) ertränkt $int($amount) Person$if($eq($amount,1),"","en")." + "$unit($unit) drowns $int($amount)." + + diff --git a/src/give.c b/src/give.c index ecbbe949c..8887018ba 100644 --- a/src/give.c +++ b/src/give.c @@ -393,6 +393,9 @@ message * disband_men(int n, unit * u, struct order *ord) { a->data.i += n; } #endif + if (fval(u->region->terrain, SEA_REGION)) { + return msg_message("give_person_ocean", "unit amount", u, n); + } return msg_message("give_person_peasants", "unit amount", u, n); } @@ -421,12 +424,19 @@ void give_unit(unit * u, unit * u2, order * ord) } if (u2 == NULL) { + message *msg; if (fval(r->terrain, SEA_REGION)) { - cmistake(u, ord, 152, MSG_COMMERCE); + /* TODO: why is this here, but the unit does not actually seem to lose any men? */ + msg = disband_men(u->number, u, ord); + if (msg) { + ADDMSG(&u->faction->msgs, msg); + } + else { + cmistake(u, ord, 152, MSG_COMMERCE); + } } else if (getunitpeasants) { unit *u3; - message *msg; for (u3 = r->units; u3; u3 = u3->next) if (u3->faction == u->faction && u != u3) @@ -596,12 +606,10 @@ void give_cmd(unit * u, order * ord) msg_feedback(u, ord, "race_notake", "race", u_race(u2))); return; } - if (!u2) { - if (!getunitpeasants) { - ADDMSG(&u->faction->msgs, msg_feedback(u, ord, - "feedback_unit_not_found", "")); - return; - } + if (!u2 && !getunitpeasants) { + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, + "feedback_unit_not_found", "")); + return; } if (u->items) { item **itmp = &u->items; @@ -691,7 +699,7 @@ void give_cmd(unit * u, order * ord) } else { message * msg; - msg = getunitpeasants ? disband_men(u->number, u, ord) : give_men(u->number, u, u2, ord); + msg = u2 ? give_men(u->number, u, u2, ord) : disband_men(u->number, u, ord); if (msg) { ADDMSG(&u->faction->msgs, msg); } @@ -747,7 +755,7 @@ void give_cmd(unit * u, order * ord) msg_feedback(u, ord, "race_noregroup", "race", u_race(u))); return; } - msg = getunitpeasants ? disband_men(u->number, u, ord) : give_men(u->number, u, u2, ord); + msg = u2 ? give_men(u->number, u, u2, ord) : disband_men(u->number, u, ord); if (msg) { ADDMSG(&u->faction->msgs, msg); } diff --git a/src/give.test.c b/src/give.test.c index f376c4577..517675e4a 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -36,6 +36,33 @@ static void setup_give(struct give *env) { env->itype->flags |= ITF_HERB; } +static void test_give_unit_to_peasants(CuTest * tc) { + struct give env; + test_cleanup(); + env.f1 = test_create_faction(0); + env.f2 = 0; + setup_give(&env); + rsetpeasants(env.r, 0); + getunitpeasants = true; + give_unit(env.src, NULL, NULL); + CuAssertIntEquals(tc, 0, env.src->number); + CuAssertIntEquals(tc, 1, env.r->land->peasants); + test_cleanup(); +} + +static void test_give_unit_in_ocean(CuTest * tc) { + struct give env; + test_cleanup(); + env.f1 = test_create_faction(0); + env.f2 = 0; + setup_give(&env); + env.r->terrain = test_create_terrain("ocean", SEA_REGION); + getunitpeasants = true; + give_unit(env.src, NULL, NULL); + CuAssertIntEquals(tc, 0, env.src->number); + test_cleanup(); +} + static void test_give_men(CuTest * tc) { struct give env; test_cleanup(); @@ -47,6 +74,21 @@ static void test_give_men(CuTest * tc) { test_cleanup(); } +static void test_give_men_in_ocean(CuTest * tc) { + struct give env; + message * msg; + + test_cleanup(); + env.f1 = test_create_faction(0); + env.f2 = 0; + setup_give(&env); + env.r->terrain = test_create_terrain("ocean", SEA_REGION); + msg = disband_men(1, env.src, NULL); + CuAssertStrEquals(tc, "give_person_ocean", (const char *)msg->parameters[0].v); + CuAssertIntEquals(tc, 0, env.src->number); + test_cleanup(); +} + static void test_give_men_too_many(CuTest * tc) { struct give env; test_cleanup(); @@ -118,16 +160,16 @@ static void test_give_men_not_to_self(CuTest * tc) { static void test_give_peasants(CuTest * tc) { struct give env; message * msg; - int peasants; + test_cleanup(); env.f1 = test_create_faction(0); env.f2 = 0; setup_give(&env); - peasants = env.r->land->peasants; + rsetpeasants(env.r, 0); msg = disband_men(1, env.src, NULL); CuAssertStrEquals(tc, "give_person_peasants", (const char*)msg->parameters[0].v); CuAssertIntEquals(tc, 0, env.src->number); - CuAssertIntEquals(tc, peasants+1, env.r->land->peasants); + CuAssertIntEquals(tc, 1, env.r->land->peasants); test_cleanup(); } @@ -207,11 +249,14 @@ CuSuite *get_give_suite(void) CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_give); SUITE_ADD_TEST(suite, test_give_men); + SUITE_ADD_TEST(suite, test_give_men_in_ocean); SUITE_ADD_TEST(suite, test_give_men_none); SUITE_ADD_TEST(suite, test_give_men_too_many); SUITE_ADD_TEST(suite, test_give_men_other_faction); SUITE_ADD_TEST(suite, test_give_men_requires_contact); SUITE_ADD_TEST(suite, test_give_men_not_to_self); + SUITE_ADD_TEST(suite, test_give_unit_in_ocean); + SUITE_ADD_TEST(suite, test_give_unit_to_peasants); SUITE_ADD_TEST(suite, test_give_peasants); SUITE_ADD_TEST(suite, test_give_herbs); SUITE_ADD_TEST(suite, test_give_okay);