Freeciv-3.3
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
caravan.c File Reference
#include <math.h>
#include "log.h"
#include "game.h"
#include "traderoutes.h"
#include "path_finding.h"
#include "pf_tools.h"
#include "caravan.h"

Go to the source code of this file.

Data Structures

struct  cfbdw_data
 

Typedefs

typedef bool(* search_callback) (const struct civ_map *nmap, void *data, const struct city *pcity, int arrival_turn, int arrival_moves_left)
 

Functions

void caravan_parameter_init_default (struct caravan_parameter *parameter)
 
void caravan_parameter_init_from_unit (struct caravan_parameter *parameter, const struct unit *caravan)
 
bool caravan_parameter_is_legal (const struct caravan_parameter *parameter)
 
static const charftl_name (enum foreign_trade_limit ftl)
 
void caravan_parameter_log_real (const struct caravan_parameter *parameter, enum log_level level, const char *file, const char *function, int line)
 
void caravan_result_init_zero (struct caravan_result *result)
 
static void caravan_result_init (struct caravan_result *result, const struct city *src, const struct city *dest, int arrival_time)
 
int caravan_result_compare (const struct caravan_result *a, const struct caravan_result *b)
 
static void caravan_search_from (const struct civ_map *nmap, const struct unit *caravan, const struct caravan_parameter *param, struct tile *start_tile, int turns_before, int moves_left_before, bool omniscient, search_callback callback, void *callback_data)
 
static double windfall_benefit (const struct unit *caravan, const struct city *src, const struct city *dest, const struct goods_type *pgood, const struct caravan_parameter *param)
 
static int one_city_trade_benefit (const struct city *pcity, const struct player *pplayer, const struct goods_type *pgood, bool countloser, int newtrade)
 
static double trade_benefit (const struct player *caravan_owner, const struct city *src, const struct city *dest, const struct goods_type *pgood, const struct caravan_parameter *param)
 
static double wonder_benefit (const struct unit *caravan, int arrival_time, const struct city *dest, const struct caravan_parameter *param)
 
static double presentvalue (double payment, int term, double rate)
 
static double perpetuity (double payment, double rate)
 
static double annuity (double payment, int term, double rate)
 
static bool does_foreign_trade_param_allow (const struct caravan_parameter *param, struct player *src, struct player *dest)
 
static bool get_discounted_reward (const struct unit *caravan, const struct caravan_parameter *parameter, struct caravan_result *result)
 
static void caravan_find_best_destination_notransit (const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *best)
 
static bool cfbdw_callback (const struct civ_map *nmap, void *vdata, const struct city *dest, int arrival_time, int moves_left)
 
static void caravan_find_best_destination_withtransit (const struct civ_map *nmap, const struct unit *caravan, const struct caravan_parameter *param, const struct city *src, int turns_before, int moves_left, bool omniscient, struct caravan_result *result)
 
void caravan_find_best_destination (const struct civ_map *nmap, const struct unit *caravan, const struct caravan_parameter *parameter, struct caravan_result *result, bool omniscient)
 

Typedef Documentation

◆ search_callback

We use the path finding in several places. This provides a single implementation of that. It is critical that this function be re-entrant since we call it recursively.

The callback should return TRUE if it wants to stop searching, FALSE otherwise.

Definition at line 204 of file caravan.c.

Function Documentation

◆ annuity()

static double annuity ( double  payment,
int  term,
double  rate 
)
static

Compute the net present value of an annuity given the discount rate. An annuity is an annual payment for a fixed term (number of turns).

Definition at line 446 of file caravan.c.

Referenced by get_discounted_reward().

◆ caravan_find_best_destination()

void caravan_find_best_destination ( const struct civ_map nmap,
const struct unit caravan,
const struct caravan_parameter parameter,
struct caravan_result result,
bool  omniscient 
)

Find the best destination city for the caravan. Store it in *destout (if destout is non-null); return the value of the trade route.

Definition at line 682 of file caravan.c.

Referenced by dai_manage_caravan().

◆ caravan_find_best_destination_notransit()

static void caravan_find_best_destination_notransit ( const struct unit caravan,
const struct caravan_parameter param,
struct caravan_result best 
)
static

Find the best destination for the caravan, ignoring transit time.

Definition at line 595 of file caravan.c.

Referenced by caravan_find_best_destination().

◆ caravan_find_best_destination_withtransit()

static void caravan_find_best_destination_withtransit ( const struct civ_map nmap,
const struct unit caravan,
const struct caravan_parameter param,
const struct city src,
int  turns_before,
int  moves_left,
bool  omniscient,
struct caravan_result result 
)
static

Using caravan_search, find the best destination.

Definition at line 649 of file caravan.c.

Referenced by caravan_find_best_destination().

◆ caravan_parameter_init_default()

void caravan_parameter_init_default ( struct caravan_parameter parameter)

Create a valid parameter with default values.

Definition at line 36 of file caravan.c.

Referenced by caravan_parameter_init_from_unit().

◆ caravan_parameter_init_from_unit()

void caravan_parameter_init_from_unit ( struct caravan_parameter parameter,
const struct unit caravan 
)

Create a valid parameter with default values based on the caravan.

Definition at line 53 of file caravan.c.

