fix use of static configuration in study_cost.

This commit is contained in:
Enno Rehling 2017-12-29 15:58:12 +01:00
parent c23e7bf9b9
commit c3a812123f
2 changed files with 65 additions and 51 deletions

View file

@ -128,38 +128,44 @@ bool magic_lowskill(unit * u)
/* ------------------------------------------------------------- */
int study_cost(unit * u, skill_t sk)
int study_cost(struct unit *u, skill_t sk)
{
static int config;
static int cost[MAXSKILLS];
int stufe, k = 50;
static int costs[MAXSKILLS];
int cost = -1;
if (config_changed(&config)) {
memset(cost, 0, sizeof(cost));
if (sk == SK_MAGIC) {
int next_level = 1 + (u ? get_level(u, sk) : 0);
/* Die Magiekosten betragen 50+Summe(50*Stufe) */
/* 'Stufe' ist dabei die naechste zu erreichende Stufe */
cost = 50 * (1 + ((next_level + 1) * next_level / 2));
}
if (cost[sk] == 0) {
char buffer[256];
sprintf(buffer, "skills.cost.%s", skillnames[sk]);
cost[sk] = config_get_int(buffer, -1);
}
if (cost[sk] >= 0) {
return cost[sk];
}
switch (sk) {
else switch (sk) {
case SK_SPY:
return 100;
cost = 100;
break;
case SK_TACTICS:
case SK_HERBALISM:
case SK_ALCHEMY:
return 200;
case SK_MAGIC: /* Die Magiekosten betragen 50+Summe(50*Stufe) */
/* 'Stufe' ist dabei die naechste zu erreichende Stufe */
stufe = 1 + get_level(u, SK_MAGIC);
return k * (1 + ((stufe + 1) * stufe / 2));
cost = 200;
break;
default:
return 0;
cost = -1;
}
if (config_changed(&config)) {
memset(costs, 0, sizeof(costs));
}
if (costs[sk] == 0) {
char buffer[256];
sprintf(buffer, "skills.cost.%s", skillnames[sk]);
costs[sk] = config_get_int(buffer, cost);
}
if (costs[sk] >= 0) {
return costs[sk];
}
return (cost > 0) ? cost : 0;
}
/* ------------------------------------------------------------- */
@ -559,7 +565,7 @@ int study_cmd(unit * u, order * ord)
if (!unit_can_study(u)) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_race_nolearn", "race",
u_race(u)));
return 0;
return -1;
}
(void)init_order(ord, u->faction->locale);
@ -567,19 +573,19 @@ int study_cmd(unit * u, order * ord)
if (sk < 0) {
cmistake(u, ord, 77, MSG_EVENT);
return 0;
return -1;
}
/* Hack: Talente mit Malus -99 koennen nicht gelernt werden */
if (u_race(u)->bonus[sk] == -99) {
cmistake(u, ord, 771, MSG_EVENT);
return 0;
return -1;
}
if (!learn_newskills) {
skill *sv = unit_skill(u, sk);
if (sv == NULL) {
/* we can only learn skills we already have */
cmistake(u, ord, 771, MSG_EVENT);
return 0;
return -1;
}
}
@ -587,7 +593,7 @@ int study_cmd(unit * u, order * ord)
if (u_race(u) == rc_snotling) {
if (get_level(u, sk) >= 8) {
cmistake(u, ord, 308, MSG_EVENT);
return 0;
return -1;
}
}
@ -603,7 +609,7 @@ int study_cmd(unit * u, order * ord)
if (studycost > 0 && !ExpensiveMigrants() && is_migrant(u)) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_migrants_nolearn",
""));
return 0;
return -1;
}
/* Akademie: */
if (active_building(u, bt_find("academy"))) {
@ -613,7 +619,7 @@ int study_cmd(unit * u, order * ord)
if (sk == SK_MAGIC) {
if (u->number > 1) {
cmistake(u, ord, 106, MSG_MAGIC);
return 0;
return -1;
}
if (is_familiar(u)) {
/* Vertraute zaehlen nicht zu den Magiern einer Partei,
@ -626,7 +632,7 @@ int study_cmd(unit * u, order * ord)
if (count_skill(u->faction, SK_MAGIC) + u->number > mmax) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_max_magicians",
"amount", mmax));
return 0;
return -1;
}
mtyp = getmagicskill(u->faction->locale);
if (mtyp == M_NONE || mtyp == M_GRAY) {
@ -641,7 +647,7 @@ int study_cmd(unit * u, order * ord)
mtyp = getmagicskill(u->faction->locale);
if (mtyp == M_NONE) {
cmistake(u, ord, 178, MSG_MAGIC);
return 0;
return -1;
}
}
}
@ -650,7 +656,7 @@ int study_cmd(unit * u, order * ord)
* als das der Partei */
if (u->faction->magiegebiet != 0) {
cmistake(u, ord, 179, MSG_MAGIC);
return 0;
return -1;
}
else {
/* Lernt zum ersten mal Magie und legt damit das
@ -669,7 +675,7 @@ int study_cmd(unit * u, order * ord)
mtyp = getmagicskill(u->faction->locale);
if (mtyp == M_NONE) {
cmistake(u, ord, 178, MSG_MAGIC);
return 0;
return -1;
}
}
/* Legt damit das Magiegebiet der Partei fest */
@ -684,7 +690,7 @@ int study_cmd(unit * u, order * ord)
if (count_skill(u->faction, SK_ALCHEMY) + u->number > amax) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_max_alchemists",
"amount", amax));
return 0;
return -1;
}
}
}

