From fd65fc79d0ef35bfea33451ccb45d9f25f905436 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 4 Jan 2023 21:44:40 +0200 Subject: [PATCH 12/12] Make is_..._unit_tile() to return bool That's what their name suggest, and what most of the callers expect. Returning a pointer to those callers was causing an error when compiler is following c2x standard. Added tile_..._unit() family of members that those former is_..._unit_tile() callers that actually expect a pointer call instead. See osdn #46367 Signed-off-by: Marko Lindqvist --- ai/default/aidiplomat.c | 2 +- client/goto.c | 6 ++-- common/unit.c | 41 ++++++++++++----------- common/unit.h | 73 ++++++++++++++++++++++++++++++++++------- server/citytools.c | 2 +- server/unithand.c | 2 +- server/unittools.c | 9 +++-- 7 files changed, 94 insertions(+), 41 deletions(-) diff --git a/ai/default/aidiplomat.c b/ai/default/aidiplomat.c index 9bb69fb9d0..7d5b25b74e 100644 --- a/ai/default/aidiplomat.c +++ b/ai/default/aidiplomat.c @@ -588,7 +588,7 @@ static bool dai_diplomat_bribe_nearby(struct ai_type *ait, struct tile *ptile = pos.tile; bool threat = FALSE; int newval, bestval = 0, cost; - struct unit *pvictim = is_other_players_unit_tile(ptile, pplayer); + struct unit *pvictim = tile_other_players_unit(ptile, pplayer); int sanity = punit->id; struct unit_type *ptype; diff --git a/client/goto.c b/client/goto.c index 4753e3cc0b..469e316cad 100644 --- a/client/goto.c +++ b/client/goto.c @@ -1435,7 +1435,7 @@ static void send_path_orders(struct unit *punit, struct pf_path *path, if (i > 0 && p.orders[i - 1] == ORDER_MOVE && (is_non_allied_city_tile(old_tile, client_player()) - || is_non_allied_unit_tile(old_tile, client_player()) != NULL)) { + || is_non_allied_unit_tile(old_tile, client_player()))) { /* Won't be able to perform a regular move to the target tile... */ if (!final_order) { /* ...and no final order exists. Choose what to do when the unit gets @@ -1700,10 +1700,10 @@ static bool order_wants_direction(enum unit_orders order, action_id act_id, } if (is_non_allied_city_tile(tgt_tile, client_player()) - || is_non_allied_unit_tile(tgt_tile, client_player()) != NULL) { + || is_non_allied_unit_tile(tgt_tile, client_player())) { /* Won't be able to move to the target tile to perform the action on * top of it. */ - /* TODO: detect situations where it also would be illegal to perform + /* TODO: Detect situations where it also would be illegal to perform * the action from the neighbor tile. */ return TRUE; } diff --git a/common/unit.c b/common/unit.c index f7ce4f8add..4cb7a5685f 100644 --- a/common/unit.c +++ b/common/unit.c @@ -1319,8 +1319,8 @@ void unit_tile_set(struct unit *punit, struct tile *ptile) (ie, if your nation A is allied with B, and B is allied with C, a tile containing units from B and C will return NULL) **************************************************************************/ -struct unit *is_allied_unit_tile(const struct tile *ptile, - const struct player *pplayer) +struct unit *tile_allied_unit(const struct tile *ptile, + const struct player *pplayer) { struct unit *punit = NULL; @@ -1336,13 +1336,13 @@ struct unit *is_allied_unit_tile(const struct tile *ptile, } /**************************************************************************** - Is there an enemy unit on this tile? Returns the unit or NULL if none. + Is there an enemy unit on this tile? Returns the unit or NULL if none. This function is likely to fail if used at the client because the client - doesn't see all units. (Maybe it should be moved into the server code.) + doesn't see all units. (Maybe it should be moved into the server code.) ****************************************************************************/ -struct unit *is_enemy_unit_tile(const struct tile *ptile, - const struct player *pplayer) +struct unit *tile_enemy_unit(const struct tile *ptile, + const struct player *pplayer) { unit_list_iterate(ptile->units, punit) { if (pplayers_at_war(unit_owner(punit), pplayer)) @@ -1353,10 +1353,10 @@ struct unit *is_enemy_unit_tile(const struct tile *ptile, } /************************************************************************** - is there an non-allied unit on this tile? + Return one of the non-allied units on the tile, if there is any **************************************************************************/ -struct unit *is_non_allied_unit_tile(const struct tile *ptile, - const struct player *pplayer) +struct unit *tile_non_allied_unit(const struct tile *ptile, + const struct player *pplayer) { unit_list_iterate(ptile->units, punit) { if (!pplayers_allied(unit_owner(punit), pplayer)) @@ -1368,10 +1368,11 @@ struct unit *is_non_allied_unit_tile(const struct tile *ptile, } /************************************************************************** - is there an unit belonging to another player on this tile? + Return an unit belonging to any other player, if there are any + on the tile. **************************************************************************/ -struct unit *is_other_players_unit_tile(const struct tile *ptile, - const struct player *pplayer) +struct unit *tile_other_players_unit(const struct tile *ptile, + const struct player *pplayer) { unit_list_iterate(ptile->units, punit) { if (unit_owner(punit) != pplayer) { @@ -1383,14 +1384,16 @@ struct unit *is_other_players_unit_tile(const struct tile *ptile, } /************************************************************************** - is there an unit we have peace or ceasefire with on this tile? + Return an unit we have peace or ceasefire with on this tile, + if any exist. **************************************************************************/ -struct unit *is_non_attack_unit_tile(const struct tile *ptile, - const struct player *pplayer) +struct unit *tile_non_attack_unit(const struct tile *ptile, + const struct player *pplayer) { unit_list_iterate(ptile->units, punit) { - if (pplayers_non_attack(unit_owner(punit), pplayer)) + if (pplayers_non_attack(unit_owner(punit), pplayer)) { return punit; + } } unit_list_iterate_end; @@ -1401,12 +1404,12 @@ struct unit *is_non_attack_unit_tile(const struct tile *ptile, Is there an occupying unit on this tile? Intended for both client and server; assumes that hiding units are not - sent to client. First check tile for known and seen. + sent to client. First check tile for known and seen. - called by city_can_work_tile(). + Called by city_can_work_tile(). ****************************************************************************/ struct unit *unit_occupies_tile(const struct tile *ptile, - const struct player *pplayer) + const struct player *pplayer) { unit_list_iterate(ptile->units, punit) { if (!is_military_unit(punit)) { diff --git a/common/unit.h b/common/unit.h index e3f34b0792..4f4cb43033 100644 --- a/common/unit.h +++ b/common/unit.h @@ -345,18 +345,69 @@ int get_transporter_capacity(const struct unit *punit); struct player *unit_nationality(const struct unit *punit); void unit_tile_set(struct unit *punit, struct tile *ptile); -struct unit *is_allied_unit_tile(const struct tile *ptile, - const struct player *pplayer); -struct unit *is_enemy_unit_tile(const struct tile *ptile, - const struct player *pplayer); -struct unit *is_non_allied_unit_tile(const struct tile *ptile, - const struct player *pplayer); -struct unit *is_other_players_unit_tile(const struct tile *ptile, - const struct player *pplayer); -struct unit *is_non_attack_unit_tile(const struct tile *ptile, - const struct player *pplayer); +struct unit *tile_allied_unit(const struct tile *ptile, + const struct player *pplayer); + +/**********************************************************************//** + Are there allied units, and only allied units, on the tile? +**************************************************************************/ +static inline bool is_allied_unit_tile(const struct tile *ptile, + const struct player *pplayer) +{ + return NULL != tile_allied_unit(ptile, pplayer); +} + +struct unit *tile_enemy_unit(const struct tile *ptile, + const struct player *pplayer); + +/**********************************************************************//** + Are there any enemy unit(s) on tile? +**************************************************************************/ +static inline bool is_enemy_unit_tile(const struct tile *ptile, + const struct player *pplayer) +{ + return NULL != tile_enemy_unit(ptile, pplayer); +} + +struct unit *tile_non_allied_unit(const struct tile *ptile, + const struct player *pplayer); + +/**********************************************************************//** + Are there any non-allied unit(s) on tile? +**************************************************************************/ +static inline bool is_non_allied_unit_tile(const struct tile *ptile, + const struct player *pplayer) +{ + return NULL != tile_non_allied_unit(ptile, pplayer); +} + +struct unit *tile_other_players_unit(const struct tile *ptile, + const struct player *pplayer); + +/**********************************************************************//** + Are there any unit(s) on tile owned by someone else than pplayer? +**************************************************************************/ +static inline bool is_other_players_unit_tile(const struct tile *ptile, + const struct player *pplayer) +{ + return NULL != tile_other_players_unit(ptile, pplayer); +} + +struct unit *tile_non_attack_unit(const struct tile *ptile, + const struct player *pplayer); + +/**********************************************************************//** + Are there any unit(s) on tile preventing player's units from + moving towards, either to attack enemy or to enter the tile with allies. +**************************************************************************/ +static inline bool is_non_attack_unit_tile(const struct tile *ptile, + const struct player *pplayer) +{ + return NULL != tile_non_attack_unit(ptile, pplayer); +} + struct unit *unit_occupies_tile(const struct tile *ptile, - const struct player *pplayer); + const struct player *pplayer); bool is_my_zoc(const struct player *unit_owner, const struct tile *ptile); bool unit_being_aggressive(const struct unit *punit); diff --git a/server/citytools.c b/server/citytools.c index 5ce828e143..f9c846ae7c 100644 --- a/server/citytools.c +++ b/server/citytools.c @@ -175,7 +175,7 @@ void city_freeze_workers_queue(struct city *pcity) bool create_city_for_player(struct player *pplayer, struct tile *ptile, const char *name) { - if (is_enemy_unit_tile(ptile, pplayer) != NULL + if (is_enemy_unit_tile(ptile, pplayer) || !city_can_be_built_here(ptile, NULL)) { return FALSE; } diff --git a/server/unithand.c b/server/unithand.c index a4edccaec9..a7a7820dec 100644 --- a/server/unithand.c +++ b/server/unithand.c @@ -785,7 +785,7 @@ static struct player *need_war_player_hlp(const struct unit *actor, return city_owner(tcity); } - if ((tunit = is_non_attack_unit_tile(target_tile, unit_owner(actor))) + if ((tunit = tile_non_attack_unit(target_tile, unit_owner(actor))) && rel_may_become_war(unit_owner(actor), unit_owner(tunit))) { return unit_owner(tunit); } diff --git a/server/unittools.c b/server/unittools.c index 1660439bc2..42b3c65550 100644 --- a/server/unittools.c +++ b/server/unittools.c @@ -1508,7 +1508,7 @@ bool is_refuel_point(const struct tile *ptile, const struct player *pplayer, const struct unit *punit) { - if (NULL != is_non_allied_unit_tile(ptile, pplayer)) { + if (is_non_allied_unit_tile(ptile, pplayer)) { return FALSE; } @@ -4077,13 +4077,12 @@ static bool maybe_cancel_patrol_due_to_enemy(struct unit *punit) struct player *pplayer = unit_owner(punit); circle_iterate(unit_tile(punit), radius_sq, ptile) { - struct unit *penemy = is_non_allied_unit_tile(ptile, pplayer); - + struct unit *penemy = tile_non_allied_unit(ptile, pplayer); struct vision_site *pdcity = map_get_player_site(ptile, pplayer); if ((penemy && can_player_see_unit(pplayer, penemy)) - || (pdcity && !pplayers_allied(pplayer, vision_site_owner(pdcity)) - && pdcity->occupied)) { + || (pdcity && !pplayers_allied(pplayer, vision_site_owner(pdcity)) + && pdcity->occupied)) { cancel = TRUE; break; } -- 2.39.0