From db555402855272bd0931dc9c6280ff93402a67d2 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 1 Apr 2023 09:50:08 +0300 Subject: [PATCH 27/27] Path Finding: Make MC and EC unsigned everywhere See osdn #47731 Signed-off-by: Marko Lindqvist --- ai/default/aiferry.c | 16 ++++++------- client/goto.c | 14 +++++------ common/aicore/path_finding.c | 8 ++++--- common/aicore/path_finding.h | 28 +++++++++++----------- common/aicore/pf_tools.c | 46 ++++++++++++++++++++---------------- server/advisors/advgoto.c | 12 +++++----- 6 files changed, 66 insertions(+), 58 deletions(-) diff --git a/ai/default/aiferry.c b/ai/default/aiferry.c index d73c507926..3eff252c92 100644 --- a/ai/default/aiferry.c +++ b/ai/default/aiferry.c @@ -392,13 +392,13 @@ int aiferry_avail_boats(struct ai_type *ait, struct player *pplayer) call-back. Doesn't care for enemy/neutral tiles, these should be excluded using a TB call-back. **************************************************************************/ -static int combined_land_sea_move(const struct tile *src_tile, - enum pf_move_scope src_scope, - const struct tile *tgt_tile, - enum pf_move_scope dst_scope, - const struct pf_parameter *param) +static unsigned combined_land_sea_move(const struct tile *src_tile, + enum pf_move_scope src_scope, + const struct tile *tgt_tile, + enum pf_move_scope dst_scope, + const struct pf_parameter *param) { - int move_cost; + unsigned move_cost; if (!((PF_MS_NATIVE | PF_MS_CITY) & dst_scope)) { /* Any-to-Sea */ @@ -419,8 +419,8 @@ static int combined_land_sea_move(const struct tile *src_tile, EC callback to account for the cost of sea moves by a ferry hurrying to pick our unit up. **************************************************************************/ -static int sea_move(const struct tile *ptile, enum known_type known, - const struct pf_parameter *param) +static unsigned sea_move(const struct tile *ptile, enum known_type known, + const struct pf_parameter *param) { if (is_ocean_tile(ptile)) { /* Approximately TURN_FACTOR / average ferry move rate diff --git a/client/goto.c b/client/goto.c index 3098c14744..618824e573 100644 --- a/client/goto.c +++ b/client/goto.c @@ -552,8 +552,8 @@ bool goto_pop_waypoint(void) PF callback to get the path with the minimal number of steps (out of all shortest paths). ****************************************************************************/ -static int get_EC(const struct tile *ptile, enum known_type known, - const struct pf_parameter *param) +static unsigned get_EC(const struct tile *ptile, enum known_type known, + const struct pf_parameter *param) { return 1; } @@ -688,7 +688,7 @@ static bool is_non_allied_city_adjacent(const struct player *pplayer, static int get_connect_road(const struct tile *src_tile, enum direction8 dir, const struct tile *dest_tile, int src_cost, int src_extra, - int *dest_cost, int *dest_extra, + unsigned *dest_cost, unsigned *dest_extra, const struct pf_parameter *param) { int activity_time, move_cost, moves_left; @@ -745,7 +745,7 @@ static int get_connect_road(const struct tile *src_tile, enum direction8 dir, total_cost += move_cost; } - /* Now need to include the activity cost. If we have moves left, they + /* Now need to include the activity cost. If we have moves left, they * will count as a full turn towards the activity time */ moves_left = param->move_rate - (total_cost % param->move_rate); if (activity_time > 0) { @@ -758,9 +758,9 @@ static int get_connect_road(const struct tile *src_tile, enum direction8 dir, } total_cost += activity_time * param->move_rate; - /* Now we determine if we have found a better path. When building + /* Now we determine if we have found a better path. When building * road type with positive move_cost, we care most about the length - * of the result. When building road type with move_cost 0, we + * of the result. When building road type with move_cost 0, we * care most about construction time. */ /* *dest_cost == -1 means we haven't reached dest until now */ @@ -799,7 +799,7 @@ static int get_connect_irrig(const struct tile *src_tile, enum direction8 dir, const struct tile *dest_tile, int src_cost, int src_extra, - int *dest_cost, int *dest_extra, + unsigned *dest_cost, unsigned *dest_extra, const struct pf_parameter *param) { int activity_time, move_cost, moves_left, total_cost; diff --git a/common/aicore/path_finding.c b/common/aicore/path_finding.c index 1dd6c04813..04c63535eb 100644 --- a/common/aicore/path_finding.c +++ b/common/aicore/path_finding.c @@ -170,7 +170,7 @@ static inline int pf_moves_left(const struct pf_parameter *param, int cost) Obtain cost-of-path from pure cost and extra cost ****************************************************************************/ static inline int pf_total_CC(const struct pf_parameter *param, - int cost, int extra) + unsigned cost, unsigned extra) { return PF_TURN_FACTOR * cost + extra * pf_move_rate(param); } @@ -535,7 +535,9 @@ static bool pf_jumbo_map_iterate(struct pf_map *pfm) * priority queue for next call to pf_jumbo_map_iterate(). */ int tindex1 = tile_index(tile1); struct pf_normal_node *node1 = pfnm->lattice + tindex1; - int priority, cost1, extra_cost1; + int priority; + unsigned cost1; + unsigned extra_cost1; /* As for the previous position, 'tile1', 'node1' and 'tindex1' are * defining the adjacent position. */ @@ -634,7 +636,7 @@ static bool pf_normal_map_iterate(struct pf_map *pfm) int tindex1 = tile_index(tile1); struct pf_normal_node *node1 = pfnm->lattice + tindex1; int cost; - int extra = 0; + unsigned extra = 0; /* As for the previous position, 'tile1', 'node1' and 'tindex1' are * defining the adjacent position. */ diff --git a/common/aicore/path_finding.h b/common/aicore/path_finding.h index 089178ca14..42fb89718f 100644 --- a/common/aicore/path_finding.h +++ b/common/aicore/path_finding.h @@ -44,7 +44,7 @@ extern "C" { * * path: a list of steps which leads from the start to the end * - * move cost (MC): move cost of a _single_ step. MC is always >= 0. + * move cost (MC): move cost of a _single_ step. MC is always positive. * [The parameter can specify what the MC of a step into the unknown is * to be (this is a constant for each map). This defaults to a * slightly large value meaning unknown tiles are avoided slightly. @@ -52,7 +52,7 @@ extern "C" { * movement through unknown tiles, or to use PF_IMPOSSIBLE_MC to * easily avoid unknown tiles.] * - * extra cost (EC): extra cost of a _single_ tile. EC is always >= 0. + * extra cost (EC): extra cost of a _single_ tile. EC is always positive. * The intended meaning for EC is "how much we want to avoid this tile", * see DISCUSSION below for more. * @@ -250,7 +250,7 @@ extern "C" { * * Hints: * 1. It is useful to limit the expansion of unknown tiles with a get_TB - * callback. In this case you might set the unknown_MC to be 0. + * callback. In this case you might set the unknown_MC to be 0. * 2. If there are two paths of the same cost to a tile, you are * not guaranteed to get the one with the least steps in it. If you care, * specifying EC to be 1 will do the job. @@ -333,8 +333,8 @@ struct pf_position { * have when reaching the tile. It is always set to * 1 when unit types are not fueled. */ - int total_MC; /* Total MC to reach this point */ - int total_EC; /* Total EC to reach this point */ + unsigned total_MC; /* Total MC to reach this point */ + unsigned total_EC; /* Total EC to reach this point */ enum direction8 dir_to_next_pos; /* Used only in 'struct pf_path'. */ enum direction8 dir_to_here; /* Where did we come from. */ @@ -379,11 +379,11 @@ struct pf_parameter { * direction 'dir'. Note that the callback can calculate 'to_tile' by * itself based on 'from_tile' and 'dir'. Excessive information 'to_tile' * is provided to ease the implementation of the callback. */ - int (*get_MC) (const struct tile *from_tile, - enum pf_move_scope src_move_scope, - const struct tile *to_tile, - enum pf_move_scope dst_move_scope, - const struct pf_parameter *param); + unsigned (*get_MC) (const struct tile *from_tile, + enum pf_move_scope src_move_scope, + const struct tile *to_tile, + enum pf_move_scope dst_move_scope, + const struct pf_parameter *param); /* Callback which determines if we can move from/to 'ptile'. */ enum pf_move_scope (*get_move_scope) (const struct tile *ptile, @@ -402,8 +402,8 @@ struct pf_parameter { /* Callback which can be used to provide extra costs depending on the * tile. Can be NULL. It can be assumed that the implementation of * "path_finding.h" will cache this value. */ - int (*get_EC) (const struct tile *ptile, enum known_type known, - const struct pf_parameter *param); + unsigned (*get_EC) (const struct tile *ptile, enum known_type known, + const struct pf_parameter *param); /* Callback which determines whether an action would be performed at * 'ptile' instead of moving to it. */ @@ -441,7 +441,7 @@ struct pf_parameter { int (*get_moves_left_req) (const struct tile *ptile, enum known_type, const struct pf_parameter *param); - /* This is a jumbo callback which overrides all previous ones. It takes + /* This is a jumbo callback which overrides all previous ones. It takes * care of everything (ZOC, known, costs etc). * Variables: * from_tile -- the source tile @@ -464,7 +464,7 @@ struct pf_parameter { enum direction8 dir, const struct tile *to_tile, int from_cost, int from_extra, - int *to_cost, int *to_extra, + unsigned *to_cost, unsigned *to_extra, const struct pf_parameter *param); /* User provided data. Can be used to attach arbitrary information diff --git a/common/aicore/pf_tools.c b/common/aicore/pf_tools.c index 33624aac12..eed7a2c2e7 100644 --- a/common/aicore/pf_tools.c +++ b/common/aicore/pf_tools.c @@ -361,15 +361,16 @@ static inline bool pf_move_possible(const struct tile *src, Use with a TB callback to prevent passing through occupied tiles. Does not permit passing through non-native tiles without transport. ****************************************************************************/ -static int normal_move(const struct tile *src, - enum pf_move_scope src_scope, - const struct tile *dst, - enum pf_move_scope dst_scope, - const struct pf_parameter *param) +static unsigned normal_move(const struct tile *src, + enum pf_move_scope src_scope, + const struct tile *dst, + enum pf_move_scope dst_scope, + const struct pf_parameter *param) { if (pf_move_possible(src, src_scope, dst, dst_scope, param)) { return map_move_cost(param->map, param->owner, param->utype, src, dst); } + return PF_IMPOSSIBLE_MC; } @@ -380,11 +381,11 @@ static int normal_move(const struct tile *src, Use with a TB callback to prevent passing through occupied tiles. Does not permit passing through non-native tiles without transport. ****************************************************************************/ -static int overlap_move(const struct tile *src, - enum pf_move_scope src_scope, - const struct tile *dst, - enum pf_move_scope dst_scope, - const struct pf_parameter *param) +static unsigned overlap_move(const struct tile *src, + enum pf_move_scope src_scope, + const struct tile *dst, + enum pf_move_scope dst_scope, + const struct pf_parameter *param) { if (pf_move_possible(src, src_scope, dst, dst_scope, param)) { return map_move_cost(param->map, param->owner, param->utype, src, dst); @@ -392,20 +393,22 @@ static int overlap_move(const struct tile *src, /* This should always be the last tile reached. */ return param->move_rate; } + return PF_IMPOSSIBLE_MC; } /************************************************************************//** A cost function for amphibious movement. ****************************************************************************/ -static int amphibious_move(const struct tile *ptile, - enum pf_move_scope src_scope, - const struct tile *ptile1, - enum pf_move_scope dst_scope, - const struct pf_parameter *param) +static unsigned amphibious_move(const struct tile *ptile, + enum pf_move_scope src_scope, + const struct tile *ptile1, + enum pf_move_scope dst_scope, + const struct pf_parameter *param) { struct pft_amphibious *amphibious = param->data; - int cost, scale; + unsigned cost; + int scale; if (PF_MS_TRANSPORT & src_scope) { if (PF_MS_TRANSPORT & dst_scope) { @@ -438,6 +441,7 @@ static int amphibious_move(const struct tile *ptile, if (cost != PF_IMPOSSIBLE_MC && cost < FC_INFINITY) { cost *= scale; } + return cost; } @@ -446,13 +450,14 @@ static int amphibious_move(const struct tile *ptile, /************************************************************************//** Extra cost call back for amphibious movement ****************************************************************************/ -static int amphibious_extra_cost(const struct tile *ptile, - enum known_type known, - const struct pf_parameter *param) +static unsigned amphibious_extra_cost(const struct tile *ptile, + enum known_type known, + const struct pf_parameter *param) { struct pft_amphibious *amphibious = param->data; const bool ferry_move = is_native_tile(amphibious->sea.utype, ptile); - int cost, scale; + unsigned cost; + int scale; if (known == TILE_UNKNOWN) { /* We can travel almost anywhere */ @@ -474,6 +479,7 @@ static int amphibious_extra_cost(const struct tile *ptile, if (cost != PF_IMPOSSIBLE_MC) { cost *= scale; } + return cost; } diff --git a/server/advisors/advgoto.c b/server/advisors/advgoto.c index 74a1a9b321..077a546b67 100644 --- a/server/advisors/advgoto.c +++ b/server/advisors/advgoto.c @@ -531,9 +531,9 @@ static double chance_killed_at(const struct tile *ptile, - Therefore the total (re)build cost is a good representation of the the cost of destruction. **************************************************************************/ -static int stack_risk(const struct tile *ptile, - struct adv_risk_cost *risk_cost, - const struct pf_parameter *param) +static unsigned stack_risk(const struct tile *ptile, + struct adv_risk_cost *risk_cost, + const struct pf_parameter *param) { double risk = 0; /* Compute the risk of destruction, assuming we will stop at this tile */ @@ -566,9 +566,9 @@ static int stack_risk(const struct tile *ptile, Avoiding tall stacks *all* along a path is useful because a unit following a path might have to stop early because of ZoCs. **************************************************************************/ -static int prefer_short_stacks(const struct tile *ptile, - enum known_type known, - const struct pf_parameter *param) +static unsigned prefer_short_stacks(const struct tile *ptile, + enum known_type known, + const struct pf_parameter *param) { return stack_risk(ptile, (struct adv_risk_cost *)param->data, param); } -- 2.39.2