View file

@ -386,41 +386,43 @@ static void test_study_magic(CuTest *tc) {
unit *u;
faction *f;
const struct locale *lang;
const struct resource_type *rtype;
const struct item_type *itype;
test_setup();
init_resources();
f = test_create_faction(0);
u = test_create_unit(f, test_create_region(0, 0, 0));
lang = f->locale;
CuAssertPtrNotNull(tc, rtype = get_resourcetype(R_SILVER));
u = test_create_unit(f, test_create_region(0, 0, 0));
u->thisorder = create_order(K_STUDY, lang, "%s", skillnames[SK_MAGIC]);
study_cmd(u, u->thisorder);
itype = test_create_silver();
CuAssertIntEquals(tc, -1, study_cmd(u, u->thisorder));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error178"));
free_order(u->thisorder);
test_clear_messages(f);
u->thisorder = create_order(K_STUDY, lang, "%s %s", skillnames[SK_MAGIC], magic_school[M_GWYRRD]);
study_cmd(u, u->thisorder);
CuAssertIntEquals(tc, 0, study_cmd(u, u->thisorder));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error65"));
test_clear_messages(f);
i_change(&u->items, rtype->itype, 100);
study_cmd(u, u->thisorder);
i_change(&u->items, itype, 100);
CuAssertIntEquals(tc, 0, study_cmd(u, u->thisorder));
CuAssertIntEquals(tc, M_GWYRRD, f->magiegebiet);
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
CuAssertIntEquals(tc, 0, i_get(u->items, itype));
CuAssertPtrNotNull(tc, get_mage_depr(u));
CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "error65"));
CuAssertIntEquals(tc, M_GWYRRD, get_mage_depr(u)->magietyp);
/* TODO: the static cost array in study_cost prevents this test:
test_clear_messages(f);
config_set("skills.cost.magic", "50");
i_change(&u->items, rtype->itype, 50);
study_cmd(u, u->thisorder);
CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "error65"));
*/
test_teardown();
}
static void test_study_cost_magic(CuTest *tc) {
test_setup();
config_set("skills.cost.magic", "50");
CuAssertIntEquals(tc, 50, study_cost(NULL, SK_MAGIC));
config_set("skills.cost.magic", "100");
CuAssertIntEquals(tc, 100, study_cost(NULL, SK_MAGIC));
test_teardown();
}
@ -429,11 +431,16 @@ static void test_study_cost(CuTest *tc) {
const struct item_type *itype;
test_setup();
init_resources();
itype = get_resourcetype(R_SILVER)->itype;
itype = test_create_silver();
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
scale_number(u, 2);
u->thisorder = create_order(K_STUDY, u->faction->locale, skillnames[SK_ALCHEMY]);
CuAssertIntEquals(tc, 200, study_cost(u, SK_ALCHEMY));
config_set("skills.cost.alchemy", "50");
CuAssertIntEquals(tc, 50, study_cost(u, SK_ALCHEMY));
i_change(&u->items, itype, u->number * study_cost(u, SK_ALCHEMY));
learn_inject();
study_cmd(u, u->thisorder);
@ -682,6 +689,7 @@ CuSuite *get_study_suite(void)
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_study_cmd);
SUITE_ADD_TEST(suite, test_study_cost);
SUITE_ADD_TEST(suite, test_study_cost_magic);
SUITE_ADD_TEST(suite, test_study_magic);
SUITE_ADD_TEST(suite, test_teach_cmd);
SUITE_ADD_TEST(suite, test_teach_magic);