Freeciv-3.2
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  cewt_data
 
struct  cfbdw_data
 
struct  cowt_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 caravan_parameter *param)
 
static int one_city_trade_benefit (const struct city *pcity, const struct player *pplayer, bool countloser, int newtrade)
 
static double trade_benefit (const struct player *caravan_owner, const struct city *src, const struct city *dest, 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_evaluate_notransit (const struct unit *caravan, const struct city *dest, const struct caravan_parameter *param, struct caravan_result *result)
 
static bool cewt_callback (const struct civ_map *nmap, void *vdata, const struct city *dest, int arrival_time, int moves_left)
 
static void caravan_evaluate_withtransit (const struct civ_map *nmap, const struct unit *caravan, const struct city *dest, const struct caravan_parameter *param, struct caravan_result *result, bool omniscient)
 
void caravan_evaluate (const struct unit *caravan, const struct city *dest, const struct caravan_parameter *param, struct caravan_result *result, bool omniscient)
 
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)
 
static void caravan_optimize_notransit (const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *best)
 
static bool cowt_callback (const struct civ_map *nmap, void *vdata, const struct city *pcity, int arrival_time, int moves_left)
 
static void caravan_optimize_withtransit (const struct civ_map *nmap, const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *result, bool omniscient)
 
void caravan_optimize_allpairs (const struct unit *caravan, const struct caravan_parameter *param, 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 437 of file caravan.c.

Referenced by get_discounted_reward().

◆ caravan_evaluate()

void caravan_evaluate ( const struct unit caravan,
const struct city dest,
const struct caravan_parameter param,
struct caravan_result result,
bool  omniscient 
)

Evaluate the value of sending the caravan to dest.

Definition at line 647 of file caravan.c.

◆ caravan_evaluate_notransit()

static void caravan_evaluate_notransit ( const struct unit caravan,
const struct city dest,
const struct caravan_parameter param,
struct caravan_result result 
)
static

Ignoring the transit time, return the value of moving the caravan to dest.

Definition at line 584 of file caravan.c.

Referenced by caravan_evaluate().

◆ caravan_evaluate_withtransit()

static void caravan_evaluate_withtransit ( const struct civ_map nmap,
const struct unit caravan,
const struct city dest,
const struct caravan_parameter param,
struct caravan_result result,
bool  omniscient 
)
static

Using the caravan_search function to take transit time into account, evaluate the benefit of sending the caravan to dest.

Definition at line 626 of file caravan.c.

Referenced by caravan_evaluate().

◆ 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 752 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 665 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 719 of file caravan.c.

Referenced by caravan_find_best_destination(), and cowt_callback().

◆ caravan_optimize_allpairs()

void caravan_optimize_allpairs ( const struct unit caravan,
const struct caravan_parameter param,
struct caravan_result result,
bool  omniscient 
)

For every city the caravan can change home in, find the best destination. Return the best src/dest pair by reference (if non-null), and return the value of that trade route.

Definition at line 872 of file caravan.c.

◆ caravan_optimize_notransit()

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

Find the best pair-wise trade route, ignoring transit time.

Definition at line 772 of file caravan.c.

Referenced by caravan_optimize_allpairs().

◆ caravan_optimize_withtransit()

static void caravan_optimize_withtransit ( const struct civ_map nmap,
const struct unit caravan,
const struct caravan_parameter param,
struct caravan_result result,
bool  omniscient 
)
static

Find the best src/dest pair (including possibly changing home city), taking account of the trip time.

Definition at line 850 of file caravan.c.

Referenced by caravan_optimize_allpairs().

◆ 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(), caravan_optimize_notransit(), cfbdw_callback(), and cowt_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_evaluate_notransit(), caravan_evaluate_withtransit(), caravan_find_best_destination_notransit(), caravan_find_best_destination_withtransit(), caravan_optimize_notransit(), cfbdw_callback(), and cowt_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 caravan_optimize_withtransit(), and 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

◆ cewt_callback()

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

Definition at line 605 of file caravan.c.

Referenced by caravan_evaluate_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 699 of file caravan.c.

Referenced by caravan_find_best_destination_withtransit().

◆ cowt_callback()

static bool cowt_callback ( const struct civ_map nmap,
void vdata,
const struct city pcity,
int  arrival_time,
int  moves_left 
)
static

Callback for the caravan_search invocation in caravan_optimize_withtransit().

For every city we can reach, use caravan_find_best_destination_withtransit() as a subroutine.

Definition at line 816 of file caravan.c.

Referenced by caravan_optimize_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 445 of file caravan.c.

Referenced by caravan_find_best_destination_notransit(), caravan_optimize_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 472 of file caravan.c.

Referenced by caravan_evaluate_notransit(), caravan_find_best_destination_notransit(), caravan_optimize_notransit(), cewt_callback(), cfbdw_callback(), and cowt_callback().

◆ one_city_trade_benefit()

static int one_city_trade_benefit ( const struct city pcity,
const struct player pplayer,
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 290 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 428 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 419 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 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 342 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 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 384 of file caravan.c.

Referenced by get_discounted_reward().