bit of testing, code improvements, formatting

This commit is contained in:
Steffen Mecke 2015-01-30 17:40:04 +01:00
parent 4c391ae6f6
commit a97b211e70
3 changed files with 85 additions and 62 deletions

View File

@ -259,43 +259,39 @@ static void calculate_emigration(region * r)
} }
static float peasant_luck_factor(void) { static float peasant_growth_factor(void)
static float factor = -1; {
return get_param_flt(global.parameters, "rules.peasants.growth.factor",
if (factor < 0)
get_param_int(global.parameters, "rules.peasants.peasantluck.factor", PEASANTLUCK);
return factor;
}
static float peasant_growth_factor(void) {
static float factor = -1;
if (factor < 0)
factor = get_param_flt(global.parameters, "rules.peasants.growth.factor",
0.0001F * PEASANTGROWTH); 0.0001F * PEASANTGROWTH);
return factor;
} }
/** Bauern vermehren sich */
#ifndef SLOWLUCK #ifndef SLOWLUCK
int peasant_luck_effect(int peasants, int luck, int maxp) { static float peasant_luck_factor(void)
int births=0; {
double mean = _min(luck, peasants) return get_param_int(global.parameters, "rules.peasants.peasantluck.factor",
* peasant_luck_factor() PEASANTLUCK);
* peasant_growth_factor() }
* ((peasants/(float)maxp < .9)?1:PEASANTFORCE);
births = RAND_ROUND(normalvariate(mean, mean/2+1)); static
int peasant_luck_effect(int peasants, int luck, int maxp, float variance)
{
int births = 0;
double mean = _min(luck, peasants) * peasant_luck_factor()
* peasant_growth_factor() * ((peasants / (float)maxp < .9) ? 1 :
PEASANTFORCE);
births = RAND_ROUND(normalvariate(mean, variance * mean));
if (births <= 0) if (births <= 0)
births = 1; births = 1;
if (births>peasants/2) if (births > peasants / 2)
births=peasants/2+1; births = peasants / 2 + 1;
return (int)births; return (int)births;
} }
#else #else
int peasant_luck_effect(int peasants, int luck, int maxp) { int peasant_luck_effect(int peasants, int luck, int maxp, float variance) {
int n, births=0; int n, births=0;
float factor = peasant_growth_factor();
for (n = peasants; n && luck; --n) { for (n = peasants; n && luck; --n) {
int chances = 0; int chances = 0;
@ -305,7 +301,7 @@ int peasant_luck_effect(int peasants, int luck, int maxp) {
} }
while (chances--) { while (chances--) {
if (rng_double() < peasant_growth_factor()) { if (rng_double() < factor) {
/* Only raise with 75% chance if peasants have /* Only raise with 75% chance if peasants have
* reached 90% of maxpopulation */ * reached 90% of maxpopulation */
if (peasants / (float)maxp < 0.9 || chance(PEASANTFORCE)) { if (peasants / (float)maxp < 0.9 || chance(PEASANTFORCE)) {
@ -336,7 +332,7 @@ static void peasants(region * r)
luck = a->data.i * 1000; luck = a->data.i * 1000;
} }
luck = peasant_luck_effect(peasants, luck, maxp); luck = peasant_luck_effect(peasants, luck, maxp, .5);
ADDMSG(&r->msgs, msg_message("peasantluck_success", "births", luck)); ADDMSG(&r->msgs, msg_message("peasantluck_success", "births", luck));
peasants += births + luck; peasants += births + luck;

View File

@ -1,5 +1,5 @@
#include <platform.h> #include <platform.h>
#include "laws.h" #include "laws.c"
#include <kernel/ally.h> #include <kernel/ally.h>
#include <kernel/config.h> #include <kernel/config.h>
@ -692,6 +692,32 @@ static void test_reserve_self(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void statistic_test(CuTest *tc, int peasants, int luck, int maxp,
float variance, int min_value, int max_value)
{
int effect, i;
for (i = 0; i < 1000; ++i) {
effect = peasant_luck_effect(peasants, luck, maxp, variance);
CuAssertTrue(tc, min_value <= effect);
CuAssertTrue(tc, max_value >= effect);
}
}
static void test_peasant_luck_effect(CuTest *tc)
{
set_param(&global.parameters, "rules.peasants.peasantluck.factor", "10");
set_param(&global.parameters, "rules.peasants.growth.factor", "0.001");
statistic_test(tc, 100, 2, 1000, 0, 1, 1);
statistic_test(tc, 1000, 400, 1000, 0, 400 * 10 * 0.001 * .75,
400 * 10 * 0.001 * .75);
statistic_test(tc, 1000, 1000, 2000, .5, 1, 501);
set_param(&global.parameters, "rules.peasants.growth.factor", "1");
statistic_test(tc, 1000, 1000, 1000, 0, 501, 501);
}
CuSuite *get_laws_suite(void) CuSuite *get_laws_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -722,6 +748,7 @@ CuSuite *get_laws_suite(void)
SUITE_ADD_TEST(suite, test_force_leave_buildings); SUITE_ADD_TEST(suite, test_force_leave_buildings);
SUITE_ADD_TEST(suite, test_force_leave_ships); SUITE_ADD_TEST(suite, test_force_leave_ships);
SUITE_ADD_TEST(suite, test_force_leave_ships_on_ocean); SUITE_ADD_TEST(suite, test_force_leave_ships_on_ocean);
SUITE_ADD_TEST(suite, test_peasant_luck_effect);
return suite; return suite;
} }

View File

@ -6,14 +6,14 @@
static void test_rng_round(CuTest * tc) static void test_rng_round(CuTest * tc)
{ {
double f; double f;
int i,r; int i, r;
for (i=0; i<1000; ++i) { for (i = 0; i < 1000; ++i) {
f = rng_double(); f = rng_double();
r = RAND_ROUND(f); r = RAND_ROUND(f);
CuAssertTrue(tc, f >= 0); CuAssertTrue(tc, f >= 0);
CuAssertTrue(tc, r <= (int) f+1); CuAssertTrue(tc, r <= (int ) f + 1);
CuAssertTrue(tc, r >= (int) f); CuAssertTrue(tc, r >= (int ) f);
CuAssertTrue(tc, r == (int) r); CuAssertTrue(tc, r == (int ) r);
CuAssertTrue(tc, r == RAND_ROUND(r)); CuAssertTrue(tc, r == RAND_ROUND(r));
} }
} }