From b5a4faa745f344a5e9353bdf3c30c3691f3d6b2f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Mar 2016 14:52:33 +0100 Subject: [PATCH] deprecate learn_skill taking a double - introduce days-based learn_skill - add a simple test --- src/academy.c | 2 +- src/bindings.c | 4 ++-- src/items/xerewards.c | 2 +- src/items/xerewards.test.c | 6 +++--- src/randenc.c | 6 +++--- src/study.c | 17 +++++++++++++---- src/study.h | 6 +++++- src/study.test.c | 19 +++++++++++++++++++ 8 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/academy.c b/src/academy.c index 60af23246..ec191fa50 100644 --- a/src/academy.c +++ b/src/academy.c @@ -28,7 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. void academy_teaching_bonus(struct unit *u, skill_t sk, int academy) { if (academy && sk != NOSKILL) { academy = academy / 30; /* anzahl gelehrter wochen, max. 10 */ - learn_skill(u, sk, academy / 30.0 / TEACHNUMBER); + learn_skill_depr(u, sk, academy / 30.0 / TEACHNUMBER); } } diff --git a/src/bindings.c b/src/bindings.c index bf5c059ff..3024b2311 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -397,7 +397,7 @@ static int tolua_learn_skill(lua_State * L) float chances = (float)tolua_tonumber(L, 3, 0); skill_t sk = findskill(skname); if (sk != NOSKILL) { - learn_skill(u, sk, chances); + learn_skill_depr(u, sk, chances); } return 0; } @@ -1119,7 +1119,7 @@ int tolua_bindings_open(lua_State * L) tolua_function(L, TOLUA_CAST "update_subscriptions", tolua_update_subscriptions); tolua_function(L, TOLUA_CAST "update_scores", tolua_update_scores); tolua_function(L, TOLUA_CAST "update_owners", tolua_update_owners); - tolua_function(L, TOLUA_CAST "learn_skill", tolua_learn_skill); + tolua_function(L, TOLUA_CAST "learn_skill_depr", tolua_learn_skill); tolua_function(L, TOLUA_CAST "create_curse", tolua_create_curse); tolua_function(L, TOLUA_CAST "autoseed", tolua_autoseed); tolua_function(L, TOLUA_CAST "get_key", tolua_getkey); diff --git a/src/items/xerewards.c b/src/items/xerewards.c index 7c9df21aa..1563810d6 100644 --- a/src/items/xerewards.c +++ b/src/items/xerewards.c @@ -55,7 +55,7 @@ struct order *ord) while (sv != u->skills + u->skill_size) { int i; for (i = 0; i != 3; ++i) - learn_skill(u, (skill_t)sv->id, 1.0); + learn_skill_depr(u, (skill_t)sv->id, 1.0); ++sv; } } diff --git a/src/items/xerewards.test.c b/src/items/xerewards.test.c index 09ac4ebef..cd620f039 100644 --- a/src/items/xerewards.test.c +++ b/src/items/xerewards.test.c @@ -31,17 +31,17 @@ static void test_skillpotion(CuTest *tc) { itype = test_create_itemtype("skillpotion"); change_resource(u, itype->rtype, 2); - learn_skill(u, SK_ENTERTAINMENT, 1.0); + learn_skill_depr(u, SK_ENTERTAINMENT, 1.0); pSkill = unit_skill(u, SK_ENTERTAINMENT); sk_set(pSkill, 5); initialWeeks_Entertainment = pSkill->weeks = 4; - learn_skill(u, SK_STAMINA, 1.0); + learn_skill_depr(u, SK_STAMINA, 1.0); pSkill = unit_skill(u, SK_STAMINA); sk_set(pSkill, 5); initialWeeks_Stamina = pSkill->weeks = 4; - learn_skill(u, SK_MAGIC, 1.0); + learn_skill_depr(u, SK_MAGIC, 1.0); pSkill = unit_skill(u, SK_MAGIC); sk_set(pSkill, 5); initialWeeks_Magic = pSkill->weeks = 4; diff --git a/src/randenc.c b/src/randenc.c index 26a2e6b4a..a7227a210 100644 --- a/src/randenc.c +++ b/src/randenc.c @@ -160,7 +160,7 @@ static int improve_all(faction * f, skill_t sk, int by_weeks) if (has_skill(u, sk)) { int weeks = 0; for (; weeks != by_weeks; ++weeks) { - learn_skill(u, sk, 1.0); + learn_skill_depr(u, sk, 1.0); ret = 0; } } @@ -250,7 +250,7 @@ void find_manual(region * r, unit * u) if (improve_all(u->faction, skill, 3) == 3) { int i; for (i = 0; i != 9; ++i) - learn_skill(u, skill, 1.0); + learn_skill_depr(u, skill, 1.0); } } @@ -984,7 +984,7 @@ static void demon_skillchanges(void) } else { while (weeks--) { - learn_skill(u, sv->id, 1.0); + learn_skill_depr(u, sv->id, 1.0); } } } diff --git a/src/study.c b/src/study.c index a69dff705..1560bceb8 100644 --- a/src/study.c +++ b/src/study.c @@ -740,12 +740,12 @@ int study_cmd(unit * u, order * ord) while (days) { if (days >= u->number * 30) { - learn_skill(u, sk, 1.0); + learn_skill_depr(u, sk, 1.0); days -= u->number * 30; } else { double chance = (double)days / u->number / 30; - learn_skill(u, sk, chance); + learn_skill_depr(u, sk, chance); days = 0; } } @@ -816,7 +816,7 @@ void produceexp_ex(struct unit *u, skill_t sk, int n, bool(*learn)(unit *, skill void produceexp(struct unit *u, skill_t sk, int n) { - produceexp_ex(u, sk, n, learn_skill); + produceexp_ex(u, sk, n, learn_skill_depr); } #ifndef NO_TESTS @@ -827,7 +827,7 @@ void inject_learn(learn_fun fun) { } #endif -bool learn_skill(unit * u, skill_t sk, double learn_chance) +bool learn_skill_depr(unit * u, skill_t sk, double learn_chance) { skill *sv = u->skills; #ifndef NO_TESTS @@ -855,3 +855,12 @@ bool learn_skill(unit * u, skill_t sk, double learn_chance) sk_set(sv, 1); return true; } + +void learn_skill(unit *u, skill_t sk, int days) { + double ch = days*1.0 / STUDYDAYS; + while (ch >= 1.0) { + learn_skill_depr(u, sk, 1.0); + ch -= 1.0; + } + learn_skill_depr(u, sk, ch); +} diff --git a/src/study.h b/src/study.h index a0c638fa2..52c384aba 100644 --- a/src/study.h +++ b/src/study.h @@ -39,10 +39,14 @@ extern "C" { typedef bool(*learn_fun)(struct unit *u, skill_t sk, double ch); void inject_learn(learn_fun fun); #endif - bool learn_skill(struct unit *u, skill_t sk, double chance); + +#define STUDYDAYS 30 + bool learn_skill_depr(struct unit *u, skill_t sk, double chance); + void learn_skill(struct unit *u, skill_t sk, int days); void produceexp(struct unit *u, skill_t sk, int n); void produceexp_ex(struct unit *u, skill_t sk, int n, bool(*learn)(struct unit *, skill_t, double)); + #define MAXTEACHERS 20 #define TEACHNUMBER 10 typedef struct teaching_info { diff --git a/src/study.test.c b/src/study.test.c index bd14096bf..b51e17e68 100644 --- a/src/study.test.c +++ b/src/study.test.c @@ -229,9 +229,28 @@ static void test_academy_building(CuTest *tc) { test_cleanup(); } +void test_learn_skill(CuTest *tc) { + unit *u; + skill *sv; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + learn_skill(u, SK_ALCHEMY, STUDYDAYS); + CuAssertPtrNotNull(tc, sv = u->skills); + CuAssertIntEquals(tc, SK_ALCHEMY, sv->id); + CuAssertIntEquals(tc, 1, sv->level); + CuAssertIntEquals(tc, 2, sv->weeks); + learn_skill(u, SK_ALCHEMY, STUDYDAYS); + CuAssertIntEquals(tc, 1, sv->weeks); + learn_skill(u, SK_ALCHEMY, STUDYDAYS * 2); + CuAssertIntEquals(tc, 2, sv->level); + CuAssertIntEquals(tc, 1, sv->weeks); + test_cleanup(); +} + CuSuite *get_study_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_learn_skill); 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);