autostudy continued (WIP)

This commit is contained in:
Enno Rehling 2018-07-09 03:31:13 +02:00 committed by Enno Rehling
parent 0e754a31ac
commit c8ebde3990
6 changed files with 170 additions and 98 deletions

View File

@ -33,13 +33,13 @@ void academy_teaching_bonus(struct unit *u, skill_t sk, int students) {
} }
} }
bool academy_can_teach(unit *teacher, unit *student, skill_t sk) { bool academy_can_teach(unit *teacher, unit *scholar, skill_t sk) {
const struct building_type *btype = bt_find("academy"); const struct building_type *btype = bt_find("academy");
if (active_building(teacher, btype) && active_building(student, btype)) { if (active_building(teacher, btype) && active_building(scholar, btype)) {
int j = study_cost(student, sk) * 2; int j = study_cost(scholar, sk) * 2;
if (j < 50) j = 50; if (j < 50) j = 50;
/* kann Einheit das zahlen? */ /* kann Einheit das zahlen? */
return get_pooled(student, get_resourcetype(R_SILVER), GET_DEFAULT, j) >= j; return get_pooled(scholar, get_resourcetype(R_SILVER), GET_DEFAULT, j) >= j;
/* sonst nehmen sie nicht am Unterricht teil */ /* sonst nehmen sie nicht am Unterricht teil */
} }
return false; return false;

View File

@ -9,7 +9,7 @@ extern "C" {
struct unit; struct unit;
void academy_teaching_bonus(struct unit *u, skill_t sk, int academy); void academy_teaching_bonus(struct unit *u, skill_t sk, int academy);
bool academy_can_teach(struct unit *teacher, struct unit *student, skill_t sk); bool academy_can_teach(struct unit *teacher, struct unit *scholar, skill_t sk);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -13,9 +13,10 @@
#include <stdlib.h> #include <stdlib.h>
int cmp_students(const void *lhs, const void *rhs) { int cmp_scholars(const void *lhs, const void *rhs)
const student *a = (const student *)lhs; {
const student *b = (const student *)rhs; const scholar *a = (const scholar *)lhs;
const scholar *b = (const scholar *)rhs;
if (a->sk == b->sk) { if (a->sk == b->sk) {
/* sort by level, descending: */ /* sort by level, descending: */
return b->level - a->level; return b->level - a->level;
@ -24,17 +25,17 @@ int cmp_students(const void *lhs, const void *rhs) {
return (int)a->sk - (int)b->sk; return (int)a->sk - (int)b->sk;
} }
int autostudy_init(student students[], int max_students, region *r) int autostudy_init(scholar scholars[], int max_scholars, region *r)
{ {
unit *u; unit *u;
int nstudents = 0; int nscholars = 0;
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
keyword_t kwd = getkeyword(u->thisorder); keyword_t kwd = getkeyword(u->thisorder);
if (kwd == K_AUTOSTUDY) { if (kwd == K_AUTOSTUDY) {
student * st = students + nstudents; scholar * st = scholars + nscholars;
if (++nstudents == max_students) { if (++nscholars == max_scholars) {
log_fatal("you must increase MAXSTUDENTS"); log_fatal("you must increase MAXSCHOLARS");
} }
st->u = u; st->u = u;
init_order(u->thisorder, u->faction->locale); init_order(u->thisorder, u->faction->locale);
@ -43,31 +44,73 @@ int autostudy_init(student students[], int max_students, region *r)
st->learn = 0; st->learn = 0;
} }
} }
if (nstudents > 0) { if (nscholars > 0) {
qsort(students, nstudents, sizeof(student), cmp_students); qsort(scholars, nscholars, sizeof(scholar), cmp_scholars);
} }
return nstudents; return nscholars;
} }
#define MAXSTUDENTS 128 void autostudy_run(scholar scholars[], int nscholars)
{
int i, t, s, ti = 0, si = 0, ts = 0, tt = 0;
skill_t sk = scholars[0].sk;
for (i = ti; i != nscholars && scholars[i].sk == sk; ++i) {
int mint;
ts += scholars[i].u->number; /* count total scholars */
mint = (ts + 10) / 11; /* need a minimum of ceil(ts/11) teachers */
while (mint>tt) {
tt += scholars[si++].u->number;
}
}
/* now si splits the teachers and students 1:10 */
/* first student must be 2 levels below first teacher: */
while (scholars[ti].level - TEACHDIFFERENCE > scholars[si].level) {
tt += scholars[si++].u->number;
}
/* invariant: unit ti can still teach i students */
i = scholars[ti].u->number * 10;
for (t = ti, s = si; t != si && s != nscholars; ++t) {
/* TODO: is there no constant for students per teacher? */
while (s != nscholars) {
int n = scholars[s].u->number;
scholars[s].learn += n;
if (i >= n) {
i -= n;
scholars[s].learn += n;
/* next student */
}
else {
scholars[s].learn += i;
/* go to next suitable teacher */
do {
++t;
}
while (scholars[t].level - TEACHDIFFERENCE < scholars[s].level);
}
}
}
}
void do_autostudy(region *r) { #define MAXSCHOLARS 128
student students[MAXSTUDENTS];
int nstudents = autostudy_init(students, MAXSTUDENTS, r);
if (nstudents > 0) { void do_autostudy(region *r)
{
scholar scholars[MAXSCHOLARS];
int nscholars = autostudy_init(scholars, MAXSCHOLARS, r);
if (nscholars > 0) {
int i; int i;
skill_t sk = NOSKILL; skill_t sk = NOSKILL;
for (i = 0; i != nstudents; ++i) { for (i = 0; i != nscholars; ++i) {
if (students[i].u) { if (scholars[i].u) {
if (sk == NOSKILL) { if (sk == NOSKILL) {
sk = students[i].sk; sk = scholars[i].sk;
} }
else if (sk != students[i].sk) { else if (sk != scholars[i].sk) {
continue; continue;
} }
students[i].u = NULL; scholars[i].u = NULL;
} }
} }
} }

View File

@ -26,15 +26,16 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
struct region; struct region;
struct unit; struct unit;
typedef struct student { typedef struct scholar {
struct unit *u; struct unit *u;
skill_t sk; skill_t sk;
int level; int level;
int learn; int learn;
} student; } scholar;
void do_autostudy(struct region *r); void do_autostudy(struct region *r);
int autostudy_init(student students[], int max_students, struct region *r); int autostudy_init(scholar scholars[], int max_scholars, struct region *r);
void autostudy_run(scholar scholars[], int nscholars);
#endif #endif

View File

@ -13,8 +13,8 @@
#include <CuTest.h> #include <CuTest.h>
static void test_autostudy(CuTest *tc) { static void test_autostudy_init(CuTest *tc) {
student students[4]; scholar scholars[4];
unit *u1, *u2, *u3; unit *u1, *u2, *u3;
faction *f; faction *f;
region *r; region *r;
@ -30,27 +30,55 @@ static void test_autostudy(CuTest *tc) {
set_level(u2, SK_ENTERTAINMENT, 2); set_level(u2, SK_ENTERTAINMENT, 2);
u3 = test_create_unit(f, r); u3 = test_create_unit(f, r);
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]); u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
students[3].u = NULL; scholars[3].u = NULL;
CuAssertIntEquals(tc, 3, autostudy_init(students, 4, r)); CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r));
CuAssertPtrEquals(tc, u2, students[0].u); CuAssertPtrEquals(tc, u2, scholars[0].u);
CuAssertIntEquals(tc, 2, students[0].level); CuAssertIntEquals(tc, 2, scholars[0].level);
CuAssertIntEquals(tc, 0, students[0].learn); CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, SK_ENTERTAINMENT, students[0].sk); CuAssertIntEquals(tc, SK_ENTERTAINMENT, scholars[0].sk);
CuAssertPtrEquals(tc, u1, students[1].u); CuAssertPtrEquals(tc, u1, scholars[1].u);
CuAssertIntEquals(tc, 0, students[1].level); CuAssertIntEquals(tc, 0, scholars[1].level);
CuAssertIntEquals(tc, 0, students[1].learn); CuAssertIntEquals(tc, 0, scholars[1].learn);
CuAssertIntEquals(tc, SK_ENTERTAINMENT, students[1].sk); CuAssertIntEquals(tc, SK_ENTERTAINMENT, scholars[1].sk);
CuAssertPtrEquals(tc, u3, students[2].u); CuAssertPtrEquals(tc, u3, scholars[2].u);
CuAssertIntEquals(tc, 0, students[2].level); CuAssertIntEquals(tc, 0, scholars[2].level);
CuAssertIntEquals(tc, 0, students[2].learn); CuAssertIntEquals(tc, 0, scholars[2].learn);
CuAssertIntEquals(tc, SK_PERCEPTION, students[2].sk); CuAssertIntEquals(tc, SK_PERCEPTION, scholars[2].sk);
CuAssertPtrEquals(tc, NULL, students[3].u); CuAssertPtrEquals(tc, NULL, scholars[3].u);
test_teardown(); test_teardown();
} }
static void test_autostudy_run(CuTest *tc) {
scholar scholars[4];
unit *u1, *u2, *u3;
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_ENTERTAINMENT]);
set_number(u1, 2);
u2 = test_create_unit(f, r);
u2->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]);
set_number(u2, 10);
set_level(u2, SK_ENTERTAINMENT, 2);
u3 = test_create_unit(f, r);
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
set_number(u3, 20);
scholars[3].u = NULL;
CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r));
autostudy_run(scholars, 3);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 20, scholars[1].learn);
CuAssertIntEquals(tc, 30, scholars[2].learn);
}
CuSuite *get_automate_suite(void) CuSuite *get_automate_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_autostudy); SUITE_ADD_TEST(suite, test_autostudy_init);
SUITE_ADD_TEST(suite, test_autostudy_run);
return suite; return suite;
} }

