Freeciv-3.3
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 "nation.h"
25#include "player.h"
26
27#include "diptreaty.h"
28
30
31static struct treaty_list *treaties = NULL;
32
33/**********************************************************************/
37 const struct player *pplayer2)
38{
39 switch (game.info.diplomacy) {
40 case DIPLO_FOR_ALL:
41 return TRUE;
43 return (is_human(pplayer1) && is_human(pplayer2));
44 case DIPLO_FOR_AIS:
45 return (is_ai(pplayer1) && is_ai(pplayer2));
46 case DIPLO_NO_AIS:
47 return (!is_ai(pplayer1) || !is_ai(pplayer2));
48 case DIPLO_NO_MIXED:
49 return ((is_human(pplayer1) && is_human(pplayer2))
50 || (is_ai(pplayer1) && is_ai(pplayer2)));
51 case DIPLO_FOR_TEAMS:
53 case DIPLO_DISABLED:
54 return FALSE;
55 }
56 log_error("%s(): Unsupported diplomacy variant %d.",
58 return FALSE;
59}
60
61/**********************************************************************/
64bool could_meet_with_player(const struct player *pplayer,
65 const struct player *aplayer)
66{
67 return (pplayer->is_alive
68 && aplayer->is_alive
69 && pplayer != aplayer
70 && diplomacy_possible(pplayer,aplayer)
71 && get_player_bonus(pplayer, EFT_NO_DIPLOMACY) <= 0
73 && (player_has_embassy(aplayer, pplayer)
74 || player_has_embassy(pplayer, aplayer)
75 || player_diplstate_get(pplayer, aplayer)->contact_turns_left
76 > 0
77 || player_diplstate_get(aplayer, pplayer)->contact_turns_left
78 > 0));
79}
80
81/**********************************************************************/
84bool could_intel_with_player(const struct player *pplayer,
85 const struct player *aplayer)
86{
87 return (pplayer->is_alive
88 && aplayer->is_alive
89 && pplayer != aplayer
90 && (player_diplstate_get(pplayer, aplayer)->contact_turns_left > 0
91 || player_diplstate_get(aplayer, pplayer)->contact_turns_left
92 > 0
93 || team_has_embassy(pplayer->team, aplayer)));
94}
95
96/**********************************************************************/
100 struct player *plr0, struct player *plr1)
101{
102 ptreaty->plr0 = plr0;
103 ptreaty->plr1 = plr1;
107}
108
109/**********************************************************************/
119
120/**********************************************************************/
124 enum clause_type type, int val)
125{
127 if (pclause->type == type && pclause->from == pfrom
128 && pclause->value == val) {
130 free(pclause);
131
134
135 return TRUE;
136 }
138
139 return FALSE;
140}
141
142/**********************************************************************/
145bool add_clause(struct treaty *ptreaty, struct player *pfrom,
146 enum clause_type type, int val,
147 struct player *client_player)
148{
149 struct player *pto = (pfrom == ptreaty->plr0
150 ? ptreaty->plr1 : ptreaty->plr0);
151 struct Clause *pclause;
152 enum diplstate_type ds
154
156 log_error("Illegal clause type encountered.");
157 return FALSE;
158 }
159
161 log_error("Illegal tech value %i in clause.", val);
162 return FALSE;
163 }
164
166 && ((ds == DS_PEACE && type == CLAUSE_PEACE)
167 || (ds == DS_ARMISTICE && type == CLAUSE_PEACE)
168 || (ds == DS_ALLIANCE && type == CLAUSE_ALLIANCE)
169 || (ds == DS_CEASEFIRE && type == CLAUSE_CEASEFIRE))) {
170 /* we already have this diplomatic state */
171 log_error("Illegal treaty suggested between %s and %s - they "
172 "already have this treaty level.",
175 return FALSE;
176 }
177
179 /* We already have embassy */
180 log_error("Illegal embassy clause: %s already have embassy with %s.",
183 return FALSE;
184 }
185
186 if (!clause_enabled(type)) {
187 return FALSE;
188 }
189
190 /* Leave it to the server to decide if the other party can meet
191 * the requirements. */
192 if (client_player == NULL || client_player == pfrom) {
193 if (!are_reqs_active(&(const struct req_context) { .player = pfrom },
194 &(const struct req_context) { .player = pto },
196 return FALSE;
197 }
198 }
199 if (client_player == NULL || client_player == pto) {
200 if (!are_reqs_active(&(const struct req_context) { .player = pto },
201 &(const struct req_context) { .player = pfrom },
203 RPT_POSSIBLE)) {
204 return FALSE;
205 }
206 }
207 if (client_player == NULL) {
208 if (!are_reqs_active(&(const struct req_context) { .player = pfrom },
209 &(const struct req_context) { .player = pto },
212 && !are_reqs_active(&(const struct req_context) { .player = pto },
213 &(const struct req_context) { .player = pfrom },
215 RPT_POSSIBLE)) {
216 return FALSE;
217 }
218 }
219
221 if (old_clause->type == type
222 && old_clause->from == pfrom
223 && old_clause->value == val) {
224 /* same clause already there */
225 return FALSE;
226 }
228 && is_pact_clause(old_clause->type)) {
229 /* pact clause already there */
232 old_clause->type = type;
233 return TRUE;
234 }
235 if (type == CLAUSE_GOLD && old_clause->type == CLAUSE_GOLD
236 && old_clause->from == pfrom) {
237 /* gold clause there, different value */
240 old_clause->value = val;
241 return TRUE;
242 }
244
245 pclause = fc_malloc(sizeof(*pclause));
246
247 pclause->type = type;
248 pclause->from = pfrom;
249 pclause->value = val;
250
252
255
256 return TRUE;
257}
258
259/**********************************************************************/
263{
264 int i;
265
266 for (i = 0; i < CLAUSE_COUNT; i++) {
267 clause_infos[i].type = i;
269 requirement_vector_init(&(clause_infos[i].giver_reqs));
270 requirement_vector_init(&(clause_infos[i].receiver_reqs));
271 requirement_vector_init(&(clause_infos[i].either_reqs));
272 }
273}
274
275/**********************************************************************/
279{
280 int i;
281
282 for (i = 0; i < CLAUSE_COUNT; i++) {
283 requirement_vector_free(&(clause_infos[i].giver_reqs));
284 requirement_vector_free(&(clause_infos[i].receiver_reqs));
285 requirement_vector_free(&(clause_infos[i].either_reqs));
286 }
287}
288
289/**********************************************************************/
293{
294 fc_assert(type >= 0 && type < CLAUSE_COUNT);
295
296 return &clause_infos[type];
297}
298
299/**********************************************************************/
306{
307 struct clause_info *info = &clause_infos[type];
308
309 if (!info->enabled) {
310 return FALSE;
311 }
312
314 return FALSE;
315 }
317 return FALSE;
318 }
320 return FALSE;
321 }
322
323 return TRUE;
324}
325
326/**********************************************************************/
330{
332}
333
334/**********************************************************************/
338{
340
342 treaties = NULL;
343}
344
345/**********************************************************************/
349{
350 /* Free memory allocated for treaties */
353 free(pt);
355
357}
358
359/**********************************************************************/
362struct treaty *find_treaty(struct player *plr0, struct player *plr1)
363{
365 if ((ptreaty->plr0 == plr0 && ptreaty->plr1 == plr1)
366 || (ptreaty->plr0 == plr1 && ptreaty->plr1 == plr0)) {
367 return ptreaty;
368 }
370
371 return NULL;
372}
373
374/**********************************************************************/
381
382/**********************************************************************/
392
393/**********************************************************************/
396void treaties_iterate(treaty_cb cb, void *data)
397{
399 cb(ptr, data);
401}
#define client_player()
char * incite_cost
Definition comments.c:76
struct treaty * ptreaty
Definition diplodlg_g.h:28
void treaty_remove(struct treaty *ptreaty)
Definition diptreaty.c:385
void treaty_add(struct treaty *ptreaty)
Definition diptreaty.c:377
static struct treaty_list * treaties
Definition diptreaty.c:31
bool diplomacy_possible(const struct player *pplayer1, const struct player *pplayer2)
Definition diptreaty.c:36
void clause_infos_init(void)
Definition diptreaty.c:262
struct treaty * find_treaty(struct player *plr0, struct player *plr1)
Definition diptreaty.c:362
void clause_infos_free(void)
Definition diptreaty.c:278
void free_treaties(void)
Definition diptreaty.c:348
void init_treaty(struct treaty *ptreaty, struct player *plr0, struct player *plr1)
Definition diptreaty.c:99
void treaties_init(void)
Definition diptreaty.c:329
bool add_clause(struct treaty *ptreaty, struct player *pfrom, enum clause_type type, int val, struct player *client_player)
Definition diptreaty.c:145
void treaties_iterate(treaty_cb cb, void *data)
Definition diptreaty.c:396
bool clause_enabled(enum clause_type type)
Definition diptreaty.c:305
static struct clause_info clause_infos[CLAUSE_COUNT]
Definition diptreaty.c:29
void clear_treaty(struct treaty *ptreaty)
Definition diptreaty.c:112
bool remove_clause(struct treaty *ptreaty, struct player *pfrom, enum clause_type type, int val)
Definition diptreaty.c:123
bool could_intel_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:84
void treaties_free(void)
Definition diptreaty.c:337
struct clause_info * clause_info_get(enum clause_type type)
Definition diptreaty.c:292
bool could_meet_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:64
#define clause_list_iterate_end
Definition diptreaty.h:73
#define treaty_list_iterate(list, p)
Definition diptreaty.h:113
#define clause_list_iterate(clauselist, pclause)
Definition diptreaty.h:71
#define treaty_list_iterate_end
Definition diptreaty.h:115
#define is_pact_clause(x)
Definition diptreaty.h:53
void(* treaty_cb)(struct treaty *, void *data)
Definition diptreaty.h:126
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:824
@ DIPLO_NO_MIXED
Definition fc_types.h:762
@ DIPLO_FOR_TEAMS
Definition fc_types.h:763
@ DIPLO_NO_AIS
Definition fc_types.h:761
@ DIPLO_FOR_HUMANS
Definition fc_types.h:759
@ DIPLO_FOR_ALL
Definition fc_types.h:758
@ DIPLO_FOR_AIS
Definition fc_types.h:760
@ DIPLO_DISABLED
Definition fc_types.h:764
@ RPT_POSSIBLE
Definition fc_types.h:532
struct civ_game game
Definition game.c:61
GType type
Definition repodlgs.c:1313
#define fc_assert(condition)
Definition log.h:177
#define log_error(message,...)
Definition log.h:104
#define fc_malloc(sz)
Definition mem.h:34
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1480
bool player_has_real_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:240
bool team_has_embassy(const struct team *pteam, const struct player *tgt_player)
Definition player.c:220
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:324
bool player_has_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:208
#define is_ai(plr)
Definition player.h:232
#define is_human(plr)
Definition player.h:231
bool are_reqs_active(const struct req_context *context, const struct req_context *other_context, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
struct packet_game_info info
Definition game.h:89
struct requirement_vector receiver_reqs
Definition diptreaty.h:61
struct requirement_vector giver_reqs
Definition diptreaty.h:60
enum clause_type type
Definition diptreaty.h:58
struct requirement_vector either_reqs
Definition diptreaty.h:62
bool enabled
Definition diptreaty.h:59
enum diplomacy_mode diplomacy
enum diplstate_type type
Definition player.h:199
struct team * team
Definition player.h:261
bool is_alive
Definition player.h:268
struct player * plr1
Definition diptreaty.h:82
struct clause_list * clauses
Definition diptreaty.h:84
bool accept1
Definition diptreaty.h:83
struct player * plr0
Definition diptreaty.h:82
bool accept0
Definition diptreaty.h:83
#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