From 21e260907c331c853b326cb1f0de7eae27aaa771 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Mar 2017 20:03:49 +0100 Subject: [PATCH] BUG 2311: unit limits were not enforced by GIVE. --- src/give.c | 17 +++++++++++++++++ src/give.test.c | 49 +++++++++++++++++++++++++++++++++---------------- src/laws.c | 2 -- src/laws.h | 1 + src/laws.test.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 98 insertions(+), 19 deletions(-) diff --git a/src/give.c b/src/give.c index 0f1c56c39..1265ab169 100644 --- a/src/give.c +++ b/src/give.c @@ -533,6 +533,23 @@ void give_unit(unit * u, unit * u2, order * ord) } } return; + } else { + int err = checkunitnumber(u2->faction, 1); + if (err) { + if (err == 1) { + ADDMSG(&u->faction->msgs, + msg_feedback(u, ord, + "too_many_units_in_alliance", + "allowed", rule_alliance_limit())); + } + else { + ADDMSG(&u->faction->msgs, + msg_feedback(u, ord, + "too_many_units_in_faction", + "allowed", rule_faction_limit())); + } + return; + } } if (!alliedunit(u2, u->faction, HELP_GIVE) && ucontact(u2, u) == 0) { diff --git a/src/give.test.c b/src/give.test.c index 1767dec36..604e42549 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -54,26 +54,12 @@ static void setup_give(struct give *env) { } } -static void test_give_unit_to_peasants(CuTest * tc) { - struct give env = { 0 }; - test_setup_ex(tc); - env.f1 = test_create_faction(0); - env.f2 = 0; - setup_give(&env); - rsetpeasants(env.r, 0); - give_unit(env.src, NULL, NULL); - CuAssertIntEquals(tc, 0, env.src->number); - CuAssertIntEquals(tc, 1, rpeasants(env.r)); - test_cleanup(); -} - static void test_give_unit(CuTest * tc) { struct give env = { 0 }; test_setup_ex(tc); env.f1 = test_create_faction(0); env.f2 = test_create_faction(0); setup_give(&env); - env.r->terrain = test_create_terrain("ocean", SEA_REGION); config_set("rules.give.max_men", "0"); give_unit(env.src, env.dst, NULL); CuAssertPtrEquals(tc, env.f1, env.src->faction); @@ -88,7 +74,37 @@ static void test_give_unit(CuTest * tc) { test_cleanup(); } -static void test_give_unit_in_ocean(CuTest * tc) { +static void test_give_unit_limits(CuTest * tc) { + struct give env = { 0 }; + test_setup_ex(tc); + env.f1 = test_create_faction(0); + env.f2 = test_create_faction(0); + setup_give(&env); + CuAssertIntEquals(tc, 1, env.f1->num_units); + CuAssertIntEquals(tc, 1, env.f2->num_units); + config_set("rules.limit.faction", "1"); + give_unit(env.src, env.dst, NULL); + CuAssertPtrEquals(tc, env.f1, env.src->faction); + CuAssertIntEquals(tc, 0, env.f2->newbies); + CuAssertIntEquals(tc, 1, env.f1->num_units); + CuAssertIntEquals(tc, 1, env.f2->num_units); + test_cleanup(); +} + +static void test_give_unit_to_peasants(CuTest * tc) { + struct give env = { 0 }; + test_setup_ex(tc); + env.f1 = test_create_faction(0); + env.f2 = 0; + setup_give(&env); + rsetpeasants(env.r, 0); + give_unit(env.src, NULL, NULL); + CuAssertIntEquals(tc, 0, env.src->number); + CuAssertIntEquals(tc, 1, rpeasants(env.r)); + test_cleanup(); +} + +static void test_give_unit_to_ocean(CuTest * tc) { struct give env = { 0 }; test_setup_ex(tc); env.f1 = test_create_faction(0); @@ -429,7 +445,8 @@ CuSuite *get_give_suite(void) 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); - SUITE_ADD_TEST(suite, test_give_unit_in_ocean); + SUITE_ADD_TEST(suite, test_give_unit_limits); + SUITE_ADD_TEST(suite, test_give_unit_to_ocean); SUITE_ADD_TEST(suite, test_give_unit_to_peasants); SUITE_ADD_TEST(suite, test_give_peasants); SUITE_ADD_TEST(suite, test_give_herbs); diff --git a/src/laws.c b/src/laws.c index 6722194a9..9c25ac461 100644 --- a/src/laws.c +++ b/src/laws.c @@ -3028,8 +3028,6 @@ int checkunitnumber(const faction * f, int add) alimit = rule_alliance_limit(); if (alimit) { - /* if unitsperalliance is true, maxunits returns the - number of units allowed in an alliance */ faction *f2; int unitsinalliance = fno; if (unitsinalliance > alimit) { diff --git a/src/laws.h b/src/laws.h index 090d2b978..7f1385483 100755 --- a/src/laws.h +++ b/src/laws.h @@ -58,6 +58,7 @@ extern "C" { void turn_process(void); void turn_end(void); + int checkunitnumber(const struct faction * f, int add); void new_units(void); void defaultorders(void); void quit(void); diff --git a/src/laws.test.c b/src/laws.test.c index 937754988..e8391b67c 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -5,6 +5,7 @@ #include "monsters.h" #include +#include #include #include #include @@ -427,7 +428,51 @@ static void test_unit_limit(CuTest * tc) test_cleanup(); } -extern int checkunitnumber(const faction * f, int add); +static void test_limit_new_units(CuTest * tc) +{ + faction *f; + unit *u; + alliance *al; + + test_setup(); + al = makealliance(1, "Hodor"); + f = test_create_faction(NULL); + u = test_create_unit(f, test_create_region(0, 0, NULL)); + CuAssertIntEquals(tc, 1, f->num_units); + CuAssertIntEquals(tc, 1, f->num_people); + scale_number(u, 10); + CuAssertIntEquals(tc, 10, f->num_people); + config_set("rules.limit.faction", "2"); + + u->orders = create_order(K_MAKETEMP, f->locale, "1"); + new_units(); + CuAssertPtrNotNull(tc, u->next); + CuAssertIntEquals(tc, 2, f->num_units); + + new_units(); + CuAssertIntEquals(tc, 2, f->num_units); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_faction")); + + setalliance(f, al); + + config_set("rules.limit.faction", "3"); + config_set("rules.limit.alliance", "2"); + + new_units(); + CuAssertIntEquals(tc, 2, f->num_units); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_alliance")); + + config_set("rules.limit.alliance", "3"); + u = test_create_unit(test_create_faction(NULL), u->region); + setalliance(u->faction, al); + + new_units(); + CuAssertIntEquals(tc, 2, f->num_units); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_alliance")); + + test_cleanup(); +} + static void test_cannot_create_unit_above_limit(CuTest * tc) { faction *f; @@ -1535,6 +1580,7 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_fishing_does_not_give_goblins_money); SUITE_ADD_TEST(suite, test_fishing_gets_reset); SUITE_ADD_TEST(suite, test_unit_limit); + SUITE_ADD_TEST(suite, test_limit_new_units); SUITE_ADD_TEST(suite, test_update_guards); SUITE_ADD_TEST(suite, test_newbie_cannot_guard); SUITE_ADD_TEST(suite, test_unarmed_cannot_guard);