From cfe544c4df1f32773f213d984a8be9c925844777 Mon Sep 17 00:00:00 2001 From: Sveinung Kvilhaugsvik Date: Tue, 23 Feb 2021 14:33:29 +0100 Subject: [PATCH] Split unit_move_handling() based on igzoc. Done as a small step for the sake of backwards compatibility. The new function unit_move_igzoc() handles cases where igzoc was set to TRUE. It is kept the exact same rules for when it is allowed to move the unit. (I think some of them should be removed.) unit_move_handling() keeps - recently enabler controlled - regular moves and the code to understand a move towards a tile as asking what actions the unit can perform against it. See osdn #41604 --- server/barbarian.c | 6 +-- server/unithand.c | 130 ++++++++++++++++++++++++++------------------- server/unithand.h | 3 +- server/unittools.c | 2 +- 4 files changed, 79 insertions(+), 62 deletions(-) diff --git a/server/barbarian.c b/server/barbarian.c index 7193b4d378..f527acfcab 100644 --- a/server/barbarian.c +++ b/server/barbarian.c @@ -341,8 +341,7 @@ bool unleash_barbarians(struct tile *ptile) if (unit_can_move_to_tile(&(wld.map), punit2, dir_tiles[rdir], TRUE, FALSE)) { /* Move */ - (void) unit_move_handling(punit2, dir_tiles[rdir], - TRUE, TRUE); + (void) unit_move_igzoc(punit2, dir_tiles[rdir]); log_debug("Moved barbarian unit from (%d, %d) to (%d, %d)", TILE_XY(ptile), TILE_XY(dir_tiles[rdir])); dest_found = TRUE; @@ -422,8 +421,7 @@ bool unleash_barbarians(struct tile *ptile) if (unit_can_move_to_tile(&(wld.map), punit2, dir_tiles[rdir], TRUE, FALSE)) { /* Move */ - (void) unit_move_handling(punit2, dir_tiles[rdir], - TRUE, TRUE); + (void) unit_move_igzoc(punit2, dir_tiles[rdir]); dest_found = TRUE; } diff --git a/server/unithand.c b/server/unithand.c index 85b520fe41..3cd5205cd0 100644 --- a/server/unithand.c +++ b/server/unithand.c @@ -4876,11 +4876,7 @@ static bool unit_do_regular_move(struct player *actor_player, was done, FALSE if it wasn't for some reason. Even if this returns TRUE, the unit may have died upon arrival to new tile. - 'igzoc' means ignore ZOC rules - not necessary for igzoc units etc, but - done in some special cases (moving barbarians out of initial hut). - Should normally be FALSE. - - 'move_do_not_act' is another special case which should normally be + 'move_do_not_act' is a special case which should normally be FALSE. If TRUE any enabler controlled actions punit can perform to pdesttile it self or something located at it will be ignored. If FALSE the system will check if punit can perform any enabler controlled action @@ -4889,30 +4885,11 @@ static bool unit_do_regular_move(struct player *actor_player, controlled action) to pdesttile the game will try to explain why. **************************************************************************/ bool unit_move_handling(struct unit *punit, struct tile *pdesttile, - bool igzoc, bool move_do_not_act) + bool move_do_not_act) { struct player *pplayer = unit_owner(punit); - /*** Phase 1: Basic checks ***/ - - /* this occurs often during lag, and to the AI due to some quirks -- Syela */ - if (!is_tiles_adjacent(unit_tile(punit), pdesttile)) { - log_debug("tiles not adjacent in move request"); - return FALSE; - } - - - if (punit->moves_left <= 0) { - notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server, - _("This unit has no moves left.")); - return FALSE; - } - - if (!unit_can_do_action_now(punit)) { - return FALSE; - } - - /*** Phase 2: Attempted action interpretation checks ***/ + /*** Phase 1: Attempted action interpretation checks ***/ /* Check if the move should be interpreted as an attempt to perform an * enabler controlled action to the target tile. When the move may be an @@ -4947,7 +4924,7 @@ bool unit_move_handling(struct unit *punit, struct tile *pdesttile, if (!move_do_not_act) { const bool can_not_move = !unit_can_move_to_tile(&(wld.map), punit, pdesttile, - igzoc, FALSE); + FALSE, FALSE); bool one_action_may_be_legal = action_tgt_unit(punit, pdesttile, can_not_move) || action_tgt_city(punit, pdesttile, can_not_move) @@ -4982,35 +4959,63 @@ bool unit_move_handling(struct unit *punit, struct tile *pdesttile, } } - /*** Phase 3: OK now move the unit ***/ - if (!igzoc) { - /* This is a regular move, subject to the rules. */ - if (is_action_enabled_unit_on_tile(ACTION_UNIT_MOVE, - punit, pdesttile, NULL)) { - return unit_perform_action(pplayer, punit->id, tile_index(pdesttile), - NO_TARGET, "", ACTION_UNIT_MOVE, - ACT_REQ_PLAYER); - } else if (is_action_enabled_unit_on_tile(ACTION_UNIT_MOVE2, - punit, pdesttile, NULL)) { - return unit_perform_action(pplayer, punit->id, tile_index(pdesttile), - NO_TARGET, "", ACTION_UNIT_MOVE2, - ACT_REQ_PLAYER); - } else if (is_action_enabled_unit_on_tile(ACTION_UNIT_MOVE3, - punit, pdesttile, NULL)) { - return unit_perform_action(pplayer, punit->id, tile_index(pdesttile), - NO_TARGET, "", ACTION_UNIT_MOVE3, - ACT_REQ_PLAYER); - } else { - /* TODO: Extend the action not enabled explanation system to cover all - * existing reasons and switch to using it. See hrm Feature #920229 */ - can_unit_move_to_tile_with_notify(punit, pdesttile, igzoc, - NULL, FALSE); - return FALSE; - } + /*** Phase 2: OK now move the unit ***/ + /* This is a regular move, subject to the rules. */ + if (is_action_enabled_unit_on_tile(ACTION_UNIT_MOVE, + punit, pdesttile, NULL)) { + return unit_perform_action(pplayer, punit->id, tile_index(pdesttile), + NO_TARGET, "", ACTION_UNIT_MOVE, + ACT_REQ_PLAYER); + } else if (is_action_enabled_unit_on_tile(ACTION_UNIT_MOVE2, + punit, pdesttile, NULL)) { + return unit_perform_action(pplayer, punit->id, tile_index(pdesttile), + NO_TARGET, "", ACTION_UNIT_MOVE2, + ACT_REQ_PLAYER); + } else if (is_action_enabled_unit_on_tile(ACTION_UNIT_MOVE3, + punit, pdesttile, NULL)) { + return unit_perform_action(pplayer, punit->id, tile_index(pdesttile), + NO_TARGET, "", ACTION_UNIT_MOVE3, + ACT_REQ_PLAYER); + } else { + /* TODO: Extend the action not enabled explanation system to cover all + * existing reasons and switch to using it. See hrm Feature #920229 */ + can_unit_move_to_tile_with_notify(punit, pdesttile, FALSE, + NULL, FALSE); + return FALSE; + } +} + +/**********************************************************************//** + Will try to move to/attack the tile dest_x,dest_y. Returns TRUE if this + was done, FALSE if it wasn't for some reason. Even if this returns TRUE, + the unit may have died upon arrival to new tile. + + igzoc means ignore ZOC rules - not necessary for igzoc units etc, but + done in some special cases (moving barbarians out of initial hut). +**************************************************************************/ +bool unit_move_igzoc(struct unit *punit, struct tile *pdesttile) +{ + struct player *pplayer = unit_owner(punit); + + /*** Phase 1: Basic checks ***/ + + /* this occurs often during lag, and to the AI due to some quirks -- Syela */ + if (!is_tiles_adjacent(unit_tile(punit), pdesttile)) { + log_debug("tiles not adjacent in move request"); + return FALSE; + } + + if (punit->moves_left <= 0) { + notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server, + _("This unit has no moves left.")); + return FALSE; } + if (!unit_can_do_action_now(punit)) { + return FALSE; + } - /* Compatibility for when igzoc is TRUE */ + /*** Phase 2: OK now move the unit ***/ /* We cannot move a transport into a tile that holds * units or cities not allied with all of our cargo. */ @@ -5033,7 +5038,8 @@ bool unit_move_handling(struct unit *punit, struct tile *pdesttile, /* Can't move if an action that block moves is legal */ if ((blocking_action = action_is_blocked_by( - NULL, punit, pdesttile, tile_city(pdesttile), NULL))) { + action_by_number(ACTION_UNIT_MOVE), punit, pdesttile, + tile_city(pdesttile), NULL))) { notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server, /* TRANS: Freight ... Recycle Unit ... Help Wonder ... */ _("Your %s can't do %s when %s is legal."), @@ -5044,13 +5050,25 @@ bool unit_move_handling(struct unit *punit, struct tile *pdesttile, } } - if (can_unit_move_to_tile_with_notify(punit, pdesttile, igzoc, + if (can_unit_move_to_tile_with_notify(punit, pdesttile, TRUE, NULL, FALSE) /* Don't override "Transport Embark" */ && can_unit_exist_at_tile(&(wld.map), punit, pdesttile) /* Don't override "Transport Disembark" or "Transport Disembark 2" */ && !unit_transported(punit)) { - return unit_do_regular_move(pplayer, punit, pdesttile, NULL); + int move_cost = map_move_cost_unit(&(wld.map), punit, pdesttile); + + unit_move(punit, pdesttile, move_cost, + /* Don't override "Transport Embark" */ + NULL, FALSE, + /* Don't override "Conquer City" */ + FALSE, + /* Don't override "Conquer Extras" */ + FALSE, + /* Don't override "Enter Hut" */ + FALSE); + + return TRUE; } else { return FALSE; } diff --git a/server/unithand.h b/server/unithand.h index c7898f9161..12bd3d12c3 100644 --- a/server/unithand.h +++ b/server/unithand.h @@ -28,7 +28,8 @@ void unit_change_homecity_handling(struct unit *punit, struct city *new_pcity, bool rehome); bool unit_move_handling(struct unit *punit, struct tile *pdesttile, - bool igzoc, bool move_diplomat_city); + bool move_diplomat_city); +bool unit_move_igzoc(struct unit *punit, struct tile *pdesttile); void unit_do_action(struct player *pplayer, const int actor_id, diff --git a/server/unittools.c b/server/unittools.c index 292668dd97..ba9cb18fb1 100644 --- a/server/unittools.c +++ b/server/unittools.c @@ -4257,7 +4257,7 @@ bool execute_orders(struct unit *punit, const bool fresh) } log_debug(" moving to %d,%d", TILE_XY(dst_tile)); - res = unit_move_handling(punit, dst_tile, FALSE, + res = unit_move_handling(punit, dst_tile, order.order != ORDER_ACTION_MOVE); if (!player_unit_by_number(pplayer, unitid)) { log_debug(" unit died while moving."); -- 2.20.1