Freeciv-3.2
Loading...
Searching...
No Matches
traderoutes.c
Go to the documentation of this file.
1/****************************************************************************
2 Freeciv - Copyright (C) 2004 - The Freeciv Team
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/* utility */
19#include "log.h"
20#include "rand.h"
21
22/* common */
23#include "city.h"
24#include "effects.h"
25#include "game.h"
26#include "map.h"
27#include "tile.h"
28#include "unittype.h"
29
30#include "traderoutes.h"
31
32const char *trade_route_type_names[] = {
33 "National", "NationalIC", "IN", "INIC", "Ally", "AllyIC",
34 "Enemy", "EnemyIC", "Team", "TeamIC"
35};
36
38 "Active", "Inactive", "Cancel"
39};
40
42
44
45/*********************************************************************/
48unsigned max_trade_routes(const struct city *pcity)
49{
51
52 return CLIP(0, eft, MAX_TRADE_ROUTES);
53}
54
55/*********************************************************************/
59 const struct city *pcity2)
60{
61 struct player *plr1 = city_owner(pcity1);
62 struct player *plr2 = city_owner(pcity2);
63
64 if (plr1 != plr2) {
65 struct player_diplstate *ds = player_diplstate_get(plr1, plr2);
66
67 if (city_tile(pcity1)->continent != city_tile(pcity2)->continent) {
68 switch (ds->type) {
69 case DS_ALLIANCE:
70 return TRT_ALLY_IC;
71 case DS_WAR:
72 return TRT_ENEMY_IC;
73 case DS_TEAM:
74 return TRT_TEAM_IC;
75 case DS_ARMISTICE:
76 case DS_CEASEFIRE:
77 case DS_PEACE:
78 case DS_NO_CONTACT:
79 return TRT_IN_IC;
80 case DS_LAST:
81 fc_assert(ds->type != DS_LAST);
82 return TRT_IN_IC;
83 }
85
86 return TRT_IN_IC;
87 } else {
88 switch (ds->type) {
89 case DS_ALLIANCE:
90 return TRT_ALLY;
91 case DS_WAR:
92 return TRT_ENEMY;
93 case DS_TEAM:
94 return TRT_TEAM;
95 case DS_ARMISTICE:
96 case DS_CEASEFIRE:
97 case DS_PEACE:
98 case DS_NO_CONTACT:
99 return TRT_IN;
100 case DS_LAST:
101 fc_assert(ds->type != DS_LAST);
102 return TRT_IN;
103 }
105
106 return TRT_IN;
107 }
108 } else {
109 if (city_tile(pcity1)->continent != city_tile(pcity2)->continent) {
110 return TRT_NATIONAL_IC;
111 } else {
112 return TRT_NATIONAL;
113 }
114 }
115
116 return TRT_LAST;
117}
118
119/*********************************************************************/
123{
124 if (type >= TRT_LAST) {
125 return 0;
126 }
127
128 return trtss[type].trade_pct;
129}
130
131/*********************************************************************/
135{
137
138 for (type = TRT_NATIONAL; type < TRT_LAST; type++) {
140
141 set->trade_pct = 100;
142 }
143}
144
145/*********************************************************************/
154
155/*********************************************************************/
159{
161
162 for (type = TRT_NATIONAL; type < TRT_LAST; type++) {
164 return type;
165 }
166 }
167
168 return TRT_LAST;
169}
170
171/*********************************************************************/
180
181/*********************************************************************/
196
197/*********************************************************************/
207
208/*********************************************************************/
214bool can_cities_trade(const struct city *pc1, const struct city *pc2)
215{
216 /* If you change the logic here, make sure to update the help in
217 * helptext_unit(). */
218 return (pc1 && pc2 && pc1 != pc2
220 || map_distance(pc1->tile, pc2->tile)
223 > 0));
224}
225
226/*********************************************************************/
231int city_trade_removable(const struct city *pcity,
233{
235 int num, i, j;
236
237 /* Sort trade route values. */
238 num = 0;
240 for (j = num; j > 0 && (proute->value < sorted[j - 1]->value) ; j--) {
241 sorted[j] = sorted[j - 1];
242 }
243 sorted[j] = proute;
244 num++;
246
247 /* No trade routes at all. */
248 if (0 == num) {
249 return 0;
250 }
251
252 /* Adjust number of concerned trade routes. */
253 num += 1 - max_trade_routes(pcity);
254 if (0 >= num) {
255 num = 1;
256 }
257
258 /* Return values. */
259 for (i = j = 0; i < num; i++) {
260 j += sorted[i]->value;
261 if (NULL != would_remove) {
263 }
264 }
265
266 return j;
267}
268
269/*********************************************************************/
275bool can_establish_trade_route(const struct city *pc1, const struct city *pc2)
276{
277 int trade = -1;
278 int maxpc1;
279 int maxpc2;
280
281 if (!pc1 || !pc2 || pc1 == pc2
284 return FALSE;
285 }
286
287 /* First check if cities can have trade routes at all. */
289 if (maxpc1 <= 0) {
290 return FALSE;
291 }
293 if (maxpc2 <= 0) {
294 return FALSE;
295 }
296
299 /* can we replace trade route? */
300 if (city_trade_removable(pc1, NULL) >= trade) {
301 return FALSE;
302 }
303 }
304
306 if (trade == -1) {
308 }
309 /* can we replace trade route? */
310 if (city_trade_removable(pc2, NULL) >= trade) {
311 return FALSE;
312 }
313 }
314
315 return TRUE;
316}
317
318/*********************************************************************/
322int trade_base_between_cities(const struct city *pc1, const struct city *pc2)
323{
324 int bonus = 0;
325
326 if (NULL == pc1 || NULL == pc1->tile || NULL == pc2 || NULL == pc2->tile) {
327 return 0;
328 }
329
331 /* Classic Freeciv */
332 int real_dist = real_map_distance(pc1->tile, pc2->tile);
336 * (real_dist * 40 / MAX(wld.map.xsize, wld.map.ysize))) / 100;
337
338 bonus = weighted_distance
340 } else if (game.info.trade_revenue_style == TRS_SIMPLE) {
341 /* Simple revenue style */
342 bonus = (pc1->citizen_base[O_TRADE] + pc2->citizen_base[O_TRADE] + 4)
343 * 3;
344 }
345
346 bonus = bonus
348 / 100;
349
350 bonus /= 12;
351
352 return bonus;
353}
354
355/*********************************************************************/
358int trade_from_route(const struct city *pc1, const struct trade_route *route,
359 int base)
360{
361 int val;
362
363 if (route->dir == RDIR_TO) {
364 val = base * route->goods->to_pct / 100;
365
366 if (route->goods->to_pct > 0) {
367 val = MAX(val, game.info.min_trade_route_val);
368 }
369 } else {
370 val = base * route->goods->from_pct / 100;
371
372 if (route->goods->from_pct > 0) {
373 val = MAX(val, game.info.min_trade_route_val);
374 }
375 }
376
377 return val;
378}
379
380/*********************************************************************/
383int city_num_trade_routes(const struct city *pcity)
384{
385 return trade_route_list_size(pcity->routes);
386}
387
388/*********************************************************************/
391static int best_value(const void *a, const void *b)
392{
393 return *(int *)a < *(int *)b;
394}
395
396/*********************************************************************/
399static int max_tile_trade(const struct city *pcity)
400{
401 int i, total = 0;
402 int radius_sq = city_map_radius_sq_get(pcity);
403 int tile_trade[city_map_tiles(radius_sq)];
404 size_t size = 0;
406 const struct civ_map *nmap = &(wld.map);
407
408 if (pcity->tile == NULL) {
409 return 0;
410 }
411
412 city_map_iterate(radius_sq, cindex, cx, cy) {
413 struct tile *ptile = city_map_to_tile(nmap, pcity->tile, radius_sq, cx, cy);
414
415 if (ptile == NULL) {
416 continue;
417 }
418
419 if (is_free_worked_index(cindex)) {
420 total += city_tile_output(pcity, ptile, is_celebrating, O_TRADE);
421 continue;
422 }
423
424 if (!city_can_work_tile(pcity, ptile)) {
425 continue;
426 }
427
429 O_TRADE);
431
433
434 for (i = 0; i < pcity->size && i < size; i++) {
435 total += tile_trade[i];
436 }
437
438 return total;
439}
440
441/*********************************************************************/
444int max_trade_prod(const struct city *pcity)
445{
446 /* Trade tile base */
447 int trade_prod = max_tile_trade(pcity);
448
449 /* Add trade routes values */
450 trade_partners_iterate(pcity, partner) {
451 trade_prod += trade_base_between_cities(pcity, partner);
453
454 return trade_prod;
455}
456
457/*********************************************************************/
468 const struct city *pc2,
469 const struct unit_type *ut,
470 struct goods_type *pgood,
471 const bool establish_trade)
472{
473 int tb = 0, bonus = 0;
475
476 if (trtss[trtype].bonus_type == TBONUS_NONE) {
477 return 0;
478 }
479
481 /* Should this be real_map_distance? */
482 int distance = map_distance(pc1->tile, pc2->tile);
483 int weighted_distance = ((100 - game.info.trade_world_rel_pct) * distance
485 * (distance * 40 / MAX(wld.map.xsize, wld.map.ysize))) / 100;
486
487 tb = weighted_distance + 10;
488 tb = (tb * (pc1->surplus[O_TRADE] + pc2->surplus[O_TRADE])) / 24;
490 /* Logarithmic bonus */
491 int distance = real_map_distance(pc1->tile, pc2->tile);
492 int weighted_distance = ((100 - game.info.trade_world_rel_pct) * distance
494 * (distance * 40 / MAX(wld.map.xsize, wld.map.ysize))) / 100;
495
497 + max_trade_prod(pc1) + max_trade_prod(pc2)) * 2, 2);
498 }
499
500 if (pgood != NULL) {
501 tb = tb * pgood->onetime_pct / 100;
502 }
503
504 /* Trade_revenue_bonus increases revenue by power of 2 in milimes */
506 &(const struct req_context) {
507 .player = city_owner(pc1),
508 .city = pc1,
509 .tile = city_tile(pc1),
510 /* Putting .unit things like unit veterancy
511 * in the scope give little gameplay
512 * and overcomplicate the AI logic */
513 .unittype = ut,
514 /* Could be used to reduce the one time
515 * bonus if no trade route is
516 * established. */
517 .action = action_by_number(
521 ),
522 }, city_owner(pc2),
524
525 /* Be mercy full to players with small amounts. Round up. */
526 tb = ceil((float)tb * pow(2.0, (double)bonus / 1000.0));
527
528 return tb;
529}
530
531/*********************************************************************/
534bool have_cities_trade_route(const struct city *pc1, const struct city *pc2)
535{
537 if (route_to->id == pc2->id) {
538 return TRUE;
539 }
541
542 return FALSE;
543}
544
545/*********************************************************************/
548void goods_init(void)
549{
550 int i;
551
552 for (i = 0; i < MAX_GOODS_TYPES; i++) {
553 goods[i].id = i;
554
557 goods[i].helptext = NULL;
558 }
559}
560
561/*********************************************************************/
564void goods_free(void)
565{
566 int i;
567
568 for (i = 0; i < MAX_GOODS_TYPES; i++) {
570
571 if (NULL != goods[i].helptext) {
572 strvec_destroy(goods[i].helptext);
573 goods[i].helptext = NULL;
574 }
575 }
576}
577
578/*********************************************************************/
582{
584
585 return pgood->id;
586}
587
588/*********************************************************************/
595{
597
598 return pgood - goods;
599}
600
601/*********************************************************************/
605{
607
608 return &goods[id];
609}
610
611/*********************************************************************/
615{
616 return name_translation_get(&pgood->name);
617}
618
619/*********************************************************************/
622const char *goods_rule_name(struct goods_type *pgood)
623{
624 return rule_name_get(&pgood->name);
625}
626
627/*********************************************************************/
632{
633 const char *qs;
634
635 if (name == NULL) {
636 return NULL;
637 }
638
639 qs = Qn_(name);
640
643 return pgood;
644 }
646
647 return NULL;
648}
649
650/*********************************************************************/
655{
658 return pgood;
659 }
661
662 return NULL;
663}
664
665/*********************************************************************/
668bool goods_has_flag(const struct goods_type *pgood, enum goods_flag_id flag)
669{
670 return BV_ISSET(pgood->flags, flag);
671}
672
673/*********************************************************************/
676bool goods_can_be_provided(const struct city *pcity,
677 const struct goods_type *pgood,
678 struct unit *punit)
679{
680 return are_reqs_active(&(const struct req_context) {
681 .player = city_owner(pcity),
682 .city = pcity,
683 .tile = city_tile(pcity),
684 .unit = punit,
685 .unittype = punit ? unit_type_get(punit) : NULL,
686 },
687 NULL,
688 &pgood->reqs, RPT_CERTAIN);
689}
690
691/*********************************************************************/
694bool city_receives_goods(const struct city *pcity,
695 const struct goods_type *pgood)
696{
698 if (proute->goods == pgood
699 && (proute->dir == RDIR_TO || proute->dir == RDIR_BIDIRECTIONAL)) {
700 struct city *tcity = game_city_by_number(proute->partner);
703
704 if (can_cities_trade(pcity, tcity)) {
705 return TRUE;
706 }
707
710
711 if (settings->cancelling == TRI_ACTIVE) {
712 return TRUE;
713 }
714 }
716
717 return FALSE;
718}
719
720/*********************************************************************/
723struct goods_type *goods_from_city_to_unit(struct city *src, struct unit *punit)
724{
725 int i = 0;
727
729 if (goods_can_be_provided(src, pgood, punit)) {
730 potential[i++] = pgood;
731 }
733
734 if (i == 0) {
735 return NULL;
736 }
737
738 return potential[fc_rand(i)];
739}
static struct action * action_by_number(action_id act_id)
Definition actions.h:635
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
bool base_city_celebrating(const struct city *pcity)
Definition city.c:1637
struct tile * city_map_to_tile(const struct civ_map *nmap, const struct tile *city_center, int city_radius_sq, int city_map_x, int city_map_y)
Definition city.c:305
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:137
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1283
int city_map_tiles(int city_radius_sq)
Definition city.c:171
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1456
#define city_tile(_pcity_)
Definition city.h:564
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
#define city_owner(_pcity_)
Definition city.h:563
#define city_map_iterate_end
Definition city.h:177
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition city.h:173
#define is_free_worked_index(city_tile_index)
Definition city.h:880
char * incite_cost
Definition comments.c:75
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2931
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 * punit
Definition dialogs_g.h:74
int int id
Definition editgui_g.h:28
struct @21::@22 reqs
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type)
Definition effects.c:748
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:846
#define MAX_TRADE_ROUTES
Definition fc_types.h:1259
int Goods_type_id
Definition fc_types.h:387
#define MAX_GOODS_TYPES
Definition fc_types.h:51
@ RPT_CERTAIN
Definition fc_types.h:701
@ O_TRADE
Definition fc_types.h:101
#define Qn_(String)
Definition fcintl.h:89
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
struct city * game_city_by_number(int id)
Definition game.c:107
GType type
Definition repodlgs.c:1313
const char * name
Definition inputfile.c:127
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:630
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:654
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:324
#define fc_rand(_size)
Definition rand.h:56
bool are_reqs_active(const struct req_context *context, const struct player *other_player, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
static struct setting settings[]
Definition settings.c:1473
#define CLIP(lower, current, upper)
Definition shared.h:57
#define MAX(x, y)
Definition shared.h:54
size_t size
Definition specvec.h:72
void strvec_destroy(struct strvec *psv)
Definition city.h:320
struct trade_route_list * routes
Definition city.h:344
citizens size
Definition city.h:332
struct tile * tile
Definition city.h:322
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
int xsize
Definition map_types.h:78
int ysize
Definition map_types.h:78
bool ruledit_disabled
struct strvec * helptext
enum trade_revenue_style trade_revenue_style
enum caravan_bonus_style caravan_bonus_style
Definition tile.h:50
Definition unit.h:138
struct civ_map map
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
int max_trade_prod(const struct city *pcity)
struct goods_type * goods_from_city_to_unit(struct city *src, struct unit *punit)
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
const char * trade_route_cancelling_type_names[]
Definition traderoutes.c:37
int trade_route_type_trade_pct(enum trade_route_type type)
int trade_base_between_cities(const struct city *pc1, const struct city *pc2)
int city_num_trade_routes(const struct city *pcity)
Goods_type_id goods_number(const struct goods_type *pgood)
static struct goods_type goods[MAX_GOODS_TYPES]
Definition traderoutes.c:43
bool goods_has_flag(const struct goods_type *pgood, enum goods_flag_id flag)
enum trade_route_illegal_cancelling trade_route_cancelling_type_by_name(const char *name)
const char * trade_route_type_name(enum trade_route_type type)
static int max_tile_trade(const struct city *pcity)
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
struct trade_route_settings trtss[TRT_LAST]
Definition traderoutes.c:41
struct goods_type * goods_by_rule_name(const char *name)
int trade_from_route(const struct city *pc1, const struct trade_route *route, int base)
const char * goods_rule_name(struct goods_type *pgood)
unsigned max_trade_routes(const struct city *pcity)
Definition traderoutes.c:48
const char * goods_name_translation(struct goods_type *pgood)
void trade_route_types_init(void)
Goods_type_id goods_index(const struct goods_type *pgood)
static int best_value(const void *a, const void *b)
struct goods_type * goods_by_translated_name(const char *name)
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)
struct goods_type * goods_by_number(Goods_type_id id)
const char * trade_route_cancelling_type_name(enum trade_route_illegal_cancelling type)
void goods_init(void)
bool goods_can_be_provided(const struct city *pcity, const struct goods_type *pgood, struct unit *punit)
bool city_receives_goods(const struct city *pcity, const struct goods_type *pgood)
void goods_free(void)
const char * trade_route_type_names[]
Definition traderoutes.c:32
enum trade_route_type trade_route_type_by_name(const char *name)
bool have_cities_trade_route(const struct city *pc1, const struct city *pc2)
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)
#define goods_type_iterate_end
trade_route_illegal_cancelling
Definition traderoutes.h:29
@ TRI_LAST
Definition traderoutes.h:33
@ TRI_ACTIVE
Definition traderoutes.h:30
#define goods_type_iterate(_p)
#define trade_routes_iterate_end
#define trade_partners_iterate_end
#define trade_routes_iterate(c, proute)
#define trade_partners_iterate(c, p)
trade_route_type
Definition traderoutes.h:37
@ TRT_NATIONAL_IC
Definition traderoutes.h:39
@ TRT_TEAM_IC
Definition traderoutes.h:47
@ TRT_ALLY_IC
Definition traderoutes.h:43
@ TRT_IN_IC
Definition traderoutes.h:41
@ TRT_NATIONAL
Definition traderoutes.h:38
@ TRT_LAST
Definition traderoutes.h:48
@ TRT_IN
Definition traderoutes.h:40
@ TRT_TEAM
Definition traderoutes.h:46
@ TRT_ALLY
Definition traderoutes.h:42
@ TRT_ENEMY_IC
Definition traderoutes.h:45
@ TRT_ENEMY
Definition traderoutes.h:44
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123