Freeciv21
Develop your civilization from humble roots to a global empire
path_finding.cpp File Reference
#include "bitvector.h"
#include "genhash.h"
#include "log.h"
#include "game.h"
#include "map.h"
#include "movement.h"
#include "pf_tools.h"
#include <QDebug>
#include <QString>
#include "path_finding.h"
#include "specpq.h"
+ Include dependency graph for path_finding.cpp:

Go to the source code of this file.

Classes

struct  pf_map
 
struct  pf_normal_node
 
struct  pf_normal_map
 
struct  pf_danger_node
 
struct  pf_danger_node::pf_danger_pos
 
struct  pf_danger_map
 
struct  pf_fuel_node
 
struct  pf_fuel_pos
 
struct  pf_fuel_map
 
struct  pf_reverse_map
 

Macros

#define SPECPQ_TAG   map_index
 
#define SPECPQ_DATA_TYPE   int
 
#define SPECPQ_PRIORITY_TYPE   int
 
#define INITIAL_QUEUE_SIZE   100
 
#define PF_NORMAL_MAP(pfm)   ((struct pf_normal_map *) (pfm))
 
#define PF_DANGER_MAP(pfm)   ((struct pf_danger_map *) (pfm))
 
#define PF_FUEL_MAP(pfm)   ((struct pf_fuel_map *) (pfm))
 

Enumerations

enum  pf_node_status {
  NS_UNINIT = 0 , NS_INIT , NS_NEW , NS_WAITING ,
  NS_PROCESSED
}
 
enum  pf_zoc_type { ZOC_MINE = 0 , ZOC_ALLIED , ZOC_NO }
 

Functions

pf_mapPF_MAP (void *x)
 Casts to pf_map More...
 
const pf_mapPF_MAP (const void *x)
 Casts to pf_map More...
 
static int pf_moves_left_initially (const struct pf_parameter *param)
 Return the number of "moves" started with. More...
 
static int pf_move_rate (const struct pf_parameter *param)
 Return the "move rate". More...
 
static int pf_turns (const struct pf_parameter *param, int cost)
 Number of turns required to reach node. More...
 
static int pf_moves_left (const struct pf_parameter *param, int cost)
 Moves left after node is reached. More...
 
static int pf_total_CC (const struct pf_parameter *param, int cost, int extra)
 Obtain cost-of-path from pure cost and extra cost. More...
 
static void pf_finalize_position (const struct pf_parameter *param, struct pf_position *pos)
 Take a position previously filled out (as by fill_position) and "finalize" it by reversing all fuel multipliers. More...
 
static void pf_position_fill_start_tile (struct pf_position *pos, const struct pf_parameter *param)
 Fill the position for the start tile of a parameter. More...
 
static bool pf_normal_node_init (struct pf_normal_map *pfnm, struct pf_normal_node *node, struct tile *ptile, enum pf_move_scope previous_scope)
 Calculates cached values of the target node. More...
 
static void pf_normal_map_fill_position (const struct pf_normal_map *pfnm, struct tile *ptile, struct pf_position *pos)
 Fill in the position which must be discovered already. More...
 
static PFPath pf_normal_map_construct_path (const struct pf_normal_map *pfnm, struct tile *dest_tile)
 Read off the path to the node dest_tile, which must already be discovered. More...
 
static int pf_normal_map_adjust_cost (int cost, int moves_left)
 Adjust MC to reflect the move_rate. More...
 
static bool pf_jumbo_map_iterate (struct pf_map *pfm)
 Bare-bones PF iterator. More...
 
static bool pf_normal_map_iterate (struct pf_map *pfm)
 Primary method for iterative path-finding. More...
 
static bool pf_normal_map_iterate_until (struct pf_normal_map *pfnm, struct tile *ptile)
 Iterate the map until 'ptile' is reached. More...
 
static int pf_normal_map_move_cost (struct pf_map *pfm, struct tile *ptile)
 Return the move cost at ptile. More...
 
static PFPath pf_normal_map_path (struct pf_map *pfm, struct tile *ptile)
 Return the path to ptile. More...
 
