47 const struct unit *caravan)
67 result->
src = result->
dest =
nullptr;
78 const struct city *src,
79 const struct city *dest,
int arrival_time)
114 int arrival_turn,
int arrival_moves_left);
118 struct tile *start_tile,
119 int moves_left_before,
bool omniscient,
146 if (pos.turn > end_time) {
151 if (pcity && callback(callback_data, pcity, pos.turn, pos.moves_left)) {
165 const struct city *src,
166 const struct city *dest,
175 src, dest,
unit_owner(caravan),
nullptr, can_establish);
193 const struct player *pplayer,
194 bool countloser,
int newtrade)
208 struct trade_route_list *would_remove =
209 (countloser ? trade_route_list_new() :
nullptr);
215 newtrade -= oldtrade;
228 if (pback->partner == pcity->
id) {
229 losttrade += pback->value;
236 trade_route_list_destroy(would_remove);
241 return newtrade - losttrade;
250 const struct city *src,
const struct city *dest,
279 "Unimplemented functionality: "
280 "using CM to calculate trade.");
293 const struct city *dest,
296 int costwithout, costwith;
297 int shields_at_arrival;
321 return costwithout - costwith;
330 return payment * pow(rate, term);
339 return payment / (1.0 - rate);
346 static double annuity(
double payment,
int term,
double rate)
348 return perpetuity(payment, rate) * (1.0 - 1.0 / pow(rate, term));
360 return (src == dest);
389 const struct city *src = result->
src;
390 const struct city *dest = result->
dest;
392 double discount = parameter->
discount;
395 bool consider_wonder;
397 bool consider_windfall;
409 ACTION_HELP_WONDER, caravan, src,
city_tile(dest),
true, dest));
413 ACTION_TRADE_ROUTE, caravan, src,
city_tile(dest),
true, dest));
417 ACTION_MARKETPLACE, caravan, src,
city_tile(dest),
true, dest));
419 if (!consider_wonder && !consider_trade && !consider_windfall) {
428 if (consider_wonder) {
438 if (consider_trade) {
442 trade =
annuity(trade, parameter->
horizon - arrival_time, discount);
449 if (consider_windfall) {
450 windfall =
presentvalue(windfall, arrival_time, discount);
455 if (consider_trade && trade + windfall >= wonder) {
456 result->
value = trade + windfall;
458 }
else if (consider_wonder) {
459 result->
value = wonder;
530 *data->
best = current;
541 const struct city *
src,
int moves_left,
bool omniscient,
545 struct tile *start_tile;
554 start_tile = src->
tile;
581 caravan, parameter, src, caravan->
moves_left, omniscient, result);
bool action_prob_possible(const struct act_prob probability)
Returns TRUE iff the given action probability belongs to an action that may be possible.
struct act_prob action_speculate_unit_on_city(const action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, const bool omniscient_cheat, const struct city *target)
Returns a speculation about the actor unit's probability of successfully performing the chosen action...
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.
static double perpetuity(double payment, double rate)
Compute the net present value of an perpetuity given the discount rate.
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?
bool(* search_callback)(void *data, const struct city *pcity, int arrival_turn, int arrival_moves_left)
We use the path finding in several places.
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.
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?
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.
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)
void caravan_result_init_zero(struct caravan_result *result)
Initialize the result to be worth zero and go from nowhere to nowhere.
static double annuity(double payment, int term, double rate)
Compute the net present value of an annuity given the discount rate.
static bool cfbdw_callback(void *vdata, const struct city *dest, int arrival_time, int moves_left)
int caravan_result_compare(const struct caravan_result *a, const struct caravan_result *b)
Compare the two results for sorting.
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,...
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.
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.
void caravan_parameter_init_default(struct caravan_parameter *parameter)
Create a valid parameter with default values.
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 pa...
static double presentvalue(double payment, int term, double rate)
Discount a value by the given discount rate.
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.
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.
bool city_production_gets_caravan_shields(const struct universal *tgt)
Returns TRUE iff the specified production should get shields from units that has done ACTION_HELP_WON...
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
#define city_list_iterate(citylist, pcity)
#define city_list_iterate_end
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
int impr_buy_gold_cost(const struct city *pcity, const struct impr_type *pimprove, int shields_in_stock)
Returns the amount of gold it takes to rush this improvement.
bool is_wonder(const struct impr_type *pimprove)
Returns whether improvement is some kind of wonder.
#define fc_assert_msg(condition, message,...)
#define fc_assert(condition)
#define fc_assert_ret_val(condition, val)
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
Factory function to create a new map according to the parameter.
void pf_map_destroy(struct pf_map *pfm)
After usage the map must be destroyed.
#define pf_map_positions_iterate_end
#define pf_map_positions_iterate(ARG_pfm, NAME_pos, COND_from_start)
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players can attack each other.
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied.
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied or at peace.
#define players_iterate_end
#define players_iterate(_pplayer)
void(* callback)(const struct caravan_result *result, void *data)
enum foreign_trade_limit allow_foreign_trade
bool account_for_broken_routes
An advisor for using caravans optimally.
Callback and struct for caravan_search invocation in caravan_find_best_destination_withtransit.
struct caravan_result * best
const struct unit * caravan
const struct caravan_parameter * param
struct universal production
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
Return TRUE iff the two cities are capable of trade; i.e., if a caravan from one city can enter the o...
int max_trade_routes(const struct city *pcity)
Return current maximum number of trade routes city can have.
int trade_base_between_cities(const struct city *pc1, const struct city *pc2)
Return the trade that exists between these cities, assuming they have a trade route.
int city_num_trade_routes(const struct city *pcity)
Return number of trade route city has.
int city_trade_removable(const struct city *pcity, struct trade_route_list *would_remove)
Return the minimum value of the sum of trade routes which could be replaced by a new one.
int get_caravan_enter_city_trade_bonus(const struct city *pc1, const struct city *pc2, const player *seen_as, struct goods_type *pgood, const bool establish_trade)
Returns the revenue trade bonus - you get this when establishing a trade route and also when you simp...
bool can_establish_trade_route(const struct city *pc1, const struct city *pc2)
Returns TRUE iff the two cities can establish a trade route.
#define trade_route_list_iterate(trade_route_list, proute)
#define trade_routes_iterate_end
#define trade_route_list_iterate_end
#define trade_routes_iterate(c, proute)
const struct impr_type * building
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Return TRUE iff this unit can do the specified generalized (ruleset defined) action enabler controlle...
int unit_build_shield_cost_base(const struct unit *punit)
Returns the number of shields this unit represents.