diff --git a/src/study.c b/src/study.c index 8bfc4f491..5b6007803 100644 --- a/src/study.c +++ b/src/study.c @@ -205,7 +205,7 @@ teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk, n = _min(n, nteaching); - if (n != 0) { + if (n != 0 && teacher->building && student->building) { const struct building_type *btype = bt_find("academy"); int index = 0; diff --git a/src/study.test.c b/src/study.test.c index bdbfb9a01..dac59fb44 100644 --- a/src/study.test.c +++ b/src/study.test.c @@ -5,8 +5,11 @@ #include #include #include +#include #include #include +#include +#include #include #include #include @@ -21,6 +24,15 @@ typedef struct { unit *teachers[2]; } study_fixture; +static void setup_locale(struct locale *lang) { + int i; + for (i = 0; i < MAXSKILLS; ++i) { + if (!locale_getstring(lang, mkname("skill", skillnames[i]))) + locale_setstring(lang, mkname("skill", skillnames[i]), skillnames[i]); + } + init_skills(lang); +} + static void setup_study(study_fixture *fix, skill_t sk) { struct region * r; struct faction *f; @@ -33,8 +45,7 @@ static void setup_study(study_fixture *fix, skill_t sk) { r = findregion(0, 0); f = test_create_faction(0); lang = get_or_create_locale(locale_name(f->locale)); - locale_setstring(lang, mkname("skill", skillnames[sk]), skillnames[sk]); - init_skills(lang); + setup_locale(lang); fix->u = test_create_unit(f, r); assert(fix->u); fix->u->thisorder = create_order(K_STUDY, f->locale, "%s", skillnames[sk]); @@ -80,12 +91,10 @@ static void test_study_with_teacher(CuTest *tc) { static void test_study_with_bad_teacher(CuTest *tc) { study_fixture fix; skill *sv; - message *msg; setup_study(&fix, SK_CROSSBOW); teach_cmd(fix.teachers[0], fix.teachers[0]->thisorder); - CuAssertPtrNotNull(tc, msg = test_get_last_message(fix.u->faction->msgs)); - CuAssertStrEquals(tc, "teach_asgood", test_get_messagetype(msg)); + CuAssertPtrNotNull(tc, test_find_messagetype(fix.u->faction->msgs, "teach_asgood")); study_cmd(fix.u, fix.u->thisorder); CuAssertPtrNotNull(tc, sv = unit_skill(fix.u, SK_CROSSBOW)); CuAssertIntEquals(tc, 1, sv->level); @@ -93,11 +102,46 @@ static void test_study_with_bad_teacher(CuTest *tc) { test_cleanup(); } +static void test_study_bug_2194(CuTest *tc) { + unit *u, *u1, *u2; + struct locale * loc; + building * b; + + test_cleanup(); + random_source_inject_constant(0.0); + init_resources(); + loc = get_or_create_locale("de"); + setup_locale(loc); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + scale_number(u, 2); + set_level(u, SK_CROSSBOW, TEACHDIFFERENCE); + u->faction->locale = loc; + u1 = test_create_unit(u->faction, u->region); + scale_number(u1, 17); + u1->thisorder = create_order(K_STUDY, loc, skillnames[SK_CROSSBOW]); + u2 = test_create_unit(u->faction, u->region); + scale_number(u2, 3); + u2->thisorder = create_order(K_STUDY, loc, skillnames[SK_MAGIC]); + u->thisorder = create_order(K_TEACH, loc, "%s %s", itoa36(u1->no), itoa36(u2->no)); + b = test_create_building(u->region, test_create_buildingtype("academy")); + b->size = 22; + u_set_building(u, b); + u_set_building(u1, b); + u_set_building(u2, b); + i_change(&u1->items, get_resourcetype(R_SILVER)->itype, 50); + i_change(&u2->items, get_resourcetype(R_SILVER)->itype, 50); + b->flags = BLD_WORKING; + teach_cmd(u, u->thisorder); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "teach_asgood")); + test_cleanup(); +} + CuSuite *get_study_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_study_no_teacher); SUITE_ADD_TEST(suite, test_study_with_teacher); SUITE_ADD_TEST(suite, test_study_with_bad_teacher); + SUITE_ADD_TEST(suite, test_study_bug_2194); return suite; }