View File

@ -195,37 +195,37 @@ const attrib_type at_learning = {
#define EXPERIENCEDAYS 10 #define EXPERIENCEDAYS 10
static int study_days(unit * student, skill_t sk) static int study_days(unit * scholar, skill_t sk)
{ {
int speed = STUDYDAYS; int speed = STUDYDAYS;
if (u_race(student)->study_speed) { if (u_race(scholar)->study_speed) {
speed += u_race(student)->study_speed[sk]; speed += u_race(scholar)->study_speed[sk];
if (speed < STUDYDAYS) { if (speed < STUDYDAYS) {
skill *sv = unit_skill(student, sk); skill *sv = unit_skill(scholar, sk);
if (sv == 0) { if (sv == 0) {
speed = STUDYDAYS; speed = STUDYDAYS;
} }
} }
} }
return student->number * speed; return scholar->number * speed;
} }
static int static int
teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk, teach_unit(unit * teacher, unit * scholar, int nteaching, skill_t sk,
bool report, int *academy_students) bool report, int *academy_students)
{ {
teaching_info *teach = NULL; teaching_info *teach = NULL;
attrib *a; attrib *a;
int students; int students;
if (magic_lowskill(student)) { if (magic_lowskill(scholar)) {
cmistake(teacher, teacher->thisorder, 292, MSG_EVENT); cmistake(teacher, teacher->thisorder, 292, MSG_EVENT);
return 0; return 0;
} }
students = student->number; students = scholar->number;
/* subtract already taught students */ /* subtract already taught students */
a = a_find(student->attribs, &at_learning); a = a_find(scholar->attribs, &at_learning);
if (a != NULL) { if (a != NULL) {
teach = (teaching_info *)a->data.v; teach = (teaching_info *)a->data.v;
students -= teach->students; students -= teach->students;
@ -235,18 +235,18 @@ teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk,
if (students > 0) { if (students > 0) {
if (teach == NULL) { if (teach == NULL) {
a = a_add(&student->attribs, a_new(&at_learning)); a = a_add(&scholar->attribs, a_new(&at_learning));
teach = (teaching_info *)a->data.v; teach = (teaching_info *)a->data.v;
} }
selist_push(&teach->teachers, teacher); selist_push(&teach->teachers, teacher);
teach->days += students * STUDYDAYS; teach->days += students * STUDYDAYS;
teach->students += students; teach->students += students;
if (student->building && teacher->building == student->building) { if (scholar->building && teacher->building == scholar->building) {
/* Solange Akademien groessenbeschraenkt sind, sollte Lehrer und /* Solange Akademien groessenbeschraenkt sind, sollte Lehrer und
* Student auch in unterschiedlichen Gebaeuden stehen duerfen */ * Student auch in unterschiedlichen Gebaeuden stehen duerfen */
/* FIXME comment contradicts implementation */ /* FIXME comment contradicts implementation */
if (academy_can_teach(teacher, student, sk)) { if (academy_can_teach(teacher, scholar, sk)) {
/* Jeder Schueler zusaetzlich +10 Tage wenn in Uni. */ /* Jeder Schueler zusaetzlich +10 Tage wenn in Uni. */
teach->days += students * EXPERIENCEDAYS; /* learning erhoehen */ teach->days += students * EXPERIENCEDAYS; /* learning erhoehen */
/* Lehrer zusaetzlich +1 Tag pro Schueler. */ /* Lehrer zusaetzlich +1 Tag pro Schueler. */
@ -304,7 +304,7 @@ int teach_cmd(unit * teacher, struct order *ord)
#if TEACH_ALL #if TEACH_ALL
if (getparam(teacher->faction->locale) == P_ANY) { if (getparam(teacher->faction->locale) == P_ANY) {
skill_t sk; skill_t sk;
unit *student; unit *scholar;
skill_t teachskill[MAXSKILLS]; skill_t teachskill[MAXSKILLS];
int t = 0; int t = 0;
@ -313,15 +313,15 @@ int teach_cmd(unit * teacher, struct order *ord)
teachskill[t] = getskill(teacher->faction->locale); teachskill[t] = getskill(teacher->faction->locale);
} while (sk != NOSKILL); } while (sk != NOSKILL);
for (student = r->units; teaching > 0 && student; student = student->next) { for (scholar = r->units; teaching > 0 && scholar; scholar = scholar->next) {
if (LongHunger(student)) { if (LongHunger(scholar)) {
continue; continue;
} }
else if (student->faction == teacher->faction) { else if (scholar->faction == teacher->faction) {
if (getkeyword(student->thisorder) == K_STUDY) { if (getkeyword(scholar->thisorder) == K_STUDY) {
/* Input ist nun von student->thisorder !! */ /* Input ist nun von student->thisorder !! */
init_order(student->thisorder, student->faction->locale); init_order(scholar->thisorder, scholar->faction->locale);
sk = getskill(student->faction->locale); sk = getskill(scholar->faction->locale);
if (sk != NOSKILL && teachskill[0] != NOSKILL) { if (sk != NOSKILL && teachskill[0] != NOSKILL) {
for (t = 0; teachskill[t] != NOSKILL; ++t) { for (t = 0; teachskill[t] != NOSKILL; ++t) {
if (sk == teachskill[t]) { if (sk == teachskill[t]) {
@ -331,20 +331,20 @@ int teach_cmd(unit * teacher, struct order *ord)
sk = teachskill[t]; sk = teachskill[t];
} }
if (sk != NOSKILL if (sk != NOSKILL
&& effskill_study(teacher, sk) - TEACHDIFFERENCE > effskill_study(student, sk)) { && effskill_study(teacher, sk) - TEACHDIFFERENCE > effskill_study(scholar, sk)) {
teaching -= teach_unit(teacher, student, teaching, sk, true, &academy_students); teaching -= teach_unit(teacher, scholar, teaching, sk, true, &academy_students);
} }
} }
} }
#ifdef TEACH_FRIENDS #ifdef TEACH_FRIENDS
else if (alliedunit(teacher, student->faction, HELP_GUARD)) { else if (alliedunit(teacher, scholar->faction, HELP_GUARD)) {
if (getkeyword(student->thisorder) == K_STUDY) { if (getkeyword(scholar->thisorder) == K_STUDY) {
/* Input ist nun von student->thisorder !! */ /* Input ist nun von student->thisorder !! */
init_order(student->thisorder, student->faction->locale); init_order(scholar->thisorder, scholar->faction->locale);
sk = getskill(student->faction->locale); sk = getskill(scholar->faction->locale);
if (sk != NOSKILL if (sk != NOSKILL
&& effskill_study(teacher, sk) - TEACHDIFFERENCE >= effskill(student, sk, NULL)) { && effskill_study(teacher, sk) - TEACHDIFFERENCE >= effskill(scholar, sk, NULL)) {
teaching -= teach_unit(teacher, student, teaching, sk, true, &academy_students); teaching -= teach_unit(teacher, scholar, teaching, sk, true, &academy_students);
} }
} }
} }
@ -363,15 +363,15 @@ int teach_cmd(unit * teacher, struct order *ord)
while (!parser_end()) { while (!parser_end()) {
skill_t sk; skill_t sk;
unit *student; unit *scholar;
bool feedback; bool feedback;
getunit(r, teacher->faction, &student); getunit(r, teacher->faction, &scholar);
++count; ++count;
/* Falls die Unit nicht gefunden wird, Fehler melden */ /* Falls die Unit nicht gefunden wird, Fehler melden */
if (!student) { if (!scholar) {
char tbuf[20]; char tbuf[20];
const char *uid; const char *uid;
const char *token; const char *token;
@ -403,8 +403,8 @@ int teach_cmd(unit * teacher, struct order *ord)
continue; continue;
} }
feedback = teacher->faction == student->faction feedback = teacher->faction == scholar->faction
|| alliedunit(student, teacher->faction, HELP_GUARD); || alliedunit(scholar, teacher->faction, HELP_GUARD);
/* Neuen Befehl zusammenbauen. TEMP-Einheiten werden automatisch in /* Neuen Befehl zusammenbauen. TEMP-Einheiten werden automatisch in
* ihre neuen Nummern uebersetzt. */ * ihre neuen Nummern uebersetzt. */
@ -412,31 +412,31 @@ int teach_cmd(unit * teacher, struct order *ord)
strncat(zOrder, " ", sz - 1); strncat(zOrder, " ", sz - 1);
--sz; --sz;
} }
sz -= str_strlcpy(zOrder + 4096 - sz, itoa36(student->no), sz); sz -= str_strlcpy(zOrder + 4096 - sz, itoa36(scholar->no), sz);
if (getkeyword(student->thisorder) != K_STUDY) { if (getkeyword(scholar->thisorder) != K_STUDY) {
ADDMSG(&teacher->faction->msgs, ADDMSG(&teacher->faction->msgs,
msg_feedback(teacher, ord, "teach_nolearn", "student", student)); msg_feedback(teacher, ord, "teach_nolearn", "student", scholar));
continue; continue;
} }
/* Input ist nun von student->thisorder !! */ /* Input ist nun von student->thisorder !! */
parser_pushstate(); parser_pushstate();
init_order(student->thisorder, student->faction->locale); init_order(scholar->thisorder, scholar->faction->locale);
sk = getskill(student->faction->locale); sk = getskill(scholar->faction->locale);
parser_popstate(); parser_popstate();
if (sk == NOSKILL) { if (sk == NOSKILL) {
ADDMSG(&teacher->faction->msgs, ADDMSG(&teacher->faction->msgs,
msg_feedback(teacher, ord, "teach_nolearn", "student", student)); msg_feedback(teacher, ord, "teach_nolearn", "student", scholar));
continue; continue;
} }
if (effskill_study(student, sk) > effskill_study(teacher, sk) if (effskill_study(scholar, sk) > effskill_study(teacher, sk)
- TEACHDIFFERENCE) { - TEACHDIFFERENCE) {
if (feedback) { if (feedback) {
ADDMSG(&teacher->faction->msgs, msg_feedback(teacher, ord, "teach_asgood", ADDMSG(&teacher->faction->msgs, msg_feedback(teacher, ord, "teach_asgood",
"student", student)); "student", scholar));
} }
continue; continue;
} }
@ -444,18 +444,18 @@ int teach_cmd(unit * teacher, struct order *ord)
/* ist der Magier schon spezialisiert, so versteht er nur noch /* ist der Magier schon spezialisiert, so versteht er nur noch
* Lehrer seines Gebietes */ * Lehrer seines Gebietes */
sc_mage *mage1 = get_mage_depr(teacher); sc_mage *mage1 = get_mage_depr(teacher);
sc_mage *mage2 = get_mage_depr(student); sc_mage *mage2 = get_mage_depr(scholar);
if (mage2 && mage1 && mage2->magietyp != M_GRAY if (mage2 && mage1 && mage2->magietyp != M_GRAY
&& mage1->magietyp != mage2->magietyp) { && mage1->magietyp != mage2->magietyp) {
if (feedback) { if (feedback) {
ADDMSG(&teacher->faction->msgs, msg_feedback(teacher, ord, ADDMSG(&teacher->faction->msgs, msg_feedback(teacher, ord,
"error_different_magic", "target", student)); "error_different_magic", "target", scholar));
} }
continue; continue;
} }
} }
sk_academy = sk; sk_academy = sk;
teaching -= teach_unit(teacher, student, teaching, sk, false, &academy_students); teaching -= teach_unit(teacher, scholar, teaching, sk, false, &academy_students);
} }
new_order = create_order(K_TEACH, teacher->faction->locale, "%s", zOrder); new_order = create_order(K_TEACH, teacher->faction->locale, "%s", zOrder);
replace_order(&teacher->orders, ord, new_order); replace_order(&teacher->orders, ord, new_order);