Freeciv-3.2
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/**********************************************************************/
123bool remove_clause(struct Treaty *ptreaty, struct player *pfrom,
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 },
195 return FALSE;
196 }
197 }
198 if (client_player == NULL || client_player == pto) {
199 if (!are_reqs_active(&(const struct req_context) { .player = pto },
201 RPT_POSSIBLE)) {
202 return FALSE;
203 }
204 }
205
207 if (old_clause->type == type
208 && old_clause->from == pfrom
209 && old_clause->value == val) {
210 /* same clause already there */
211 return FALSE;
212 }
214 && is_pact_clause(old_clause->type)) {
215 /* pact clause already there */
218 old_clause->type = type;
219 return TRUE;
220 }
221 if (type == CLAUSE_GOLD && old_clause->type == CLAUSE_GOLD
222 && old_clause->from == pfrom) {
223 /* gold clause there, different value */
226 old_clause->value = val;
227 return TRUE;
228 }
230
231 pclause = fc_malloc(sizeof(*pclause));
232
233 pclause->type = type;
234 pclause->from = pfrom;
235 pclause->value = val;
236
238
241
242 return TRUE;
243}
244
245/**********************************************************************/
249{
250 int i;
251
252 for (i = 0; i < CLAUSE_COUNT; i++) {
253 clause_infos[i].type = i;
255 requirement_vector_init(&(clause_infos[i].giver_reqs));
256 requirement_vector_init(&(clause_infos[i].receiver_reqs));
257 }
258}
259
260/**********************************************************************/
264{
265 int i;
266
267 for (i = 0; i < CLAUSE_COUNT; i++) {
268 requirement_vector_free(&(clause_infos[i].giver_reqs));
269 requirement_vector_free(&(clause_infos[i].receiver_reqs));
270 }
271}
272
273/**********************************************************************/
277{
278 fc_assert(type >= 0 && type < CLAUSE_COUNT);
279
280 return &clause_infos[type];
281}
282
283/**********************************************************************/
290{
291 struct clause_info *info = &clause_infos[type];
292
293 if (!info->enabled) {
294 return FALSE;
295 }
296
298 return FALSE;
299 }
301 return FALSE;
302 }
304 return FALSE;
305 }
306
307 return TRUE;
308}
309
310/**********************************************************************/
314{
316}
317
318/**********************************************************************/
322{
324
326 treaties = NULL;
327}
328
329/**********************************************************************/
333{
334 /* Free memory allocated for treaties */
337 free(pt);
339
341}
342
343/**********************************************************************/
346struct Treaty *find_treaty(struct player *plr0, struct player *plr1)
347{
349 if ((ptreaty->plr0 == plr0 && ptreaty->plr1 == plr1)
350 || (ptreaty->plr0 == plr1 && ptreaty->plr1 == plr0)) {
351 return ptreaty;
352 }
354
355 return NULL;
356}
357
358/**********************************************************************/
365
366/**********************************************************************/
376
377/**********************************************************************/
380void treaties_iterate(treaty_cb cb, void *data)
381{
383 cb(ptr, data);
385}
#define client_player()
char * incite_cost
Definition comments.c:75
struct Treaty * ptreaty
Definition diplodlg_g.h:28
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:248
void clause_infos_free(void)
Definition diptreaty.c:263
void free_treaties(void)
Definition diptreaty.c:332
void treaties_init(void)
Definition diptreaty.c:313
void clear_treaty(struct Treaty *ptreaty)
Definition diptreaty.c:112
void init_treaty(struct Treaty *ptreaty, struct player *plr0, struct player *plr1)
Definition diptreaty.c:99
void treaties_iterate(treaty_cb cb, void *data)
Definition diptreaty.c:380
bool add_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val, struct player *client_player)
Definition diptreaty.c:145
bool clause_enabled(enum clause_type type)
Definition diptreaty.c:289
void treaty_add(struct Treaty *ptreaty)
Definition diptreaty.c:361
static struct clause_info clause_infos[CLAUSE_COUNT]
Definition diptreaty.c:29
bool remove_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val)
Definition diptreaty.c:123
void treaty_remove(struct Treaty *ptreaty)
Definition diptreaty.c:369
bool could_intel_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:84
void treaties_free(void)
Definition diptreaty.c:321
struct Treaty * find_treaty(struct player *plr0, struct player *plr1)
Definition diptreaty.c:346
struct clause_info * clause_info_get(enum clause_type type)
Definition diptreaty.c:276
bool could_meet_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:64
void(* treaty_cb)(struct Treaty *, void *data)
Definition diptreaty.h:123
#define clause_list_iterate_end
Definition diptreaty.h:70
#define treaty_list_iterate(list, p)
Definition diptreaty.h:110
#define clause_list_iterate(clauselist, pclause)
Definition diptreaty.h:68
#define treaty_list_iterate_end
Definition diptreaty.h:112
#define is_pact_clause(x)
Definition diptreaty.h:51
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:828
@ DIPLO_NO_MIXED
Definition fc_types.h:1054
@ DIPLO_FOR_TEAMS
Definition fc_types.h:1055
@ DIPLO_NO_AIS
Definition fc_types.h:1053
@ DIPLO_FOR_HUMANS
Definition fc_types.h:1051
@ DIPLO_FOR_ALL
Definition fc_types.h:1050
@ DIPLO_FOR_AIS
Definition fc_types.h:1052
@ DIPLO_DISABLED
Definition fc_types.h:1056
@ RPT_POSSIBLE
Definition fc_types.h:700
struct civ_game game
Definition game.c:62
GType type
Definition repodlgs.c:1313
#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: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:1476
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:230
#define is_human(plr)
Definition player.h:229
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)
struct player * plr0
Definition diptreaty.h:79
bool accept0
Definition diptreaty.h:80
bool accept1
Definition diptreaty.h:80
struct clause_list * clauses
Definition diptreaty.h:81
struct player * plr1
Definition diptreaty.h:79
struct packet_game_info info
Definition game.h:89
struct requirement_vector receiver_reqs
Definition diptreaty.h:59
struct requirement_vector giver_reqs
Definition diptreaty.h:58
enum clause_type type
Definition diptreaty.h:56
bool enabled
Definition diptreaty.h:57
enum diplomacy_mode diplomacy
enum diplstate_type type
Definition player.h:197
struct team * team
Definition player.h:259
bool is_alive
Definition player.h:266
#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