Referenced by dai_manage_caravan().

◆ caravan_parameter_is_legal()

bool caravan_parameter_is_legal ( const struct caravan_parameter parameter)

Check for legality.

Definition at line 72 of file caravan.c.

◆ caravan_parameter_log_real()

void caravan_parameter_log_real ( const struct caravan_parameter parameter,
enum log_level  level,
const char file,
const char function,
int  line 
)

For debugging, print out the parameter.

Definition at line 115 of file caravan.c.

◆ caravan_result_compare()

int caravan_result_compare ( const struct caravan_result a,
const struct caravan_result b 
)

Compare the two results for sorting.

Definition at line 183 of file caravan.c.

Referenced by caravan_find_best_destination_notransit(), and cfbdw_callback().

◆ caravan_result_init()

static void caravan_result_init ( struct caravan_result result,
const struct city src,
const struct city dest,
int  arrival_time 
)
static

Initialize the result to go from src to dest with the given amount of time. This is useful for calling get_discounted_reward() and the such.

Definition at line 157 of file caravan.c.

Referenced by caravan_find_best_destination_notransit(), caravan_find_best_destination_withtransit(), and cfbdw_callback().

◆ caravan_result_init_zero()

void caravan_result_init_zero ( struct caravan_result result)

Initialize the result to be worth zero and go from nowhere to nowhere.

Definition at line 144 of file caravan.c.

Referenced by get_discounted_reward().

◆ caravan_search_from()

static void caravan_search_from ( const struct civ_map nmap,
const struct unit caravan,
const struct caravan_parameter param,
struct tile start_tile,
int  turns_before,
int  moves_left_before,
bool  omniscient,
search_callback  callback,
void callback_data 
)
static

Definition at line 208 of file caravan.c.

Referenced by caravan_find_best_destination_withtransit().

◆ cfbdw_callback()

static bool cfbdw_callback ( const struct civ_map nmap,
void vdata,
const struct city dest,
int  arrival_time,
int  moves_left 
)
static

Definition at line 629 of file caravan.c.

Referenced by caravan_find_best_destination_withtransit().

◆ does_foreign_trade_param_allow()

static bool does_foreign_trade_param_allow ( const struct caravan_parameter param,
struct player src,
struct player dest 
)
static

Are the two players allowed to trade by the parameter settings?

Definition at line 454 of file caravan.c.

Referenced by caravan_find_best_destination_notransit(), and get_discounted_reward().

◆ ftl_name()

static const char * ftl_name ( enum foreign_trade_limit  ftl)
static

Human readable name of the foreign_trade_limit

Definition at line 96 of file caravan.c.

Referenced by caravan_parameter_log_real().

◆ get_discounted_reward()

static bool get_discounted_reward ( const struct unit caravan,
const struct caravan_parameter parameter,
struct caravan_result result 
)
static

Compute the discounted reward from the trade route that is indicated by the src, dest, and arrival_time fields of the result: Fills in the value and help_wonder fields. Assumes the owner of src is the owner of the caravan.

Definition at line 481 of file caravan.c.

Referenced by caravan_find_best_destination_notransit(), and cfbdw_callback().

◆ one_city_trade_benefit()

static int one_city_trade_benefit ( const struct city pcity,
const struct player pplayer,
const struct goods_type pgood,
bool  countloser,
int  newtrade 
)
static

How much does the city benefit from the new trade route? How much does the former partner lose?

Definition at line 292 of file caravan.c.

Referenced by trade_benefit().

◆ perpetuity()

static double perpetuity ( double  payment,
double  rate 
)
static

Compute the net present value of an perpetuity given the discount rate. A perpetuity is an annual payment for an infinite number of turns.

Definition at line 437 of file caravan.c.

Referenced by annuity(), and get_discounted_reward().

◆ presentvalue()

static double presentvalue ( double  payment,
int  term,
double  rate 
)
static

Discount a value by the given discount rate. The payment occurs as a lump sum in 'term' turns.

Definition at line 428 of file caravan.c.

Referenced by get_discounted_reward().

◆ trade_benefit()

static double trade_benefit ( const struct player caravan_owner,
const struct city src,
const struct city dest,
const struct goods_type pgood,
const struct caravan_parameter param 
)
static

Compute one_trade_benefit for both cities and do some other logic. This yields the total benefit in terms of trade per turn of establishing a route from src to dest.

Definition at line 347 of file caravan.c.

Referenced by get_discounted_reward().

◆ windfall_benefit()

static double windfall_benefit ( const struct unit caravan,
const struct city src,
const struct city dest,
const struct goods_type pgood,
const struct caravan_parameter param 
)
static

When the caravan arrives, compute the benefit from the immediate windfall, taking into account the parameter's objective.

Definition at line 258 of file caravan.c.

Referenced by get_discounted_reward().

◆ wonder_benefit()

static double wonder_benefit ( const struct unit caravan,
int  arrival_time,
const struct city dest,
const struct caravan_parameter param 
)
static

Check the benefit of helping build the wonder in dest. This is based on how much the caravan would help if it arrived after turns_delay turns during which the city managed the same production it currently gets (i.e. no other caravans, no population growth or terrain improvement, ...)

Definition at line 393 of file caravan.c.

Referenced by get_discounted_reward().