move_cmd caller no longer has to call init_order.

refactor move_cmd to be more like other command processors.
This commit is contained in:
Enno Rehling 2020-08-12 20:07:17 +02:00
parent 2d415b400d
commit f7dc15b64f
6 changed files with 21 additions and 27 deletions

View File

@ -1881,8 +1881,6 @@ bool translate_order(order *ord, const struct locale *from_lang, const struct lo
default: default:
return false; return false;
} }
return false;
} }
void translate_orders(unit *u, const struct locale *lang, order **list, bool del) void translate_orders(unit *u, const struct locale *lang, order **list, bool del)

View File

@ -2175,9 +2175,17 @@ static void travel(unit * u, order *ord)
} }
} }
void move_cmd(unit * u, order * ord) void move_cmd_ex(unit * u, order * ord, const char *directions)
{ {
assert(u->number); assert(u->number);
if (directions) {
/* Befehl ignorieren und Parsing vom String initialisieren. */
init_tokens_str(directions);
}
else {
init_order(ord, u->faction->locale);
}
if (u->ship && u == ship_owner(u->ship)) { if (u->ship && u == ship_owner(u->ship)) {
bool drifting = (getkeyword(ord) == K_MOVE); bool drifting = (getkeyword(ord) == K_MOVE);
sail(u, ord, drifting); sail(u, ord, drifting);
@ -2187,6 +2195,11 @@ void move_cmd(unit * u, order * ord)
} }
fset(u, UFL_LONGACTION | UFL_NOTMOVING); fset(u, UFL_LONGACTION | UFL_NOTMOVING);
}
void move_cmd(unit * u, order * ord)
{
move_cmd_ex(u, ord, NULL);
set_order(&u->thisorder, NULL); set_order(&u->thisorder, NULL);
} }
@ -2266,8 +2279,6 @@ int follow_ship(unit * u, order * ord)
} }
sbs_init(&sbcmd, command, sizeof(command)); sbs_init(&sbcmd, command, sizeof(command));
sbs_strcat(&sbcmd, LOC(u->faction->locale, keyword(K_MOVE)));
sbs_strcat(&sbcmd, " ");
sbs_strcat(&sbcmd, LOC(u->faction->locale, directions[dir])); sbs_strcat(&sbcmd, LOC(u->faction->locale, directions[dir]));
moves = 1; moves = 1;
@ -2292,11 +2303,8 @@ int follow_ship(unit * u, order * ord)
/* In command steht jetzt das NACH-Kommando. */ /* In command steht jetzt das NACH-Kommando. */
/* NACH ignorieren und Parsing initialisieren. */
init_tokens_str(command);
getstrtoken();
/* NACH ausfuehren */ /* NACH ausfuehren */
move_cmd(u, ord); move_cmd_ex(u, ord, command);
return 1; /* true -> Einheitenliste von vorne durchgehen */ return 1; /* true -> Einheitenliste von vorne durchgehen */
} }
@ -2446,13 +2454,11 @@ void movement(void)
else { else {
if (ships) { if (ships) {
if (u->ship && ship_owner(u->ship) == u) { if (u->ship && ship_owner(u->ship) == u) {
init_order_depr(u->thisorder);
move_cmd(u, u->thisorder); move_cmd(u, u->thisorder);
} }
} }
else { else {
if (!u->ship || ship_owner(u->ship) != u) { if (!u->ship || ship_owner(u->ship) != u) {
init_order_depr(u->thisorder);
move_cmd(u, u->thisorder); move_cmd(u, u->thisorder);
} }
} }

View File

@ -69,6 +69,7 @@ extern "C" {
const struct region *dest); const struct region *dest);
bool can_takeoff(const struct ship * sh, const struct region * from, const struct region * to); bool can_takeoff(const struct ship * sh, const struct region * from, const struct region * to);
void move_cmd(struct unit * u, struct order * ord); void move_cmd(struct unit * u, struct order * ord);
void move_cmd_ex(struct unit * u, struct order * ord, const char *directions);
int follow_ship(struct unit * u, struct order * ord); int follow_ship(struct unit * u, struct order * ord);
#define SA_HARBOUR 1 #define SA_HARBOUR 1

View File

@ -550,7 +550,7 @@ static void test_follow_ship_msg(CuTest * tc) {
"unit:unit", "region:region", "command:order", MT_NEW_END); "unit:unit", "region:region", "command:order", MT_NEW_END);
init_order_depr(ord); init_order_depr(ord);
getstrtoken(); skip_token();
follow_ship(u, ord); follow_ship(u, ord);
@ -651,7 +651,6 @@ static void test_route_cycle(CuTest *tc) {
CuAssertIntEquals(tc, RCF_WALK, u->_race->flags & RCF_WALK); CuAssertIntEquals(tc, RCF_WALK, u->_race->flags & RCF_WALK);
u->orders = create_order(K_ROUTE, u->faction->locale, "WEST EAST NW"); u->orders = create_order(K_ROUTE, u->faction->locale, "WEST EAST NW");
CuAssertStrEquals(tc, "route WEST EAST NW", get_command(u->orders, lang, buffer, sizeof(buffer))); CuAssertStrEquals(tc, "route WEST EAST NW", get_command(u->orders, lang, buffer, sizeof(buffer)));
init_order(u->orders, u->faction->locale);
move_cmd(u, u->orders); move_cmd(u, u->orders);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "moveblocked")); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "moveblocked"));
CuAssertIntEquals(tc, 1, u->region->x); CuAssertIntEquals(tc, 1, u->region->x);
@ -700,7 +699,6 @@ static void test_route_pause(CuTest *tc) {
CuAssertIntEquals(tc, RCF_WALK, u->_race->flags & RCF_WALK); CuAssertIntEquals(tc, RCF_WALK, u->_race->flags & RCF_WALK);
u->orders = create_order(K_ROUTE, u->faction->locale, "PAUSE EAST NW"); u->orders = create_order(K_ROUTE, u->faction->locale, "PAUSE EAST NW");
CuAssertStrEquals(tc, "route PAUSE EAST NW", get_command(u->orders, lang, buffer, sizeof(buffer))); CuAssertStrEquals(tc, "route PAUSE EAST NW", get_command(u->orders, lang, buffer, sizeof(buffer)));
init_order(u->orders, u->faction->locale);
move_cmd(u, u->orders); move_cmd(u, u->orders);
CuAssertIntEquals(tc, 2, u->region->x); CuAssertIntEquals(tc, 2, u->region->x);
CuAssertStrEquals(tc, "route PAUSE EAST NW", get_command(u->orders, lang, buffer, sizeof(buffer))); CuAssertStrEquals(tc, "route PAUSE EAST NW", get_command(u->orders, lang, buffer, sizeof(buffer)));

View File

@ -114,7 +114,6 @@ direction_t find_piracy_target(unit *u, int *il) {
void piracy_cmd(unit * u) void piracy_cmd(unit * u)
{ {
order *ord;
region *r = u->region; region *r = u->region;
ship *sh = u->ship, *sh2; ship *sh = u->ship, *sh2;
direction_t target_dir; direction_t target_dir;
@ -200,15 +199,7 @@ void piracy_cmd(unit * u)
ADDMSG(&u->faction->msgs, msg_message("piratesawvictim", ADDMSG(&u->faction->msgs, msg_message("piratesawvictim",
"ship unit region dir", sh, u, r, target_dir)); "ship unit region dir", sh, u, r, target_dir));
/* Befehl konstruieren */ move_cmd_ex(u, u->thisorder, LOC(u->faction->locale, directions[target_dir]));
/* TODO: why change u->thisorder? */
/* FIXME: when u->thisorder == ord, set_order calls free, destroys both. */
ord = create_order(K_MOVE, u->faction->locale, "%s", LOC(u->faction->locale, directions[target_dir]));
/* Bewegung ausfuehren */
init_order_depr(ord);
move_cmd(u, ord);
free_order(ord);
} }
void age_piracy(region *r) { void age_piracy(region *r) {

View File

@ -108,7 +108,7 @@ static void test_piracy_cmd(CuTest * tc) {
u->thisorder = create_order(K_PIRACY, f->locale, "%s", itoa36(u2->faction->no)); u->thisorder = create_order(K_PIRACY, f->locale, "%s", itoa36(u2->faction->no));
piracy_cmd(u); piracy_cmd(u);
CuAssertPtrEquals(tc, NULL, u->thisorder); CuAssertPtrNotNull(tc, u->thisorder);
CuAssertTrue(tc, u->region != r); CuAssertTrue(tc, u->region != r);
CuAssertPtrEquals(tc, u2->region, u->region); CuAssertPtrEquals(tc, u2->region, u->region);
CuAssertPtrEquals(tc, u2->region, u->ship->region); CuAssertPtrEquals(tc, u2->region, u->ship->region);
@ -213,7 +213,7 @@ static void test_piracy_cmd_land_to_land(CuTest * tc) {
u->thisorder = create_order(K_PIRACY, f->locale, "%s", itoa36(target)); u->thisorder = create_order(K_PIRACY, f->locale, "%s", itoa36(target));
piracy_cmd(u); piracy_cmd(u);
CuAssertPtrEquals(tc, NULL, u->thisorder); CuAssertPtrNotNull(tc, u->thisorder);
CuAssertPtrEquals(tc, r, u->region); CuAssertPtrEquals(tc, r, u->region);
test_teardown(); test_teardown();
@ -228,7 +228,7 @@ static void test_piracy_cmd_swimmer(CuTest * tc) {
r = pirate->region; r = pirate->region;
piracy_cmd(pirate); piracy_cmd(pirate);
CuAssertPtrEquals(tc, NULL, pirate->thisorder); CuAssertPtrNotNull(tc, pirate->thisorder);
CuAssertTrue(tc, pirate->region != r); CuAssertTrue(tc, pirate->region != r);
CuAssertPtrEquals(tc, victim->region, pirate->region); CuAssertPtrEquals(tc, victim->region, pirate->region);
CuAssertPtrNotNullMsg(tc, "successful PIRACY message", test_find_messagetype(pirate->faction->msgs, "piratesawvictim")); CuAssertPtrNotNullMsg(tc, "successful PIRACY message", test_find_messagetype(pirate->faction->msgs, "piratesawvictim"));