]> git.pond.sub.org Git - empserver/blob - src/lib/common/nat.c
df28e1d52c688f1e50807bb44d2ba2652ad93b67
[empserver] / src / lib / common / nat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  *  nat.c: Misc. accesses on the nation file
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Ron Koenderink, 2005
32  *     Markus Armbruster, 2006-2009
33  */
34
35 #include <config.h>
36
37 #include "file.h"
38 #include "misc.h"
39 #include "nat.h"
40 #include "sect.h"
41
42 char *relates[] = {
43     /* must follow nation relation defines in nat.h */
44     "At War", "Hostile", "Neutral", "Friendly", "Allied"
45 };
46
47 char *
48 cname(natid n)
49 {
50     struct natstr *np;
51
52     if (!(np = getnatp(n)))
53         return NULL;
54     return np->nat_cnam;
55 }
56
57 char *
58 relatename(struct natstr *np, natid other)
59 {
60     return relates[getrel(np, other)];
61 }
62
63 char *
64 rejectname(struct natstr *np, natid other)
65 {
66     static char *rejects[] = {
67         /* must follow reject flags defined in nat.h */
68         "  YES  YES  YES  YES",
69         "  NO   YES  YES  YES",
70         "  YES  NO   YES  YES",
71         "  NO   NO   YES  YES",
72         "  YES  YES  NO   YES",
73         "  NO   YES  NO   YES",
74         "  YES  NO   NO   YES",
75         "  NO   NO   NO   YES",
76         "  YES  YES  YES  NO ",
77         "  NO   YES  YES  NO ",
78         "  YES  NO   YES  NO ",
79         "  NO   NO   YES  NO ",
80         "  YES  YES  NO   NO ",
81         "  NO   YES  NO   NO ",
82         "  YES  NO   NO   NO ",
83         "  NO   NO   NO   NO "
84     };
85
86     return rejects[getrejects(other, np)];
87 }
88
89 char *
90 natstate(struct natstr *np)
91 {
92     static char *stnam[] = {
93         /* must match nat_status */
94         "FREE", "VISITOR", "VISITOR", "SANCTUARY", "ACTIVE", "DEITY"
95     };
96     return stnam[np->nat_stat];
97 }
98
99 /* This returns the relations that np has with them */
100 int
101 getrel(struct natstr *np, natid them)
102 {
103     return np->nat_relate[them];
104 }
105
106 /*
107  * Return relations US has with THEM.
108  * Countries are considered allied to themselves.
109  */
110 int
111 relations_with(natid us, natid them)
112 {
113     return us == them ? ALLIED : getrel(getnatp(us), them);
114 }
115
116 int
117 getrejects(natid them, struct natstr *np)
118 {
119     return np->nat_rejects[them];
120 }
121
122 void
123 agecontact(struct natstr *np)
124 {
125     int them;
126
127     for (them = 1; them < MAXNOC; ++them) {
128         if (them != np->nat_cnum && np->nat_contact[them]) {
129             --np->nat_contact[them];
130         }
131     }
132 }
133
134 int
135 getcontact(struct natstr *np, natid them)
136 {
137     return np->nat_contact[them];
138 }
139
140 void
141 putrel(struct natstr *np, natid them, int relate)
142 {
143     np->nat_relate[them] = relate;
144 }
145
146 void
147 putreject(struct natstr *np, natid them, int how, int what)
148 {
149     if (how)
150         np->nat_rejects[them] |= what;
151     else
152         np->nat_rejects[them] &= ~what;
153 }
154
155 void
156 putcontact(struct natstr *np, natid them, int contact)
157 {
158     if (CANT_HAPPEN(contact < 0))
159         contact = 0;
160     if (CANT_HAPPEN(contact > 255))
161         contact = 255;
162
163     if (np->nat_contact[them] < contact)
164         np->nat_contact[them] = contact;
165 }
166
167 int
168 influx(struct natstr *np)
169 {
170     struct sctstr sect;
171
172     getsect(np->nat_xcap, np->nat_ycap, &sect);
173     if (sect.sct_own != np->nat_cnum ||
174         (sect.sct_type != SCT_CAPIT && sect.sct_type != SCT_MOUNT &&
175          sect.sct_type != SCT_SANCT) ||
176         (np->nat_flags & NF_SACKED))
177         return 1;
178     else
179         return 0;
180 }