Allow batching of too many students.

Limit number of batched units to 128.
This commit is contained in:
Enno Rehling 2019-06-24 21:41:04 +02:00
parent 6e64d749b0
commit 38ff25042b
3 changed files with 49 additions and 5 deletions

View File

@ -1,5 +1,6 @@
#include <platform.h> #include <platform.h>
#include "kernel/config.h"
#include "kernel/faction.h" #include "kernel/faction.h"
#include "kernel/messages.h" #include "kernel/messages.h"
#include "kernel/order.h" #include "kernel/order.h"
@ -46,8 +47,10 @@ int autostudy_init(scholar scholars[], int max_scholars, unit **units)
st->level = (short)effskill_study(u, sk); st->level = (short)effskill_study(u, sk);
st->learn = 0; st->learn = 0;
st->u = u; st->u = u;
if (++nscholars > max_scholars) { if (++nscholars >= max_scholars) {
log_fatal("you must increase MAXSCHOLARS"); log_warning("you must increase MAXSCHOLARS");
*units = u->next;
return max_scholars;
} }
} }
} }
@ -161,15 +164,19 @@ void autostudy_run(scholar scholars[], int nscholars)
} }
} }
#define MAXSCHOLARS 512
void do_autostudy(region *r) void do_autostudy(region *r)
{ {
static int max_scholars; static int max_scholars;
unit *units = r->units; unit *units = r->units;
scholar scholars[MAXSCHOLARS]; scholar scholars[MAXSCHOLARS];
static int config;
static int batchsize = MAXSCHOLARS;
if (config_changed(&config)) {
batchsize = config_get_int("automate.batchsize", MAXSCHOLARS);
assert(batchsize <= MAXSCHOLARS);
}
while (units) { while (units) {
int i, nscholars = autostudy_init(scholars, MAXSCHOLARS, &units); int i, nscholars = autostudy_init(scholars, batchsize, &units);
if (nscholars > max_scholars) { if (nscholars > max_scholars) {
stats_count("automate.max_scholars", nscholars - max_scholars); stats_count("automate.max_scholars", nscholars - max_scholars);
max_scholars = nscholars; max_scholars = nscholars;

View File

@ -33,6 +33,7 @@ typedef struct scholar {
short level; short level;
} scholar; } scholar;
#define MAXSCHOLARS 128
#define STUDENTS_PER_TEACHER 10 #define STUDENTS_PER_TEACHER 10
void do_autostudy(struct region *r); void do_autostudy(struct region *r);

View File

@ -4,6 +4,7 @@
#include "automate.h" #include "automate.h"
#include "kernel/config.h"
#include "kernel/faction.h" #include "kernel/faction.h"
#include "kernel/order.h" #include "kernel/order.h"
#include "kernel/region.h" #include "kernel/region.h"
@ -231,9 +232,44 @@ static void test_autostudy_run_skilldiff(CuTest *tc) {
test_teardown(); test_teardown();
} }
static void test_do_autostudy(CuTest *tc) {
scholar scholars[2];
int nscholars;
unit *u1, *u2, *u3, *ulist;
faction *f;
region *r;
test_setup();
r = test_create_plain(0, 0);
f = test_create_faction(NULL);
u1 = test_create_unit(f, r);
u1->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
set_number(u1, 1);
set_level(u1, SK_PERCEPTION, 2);
u2 = test_create_unit(f, r);
u2->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
set_number(u2, 10);
u3 = test_create_unit(f, r);
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
set_number(u3, 10);
scholars[1].u = NULL;
ulist = r->units;
config_set("automate.batchsize", "2");
CuAssertIntEquals(tc, 2, nscholars = autostudy_init(scholars, 2, &ulist));
CuAssertPtrEquals(tc, u3, ulist);
autostudy_run(scholars, nscholars);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 20, scholars[1].learn);
CuAssertIntEquals(tc, 1, nscholars = autostudy_init(scholars, 2, &ulist));
autostudy_run(scholars, nscholars);
CuAssertIntEquals(tc, 10, scholars[0].learn);
test_teardown();
}
CuSuite *get_automate_suite(void) CuSuite *get_automate_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_do_autostudy);
SUITE_ADD_TEST(suite, test_autostudy_init); SUITE_ADD_TEST(suite, test_autostudy_init);
SUITE_ADD_TEST(suite, test_autostudy_run); SUITE_ADD_TEST(suite, test_autostudy_run);
SUITE_ADD_TEST(suite, test_autostudy_run_noteachers); SUITE_ADD_TEST(suite, test_autostudy_run_noteachers);