remove side-effects from movement_speed().

This commit is contained in:
Enno Rehling 2017-10-06 22:15:48 +02:00
parent c73af8b89b
commit 6265abac74
4 changed files with 70 additions and 50 deletions

View File

@ -89,14 +89,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <limits.h> #include <limits.h>
#include <float.h> #include <float.h>
/* 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; int *storms;
typedef struct traveldir { typedef struct traveldir {
@ -265,7 +257,7 @@ get_transporters(const item * itm, int *p_animals, int *p_acap, int *p_vehicles,
*p_acap = acap; *p_acap = acap;
} }
static int ridingcapacity(unit * u) static int ridingcapacity(const unit * u)
{ {
int vehicles = 0, vcap = 0; int vehicles = 0, vcap = 0;
int animals = 0, acap = 0; int animals = 0, acap = 0;
@ -433,7 +425,7 @@ bool canswim(unit * u)
return false; return false;
} }
static int canride(unit * u) static int canride(const unit * u)
{ {
int horses = 0, maxhorses, unicorns = 0, maxunicorns; int horses = 0, maxhorses, unicorns = 0, maxunicorns;
int skill = effskill(u, SK_RIDING, 0); int skill = effskill(u, SK_RIDING, 0);
@ -460,17 +452,17 @@ static int canride(unit * u)
if (!(u_race(u)->flags & RCF_HORSE) if (!(u_race(u)->flags & RCF_HORSE)
&& ((horses == 0 && unicorns == 0) && ((horses == 0 && unicorns == 0)
|| horses > maxhorses || unicorns > maxunicorns)) { || horses > maxhorses || unicorns > maxunicorns)) {
return 0; return BP_WALKING;
} }
if (ridingcapacity(u) - eff_weight(u) >= 0) { if (ridingcapacity(u) - eff_weight(u) >= 0) {
if (horses == 0 && unicorns >= u->number && !(u_race(u)->flags & RCF_HORSE)) { 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) 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. * Normalerweise verliert man 3 BP pro Region, bei Straßen nur 2 BP.
* Außerdem: Wenn Einheit transportiert, nur halbe 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); const race *rc = u_race(u);
double dk = rc->speed; double dk = rc->speed;
assert(u->number); assert(u->number);
@ -1415,6 +1407,10 @@ static int movement_speed(unit * u)
mp = BP_DRAGON; mp = BP_DRAGON;
break; break;
default: default:
mp = canride(u);
if (mp>=BP_RIDING) {
dk = 1.0;
}
break; break;
} }
@ -1426,22 +1422,6 @@ 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 */ /* unicorn in inventory */
if (u->number <= i_get(u->items, it_find("fairyboot"))) { if (u->number <= i_get(u->items, it_find("fairyboot"))) {
mp *= 2; mp *= 2;
@ -1456,8 +1436,7 @@ static int movement_speed(unit * u)
mp *= 2; mp *= 2;
} }
} }
break;
}
return (int)(dk * mp); 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) const region_list * route_end, order * ord, int mode, follower ** followers)
{ {
region *r = u->region; region *r = u->region;
int mp;
if (u->building && !can_leave(u)) { if (u->building && !can_leave(u)) {
cmistake(u, u->thisorder, 150, MSG_MOVE); cmistake(u, u->thisorder, 150, MSG_MOVE);
return route_begin; 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); cmistake(u, ord, 42, MSG_MOVE);
return route_begin; 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); route_end = travel_route(u, route_begin, route_end, ord, mode);
if (u->flags&UFL_FOLLOWED) { if (u->flags&UFL_FOLLOWED) {

View File

@ -36,6 +36,14 @@ extern "C" {
extern struct attrib_type at_shiptrail; extern struct attrib_type at_shiptrail;
extern int *storms; 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 */ /* die Zahlen sind genau äquivalent zu den race Flags */
#define MV_CANNOTMOVE (1<<5) #define MV_CANNOTMOVE (1<<5)
#define MV_FLY (1<<7) /* kann fliegen */ #define MV_FLY (1<<7) /* kann fliegen */
@ -70,6 +78,7 @@ extern "C" {
struct ship *move_ship(struct ship *sh, struct region *from, struct ship *move_ship(struct ship *sh, struct region *from,
struct region *to, struct region_list *route); struct region *to, struct region_list *route);
int walkingcapacity(const struct unit *u); int walkingcapacity(const struct unit *u);
int movement_speed(const struct unit * u);
void follow_unit(struct unit *u); void follow_unit(struct unit *u);
struct unit *owner_buildingtyp(const struct region *r, struct unit *owner_buildingtyp(const struct region *r,
const struct building_type *bt); const struct building_type *bt);

View File

@ -522,9 +522,33 @@ static void test_ship_leave_trail(CuTest *tc) {
test_cleanup(); 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 *get_move_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_movement_speed);
SUITE_ADD_TEST(suite, test_walkingcapacity); SUITE_ADD_TEST(suite, test_walkingcapacity);
SUITE_ADD_TEST(suite, test_ship_not_allowed_in_coast); SUITE_ADD_TEST(suite, test_ship_not_allowed_in_coast);
SUITE_ADD_TEST(suite, test_ship_leave_trail); SUITE_ADD_TEST(suite, test_ship_leave_trail);

View File

@ -39,7 +39,7 @@
struct race *test_create_race(const char *name) 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->maintenance = 10;
rc->hitpoints = 20; rc->hitpoints = 20;
rc->maxaura = 100; rc->maxaura = 100;