refactoring: move produceexp from unit.c to study.c

This commit is contained in:
Enno Rehling 2016-03-08 14:09:22 +01:00
parent e1f27448bc
commit 3d5c7ba8fb
14 changed files with 62 additions and 55 deletions

View File

@ -21,6 +21,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "alchemy.h" #include "alchemy.h"
#include "move.h" #include "move.h"
#include "skill.h" #include "skill.h"
#include "study.h"
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/faction.h> #include <kernel/faction.h>

View File

@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "laws.h" #include "laws.h"
#include "randenc.h" #include "randenc.h"
#include "spy.h" #include "spy.h"
#include "study.h"
#include "move.h" #include "move.h"
#include "monster.h" #include "monster.h"
#include "morale.h" #include "morale.h"

View File

@ -23,6 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "alchemy.h" #include "alchemy.h"
#include "direction.h" #include "direction.h"
#include "move.h" #include "move.h"
#include "study.h"
#include "laws.h" #include "laws.h"
#include "skill.h" #include "skill.h"
#include "lighthouse.h" #include "lighthouse.h"

View File

@ -28,7 +28,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "curse.h" #include "curse.h"
#include "item.h" #include "item.h"
#include "move.h" #include "move.h"
#include "monster.h"
#include "order.h" #include "order.h"
#include "plane.h" #include "plane.h"
#include "race.h" #include "race.h"
@ -1892,25 +1891,6 @@ bool unit_can_study(const unit *u) {
return !((u_race(u)->flags & RCF_NOLEARN) || fval(u, UFL_WERE)); return !((u_race(u)->flags & RCF_NOLEARN) || fval(u, UFL_WERE));
} }
static double produceexp_chance(void) {
return config_get_flt("study.from_use", 1.0 / 3);
}
void produceexp_ex(struct unit *u, skill_t sk, int n, bool(*learn)(unit *, skill_t, double))
{
if (n != 0 && (is_monsters(u->faction) || playerrace(u_race(u)))) {
double chance = produceexp_chance();
if (chance > 0.0F) {
learn(u, sk, (n * chance) / u->number);
}
}
}
void produceexp(struct unit *u, skill_t sk, int n)
{
produceexp_ex(u, sk, n, learn_skill);
}
/* ID's für Einheiten und Zauber */ /* ID's für Einheiten und Zauber */
int newunitid(void) int newunitid(void)
{ {

View File

@ -162,9 +162,6 @@ extern "C" {
struct skill *unit_skill(const struct unit *u, skill_t id); struct skill *unit_skill(const struct unit *u, skill_t id);
bool has_skill(const unit * u, skill_t sk); bool has_skill(const unit * u, skill_t sk);
int effskill(const struct unit *u, skill_t sk, const struct region *r); int effskill(const struct unit *u, skill_t sk, const struct region *r);
int SkillCap(skill_t sk);
void produceexp(struct unit *u, skill_t sk, int n);
void produceexp_ex(struct unit *u, skill_t sk, int n, bool (*learn)(unit *, skill_t, double));
void set_level(struct unit *u, skill_t id, int level); void set_level(struct unit *u, skill_t id, int level);
int get_level(const struct unit *u, skill_t id); int get_level(const struct unit *u, skill_t id);

View File

@ -341,32 +341,6 @@ static void test_age_familiar(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static CuTest *g_tc;
static bool cb_learn_one(unit *u, skill_t sk, double chance) {
CuAssertIntEquals(g_tc, SK_ALCHEMY, sk);
CuAssertDblEquals(g_tc, 0.5 / u->number, chance, 0.01);
return false;
}
static bool cb_learn_two(unit *u, skill_t sk, double chance) {
CuAssertIntEquals(g_tc, SK_ALCHEMY, sk);
CuAssertDblEquals(g_tc, 2 * 0.5 / u->number, chance, 0.01);
return false;
}
static void test_produceexp(CuTest *tc) {
unit *u;
g_tc = tc;
test_cleanup();
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
config_set("study.from_use", "0.5");
produceexp_ex(u, SK_ALCHEMY, 1, cb_learn_one);
produceexp_ex(u, SK_ALCHEMY, 2, cb_learn_two);
test_cleanup();
}
static void test_inside_building(CuTest *tc) { static void test_inside_building(CuTest *tc) {
unit *u; unit *u;
building *b; building *b;
@ -433,7 +407,6 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_skill_familiar); SUITE_ADD_TEST(suite, test_skill_familiar);
SUITE_ADD_TEST(suite, test_age_familiar); SUITE_ADD_TEST(suite, test_age_familiar);
SUITE_ADD_TEST(suite, test_inside_building); SUITE_ADD_TEST(suite, test_inside_building);
SUITE_ADD_TEST(suite, test_produceexp);
SUITE_ADD_TEST(suite, test_limited_skills); SUITE_ADD_TEST(suite, test_limited_skills);
return suite; return suite;
} }

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "magic.h" #include "magic.h"
#include "skill.h" #include "skill.h"
#include "study.h"
#include "laws.h" #include "laws.h"
#include <kernel/ally.h> #include <kernel/ally.h>

View File

@ -26,6 +26,7 @@
#include "monster.h" #include "monster.h"
#include "laws.h" #include "laws.h"
#include "keyword.h" #include "keyword.h"
#include "study.h"
/* triggers includes */ /* triggers includes */
#include <triggers/removecurse.h> #include <triggers/removecurse.h>

View File

@ -13,6 +13,7 @@
#include "monster.h" #include "monster.h"
#include "guard.h" #include "guard.h"
#include "skill.h" #include "skill.h"
#include "study.h"
#include <util/language.h> #include <util/language.h>

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "move.h" #include "move.h"
#include "laws.h" #include "laws.h"
#include "reports.h" #include "reports.h"
#include "study.h"
#include "alchemy.h" #include "alchemy.h"
#include "travelthru.h" #include "travelthru.h"
#include "vortex.h" #include "vortex.h"

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "laws.h" #include "laws.h"
#include "move.h" #include "move.h"
#include "reports.h" #include "reports.h"
#include "study.h"
/* kernel includes */ /* kernel includes */
#include <kernel/item.h> #include <kernel/item.h>

View File

@ -23,6 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/config.h> #include <kernel/config.h>
#include "study.h" #include "study.h"
#include "move.h" #include "move.h"
#include "monster.h"
#include "alchemy.h" #include "alchemy.h"
#include <kernel/ally.h> #include <kernel/ally.h>
@ -804,3 +805,22 @@ int study_cmd(unit * u, order * ord)
return 0; return 0;
} }
static double produceexp_chance(void) {
return config_get_flt("study.from_use", 1.0 / 3);
}
void produceexp_ex(struct unit *u, skill_t sk, int n, bool(*learn)(unit *, skill_t, double))
{
if (n != 0 && (is_monsters(u->faction) || playerrace(u_race(u)))) {
double chance = produceexp_chance();
if (chance > 0.0F) {
learn(u, sk, (n * chance) / u->number);
}
}
}
void produceexp(struct unit *u, skill_t sk, int n)
{
produceexp_ex(u, sk, n, learn_skill);
}

View File

@ -26,13 +26,15 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern "C" { extern "C" {
#endif #endif
extern int teach_cmd(struct unit *u, struct order *ord); int teach_cmd(struct unit *u, struct order *ord);
extern int study_cmd(struct unit *u, struct order *ord); int study_cmd(struct unit *u, struct order *ord);
extern magic_t getmagicskill(const struct locale *lang); magic_t getmagicskill(const struct locale *lang);
extern bool is_migrant(struct unit *u); bool is_migrant(struct unit *u);
extern int study_cost(struct unit *u, skill_t talent); int study_cost(struct unit *u, skill_t talent);
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 MAXTEACHERS 20
typedef struct teaching_info { typedef struct teaching_info {
struct unit *teachers[MAXTEACHERS]; struct unit *teachers[MAXTEACHERS];

View File

@ -136,12 +136,39 @@ static void test_study_bug_2194(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static CuTest *g_tc;
static bool cb_learn_one(unit *u, skill_t sk, double chance) {
CuAssertIntEquals(g_tc, SK_ALCHEMY, sk);
CuAssertDblEquals(g_tc, 0.5 / u->number, chance, 0.01);
return false;
}
static bool cb_learn_two(unit *u, skill_t sk, double chance) {
CuAssertIntEquals(g_tc, SK_ALCHEMY, sk);
CuAssertDblEquals(g_tc, 2 * 0.5 / u->number, chance, 0.01);
return false;
}
static void test_produceexp(CuTest *tc) {
unit *u;
g_tc = tc;
test_cleanup();
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
config_set("study.from_use", "0.5");
produceexp_ex(u, SK_ALCHEMY, 1, cb_learn_one);
produceexp_ex(u, SK_ALCHEMY, 2, cb_learn_two);
test_cleanup();
}
CuSuite *get_study_suite(void) CuSuite *get_study_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_study_no_teacher); SUITE_ADD_TEST(suite, test_study_no_teacher);
SUITE_ADD_TEST(suite, test_study_with_teacher); SUITE_ADD_TEST(suite, test_study_with_teacher);
SUITE_ADD_TEST(suite, test_study_with_bad_teacher); SUITE_ADD_TEST(suite, test_study_with_bad_teacher);
SUITE_ADD_TEST(suite, test_produceexp);
DISABLE_TEST(suite, test_study_bug_2194); DISABLE_TEST(suite, test_study_bug_2194);
return suite; return suite;
} }