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