]> git.pond.sub.org Git - empserver/blob - src/lib/common/nat.c
Some s_char killings.
[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 <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     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     if ((np->nat_stat & STAT_INUSE) == 0)
94         return "FREE";
95     if (np->nat_stat & STAT_GOD)
96         return "DEITY";
97     if (np->nat_stat & STAT_SANCT)
98         return "SANCTUARY";
99     if ((np->nat_stat & STAT_NORM) == 0)
100         return "VISITOR";
101     return "ACTIVE";
102 }
103
104 /* This returns the relations that np has with them */
105 int
106 getrel(struct natstr *np, natid them)
107 {
108     return np->nat_relate[them];
109 }
110
111 int
112 getrejects(natid them, struct natstr *np)
113 {
114     int ind;
115     int shift;
116     int reject;
117
118     ind = them / 4;
119     shift = 12 - ((them - ((them / 4) << 2)) * 4);
120     reject = (np->nat_rejects[ind] >> shift) & 0x0f;
121     return reject;
122 }
123
124 void
125 agecontact(struct natstr *np)
126 {
127     int them;
128
129     for (them = 1; them < MAXNOC; ++them) {
130         if (them != np->nat_cnum && np->nat_contact[them]) {
131             --np->nat_contact[them];
132         }
133     }
134 }
135
136 int
137 getcontact(struct natstr *np, natid them)
138 {
139     return np->nat_contact[them];
140 }
141
142 void
143 putrel(struct natstr *np, natid them, int relate)
144 {
145     np->nat_relate[them] = relate;
146 }
147
148 void
149 putreject(struct natstr *np, natid them, int how, int what)
150 {
151     int shift;
152     int newrej;
153     int ind;
154
155     what &= 0x0f;
156     ind = them / 4;
157     shift = 12 - ((them - ((them / 4) << 2)) * 4);
158     newrej = np->nat_rejects[ind];
159     if (how)
160         newrej |= (what << shift);
161     else
162         newrej &= ~(what << shift);
163     np->nat_rejects[ind] = newrej;
164 }
165
166 void
167 putcontact(struct natstr *np, natid them, int contact)
168 {
169     if (CANT_HAPPEN(contact < 0))
170         contact = 0;
171     if (CANT_HAPPEN(contact > 255))
172         contact = 255;
173
174     if (np->nat_contact[them] < contact)
175         np->nat_contact[them] = contact;
176 }
177
178 int
179 influx(struct natstr *np)
180 {
181     struct sctstr sect;
182
183     getsect(np->nat_xcap, np->nat_ycap, &sect);
184     if (sect.sct_own != np->nat_cnum ||
185         (sect.sct_type != SCT_CAPIT && sect.sct_type != SCT_MOUNT &&
186          sect.sct_type != SCT_SANCT) ||
187         (np->nat_flags & NF_SACKED))
188         return 1;
189     else
190         return 0;
191 }