Merge branch 'hotfix/bug-2109-give-unit' of https://github.com/badgerman/eressea into badgerman-hotfix/bug-2109-give-unit

Conflicts:
	scripts/eressea/xmlconf.lua
This commit is contained in:
Enno Rehling 2015-06-08 09:43:36 +02:00
commit 266b019928
3 changed files with 42 additions and 12 deletions

View File

@ -401,7 +401,7 @@ message * disband_men(int n, unit * u, struct order *ord) {
void give_unit(unit * u, unit * u2, order * ord) void give_unit(unit * u, unit * u2, order * ord)
{ {
region *r = u->region; region *r = u->region;
int n = u->number; int maxt = max_transfers();
if (!rule_transfermen() && u->faction != u2->faction) { if (!rule_transfermen() && u->faction != u2->faction) {
cmistake(u, ord, 74, MSG_COMMERCE); cmistake(u, ord, 74, MSG_COMMERCE);
@ -472,9 +472,11 @@ void give_unit(unit * u, unit * u2, order * ord)
cmistake(u, ord, 105, MSG_COMMERCE); cmistake(u, ord, 105, MSG_COMMERCE);
return; return;
} }
if (u2->faction->newbies + n > max_transfers()) { if (maxt >= 0 && u->faction != u2->faction) {
cmistake(u, ord, 129, MSG_COMMERCE); if (u2->faction->newbies + u->number > maxt) {
return; cmistake(u, ord, 129, MSG_COMMERCE);
return;
}
} }
if (u_race(u) != u2->faction->race) { if (u_race(u) != u2->faction->race) {
if (u2->faction->race != get_race(RC_HUMAN)) { if (u2->faction->race != get_race(RC_HUMAN)) {
@ -510,9 +512,9 @@ void give_unit(unit * u, unit * u2, order * ord)
cmistake(u, ord, 156, MSG_COMMERCE); cmistake(u, ord, 156, MSG_COMMERCE);
return; return;
} }
add_give(u, u2, n, n, get_resourcetype(R_PERSON), ord, 0); add_give(u, u2, u->number, u->number, get_resourcetype(R_PERSON), ord, 0);
u_setfaction(u, u2->faction); u_setfaction(u, u2->faction);
u2->faction->newbies += n; u2->faction->newbies += u->number;
} }
bool can_give_to(unit *u, unit *u2) { bool can_give_to(unit *u, unit *u2) {

View File

@ -3,6 +3,7 @@
#include "give.h" #include "give.h"
#include "economy.h" #include "economy.h"
#include <kernel/ally.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/terrain.h> #include <kernel/terrain.h>
@ -34,6 +35,10 @@ static void setup_give(struct give *env) {
env->dst = env->f2 ? test_create_unit(env->f2, env->r) : 0; env->dst = env->f2 ? test_create_unit(env->f2, env->r) : 0;
env->itype = it_get_or_create(rt_get_or_create("money")); env->itype = it_get_or_create(rt_get_or_create("money"));
env->itype->flags |= ITF_HERB; env->itype->flags |= ITF_HERB;
if (env->f1 && env->f2) {
ally * al = ally_add(&env->f2->allies, env->f1);
al->status = HELP_GIVE;
}
} }
static void test_give_unit_to_peasants(CuTest * tc) { static void test_give_unit_to_peasants(CuTest * tc) {
@ -49,6 +54,25 @@ static void test_give_unit_to_peasants(CuTest * tc) {
test_cleanup(); test_cleanup();
} }
static void test_give_unit(CuTest * tc) {
struct give env;
test_cleanup();
env.f1 = test_create_faction(0);
env.f2 = test_create_faction(0);
setup_give(&env);
env.r->terrain = test_create_terrain("ocean", SEA_REGION);
set_param(&global.parameters, "rules.give.max_men", "0");
give_unit(env.src, env.dst, NULL);
CuAssertPtrEquals(tc, env.f1, env.src->faction);
CuAssertIntEquals(tc, 0, env.f2->newbies);
set_param(&global.parameters, "rules.give.max_men", "-1");
give_unit(env.src, env.dst, NULL);
CuAssertPtrEquals(tc, env.f2, env.src->faction);
CuAssertIntEquals(tc, 1, env.f2->newbies);
CuAssertPtrEquals(tc, 0, env.f1->units);
test_cleanup();
}
static void test_give_unit_in_ocean(CuTest * tc) { static void test_give_unit_in_ocean(CuTest * tc) {
struct give env; struct give env;
test_cleanup(); test_cleanup();
@ -284,6 +308,7 @@ CuSuite *get_give_suite(void)
SUITE_ADD_TEST(suite, test_give_men_other_faction); 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_requires_contact);
SUITE_ADD_TEST(suite, test_give_men_not_to_self); 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_in_ocean);
SUITE_ADD_TEST(suite, test_give_unit_to_peasants); SUITE_ADD_TEST(suite, test_give_unit_to_peasants);
SUITE_ADD_TEST(suite, test_give_peasants); SUITE_ADD_TEST(suite, test_give_peasants);

View File

@ -696,12 +696,11 @@ static void test_reserve_self(CuTest *tc) {
static void statistic_test(CuTest *tc, int peasants, int luck, int maxp, static void statistic_test(CuTest *tc, int peasants, int luck, int maxp,
double variance, int min_value, int max_value) { double variance, int min_value, int max_value) {
int effect, i; int effect;
for (i = 0; i < 1000; ++i) {
effect = peasant_luck_effect(peasants, luck, maxp, variance); effect = peasant_luck_effect(peasants, luck, maxp, variance);
CuAssertTrue(tc, min_value <= effect); CuAssertTrue(tc, min_value <= effect);
CuAssertTrue(tc, max_value >= effect); CuAssertTrue(tc, max_value >= effect);
}
} }
static void test_peasant_luck_effect(CuTest *tc) { static void test_peasant_luck_effect(CuTest *tc) {
@ -713,6 +712,10 @@ static void test_peasant_luck_effect(CuTest *tc) {
statistic_test(tc, 100, 0, 1000, 0, 0, 0); statistic_test(tc, 100, 0, 1000, 0, 0, 0);
statistic_test(tc, 100, 2, 1000, 0, 1, 1); statistic_test(tc, 100, 2, 1000, 0, 1, 1);
/*
statistic_test(tc, 1000, 400, 1000, 0, (int)(400 * 10 * 0.001 * .75),
(int)(400 * 10 * 0.001 * .75));
*/
statistic_test(tc, 1000, 400, 1000, 0, 3, 3); statistic_test(tc, 1000, 400, 1000, 0, 3, 3);
statistic_test(tc, 1000, 1000, 2000, .5, 1, 501); statistic_test(tc, 1000, 1000, 2000, .5, 1, 501);