Merge pull request #893 from ennorehling/develop

3.23 RC1
This commit is contained in:
Enno Rehling 2020-03-01 18:33:52 +01:00 committed by GitHub
commit b194ed39bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 18 deletions

View File

@ -37,10 +37,15 @@ int u_geteffstealth(const unit *u)
if (skill_enabled(SK_STEALTH)) { if (skill_enabled(SK_STEALTH)) {
if (u->flags & UFL_STEALTH) { if (u->flags & UFL_STEALTH) {
attrib *a = a_find(u->attribs, &at_stealth); attrib *a = a_find(u->attribs, &at_stealth);
if (a != NULL) if (a != NULL) {
int eff = effskill(u, SK_STEALTH, u->region);
if (eff < a->data.i) {
return eff;
}
return a->data.i; return a->data.i;
} }
} }
}
return -1; return -1;
} }

View File

@ -20,7 +20,8 @@
static int cmp_scholars(const void *lhs, const void *rhs) { static int cmp_scholars(const void *lhs, const void *rhs) {
const scholar *a = (const scholar *)lhs; const scholar *a = (const scholar *)lhs;
const scholar *b = (const scholar *)rhs; const scholar *b = (const scholar *)rhs;
return b->level - a->level; int diff = b->level - a->level;
return (diff != 0) ? diff : b->u->number - a->u->number;
} }
int autostudy_init(scholar scholars[], int max_scholars, unit **units, skill_t *o_skill) int autostudy_init(scholar scholars[], int max_scholars, unit **units, skill_t *o_skill)
@ -77,6 +78,7 @@ int autostudy_init(scholar scholars[], int max_scholars, unit **units, skill_t *
static void teaching(scholar *s, int n) { static void teaching(scholar *s, int n) {
assert(n <= s->u->number); assert(n <= s->u->number);
// doppelter Effekt mit Lehrer:
s->learn += n; s->learn += n;
s->u->flags |= UFL_LONGACTION; s->u->flags |= UFL_LONGACTION;
} }
@ -91,27 +93,31 @@ void autostudy_run(scholar scholars[], int nscholars)
{ {
int ti = 0; int ti = 0;
while (ti != nscholars) { while (ti != nscholars) {
int t, se, ts = 0, tt = 0, si = ti; int t, ts = 0, tt = 0, si = ti, mint = 0, ns = 0;
for (se = ti; se != nscholars; ++se) { for (t = ti; t != nscholars; ++t) {
int mint; ts += scholars[t].u->number; /* count total scholars */
ts += scholars[se].u->number; /* count total scholars */
mint = (ts + 10) / 11; /* need a minimum of ceil(ts/11) teachers */ mint = (ts + 10) / 11; /* need a minimum of ceil(ts/11) teachers */
for (; mint > tt && si != nscholars - 1; ++si) { for (; mint > tt && si != nscholars; ++si) {
tt += scholars[si].u->number; tt += scholars[si].u->number;
} }
} }
if (mint < tt) {
/* die Einheit si-1 hat einen Mix aus Lehrer und Schüler */
--si;
ns = tt - mint;
}
/* now si splits the teachers and students 1:10 */ /* now si splits the teachers and students 1:10 */
/* first student must be 2 levels below first teacher: */ /* first student must be 2 levels below first teacher: */
for (; si != se; ++si) { for (; si != nscholars; ++si) {
if (scholars[si].level + TEACHDIFFERENCE <= scholars[ti].level) { if (scholars[si].level + TEACHDIFFERENCE <= scholars[ti].level) {
break; break;
} }
tt += scholars[si].u->number; ns = 0;
} }
/* now si is the first unit we can teach, if we can teach any */ /* now si is the first unit we can teach, if we can teach any */
if (si == se) { if (si == nscholars) {
/* there are no students, so standard learning for everyone */ /* there are no students, so standard learning for everyone */
for (t = ti; t != se; ++t) { for (t = ti; t != nscholars; ++t) {
learning(scholars + t, scholars[t].u->number); learning(scholars + t, scholars[t].u->number);
} }
} }
@ -119,16 +125,17 @@ void autostudy_run(scholar scholars[], int nscholars)
/* invariant: unit ti can still teach i students */ /* invariant: unit ti can still teach i students */
int i = scholars[ti].u->number * STUDENTS_PER_TEACHER; int i = scholars[ti].u->number * STUDENTS_PER_TEACHER;
/* invariant: unit si has n students that can still be taught */ /* invariant: unit si has n students that can still be taught */
int s, n = scholars[si].u->number; int s, n = (ns > 0) ? ns : scholars[si].u->number;
for (t = ti, s = si; t != si && s != se; ) { for (t = ti, s = si; t != si && s != nscholars; ) {
if (i >= n) { if (i >= n) {
/* t has more than enough teaching capacity for s */ /* t has more than enough teaching capacity for s */
i -= n; i -= n;
teaching(scholars + s, n); teaching(scholars + s, n);
learning(scholars + s, scholars[s].u->number); learning(scholars + s, scholars[s].u->number);
/* next student, please: */ /* next student, please: */
if (++s == se) { if (++s == nscholars) {
continue; /* no more students */
break;
} }
n = scholars[s].u->number; n = scholars[s].u->number;
} }
@ -147,7 +154,7 @@ void autostudy_run(scholar scholars[], int nscholars)
do { do {
/* remaining students learn without a teacher: */ /* remaining students learn without a teacher: */
learning(scholars + s, n); learning(scholars + s, n);
if (++s == se) { if (++s == nscholars) {
break; break;
} }
n = scholars[s].u->number; n = scholars[s].u->number;
@ -163,10 +170,15 @@ void autostudy_run(scholar scholars[], int nscholars)
} }
++t; ++t;
for (; t < si; ++t) { for (; t < si; ++t) {
/* teachers that did not teach */
learning(scholars + t, scholars[t].u->number); learning(scholars + t, scholars[t].u->number);
} }
for (; s < nscholars; ++s) {
/* students that were not taught */
learning(scholars + s, scholars[s].u->number);
} }
ti = se; }
ti = nscholars;
} }
} }

View File

@ -139,7 +139,78 @@ static void test_autostudy_run_bigunit(CuTest *tc) {
autostudy_run(scholars, nscholars); autostudy_run(scholars, nscholars);
CuAssertIntEquals(tc, SK_ENTERTAINMENT, skill); CuAssertIntEquals(tc, SK_ENTERTAINMENT, skill);
CuAssertIntEquals(tc, 0, scholars[0].learn); CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 200, scholars[1].learn); CuAssertIntEquals(tc, 1200, scholars[1].learn);
test_teardown();
}
static void test_autostudy_run_few_teachers(CuTest *tc) {
scholar scholars[4];
int nscholars;
unit *u1, *u2, *u3, *ulist;
faction *f;
region *r;
skill_t skill;
test_setup();
r = test_create_plain(0, 0);
f = test_create_faction(NULL);
u1 = test_create_unit(f, r);
set_number(u1, 20);
set_level(u1, SK_ENTERTAINMENT, 16);
u1->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u2 = test_create_unit(f, r);
set_number(u2, 500);
set_level(u2, SK_ENTERTAINMENT, 10);
u2->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u3 = test_create_unit(f, r);
set_number(u3, 100);
set_level(u3, SK_ENTERTAINMENT, 9);
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
ulist = r->units;
CuAssertIntEquals(tc, 3, nscholars = autostudy_init(scholars, 4, &ulist, &skill));
CuAssertPtrEquals(tc, NULL, ulist);
autostudy_run(scholars, nscholars);
CuAssertIntEquals(tc, SK_ENTERTAINMENT, skill);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 700, scholars[1].learn);
CuAssertIntEquals(tc, 100, scholars[2].learn);
test_teardown();
}
static void test_autostudy_run_few_teachers_reverse(CuTest *tc) {
scholar scholars[4];
int nscholars;
unit *u1, *u2, *u3, *ulist;
faction *f;
region *r;
skill_t skill;
test_setup();
r = test_create_plain(0, 0);
f = test_create_faction(NULL);
u1 = test_create_unit(f, r);
set_number(u1, 20);
set_level(u1, SK_ENTERTAINMENT, 16);
u1->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u2 = test_create_unit(f, r);
set_number(u2, 100);
set_level(u2, SK_ENTERTAINMENT, 10);
u2->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
u3 = test_create_unit(f, r);
set_number(u3, 500);
set_level(u3, SK_ENTERTAINMENT, 9);
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
ulist = r->units;
CuAssertIntEquals(tc, 3, nscholars = autostudy_init(scholars, 4, &ulist, &skill));
CuAssertPtrEquals(tc, NULL, ulist);
autostudy_run(scholars, nscholars);
CuAssertIntEquals(tc, SK_ENTERTAINMENT, skill);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 800, scholars[1].learn + scholars[2].learn);
test_teardown(); test_teardown();
} }
@ -360,6 +431,8 @@ CuSuite *get_automate_suite(void)
SUITE_ADD_TEST(suite, test_autostudy_run_teachers_learn); SUITE_ADD_TEST(suite, test_autostudy_run_teachers_learn);
SUITE_ADD_TEST(suite, test_autostudy_run_twoteachers); SUITE_ADD_TEST(suite, test_autostudy_run_twoteachers);
SUITE_ADD_TEST(suite, test_autostudy_run_bigunit); SUITE_ADD_TEST(suite, test_autostudy_run_bigunit);
SUITE_ADD_TEST(suite, test_autostudy_run_few_teachers);
SUITE_ADD_TEST(suite, test_autostudy_run_few_teachers_reverse);
SUITE_ADD_TEST(suite, test_autostudy_run_skilldiff); SUITE_ADD_TEST(suite, test_autostudy_run_skilldiff);
return suite; return suite;
} }