Freeciv-3.1
Loading...
Searching...
No Matches
diptreaty.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/* utility */
19#include "log.h"
20#include "mem.h"
21
22/* common */
23#include "game.h"
24#include "player.h"
25
26#include "diptreaty.h"
27
28static struct clause_info clause_infos[CLAUSE_COUNT];
29
30/**********************************************************************/
33bool diplomacy_possible(const struct player *pplayer1,
34 const struct player *pplayer2)
35{
36 switch (game.info.diplomacy) {
37 case DIPLO_FOR_ALL:
38 return TRUE;
40 return (is_human(pplayer1) && is_human(pplayer2));
41 case DIPLO_FOR_AIS:
42 return (is_ai(pplayer1) && is_ai(pplayer2));
43 case DIPLO_NO_AIS:
44 return (!is_ai(pplayer1) || !is_ai(pplayer2));
45 case DIPLO_NO_MIXED:
46 return ((is_human(pplayer1) && is_human(pplayer2))
47 || (is_ai(pplayer1) && is_ai(pplayer2)));
48 case DIPLO_FOR_TEAMS:
49 return players_on_same_team(pplayer1, pplayer2);
50 case DIPLO_DISABLED:
51 return FALSE;
52 }
53 log_error("%s(): Unsupported diplomacy variant %d.",
54 __FUNCTION__, game.info.diplomacy);
55 return FALSE;
56}
57
58/**********************************************************************/
61bool could_meet_with_player(const struct player *pplayer,
62 const struct player *aplayer)
63{
64 return (pplayer->is_alive
65 && aplayer->is_alive
66 && pplayer != aplayer
67 && diplomacy_possible(pplayer,aplayer)
68 && get_player_bonus(pplayer, EFT_NO_DIPLOMACY) <= 0
69 && get_player_bonus(aplayer, EFT_NO_DIPLOMACY) <= 0
70 && (player_has_embassy(aplayer, pplayer)
71 || player_has_embassy(pplayer, aplayer)
72 || player_diplstate_get(pplayer, aplayer)->contact_turns_left
73 > 0
74 || player_diplstate_get(aplayer, pplayer)->contact_turns_left
75 > 0));
76}
77
78/**********************************************************************/
81bool could_intel_with_player(const struct player *pplayer,
82 const struct player *aplayer)
83{
84 return (pplayer->is_alive
85 && aplayer->is_alive
86 && pplayer != aplayer
87 && (player_diplstate_get(pplayer, aplayer)->contact_turns_left > 0
88 || player_diplstate_get(aplayer, pplayer)->contact_turns_left
89 > 0
90 || team_has_embassy(pplayer->team, aplayer)));
91}
92
93/**********************************************************************/
96void init_treaty(struct Treaty *ptreaty,
97 struct player *plr0, struct player *plr1)
98{
99 ptreaty->plr0=plr0;
100 ptreaty->plr1=plr1;
101 ptreaty->accept0 = FALSE;
102 ptreaty->accept1 = FALSE;
103 ptreaty->clauses = clause_list_new();
104}
105
106/**********************************************************************/
109void clear_treaty(struct Treaty *ptreaty)
110{
111 clause_list_iterate(ptreaty->clauses, pclause) {
112 free(pclause);
114 clause_list_destroy(ptreaty->clauses);
115}
116
117/**********************************************************************/
120bool remove_clause(struct Treaty *ptreaty, struct player *pfrom,
121 enum clause_type type, int val)
122{
123 clause_list_iterate(ptreaty->clauses, pclause) {
124 if (pclause->type == type && pclause->from == pfrom
125 && pclause->value == val) {
126 clause_list_remove(ptreaty->clauses, pclause);
127 free(pclause);
128
129 ptreaty->accept0 = FALSE;
130 ptreaty->accept1 = FALSE;
131
132 return TRUE;
133 }
135
136 return FALSE;
137}
138
139/**********************************************************************/
142bool add_clause(struct Treaty *ptreaty, struct player *pfrom,
143 enum clause_type type, int val,
144 struct player *client_player)
145{
146 struct player *pto = (pfrom == ptreaty->plr0
147 ? ptreaty->plr1 : ptreaty->plr0);
148 struct Clause *pclause;
149 enum diplstate_type ds
150 = player_diplstate_get(ptreaty->plr0, ptreaty->plr1)->type;
151
152 if (!clause_type_is_valid(type)) {
153 log_error("Illegal clause type encountered.");
154 return FALSE;
155 }
156
157 if (type == CLAUSE_ADVANCE && !valid_advance_by_number(val)) {
158 log_error("Illegal tech value %i in clause.", val);
159 return FALSE;
160 }
161
163 && ((ds == DS_PEACE && type == CLAUSE_PEACE)
164 || (ds == DS_ARMISTICE && type == CLAUSE_PEACE)
165 || (ds == DS_ALLIANCE && type == CLAUSE_ALLIANCE)
166 || (ds == DS_CEASEFIRE && type == CLAUSE_CEASEFIRE))) {
167 /* we already have this diplomatic state */
168 log_error("Illegal treaty suggested between %s and %s - they "
169 "already have this treaty level.",
172 return FALSE;
173 }
174
175 if (type == CLAUSE_EMBASSY && player_has_real_embassy(pto, pfrom)) {
176 /* We already have embassy */
177 log_error("Illegal embassy clause: %s already have embassy with %s.",
180 return FALSE;
181 }
182
183 if (!clause_enabled(type)) {
184 return FALSE;
185 }
186
187 /* Leave it to the server to decide if the other party can meet
188 * the requirements. */
189 if (client_player == NULL || client_player == pfrom) {
190 if (!are_reqs_active(&(const struct req_context) { .player = pfrom },
192 return FALSE;
193 }
194 }
195 if (client_player == NULL || client_player == pto) {
196 if (!are_reqs_active(&(const struct req_context) { .player = pto },
198 RPT_POSSIBLE)) {
199 return FALSE;
200 }
201 }
202
203 clause_list_iterate(ptreaty->clauses, old_clause) {
204 if (old_clause->type == type
205 && old_clause->from == pfrom
206 && old_clause->value == val) {
207 /* same clause already there */
208 return FALSE;
209 }
211 && is_pact_clause(old_clause->type)) {
212 /* pact clause already there */
213 ptreaty->accept0 = FALSE;
214 ptreaty->accept1 = FALSE;
215 old_clause->type = type;
216 return TRUE;
217 }
218 if (type == CLAUSE_GOLD && old_clause->type == CLAUSE_GOLD
219 && old_clause->from == pfrom) {
220 /* gold clause there, different value */
221 ptreaty->accept0 = FALSE;
222 ptreaty->accept1 = FALSE;
223 old_clause->value = val;
224 return TRUE;
225 }
227
228 pclause = fc_malloc(sizeof(*pclause));
229
230 pclause->type = type;
231 pclause->from = pfrom;
232 pclause->value = val;
233
234 clause_list_append(ptreaty->clauses, pclause);
235
236 ptreaty->accept0 = FALSE;
237 ptreaty->accept1 = FALSE;
238
239 return TRUE;
240}
241
242/**********************************************************************/
246{
247 int i;
248
249 for (i = 0; i < CLAUSE_COUNT; i++) {
250 clause_infos[i].type = i;
252 requirement_vector_init(&(clause_infos[i].giver_reqs));
253 requirement_vector_init(&(clause_infos[i].receiver_reqs));
254 }
255}
256
257/**********************************************************************/
261{
262 int i;
263
264 for (i = 0; i < CLAUSE_COUNT; i++) {
265 requirement_vector_free(&(clause_infos[i].giver_reqs));
266 requirement_vector_free(&(clause_infos[i].receiver_reqs));
267 }
268}
269
270/**********************************************************************/
273struct clause_info *clause_info_get(enum clause_type type)
274{
275 fc_assert(type >= 0 && type < CLAUSE_COUNT);
276
277 return &clause_infos[type];
278}
279
280/**********************************************************************/
286bool clause_enabled(enum clause_type type)
287{
288 struct clause_info *info = &clause_infos[type];
289
290 if (!info->enabled) {
291 return FALSE;
292 }
293
294 if (!game.info.trading_gold && type == CLAUSE_GOLD) {
295 return FALSE;
296 }
297 if (!game.info.trading_tech && type == CLAUSE_ADVANCE) {
298 return FALSE;
299 }
300 if (!game.info.trading_city && type == CLAUSE_CITY) {
301 return FALSE;
302 }
303
304 return TRUE;
305}
#define client_player()
bool diplomacy_possible(const struct player *pplayer1, const struct player *pplayer2)
Definition diptreaty.c:33
void clause_infos_init(void)
Definition diptreaty.c:245
void clause_infos_free(void)
Definition diptreaty.c:260
void clear_treaty(struct Treaty *ptreaty)
Definition diptreaty.c:109
void init_treaty(struct Treaty *ptreaty, struct player *plr0, struct player *plr1)
Definition diptreaty.c:96
bool add_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val, struct player *client_player)
Definition diptreaty.c:142
bool clause_enabled(enum clause_type type)
Definition diptreaty.c:286
static struct clause_info clause_infos[CLAUSE_COUNT]
Definition diptreaty.c:28
bool remove_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val)
Definition diptreaty.c:120
bool could_intel_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:81
struct clause_info * clause_info_get(enum clause_type type)
Definition diptreaty.c:273
bool could_meet_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:61
#define clause_list_iterate_end
Definition diptreaty.h:68
#define clause_list_iterate(clauselist, pclause)
Definition diptreaty.h:66
#define is_pact_clause(x)
Definition diptreaty.h:49
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:771
@ DIPLO_NO_MIXED
Definition fc_types.h:908
@ DIPLO_FOR_TEAMS
Definition fc_types.h:909
@ DIPLO_NO_AIS
Definition fc_types.h:907
@ DIPLO_FOR_HUMANS
Definition fc_types.h:905
@ DIPLO_FOR_ALL
Definition fc_types.h:904
@ DIPLO_FOR_AIS
Definition fc_types.h:906
@ DIPLO_DISABLED
Definition fc_types.h:910
@ RPT_POSSIBLE
Definition fc_types.h:585
struct civ_game game
Definition game.c:57
GType type
Definition repodlgs.c:1312
#define fc_assert(condition)
Definition log.h:176
#define log_error(message,...)
Definition log.h:103
#define fc_malloc(sz)
Definition mem.h:34
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:137
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:443
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1452
bool player_has_real_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:234
bool team_has_embassy(const struct team *pteam, const struct player *tgt_player)
Definition player.c:214
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:317
bool player_has_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:202
#define is_ai(plr)
Definition player.h:234
#define is_human(plr)
Definition player.h:233
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)
enum clause_type type
Definition diptreaty.h:71
struct player * from
Definition diptreaty.h:72
int value
Definition diptreaty.h:73
struct player * plr0
Definition diptreaty.h:77
bool accept0
Definition diptreaty.h:78
bool accept1
Definition diptreaty.h:78
struct clause_list * clauses
Definition diptreaty.h:79
struct player * plr1
Definition diptreaty.h:77
struct packet_game_info info
Definition game.h:89
struct requirement_vector receiver_reqs
Definition diptreaty.h:57
struct requirement_vector giver_reqs
Definition diptreaty.h:56
enum clause_type type
Definition diptreaty.h:54
bool enabled
Definition diptreaty.h:55
enum diplomacy_mode diplomacy
enum diplstate_type type
Definition player.h:201
struct team * team
Definition player.h:261
bool is_alive
Definition player.h:268
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:176