From 6265abac746e27a1fdec3d5abf34483963d796b1 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 6 Oct 2017 22:15:48 +0200 Subject: [PATCH] remove side-effects from movement_speed(). --- src/move.c | 85 +++++++++++++++++++++---------------------------- src/move.h | 9 ++++++ src/move.test.c | 24 ++++++++++++++ src/tests.c | 2 +- 4 files changed, 70 insertions(+), 50 deletions(-) diff --git a/src/move.c b/src/move.c index 2b0e4d8d7..46a8f200f 100644 --- a/src/move.c +++ b/src/move.c @@ -89,14 +89,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -/* Bewegungsweiten: */ -#define BP_WALKING 4 -#define BP_RIDING 6 -#define BP_UNICORN 9 -#define BP_DRAGON 4 -#define BP_NORMAL 3 -#define BP_ROAD 2 - int *storms; typedef struct traveldir { @@ -265,7 +257,7 @@ get_transporters(const item * itm, int *p_animals, int *p_acap, int *p_vehicles, *p_acap = acap; } -static int ridingcapacity(unit * u) +static int ridingcapacity(const unit * u) { int vehicles = 0, vcap = 0; int animals = 0, acap = 0; @@ -433,7 +425,7 @@ bool canswim(unit * u) return false; } -static int canride(unit * u) +static int canride(const unit * u) { int horses = 0, maxhorses, unicorns = 0, maxunicorns; int skill = effskill(u, SK_RIDING, 0); @@ -460,17 +452,17 @@ static int canride(unit * u) if (!(u_race(u)->flags & RCF_HORSE) && ((horses == 0 && unicorns == 0) || horses > maxhorses || unicorns > maxunicorns)) { - return 0; + return BP_WALKING; } if (ridingcapacity(u) - eff_weight(u) >= 0) { if (horses == 0 && unicorns >= u->number && !(u_race(u)->flags & RCF_HORSE)) { - return 2; + return BP_UNICORN; } - return 1; + return BP_RIDING; } - return 0; + return BP_WALKING; } static bool cansail(const region * r, ship * sh) @@ -1399,9 +1391,9 @@ static void make_route(unit * u, order * ord, region_list ** routep) * Normalerweise verliert man 3 BP pro Region, bei Straßen nur 2 BP. * Außerdem: Wenn Einheit transportiert, nur halbe BP */ -static int movement_speed(unit * u) +int movement_speed(const unit * u) { - int mp = BP_WALKING; + int mp = 0; const race *rc = u_race(u); double dk = rc->speed; assert(u->number); @@ -1415,6 +1407,10 @@ static int movement_speed(unit * u) mp = BP_DRAGON; break; default: + mp = canride(u); + if (mp>=BP_RIDING) { + dk = 1.0; + } break; } @@ -1426,38 +1422,21 @@ static int movement_speed(unit * u) } } - switch (canride(u)) { - case 1: /* Pferd */ - mp = BP_RIDING; - break; - - case 2: /* Einhorn */ - mp = BP_UNICORN; - break; - - default: - /* Siebenmeilentee */ - if (get_effect(u, oldpotiontype[P_FAST]) >= u->number) { - mp *= 2; - change_effect(u, oldpotiontype[P_FAST], -u->number); - } - - /* unicorn in inventory */ - if (u->number <= i_get(u->items, it_find("fairyboot"))) { - mp *= 2; - } - - /* Im Astralraum sind Tyb und Ill-Magier doppelt so schnell. - * Nicht kumulativ mit anderen Beschleunigungen! */ - if (mp * dk <= BP_WALKING * u_race(u)->speed && is_astral(u->region) - && is_mage(u)) { - sc_mage *mage = get_mage_depr(u); - if (mage->magietyp == M_TYBIED || mage->magietyp == M_ILLAUN) { - mp *= 2; - } - } - break; + /* unicorn in inventory */ + if (u->number <= i_get(u->items, it_find("fairyboot"))) { + mp *= 2; } + + /* Im Astralraum sind Tyb und Ill-Magier doppelt so schnell. + * Nicht kumulativ mit anderen Beschleunigungen! */ + if (mp * dk <= BP_WALKING * u_race(u)->speed && is_astral(u->region) + && is_mage(u)) { + sc_mage *mage = get_mage_depr(u); + if (mage->magietyp == M_TYBIED || mage->magietyp == M_ILLAUN) { + mp *= 2; + } + } + return (int)(dk * mp); } @@ -2044,7 +2023,7 @@ static const region_list *travel_i(unit * u, const region_list * route_begin, const region_list * route_end, order * ord, int mode, follower ** followers) { region *r = u->region; - + int mp; if (u->building && !can_leave(u)) { cmistake(u, u->thisorder, 150, MSG_MOVE); return route_begin; @@ -2060,7 +2039,15 @@ static const region_list *travel_i(unit * u, const region_list * route_begin, cmistake(u, ord, 42, MSG_MOVE); return route_begin; } - route_end = cap_route(r, route_begin, route_end, movement_speed(u)); + + mp = movement_speed(u); + /* Siebenmeilentee */ + if (get_effect(u, oldpotiontype[P_FAST]) >= u->number) { + mp *= 2; + change_effect(u, oldpotiontype[P_FAST], -u->number); + } + + route_end = cap_route(r, route_begin, route_end, mp); route_end = travel_route(u, route_begin, route_end, ord, mode); if (u->flags&UFL_FOLLOWED) { diff --git a/src/move.h b/src/move.h index d5b07fef9..4b9c75749 100644 --- a/src/move.h +++ b/src/move.h @@ -36,6 +36,14 @@ extern "C" { extern struct attrib_type at_shiptrail; extern int *storms; + /* Bewegungsweiten: */ +#define BP_WALKING 4 +#define BP_RIDING 6 +#define BP_UNICORN 9 +#define BP_DRAGON 4 +#define BP_NORMAL 3 +#define BP_ROAD 2 + /* die Zahlen sind genau äquivalent zu den race Flags */ #define MV_CANNOTMOVE (1<<5) #define MV_FLY (1<<7) /* kann fliegen */ @@ -70,6 +78,7 @@ extern "C" { struct ship *move_ship(struct ship *sh, struct region *from, struct region *to, struct region_list *route); int walkingcapacity(const struct unit *u); + int movement_speed(const struct unit * u); void follow_unit(struct unit *u); struct unit *owner_buildingtyp(const struct region *r, const struct building_type *bt); diff --git a/src/move.test.c b/src/move.test.c index ab30300e8..c8fdb2436 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -522,9 +522,33 @@ static void test_ship_leave_trail(CuTest *tc) { test_cleanup(); } +static void test_movement_speed(CuTest *tc) { + unit * u; + race * rc; + const struct item_type *it_horse; + + test_setup(); + it_horse = test_create_horse(); + rc = test_create_race(NULL); + u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, NULL)); + + rc->speed = 1.0; + CuAssertIntEquals(tc, BP_WALKING, movement_speed(u)); + + rc->speed = 2.0; + CuAssertIntEquals(tc, 2 * BP_WALKING, movement_speed(u)); + + set_level(u, SK_RIDING, 1); + i_change(&u->items, it_horse, 1); + CuAssertIntEquals(tc, BP_RIDING, movement_speed(u)); + + test_cleanup(); +} + CuSuite *get_move_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_movement_speed); SUITE_ADD_TEST(suite, test_walkingcapacity); SUITE_ADD_TEST(suite, test_ship_not_allowed_in_coast); SUITE_ADD_TEST(suite, test_ship_leave_trail); diff --git a/src/tests.c b/src/tests.c index 49b5b7c05..7d20b59c4 100644 --- a/src/tests.c +++ b/src/tests.c @@ -39,7 +39,7 @@ struct race *test_create_race(const char *name) { - race *rc = rc_get_or_create(name); + race *rc = rc_get_or_create(name ? name : "smurf"); rc->maintenance = 10; rc->hitpoints = 20; rc->maxaura = 100;