]> git.pond.sub.org Git - empserver/blob - src/lib/common/nat.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / common / nat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  */
33
34 #include <config.h>
35
36 #include "prototypes.h"
37 #include "misc.h"
38 #include "nat.h"
39 #include "file.h"
40 #include "optlist.h"
41
42 char *relates[] = {
43     /* must follow nation relation defines in nat.h */
44     "At War", "Sitzkrieg", "Mobilizing", "Hostile", "Neutral", "Friendly",
45     "Allied"
46 };
47
48 char *
49 cname(natid n)
50 {
51     struct natstr *np;
52
53     if ((np = getnatp(n)) == 0)
54         return 0;
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 int
108 getrejects(natid them, struct natstr *np)
109 {
110     int ind;
111     int shift;
112     int reject;
113
114     ind = them / 4;
115     shift = 12 - ((them - ((them / 4) << 2)) * 4);
116     reject = (np->nat_rejects[ind] >> shift) & 0x0f;
117     return reject;
118 }
119
120 void
121 agecontact(struct natstr *np)
122 {
123     int them;
124
125     for (them = 1; them < MAXNOC; ++them) {
126         if (them != np->nat_cnum && np->nat_contact[them]) {
127             --np->nat_contact[them];
128         }
129     }
130 }
131
132 int
133 getcontact(struct natstr *np, natid them)
134 {
135     return np->nat_contact[them];
136 }
137
138 void
139 putrel(struct natstr *np, natid them, int relate)
140 {
141     np->nat_relate[them] = relate;
142 }
143
144 void
145 putreject(struct natstr *np, natid them, int how, int what)
146 {
147     int shift;
148     int newrej;
149     int ind;
150
151     what &= 0x0f;
152     ind = them / 4;
153     shift = 12 - ((them - ((them / 4) << 2)) * 4);
154     newrej = np->nat_rejects[ind];
155     if (how)
156         newrej |= (what << shift);
157     else
158         newrej &= ~(what << shift);
159     np->nat_rejects[ind] = newrej;
160 }
161
162 void
163 putcontact(struct natstr *np, natid them, int contact)
164 {
165     if (CANT_HAPPEN(contact < 0))
166         contact = 0;
167     if (CANT_HAPPEN(contact > 255))
168         contact = 255;
169
170     if (np->nat_contact[them] < contact)
171         np->nat_contact[them] = contact;
172 }
173
174 int
175 influx(struct natstr *np)
176 {
177     struct sctstr sect;
178
179     getsect(np->nat_xcap, np->nat_ycap, &sect);
180     if (sect.sct_own != np->nat_cnum ||
181         (sect.sct_type != SCT_CAPIT && sect.sct_type != SCT_MOUNT &&
182          sect.sct_type != SCT_SANCT) ||
183         (np->nat_flags & NF_SACKED))
184         return 1;
185     else
186         return 0;
187 }