BUG 2311: unit limits were not enforced by GIVE.

This commit is contained in:
Enno Rehling 2017-03-11 20:03:49 +01:00
parent d633c2a9fb
commit 21e260907c
5 changed files with 98 additions and 19 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -5,6 +5,7 @@
#include "monsters.h"
#include <kernel/ally.h>
#include <kernel/alliance.h>
#include <kernel/config.h>
#include <kernel/building.h>
#include <kernel/faction.h>
@ -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);