Freeciv21
Develop your civilization from humble roots to a global empire
caravan.cpp File Reference
#include <cmath>
#include "game.h"
#include "traderoutes.h"
#include "path_finding.h"
#include "pf_tools.h"
#include "caravan.h"
+ Include dependency graph for caravan.cpp:

Go to the source code of this file.

Classes

struct  cfbdw_data
 Callback and struct for caravan_search invocation in caravan_find_best_destination_withtransit. More...
 

Typedefs

typedef bool(* search_callback) (void *data, const struct city *pcity, int arrival_turn, int arrival_moves_left)
 We use the path finding in several places. More...
 

Functions

void caravan_parameter_init_default (struct caravan_parameter *parameter)
 Create a valid parameter with default values. More...
 
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. More...
 
void caravan_result_init_zero (struct caravan_result *result)
 Initialize the result to be worth zero and go from nowhere to nowhere. More...
 
static void caravan_result_init (struct caravan_result *result, const struct city *src, const struct city *dest, int arrival_time)
 Initialize the result to go from src to dest with the given amount of time. More...
 
int caravan_result_compare (const struct caravan_result *a, const struct caravan_result *b)
 Compare the two results for sorting. More...
 
static void caravan_search_from (const struct unit *caravan, const struct caravan_parameter *param, struct tile *start_tile, 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)
 When the caravan arrives, compute the benefit from the immediate windfall, taking into account the parameter's objective. More...
 
static int one_city_trade_benefit (const struct city *pcity, const struct player *pplayer, bool countloser, int newtrade)
 How much does the city benefit from the new trade route? How much does the former partner lose? More...
 
static double trade_benefit (const struct player *caravan_owner, const struct city *src, const struct city *dest, const struct caravan_parameter *param)
 Compute one_trade_benefit for both cities and do some other logic. More...
 
static double wonder_benefit (const struct unit *caravan, int arrival_time, const struct city *dest, const struct caravan_parameter *param)
 Check the benefit of helping build the wonder in dest. More...
 
static double presentvalue (double payment, int term, double rate)
 Discount a value by the given discount rate. More...
 
static double perpetuity (double payment, double rate)
 Compute the net present value of an perpetuity given the discount rate. More...
 
static double annuity (double payment, int term, double rate)
 Compute the net present value of an annuity given the discount rate. More...
 
static bool does_foreign_trade_param_allow (const struct caravan_parameter *param, struct player *src, struct player *dest)
 Are the two players allowed to trade by the parameter settings? More...
 
static bool get_discounted_reward (const struct unit *caravan, const struct caravan_parameter *parameter, struct caravan_result *result)
 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. More...
 
static void caravan_find_best_destination_notransit (const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *best)
 Functions to compute the benefit of moving the caravan to dest. More...
 
static bool cfbdw_callback (void *vdata, const struct city *dest, int arrival_time, int moves_left)
 
static void caravan_find_best_destination_withtransit (const struct unit *caravan, const struct caravan_parameter *param, const struct city *src, int moves_left, bool omniscient, struct caravan_result *result)
 Using caravan_search, find the best destination. More...
 
void caravan_find_best_destination (const struct unit *caravan, const struct caravan_parameter *parameter, struct caravan_result *result, bool omniscient)
 Find the best destination city for the caravan. More...
 

Typedef Documentation

◆ search_callback

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

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 113 of file caravan.cpp.

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 346 of file caravan.cpp.

Referenced by get_discounted_reward().

◆ caravan_find_best_destination()

void caravan_find_best_destination ( 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 568 of file caravan.cpp.

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

Functions to compute the benefit of moving the caravan to dest.

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

Definition at line 480 of file caravan.cpp.

Referenced by caravan_find_best_destination().

◆ caravan_find_best_destination_withtransit()

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

Using caravan_search, find the best destination.

Definition at line 539 of file caravan.cpp.

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 29 of file caravan.cpp.

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 46 of file caravan.cpp.

Referenced by dai_manage_caravan().

◆ 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 92 of file caravan.cpp.

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 77 of file caravan.cpp.

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 65 of file caravan.cpp.

Referenced by get_discounted_reward().

◆ caravan_search_from()

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

Definition at line 116 of file caravan.cpp.

Referenced by caravan_find_best_destination_withtransit().

◆ cfbdw_callback()

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

Definition at line 519 of file caravan.cpp.

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 355 of file caravan.cpp.

Referenced by caravan_find_best_destination_notransit(), and get_discounted_reward().

◆ 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 382 of file caravan.cpp.

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,
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 192 of file caravan.cpp.

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 337 of file caravan.cpp.

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 328 of file caravan.cpp.

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 249 of file caravan.cpp.

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 164 of file caravan.cpp.

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 292 of file caravan.cpp.

Referenced by get_discounted_reward().