Freeciv-3.1
Loading...
Searching...
No Matches
caravan.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <math.h>
19
20/* utility */
21#include "log.h"
22
23/* common */
24#include "game.h"
25#include "traderoutes.h"
26
27/* aicore */
28#include "path_finding.h"
29#include "pf_tools.h"
30
31#include "caravan.h"
32
33/************************************************************************/
37{
38 parameter->horizon = FC_INFINITY;
39 parameter->discount = 0.95;
40 parameter->consider_windfall = TRUE;
41 parameter->consider_trade = TRUE;
42 parameter->consider_wonders = TRUE; /* see also init_from_unit */
45 parameter->ignore_transit_time = FALSE;
46 parameter->convert_trade = FALSE;
47 parameter->callback = NULL;
48}
49
50/************************************************************************/
54 const struct unit *caravan)
55{
57 if (!unit_can_do_action(caravan, ACTION_TRADE_ROUTE)) {
58 parameter->consider_trade = FALSE;
59 }
60 if (!unit_can_do_action(caravan, ACTION_MARKETPLACE)
61 && !unit_can_do_action(caravan, ACTION_TRADE_ROUTE)) {
62 parameter->consider_windfall = FALSE;
63 }
64 if (!unit_can_do_action(caravan, ACTION_HELP_WONDER)) {
65 parameter->consider_wonders = FALSE;
66 }
67}
68
69/************************************************************************/
72bool caravan_parameter_is_legal(const struct caravan_parameter *parameter)
73{
74 /* a discount > 1.0 means money later is worth more than money now,
75 which is ridiculous. */
76 if (parameter->discount > 1.0) {
77 return FALSE;
78 }
79
80 /* a negative discount doesn't converge */
81 if (parameter->discount < 0.0) {
82 return FALSE;
83 }
84
85 /* infinite horizon with no discount gives infinite reward. */
86 if (parameter->horizon == FC_INFINITY && parameter->discount == 1.0) {
87 return FALSE;
88 }
89
90 return TRUE;
91}
92
93/************************************************************************/
96static const char *ftl_name(enum foreign_trade_limit ftl)
97{
98 switch (ftl) {
100 return "no";
101 case FTL_ALLIED:
102 return "allied";
103 case FTL_PEACEFUL:
104 return "peaceful";
105 case FTL_NONWAR:
106 return "anything but enemies";
107 }
108
109 return "<illegal>";
110}
111
112/************************************************************************/
116 enum log_level level, const char *file,
117 const char *function, int line)
118{
119 do_log(file, function, line, FALSE, level,
120 "parameter {\n"
121 " horizon = %d\n"
122 " discount = %g\n"
123 " objective = <%s,%s,%s>\n"
124 " account-broken = %s\n"
125 " allow-foreign = %s\n"
126 " ignore-transit = %s\n"
127 " convert-trade = %s\n"
128 "}\n",
129 parameter->horizon,
130 parameter->discount,
131 parameter->consider_windfall ? "windfall" : "-",
132 parameter->consider_trade ? "trade" : "-",
133 parameter->consider_wonders ? "wonders" : "-",
134 parameter->account_for_broken_routes ? "yes" : "no",
135 ftl_name(parameter->allow_foreign_trade),
136 parameter->ignore_transit_time ? "yes" : "no",
137 parameter->convert_trade ? "yes" : "no");
138}
139
140
141/************************************************************************/
145{
146 result->src = result->dest = NULL;
147 result->arrival_time = 0;
148 result->value = 0;
149 result->help_wonder = FALSE;
150 result->required_boat = FALSE;
151}
152
153/************************************************************************/
157static void caravan_result_init(struct caravan_result *result,
158 const struct city *src,
159 const struct city *dest,
160 int arrival_time)
161{
162 result->src = src;
163 result->dest = dest;
164 result->arrival_time = arrival_time;
165
166 result->value = 0;
167 result->help_wonder = FALSE;
168 /* FIXME: required_boat field is never used. */
169 if ((src != NULL) && (dest != NULL)) {
170 if (tile_continent(src->tile) != tile_continent(dest->tile)) {
171 result->required_boat = TRUE;
172 } else {
173 result->required_boat = FALSE;
174 }
175 } else {
176 result->required_boat = FALSE;
177 }
178}
179
180/************************************************************************/
184 const struct caravan_result *b)
185{
186 if (a->value > b->value) {
187 return 1;
188 } else if (a->value < b->value) {
189 return -1;
190 } else {
191 /* Faster time is better, so reverse-sort on time. */
192 return b->arrival_time - a->arrival_time ;
193 }
194}
195
196/************************************************************************/
204typedef bool (*search_callback) (const struct civ_map *nmap,
205 void *data, const struct city *pcity,
206 int arrival_turn, int arrival_moves_left);
207
208static void caravan_search_from(const struct civ_map *nmap,
209 const struct unit *caravan,
210 const struct caravan_parameter *param,
211 struct tile *start_tile,
212 int turns_before, int moves_left_before,
213 bool omniscient,
215 void *callback_data)
216{
217 struct pf_map *pfm;
218 struct pf_parameter pfparam;
219 int end_time;
220
221 end_time = param->horizon - turns_before;
222
223 /* Initialize the pf run. */
224 pft_fill_unit_parameter(&pfparam, nmap, (struct unit *) caravan);
225 pfparam.start_tile = start_tile;
226 pfparam.moves_left_initially = moves_left_before;
227 pfparam.omniscience = omniscient;
228 pfm = pf_map_new(&pfparam);
229
230 /* For every tile in distance order:
231 quit if we've exceeded the maximum number of turns
232 otherwise, run the callback if we're on a city.
233 Do-while loop rather than while loop to make sure to process the
234 start tile.
235 */
237 struct city *pcity;
238
239 if (pos.turn > end_time) {
240 break;
241 }
242
243 pcity = tile_city(pos.tile);
244 if (pcity != NULL
245 && callback(nmap, callback_data, pcity, turns_before + pos.turn,
246 pos.moves_left)) {
247 break;
248 }
250
251 pf_map_destroy(pfm);
252}
253
254/************************************************************************/
258static double windfall_benefit(const struct unit *caravan,
259 const struct city *src,
260 const struct city *dest,
261 const struct caravan_parameter *param)
262{
263 if (!param->consider_windfall || !can_cities_trade(src, dest)) {
264 return 0;
265 } else {
266 bool can_establish = (unit_can_do_action(caravan, ACTION_TRADE_ROUTE)
267 && can_establish_trade_route(src, dest));
268 int bonus = get_caravan_enter_city_trade_bonus(src, dest, NULL,
269 can_establish);
270
271 /* when bonus goes to both sci and gold, double it */
272 if (TBONUS_BOTH == trade_route_settings_by_type
273 (cities_trade_route_type(src, dest))->bonus_type) {
274 bonus *= 2;
275 }
276
277 return bonus;
278 }
279}
280
281/****************************************************************************
282 Compute the change in the per-turn trade.
283****************************************************************************/
284
285/************************************************************************/
289static int one_city_trade_benefit(const struct city *pcity,
290 const struct player *pplayer,
291 bool countloser, int newtrade)
292{
293 int losttrade = 0;
294
295 /* if the city is owned by someone else, we don't benefit from the
296 new trade (but we might still lose from a broken trade route) */
297 if (city_owner(pcity) != pplayer) {
298 newtrade = 0;
299 }
300
301 if (city_num_trade_routes(pcity) < max_trade_routes(pcity)) {
302 /* if the city can handle this route, we don't break any old routes */
303 losttrade = 0;
304 } else {
305 struct trade_route_list *would_remove = (countloser ? trade_route_list_new() : NULL);
306 int oldtrade = city_trade_removable(pcity, would_remove);
307
308 /* if we own the city, the trade benefit is only by how much
309 better we are than the old trade route */
310 if (city_owner(pcity) == pplayer) {
311 newtrade -= oldtrade;
312 }
313
314 /* if the cities that lost a trade route is one of ours, and if we
315 care about accounting for the lost trade, count it. */
316 if (countloser) {
317 trade_route_list_iterate(would_remove, plost) {
318 struct city *losercity = game_city_by_number(plost->partner);
319
320 if (city_owner(losercity) == pplayer) {
321 trade_routes_iterate(losercity, pback) {
322 if (pback->partner == pcity->id) {
323 losttrade += pback->value;
324 }
326 }
328 trade_route_list_destroy(would_remove);
329 }
330 }
331
332 /* find the increase or decrease in trade we benefit from */
333 return newtrade - losttrade;
334}
335
336/************************************************************************/
341static double trade_benefit(const struct player *caravan_owner,
342 const struct city *src,
343 const struct city *dest,
344 const struct caravan_parameter *param)
345{
346 /* do we care about trade at all? */
347 if (!param->consider_trade) {
348 return 0;
349 }
350
351 /* first, see if a new route is made. */
352 if (!can_cities_trade(src, dest) || !can_establish_trade_route(src, dest)) {
353 return 0;
354 }
355 if (max_trade_routes(src) <= 0 || max_trade_routes(dest) <= 0) {
356 /* Can't create new trade routes even by replacing old ones if
357 * there's no slots at all. */
358 return 0;
359 }
360
361 if (!param->convert_trade) {
362 bool countloser = param->account_for_broken_routes;
363 int newtrade = trade_base_between_cities(src, dest);
364
365 return one_city_trade_benefit(src, caravan_owner, countloser, newtrade)
366 + one_city_trade_benefit(dest, caravan_owner, countloser, newtrade);
367 } else {
368 /* Always fails. */
370 "Unimplemented functionality: "
371 "using CM to calculate trade.");
372 return 0;
373 }
374}
375
376/************************************************************************/
383static double wonder_benefit(const struct unit *caravan, int arrival_time,
384 const struct city *dest,
385 const struct caravan_parameter *param)
386{
387 int costwithout, costwith;
388 int shields_at_arrival;
389
390 if (!param->consider_wonders
391 /* TODO: Should helping an ally to build be considered when legal? */
392 || unit_owner(caravan) != city_owner(dest)
394 /* TODO: Should helping to build a unit be considered when legal? */
395 || VUT_UTYPE == dest->production.kind
396 /* TODO: Should helping to build an improvement be considered when
397 * legal? */
398 || !is_wonder(dest->production.value.building)) {
399 return 0;
400 }
401
402 shields_at_arrival = dest->shield_stock
403 + arrival_time * dest->surplus[O_SHIELD];
404
405 costwithout = impr_buy_gold_cost(dest, dest->production.value.building,
406 shields_at_arrival);
407 costwith = impr_buy_gold_cost(dest, dest->production.value.building,
408 shields_at_arrival + unit_build_shield_cost_base(caravan));
409
410 fc_assert_ret_val(costwithout >= costwith, -1.0);
411 return costwithout - costwith;
412}
413
414/************************************************************************/
418static double presentvalue(double payment, int term, double rate)
419{
420 return payment * pow(rate, term);
421}
422
423/************************************************************************/
427static double perpetuity(double payment, double rate)
428{
429 return payment / (1.0 - rate);
430}
431
432/************************************************************************/
436static double annuity(double payment, int term, double rate)
437{
438 return perpetuity(payment, rate) * (1.0 - 1.0 / pow(rate, term));
439}
440
441/************************************************************************/
444static bool does_foreign_trade_param_allow(const struct caravan_parameter *param,
445 struct player *src, struct player *dest)
446{
447 switch (param->allow_foreign_trade) {
449 return (src == dest);
450 break;
451 case FTL_ALLIED:
452 return pplayers_allied(src, dest);
453 break;
454 case FTL_PEACEFUL:
455 return pplayers_in_peace(src, dest);
456 break;
457 case FTL_NONWAR:
458 return !pplayers_at_war(src, dest);
459 }
460
462 return FALSE;
463}
464
465/************************************************************************/
471static bool get_discounted_reward(const struct unit *caravan,
472 const struct caravan_parameter *parameter,
473 struct caravan_result *result)
474{
475 double trade;
476 double windfall;
477 double wonder;
478 const struct city *src = result->src;
479 const struct city *dest = result->dest;
480 int arrival_time = result->arrival_time;
481 double discount = parameter->discount;
482 struct player *pplayer_src = city_owner(src);
483 struct player *pplayer_dest = city_owner(dest);
484 int cost = unit_build_shield_cost(src, caravan);
485 bool consider_wonder;
486 bool consider_trade;
487 bool consider_windfall;
488 const struct civ_map *nmap = &(wld.map);
489
490 /* If no foreign trade is allowed, just quit. */
491 if (!does_foreign_trade_param_allow(parameter, pplayer_src, pplayer_dest)) {
493 return FALSE;
494 }
495
496 consider_wonder = parameter->consider_wonders
498 action_speculate_unit_on_city(nmap, ACTION_HELP_WONDER,
499 caravan, src, city_tile(dest),
500 TRUE, dest));
501 consider_trade = parameter->consider_trade
503 action_speculate_unit_on_city(nmap, ACTION_TRADE_ROUTE,
504 caravan, src, city_tile(dest),
505 TRUE, dest));
506 consider_windfall = parameter->consider_windfall
508 action_speculate_unit_on_city(nmap, ACTION_MARKETPLACE,
509 caravan, src, city_tile(dest),
510 TRUE, dest));
511
512 if (!consider_wonder && !consider_trade && !consider_windfall) {
513 /* No caravan action is possible against this target. */
515
516 return FALSE;
517 }
518
519 if (consider_wonder) {
520 wonder = wonder_benefit(caravan, arrival_time, dest, parameter);
521 /* we want to aid for wonder building */
522 wonder *= 2;
523
524 wonder = presentvalue(wonder, arrival_time, discount);
525 } else {
526 wonder = -1.0;
527 }
528
529 if (consider_trade) {
530 trade = trade_benefit(pplayer_src, src, dest, parameter);
531 if (parameter->horizon == FC_INFINITY) {
532 trade = perpetuity(trade, discount);
533 } else {
534 trade = annuity(trade, parameter->horizon - arrival_time, discount);
535 }
536 trade = presentvalue(trade, arrival_time, discount);
537 } else {
538 trade = 0.0;
539 }
540
541 if (consider_windfall) {
542 windfall = windfall_benefit(caravan, src, dest, parameter);
543 windfall = presentvalue(windfall, arrival_time, discount);
544 } else {
545 windfall = 0.0;
546 }
547
548 if ((consider_trade
549 || (consider_windfall
550 && (!parameter->consider_trade /* Can only enter marketplaces */
551 /* (FIXME: test any extra restrictions for trade routes) */
552 /* or the result is so big that we are still in plus */
553 /* (consider having produced IF_GOLD instead) */
554 || windfall
556 / MAX(src->surplus[O_SHIELD], 2), discount)))
557 ) && trade + windfall >= wonder) {
558 result->value = trade + windfall;
559 result->help_wonder = FALSE;
560 } else if (consider_wonder) {
561 result->value = wonder;
562 result->help_wonder = TRUE;
563 } else {
565 return FALSE;
566 }
567
568 if (parameter->callback) {
569 parameter->callback(result, parameter->callback_data);
570 }
571
572 return TRUE;
573}
574
575/****************************************************************************
576 Functions to compute the benefit of moving the caravan to dest.
577****************************************************************************/
578
579/************************************************************************/
583static void caravan_evaluate_notransit(const struct unit *caravan,
584 const struct city *dest,
585 const struct caravan_parameter *param,
586 struct caravan_result *result)
587{
588 const struct city *src = game_city_by_number(caravan->homecity);
589
590 caravan_result_init(result, src, dest, 0);
591 get_discounted_reward(caravan, param, result);
592}
593
594/************************************************************************/
598struct cewt_data {
599 const struct unit *caravan;
602};
603
604static bool cewt_callback(const struct civ_map *nmap,
605 void *vdata, const struct city *dest,
606 int arrival_time, int moves_left)
607{
608 struct cewt_data *data = vdata;
609
611
612 if (dest == data->result->dest) {
613 data->result->arrival_time = arrival_time;
614 get_discounted_reward(data->caravan, data->param, data->result);
615 return TRUE;
616 } else {
617 return FALSE;
618 }
619}
620
621/************************************************************************/
625static void caravan_evaluate_withtransit(const struct civ_map *nmap,
626 const struct unit *caravan,
627 const struct city *dest,
628 const struct caravan_parameter *param,
629 struct caravan_result *result,
630 bool omniscient)
631{
632 struct cewt_data data;
633
634 data.caravan = caravan;
635 data.param = param;
637 dest, 0);
638 data.result = result;
640 caravan->moves_left, omniscient, cewt_callback, &data);
641}
642
643/************************************************************************/
646void caravan_evaluate(const struct unit *caravan,
647 const struct city *dest,
648 const struct caravan_parameter *param,
649 struct caravan_result *result, bool omniscient)
650{
651 const struct civ_map *nmap = &(wld.map);
652
653 if (param->ignore_transit_time) {
654 caravan_evaluate_notransit(caravan, dest, param, result);
655 } else {
656 caravan_evaluate_withtransit(nmap, caravan, dest, param, result, omniscient);
657 }
658}
659
660
661/************************************************************************/
664static void caravan_find_best_destination_notransit(const struct unit *caravan,
665 const struct caravan_parameter *param,
666 struct caravan_result *best)
667{
668 struct caravan_result current;
669 struct city *pcity = game_city_by_number(caravan->homecity);
670 struct player *src_owner = city_owner(pcity);
671
672 caravan_result_init(best, pcity, NULL, 0);
673 current = *best;
674
675 players_iterate(dest_owner) {
676 if (does_foreign_trade_param_allow(param, src_owner, dest_owner)) {
677 city_list_iterate(dest_owner->cities, dest) {
678 caravan_result_init(&current, pcity, dest, 0);
679 if (get_discounted_reward(caravan, param, &current)
680 && caravan_result_compare(&current, best) > 0) {
681 *best = current;
682 }
684 }
686}
687
688/************************************************************************/
694 const struct unit *caravan;
696};
697
698static bool cfbdw_callback(const struct civ_map *nmap,
699 void *vdata, const struct city *dest,
700 int arrival_time, int moves_left)
701{
702 struct cfbdw_data *data = vdata;
703 struct caravan_result current;
704
705 caravan_result_init(&current, data->best->src, dest, arrival_time);
706
707 if (get_discounted_reward(data->caravan, data->param, &current)
708 && caravan_result_compare(&current, data->best) > 0) {
709 *data->best = current;
710 }
711
712 return FALSE; /* Don't stop. */
713}
714
715/************************************************************************/
719 const struct civ_map *nmap,
720 const struct unit *caravan,
721 const struct caravan_parameter *param,
722 const struct city *src,
723 int turns_before,
724 int moves_left,
725 bool omniscient,
726 struct caravan_result *result)
727{
728 struct tile *start_tile;
729 struct cfbdw_data data;
730
731 data.param = param;
732 data.caravan = caravan;
733 data.best = result;
734 caravan_result_init(data.best, src, NULL, 0);
735
736 if (src->id != caravan->homecity) {
737 start_tile = src->tile;
738 } else {
739 start_tile = unit_tile(caravan);
740 }
741
742 caravan_search_from(nmap, caravan, param, start_tile, turns_before,
743 caravan->moves_left, omniscient, cfbdw_callback, &data);
744}
745
746/************************************************************************/
752 const struct unit *caravan,
753 const struct caravan_parameter *parameter,
754 struct caravan_result *result, bool omniscient)
755{
756 if (parameter->ignore_transit_time) {
758 } else {
759 const struct city *src = game_city_by_number(caravan->homecity);
760
761 fc_assert(src != NULL);
762
763 caravan_find_best_destination_withtransit(nmap, caravan, parameter, src, 0,
764 caravan->moves_left, omniscient, result);
765 }
766}
767
768/************************************************************************/
771static void caravan_optimize_notransit(const struct unit *caravan,
772 const struct caravan_parameter *param,
773 struct caravan_result *best)
774{
775 struct player *pplayer = unit_owner(caravan);
776
777 /* Iterate over all cities we own (since the caravan could change its
778 * home city); iterate over all cities we know about (places the caravan
779 * can go to); pick out the best trade route. */
780 city_list_iterate(pplayer->cities, src) {
781 players_iterate(dest_owner) {
782 if (does_foreign_trade_param_allow(param, pplayer, dest_owner)) {
783 city_list_iterate(dest_owner->cities, dest) {
784 struct caravan_result current;
785
786 caravan_result_init(&current, src, dest, 0);
787 get_discounted_reward(caravan, param, &current);
788 if (caravan_result_compare(&current, best) > 0) {
789 *best = current;
790 }
792 }
795}
796
797/****************************************************************************
798 Struct for the caravan_search invocation in
799 caravan_optimize_withtransit.
800****************************************************************************/
801struct cowt_data {
803 const struct unit *caravan;
806};
807
808/************************************************************************/
815static bool cowt_callback(const struct civ_map *nmap, void *vdata,
816 const struct city *pcity,
817 int arrival_time, int moves_left)
818{
819 struct cowt_data *data = vdata;
820 const struct unit *caravan = data->caravan;
821 struct caravan_result current;
822
824 pcity, arrival_time);
825
826 /* first, see what benefit we'd get from not changing home city */
827 get_discounted_reward(caravan, data->param, &current);
828 if (caravan_result_compare(&current, data->best) > 0) {
829 *data->best = current;
830 }
831
832 /* next, try changing home city (if we're allowed to) */
833 if (city_owner(pcity) == unit_owner(caravan)) {
835 nmap, caravan, data->param, pcity, arrival_time, moves_left, data->omniscient,
836 &current);
837 if (caravan_result_compare(&current, data->best) > 0) {
838 *data->best = current;
839 }
840 }
841
842 return FALSE; /* don't stop searching */
843}
844
845/************************************************************************/
849static void caravan_optimize_withtransit(const struct civ_map *nmap,
850 const struct unit *caravan,
851 const struct caravan_parameter *param,
852 struct caravan_result *result,
853 bool omniscient)
854{
855 struct cowt_data data;
856
857 data.param = param;
858 data.caravan = caravan;
859 data.best = result;
860 data.omniscient = omniscient;
864}
865
866/************************************************************************/
872 const struct caravan_parameter *param,
873 struct caravan_result *result, bool omniscient)
874{
875 const struct civ_map *nmap = &(wld.map);
876
877 if (param->ignore_transit_time) {
878 caravan_optimize_notransit(caravan, param, result);
879 } else {
880 caravan_optimize_withtransit(nmap, caravan, param, result, omniscient);
881 }
882}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:6703
struct act_prob action_speculate_unit_on_city(const struct civ_map *nmap, 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)
Definition actions.c:6456
static double wonder_benefit(const struct unit *caravan, int arrival_time, const struct city *dest, const struct caravan_parameter *param)
Definition caravan.c:383
static double perpetuity(double payment, double rate)
Definition caravan.c:427
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)
Definition caravan.c:751
static bool does_foreign_trade_param_allow(const struct caravan_parameter *param, struct player *src, struct player *dest)
Definition caravan.c:444
static bool cowt_callback(const struct civ_map *nmap, void *vdata, const struct city *pcity, int arrival_time, int moves_left)
Definition caravan.c:815
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)
Definition caravan.c:208
bool caravan_parameter_is_legal(const struct caravan_parameter *parameter)
Definition caravan.c:72
static void caravan_find_best_destination_notransit(const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *best)
Definition caravan.c:664
static void caravan_evaluate_notransit(const struct unit *caravan, const struct city *dest, const struct caravan_parameter *param, struct caravan_result *result)
Definition caravan.c:583
static int one_city_trade_benefit(const struct city *pcity, const struct player *pplayer, bool countloser, int newtrade)
Definition caravan.c:289
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)
Definition caravan.c:849
void caravan_result_init_zero(struct caravan_result *result)
Definition caravan.c:144
static bool cewt_callback(const struct civ_map *nmap, void *vdata, const struct city *dest, int arrival_time, int moves_left)
Definition caravan.c:604
static void caravan_optimize_notransit(const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *best)
Definition caravan.c:771
static double annuity(double payment, int term, double rate)
Definition caravan.c:436
static bool cfbdw_callback(const struct civ_map *nmap, void *vdata, const struct city *dest, int arrival_time, int moves_left)
Definition caravan.c:698
static const char * ftl_name(enum foreign_trade_limit ftl)
Definition caravan.c:96
bool(* search_callback)(const struct civ_map *nmap, void *data, const struct city *pcity, int arrival_turn, int arrival_moves_left)
Definition caravan.c:204
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)
Definition caravan.c:625
int caravan_result_compare(const struct caravan_result *a, const struct caravan_result *b)
Definition caravan.c:183
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)
Definition caravan.c:718
void caravan_parameter_log_real(const struct caravan_parameter *parameter, enum log_level level, const char *file, const char *function, int line)
Definition caravan.c:115
void caravan_evaluate(const struct unit *caravan, const struct city *dest, const struct caravan_parameter *param, struct caravan_result *result, bool omniscient)
Definition caravan.c:646
static bool get_discounted_reward(const struct unit *caravan, const struct caravan_parameter *parameter, struct caravan_result *result)
Definition caravan.c:471
static double trade_benefit(const struct player *caravan_owner, const struct city *src, const struct city *dest, const struct caravan_parameter *param)
Definition caravan.c:341
void caravan_parameter_init_default(struct caravan_parameter *parameter)
Definition caravan.c:36
static double windfall_benefit(const struct unit *caravan, const struct city *src, const struct city *dest, const struct caravan_parameter *param)
Definition caravan.c:258
static double presentvalue(double payment, int term, double rate)
Definition caravan.c:418
void caravan_parameter_init_from_unit(struct caravan_parameter *parameter, const struct unit *caravan)
Definition caravan.c:53
static void caravan_result_init(struct caravan_result *result, const struct city *src, const struct city *dest, int arrival_time)
Definition caravan.c:157
void caravan_optimize_allpairs(const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *result, bool omniscient)
Definition caravan.c:871
foreign_trade_limit
Definition caravan.h:29
@ FTL_ALLIED
Definition caravan.h:31
@ FTL_NATIONAL_ONLY
Definition caravan.h:30
@ FTL_NONWAR
Definition caravan.h:33
@ FTL_PEACEFUL
Definition caravan.h:32
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1833
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
#define city_owner(_pcity_)
Definition city.h:543
#define city_list_iterate_end
Definition city.h:490
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:73
@ O_SHIELD
Definition fc_types.h:91
struct world wld
Definition game.c:58
struct city * game_city_by_number(int id)
Definition game.c:102
static struct tile * pos
Definition finddlg.c:53
int impr_buy_gold_cost(const struct city *pcity, const struct impr_type *pimprove, int shields_in_stock)
bool is_wonder(const struct impr_type *pimprove)
void do_log(const char *file, const char *function, int line, bool print_from_where, enum log_level level, const char *message,...)
Definition log.c:514
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_level
Definition log.h:28
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
void pf_map_destroy(struct pf_map *pfm)
#define pf_map_positions_iterate_end
#define pf_map_positions_iterate(ARG_pfm, NAME_pos, COND_from_start)
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:840
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1364
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1381
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1402
#define players_iterate_end
Definition player.h:535
#define players_iterate(_pplayer)
Definition player.h:530
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
bool consider_trade
Definition caravan.h:86
bool consider_windfall
Definition caravan.h:85
void(* callback)(const struct caravan_result *result, void *data)
Definition caravan.h:124
enum foreign_trade_limit allow_foreign_trade
Definition caravan.h:99
bool consider_wonders
Definition caravan.h:87
bool ignore_transit_time
Definition caravan.h:108
double discount
Definition caravan.h:77
bool account_for_broken_routes
Definition caravan.h:93
void * callback_data
Definition caravan.h:125
const struct city * src
Definition caravan.h:52
const struct city * dest
Definition caravan.h:53
int arrival_time
Definition caravan.h:54
bool help_wonder
Definition caravan.h:57
bool required_boat
Definition caravan.h:58
double value
Definition caravan.h:56
const struct unit * caravan
Definition caravan.c:599
const struct caravan_parameter * param
Definition caravan.c:601
struct caravan_result * result
Definition caravan.c:600
struct caravan_result * best
Definition caravan.c:695
const struct unit * caravan
Definition caravan.c:694
const struct caravan_parameter * param
Definition caravan.c:693
Definition city.h:309
int surplus[O_LAST]
Definition city.h:343
int id
Definition city.h:315
struct universal production
Definition city.h:382
int bonus[O_LAST]
Definition city.h:351
struct tile * tile
Definition city.h:311
int shield_stock
Definition city.h:355
const struct caravan_parameter * param
Definition caravan.c:802
struct caravan_result * best
Definition caravan.c:804
const struct unit * caravan
Definition caravan.c:803
bool omniscient
Definition caravan.c:805
int moves_left_initially
struct tile * start_tile
struct city_list * cities
Definition player.h:281
Definition tile.h:49
Definition unit.h:138
int moves_left
Definition unit.h:150
int homecity
Definition unit.h:146
enum universals_n kind
Definition fc_types.h:758
universals_u value
Definition fc_types.h:757
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define bool
Definition support.h:61
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_continent(_tile)
Definition tile.h:91
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
enum trade_route_type cities_trade_route_type(const struct city *pcity1, const struct city *pcity2)
Definition traderoutes.c:58
int trade_base_between_cities(const struct city *pc1, const struct city *pc2)
int city_num_trade_routes(const struct city *pcity)
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
unsigned max_trade_routes(const struct city *pcity)
Definition traderoutes.c:48
int get_caravan_enter_city_trade_bonus(const struct city *pc1, const struct city *pc2, struct goods_type *pgood, const bool establish_trade)
int city_trade_removable(const struct city *pcity, struct trade_route_list *would_remove)
bool can_establish_trade_route(const struct city *pc1, const struct city *pc2)
#define trade_route_list_iterate(trade_route_list, proute)
Definition traderoutes.h:95
#define trade_routes_iterate_end
#define trade_route_list_iterate_end
Definition traderoutes.h:97
#define trade_routes_iterate(c, proute)
const struct impr_type * building
Definition fc_types.h:598
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:328
#define unit_tile(_pu)
Definition unit.h:395
#define unit_owner(_pu)
Definition unit.h:394
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
Definition unittype.c:1528
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1536