]> git.pond.sub.org Git - empserver/blob - src/lib/common/nat.c
(coun_list, repo_list, nati): Replace in-flux determination code with influx().
[empserver] / src / lib / common / nat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "prototypes.h"
35 #include "misc.h"
36 #include "nat.h"
37 #include "file.h"
38 #include "optlist.h"
39
40 s_char *relates[] = {
41     /* must follow nation relation defines in nat.h */
42     "At War", "Sitzkrieg", "Mobilizing", "Hostile", "Neutral", "Friendly",
43     "Allied"
44 };
45
46 s_char *
47 cname(natid n)
48 {
49     struct natstr *np;
50
51     if ((np = getnatp(n)) == 0)
52         return 0;
53     return np->nat_cnam;
54 }
55
56 s_char *
57 relatename(struct natstr *np, natid other)
58 {
59     return relates[getrel(np, other)];
60 }
61
62 s_char *
63 rejectname(struct natstr *np, natid other)
64 {
65     s_char *rejects[] = {
66         /* must follow reject flags defined in nat.h */
67         "  YES  YES  YES  YES",
68         "  NO   YES  YES  YES",
69         "  YES  NO   YES  YES",
70         "  NO   NO   YES  YES",
71         "  YES  YES  NO   YES",
72         "  NO   YES  NO   YES",
73         "  YES  NO   NO   YES",
74         "  NO   NO   NO   YES",
75         "  YES  YES  YES  NO ",
76         "  NO   YES  YES  NO ",
77         "  YES  NO   YES  NO ",
78         "  NO   NO   YES  NO ",
79         "  YES  YES  NO   NO ",
80         "  NO   YES  NO   NO ",
81         "  YES  NO   NO   NO ",
82         "  NO   NO   NO   NO "
83     };
84
85     return rejects[getrejects(other, np)];
86 }
87
88 s_char *
89 natstate(struct natstr *np)
90 {
91     if ((np->nat_stat & STAT_INUSE) == 0)
92         return "FREE";
93     if (np->nat_stat & STAT_GOD)
94         return "DEITY";
95     if ((np->nat_stat & STAT_NORM) == 0)
96         return "VISITOR";
97     return "ACTIVE";
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 &&\r
182          sect.sct_type != SCT_SANCT) ||
183         (np->nat_flags & NF_SACKED))
184         return 1;
185     else
186         return 0;
187 }