diff --git a/process/backup-eressea b/process/backup-eressea index 4e5f69002..0f81cf53e 100755 --- a/process/backup-eressea +++ b/process/backup-eressea @@ -34,4 +34,4 @@ fi tar cjf backup/$TURN.tar.bz2 $files echo "uploading game-$GAME/$TURN.tar.bz2" curl -s -n -T backup/$TURN.tar.bz2 https://dav.box.com/dav/Eressea/game-$GAME/$TURN.tar.bz2 -curl -s -n -T eressea.db https://dav.box.com/dav/Eressea/eressea.db +#curl -s -n -T eressea.db https://dav.box.com/dav/Eressea/eressea.db diff --git a/src/alchemy.test.c b/src/alchemy.test.c index 32e0c84a5..e7d9206c4 100644 --- a/src/alchemy.test.c +++ b/src/alchemy.test.c @@ -26,7 +26,6 @@ static void test_herbsearch(CuTest * tc) const item_type *itype; test_setup(); - test_inject_messagetypes(); r = test_create_region(0, 0, NULL); rc = rc_get_or_create("dragon"); rc->flags |= RCF_UNARMEDGUARD; diff --git a/src/automate.c b/src/automate.c index c2b07fbd0..200541241 100644 --- a/src/automate.c +++ b/src/automate.c @@ -34,13 +34,13 @@ int autostudy_init(scholar scholars[], int max_scholars, region *r) int nscholars = 0; for (u = r->units; u; u = u->next) { - keyword_t kwd = getkeyword(u->thisorder); + keyword_t kwd = init_order(u->thisorder, u->faction->locale); if (kwd == K_AUTOSTUDY) { if (long_order_allowed(u)) { - if (unit_can_study(u)) { - scholar * st = scholars + nscholars; - init_order(u->thisorder, u->faction->locale); - st->sk = getskill(u->faction->locale); + scholar * st = scholars + nscholars; + skill_t sk = getskill(u->faction->locale); + if (check_student(u, u->thisorder, sk)) { + st->sk = sk; st->level = effskill_study(u, st->sk); st->learn = 0; st->u = u; @@ -48,10 +48,6 @@ int autostudy_init(scholar scholars[], int max_scholars, region *r) log_fatal("you must increase MAXSCHOLARS"); } } - else { - ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "error_race_nolearn", "race", - u_race(u))); - } } } } diff --git a/src/automate.test.c b/src/automate.test.c index a77376668..6c87b8656 100644 --- a/src/automate.test.c +++ b/src/automate.test.c @@ -9,17 +9,22 @@ #include "kernel/region.h" #include "kernel/unit.h" +#include "util/message.h" + #include "tests.h" #include static void test_autostudy_init(CuTest *tc) { scholar scholars[4]; - unit *u1, *u2, *u3; + unit *u1, *u2, *u3, *u4; faction *f; region *r; test_setup(); + mt_create_error(77); + mt_create_error(771); + r = test_create_plain(0, 0); f = test_create_faction(NULL); u1 = test_create_unit(f, r); @@ -30,8 +35,11 @@ static void test_autostudy_init(CuTest *tc) { set_level(u2, SK_ENTERTAINMENT, 2); u3 = test_create_unit(f, r); u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]); + u4 = test_create_unit(test_create_faction(NULL), r); + u4->thisorder = create_order(K_AUTOSTUDY, f->locale, "Dudelidu"); scholars[3].u = NULL; CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r)); + CuAssertPtrNotNull(tc, test_find_messagetype(u4->faction->msgs, "error77")); CuAssertPtrEquals(tc, u2, scholars[0].u); CuAssertIntEquals(tc, 2, scholars[0].level); CuAssertIntEquals(tc, 0, scholars[0].learn); diff --git a/src/bind_eressea.c b/src/bind_eressea.c index 4d3918899..f76d0a903 100755 --- a/src/bind_eressea.c +++ b/src/bind_eressea.c @@ -36,12 +36,16 @@ int eressea_write_game(const char * filename) { int eressea_read_orders(const char * filename) { FILE * F = fopen(filename, "r"); + int result; + if (!F) { perror(filename); return -1; } log_info("reading orders from %s", filename); - return parseorders(F); + result = parseorders(F); + fclose(F); + return result; } int eressea_export_json(const char * filename, int flags) { diff --git a/src/economy.test.c b/src/economy.test.c index 5425ca56f..236c181c5 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -145,6 +145,7 @@ static struct unit *create_recruiter(void) { static void setup_production(void) { init_resources(); + mt_create_feedback("error_cannotmake"); mt_create_va(mt_new("produce", NULL), "unit:unit", "region:region", "amount:int", "wanted:int", "resource:resource", MT_NEW_END); mt_create_va(mt_new("income", NULL), "unit:unit", "region:region", "amount:int", "wanted:int", "mode:int", MT_NEW_END); mt_create_va(mt_new("buy", NULL), "unit:unit", "money:int", MT_NEW_END); @@ -738,6 +739,7 @@ static void test_loot(CuTest *tc) { test_setup(); setup_production(); + mt_create_error(48); /* unit is unarmed */ it_silver = test_create_silver(); config_set("rules.enable_loot", "1"); u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); @@ -747,7 +749,7 @@ static void test_loot(CuTest *tc) { test_clear_messages(f); arm_unit(u); produce(u->region); - CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "income")); /* unit is unarmed */ + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "income")); CuAssertIntEquals(tc, 2 * TAXFRACTION, i_get(u->items, it_silver)); CuAssertIntEquals(tc, UFL_LONGACTION | UFL_NOTMOVING, fval(u, UFL_LONGACTION | UFL_NOTMOVING)); test_teardown(); diff --git a/src/give.test.c b/src/give.test.c index 2828456ae..f3164470c 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -65,11 +65,14 @@ static void setup_give(struct give *env) { mt_create_va(mt_new("give", NULL), "unit:unit", "target:unit", "resource:resource", "amount:int", MT_NEW_END); mt_create_va(mt_new("give_peasants", NULL), "unit:unit", "resource:resource", "amount:int", MT_NEW_END); /* error messages: */ + mt_create_error(129); + mt_create_error(96); + mt_create_error(10); + mt_create_feedback("feedback_give_forbidden"); + mt_create_feedback("peasants_give_invalid"); mt_create_va(mt_new("too_many_units_in_faction", NULL), "unit:unit", "region:region", "command:order", "allowed:int", MT_NEW_END); mt_create_va(mt_new("too_many_units_in_alliance", NULL), "unit:unit", "region:region", "command:order", "allowed:int", MT_NEW_END); mt_create_va(mt_new("feedback_no_contact", NULL), "unit:unit", "region:region", "command:order", "target:unit", MT_NEW_END); - mt_create_va(mt_new("feedback_give_forbidden", NULL), "unit:unit", "region:region", "command:order", MT_NEW_END); - mt_create_va(mt_new("peasants_give_invalid", NULL), "unit:unit", "region:region", "command:order", MT_NEW_END); mt_create_va(mt_new("giverestriction", NULL), "unit:unit", "region:region", "command:order", "turns:int", MT_NEW_END); mt_create_va(mt_new("error_unit_size", NULL), "unit:unit", "region:region", "command:order", "maxsize:int", MT_NEW_END); mt_create_va(mt_new("nogive_reserved", NULL), "unit:unit", "region:region", "command:order", "resource:resource", "reservation:int", MT_NEW_END); @@ -159,11 +162,12 @@ static void test_give_men_magicians(CuTest * tc) { message * msg; test_setup_ex(tc); + mt_create_error(158); env.f2 = env.f1 = test_create_faction(NULL); setup_give(&env); set_level(env.src, SK_MAGIC, 1); CuAssertPtrNotNull(tc, msg = give_men(1, env.src, env.dst, NULL)); - CuAssertStrEquals(tc, "error158", (const char *)msg->parameters[3].v); + CuAssertStrEquals(tc, "error158", test_get_messagetype(msg)); CuAssertIntEquals(tc, 1, env.dst->number); CuAssertIntEquals(tc, 1, env.src->number); msg_release(msg); diff --git a/src/items/xerewards.test.c b/src/items/xerewards.test.c index 8e30d88eb..dc9aa2621 100644 --- a/src/items/xerewards.test.c +++ b/src/items/xerewards.test.c @@ -18,7 +18,6 @@ static void test_manacrystal(CuTest *tc) { unit *u; test_setup(); - test_inject_messagetypes(); u = test_create_unit(test_create_faction(NULL), test_create_plain(0, 0)); itype = test_create_itemtype("manacrystal"); @@ -49,7 +48,6 @@ static void test_skillpotion(CuTest *tc) { int initialWeeks_Magic = 0; test_setup(); - test_inject_messagetypes(); u = test_create_unit(test_create_faction(NULL), test_create_plain(0, 0)); itype = test_create_itemtype("skillpotion"); change_resource(u, itype->rtype, 2); diff --git a/src/kernel/build.c b/src/kernel/build.c index 2b7251f1d..0c1bd71de 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -500,6 +500,28 @@ static int count_materials(unit *u, const construction *type, int n, int complet return n; } +int build_skill(unit *u, int basesk, int skill_mod) { + int effsk, skills; + int dm = get_effect(u, oldpotiontype[P_DOMORE]); + + effsk = basesk + skill_mod; + assert(effsk >= 0); + + skills = effsk * u->number; + + /* technically, nimblefinge and domore should be in a global set of + * "game"-attributes, (as at_skillmod) but for a while, we're leaving + * them in here. */ + + if (dm != 0) { + /* Auswirkung Schaffenstrunk */ + if (dm > u->number) dm = u->number; + change_effect(u, oldpotiontype[P_DOMORE], -dm); + skills += dm * effsk; + } + return skills; +} + /** Use up resources for building an object. * Build up to 'size' points of 'type', where 'completed' * of the first object have already been finished. return the @@ -521,28 +543,11 @@ int build(unit * u, const construction * ctype, int completed, int want, int ski return ECOMPLETE; } if (con->skill != NOSKILL) { - int effsk; - int dm = get_effect(u, oldpotiontype[P_DOMORE]); - basesk = effskill(u, con->skill, 0); if (basesk == 0) return ENEEDSKILL; - effsk = basesk + skill_mod; - assert(effsk >= 0); - - skills = effsk * u->number; - - /* technically, nimblefinge and domore should be in a global set of - * "game"-attributes, (as at_skillmod) but for a while, we're leaving - * them in here. */ - - if (dm != 0) { - /* Auswirkung Schaffenstrunk */ - if (dm > u->number) dm = u->number; - change_effect(u, oldpotiontype[P_DOMORE], -dm); - skills += dm * effsk; - } + skills = build_skill(u, basesk, skill_mod); } for (; want > 0 && skills > 0;) { int err, n; diff --git a/src/kernel/build.test.c b/src/kernel/build.test.c index ae3e47d0d..ea3e2aeb5 100644 --- a/src/kernel/build.test.c +++ b/src/kernel/build.test.c @@ -36,7 +36,6 @@ typedef struct build_fixture { static unit * setup_build(build_fixture *bf) { test_setup(); - test_inject_messagetypes(); init_resources(); test_create_itemtype("stone"); @@ -312,7 +311,6 @@ static void test_build_destroy_road_guard(CuTest *tc) order *ord; test_setup(); - test_inject_messagetypes(); test_create_region(1, 0, 0); r = test_create_region(0, 0, NULL); rsetroad(r, D_EAST, 100); @@ -345,7 +343,6 @@ static void test_build_destroy_road_limit(CuTest *tc) order *ord; test_setup(); - test_inject_messagetypes(); test_create_region(1, 0, 0); r = test_create_region(0, 0, NULL); rsetroad(r, D_EAST, 100); @@ -370,6 +367,7 @@ static void test_build_destroy_cmd(CuTest *tc) { faction *f; test_setup(); + mt_create_error(138); u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); u->thisorder = create_order(K_DESTROY, f->locale, NULL); CuAssertIntEquals(tc, 138, destroy_cmd(u, u->thisorder)); diff --git a/src/kernel/curse.test.c b/src/kernel/curse.test.c index da0806914..886e25ee4 100644 --- a/src/kernel/curse.test.c +++ b/src/kernel/curse.test.c @@ -51,7 +51,6 @@ typedef struct { static void setup_curse(curse_fixture *fix, const char *name) { test_setup(); - test_inject_messagetypes(); fix->r = test_create_region(0, 0, NULL); fix->u = test_create_unit(test_create_faction(NULL), fix->r); fix->c = create_curse(fix->u, &fix->r->attribs, ct_find(name), 1.0, 1, 1.0, 0); diff --git a/src/kernel/db/sqlite.c b/src/kernel/db/sqlite.c index 6aff52cf1..29638d900 100644 --- a/src/kernel/db/sqlite.c +++ b/src/kernel/db/sqlite.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -226,7 +227,11 @@ void db_driver_close(database_t db) FILE * F = fopen(g_swapname, "r"); if (F) { fclose(F); - remove(g_swapname); + if (0 != remove(g_swapname)) { + log_error("could not remove %s: %s", g_swapname, + strerror(errno)); + errno = 0; + } } } } diff --git a/src/kernel/messages.c b/src/kernel/messages.c index e0513b00d..48ea13f1f 100644 --- a/src/kernel/messages.c +++ b/src/kernel/messages.c @@ -67,6 +67,46 @@ variant v) } } +static int missing_message_mode; + +void message_handle_missing(int mode) { + missing_message_mode = mode; +} + +static message *missing_feedback(const char *name, const struct unit *u, + const struct region *r, struct order *ord) +{ + if (missing_message_mode == MESSAGE_MISSING_ERROR) { + log_error("trying to create undefined feedback of type \"%s\"\n", name); + } + else if (missing_message_mode == MESSAGE_MISSING_REPLACE) { + if (strcmp(name, "missing_feedback") != 0) { + if (!mt_find("missing_feedback")) { + mt_create_va(mt_new("missing_feedback", NULL), "unit:unit", + "region:region", "command:order", "name:string", MT_NEW_END); + } + return msg_message("missing_feedback", "unit region command name", u, r, ord, name); + } + } + return NULL; +} + +static message *missing_message(const char *name) { + if (missing_message_mode == MESSAGE_MISSING_ERROR) { + log_error("trying to create undefined message of type \"%s\"\n", name); + } + else if (missing_message_mode == MESSAGE_MISSING_REPLACE) { + log_warning("trying to create undefined message of type \"%s\"\n", name); + if (strcmp(name, "missing_message") != 0) { + if (!mt_find("missing_message")) { + mt_create_va(mt_new("missing_message", NULL), "name:string", MT_NEW_END); + } + return msg_message("missing_message", "name", name); + } + } + return NULL; +} + struct message *msg_feedback(const struct unit *u, struct order *ord, const char *name, const char *sig, ...) { @@ -80,13 +120,7 @@ struct message *msg_feedback(const struct unit *u, struct order *ord, } if (!mtype) { - log_warning("trying to create message of unknown type \"%s\"\n", name); - if (!mt_find("missing_feedback")) { - mt_create_va(mt_new("missing_feedback", NULL), "unit:unit", - "region:region", "command:order", "name:string", MT_NEW_END); - } - return msg_message("missing_feedback", "name unit region command", - name, u, u->region, ord); + return missing_feedback(name, u, u->region, ord); } var.v = (void *)u; @@ -139,28 +173,6 @@ struct message *msg_feedback(const struct unit *u, struct order *ord, return msg_create(mtype, args); } -static int missing_message_mode; - -void message_handle_missing(int mode) { - missing_message_mode = mode; -} - -static message *missing_message(const char *name) { - if (missing_message_mode == MESSAGE_MISSING_ERROR) { - log_error("trying to create undefined message of type \"%s\"\n", name); - } - else if (missing_message_mode == MESSAGE_MISSING_REPLACE) { - log_warning("trying to create undefined message of type \"%s\"\n", name); - if (strcmp(name, "missing_message") != 0) { - if (!mt_find("missing_message")) { - mt_create_va(mt_new("missing_message", NULL), "name:string", MT_NEW_END); - } - return msg_message("missing_message", "name", name); - } - } - return NULL; -} - message *msg_message(const char *name, const char *sig, ...) /* msg_message("oops_error", "unit region command", u, r, cmd) */ { diff --git a/src/kernel/messages.test.c b/src/kernel/messages.test.c index e852bbc61..c186973fa 100644 --- a/src/kernel/messages.test.c +++ b/src/kernel/messages.test.c @@ -18,6 +18,21 @@ void test_missing_message(CuTest *tc) { CuAssertPtrNotNull(tc, msg); CuAssertPtrNotNull(tc, msg->type); CuAssertStrEquals(tc, msg->type->name, "missing_message"); + CuAssertStrEquals(tc, "unknown", (const char *)msg->parameters[0].v); + msg_release(msg); + test_teardown(); +} + +void test_missing_feedback(CuTest *tc) { + message *msg; + + test_setup(); + message_handle_missing(MESSAGE_MISSING_REPLACE); + msg = msg_error(NULL, NULL, 77); + CuAssertPtrNotNull(tc, msg); + CuAssertPtrNotNull(tc, msg->type); + CuAssertStrEquals(tc, msg->type->name, "missing_feedback"); + CuAssertStrEquals(tc, "error77", (const char *)msg->parameters[3].v); msg_release(msg); test_teardown(); } diff --git a/src/kernel/order.c b/src/kernel/order.c index 818c77a72..493922faa 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -390,7 +390,7 @@ order *parse_order(const char *s, const struct locale * lang) sptr = sp; p = parse_token(&sp, token, sizeof(token)); sk = get_skill(p, lang); - if (!expensive_skill(sk)) { + if (sk == NOSKILL || !expensive_skill(sk)) { kwd = K_AUTOSTUDY; } } diff --git a/src/kernel/spell.test.c b/src/kernel/spell.test.c index d7dfbad7e..027a57079 100644 --- a/src/kernel/spell.test.c +++ b/src/kernel/spell.test.c @@ -33,7 +33,6 @@ static void test_create_duplicate_spell(CuTest * tc) strlist *sl = 0; test_setup(); - test_inject_messagetypes(); test_log_stderr(0); /* suppress the "duplicate spell" error message */ log = test_log_start(LOG_CPERROR, &sl); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index b5346e04d..b1da047bd 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1801,10 +1801,6 @@ bool unit_name_equals_race(const unit *u) { return false; } -bool unit_can_study(const unit *u) { - return !((u_race(u)->flags & RCF_NOLEARN) || fval(u, UFL_WERE)); -} - static int read_newunitid(const faction * f, const region * r) { int n; diff --git a/src/kernel/unit.h b/src/kernel/unit.h index a0f2f5a4c..86c2e2e61 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -243,7 +243,6 @@ extern "C" { const char *unitname(const struct unit *u); char *write_unitname(const struct unit *u, char *buffer, size_t size); bool unit_name_equals_race(const struct unit *u); - bool unit_can_study(const struct unit *u); /* getunit results: */ #define GET_UNIT 0 diff --git a/src/laws.test.c b/src/laws.test.c index 7531b11f8..5a4f85b85 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -140,7 +140,6 @@ static void test_enter_ship(CuTest * tc) race * rc; test_setup(); - r = test_create_region(0, 0, NULL); rc = test_create_race("smurf"); u = test_create_unit(test_create_faction(rc), r); @@ -475,6 +474,10 @@ static void test_limit_new_units(CuTest * tc) alliance *al; test_setup(); + mt_create_va(mt_new("too_many_units_in_faction", NULL), "unit:unit", + "region:region", "command:order", "allowed:int", MT_NEW_END); + mt_create_va(mt_new("too_many_units_in_alliance", NULL), "unit:unit", + "region:region", "command:order", "allowed:int", MT_NEW_END); al = makealliance(1, "Hodor"); f = test_create_faction(NULL); u = test_create_unit(f, test_create_region(0, 0, NULL)); @@ -890,6 +893,9 @@ static unit * setup_name_cmd(void) { faction *f; test_setup(); + mt_create_error(84); + mt_create_error(148); + mt_create_error(12); mt_create_va(mt_new("renamed_building_seen", NULL), "renamer:unit", "region:region", "building:building", MT_NEW_END); mt_create_va(mt_new("renamed_building_notseen", NULL), "region:region", "building:building", MT_NEW_END); f = test_create_faction(NULL); @@ -1088,6 +1094,7 @@ static void test_long_order_multi_long(CuTest *tc) { /* TODO: write more tests */ unit *u; test_setup(); + mt_create_error(52); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); unit_addorder(u, create_order(K_MOVE, u->faction->locale, NULL)); unit_addorder(u, create_order(K_DESTROY, u->faction->locale, NULL)); @@ -1102,6 +1109,7 @@ static void test_long_order_multi_buy(CuTest *tc) { /* TODO: write more tests */ unit *u; test_setup(); + mt_create_error(52); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); unit_addorder(u, create_order(K_BUY, u->faction->locale, 0)); unit_addorder(u, create_order(K_BUY, u->faction->locale, 0)); @@ -1131,6 +1139,7 @@ static void test_long_order_buy_cast(CuTest *tc) { /* TODO: write more tests */ unit *u; test_setup(); + mt_create_error(52); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); unit_addorder(u, create_order(K_BUY, u->faction->locale, 0)); unit_addorder(u, create_order(K_CAST, u->faction->locale, 0)); @@ -1163,6 +1172,7 @@ static void test_ally_cmd_errors(CuTest *tc) { order *ord; test_setup(); + mt_create_error(66); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); fid = u->faction->no + 1; CuAssertPtrEquals(tc, NULL, findfaction(fid)); @@ -1314,6 +1324,8 @@ static unit * setup_mail_cmd(void) { faction *f; test_setup(); + mt_create_error(66); + mt_create_error(30); mt_create_va(mt_new("regionmessage", NULL), "region:region", "sender:unit", "string:string", MT_NEW_END); mt_create_va(mt_new("unitmessage", NULL), "region:region", "sender:unit", "string:string", "unit:unit", MT_NEW_END); mt_create_va(mt_new("mail_result", NULL), "message:string", "unit:unit", MT_NEW_END); @@ -1433,6 +1445,8 @@ static void test_show_without_item(CuTest *tc) struct locale *loc; test_setup(); + mt_create_error(21); + mt_create_error(36); mt_create_va(mt_new("displayitem", NULL), "weight:int", "item:resource", "description:string", MT_NEW_END); loc = get_or_create_locale("de"); @@ -1477,7 +1491,6 @@ static void test_show_race(CuTest *tc) { message * msg; test_setup(); - mt_create_va(mt_new("msg_event", NULL), "string:string", MT_NEW_END); test_create_race("human"); rc = test_create_race("elf"); @@ -1493,8 +1506,8 @@ static void test_show_race(CuTest *tc) { ord = create_order(K_RESHOW, loc, "Mensch"); reshow_cmd(u, ord); - CuAssertTrue(tc, test_find_messagetype(u->faction->msgs, "error21") != NULL); - CuAssertTrue(tc, test_find_messagetype(u->faction->msgs, "msg_event") == NULL); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error21")); + CuAssertPtrEquals(tc, NULL, test_find_messagetype(u->faction->msgs, "msg_event")); test_clear_messages(u->faction); free_order(ord); diff --git a/src/monsters.c b/src/monsters.c index 9f17db54d..0d383ac05 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -557,7 +557,7 @@ static order *monster_learn(unit * u) const struct locale *lang = u->faction->locale; /* can these monsters even study? */ - if (!unit_can_study(u)) { + if (!check_student(u, NULL, SK_PERCEPTION)) { return NULL; } @@ -832,8 +832,7 @@ void plan_monsters(faction * f) } } } - - if (long_order == NULL && unit_can_study(u)) { + if (long_order == NULL && check_student(u, NULL, SK_WEAPONLESS)) { /* Einheiten, die Waffenlosen Kampf lernen k�nnten, lernen es um * zu bewachen: */ if (rc->bonus[SK_WEAPONLESS] != -99) { diff --git a/src/piracy.test.c b/src/piracy.test.c index 7aa57bc2d..74e79103d 100644 --- a/src/piracy.test.c +++ b/src/piracy.test.c @@ -30,6 +30,8 @@ static void setup_piracy(void) { st_boat = test_create_shiptype("boat"); st_boat->cargo = 1000; + mt_create_error(144); + mt_create_error(146); mt_create_va(mt_new("piratenovictim", NULL), "ship:ship", "unit:unit", "region:region", MT_NEW_END); mt_create_va(mt_new("piratesawvictim", NULL), diff --git a/src/renumber.test.c b/src/renumber.test.c index 8d44b92da..e5f9c5c33 100644 --- a/src/renumber.test.c +++ b/src/renumber.test.c @@ -15,12 +15,19 @@ #include #include +static void setup_renumber(CuTest *tc) { + test_setup_ex(tc); + mt_create_error(114); + mt_create_error(115); + mt_create_error(116); +} + static void test_renumber_faction(CuTest *tc) { unit *u; int uno, no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); no = u->faction->no; uno = (no > 1) ? no - 1 : no + 1; @@ -38,7 +45,7 @@ static void test_renumber_faction_duplicate(CuTest *tc) { int no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); mt_create_va(mt_new("renumber_inuse", NULL), "id:int", MT_NEW_END); f2 = test_create_faction(NULL); u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); @@ -58,7 +65,7 @@ static void test_renumber_faction_invalid(CuTest *tc) { int no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); no = f->no; lang = f->locale; @@ -88,7 +95,7 @@ static void test_renumber_building(CuTest *tc) { int uno, no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); u->building = test_create_building(u->region, NULL); no = u->building->no; @@ -106,7 +113,7 @@ static void test_renumber_building_duplicate(CuTest *tc) { int uno, no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); u->building = test_create_building(u->region, NULL); uno = u->building->no; @@ -125,7 +132,7 @@ static void test_renumber_ship(CuTest *tc) { int uno, no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); u->ship = test_create_ship(u->region, NULL); no = u->ship->no; @@ -142,7 +149,7 @@ static void test_renumber_ship_twice(CuTest *tc) { int uno, no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); u->ship = test_create_ship(u->region, NULL); no = u->ship->no; @@ -164,7 +171,7 @@ static void test_renumber_ship_duplicate(CuTest *tc) { int uno, no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); u->ship = test_create_ship(u->region, NULL); uno = u->ship->no; @@ -183,7 +190,7 @@ static void test_renumber_unit(CuTest *tc) { int uno, no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); no = u->no; uno = (no > 1) ? no - 1 : no + 1; @@ -201,7 +208,7 @@ static void test_renumber_unit_duplicate(CuTest *tc) { int no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); no = u->no; u2 = test_create_unit(f, u->region); @@ -220,7 +227,7 @@ static void test_renumber_unit_limit(CuTest *tc) { int no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); no = u->no; lang = f->locale; @@ -238,7 +245,7 @@ static void test_renumber_unit_invalid(CuTest *tc) { int no; const struct locale *lang; - test_setup_ex(tc); + setup_renumber(tc); u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL)); no = u->no; lang = f->locale; diff --git a/src/report.c b/src/report.c index a897f29ad..3e896a934 100644 --- a/src/report.c +++ b/src/report.c @@ -902,6 +902,7 @@ void report_region(struct stream *out, const region * r, faction * f) assert(f); assert(r); + memset(edges, 0, sizeof(edges)); for (d = 0; d != MAXDIRECTIONS; d++) { /* Nachbarregionen, die gesehen werden, ermitteln */ region *r2 = rconnect(r, d); diff --git a/src/reports.test.c b/src/reports.test.c index 8bcc457b9..247676451 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -789,7 +789,6 @@ static void test_insect_warnings(CuTest *tc) { test_setup(); test_create_calendar(); - test_inject_messagetypes(); f = test_create_faction(test_create_race("insect")); CuAssertIntEquals(tc, SEASON_AUTUMN, get_gamedate(1083, &gd)->season); @@ -817,7 +816,6 @@ static void test_newbie_warning(CuTest *tc) { faction *f; test_setup(); - test_inject_messagetypes(); f = test_create_faction(NULL); config_set_int("NewbieImmunity", 3); diff --git a/src/skill.c b/src/skill.c index 3d91a2598..abeecca04 100644 --- a/src/skill.c +++ b/src/skill.c @@ -118,7 +118,9 @@ skill_t get_skill(const char *s, const struct locale * lang) int skill_cost(skill_t sk) { static int config; static int costs[MAXSKILLS]; - int cost; + int cost = -1; + + assert(sk >= 0 && sk < MAXSKILLS); switch (sk) { case SK_SPY: cost = 100; @@ -148,5 +150,6 @@ int skill_cost(skill_t sk) { } bool expensive_skill(skill_t sk) { + assert(sk >= 0 && sk < MAXSKILLS); return (sk == SK_MAGIC) || skill_cost(sk) > 0; } diff --git a/src/spells.test.c b/src/spells.test.c index 5950fab16..6ee4a305c 100644 --- a/src/spells.test.c +++ b/src/spells.test.c @@ -27,10 +27,6 @@ #include #include -static void setup_spells(void) { - test_inject_messagetypes(); -} - static void test_good_dreams(CuTest *tc) { struct region *r; struct faction *f1, *f2; @@ -40,7 +36,6 @@ static void test_good_dreams(CuTest *tc) { curse *curse; test_setup(); - setup_spells(); test_create_world(); r = findregion(0, 0); f1 = test_create_faction(NULL); @@ -70,7 +65,6 @@ static void test_dreams(CuTest *tc) { castorder co; test_setup(); - setup_spells(); r = test_create_region(0, 0, NULL); f1 = test_create_faction(NULL); f2 = test_create_faction(NULL); @@ -98,7 +92,6 @@ static void test_bad_dreams(CuTest *tc) { curse *curse; test_setup(); - setup_spells(); test_create_world(); r = findregion(0, 0); f1 = test_create_faction(NULL); @@ -129,7 +122,6 @@ static void test_view_reality(CuTest *tc) { castorder co; test_setup(); - setup_spells(); mt_create_va(mt_new("spell_astral_only", NULL), "unit:unit", "region:region", "command:order", MT_NEW_END); mt_create_va(mt_new("viewreality_effect", NULL), @@ -165,7 +157,6 @@ static void test_watch_region(CuTest *tc) { region *r; faction *f; test_setup(); - setup_spells(); r = test_create_region(0, 0, NULL); f = test_create_faction(NULL); CuAssertIntEquals(tc, -1, get_observer(r, f)); diff --git a/src/study.c b/src/study.c index ff961dc13..5ac5ac7ff 100644 --- a/src/study.c +++ b/src/study.c @@ -524,6 +524,48 @@ static void msg_teachers(struct selist *teachers, struct unit *u, skill_t sk) { selist_foreach_ex(teachers, cb_msg_teach, &cbdata); } +bool check_student(const struct unit *u, struct order *ord, skill_t sk) { + int err = 0; + + if (sk < 0) { + err = 77; + } + /* Hack: Talente mit Malus -99 koennen nicht gelernt werden */ + else if (u_race(u)->bonus[sk] == -99) { + err = 771; + } + else { + static int config; + static bool learn_newskills; + + if (config_changed(&config)) { + learn_newskills = config_get_int("study.newskills", 1) != 0; + } + if (!learn_newskills) { + skill *sv = unit_skill(u, sk); + if (sv == NULL) { + /* we can only learn skills we already have */ + err = 771; + } + } + } + if (err) { + if (ord) { + cmistake(u, ord, err, MSG_EVENT); + } + return false; + } + + if ((u_race(u)->flags & RCF_NOLEARN) || fval(u, UFL_WERE)) { + if (ord) { + ADDMSG(&u->faction->msgs, + msg_feedback(u, ord, "error_race_nolearn", "race", u_race(u))); + } + return false; + } + return true; +} + int study_cmd(unit * u, order * ord) { region *r = u->region; @@ -537,7 +579,6 @@ int study_cmd(unit * u, order * ord) skill_t sk; int maxalchemy = 0; int speed_rule = (study_rule_t)config_get_int("study.speedup", 0); - bool learn_newskills = config_get_int("study.newskills", 1) != 0; static const race *rc_snotling; static int rc_cache; @@ -545,32 +586,12 @@ int study_cmd(unit * u, order * ord) rc_snotling = get_race(RC_SNOTLING); } - if (!unit_can_study(u)) { - ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_race_nolearn", "race", - u_race(u))); - return -1; - } - (void)init_order(ord, u->faction->locale); sk = getskill(u->faction->locale); - if (sk < 0) { - cmistake(u, ord, 77, MSG_EVENT); + if (!check_student(u, ord, sk)) { 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 -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 -1; - } - } /* snotlings koennen Talente nur bis T8 lernen */ if (u_race(u) == rc_snotling) { diff --git a/src/study.h b/src/study.h index 686677064..c1d44ed7b 100644 --- a/src/study.h +++ b/src/study.h @@ -48,6 +48,7 @@ extern "C" { skill_t getskill(const struct locale *lang); bool is_migrant(struct unit *u); int study_cost(struct unit *u, skill_t sk); + bool check_student(const struct unit *u, struct order *ord, skill_t sk); typedef void(*learn_fun)(struct unit *u, skill_t sk, int days); diff --git a/src/study.test.c b/src/study.test.c index 420608e5c..b2ac39172 100644 --- a/src/study.test.c +++ b/src/study.test.c @@ -58,6 +58,11 @@ typedef struct { } study_fixture; static void setup_study(void) { + test_setup(); + mt_create_error(178); + mt_create_error(65); + mt_create_va(mt_new("teach_asgood", NULL), + "unit:unit", "region:region", "command:order", "student:unit", MT_NEW_END); mt_create_va(mt_new("studycost", NULL), "unit:unit", "region:region", "cost:int", "skill:int", MT_NEW_END); mt_create_va(mt_new("teach_teacher", NULL), @@ -81,7 +86,7 @@ static void setup_teacher(study_fixture *fix, skill_t sk) { struct locale *lang; assert(fix); - test_setup(); + setup_study(); config_set("study.random_progress", "0"); r = test_create_region(0, 0, NULL); f = test_create_faction(NULL); @@ -148,7 +153,7 @@ static void test_study_bug_2194(CuTest *tc) { struct locale * loc; building * b; - test_setup(); + setup_study(); random_source_inject_constant(0.0); init_resources(); loc = test_create_locale(); @@ -205,7 +210,7 @@ static void test_produceexp(CuTest *tc) { unit *u; g_tc = tc; - test_setup(); + setup_study(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); scale_number(u, 2); config_set("study.produceexp", "20"); @@ -220,7 +225,7 @@ static void test_academy_building(CuTest *tc) { building * b; message * msg; - test_setup(); + setup_study(); mt_create_va(mt_new("teach_asgood", NULL), "unit:unit", "region:region", "command:order", "student:unit", MT_NEW_END); @@ -271,7 +276,6 @@ static void test_academy_bonus(CuTest *tc) { struct locale * loc; building * b; - test_setup(); setup_study(); random_source_inject_constant(0.0); @@ -328,7 +332,8 @@ static void test_academy_bonus(CuTest *tc) { void test_learn_skill_single(CuTest *tc) { unit *u; skill *sv; - test_setup(); + + setup_study(); config_set("study.random_progress", "0"); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); learn_skill(u, SK_ALCHEMY, STUDYDAYS); @@ -347,7 +352,8 @@ void test_learn_skill_single(CuTest *tc) { void test_learn_skill_multi(CuTest *tc) { unit *u; skill *sv; - test_setup(); + + setup_study(); config_set("study.random_progress", "0"); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); scale_number(u, 10); @@ -367,7 +373,8 @@ void test_learn_skill_multi(CuTest *tc) { static void test_demon_skillchanges(CuTest *tc) { unit * u; const race * rc; - test_setup(); + + setup_study(); rc = test_create_race("demon"); CuAssertPtrEquals(tc, (void *)rc, (void *)get_race(RC_DAEMON)); u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, NULL)); @@ -380,7 +387,8 @@ static void test_demon_skillchanges(CuTest *tc) { static void test_study_cmd(CuTest *tc) { unit *u; - test_setup(); + + setup_study(); init_resources(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); u->thisorder = create_order(K_STUDY, u->faction->locale, "CROSSBOW"); @@ -399,7 +407,6 @@ static void test_study_magic(CuTest *tc) { const struct locale *lang; const struct item_type *itype; - test_setup(); setup_study(); init_resources(); f = test_create_faction(NULL); @@ -432,7 +439,6 @@ static void test_study_magic(CuTest *tc) { static void test_study_cost_magic(CuTest *tc) { unit * u; - test_setup(); setup_study(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); @@ -456,7 +462,6 @@ static void test_study_cost(CuTest *tc) { unit *u; const struct item_type *itype; - test_setup(); setup_study(); itype = test_create_silver(); @@ -484,7 +489,6 @@ static void test_teach_magic(CuTest *tc) { faction *f; const struct item_type *itype; - test_setup(); setup_study(); init_resources(); itype = get_resourcetype(R_SILVER)->itype; @@ -510,7 +514,8 @@ static void test_teach_magic(CuTest *tc) { static void test_teach_cmd(CuTest *tc) { unit *u, *ut; - test_setup(); + + setup_study(); init_resources(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); scale_number(u, 10); @@ -530,7 +535,8 @@ static void test_teach_cmd(CuTest *tc) { static void test_teach_two(CuTest *tc) { unit *u1, *u2, *ut; - test_setup(); + + setup_study(); init_resources(); u1 = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); scale_number(u1, 5); @@ -560,7 +566,7 @@ static void test_teach_two_skills(CuTest *tc) { faction *f; region *r; - test_setup(); + setup_study(); init_resources(); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); @@ -590,7 +596,8 @@ static void test_teach_two_skills(CuTest *tc) { static void test_teach_one_to_many(CuTest *tc) { unit *u, *ut; - test_setup(); + + setup_study(); init_resources(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); scale_number(u, 20); @@ -611,7 +618,7 @@ static void test_teach_one_to_many(CuTest *tc) { static void test_teach_many_to_one(CuTest *tc) { unit *u, *u1, *u2; - test_setup(); + setup_study(); init_resources(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); scale_number(u, 20); @@ -638,11 +645,7 @@ static void test_teach_message(CuTest *tc) { attrib *a; teaching_info *teach; - test_setup(); - mt_create_va(mt_new("teach_teacher", NULL), - "teacher:unit", "student:unit", "skill:int", "level:int", MT_NEW_END); - mt_create_va(mt_new("teach_student", NULL), - "teacher:unit", "student:unit", "skill:int", MT_NEW_END); + setup_study(); init_resources(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); scale_number(u, 20); @@ -681,7 +684,7 @@ static void test_teach_many_to_many(CuTest *tc) { region *r; faction *f; - test_setup(); + setup_study(); init_resources(); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); diff --git a/src/tests.c b/src/tests.c index 3a8c9e095..22c18ef9c 100644 --- a/src/tests.c +++ b/src/tests.c @@ -305,15 +305,10 @@ void test_create_calendar(void) { month_season[8] = SEASON_SUMMER; } -void test_inject_messagetypes(void) -{ - message_handle_missing(MESSAGE_MISSING_REPLACE); -} - void test_setup_test(CuTest *tc, const char *file, int line) { test_log_stderr(LOG_CPERROR); test_reset(); - message_handle_missing(MESSAGE_MISSING_ERROR); + message_handle_missing(MESSAGE_MISSING_REPLACE); if (tc) { log_debug("start test: %s", tc->name); } @@ -325,6 +320,7 @@ void test_setup_test(CuTest *tc, const char *file, int line) { void test_teardown(void) { + message_handle_missing(MESSAGE_MISSING_IGNORE); test_reset(); test_log_stderr(0); } diff --git a/src/tests.h b/src/tests.h index 0f3af1972..17da12d4e 100644 --- a/src/tests.h +++ b/src/tests.h @@ -76,7 +76,6 @@ extern "C" { void assert_string_parameter(struct CuTest * tc, struct message *msg, int index, const char *arg); void disabled_test(void *suite, void (*)(struct CuTest *), const char *name); - void test_inject_messagetypes(void); #define DISABLE_TEST(SUITE, TEST) disabled_test(SUITE, TEST, #TEST) diff --git a/src/util/message.c b/src/util/message.c index d8c66f5e0..47a8eb7af 100644 --- a/src/util/message.c +++ b/src/util/message.c @@ -192,6 +192,16 @@ message_type *mt_create_va(message_type *mtype, ...) return mt_create(mtype, args, i - 1); } +message_type *mt_create_feedback(const char *name) { + return mt_create_va(mt_new(name, NULL), "unit:unit", "region:region", "command:order", MT_NEW_END); +} + +message_type *mt_create_error(int error) { + char name[16]; + snprintf(name, sizeof(name), "error%d", error); + return mt_create_feedback(name); +} + static variant copy_arg(const arg_type * atype, variant data) { assert(atype != NULL); diff --git a/src/util/message.h b/src/util/message.h index 5aa16232b..52b087203 100644 --- a/src/util/message.h +++ b/src/util/message.h @@ -64,6 +64,8 @@ extern "C" { struct message_type *mt_new(const char *name, const char *section); /** message_type registry (optional): **/ struct message_type *mt_create(struct message_type *, const char *args[], int nargs); + struct message_type *mt_create_feedback(const char *name); + struct message_type *mt_create_error(int error); struct message_type *mt_create_va(struct message_type *, ...); const struct message_type *mt_find(const char *);