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