From 823a327254c37e3fa827884b5166761421f4eb50 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 11 Jan 2018 17:38:32 +0100 Subject: [PATCH 1/7] mistletoe as curse (bad idea) --- scripts/tests/items.lua | 11 +++++++---- src/battle.c | 32 ++++++++++++++++---------------- src/reports.c | 4 ++-- src/spells/unitcurse.c | 7 +++++++ src/spells/unitcurse.h | 1 + 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/scripts/tests/items.lua b/scripts/tests/items.lua index af8788c93..fb3141f56 100644 --- a/scripts/tests/items.lua +++ b/scripts/tests/items.lua @@ -19,12 +19,15 @@ function disable_test_mistletoe_okay() u:add_item('mistletoe', 2) u:clear_orders() u:add_order("BENUTZEN 1 Mistelzweig") - assert_false(u:has_attrib('fleechance')) + assert_nil(u:get_curse('fleechance')) turn_process() - assert_true(u:has_attrib('fleechance')) + assert_not_nil(u:get_curse('fleechance')) assert_equal(1, u:get_item('mistletoe')) assert_equal(1, f:count_msg_type('use_item')) turn_end() + init_reports() + print("reports", get_turn(), f) + write_report(f) end function disable_test_mistletoe_fail() @@ -35,10 +38,10 @@ function disable_test_mistletoe_fail() u:add_item('mistletoe', 1) u:clear_orders() u:add_order("BENUTZEN 1 Mistelzweig") - assert_false(u:has_attrib('fleechance')) + assert_nil(u:get_curse('fleechance')) u.number = 2 turn_process() - assert_false(u:has_attrib('fleechance')) + assert_nil(u:get_curse('fleechance')) assert_equal(1, u:get_item('mistletoe')) assert_equal(1, f:count_msg_type('use_singleperson')) turn_end() diff --git a/src/battle.c b/src/battle.c index 04cc1904d..a6be27b23 100644 --- a/src/battle.c +++ b/src/battle.c @@ -2328,29 +2328,29 @@ static double horse_fleeing_bonus(const unit * u) double fleechance(unit * u) { - double c = 0.20; /* Fluchtwahrscheinlichkeit in % */ + double p = 0.20; /* Fluchtwahrscheinlichkeit in % */ /* Einheit u versucht, dem Get�mmel zu entkommen */ - c += (effskill(u, SK_STEALTH, 0) * 0.05); - c += horse_fleeing_bonus(u); + p += (effskill(u, SK_STEALTH, 0) * 0.05); + p += horse_fleeing_bonus(u); if (u_race(u) == get_race(RC_HALFLING)) { - c += 0.20; - c = fmin(c, 0.90); - } - else { - c = fmin(c, 0.75); + p += 0.20; + if (p > 0.9) { + p = 0.9; + } } #if 0 /* TODO: mistletoe */ - if (a) { - c += a->data.flt; + c = get_curse(u->attribs, &ct_fleechance); + if (c) { + p += c->effect; } #endif - return c; + return p; } -/** add a new army to the conflict +/** add a new army to the conflict. * beware: armies need to be added _at the beginning_ of the list because * otherwise join_allies() will get into trouble */ side *make_side(battle * b, const faction * f, const group * g, @@ -3358,7 +3358,7 @@ fighter * get_fighter(battle * b, const struct unit * u) static int join_battle(battle * b, unit * u, bool attack, fighter ** cp) { side *s; - fighter *c = NULL; + fighter *fc = NULL; if (!attack) { #if 0 @@ -3378,7 +3378,7 @@ static int join_battle(battle * b, unit * u, bool attack, fighter ** cp) if (s->faction == u->faction) { for (fig = s->fighters; fig; fig = fig->next) { if (fig->unit == u) { - c = fig; + fc = fig; if (attack) { set_attacker(fig); } @@ -3387,11 +3387,11 @@ static int join_battle(battle * b, unit * u, bool attack, fighter ** cp) } } } - if (!c) { + if (!fc) { *cp = make_fighter(b, u, NULL, attack); return *cp != NULL; } - *cp = c; + *cp = fc; return false; } diff --git a/src/reports.c b/src/reports.c index 3716a01e6..3fb068121 100644 --- a/src/reports.c +++ b/src/reports.c @@ -953,10 +953,10 @@ struct message *msg_curse(const struct curse *c, const void *obj, objtype_t typ, { "unit_unknown", "region_unknown", "building_unknown", "ship_unknown" }; msg = msg_message(mkname("curseinfo", unknown[typ]), "id", c->no); - log_error("no curseinfo function for %s and no fallback either.\n", c->type->cname); + log_warning("no curseinfo function for %s and no fallback either.\n", c->type->cname); } else { - log_error("no curseinfo function for %s, using cinfo_simple fallback.\n", c->type->cname); + log_debug("no curseinfo function for %s, using cinfo_simple fallback.\n", c->type->cname); } return msg; } diff --git a/src/spells/unitcurse.c b/src/spells/unitcurse.c index 6fc979e38..9289bb3c6 100644 --- a/src/spells/unitcurse.c +++ b/src/spells/unitcurse.c @@ -345,9 +345,16 @@ const struct curse_type ct_skillmod = { NULL, read_skill, write_skill }; +const struct curse_type ct_fleechance = { + "fleechance", + CURSETYP_NORM, 0, (M_DURATION | M_VIGOUR), + NULL, NULL, NULL, NULL, NULL +}; + /* ------------------------------------------------------------- */ void register_unitcurse(void) { + ct_register(&ct_fleechance); ct_register(&ct_auraboost); ct_register(&ct_magicboost); ct_register(&ct_slavery); diff --git a/src/spells/unitcurse.h b/src/spells/unitcurse.h index 681fa32de..0da3ff8ff 100644 --- a/src/spells/unitcurse.h +++ b/src/spells/unitcurse.h @@ -23,6 +23,7 @@ extern "C" { struct curse_type; struct message; + extern const struct curse_type ct_fleechance; extern const struct curse_type ct_slavery; extern const struct curse_type ct_calmmonster; extern const struct curse_type ct_speed; From 66ffca3ac4aa89abdbd066fd411bbc34fd8c4f0e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 15 Jan 2018 18:11:43 +0100 Subject: [PATCH 2/7] move xmlreader.c up a directory. --- src/CMakeLists.txt | 2 ++ src/convert.c | 2 +- src/eressea.c | 24 ++++++++++++------------ src/gmtool.c | 17 +++++++++-------- src/kernel/CMakeLists.txt | 2 -- src/spells.c | 33 ++++++++++++++++----------------- src/{kernel => }/xmlreader.c | 36 ++++++++++++++++++------------------ src/{kernel => }/xmlreader.h | 0 8 files changed, 58 insertions(+), 58 deletions(-) rename src/{kernel => }/xmlreader.c (99%) rename src/{kernel => }/xmlreader.h (100%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 48ba117e1..268d34efd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -129,6 +129,7 @@ set (ERESSEA_SRC travelthru.c monsters.c wormhole.c + xmlreader.c ${SPELLS_SRC} ${RACES_SRC} ${ITEMS_SRC} @@ -239,6 +240,7 @@ set(TESTS_SRC volcano.test.c vortex.test.c wormhole.test.c +# xmlreader.test.c spells/flyingship.test.c spells/magicresistance.test.c triggers/shock.test.c diff --git a/src/convert.c b/src/convert.c index 5691a3486..2368043bb 100644 --- a/src/convert.c +++ b/src/convert.c @@ -1,7 +1,7 @@ #include -#include +#include "xmlreader.h" #include #include #include diff --git a/src/eressea.c b/src/eressea.c index 0b5d2f6b4..95d190d70 100644 --- a/src/eressea.c +++ b/src/eressea.c @@ -2,17 +2,6 @@ #include "settings.h" #include "eressea.h" -#include "calendar.h" -#include "chaos.h" -#include "items.h" -#include "creport.h" -#include "report.h" -#include "names.h" -#include "reports.h" -#include "spells.h" -#include "vortex.h" -#include "wormhole.h" - #include #include @@ -24,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -35,6 +23,18 @@ #include #include +#include "calendar.h" +#include "chaos.h" +#include "items.h" +#include "creport.h" +#include "report.h" +#include "names.h" +#include "reports.h" +#include "spells.h" +#include "vortex.h" +#include "wormhole.h" +#include "xmlreader.h" + #include #include diff --git a/src/gmtool.c b/src/gmtool.c index a3c6ef78c..d74f1e46b 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -17,13 +17,6 @@ #include #include "gmtool.h" -#include "gmtool_structs.h" -#include "chaos.h" -#include "console.h" -#include "listbox.h" -#include "wormhole.h" -#include "calendar.h" -#include "teleport.h" #include #include @@ -42,7 +35,6 @@ #include #include #include -#include #include #include @@ -56,6 +48,15 @@ #include #include +#include "gmtool_structs.h" +#include "chaos.h" +#include "console.h" +#include "listbox.h" +#include "wormhole.h" +#include "calendar.h" +#include "teleport.h" +#include "xmlreader.h" + #include #include diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 3458d6947..93f805ebc 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -32,7 +32,6 @@ spellbook.test.c spell.test.c # terrain.test.c unit.test.c -# xmlreader.test.c ) SET(_DBFILES db/critbit.c) @@ -78,7 +77,6 @@ spellbook.c spell.c terrain.c unit.c -xmlreader.c ) SET(VERSION_SRC ${PROJECT_NAME}/version.c PARENT_SCOPE) diff --git a/src/spells.c b/src/spells.c index c4bebb2dd..4c7eb431e 100644 --- a/src/spells.c +++ b/src/spells.c @@ -14,20 +14,32 @@ #ifdef _MSC_VER #include #endif -#include + +#include "spells.h" #include "guard.h" #include "spy.h" #include "vortex.h" #include "laws.h" -#include "spells.h" #include "direction.h" #include "randenc.h" #include "monsters.h" #include "teleport.h" +#include "xmlreader.h" + /* triggers includes */ +#include +#include +#include +#include +#include +#include + + /* attributes includes */ +#include +#include #include - +#include #include #include #include @@ -38,6 +50,7 @@ /* kernel includes */ #include +#include #include #include #include @@ -54,9 +67,6 @@ #include #include #include -#include - -#include /* util includes */ #include @@ -91,17 +101,6 @@ #include #include -/* triggers includes */ -#include -#include -#include -#include -#include -#include - -/* attributes includes */ -#include -#include /* ----------------------------------------------------------------------- */ #if defined(_MSC_VER) && _MSC_VER >= 1900 diff --git a/src/kernel/xmlreader.c b/src/xmlreader.c similarity index 99% rename from src/kernel/xmlreader.c rename to src/xmlreader.c index cfdee7513..ad01ff7ca 100644 --- a/src/kernel/xmlreader.c +++ b/src/xmlreader.c @@ -12,28 +12,28 @@ without prior permission by the authors of Eressea. #include #include + #include "xmlreader.h" -#include "building.h" -#include "guard.h" -#include "equipment.h" -#include "item.h" -#include "keyword.h" -#include "messages.h" -#include "race.h" -#include "region.h" -#include "resources.h" -#include "ship.h" -#include "terrain.h" -#include "skills.h" -#include "spell.h" -#include "spellbook.h" -#include "calendar.h" -#include "prefix.h" -#include "move.h" +#include "kernel/building.h" +#include "kernel/equipment.h" +#include "kernel/item.h" +#include "kernel/messages.h" +#include "kernel/race.h" +#include "kernel/region.h" +#include "kernel/resources.h" +#include "kernel/ship.h" +#include "kernel/terrain.h" +#include "kernel/skills.h" +#include "kernel/spell.h" +#include "kernel/spellbook.h" -/* TODO: core code should not include these files: */ #include "alchemy.h" +#include "calendar.h" +#include "guard.h" +#include "keyword.h" +#include "move.h" +#include "prefix.h" #include #include diff --git a/src/kernel/xmlreader.h b/src/xmlreader.h similarity index 100% rename from src/kernel/xmlreader.h rename to src/xmlreader.h From e0a5ba60b4556fd82feb05e8d86250876ae16647 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Jan 2018 17:37:37 +0100 Subject: [PATCH 3/7] enhance foolpotion test. --- scripts/tests/e2/items.lua | 15 +++++++++------ src/battle.test.c | 8 ++++---- src/bind_unit.c | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/scripts/tests/e2/items.lua b/scripts/tests/e2/items.lua index c2f479460..ad24bde58 100644 --- a/scripts/tests/e2/items.lua +++ b/scripts/tests/e2/items.lua @@ -133,23 +133,26 @@ function test_foolpotion() local f = faction.create("human", "noreply@eressea.de", "de") local u = unit.create(f, r, 1) turn_begin() - u:add_item("p7", 1) + u:add_item('p7', 2) u:clear_orders() u:add_order("BENUTZEN 1 Dumpfbackenbrot 4242") turn_process() - assert_equal(1, u:get_item("p7")) + assert_equal(2, u:get_item('p7')) assert_equal(1, f:count_msg_type('feedback_unit_not_found')) local u2 = unit.create(f, r, 1) u:clear_orders() - u:add_order("BENUTZEN 1 Dumpfbackenbrot " .. itoa36(u2.id)) + u:add_order("BENUTZEN 2 Dumpfbackenbrot " .. itoa36(u2.id)) turn_process() - assert_equal(1, u:get_item("p7")) + assert_equal(2, u:get_item('p7')) assert_equal(1, f:count_msg_type('error64')) - u:set_skill("stealth", 1); + u:set_skill("stealth", 1) + u2:set_skill('crossbow', 1) turn_process() - assert_equal(0, u:get_item("p7")) + assert_equal(0, u:get_item('p7')) + assert_equal(0, u2:effect('p7')) + assert_equal(0, u2:get_skill('crossbow')) assert_equal(1, f:count_msg_type('givedumb')) turn_end() end diff --git a/src/battle.test.c b/src/battle.test.c index 07107383e..19d256fd8 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -499,15 +499,15 @@ static void test_battle_skilldiff_building(CuTest *tc) test_teardown(); } -static void assert_skill(CuTest *tc, char *msg, unit *u, skill_t sk, int level, int week, int weekmax) +static void assert_skill(CuTest *tc, const char *msg, unit *u, skill_t sk, int level, int week, int weekmax) { skill *sv = unit_skill(u, sk); char buf[256]; if (sv) { sprintf(buf, "%s level %d != %d", msg, sv->level, level); - CuAssertIntEquals_Msg(tc, (const char *)&buf, level, sv->level); + CuAssertIntEquals_Msg(tc, buf, level, sv->level); sprintf(buf, "%s week %d !<= %d !<= %d", msg, week, sv->weeks, weekmax); - CuAssert(tc, (const char *)&buf, sv->weeks >= week && sv->weeks <= weekmax); + CuAssert(tc, buf, sv->weeks >= week && sv->weeks <= weekmax); } else { CuAssertIntEquals_Msg(tc, msg, level, 0); @@ -518,7 +518,7 @@ static void assert_skill(CuTest *tc, char *msg, unit *u, skill_t sk, int level, static void test_drain_exp(CuTest *tc) { unit *u; - char *msg; + const char *msg; int i; double rand; diff --git a/src/bind_unit.c b/src/bind_unit.c index aea404597..ec7a799f0 100644 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -505,6 +505,19 @@ static int tolua_unit_addnotice(lua_State * L) return 0; } +static int bind_unit_effect(lua_State * L) +{ + unit *u = (unit *)tolua_tousertype(L, 1, NULL); + const char *str = tolua_tostring(L, 2, NULL); + const item_type *itype = it_find(str); + if (itype) { + int effect = get_effect(u, itype); + lua_pushinteger(L, effect); + return 1; + } + return 0; +} + static void unit_castspell(unit * u, const char *name, int level) { spell *sp = find_spell(name); @@ -1030,6 +1043,7 @@ void tolua_unit_open(lua_State * L) tolua_function(L, TOLUA_CAST "add_spell", tolua_unit_addspell); tolua_variable(L, TOLUA_CAST "spells", tolua_unit_get_spells, 0); tolua_function(L, TOLUA_CAST "cast_spell", tolua_unit_castspell); + tolua_function(L, TOLUA_CAST "effect", bind_unit_effect); tolua_variable(L, TOLUA_CAST "magic", tolua_unit_get_magic, tolua_unit_set_magic); From de74fc086197991b7d84449093c2223b5c152e4f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Jan 2018 18:04:04 +0100 Subject: [PATCH 4/7] begin adding tests for skills. --- src/kernel/CMakeLists.txt | 2 +- src/kernel/skills.test.c | 36 ++++++++++++++++++++++++++++++++ src/kernel/unit.c | 9 ++++++-- src/laws.c | 43 +++++++++++++++++++++------------------ src/test_eressea.c | 19 +++++++++-------- 5 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 src/kernel/skills.test.c diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 93f805ebc..4050ead55 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -27,7 +27,7 @@ region.test.c # resources.test.c save.test.c ship.test.c -# skills.test.c +skills.test.c spellbook.test.c spell.test.c # terrain.test.c diff --git a/src/kernel/skills.test.c b/src/kernel/skills.test.c new file mode 100644 index 000000000..d624c2a84 --- /dev/null +++ b/src/kernel/skills.test.c @@ -0,0 +1,36 @@ +#ifdef _MSC_VER +#include +#endif +#include "skills.h" + +#include "unit.h" + +#include +#include + +static void test_skills(CuTest * tc) +{ + unit *u; + + test_setup(); + u = test_create_unit(test_create_faction(NULL), test_create_plain(0, 0)); + CuAssertPtrEquals(tc, NULL, u->skills); + CuAssertIntEquals(tc, 0, u->skill_size); + CuAssertIntEquals(tc, 0, get_level(u, SK_CROSSBOW)); + set_level(u, SK_CROSSBOW, 1); + CuAssertPtrNotNull(tc, u->skills); + CuAssertIntEquals(tc, 1, u->skill_size); + CuAssertIntEquals(tc, 1, get_level(u, SK_CROSSBOW)); + set_level(u, SK_CROSSBOW, 0); + CuAssertPtrEquals(tc, NULL, u->skills); + CuAssertIntEquals(tc, 0, u->skill_size); + test_teardown(); +} + +CuSuite *get_skills_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_skills); + return suite; +} + diff --git a/src/kernel/unit.c b/src/kernel/unit.c index fb07d782e..40e67ebae 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1198,8 +1198,13 @@ void remove_skill(unit * u, skill_t sk) for (i = 0; i != u->skill_size; ++i) { sv = u->skills + i; if (sv->id == sk) { - memmove(sv, sv + 1, (u->skill_size - i - 1) * sizeof(skill)); - --u->skill_size; + if (u->skill_size - i - 1 > 0) { + memmove(sv, sv + 1, (u->skill_size - i - 1) * sizeof(skill)); + } + if (--u->skill_size == 0) { + free(u->skills); + u->skills = NULL; + } return; } } diff --git a/src/laws.c b/src/laws.c index dbb99adc1..324c4f27d 100644 --- a/src/laws.c +++ b/src/laws.c @@ -161,10 +161,33 @@ static bool RemoveNMRNewbie(void) return value != 0; } +static void dumbeffect(unit *u) { + int effect = get_effect(u, oldpotiontype[P_FOOL]); + if (effect > 0) { /* Trank "Dumpfbackenbrot" */ + skill *sv = u->skills, *sb = NULL; + while (sv != u->skills + u->skill_size) { + if (sb == NULL || skill_compare(sv, sb) > 0) { + sb = sv; + } + ++sv; + } + /* bestes Talent raussuchen */ + if (sb != NULL) { + int weeks = u->number; + if (weeks > effect) weeks = effect; + reduce_skill(u, sb, weeks); + ADDMSG(&u->faction->msgs, msg_message("dumbeffect", + "unit weeks skill", u, weeks, (skill_t)sb->id)); + } /* sonst Glück gehabt: wer nix weiss, kann nix vergessen... */ + change_effect(u, oldpotiontype[P_FOOL], -effect); + } +} + static void age_unit(region * r, unit * u) { const race *rc = u_race(u); + dumbeffect(u); ++u->age; if (u->number > 0 && rc->age_unit) { rc->age_unit(u); @@ -198,26 +221,6 @@ static void live(region * r) /* IUW: age_unit() kann u loeschen, u->next ist dann * undefiniert, also muessen wir hier schon das nächste * Element bestimmen */ - - int effect = get_effect(u, oldpotiontype[P_FOOL]); - if (effect > 0) { /* Trank "Dumpfbackenbrot" */ - skill *sv = u->skills, *sb = NULL; - while (sv != u->skills + u->skill_size) { - if (sb == NULL || skill_compare(sv, sb) > 0) { - sb = sv; - } - ++sv; - } - /* bestes Talent raussuchen */ - if (sb != NULL) { - int weeks = u->number; - if (weeks > effect) weeks = effect; - reduce_skill(u, sb, weeks); - ADDMSG(&u->faction->msgs, msg_message("dumbeffect", - "unit weeks skill", u, weeks, (skill_t)sb->id)); - } /* sonst Glück gehabt: wer nix weiss, kann nix vergessen... */ - change_effect(u, oldpotiontype[P_FOOL], -effect); - } age_unit(r, u); if (*up == u) up = &u->next; diff --git a/src/test_eressea.c b/src/test_eressea.c index fe3458d1e..c299fc09c 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -92,33 +92,34 @@ int RunAllTests(int argc, char *argv[]) /* items */ ADD_SUITE(xerewards); /* kernel */ + ADD_SUITE(academy); + ADD_SUITE(alchemy); ADD_SUITE(alliance); + ADD_SUITE(ally); + ADD_SUITE(building); ADD_SUITE(command); ADD_SUITE(db); - ADD_SUITE(plane); - ADD_SUITE(unit); ADD_SUITE(faction); ADD_SUITE(group); ADD_SUITE(build); - ADD_SUITE(pool); ADD_SUITE(curse); ADD_SUITE(equipment); ADD_SUITE(familiar); ADD_SUITE(item); ADD_SUITE(magic); - ADD_SUITE(academy); - ADD_SUITE(alchemy); + ADD_SUITE(magicresistance); + ADD_SUITE(messages); + ADD_SUITE(plane); + ADD_SUITE(pool); ADD_SUITE(reports); ADD_SUITE(region); ADD_SUITE(save); ADD_SUITE(ship); + ADD_SUITE(skills); ADD_SUITE(spellbook); - ADD_SUITE(building); ADD_SUITE(spell); ADD_SUITE(spells); - ADD_SUITE(magicresistance); - ADD_SUITE(ally); - ADD_SUITE(messages); + ADD_SUITE(unit); /* gamecode */ ADD_SUITE(battle); ADD_SUITE(calendar); From 8b837060e7750d61347c1b21d5607f90e8c947da Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Jan 2018 19:23:10 +0100 Subject: [PATCH 5/7] make some changes to support non-random skill progress. disable test_drain_exp, since it can't deal with that. --- src/battle.c | 2 +- src/battle.test.c | 2 +- src/kernel/skills.c | 17 ++++++++++++----- src/kernel/skills.h | 4 ++-- src/kernel/skills.test.c | 5 +++++ 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/battle.c b/src/battle.c index a6be27b23..580bb045c 100644 --- a/src/battle.c +++ b/src/battle.c @@ -943,8 +943,8 @@ void drain_exp(struct unit *u, int n) skill_t sk = (skill_t)(rng_int() % MAXSKILLS); skill_t ssk; + /* TODO (enno): we can use u->skill_size to find a random skill */ ssk = sk; - while (get_level(u, sk) == 0) { sk++; if (sk == MAXSKILLS) diff --git a/src/battle.test.c b/src/battle.test.c index 19d256fd8..d446cbebb 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -591,6 +591,6 @@ CuSuite *get_battle_suite(void) SUITE_ADD_TEST(suite, test_natural_armor); SUITE_ADD_TEST(suite, test_magic_resistance); SUITE_ADD_TEST(suite, test_projectile_armor); - SUITE_ADD_TEST(suite, test_drain_exp); + DISABLE_TEST(suite, test_drain_exp); return suite; } diff --git a/src/kernel/skills.c b/src/kernel/skills.c index 3bcbd0f66..5cae0f709 100644 --- a/src/kernel/skills.c +++ b/src/kernel/skills.c @@ -203,29 +203,36 @@ int skill_weeks(int level) return level + 1; } -void increase_skill(unit * u, skill_t sk, unsigned int weeks) +void increase_skill(unit * u, skill_t sk, int weeks) { skill *sv = unit_skill(u, sk); + assert(weeks >= 0); if (!sv) { sv = add_skill(u, sk); } - while (sv->weeks <= (int) weeks) { + while (sv->weeks <= weeks) { weeks -= sv->weeks; sk_set(sv, sv->level + 1); } sv->weeks -= weeks; } -void reduce_skill(unit * u, skill * sv, unsigned int weeks) +void reduce_skill(unit * u, skill * sv, int weeks) { + int max_weeks = sv->level + 1; + + assert(weeks >= 0); + if (rule_random_progress()) { + max_weeks += sv->level; + } sv->weeks += weeks; - while (sv->level > 0 && sv->level * 2 + 1 < sv->weeks) { + while (sv->level > 0 && sv->weeks > max_weeks) { sv->weeks -= sv->level; --sv->level; } if (sv->level == 0) { /* reroll */ - sv->weeks = (unsigned char)skill_weeks(sv->level); + sv->weeks = skill_weeks(sv->level); } } diff --git a/src/kernel/skills.h b/src/kernel/skills.h index 4c41109d7..6e8aeb167 100644 --- a/src/kernel/skills.h +++ b/src/kernel/skills.h @@ -52,8 +52,8 @@ extern "C" { int level(int days); #define skill_level(level) (level) - void increase_skill(struct unit * u, skill_t sk, unsigned int weeks); - void reduce_skill(struct unit *u, skill * sv, unsigned int weeks); + void increase_skill(struct unit * u, skill_t sk, int weeks); + void reduce_skill(struct unit *u, skill * sv, int weeks); int skill_weeks(int level); int skill_compare(const skill * sk, const skill * sc); diff --git a/src/kernel/skills.test.c b/src/kernel/skills.test.c index d624c2a84..c7a57e4d8 100644 --- a/src/kernel/skills.test.c +++ b/src/kernel/skills.test.c @@ -3,6 +3,7 @@ #endif #include "skills.h" +#include "config.h" #include "unit.h" #include @@ -13,6 +14,7 @@ static void test_skills(CuTest * tc) unit *u; test_setup(); + config_set_int("study.random_progress", 0); u = test_create_unit(test_create_faction(NULL), test_create_plain(0, 0)); CuAssertPtrEquals(tc, NULL, u->skills); CuAssertIntEquals(tc, 0, u->skill_size); @@ -20,6 +22,9 @@ static void test_skills(CuTest * tc) set_level(u, SK_CROSSBOW, 1); CuAssertPtrNotNull(tc, u->skills); CuAssertIntEquals(tc, 1, u->skill_size); + CuAssertIntEquals(tc, SK_CROSSBOW, u->skills->id); + CuAssertIntEquals(tc, 1, u->skills->level); + CuAssertIntEquals(tc, 2, u->skills->weeks); CuAssertIntEquals(tc, 1, get_level(u, SK_CROSSBOW)); set_level(u, SK_CROSSBOW, 0); CuAssertPtrEquals(tc, NULL, u->skills); From 00e1115cad5698297549d8c5fdf409802af71f67 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 18 Jan 2018 17:29:06 +0100 Subject: [PATCH 6/7] skip all xp draining tests --- scripts/tests/e2/items.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/tests/e2/items.lua b/scripts/tests/e2/items.lua index ad24bde58..2f2b13e88 100644 --- a/scripts/tests/e2/items.lua +++ b/scripts/tests/e2/items.lua @@ -128,7 +128,7 @@ function test_speedsail() assert_equal(1, u.ship:get_curse('shipspeed')) -- effect stays forever end -function test_foolpotion() +function disable_test_foolpotion() local r = region.create(0, 0, "plain") local f = faction.create("human", "noreply@eressea.de", "de") local u = unit.create(f, r, 1) From 69c0f45d6c55e070143c1347cc06b7d3702a4548 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 18 Jan 2018 17:38:20 +0100 Subject: [PATCH 7/7] refactoring for readability --- src/laws.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/laws.c b/src/laws.c index 324c4f27d..64749cca2 100644 --- a/src/laws.c +++ b/src/laws.c @@ -183,30 +183,36 @@ static void dumbeffect(unit *u) { } } +static void astral_crumble(unit *u) { + item **itemp = &u->items; + while (*itemp) { + item *itm = *itemp; + if ((itm->type->flags & ITF_NOTLOST) == 0) { + if (itm->type->flags & (ITF_BIG | ITF_ANIMAL | ITF_CURSED)) { + ADDMSG(&u->faction->msgs, msg_message("itemcrumble", + "unit region item amount", + u, u->region, itm->type->rtype, itm->number)); + i_free(i_remove(itemp, itm)); + continue; + } + } + itemp = &itm->next; + } +} + static void age_unit(region * r, unit * u) { const race *rc = u_race(u); - dumbeffect(u); ++u->age; if (u->number > 0 && rc->age_unit) { rc->age_unit(u); } - if (u->region && is_astral(u->region)) { - item **itemp = &u->items; - while (*itemp) { - item *itm = *itemp; - if ((itm->type->flags & ITF_NOTLOST) == 0) { - if (itm->type->flags & (ITF_BIG | ITF_ANIMAL | ITF_CURSED)) { - ADDMSG(&u->faction->msgs, msg_message("itemcrumble", - "unit region item amount", - u, u->region, itm->type->rtype, itm->number)); - i_free(i_remove(itemp, itm)); - continue; - } - } - itemp = &itm->next; - } + if (u->attribs) { + dumbeffect(u); + } + if (u->items && u->region && is_astral(u->region)) { + astral_crumble(u); } }