]> git.pond.sub.org Git - empserver/blob - src/lib/common/nat.c
f539b2d26cca41bb5771e3b0fd86b5c4d9651119
[empserver] / src / lib / common / nat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  nat.c: Misc. accesses on the nation file
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Ron Koenderink, 2005
32  *     Markus Armbruster, 2006-2011
33  */
34
35 #include <config.h>
36
37 #include <fcntl.h>
38 #include <string.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include "file.h"
42 #include "misc.h"
43 #include "nat.h"
44 #include "optlist.h"
45 #include "sect.h"
46 #include "tel.h"
47
48 char *relates[] = {
49     /* must follow nation relation defines in nat.h */
50     "At War", "Hostile", "Neutral", "Friendly", "Allied"
51 };
52
53 char *
54 cname(natid n)
55 {
56     struct natstr *np;
57
58     if (!(np = getnatp(n)))
59         return NULL;
60     return np->nat_cnam;
61 }
62
63 char *
64 relatename(struct natstr *np, natid other)
65 {
66     return relates[getrel(np, other)];
67 }
68
69 char *
70 rejectname(struct natstr *np, natid other)
71 {
72     static char *rejects[] = {
73         /* must follow reject flags defined in nat.h */
74         "  YES  YES  YES  YES",
75         "  NO   YES  YES  YES",
76         "  YES  NO   YES  YES",
77         "  NO   NO   YES  YES",
78         "  YES  YES  NO   YES",
79         "  NO   YES  NO   YES",
80         "  YES  NO   NO   YES",
81         "  NO   NO   NO   YES",
82         "  YES  YES  YES  NO ",
83         "  NO   YES  YES  NO ",
84         "  YES  NO   YES  NO ",
85         "  NO   NO   YES  NO ",
86         "  YES  YES  NO   NO ",
87         "  NO   YES  NO   NO ",
88         "  YES  NO   NO   NO ",
89         "  NO   NO   NO   NO "
90     };
91
92     return rejects[getrejects(other, np)];
93 }
94
95 char *
96 natstate(struct natstr *np)
97 {
98     static char *stnam[] = {
99         /* must match nat_status */
100         "FREE", "VISITOR", "VISITOR", "SANCTUARY", "ACTIVE", "DEITY"
101     };
102     return stnam[np->nat_stat];
103 }
104
105 /* This returns the relations that np has with them */
106 int
107 getrel(struct natstr *np, natid them)
108 {
109     return np->nat_relate[them];
110 }
111
112 /*
113  * Return relations US has with THEM.
114  * Countries are considered allied to themselves.
115  */
116 int
117 relations_with(natid us, natid them)
118 {
119     return us == them ? ALLIED : getrel(getnatp(us), them);
120 }
121
122 int
123 getrejects(natid them, struct natstr *np)
124 {
125     return np->nat_rejects[them];
126 }
127
128 void
129 agecontact(struct natstr *np)
130 {
131     int them;
132
133     for (them = 1; them < MAXNOC; ++them) {
134         if (them != np->nat_cnum && np->nat_contact[them]) {
135             --np->nat_contact[them];
136         }
137     }
138 }
139
140 int
141 getcontact(struct natstr *np, natid them)
142 {
143     return np->nat_contact[them];
144 }
145
146 void
147 putrel(struct natstr *np, natid them, int relate)
148 {
149     np->nat_relate[them] = relate;
150 }
151
152 void
153 putreject(struct natstr *np, natid them, int how, int what)
154 {
155     if (how)
156         np->nat_rejects[them] |= what;
157     else
158         np->nat_rejects[them] &= ~what;
159 }
160
161 void
162 putcontact(struct natstr *np, natid them, int contact)
163 {
164     if (CANT_HAPPEN(contact < 0))
165         contact = 0;
166     if (CANT_HAPPEN(contact > 255))
167         contact = 255;
168
169     if (np->nat_contact[them] < contact)
170         np->nat_contact[them] = contact;
171 }
172
173 int
174 influx(struct natstr *np)
175 {
176     struct sctstr sect;
177
178     getsect(np->nat_xcap, np->nat_ycap, &sect);
179     if (sect.sct_own != np->nat_cnum ||
180         (sect.sct_type != SCT_CAPIT && sect.sct_type != SCT_MOUNT &&
181          sect.sct_type != SCT_SANCT) ||
182         (np->nat_flags & NF_SACKED))
183         return 1;
184     else
185         return 0;
186 }
187
188 /*
189  * Initialize NATP for country #CNUM in status STAT.
190  * STAT must be STAT_UNUSED, STAT_NEW, STAT_VIS or STAT_GOD.
191  * Also wipe realms and telegrams.
192  */
193 struct natstr *
194 nat_reset(struct natstr *natp, natid cnum, enum nat_status stat)
195 {
196     struct realmstr newrealm;
197     char buf[1024];
198     int i;
199
200     ef_blank(EF_NATION, cnum, natp);
201     natp->nat_stat = stat;
202     for (i = 0; i < MAXNOR; i++) {
203         ef_blank(EF_REALM, i + cnum * MAXNOR, &newrealm);
204         putrealm(&newrealm);
205     }
206     close(creat(mailbox(buf, cnum),
207                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
208     /* FIXME natp->nat_ann = #annos */
209     natp->nat_level[NAT_HLEV] = start_happiness;
210     natp->nat_level[NAT_RLEV] = start_research;
211     natp->nat_level[NAT_TLEV] = start_technology;
212     natp->nat_level[NAT_ELEV] = start_education;
213     for (i = 0; i < MAXNOC; i++)
214         natp->nat_relate[i] = NEUTRAL;
215     natp->nat_flags =
216         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
217     return natp;
218 }