static bool pf_normal_map_position (struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
 Get info about position at ptile and put it in pos. More...
 
static void pf_normal_map_destroy (struct pf_map *pfm)
 'pf_normal_map' destructor. More...
 
static struct pf_mappf_normal_map_new (const struct pf_parameter *parameter)
 'pf_normal_map' constructor. More...
 
static bool pf_danger_node_init (struct pf_danger_map *pfdm, struct pf_danger_node *node, struct tile *ptile, enum pf_move_scope previous_scope)
 Calculates cached values of the target node. More...
 
static void pf_danger_map_fill_position (const struct pf_danger_map *pfdm, struct tile *ptile, struct pf_position *pos)
 Fill in the position which must be discovered already. More...
 
static int pf_danger_map_fill_cost_for_full_moves (const struct pf_parameter *param, int cost)
 This function returns the fills the cost needed for a position, to get full moves at the next turn. More...
 
static PFPath pf_danger_map_construct_path (const struct pf_danger_map *pfdm, struct tile *ptile)
 Read off the path to the node 'ptile', but with dangers. More...
 
static void pf_danger_map_create_segment (struct pf_danger_map *pfdm, struct pf_danger_node *node1)
 Creating path segment going back from node1 to a safe tile. More...
 
static int pf_danger_map_adjust_cost (const struct pf_parameter *params, int cost, bool to_danger, int moves_left)
 Adjust cost taking into account possibility of making the move. More...
 
static bool pf_danger_map_iterate (struct pf_map *pfm)
 Primary method for iterative path-finding in presence of danger Notes: More...
 
static bool pf_danger_map_iterate_until (struct pf_danger_map *pfdm, struct tile *ptile)
 Iterate the map until 'ptile' is reached. More...
 
static int pf_danger_map_move_cost (struct pf_map *pfm, struct tile *ptile)
 Return the move cost at ptile. More...
 
static PFPath pf_danger_map_path (struct pf_map *pfm, struct tile *ptile)
 Return the path to ptile. More...
 
static bool pf_danger_map_position (struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
 Get info about position at ptile and put it in pos . More...
 
static void pf_danger_map_destroy (struct pf_map *pfm)
 'pf_danger_map' destructor. More...
 
static struct pf_mappf_danger_map_new (const struct pf_parameter *parameter)
 'pf_danger_map' constructor. More...
 
static int pf_fuel_total_CC (const struct pf_parameter *param, int cost, int extra, int safety)
 Obtain cost-of-path from pure cost, extra cost and safety. More...
 
static int pf_fuel_waited_total_CC (int cost, int safety)
 Obtain cost-of-path for constant extra cost (used for node after waited). More...
 
static bool pf_fuel_node_init (struct pf_fuel_map *pffm, struct pf_fuel_node *node, struct tile *ptile, enum pf_move_scope previous_scope)
 Calculates cached values of the target node. More...
 
static bool pf_fuel_node_dangerous (const struct pf_fuel_node *node)
 Returns whether this node is dangerous or not. More...
 
static struct pf_fuel_pospf_fuel_pos_ref (struct pf_fuel_pos *pos)
 Forget how we went to position. More...
 
static void pf_fuel_pos_unref (struct pf_fuel_pos *pos)
 Forget how we went to position. More...
 
static struct pf_fuel_pospf_fuel_pos_replace (struct pf_fuel_pos *pos, const struct pf_fuel_node *node)
 Replace the position (unreferences it). More...
 
static void pf_fuel_finalize_position_base (const struct pf_parameter *param, struct pf_position *pos, int cost, int moves_left)
 Finalize the fuel position. More...
 
static void pf_fuel_finalize_position (struct pf_position *pos, const struct pf_parameter *params, const struct pf_fuel_node *node, const struct pf_fuel_pos *head)
 Finalize the fuel position. More...
 
static void pf_fuel_map_fill_position (const struct pf_fuel_map *pffm, struct tile *ptile, struct pf_position *pos)
 Fill in the position which must be discovered already. More...
 
static int pf_fuel_map_fill_cost_for_full_moves (const struct pf_parameter *param, int cost, int moves_left)
 This function returns the fill cost needed for a position, to get full moves at the next turn. More...
 
static PFPath pf_fuel_map_construct_path (const struct pf_fuel_map *pffm, struct tile *ptile)
 Read off the path to the node 'ptile', but with fuel danger. More...
 
static void pf_fuel_map_create_segment (struct pf_fuel_map *pffm, struct tile *ptile, struct pf_fuel_node *node)
 Creating path segment going back from node1 to a safe tile. More...
 
static int pf_fuel_map_adjust_cost (int cost, int moves_left, int move_rate)
 Adjust cost for move_rate and fuel usage. More...
 
static bool pf_fuel_map_attack_is_possible (const struct pf_parameter *param, int moves_left, int moves_left_req)
 This function returns whether a unit with or without fuel can attack. More...
 
static bool pf_fuel_map_iterate (struct pf_map *pfm)
 Primary method for iterative path-finding for fuel units. More...
 
static bool pf_fuel_map_iterate_until (struct pf_fuel_map *pffm, struct tile *ptile)
 Iterate the map until 'ptile' is reached. More...
 
static int pf_fuel_map_move_cost (struct pf_map *pfm, struct tile *ptile)
 Return the move cost at ptile. More...
 
static PFPath pf_fuel_map_path (struct pf_map *pfm, struct tile *ptile)
 Return the path to ptile. More...
 
static bool pf_fuel_map_position (struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
 Get info about position at ptile and put it in pos. More...
 
static void pf_fuel_map_destroy (struct pf_map *pfm)
 'pf_fuel_map' destructor. More...
 
static struct pf_mappf_fuel_map_new (const struct pf_parameter *parameter)
 'pf_fuel_map' constructor. More...
 
struct pf_mappf_map_new (const struct pf_parameter *parameter)
 Factory function to create a new map according to the parameter. More...
 
void pf_map_destroy (struct pf_map *pfm)
 After usage the map must be destroyed. More...
 
int pf_map_move_cost (struct pf_map *pfm, struct tile *ptile)
 Tries to find the minimal move cost to reach ptile. More...
 
PFPath pf_map_path (struct pf_map *pfm, struct tile *ptile)
 CHECK DOCS AFTER FULL CONVERSTION OF pf_path to class PFPath Tries to find the best path in the given map to the position ptile. More...
 
bool pf_map_position (struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
 Get info about position at ptile and put it in pos. More...
 
bool pf_map_iterate (struct pf_map *pfm)
 Iterates the path-finding algorithm one step further, to the next nearest position. More...
 
struct tilepf_map_iter (struct pf_map *pfm)
 Return the current tile. More...
 
int pf_map_iter_move_cost (struct pf_map *pfm)
 Return the move cost at the current position. More...
 
PFPath pf_map_iter_path (struct pf_map *pfm)
 Return the path to our current position.This is equivalent to pf_map_path(pfm, pf_map_iter(pfm)). More...
 
void pf_map_iter_position (struct pf_map *pfm, struct pf_position *pos)
 Read all info about the current position into pos. More...
 
const struct pf_parameterpf_map_parameter (const struct pf_map *pfm)
 Return the pf_parameter for given pf_map. More...
 
QDebug & operator<< (QDebug &logger, const PFPath &path)
 Debug a path. More...
 
bool operator== (const pf_parameter &e1, const pf_parameter &e2)
 
struct pf_reverse_mappf_reverse_map_new (const struct player *pplayer, struct tile *target_tile, int max_turns, bool omniscient, const struct civ_map *map)
 'pf_reverse_map' constructor. More...
 
struct pf_reverse_mappf_reverse_map_new_for_city (const struct city *pcity, const struct player *attacker, int max_turns, bool omniscient, const struct civ_map *map)
 'pf_reverse_map' constructor for city. More...
 
void pf_reverse_map_destroy (struct pf_reverse_map *pfrm)
 'pf_reverse_map' destructor. More...
 
static const struct pf_positionpf_reverse_map_pos (struct pf_reverse_map *pfrm, const struct pf_parameter *param)
 Returns the map for the unit type. More...
 
static const struct pf_positionpf_reverse_map_unit_pos (struct pf_reverse_map *pfrm, const struct unit *punit)
 Returns the position for the unit. More...
 
int pf_reverse_map_unit_move_cost (struct pf_reverse_map *pfrm, const struct unit *punit)
 Get the move costs that a unit needs to reach the start tile. More...
 
bool pf_reverse_map_unit_position (struct pf_reverse_map *pfrm, const struct unit *punit, struct pf_position *pos)
 Fill the position. More...
 

Variables

static enum unit_type_flag_id signifiant_flags [3]
 

Macro Definition Documentation

◆ INITIAL_QUEUE_SIZE

#define INITIAL_QUEUE_SIZE   100

Definition at line 36 of file path_finding.cpp.

◆ PF_DANGER_MAP

#define PF_DANGER_MAP (   pfm)    ((struct pf_danger_map *) (pfm))

Definition at line 965 of file path_finding.cpp.

◆ PF_FUEL_MAP

#define PF_FUEL_MAP (   pfm)    ((struct pf_fuel_map *) (pfm))

Definition at line 1933 of file path_finding.cpp.

◆ PF_NORMAL_MAP

#define PF_NORMAL_MAP (   pfm)    ((struct pf_normal_map *) (pfm))

Definition at line 251 of file path_finding.cpp.

◆ SPECPQ_DATA_TYPE

#define SPECPQ_DATA_TYPE   int

Definition at line 33 of file path_finding.cpp.

◆ SPECPQ_PRIORITY_TYPE

#define SPECPQ_PRIORITY_TYPE   int

Definition at line 34 of file path_finding.cpp.

◆ SPECPQ_TAG

#define SPECPQ_TAG   map_index

Definition at line 32 of file path_finding.cpp.

Enumeration Type Documentation

◆ pf_node_status

Enumerator
NS_UNINIT 
NS_INIT 
NS_NEW 
NS_WAITING 
NS_PROCESSED 

Definition at line 54 of file path_finding.cpp.

◆ pf_zoc_type

Enumerator
ZOC_MINE 
ZOC_ALLIED 
ZOC_NO 

Definition at line 67 of file path_finding.cpp.

Function Documentation

◆ operator<<()

QDebug& operator<< ( QDebug &  logger,
const PFPath path 
)

Debug a path.

Definition at line 3310 of file path_finding.cpp.

◆ operator==()

bool operator== ( const pf_parameter e1,
const pf_parameter e2 
)
inline

Definition at line 3344 of file path_finding.cpp.

◆ pf_danger_map_adjust_cost()

static int pf_danger_map_adjust_cost ( const struct pf_parameter params,
int  cost,
bool  to_danger,
int  moves_left 
)
inlinestatic

Adjust cost taking into account possibility of making the move.

Definition at line 1358 of file path_finding.cpp.

Referenced by pf_danger_map_iterate().

◆ pf_danger_map_construct_path()

static PFPath pf_danger_map_construct_path ( const struct pf_danger_map pfdm,
struct tile ptile 
)
static

Read off the path to the node 'ptile', but with dangers.

NB: will only find paths to safe tiles!

Definition at line 1143 of file path_finding.cpp.

Referenced by pf_danger_map_path().

◆ pf_danger_map_create_segment()

static void pf_danger_map_create_segment ( struct pf_danger_map pfdm,
struct pf_danger_node node1 
)
static

Creating path segment going back from node1 to a safe tile.

We need to remember the whole segment because any node can be crossed by many danger segments.

Example: be A, B, C and D points safe positions, E a dangerous one. A B E C D We have found dangerous path from A to D, and later one from C to B: A B A B \ / C D C D If we didn't save the segment from A to D when a new segment passing by E is found, then finding the path from A to D will produce an error. (The path is always done in reverse order.) D->dir_to_here will point to E, which is correct, but E->dir_to_here will point to C.

Definition at line 1301 of file path_finding.cpp.

Referenced by pf_danger_map_iterate().

◆ pf_danger_map_destroy()

static void pf_danger_map_destroy ( struct pf_map pfm)
static

'pf_danger_map' destructor.

Definition at line 1767 of file path_finding.cpp.

Referenced by pf_danger_map_new().

◆ pf_danger_map_fill_cost_for_full_moves()

static int pf_danger_map_fill_cost_for_full_moves ( const struct pf_parameter param,
int  cost 
)
inlinestatic

This function returns the fills the cost needed for a position, to get full moves at the next turn.

This would be called only when the status is NS_WAITING.

Definition at line 1127 of file path_finding.cpp.

Referenced by pf_danger_map_construct_path(), and pf_danger_map_iterate().

◆ pf_danger_map_fill_position()

static void pf_danger_map_fill_position ( const struct pf_danger_map pfdm,
struct tile ptile,
struct pf_position pos 
)
static

Fill in the position which must be discovered already.

A helper for pf_danger_map_position(). This also "finalizes" the position.

Definition at line 1088 of file path_finding.cpp.

Referenced by pf_danger_map_position().

◆ pf_danger_map_iterate()

static bool pf_danger_map_iterate ( struct pf_map pfm)
static

Primary method for iterative path-finding in presence of danger Notes:

  1. Whenever the path-finding stumbles upon a dangerous location, it goes into a sub-Dijkstra which processes only dangerous locations, by means of a separate queue. When this sub-Dijkstra reaches a safe location, it records the segment of the path going across the dangerous tiles. Hence danger_segment is an extended (and reversed) version of the dir_to_here field (see comment for pf_danger_map_create_segment()). It can be re-recorded multiple times as we find shorter and shorter routes.
  2. Waiting is realised by inserting the (safe) tile back into the queue with a lower priority P. This tile might pop back sooner than P, because there might be several copies of it in the queue already. But that does not seem to present any problems.
  3. For some purposes, NS_WAITING is just another flavour of NS_PROCESSED, since the path to a NS_WAITING tile has already been found.
  4. This algorithm cannot guarantee the best safe segments across dangerous region. However it will find a safe segment if there is one. To gurantee the best (in terms of total_CC) safe segments across danger, supply 'get_EC' which returns small extra on dangerous tiles.

During the iteration, the node status will be changed: A. NS_UNINIT: The node is not initialized, we didn't reach it at all. B. NS_INIT: We have initialized the cached values, however, we failed to reach this node. C. NS_NEW: We have reached this node, but we are not sure it was the best path. Dangerous tiles never get upper status. D. NS_PROCESSED: Now, we are sure we have the best path. E. NS_WAITING: The safe node (never the dangerous ones) is re-inserted in the priority queue, as explained above (2.). We need to consider if waiting for full moves open or not new possibilities for moving accross dangerous areas. F. NS_PROCESSED: When finished to consider waiting at the node, revert the status to NS_PROCESSED. In points D., E., and F., the best path to the node can be considered as found (only safe nodes).

Definition at line 1413 of file path_finding.cpp.

Referenced by pf_danger_map_new().

◆ pf_danger_map_iterate_until()

static bool pf_danger_map_iterate_until ( struct pf_danger_map pfdm,
struct tile ptile 
)
inlinestatic

Iterate the map until 'ptile' is reached.

Definition at line 1675 of file path_finding.cpp.

Referenced by pf_danger_map_move_cost(), pf_danger_map_path(), and pf_danger_map_position().

◆ pf_danger_map_move_cost()

static int pf_danger_map_move_cost ( struct pf_map pfm,
struct tile ptile 
)
static

Return the move cost at ptile.

If ptile has not been reached yet, iterate the map until we reach it or run out of map. This function returns PF_IMPOSSIBLE_MC on unreachable positions.

Definition at line 1711 of file path_finding.cpp.

Referenced by pf_danger_map_new().

◆ pf_danger_map_new()

static struct pf_map* pf_danger_map_new ( const struct pf_parameter parameter)
static

'pf_danger_map' constructor.

Definition at line 1787 of file path_finding.cpp.

Referenced by pf_map_new().

◆ pf_danger_map_path()

static PFPath pf_danger_map_path ( struct pf_map pfm,
struct tile ptile 
)
static

Return the path to ptile.

If ptile has not been reached yet, iterate the map until we reach it or run out of map.

Definition at line 1730 of file path_finding.cpp.

Referenced by pf_danger_map_new().

◆ pf_danger_map_position()

static bool pf_danger_map_position ( struct pf_map pfm,
struct tile ptile,
struct pf_position pos 
)
static

Get info about position at ptile and put it in pos .

If ptile has not been reached yet, iterate the map until we reach it. Should always check the return value, for the position might be unreachable.

Definition at line 1748 of file path_finding.cpp.

Referenced by pf_danger_map_new().

◆ pf_danger_node_init()

static bool pf_danger_node_init ( struct pf_danger_map pfdm,
struct pf_danger_node node,
struct tile ptile,
enum pf_move_scope  previous_scope 
)
inlinestatic

Calculates cached values of the target node.

Set the node status to NS_INIT to avoid recalculating all values. Returns FALSE if we cannot enter node (in this case, most of the cached values are not set).

Definition at line 975 of file path_finding.cpp.

Referenced by pf_danger_map_iterate(), pf_danger_map_iterate_until(), and pf_danger_map_new().

◆ pf_finalize_position()

static void pf_finalize_position ( const struct pf_parameter param,
struct pf_position pos 
)
inlinestatic

Take a position previously filled out (as by fill_position) and "finalize" it by reversing all fuel multipliers.

See pf_moves_left_initially() and pf_move_rate().

Definition at line 194 of file path_finding.cpp.

Referenced by pf_danger_map_construct_path(), pf_danger_map_fill_position(), and pf_normal_map_fill_position().

◆ pf_fuel_finalize_position()

static void pf_fuel_finalize_position ( struct pf_position pos,
const struct pf_parameter params,
const struct pf_fuel_node node,
const struct pf_fuel_pos head 
)
inlinestatic

Finalize the fuel position.

If we have a fuel segment, then use it.

Definition at line 2185 of file path_finding.cpp.

Referenced by pf_fuel_map_construct_path(), and pf_fuel_map_fill_position().

◆ pf_fuel_finalize_position_base()

static void pf_fuel_finalize_position_base ( const struct pf_parameter param,
struct pf_position pos,
int  cost,
int  moves_left 
)
inlinestatic

Finalize the fuel position.

Definition at line 2163 of file path_finding.cpp.

Referenced by pf_fuel_finalize_position().

◆ pf_fuel_map_adjust_cost()

static int pf_fuel_map_adjust_cost ( int  cost,
int  moves_left,
int  move_rate 
)
inlinestatic

Adjust cost for move_rate and fuel usage.

Definition at line 2411 of file path_finding.cpp.

Referenced by pf_fuel_map_iterate().

◆ pf_fuel_map_attack_is_possible()

static bool pf_fuel_map_attack_is_possible ( const struct pf_parameter param,
int  moves_left,
int  moves_left_req 
)
inlinestatic

This function returns whether a unit with or without fuel can attack.

moves_left: moves left before the attack. moves_left_req: required moves left to hold on the tile after attacking.

Definition at line 2434 of file path_finding.cpp.

Referenced by pf_fuel_map_iterate().

◆ pf_fuel_map_construct_path()

static PFPath pf_fuel_map_construct_path ( const struct pf_fuel_map pffm,
struct tile ptile 
)
static

Read off the path to the node 'ptile', but with fuel danger.

Definition at line 2243 of file path_finding.cpp.

Referenced by pf_fuel_map_path().

◆ pf_fuel_map_create_segment()

static void pf_fuel_map_create_segment ( struct pf_fuel_map pffm,
struct tile ptile,
struct pf_fuel_node node 
)
inlinestatic

Creating path segment going back from node1 to a safe tile.

We need to remember the whole segment because any node can be crossed by many fuel segments.

Example: be A, a refuel point, B and C not. We start the path from B and have only (3 * SINGLE_MOVE) moves lefts: A B C B cannot move to C because we would have only (1 * SINGLE_MOVE) move left reaching it, and the refuel point is too far. So C->moves_left_req = (4 * SINGLE_MOVE). The path would be to return to A, wait the end of turn to get full moves, and go to C. In a single line: B A B C. So, the point B would be reached twice. but, it needs to stores different data for B->cost, B->extra_cost, B->moves_left, and B->dir_to_here. That's why we need to record every path to unsafe nodes (not for refuel points).

Definition at line 2375 of file path_finding.cpp.

Referenced by pf_fuel_map_iterate().

◆ pf_fuel_map_destroy()

static void pf_fuel_map_destroy ( struct pf_map pfm)
static

'pf_fuel_map' destructor.

Definition at line 2934 of file path_finding.cpp.

Referenced by pf_fuel_map_new().

◆ pf_fuel_map_fill_cost_for_full_moves()

static int pf_fuel_map_fill_cost_for_full_moves ( const struct pf_parameter param,
int  cost,
int  moves_left 
)
inlinestatic

This function returns the fill cost needed for a position, to get full moves at the next turn.

This would be called only when the status is NS_WAITING.

Definition at line 2231 of file path_finding.cpp.

Referenced by pf_fuel_map_iterate().

◆ pf_fuel_map_fill_position()

static void pf_fuel_map_fill_position ( const struct pf_fuel_map pffm,
struct tile ptile,
struct pf_position pos 
)
static

Fill in the position which must be discovered already.

A helper for pf_fuel_map_position(). This also "finalizes" the position.

Definition at line 2202 of file path_finding.cpp.

Referenced by pf_fuel_map_position().

◆ pf_fuel_map_iterate()

static bool pf_fuel_map_iterate ( struct pf_map pfm)
static

Primary method for iterative path-finding for fuel units.

Notes:

  1. We process nodes in the main queue, like for normal maps. Because we process in a different queue common tiles (!= refuel points), we needed to register every path to any tile from a refuel point or the start tile (see comment for pf_fuel_map_create_segment()).
  2. Waiting is realised by inserting the refuel point back into the main queue with a lower priority P. Because this tile might pop back sooner than P, because there might be several copies of it in the queue already, we must delete all these copies, to preserve the priority of the process.
  3. For some purposes, NS_WAITING is just another flavour of NS_PROCESSED, since the path to a NS_WAITING tile has already been found.
  4. This algorithm cannot guarantee the best safe segments across dangerous region. However it will find a safe segment if there is one. To gurantee the best (in terms of total_CC) safe segments across danger, supply 'get_EC' which returns small extra on dangerous tiles.

During the iteration, the node status will be changed: A. NS_UNINIT: The node is not initialized, we didn't reach it at all. B. NS_INIT: We have initialized the cached values, however, we failed to reach this node. C. NS_NEW: We have reached this node, but we are not sure it was the best path. D. NS_PROCESSED: Now, we are sure we have the best path. Not refuel node can even be processed. E. NS_WAITING: The refuel node is re-inserted in the priority queue, as explained above (2.). We need to consider if waiting for full moves open or not new possibilities for moving. F. NS_PROCESSED: When finished to consider waiting at the node, revert the status to NS_PROCESSED. In points D., E., and F., the best path to the node can be considered as found.

Definition at line 2489 of file path_finding.cpp.

Referenced by pf_fuel_map_new().

◆ pf_fuel_map_iterate_until()

static bool pf_fuel_map_iterate_until ( struct pf_fuel_map pffm,
struct tile ptile 
)
inlinestatic

Iterate the map until 'ptile' is reached.

Definition at line 2842 of file path_finding.cpp.

Referenced by pf_fuel_map_move_cost(), pf_fuel_map_path(), and pf_fuel_map_position().

◆ pf_fuel_map_move_cost()

static int pf_fuel_map_move_cost ( struct pf_map pfm,
struct tile ptile 
)
static

Return the move cost at ptile.

If 'ptile' has not been reached yet, iterate the map until we reach it or run out of map. This function returns PF_IMPOSSIBLE_MC on unreachable positions.

Definition at line 2877 of file path_finding.cpp.

Referenced by pf_fuel_map_new().

◆ pf_fuel_map_new()

static struct pf_map* pf_fuel_map_new ( const struct pf_parameter parameter)
static

'pf_fuel_map' constructor.

Definition at line 2954 of file path_finding.cpp.

Referenced by pf_map_new().

◆ pf_fuel_map_path()

static PFPath pf_fuel_map_path ( struct pf_map pfm,
struct tile ptile 
)
static

Return the path to ptile.

If 'ptile' has not been reached yet, iterate the map until we reach it or run out of map.

Definition at line 2897 of file path_finding.cpp.

Referenced by pf_fuel_map_new().

◆ pf_fuel_map_position()

static bool pf_fuel_map_position ( struct pf_map pfm,
struct tile ptile,
struct pf_position pos 
)
static

Get info about position at ptile and put it in pos.

If 'ptile' has not been reached yet, iterate the map until we reach it. Should always check the return value, forthe position might be unreachable.

Definition at line 2915 of file path_finding.cpp.

Referenced by pf_fuel_map_new().

◆ pf_fuel_node_dangerous()

static bool pf_fuel_node_dangerous ( const struct pf_fuel_node node)
inlinestatic

Returns whether this node is dangerous or not.

Definition at line 2094 of file path_finding.cpp.

Referenced by pf_fuel_map_iterate().

◆ pf_fuel_node_init()

static bool pf_fuel_node_init ( struct pf_fuel_map pffm,
struct pf_fuel_node node,
struct tile ptile,
enum pf_move_scope  previous_scope 
)
inlinestatic

Calculates cached values of the target node.

Set the node status to NS_INIT to avoid recalculating all values. Returns FALSE if we cannot enter node (in this case, most of the cached values are not set).

Definition at line 1960 of file path_finding.cpp.

Referenced by pf_fuel_map_iterate(), pf_fuel_map_iterate_until(), and pf_fuel_map_new().

◆ pf_fuel_pos_ref()

static struct pf_fuel_pos* pf_fuel_pos_ref ( struct pf_fuel_pos pos)
inlinestatic

Forget how we went to position.

Maybe destroy the position, and previous ones.

Definition at line 2105 of file path_finding.cpp.

Referenced by pf_fuel_map_create_segment(), pf_fuel_map_iterate(), and pf_fuel_map_new().

◆ pf_fuel_pos_replace()

static struct pf_fuel_pos* pf_fuel_pos_replace ( struct pf_fuel_pos pos,
const struct pf_fuel_node node 
)
inlinestatic

Replace the position (unreferences it).

Instead of destroying, re-use the memory, else return a newly allocated position.

Definition at line 2135 of file path_finding.cpp.

Referenced by pf_fuel_map_create_segment(), and pf_fuel_map_new().

◆ pf_fuel_pos_unref()

static void pf_fuel_pos_unref ( struct pf_fuel_pos pos)
inlinestatic

Forget how we went to position.

Maybe destroy the position, and previous ones.

Definition at line 2120 of file path_finding.cpp.

Referenced by pf_fuel_map_destroy(), and pf_fuel_pos_replace().

◆ pf_fuel_total_CC()

static int pf_fuel_total_CC ( const struct pf_parameter param,
int  cost,
int  extra,
int  safety 
)
inlinestatic

Obtain cost-of-path from pure cost, extra cost and safety.

Definition at line 1941 of file path_finding.cpp.

Referenced by pf_fuel_map_iterate().

◆ pf_fuel_waited_total_CC()

static int pf_fuel_waited_total_CC ( int  cost,
int  safety 
)
inlinestatic

Obtain cost-of-path for constant extra cost (used for node after waited).

Definition at line 1950 of file path_finding.cpp.

Referenced by pf_fuel_map_iterate().

◆ pf_jumbo_map_iterate()

static bool pf_jumbo_map_iterate ( struct pf_map pfm)
static

Bare-bones PF iterator.

All Freeciv rules logic is hidden in 'get_costs' callback (compare to pf_normal_map_iterate function). This function is called when the pf_map was created with a 'get_cost' callback.

Plan: 1. Process previous position.

  1. Get new nearest position and return it.

During the iteration, the node status will be changed: A. NS_UNINIT: The node is not initialized, we didn't reach it at all. (NS_INIT not used here) B. NS_NEW: We have reached this node, but we are not sure it was the best path. (NS_WAITING not used here) C. NS_PROCESSED: Now, we are sure we have the best path. Then, we won't do anything more with this node.

Definition at line 486 of file path_finding.cpp.

Referenced by pf_normal_map_new().

◆ PF_MAP() [1/2]

const pf_map* PF_MAP ( const void *  x)

Casts to pf_map

Todo:
Capitalized because this used to be a macro.

Definition at line 104 of file path_finding.cpp.

◆ PF_MAP() [2/2]

◆ pf_map_destroy()

◆ pf_map_iter()

struct tile* pf_map_iter ( struct pf_map pfm)

Return the current tile.

Definition at line 3160 of file path_finding.cpp.

◆ pf_map_iter_move_cost()

int pf_map_iter_move_cost ( struct pf_map pfm)

Return the move cost at the current position.

This is equivalent to pf_map_move_cost(pfm, pf_map_iter(pfm)).

Definition at line 3172 of file path_finding.cpp.

◆ pf_map_iter_path()

PFPath pf_map_iter_path ( struct pf_map pfm)

Return the path to our current position.This is equivalent to pf_map_path(pfm, pf_map_iter(pfm)).

Definition at line 3185 of file path_finding.cpp.

◆ pf_map_iter_position()

void pf_map_iter_position ( struct pf_map pfm,
struct pf_position pos 
)

Read all info about the current position into pos.

This is equivalent to pf_map_position(pfm, pf_map_iter(pfm), &pos).

Definition at line 3198 of file path_finding.cpp.

Referenced by process_attacker_want().

◆ pf_map_iterate()

bool pf_map_iterate ( struct pf_map pfm)

Iterates the path-finding algorithm one step further, to the next nearest position.

This full info on this position and the best path to it can be obtained using pf_map_iter_move_cost(), pf_map_iter_path(), and pf_map_iter_position() correspondingly. Returns FALSE if no further positions are available in this map.

NB: If pf_map_move_cost(pfm, ptile), pf_map_path(pfm, ptile), or pf_map_position(pfm, ptile) has been called before the call to pf_map_iterate(), the iteration will resume from 'ptile'.

Definition at line 3136 of file path_finding.cpp.

Referenced by pf_danger_map_iterate_until(), pf_fuel_map_iterate_until(), and pf_normal_map_iterate_until().

◆ pf_map_move_cost()

int pf_map_move_cost ( struct pf_map pfm,
struct tile ptile 
)

Tries to find the minimal move cost to reach ptile.

Returns PF_IMPOSSIBLE_MC if not reachable. If ptile has not been reached yet, iterate the map until we reach it or run out of map.

Definition at line 3068 of file path_finding.cpp.

Referenced by dai_manage_barbarian_leader(), find_beachhead(), and goto_is_sane().

◆ pf_map_new()

◆ pf_map_parameter()

◆ pf_map_path()

PFPath pf_map_path ( struct pf_map pfm,
struct tile ptile 
)

CHECK DOCS AFTER FULL CONVERSTION OF pf_path to class PFPath Tries to find the best path in the given map to the position ptile.

If empty path is returned no path could be found. The pf_path[-1] of such path would be the same (almost) as the result of the call to pf_map_position(). If ptile has not been reached yet, iterate the map until we reach it or run out of map.

Definition at line 3085 of file path_finding.cpp.

Referenced by auto_settler_setup_work(), dai_diplomat_bribe_nearby(), dai_find_strategic_airbase(), dai_hunter_manage(), dai_manage_airunit(), dai_manage_barbarian_leader(), dai_manage_diplomat(), dai_unit_goto_constrained(), explorer_goto(), find_nearest_airbase(), find_rampage_target(), find_something_to_bomb(), find_something_to_kill(), immediate_destination(), player_restore_units(), send_attack_tile(), send_goto_tile(), send_rally_tile(), settler_evaluate_city_requests(), settler_evaluate_improvements(), and tile_before_end_path().

◆ pf_map_position()

bool pf_map_position ( struct pf_map pfm,
struct tile ptile,
struct pf_position pos 
)

Get info about position at ptile and put it in pos.

If ptile has not been reached yet, iterate the map until we reach it. Should always check the return value, forthe position might be unreachable.

Definition at line 3115 of file path_finding.cpp.

Referenced by dai_manage_diplomat(), find_something_to_kill(), process_attacker_want(), settler_evaluate_city_requests(), and settler_evaluate_improvements().

◆ pf_move_rate()

static int pf_move_rate ( const struct pf_parameter param)
inlinestatic

Return the "move rate".

This is different from the parameter's move_rate because of fuel. For units with fuel > 1 all moves on the same tank of fuel are considered to be one turn. Thus the rest of the PF code doesn't actually know that the unit has fuel, it just thinks it has that many more MP.

Definition at line 133 of file path_finding.cpp.

Referenced by pf_danger_map_adjust_cost(), pf_danger_map_construct_path(), pf_danger_map_fill_cost_for_full_moves(), pf_danger_map_fill_position(), pf_danger_map_iterate(), pf_danger_map_move_cost(), pf_danger_map_new(), pf_fuel_map_construct_path(), pf_fuel_map_fill_position(), pf_fuel_map_iterate(), pf_fuel_map_move_cost(), pf_fuel_map_new(), pf_moves_left(), pf_normal_map_fill_position(), pf_normal_map_move_cost(), pf_normal_map_new(), and pf_total_CC().

◆ pf_moves_left()

static int pf_moves_left ( const struct pf_parameter param,
int  cost 
)
inlinestatic

◆ pf_moves_left_initially()

static int pf_moves_left_initially ( const struct pf_parameter param)
inlinestatic

Return the number of "moves" started with.

This is different from the moves_left_initially because of fuel. For units with fuel > 1 all moves on the same tank of fuel are considered to be one turn. Thus the rest of the PF code doesn't actually know that the unit has fuel, it just thinks it has that many more MP.

Definition at line 119 of file path_finding.cpp.

Referenced by pf_danger_map_construct_path(), pf_danger_map_fill_position(), pf_danger_map_move_cost(), pf_danger_map_new(), pf_fuel_map_construct_path(), pf_fuel_map_fill_position(), pf_fuel_map_move_cost(), pf_fuel_map_new(), pf_normal_map_fill_position(), pf_normal_map_move_cost(), and pf_normal_map_new().

◆ pf_normal_map_adjust_cost()

static int pf_normal_map_adjust_cost ( int  cost,
int  moves_left 
)
static

Adjust MC to reflect the move_rate.

Definition at line 462 of file path_finding.cpp.

Referenced by pf_normal_map_iterate().

◆ pf_normal_map_construct_path()

static PFPath pf_normal_map_construct_path ( const struct pf_normal_map pfnm,
struct tile dest_tile 
)
static

Read off the path to the node dest_tile, which must already be discovered.

A helper for pf_normal_map_path functions.

Definition at line 407 of file path_finding.cpp.

Referenced by pf_normal_map_path().

◆ pf_normal_map_destroy()

static void pf_normal_map_destroy ( struct pf_map pfm)
static

'pf_normal_map' destructor.

Definition at line 814 of file path_finding.cpp.

Referenced by pf_normal_map_new().

◆ pf_normal_map_fill_position()

static void pf_normal_map_fill_position ( const struct pf_normal_map pfnm,
struct tile ptile,
struct pf_position pos 
)
static

Fill in the position which must be discovered already.

A helper for pf_normal_map_position(). This also "finalizes" the position.

Definition at line 371 of file path_finding.cpp.

Referenced by pf_normal_map_construct_path(), pf_normal_map_position(), and pf_reverse_map_pos().

◆ pf_normal_map_iterate()

static bool pf_normal_map_iterate ( struct pf_map pfm)
static

Primary method for iterative path-finding.

Plan: 1. Process previous position.

  1. Get new nearest position and return it.

During the iteration, the node status will be changed: A. NS_UNINIT: The node is not initialized, we didn't reach it at all. B. NS_INIT: We have initialized the cached values, however, we failed to reach this node. C. NS_NEW: We have reached this node, but we are not sure it was the best path. (NS_WAITING not used here) D. NS_PROCESSED: Now, we are sure we have the best path. Then, we won't do anything more with this node.

Definition at line 580 of file path_finding.cpp.

Referenced by pf_normal_map_new().

◆ pf_normal_map_iterate_until()

static bool pf_normal_map_iterate_until ( struct pf_normal_map pfnm,
struct tile ptile 
)
inlinestatic

Iterate the map until 'ptile' is reached.

Definition at line 722 of file path_finding.cpp.

Referenced by pf_normal_map_move_cost(), pf_normal_map_path(), and pf_normal_map_position().

◆ pf_normal_map_move_cost()

static int pf_normal_map_move_cost ( struct pf_map pfm,
struct tile ptile 
)
static

Return the move cost at ptile.

If ptile has not been reached yet, iterate the map until we reach it or run out of map. This function returns PF_IMPOSSIBLE_MC on unreachable positions.

Definition at line 758 of file path_finding.cpp.

Referenced by pf_normal_map_new().

◆ pf_normal_map_new()

static struct pf_map* pf_normal_map_new ( const struct pf_parameter parameter)
static

'pf_normal_map' constructor.

Definition at line 826 of file path_finding.cpp.

Referenced by pf_map_new(), and pf_reverse_map_pos().

◆ pf_normal_map_path()

static PFPath pf_normal_map_path ( struct pf_map pfm,
struct tile ptile 
)
static

Return the path to ptile.

If ptile has not been reached yet, iterate the map until we reach it or run out of map.

Definition at line 777 of file path_finding.cpp.

Referenced by pf_normal_map_new().

◆ pf_normal_map_position()

static bool pf_normal_map_position ( struct pf_map pfm,
struct tile ptile,
struct pf_position pos 
)
static

Get info about position at ptile and put it in pos.

If ptile has not been reached yet, iterate the map until we reach it. Should always check the return value, forthe position might be unreachable.

Definition at line 795 of file path_finding.cpp.

Referenced by pf_normal_map_new().

◆ pf_normal_node_init()

static bool pf_normal_node_init ( struct pf_normal_map pfnm,
struct pf_normal_node node,
struct tile ptile,
enum pf_move_scope  previous_scope 
)
inlinestatic

Calculates cached values of the target node.

Set the node status to NS_INIT to avoid recalculating all values. Returns FALSE if we cannot enter node (in this case, most of the cached values are not set).

Definition at line 261 of file path_finding.cpp.

Referenced by pf_normal_map_iterate(), pf_normal_map_iterate_until(), and pf_normal_map_new().

◆ pf_position_fill_start_tile()

static void pf_position_fill_start_tile ( struct pf_position pos,
const struct pf_parameter param 
)
static

Fill the position for the start tile of a parameter.

Definition at line 3226 of file path_finding.cpp.

Referenced by pf_danger_map_position(), pf_fuel_map_construct_path(), pf_fuel_map_position(), pf_normal_map_position(), and PFPath::PFPath().

◆ pf_reverse_map_destroy()

void pf_reverse_map_destroy ( struct pf_reverse_map pfrm)

'pf_reverse_map' destructor.

Definition at line 3433 of file path_finding.cpp.

Referenced by assess_danger(), and dai_manage_barbarian_leader().

◆ pf_reverse_map_new()

struct pf_reverse_map* pf_reverse_map_new ( const struct player pplayer,
struct tile target_tile,
int  max_turns,
bool  omniscient,
const struct civ_map map 
)

'pf_reverse_map' constructor.

If 'max_turns' is positive, then it won't try to iterate the maps beyond this number of turns.

Definition at line 3394 of file path_finding.cpp.

Referenced by dai_manage_barbarian_leader(), and pf_reverse_map_new_for_city().

◆ pf_reverse_map_new_for_city()

struct pf_reverse_map* pf_reverse_map_new_for_city ( const struct city pcity,
const struct player attacker,
int  max_turns,
bool  omniscient,
const struct civ_map map 
)

'pf_reverse_map' constructor for city.

If 'max_turns' is positive, then it won't try to iterate the maps beyond this number of turns.

Definition at line 3422 of file path_finding.cpp.

Referenced by assess_danger().

◆ pf_reverse_map_pos()

static const struct pf_position* pf_reverse_map_pos ( struct pf_reverse_map pfrm,
const struct pf_parameter param 
)
static

Returns the map for the unit type.

Creates it if needed. Returns nullptr if 'target_tile' is unreachable.

Definition at line 3452 of file path_finding.cpp.

Referenced by pf_reverse_map_unit_pos().

◆ pf_reverse_map_unit_move_cost()

int pf_reverse_map_unit_move_cost ( struct pf_reverse_map pfrm,
const struct unit punit 
)

Get the move costs that a unit needs to reach the start tile.

Returns PF_IMPOSSIBLE_MC if the tile is unreachable.

Definition at line 3538 of file path_finding.cpp.

Referenced by dai_manage_barbarian_leader().

◆ pf_reverse_map_unit_pos()

static const struct pf_position* pf_reverse_map_unit_pos ( struct pf_reverse_map pfrm,
const struct unit punit 
)
inlinestatic

Returns the position for the unit.

Creates it if needed. Returns nullptr if 'target_tile' is unreachable.

Definition at line 3518 of file path_finding.cpp.

Referenced by pf_reverse_map_unit_move_cost(), and pf_reverse_map_unit_position().

◆ pf_reverse_map_unit_position()

bool pf_reverse_map_unit_position ( struct pf_reverse_map pfrm,
const struct unit punit,
struct pf_position pos 
)

Fill the position.

Return TRUE if the tile is reachable.

Definition at line 3549 of file path_finding.cpp.

Referenced by assess_danger_unit().

◆ pf_total_CC()

static int pf_total_CC ( const struct pf_parameter param,
int  cost,
int  extra 
)
inlinestatic

Obtain cost-of-path from pure cost and extra cost.

Definition at line 182 of file path_finding.cpp.

Referenced by pf_danger_map_iterate(), pf_fuel_total_CC(), and pf_normal_map_iterate().

◆ pf_turns()

static int pf_turns ( const struct pf_parameter param,
int  cost 
)
inlinestatic

Number of turns required to reach node.

See comment in the body of pf_normal_map_new(), pf_danger_map_new(), and pf_fuel_map_new() functions about the usage of moves_left_initially().

Definition at line 143 of file path_finding.cpp.

Referenced by pf_danger_map_construct_path(), pf_danger_map_fill_position(), pf_fuel_finalize_position_base(), pf_fuel_map_construct_path(), and pf_normal_map_fill_position().

Variable Documentation

◆ signifiant_flags

enum unit_type_flag_id signifiant_flags[3]
static
Initial value:
= {
UTYF_IGTER, UTYF_CIVILIAN, UTYF_COAST_STRICT}

Definition at line 3310 of file path_finding.cpp.

Referenced by operator==().