Freeciv-3.3: /fast/freeciv/nightly/main/common/aicore/caravan.c Source File

Freeciv-3.3

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/************************************************************************/
36 void caravan_parameter_init_default(struct caravan_parameter *parameter)
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 */
43 parameter->account_for_broken_routes = TRUE;
44 parameter->allow_foreign_trade = FTL_NATIONAL_ONLY;
45 parameter->ignore_transit_time = FALSE;
46 parameter->convert_trade = FALSE;
47 parameter->callback = NULL;
48}
49
50/************************************************************************/
53 void caravan_parameter_init_from_unit(struct caravan_parameter *parameter,
54 const struct unit *caravan)
55{
56 caravan_parameter_init_default(parameter);
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/************************************************************************/
72 bool 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/************************************************************************/
96 static const char *ftl_name(enum foreign_trade_limit ftl)
97{
98 switch (ftl) {
99 case FTL_NATIONAL_ONLY:
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/************************************************************************/
115 void caravan_parameter_log_real(const struct caravan_parameter *parameter,
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/************************************************************************/
144 void caravan_result_init_zero(struct caravan_result *result)
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/************************************************************************/
157 static 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/************************************************************************/
183 int caravan_result_compare(const struct caravan_result *a,
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/************************************************************************/
204 typedef bool (*search_callback) (const struct civ_map *nmap,
205 void *data, const struct city *pcity,
206 int arrival_turn, int arrival_moves_left);
207
208 static 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,
214 search_callback callback,
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 */
236 pf_map_positions_iterate(pfm, pos, TRUE) {
237 struct city *pcity;
238
239 if (pos.turn > end_time) {
240 break;
241 }
242
243 pcity = tile_city(pos.tile);
244 if (pcity && callback(nmap, callback_data, pcity, turns_before + pos.turn,
245 pos.moves_left)) {
246 break;
247 }
248 } pf_map_positions_iterate_end;
249
250 pf_map_destroy(pfm);
251}
252
253/************************************************************************/
257 static double windfall_benefit(const struct unit *caravan,
258 const struct city *src,
259 const struct city *dest,
260 const struct goods_type *pgood,
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 pgood->priority));
269 int bonus = get_caravan_enter_city_trade_bonus(src, dest,
270 unit_type_get(caravan),
271 NULL, can_establish);
272
273 /* when bonus goes to both sci and gold, double it */
274 if (TBONUS_BOTH == trade_route_settings_by_type
275 (cities_trade_route_type(src, dest))->bonus_type) {
276 bonus *= 2;
277 }
278
279 return bonus;
280 }
281}
282
283/****************************************************************************
284 Compute the change in the per-turn trade.
285****************************************************************************/
286
287/************************************************************************/
291 static int one_city_trade_benefit(const struct city *pcity,
292 const struct player *pplayer,
293 const struct goods_type *pgood,
294 bool countloser, int newtrade)
295{
296 int losttrade = 0;
297
298 /* If the city is owned by someone else, we don't benefit from the
299 new trade (but we might still lose from a broken trade route) */
300 if (city_owner(pcity) != pplayer) {
301 newtrade = 0;
302 }
303
304 if (city_num_trade_routes(pcity) < max_trade_routes(pcity)) {
305 /* If the city can handle this route, we don't break any old routes */
306 losttrade = 0;
307 } else {
308 struct trade_route_list *would_remove = (countloser ? trade_route_list_new() : NULL);
309 int oldtrade = city_trade_removable(pcity, pgood->priority, would_remove);
310
311 /* If we own the city, the trade benefit is only by how much
312 better we are than the old trade route */
313 if (city_owner(pcity) == pplayer) {
314 newtrade -= oldtrade;
315 }
316
317 /* If the cities that lost a trade route is one of ours, and if we
318 care about accounting for the lost trade, count it. */
319 if (countloser) {
320 trade_route_list_iterate(would_remove, plost) {
321 struct city *losercity = game_city_by_number(plost->partner);
322
323 if (city_owner(losercity) == pplayer) {
324 trade_routes_iterate(losercity, pback) {
325 if (pback->partner == pcity->id) {
326 losttrade += pback->value;
327 }
328 } trade_routes_iterate_end;
329 }
330 } trade_route_list_iterate_end;
331 trade_route_list_destroy(would_remove);
332 }
333 }
334
335 /* Find the increase or decrease in trade we benefit from */
336 return newtrade - losttrade;
337}
338
339/************************************************************************/
344 static double trade_benefit(const struct player *caravan_owner,
345 const struct city *src,
346 const struct city *dest,
347 const struct goods_type *pgood,
348 const struct caravan_parameter *param)
349{
350 /* Do we care about trade at all? */
351 if (!param->consider_trade) {
352 return 0;
353 }
354
355 /* First, see if a new route is made. */
356 if (!can_cities_trade(src, dest)
357 || !can_establish_trade_route(src, dest, pgood->priority)) {
358 return 0;
359 }
360 if (max_trade_routes(src) <= 0 || max_trade_routes(dest) <= 0) {
361 /* Can't create new trade routes even by replacing old ones if
362 * there's no slots at all. */
363 return 0;
364 }
365
366 if (!param->convert_trade) {
367 bool countloser = param->account_for_broken_routes;
368 int newtrade = trade_base_between_cities(src, dest);
369
370 return one_city_trade_benefit(src, caravan_owner, pgood,
371 countloser, newtrade)
372 + one_city_trade_benefit(dest, caravan_owner, pgood,
373 countloser, newtrade);
374 } else {
375 /* Always fails. */
376 fc_assert_msg(!param->convert_trade,
377 "Unimplemented functionality: "
378 "using CM to calculate trade.");
379 return 0;
380 }
381}
382
383/************************************************************************/
390 static double wonder_benefit(const struct unit *caravan, int arrival_time,
391 const struct city *dest,
392 const struct caravan_parameter *param)
393{
394 int costwithout, costwith;
395 int shields_at_arrival;
396
397 if (!param->consider_wonders
398 /* TODO: Should helping an ally to build be considered when legal? */
399 || unit_owner(caravan) != city_owner(dest)
400 || !city_production_gets_caravan_shields(&dest->production)
401 /* TODO: Should helping to build a unit be considered when legal? */
402 || VUT_UTYPE == dest->production.kind
403 /* TODO: Should helping to build an improvement be considered when
404 * legal? */
405 || !is_wonder(dest->production.value.building)) {
406 return 0;
407 }
408
409 shields_at_arrival = dest->shield_stock
410 + arrival_time * dest->surplus[O_SHIELD];
411
412 costwithout = impr_buy_gold_cost(dest, dest->production.value.building,
413 shields_at_arrival);
414 costwith = impr_buy_gold_cost(dest, dest->production.value.building,
415 shields_at_arrival + unit_build_shield_cost_base(caravan));
416
417 fc_assert_ret_val(costwithout >= costwith, -1.0);
418 return costwithout - costwith;
419}
420
421/************************************************************************/
425 static double presentvalue(double payment, int term, double rate)
426{
427 return payment * pow(rate, term);
428}
429
430/************************************************************************/
434 static double perpetuity(double payment, double rate)
435{
436 return payment / (1.0 - rate);
437}
438
439/************************************************************************/
443 static double annuity(double payment, int term, double rate)
444{
445 return perpetuity(payment, rate) * (1.0 - 1.0 / pow(rate, term));
446}
447
448/************************************************************************/
451 static bool does_foreign_trade_param_allow(const struct caravan_parameter *param,
452 struct player *src, struct player *dest)
453{
454 switch (param->allow_foreign_trade) {
455 case FTL_NATIONAL_ONLY:
456 return (src == dest);
457 break;
458 case FTL_ALLIED:
459 return pplayers_allied(src, dest);
460 break;
461 case FTL_PEACEFUL:
462 return pplayers_in_peace(src, dest);
463 break;
464 case FTL_NONWAR:
465 return !pplayers_at_war(src, dest);
466 }
467
468 fc_assert(FALSE);
469 return FALSE;
470}
471
472/************************************************************************/
478 static bool get_discounted_reward(const struct unit *caravan,
479 const struct caravan_parameter *parameter,
480 struct caravan_result *result)
481{
482 double trade;
483 double windfall;
484 double wonder;
485 const struct city *src = result->src;
486 const struct city *dest = result->dest;
487 int arrival_time = result->arrival_time;
488 double discount = parameter->discount;
489 struct player *pplayer_src = city_owner(src);
490 struct player *pplayer_dest = city_owner(dest);
491 int cost = unit_build_shield_cost(src, caravan);
492 bool consider_wonder;
493 bool consider_trade;
494 bool consider_windfall;
495 struct goods_type *pgood;
496
497 /* if no foreign trade is allowed, just quit. */
498 if (!does_foreign_trade_param_allow(parameter, pplayer_src, pplayer_dest)) {
499 caravan_result_init_zero(result);
500 return FALSE;
501 }
502
503 consider_wonder = parameter->consider_wonders
504 && action_prob_possible(
505 action_speculate_unit_on_city(ACTION_HELP_WONDER,
506 caravan, src, city_tile(dest),
507 TRUE, dest));
508 consider_trade = parameter->consider_trade
509 && action_prob_possible(
510 action_speculate_unit_on_city(ACTION_TRADE_ROUTE,
511 caravan, src, city_tile(dest),
512 TRUE, dest));
513 consider_windfall = parameter->consider_windfall
514 && action_prob_possible(
515 action_speculate_unit_on_city(ACTION_MARKETPLACE,
516 caravan, src, city_tile(dest),
517 TRUE, dest));
518
519 if (!consider_wonder && !consider_trade && !consider_windfall) {
520 /* No caravan action is possible against this target. */
521 caravan_result_init_zero(result);
522
523 return FALSE;
524 }
525
526 if (consider_wonder) {
527 wonder = wonder_benefit(caravan, arrival_time, dest, parameter);
528 /* We want to aid for wonder building */
529 wonder *= 2;
530
531 wonder = presentvalue(wonder, arrival_time, discount);
532 } else {
533 wonder = -1.0;
534 }
535
536 pgood = goods_from_city_to_unit(src, NULL);
537
538 if (consider_trade) {
539 trade = trade_benefit(pplayer_src, src, dest, pgood, parameter);
540 if (parameter->horizon == FC_INFINITY) {
541 trade = perpetuity(trade, discount);
542 } else {
543 trade = annuity(trade, parameter->horizon - arrival_time, discount);
544 }
545 trade = presentvalue(trade, arrival_time, discount);
546 } else {
547 trade = 0.0;
548 }
549
550 if (consider_windfall) {
551 windfall = windfall_benefit(caravan, src, dest, pgood, parameter);
552 windfall = presentvalue(windfall, arrival_time, discount);
553 } else {
554 windfall = 0.0;
555 }
556
557 if ((consider_trade
558 || (consider_windfall
559 && (!parameter->consider_trade /* can only enter marketplaces */
560 /* (FIXME: test any extra restrictions for trade routes) */
561 /* or the result is so big that we are still in plus */
562 /* (consider having produced IF_GOLD instead) */
563 || windfall
564 >= presentvalue(cost, -cost
565 / MAX(src->surplus[O_SHIELD], 2), discount)))
566 ) && trade + windfall >= wonder) {
567 result->value = trade + windfall;
568 result->help_wonder = FALSE;
569 } else if (consider_wonder) {
570 result->value = wonder;
571 result->help_wonder = TRUE;
572 } else {
573 caravan_result_init_zero(result);
574 return FALSE;
575 }
576
577 if (parameter->callback) {
578 parameter->callback(result, parameter->callback_data);
579 }
580
581 return TRUE;
582}
583
584/****************************************************************************
585 Functions to compute the benefit of moving the caravan to dest.
586****************************************************************************/
587
588/************************************************************************/
591 static void caravan_find_best_destination_notransit(const struct unit *caravan,
592 const struct caravan_parameter *param,
593 struct caravan_result *best)
594{
595 struct caravan_result current;
596 struct city *pcity = game_city_by_number(caravan->homecity);
597 struct player *src_owner = city_owner(pcity);
598
599 caravan_result_init(best, pcity, NULL, 0);
600 current = *best;
601
602 players_iterate(dest_owner) {
603 if (does_foreign_trade_param_allow(param, src_owner, dest_owner)) {
604 city_list_iterate(dest_owner->cities, dest) {
605 caravan_result_init(&current, pcity, dest, 0);
606 get_discounted_reward(caravan, param, &current);
607
608 if (caravan_result_compare(&current, best) > 0) {
609 *best = current;
610 }
611 } city_list_iterate_end;
612 }
613 } players_iterate_end;
614}
615
616/************************************************************************/
620 struct cfbdw_data {
621 const struct caravan_parameter *param;
622 const struct unit *caravan;
623 struct caravan_result *best;
624};
625
626 static bool cfbdw_callback(const struct civ_map *nmap,
627 void *vdata, const struct city *dest,
628 int arrival_time, int moves_left)
629{
630 struct cfbdw_data *data = vdata;
631 struct caravan_result current;
632
633 caravan_result_init(&current, data->best->src, dest, arrival_time);
634
635 get_discounted_reward(data->caravan, data->param, &current);
636 if (caravan_result_compare(&current, data->best) > 0) {
637 *data->best = current;
638 }
639
640 return FALSE; /* don't stop. */
641}
642
643/************************************************************************/
646 static void caravan_find_best_destination_withtransit(
647 const struct civ_map *nmap,
648 const struct unit *caravan,
649 const struct caravan_parameter *param,
650 const struct city *src,
651 int turns_before,
652 int moves_left,
653 bool omniscient,
654 struct caravan_result *result)
655{
656 struct tile *start_tile;
657 struct cfbdw_data data;
658
659 data.param = param;
660 data.caravan = caravan;
661 data.best = result;
662 caravan_result_init(data.best, src, NULL, 0);
663
664 if (src->id != caravan->homecity) {
665 start_tile = src->tile;
666 } else {
667 start_tile = unit_tile(caravan);
668 }
669
670 caravan_search_from(nmap, caravan, param, start_tile, turns_before,
671 caravan->moves_left, omniscient, cfbdw_callback, &data);
672}
673
674/************************************************************************/
679 void caravan_find_best_destination(const struct civ_map *nmap,
680 const struct unit *caravan,
681 const struct caravan_parameter *parameter,
682 struct caravan_result *result, bool omniscient)
683{
684 if (parameter->ignore_transit_time) {
685 caravan_find_best_destination_notransit(caravan, parameter, result);
686 } else {
687 const struct city *src = game_city_by_number(caravan->homecity);
688
689 fc_assert(src != NULL);
690
691 caravan_find_best_destination_withtransit(nmap, caravan, parameter, src, 0,
692 caravan->moves_left, omniscient, result);
693 }
694}
695
696/************************************************************************/
699 static void caravan_optimize_notransit(const struct unit *caravan,
700 const struct caravan_parameter *param,
701 struct caravan_result *best)
702{
703 struct player *pplayer = unit_owner(caravan);
704
705 /* Iterate over all cities we own (since the caravan could change its
706 * home city); iterate over all cities we know about (places the caravan
707 * can go to); pick out the best trade route. */
708 city_list_iterate(pplayer->cities, src) {
709 players_iterate(dest_owner) {
710 if (does_foreign_trade_param_allow(param, pplayer, dest_owner)) {
711 city_list_iterate(dest_owner->cities, dest) {
712 struct caravan_result current;
713
714 caravan_result_init(&current, src, dest, 0);
715 get_discounted_reward(caravan, param, &current);
716 if (caravan_result_compare(&current, best) > 0) {
717 *best = current;
718 }
719 } city_list_iterate_end;
720 }
721 } players_iterate_end;
722 } city_list_iterate_end;
723}
724
725/****************************************************************************
726 Struct for the caravan_search invocation in
727 caravan_optimize_withtransit.
728****************************************************************************/
729 struct cowt_data {
730 const struct caravan_parameter *param;
731 const struct unit *caravan;
732 struct caravan_result *best;
733 bool omniscient;
734};
735
736/************************************************************************/
743 static bool cowt_callback(const struct civ_map *nmap, void *vdata,
744 const struct city *pcity,
745 int arrival_time, int moves_left)
746{
747 struct cowt_data *data = vdata;
748 const struct unit *caravan = data->caravan;
749 struct caravan_result current;
750
751 caravan_result_init(&current, game_city_by_number(caravan->homecity),
752 pcity, arrival_time);
753
754 /* First, see what benefit we'd get from not changing home city */
755 get_discounted_reward(caravan, data->param, &current);
756 if (caravan_result_compare(&current, data->best) > 0) {
757 *data->best = current;
758 }
759
760 /* Next, try changing home city (if we're allowed to) */
761 if (city_owner(pcity) == unit_owner(caravan)) {
762 caravan_find_best_destination_withtransit(
763 nmap, caravan, data->param, pcity, arrival_time, moves_left, data->omniscient,
764 &current);
765 if (caravan_result_compare(&current, data->best) > 0) {
766 *data->best = current;
767 }
768 }
769
770 return FALSE; /* Don't stop searching */
771}
772
773/************************************************************************/
777 static void caravan_optimize_withtransit(const struct civ_map *nmap,
778 const struct unit *caravan,
779 const struct caravan_parameter *param,
780 struct caravan_result *result,
781 bool omniscient)
782{
783 struct cowt_data data;
784
785 data.param = param;
786 data.caravan = caravan;
787 data.best = result;
788 data.omniscient = omniscient;
789 caravan_result_init_zero(data.best);
790 caravan_search_from(nmap, caravan, param, unit_tile(caravan), 0,
791 caravan->moves_left, omniscient, cowt_callback, &data);
792}
793
794/************************************************************************/
799 void caravan_optimize_allpairs(const struct unit *caravan,
800 const struct caravan_parameter *param,
801 struct caravan_result *result, bool omniscient)
802{
803 const struct civ_map *nmap = &(wld.map);
804
805 if (param->ignore_transit_time) {
806 caravan_optimize_notransit(caravan, param, result);
807 } else {
808 caravan_optimize_withtransit(nmap, caravan, param, result, omniscient);
809 }
810}
action_prob_possible
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5034
action_speculate_unit_on_city
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)
Definition actions.c:4786
wonder_benefit
static double wonder_benefit(const struct unit *caravan, int arrival_time, const struct city *dest, const struct caravan_parameter *param)
Definition caravan.c:390
perpetuity
static double perpetuity(double payment, double rate)
Definition caravan.c:434
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)
Definition caravan.c:679
does_foreign_trade_param_allow
static bool does_foreign_trade_param_allow(const struct caravan_parameter *param, struct player *src, struct player *dest)
Definition caravan.c:451
cowt_callback
static bool cowt_callback(const struct civ_map *nmap, void *vdata, const struct city *pcity, int arrival_time, int moves_left)
Definition caravan.c:743
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)
Definition caravan.c:208
caravan_parameter_is_legal
bool caravan_parameter_is_legal(const struct caravan_parameter *parameter)
Definition caravan.c:72
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)
Definition caravan.c:591
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)
Definition caravan.c:777
caravan_result_init_zero
void caravan_result_init_zero(struct caravan_result *result)
Definition caravan.c:144
caravan_optimize_notransit
static void caravan_optimize_notransit(const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *best)
Definition caravan.c:699
annuity
static double annuity(double payment, int term, double rate)
Definition caravan.c:443
cfbdw_callback
static bool cfbdw_callback(const struct civ_map *nmap, void *vdata, const struct city *dest, int arrival_time, int moves_left)
Definition caravan.c:626
ftl_name
static const char * ftl_name(enum foreign_trade_limit ftl)
Definition caravan.c:96
search_callback
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
caravan_result_compare
int caravan_result_compare(const struct caravan_result *a, const struct caravan_result *b)
Definition caravan.c:183
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)
Definition caravan.c:291
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)
Definition caravan.c:646
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)
Definition caravan.c:115
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)
Definition caravan.c:344
get_discounted_reward
static bool get_discounted_reward(const struct unit *caravan, const struct caravan_parameter *parameter, struct caravan_result *result)
Definition caravan.c:478
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)
Definition caravan.c:257
caravan_parameter_init_default
void caravan_parameter_init_default(struct caravan_parameter *parameter)
Definition caravan.c:36
presentvalue
static double presentvalue(double payment, int term, double rate)
Definition caravan.c:425
caravan_parameter_init_from_unit
void caravan_parameter_init_from_unit(struct caravan_parameter *parameter, const struct unit *caravan)
Definition caravan.c:53
caravan_result_init
static void caravan_result_init(struct caravan_result *result, const struct city *src, const struct city *dest, int arrival_time)
Definition caravan.c:157
caravan_optimize_allpairs
void caravan_optimize_allpairs(const struct unit *caravan, const struct caravan_parameter *param, struct caravan_result *result, bool omniscient)
Definition caravan.c:799
caravan.h
foreign_trade_limit
foreign_trade_limit
Definition caravan.h:29
FTL_ALLIED
@ FTL_ALLIED
Definition caravan.h:31
FTL_NATIONAL_ONLY
@ FTL_NATIONAL_ONLY
Definition caravan.h:30
FTL_NONWAR
@ FTL_NONWAR
Definition caravan.h:33
FTL_PEACEFUL
@ FTL_PEACEFUL
Definition caravan.h:32
city_production_gets_caravan_shields
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1852
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:508
city_tile
#define city_tile(_pcity_)
Definition city.h:564
city_owner
#define city_owner(_pcity_)
Definition city.h:563
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:510
incite_cost
char * incite_cost
Definition comments.c:74
cost
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:74
O_SHIELD
@ O_SHIELD
Definition fc_types.h:101
wld
struct world wld
Definition game.c:63
game_city_by_number
struct city * game_city_by_number(int id)
Definition game.c:107
game.h
pos
static struct tile * pos
Definition finddlg.c:53
impr_buy_gold_cost
int impr_buy_gold_cost(const struct city *pcity, const struct impr_type *pimprove, int shields_in_stock)
Definition improvement.c:306
is_wonder
bool is_wonder(const struct impr_type *pimprove)
Definition improvement.c:343
do_log
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
log.h
fc_assert_msg
#define fc_assert_msg(condition, message,...)
Definition log.h:181
fc_assert
#define fc_assert(condition)
Definition log.h:176
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_level
log_level
Definition log.h:28
pf_map_new
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
Definition path_finding.c:3182
pf_map_destroy
void pf_map_destroy(struct pf_map *pfm)
Definition path_finding.c:3206
path_finding.h
pf_map_positions_iterate_end
#define pf_map_positions_iterate_end
Definition path_finding.h:612
pf_map_positions_iterate
#define pf_map_positions_iterate(ARG_pfm, NAME_pos, COND_from_start)
Definition path_finding.h:605
pft_fill_unit_parameter
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:843
pf_tools.h
pplayers_at_war
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
pplayers_allied
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1409
pplayers_in_peace
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1430
players_iterate_end
#define players_iterate_end
Definition player.h:539
players_iterate
#define players_iterate(_pplayer)
Definition player.h:534
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
FC_INFINITY
#define FC_INFINITY
Definition shared.h:36
MAX
#define MAX(x, y)
Definition shared.h:54
callback
Definition gui_main.c:2404
caravan_parameter
Definition caravan.h:61
caravan_parameter::horizon
int horizon
Definition caravan.h:65
caravan_parameter::consider_trade
bool consider_trade
Definition caravan.h:86
caravan_parameter::convert_trade
bool convert_trade
Definition caravan.h:116
caravan_parameter::consider_windfall
bool consider_windfall
Definition caravan.h:85
caravan_parameter::callback
void(* callback)(const struct caravan_result *result, void *data)
Definition caravan.h:124
caravan_parameter::allow_foreign_trade
enum foreign_trade_limit allow_foreign_trade
Definition caravan.h:99
caravan_parameter::consider_wonders
bool consider_wonders
Definition caravan.h:87
caravan_parameter::ignore_transit_time
bool ignore_transit_time
Definition caravan.h:108
caravan_parameter::discount
double discount
Definition caravan.h:77
caravan_parameter::account_for_broken_routes
bool account_for_broken_routes
Definition caravan.h:93
caravan_parameter::callback_data
void * callback_data
Definition caravan.h:125
caravan_result
Definition caravan.h:51
caravan_result::src
const struct city * src
Definition caravan.h:52
caravan_result::dest
const struct city * dest
Definition caravan.h:53
caravan_result::arrival_time
int arrival_time
Definition caravan.h:54
caravan_result::help_wonder
bool help_wonder
Definition caravan.h:57
caravan_result::required_boat
bool required_boat
Definition caravan.h:58
caravan_result::value
double value
Definition caravan.h:56
cfbdw_data
Definition caravan.c:620
cfbdw_data::best
struct caravan_result * best
Definition caravan.c:623
cfbdw_data::caravan
const struct unit * caravan
Definition caravan.c:622
cfbdw_data::param
const struct caravan_parameter * param
Definition caravan.c:621
city
Definition city.h:320
city::surplus
int surplus[O_LAST]
Definition city.h:355
city::id
int id
Definition city.h:326
city::production
struct universal production
Definition city.h:396
city::bonus
int bonus[O_LAST]
Definition city.h:363
city::tile
struct tile * tile
Definition city.h:322
city::shield_stock
int shield_stock
Definition city.h:368
civ_map
Definition map_types.h:71
cowt_data
Definition caravan.c:729
cowt_data::param
const struct caravan_parameter * param
Definition caravan.c:730
cowt_data::best
struct caravan_result * best
Definition caravan.c:732
cowt_data::caravan
const struct unit * caravan
Definition caravan.c:731
cowt_data::omniscient
bool omniscient
Definition caravan.c:733
goods_type
Definition traderoutes.h:187
pf_map
Definition path_finding.c:78
pf_parameter
Definition path_finding.h:357
pf_parameter::start_tile
struct tile * start_tile
Definition path_finding.h:359
player
Definition player.h:249
player::cities
struct city_list * cities
Definition player.h:281
tile
Definition tile.h:50
unit
Definition unit.h:140
unit::moves_left
int moves_left
Definition unit.h:152
unit::homecity
int homecity
Definition unit.h:148
universal::kind
enum universals_n kind
Definition fc_types.h:910
universal::value
universals_u value
Definition fc_types.h:909
world::map
struct civ_map map
Definition world_object.h:38
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
bool
#define bool
Definition support.h:71
tile_city
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
tile_continent
#define tile_continent(_tile)
Definition tile.h:93
can_cities_trade
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
Definition traderoutes.c:214
cities_trade_route_type
enum trade_route_type cities_trade_route_type(const struct city *pcity1, const struct city *pcity2)
Definition traderoutes.c:58
trade_base_between_cities
int trade_base_between_cities(const struct city *pc1, const struct city *pc2)
Definition traderoutes.c:331
city_num_trade_routes
int city_num_trade_routes(const struct city *pcity)
Definition traderoutes.c:392
trade_route_settings_by_type
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
Definition traderoutes.c:201
goods_from_city_to_unit
struct goods_type * goods_from_city_to_unit(const struct city *src, const struct unit *punit)
Definition traderoutes.c:734
max_trade_routes
unsigned max_trade_routes(const struct city *pcity)
Definition traderoutes.c:48
city_trade_removable
int city_trade_removable(const struct city *pcity, int priority, struct trade_route_list *would_remove)
Definition traderoutes.c:231
can_establish_trade_route
bool can_establish_trade_route(const struct city *pc1, const struct city *pc2, int priority)
Definition traderoutes.c:283
get_caravan_enter_city_trade_bonus
int get_caravan_enter_city_trade_bonus(const struct city *pc1, const struct city *pc2, const struct unit_type *ut, struct goods_type *pgood, const bool establish_trade)
Definition traderoutes.c:476
traderoutes.h
trade_route_list_iterate
#define trade_route_list_iterate(trade_route_list, proute)
Definition traderoutes.h:95
trade_routes_iterate_end
#define trade_routes_iterate_end
Definition traderoutes.h:135
trade_route_list_iterate_end
#define trade_route_list_iterate_end
Definition traderoutes.h:97
trade_routes_iterate
#define trade_routes_iterate(c, proute)
Definition traderoutes.h:131
universals_u::building
const struct impr_type * building
Definition fc_types.h:721
unit_can_do_action
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:385
unit_tile
#define unit_tile(_pu)
Definition unit.h:397
unit_owner
#define unit_owner(_pu)
Definition unit.h:396
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
unit_build_shield_cost
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
Definition unittype.c:1476
unit_build_shield_cost_base
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1484