diff --git a/src/automate.c b/src/automate.c index 9f4d85b6c..0764381f2 100644 --- a/src/automate.c +++ b/src/automate.c @@ -1,5 +1,6 @@ #include +#include "kernel/config.h" #include "kernel/faction.h" #include "kernel/messages.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->learn = 0; st->u = u; - if (++nscholars > max_scholars) { - log_fatal("you must increase MAXSCHOLARS"); + if (++nscholars >= max_scholars) { + 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) { static int max_scholars; unit *units = r->units; 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) { - int i, nscholars = autostudy_init(scholars, MAXSCHOLARS, &units); + int i, nscholars = autostudy_init(scholars, batchsize, &units); if (nscholars > max_scholars) { stats_count("automate.max_scholars", nscholars - max_scholars); max_scholars = nscholars; diff --git a/src/automate.h b/src/automate.h index 1a6e9638a..1a7738363 100644 --- a/src/automate.h +++ b/src/automate.h @@ -33,6 +33,7 @@ typedef struct scholar { short level; } scholar; +#define MAXSCHOLARS 128 #define STUDENTS_PER_TEACHER 10 void do_autostudy(struct region *r); diff --git a/src/automate.test.c b/src/automate.test.c index 732ee46a9..d4e5650e6 100644 --- a/src/automate.test.c +++ b/src/automate.test.c @@ -4,6 +4,7 @@ #include "automate.h" +#include "kernel/config.h" #include "kernel/faction.h" #include "kernel/order.h" #include "kernel/region.h" @@ -231,9 +232,44 @@ static void test_autostudy_run_skilldiff(CuTest *tc) { 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 *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_do_autostudy); SUITE_ADD_TEST(suite, test_autostudy_init); SUITE_ADD_TEST(suite, test_autostudy_run); SUITE_ADD_TEST(suite, test_autostudy_run_noteachers);