From 1a10c0271d8eb7fcfdc63b668dc01cb4e3616bd5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 4 Nov 2018 09:08:50 +0100 Subject: [PATCH] Bug 2511: LERNE AUTO fixed, student limit increased --- src/automate.c | 53 ++++++++++++++++++++++++++++++--------------- src/automate.h | 2 +- src/automate.test.c | 29 +++++++++++++++++++------ src/kernel/skills.h | 2 +- 4 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/automate.c b/src/automate.c index 9a3516c44..e4d7d60ad 100644 --- a/src/automate.c +++ b/src/automate.c @@ -28,33 +28,42 @@ static int cmp_scholars(const void *lhs, const void *rhs) return (int)a->sk - (int)b->sk; } -int autostudy_init(scholar scholars[], int max_scholars, region *r) +int autostudy_init(scholar scholars[], int max_scholars, unit **units) { - unit *u; + unit *unext = NULL, *u = *units; + faction *f = u->faction; int nscholars = 0; - for (u = r->units; u; u = u->next) { + while (u) { keyword_t kwd = init_order(u->thisorder, u->faction->locale); if (kwd == K_AUTOSTUDY) { if (long_order_allowed(u)) { - scholar * st = scholars + nscholars; - skill_t sk = getskill(u->faction->locale); - if (check_student(u, u->thisorder, sk)) { - st->sk = sk; - st->level = effskill_study(u, st->sk); - st->learn = 0; - st->u = u; - if (++nscholars == max_scholars) { - log_fatal("you must increase MAXSCHOLARS"); + if (f == u->faction) { + scholar * st = scholars + nscholars; + skill_t sk = getskill(u->faction->locale); + if (check_student(u, u->thisorder, sk)) { + st->sk = sk; + st->level = effskill_study(u, st->sk); + st->learn = 0; + st->u = u; + if (++nscholars == max_scholars) { + log_fatal("you must increase MAXSCHOLARS"); + } } } + else if (!unext) { + unext = u; + } } else { ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "error_race_nolearn", "race", u_race(u))); } } + u = u->next; } + *units = unext; + scholars[nscholars].u = NULL; if (nscholars > 0) { qsort(scholars, nscholars, sizeof(scholar), cmp_scholars); } @@ -149,15 +158,23 @@ void autostudy_run(scholar scholars[], int nscholars) } } -#define MAXSCHOLARS 128 +#define MAXSCHOLARS 512 void do_autostudy(region *r) { + static int max_scholars; + unit *units = r->units; scholar scholars[MAXSCHOLARS]; - int i, nscholars = autostudy_init(scholars, MAXSCHOLARS, r); - autostudy_run(scholars, nscholars); - for (i = 0; i != nscholars; ++i) { - int days = STUDYDAYS * scholars[i].learn; - learn_skill(scholars[i].u, scholars[i].sk, days); + while (units) { + int i, nscholars = autostudy_init(scholars, MAXSCHOLARS, &units); + if (nscholars > max_scholars) { + stats_count("automate.max_scholars", nscholars - max_scholars); + max_scholars = nscholars; + } + autostudy_run(scholars, nscholars); + for (i = 0; i != nscholars; ++i) { + int days = STUDYDAYS * scholars[i].learn; + learn_skill(scholars[i].u, scholars[i].sk, days); + } } } diff --git a/src/automate.h b/src/automate.h index 61fed6866..314791b8f 100644 --- a/src/automate.h +++ b/src/automate.h @@ -37,7 +37,7 @@ typedef struct scholar { void do_autostudy(struct region *r); -int autostudy_init(scholar scholars[], int max_scholars, struct region *r); +int autostudy_init(scholar scholars[], int max_scholars, struct unit **units); void autostudy_run(scholar scholars[], int nscholars); #endif diff --git a/src/automate.test.c b/src/automate.test.c index 6c87b8656..0caa70f3b 100644 --- a/src/automate.test.c +++ b/src/automate.test.c @@ -17,7 +17,7 @@ static void test_autostudy_init(CuTest *tc) { scholar scholars[4]; - unit *u1, *u2, *u3, *u4; + unit *u1, *u2, *u3, *u4, *u5, *ulist; faction *f; region *r; @@ -35,10 +35,13 @@ static void test_autostudy_init(CuTest *tc) { set_level(u2, SK_ENTERTAINMENT, 2); u3 = test_create_unit(f, r); u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]); - u4 = test_create_unit(test_create_faction(NULL), r); + u4 = test_create_unit(f, r); u4->thisorder = create_order(K_AUTOSTUDY, f->locale, "Dudelidu"); + u5 = test_create_unit(test_create_faction(NULL), r); + u5->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]); scholars[3].u = NULL; - CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r)); + ulist = r->units; + CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, &ulist)); CuAssertPtrNotNull(tc, test_find_messagetype(u4->faction->msgs, "error77")); CuAssertPtrEquals(tc, u2, scholars[0].u); CuAssertIntEquals(tc, 2, scholars[0].level); @@ -53,12 +56,20 @@ static void test_autostudy_init(CuTest *tc) { CuAssertIntEquals(tc, 0, scholars[2].learn); CuAssertIntEquals(tc, SK_PERCEPTION, scholars[2].sk); CuAssertPtrEquals(tc, NULL, scholars[3].u); + CuAssertPtrEquals(tc, u5, ulist); + CuAssertIntEquals(tc, 1, autostudy_init(scholars, 4, &ulist)); + CuAssertPtrEquals(tc, u5, scholars[0].u); + CuAssertIntEquals(tc, 0, scholars[0].level); + CuAssertIntEquals(tc, 0, scholars[0].learn); + CuAssertIntEquals(tc, SK_PERCEPTION, scholars[0].sk); + CuAssertPtrEquals(tc, NULL, scholars[1].u); + CuAssertPtrEquals(tc, NULL, ulist); test_teardown(); } static void test_autostudy_run(CuTest *tc) { scholar scholars[4]; - unit *u1, *u2, *u3; + unit *u1, *u2, *u3, *ulist; faction *f; region *r; @@ -76,7 +87,9 @@ static void test_autostudy_run(CuTest *tc) { u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]); set_number(u3, 15); scholars[3].u = NULL; - CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r)); + ulist = r->units; + CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, &ulist)); + CuAssertPtrEquals(tc, NULL, ulist); autostudy_run(scholars, 3); CuAssertIntEquals(tc, 0, scholars[0].learn); CuAssertIntEquals(tc, 20, scholars[1].learn); @@ -86,7 +99,7 @@ static void test_autostudy_run(CuTest *tc) { static void test_autostudy_run_noteachers(CuTest *tc) { scholar scholars[4]; - unit *u1, *u2, *u3; + unit *u1, *u2, *u3, *ulist; faction *f; region *r; @@ -104,7 +117,9 @@ static void test_autostudy_run_noteachers(CuTest *tc) { u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]); set_number(u3, 15); scholars[3].u = NULL; - CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r)); + ulist = r->units; + CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, &ulist)); + CuAssertPtrEquals(tc, NULL, ulist); autostudy_run(scholars, 3); CuAssertIntEquals(tc, 2, scholars[0].learn); CuAssertIntEquals(tc, 10, scholars[1].learn); diff --git a/src/kernel/skills.h b/src/kernel/skills.h index 94576295c..aa3c62339 100644 --- a/src/kernel/skills.h +++ b/src/kernel/skills.h @@ -26,7 +26,7 @@ extern "C" { #endif typedef struct skill { - skill_t id : 8; + int id : 8; int level : 8; int weeks : 8; int old : 8;