bug: learn_skill was not learning the right skill

refactor: further reduce learn_skill_depr usage
This commit is contained in:
Enno Rehling 2016-03-17 21:21:23 +01:00
parent 0464fb629a
commit f97b6c9821
6 changed files with 25 additions and 32 deletions

View File

@ -394,10 +394,10 @@ static int tolua_learn_skill(lua_State * L)
{ {
unit *u = (unit *)tolua_tousertype(L, 1, 0); unit *u = (unit *)tolua_tousertype(L, 1, 0);
const char *skname = tolua_tostring(L, 2, 0); const char *skname = tolua_tostring(L, 2, 0);
float chances = (float)tolua_tonumber(L, 3, 0); int days = (int)tolua_tonumber(L, 3, 0);
skill_t sk = findskill(skname); skill_t sk = findskill(skname);
if (sk != NOSKILL) { if (sk != NOSKILL) {
learn_skill_depr(u, sk, chances); learn_skill(u, sk, days);
} }
return 0; 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_subscriptions", tolua_update_subscriptions);
tolua_function(L, TOLUA_CAST "update_scores", tolua_update_scores); 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 "update_owners", tolua_update_owners);
tolua_function(L, TOLUA_CAST "learn_skill_depr", tolua_learn_skill); tolua_function(L, TOLUA_CAST "learn_skill", tolua_learn_skill);
tolua_function(L, TOLUA_CAST "create_curse", tolua_create_curse); tolua_function(L, TOLUA_CAST "create_curse", tolua_create_curse);
tolua_function(L, TOLUA_CAST "autoseed", tolua_autoseed); tolua_function(L, TOLUA_CAST "autoseed", tolua_autoseed);
tolua_function(L, TOLUA_CAST "get_key", tolua_getkey); tolua_function(L, TOLUA_CAST "get_key", tolua_getkey);

View File

@ -53,9 +53,7 @@ struct order *ord)
for (n = 0; n != amount; ++n) { for (n = 0; n != amount; ++n) {
skill *sv = u->skills; skill *sv = u->skills;
while (sv != u->skills + u->skill_size) { while (sv != u->skills + u->skill_size) {
int i; learn_skill(u, (skill_t)sv->id, STUDYDAYS * 3);
for (i = 0; i != 3; ++i)
learn_skill_depr(u, (skill_t)sv->id, 1.0);
++sv; ++sv;
} }
} }

View File

@ -31,17 +31,17 @@ static void test_skillpotion(CuTest *tc) {
itype = test_create_itemtype("skillpotion"); itype = test_create_itemtype("skillpotion");
change_resource(u, itype->rtype, 2); change_resource(u, itype->rtype, 2);
learn_skill_depr(u, SK_ENTERTAINMENT, 1.0); learn_skill(u, SK_ENTERTAINMENT, STUDYDAYS);
pSkill = unit_skill(u, SK_ENTERTAINMENT); pSkill = unit_skill(u, SK_ENTERTAINMENT);
sk_set(pSkill, 5); sk_set(pSkill, 5);
initialWeeks_Entertainment = pSkill->weeks = 4; initialWeeks_Entertainment = pSkill->weeks = 4;
learn_skill_depr(u, SK_STAMINA, 1.0); learn_skill(u, SK_STAMINA, STUDYDAYS);
pSkill = unit_skill(u, SK_STAMINA); pSkill = unit_skill(u, SK_STAMINA);
sk_set(pSkill, 5); sk_set(pSkill, 5);
initialWeeks_Stamina = pSkill->weeks = 4; initialWeeks_Stamina = pSkill->weeks = 4;
learn_skill_depr(u, SK_MAGIC, 1.0); learn_skill(u, SK_MAGIC, STUDYDAYS);
pSkill = unit_skill(u, SK_MAGIC); pSkill = unit_skill(u, SK_MAGIC);
sk_set(pSkill, 5); sk_set(pSkill, 5);
initialWeeks_Magic = pSkill->weeks = 4; initialWeeks_Magic = pSkill->weeks = 4;

View File

@ -206,7 +206,7 @@ int level(int days)
void sk_set(skill * sv, int level) void sk_set(skill * sv, int level)
{ {
assert(level != 0); assert(sv && level != 0);
sv->weeks = skill_weeks(level); sv->weeks = skill_weeks(level);
sv->level = level; sv->level = level;
} }

View File

@ -151,22 +151,17 @@ static void dissolve_units(void)
remove_empty_units(); remove_empty_units();
} }
static int improve_all(faction * f, skill_t sk, int by_weeks) static bool improve_all(faction * f, skill_t sk, int by_weeks)
{ {
unit *u; unit *u;
bool ret = by_weeks; bool result = false;
for (u = f->units; u; u = u->nextF) { for (u = f->units; u; u = u->nextF) {
if (has_skill(u, sk)) { if (has_skill(u, sk)) {
int weeks = 0; learn_skill(u, sk, by_weeks * STUDYDAYS);
for (; weeks != by_weeks; ++weeks) { result = true;
learn_skill_depr(u, sk, 1.0);
ret = 0;
}
} }
} }
return result;
return ret;
} }
void find_manual(region * r, unit * u) void find_manual(region * r, unit * u)
@ -247,10 +242,8 @@ void find_manual(region * r, unit * u)
msg_release(msg); msg_release(msg);
} }
if (improve_all(u->faction, skill, 3) == 3) { if (!improve_all(u->faction, skill, 3)) {
int i; learn_skill(u, skill, 9 * STUDYDAYS);
for (i = 0; i != 9; ++i)
learn_skill_depr(u, skill, 1.0);
} }
} }

View File

@ -847,7 +847,6 @@ bool learn_skill_depr(unit * u, skill_t sk, double learn_chance)
} }
void learn_skill(unit *u, skill_t sk, int days) { void learn_skill(unit *u, skill_t sk, int days) {
skill *sv = u->skills;
int leveldays = STUDYDAYS * u->number; int leveldays = STUDYDAYS * u->number;
int weeks = 0; int weeks = 0;
while (days >= leveldays) { while (days >= leveldays) {
@ -857,14 +856,17 @@ void learn_skill(unit *u, skill_t sk, int days) {
if (days > 0 && rng_int() % leveldays < days) { if (days > 0 && rng_int() % leveldays < days) {
++weeks; ++weeks;
} }
if (weeks > 0 && !sv) { if (weeks > 0) {
sv = add_skill(u, sk); skill *sv = unit_skill(u, sk);
if (!sv) {
sv = add_skill(u, sk);
}
while (sv->weeks <= weeks) {
weeks -= sv->weeks;
sk_set(sv, sv->level + 1);
}
sv->weeks -= weeks;
} }
while (sv->weeks <= weeks) {
weeks -= sv->weeks;
sk_set(sv, sv->level + 1);
}
sv->weeks -= weeks;
} }
/** Talente von Dämonen verschieben sich. /** Talente von Dämonen verschieben sich.