]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trechk.c
Update copyright notice
[empserver] / src / lib / subs / trechk.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  trechk.c: Check to see if an actor has violated a treaty.
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 1998-1999
31  */
32
33 /*
34  * Check to see if an actor has a treaty forbidding a given action,
35  * and if so, see if the actor wishes to break the treaty.
36  * All applicable treaties are checked.  All treaties must be broken
37  * if the action is to be allowed.  Propsed treaties are warned about,
38  * but no mention of broken pending treaties are made in the news.
39  */
40
41 #include <config.h>
42
43 #include "file.h"
44 #include "nat.h"
45 #include "news.h"
46 #include "optlist.h"
47 #include "prototypes.h"
48 #include "treaty.h"
49
50 int
51 trechk(natid actor, natid victim, int provision)
52 {
53     natid cn;
54     char buf[1024];
55     int news_verb;
56     int involved[MAXNOC];
57     struct trtstr treaty;
58     time_t now;
59     int conditions;
60     natid other;
61     int broken;
62     int applied;
63     struct nstr_item nstr;
64
65     if (!opt_TREATIES)
66         return 1;
67     (void)time(&now);
68     broken = 0;
69     applied = 0;
70     for (cn = 0; cn < MAXNOC; cn++)
71         involved[cn] = 0;
72     snxtitem_all(&nstr, EF_TREATY);
73     while (nxtitem(&nstr, &treaty)) {
74         if (treaty.trt_status == TS_FREE)
75             continue;
76         if (treaty.trt_exp < now)
77             continue;
78         if (actor == treaty.trt_cna) {
79             conditions = treaty.trt_acond;
80             other = treaty.trt_cnb;
81         } else if (actor == treaty.trt_cnb) {
82             conditions = treaty.trt_bcond;
83             other = treaty.trt_cna;
84         } else
85             continue;
86         if ((conditions & provision) == 0)
87             continue;
88         if (victim != other) {
89             switch (provision) {
90                 /* These are violations no matter who the victim is */
91             case NEWSHP:
92             case NEWLND:
93             case NEWNUK:
94             case NEWPLN:
95             case TRTENL:
96                 break;
97             default:
98                 /* The rest are only violations against the victim */
99                 continue;
100             }
101         }
102         /* treaty applies to actor */
103         applied++;
104         pr("This action is in contravention of ");
105         if (treaty.trt_status == TS_PROPOSED)
106             pr("pending ");
107         pr(" treaty #%d (with %s)\n", nstr.cur, cname(other));
108         getstring("Do you wish to go ahead anyway? [yn] ", buf);
109         if (*buf == 'n' || *buf == 'N')
110             broken = 0;
111         else
112             broken = 1;
113         if (treaty.trt_status == TS_SIGNED)
114             involved[other]++;
115     }
116     if (applied > 0) {
117         news_verb = N_HONOR_TRE;
118         if (broken > 0)
119             news_verb = N_VIOL_TRE;
120         for (cn = 0; cn < MAXNOC; cn++)
121             if (involved[cn] > 0)
122                 nreport(actor, news_verb, cn, 1);
123     }
124     if (applied && !broken) {
125         /*
126          * if any treaty applied, and none were broken
127          * the intended action is NOT performed.
128          */
129         return 0;
130     }
131     return 1;
132 }