Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/diptreaty.c Source File

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
28 static struct clause_info clause_infos[CLAUSE_COUNT];
29
30/**********************************************************************/
33 bool 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;
39 case DIPLO_FOR_HUMANS:
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/**********************************************************************/
61 bool 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/**********************************************************************/
81 bool 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/**********************************************************************/
96 void 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/**********************************************************************/
109 void clear_treaty(struct Treaty *ptreaty)
110{
111 clause_list_iterate(ptreaty->clauses, pclause) {
112 free(pclause);
113 } clause_list_iterate_end;
114 clause_list_destroy(ptreaty->clauses);
115}
116
117/**********************************************************************/
120 bool 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 }
134 } clause_list_iterate_end;
135
136 return FALSE;
137}
138
139/**********************************************************************/
142 bool 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
162 if (is_pact_clause(type)
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.",
170 nation_rule_name(nation_of_player(ptreaty->plr0)),
171 nation_rule_name(nation_of_player(ptreaty->plr1)));
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.",
178 nation_rule_name(nation_of_player(pto)),
179 nation_rule_name(nation_of_player(pfrom)));
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 },
191 pto, &clause_infos[type].giver_reqs, RPT_POSSIBLE)) {
192 return FALSE;
193 }
194 }
195 if (client_player == NULL || client_player == pto) {
196 if (!are_reqs_active(&(const struct req_context) { .player = pto },
197 pfrom, &clause_infos[type].receiver_reqs,
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 }
210 if (is_pact_clause(type)
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 }
226 } clause_list_iterate_end;
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/**********************************************************************/
245 void clause_infos_init(void)
246{
247 int i;
248
249 for (i = 0; i < CLAUSE_COUNT; i++) {
250 clause_infos[i].type = i;
251 clause_infos[i].enabled = FALSE;
252 requirement_vector_init(&(clause_infos[i].giver_reqs));
253 requirement_vector_init(&(clause_infos[i].receiver_reqs));
254 }
255}
256
257/**********************************************************************/
260 void clause_infos_free(void)
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/**********************************************************************/
273 struct 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/**********************************************************************/
286 bool 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}
client_player
#define client_player()
Definition client_main.h:106
diplomacy_possible
bool diplomacy_possible(const struct player *pplayer1, const struct player *pplayer2)
Definition diptreaty.c:33
clause_infos_init
void clause_infos_init(void)
Definition diptreaty.c:245
clause_infos_free
void clause_infos_free(void)
Definition diptreaty.c:260
clear_treaty
void clear_treaty(struct Treaty *ptreaty)
Definition diptreaty.c:109
init_treaty
void init_treaty(struct Treaty *ptreaty, struct player *plr0, struct player *plr1)
Definition diptreaty.c:96
add_clause
bool add_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val, struct player *client_player)
Definition diptreaty.c:142
clause_enabled
bool clause_enabled(enum clause_type type)
Definition diptreaty.c:286
clause_infos
static struct clause_info clause_infos[CLAUSE_COUNT]
Definition diptreaty.c:28
remove_clause
bool remove_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val)
Definition diptreaty.c:120
could_intel_with_player
bool could_intel_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:81
clause_info_get
struct clause_info * clause_info_get(enum clause_type type)
Definition diptreaty.c:273
could_meet_with_player
bool could_meet_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:61
diptreaty.h
clause_list_iterate_end
#define clause_list_iterate_end
Definition diptreaty.h:68
clause_list_iterate
#define clause_list_iterate(clauselist, pclause)
Definition diptreaty.h:66
is_pact_clause
#define is_pact_clause(x)
Definition diptreaty.h:49
get_player_bonus
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:771
DIPLO_NO_MIXED
@ DIPLO_NO_MIXED
Definition fc_types.h:908
DIPLO_FOR_TEAMS
@ DIPLO_FOR_TEAMS
Definition fc_types.h:909
DIPLO_NO_AIS
@ DIPLO_NO_AIS
Definition fc_types.h:907
DIPLO_FOR_HUMANS
@ DIPLO_FOR_HUMANS
Definition fc_types.h:905
DIPLO_FOR_ALL
@ DIPLO_FOR_ALL
Definition fc_types.h:904
DIPLO_FOR_AIS
@ DIPLO_FOR_AIS
Definition fc_types.h:906
DIPLO_DISABLED
@ DIPLO_DISABLED
Definition fc_types.h:910
RPT_POSSIBLE
@ RPT_POSSIBLE
Definition fc_types.h:585
game
struct civ_game game
Definition game.c:57
game.h
type
GType type
Definition repodlgs.c:1312
log.h
fc_assert
#define fc_assert(condition)
Definition log.h:176
log_error
#define log_error(message,...)
Definition log.h:103
mem.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
nation_rule_name
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:137
nation_of_player
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:443
players_on_same_team
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1452
player_has_real_embassy
bool player_has_real_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:234
team_has_embassy
bool team_has_embassy(const struct team *pteam, const struct player *tgt_player)
Definition player.c:214
player_diplstate_get
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:317
player_has_embassy
bool player_has_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:202
player.h
is_ai
#define is_ai(plr)
Definition player.h:234
is_human
#define is_human(plr)
Definition player.h:233
are_reqs_active
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)
Definition requirements.c:4361
Clause
Definition diptreaty.h:70
Clause::type
enum clause_type type
Definition diptreaty.h:71
Clause::from
struct player * from
Definition diptreaty.h:72
Clause::value
int value
Definition diptreaty.h:73
Treaty
Definition diptreaty.h:76
Treaty::plr0
struct player * plr0
Definition diptreaty.h:77
Treaty::accept0
bool accept0
Definition diptreaty.h:78
Treaty::accept1
bool accept1
Definition diptreaty.h:78
Treaty::clauses
struct clause_list * clauses
Definition diptreaty.h:79
Treaty::plr1
struct player * plr1
Definition diptreaty.h:77
civ_game::info
struct packet_game_info info
Definition game.h:89
clause_info
Definition diptreaty.h:53
clause_info::receiver_reqs
struct requirement_vector receiver_reqs
Definition diptreaty.h:57
clause_info::giver_reqs
struct requirement_vector giver_reqs
Definition diptreaty.h:56
clause_info::type
enum clause_type type
Definition diptreaty.h:54
clause_info::enabled
bool enabled
Definition diptreaty.h:55
packet_game_info::trading_city
bool trading_city
Definition packets_gen.h:238
packet_game_info::diplomacy
enum diplomacy_mode diplomacy
Definition packets_gen.h:151
packet_game_info::trading_gold
bool trading_gold
Definition packets_gen.h:239
packet_game_info::trading_tech
bool trading_tech
Definition packets_gen.h:240
player_diplstate::type
enum diplstate_type type
Definition player.h:201
player
Definition player.h:249
player::team
struct team * team
Definition player.h:261
player::is_alive
bool is_alive
Definition player.h:268
req_context
Definition requirements.h:99
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
valid_advance_by_number
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:176