]> git.pond.sub.org Git - empserver/blob - src/lib/common/nat.c
666b4103a53b473797f4f66cc519acab069676da
[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 <string.h>
38 #include "file.h"
39 #include "misc.h"
40 #include "nat.h"
41 #include "optlist.h"
42 #include "sect.h"
43 #include "tel.h"
44
45 char *relates[] = {
46     /* must follow nation relation defines in nat.h */
47     "At War", "Hostile", "Neutral", "Friendly", "Allied"
48 };
49
50 char *
51 cname(natid n)
52 {
53     struct natstr *np;
54
55     if (!(np = getnatp(n)))
56         return NULL;
57     return np->nat_cnam;
58 }
59
60 char *
61 relatename(struct natstr *np, natid other)
62 {
63     return relates[getrel(np, other)];
64 }
65
66 char *
67 rejectname(struct natstr *np, natid other)
68 {
69     static char *rejects[] = {
70         /* must follow reject flags defined in nat.h */
71         "  YES  YES  YES  YES",
72         "  NO   YES  YES  YES",
73         "  YES  NO   YES  YES",
74         "  NO   NO   YES  YES",
75         "  YES  YES  NO   YES",
76         "  NO   YES  NO   YES",
77         "  YES  NO   NO   YES",
78         "  NO   NO   NO   YES",
79         "  YES  YES  YES  NO ",
80         "  NO   YES  YES  NO ",
81         "  YES  NO   YES  NO ",
82         "  NO   NO   YES  NO ",
83         "  YES  YES  NO   NO ",
84         "  NO   YES  NO   NO ",
85         "  YES  NO   NO   NO ",
86         "  NO   NO   NO   NO "
87     };
88
89     return rejects[getrejects(other, np)];
90 }
91
92 char *
93 natstate(struct natstr *np)
94 {
95     static char *stnam[] = {
96         /* must match nat_status */
97         "FREE", "VISITOR", "VISITOR", "SANCTUARY", "ACTIVE", "DEITY"
98     };
99     return stnam[np->nat_stat];
100 }
101
102 /* This returns the relations that np has with them */
103 int
104 getrel(struct natstr *np, natid them)
105 {
106     return np->nat_relate[them];
107 }
108
109 /*
110  * Return relations US has with THEM.
111  * Countries are considered allied to themselves.
112  */
113 int
114 relations_with(natid us, natid them)
115 {
116     return us == them ? ALLIED : getrel(getnatp(us), them);
117 }
118
119 int
120 getrejects(natid them, struct natstr *np)
121 {
122     return np->nat_rejects[them];
123 }
124
125 void
126 agecontact(struct natstr *np)
127 {
128     int them;
129
130     for (them = 1; them < MAXNOC; ++them) {
131         if (them != np->nat_cnum && np->nat_contact[them]) {
132             --np->nat_contact[them];
133         }
134     }
135 }
136
137 int
138 getcontact(struct natstr *np, natid them)
139 {
140     return np->nat_contact[them];
141 }
142
143 void
144 putrel(struct natstr *np, natid them, int relate)
145 {
146     np->nat_relate[them] = relate;
147 }
148
149 void
150 putreject(struct natstr *np, natid them, int how, int what)
151 {
152     if (how)
153         np->nat_rejects[them] |= what;
154     else
155         np->nat_rejects[them] &= ~what;
156 }
157
158 void
159 putcontact(struct natstr *np, natid them, int contact)
160 {
161     if (CANT_HAPPEN(contact < 0))
162         contact = 0;
163     if (CANT_HAPPEN(contact > 255))
164         contact = 255;
165
166     if (np->nat_contact[them] < contact)
167         np->nat_contact[them] = contact;
168 }
169
170 int
171 influx(struct natstr *np)
172 {
173     struct sctstr sect;
174
175     getsect(np->nat_xcap, np->nat_ycap, &sect);
176     if (sect.sct_own != np->nat_cnum ||
177         (sect.sct_type != SCT_CAPIT && sect.sct_type != SCT_MOUNT &&
178          sect.sct_type != SCT_SANCT) ||
179         (np->nat_flags & NF_SACKED))
180         return 1;
181     else
182         return 0;
183 }
184
185 /*
186  * Initialize NATP for country #CNUM in status STAT.
187  * STAT must be STAT_UNUSED, STAT_NEW, STAT_VIS or STAT_GOD.
188  * Also wipe realms and telegrams.
189  */
190 struct natstr *
191 nat_reset(struct natstr *natp, natid cnum, char *name, char *rep,
192           enum nat_status stat)
193 {
194     struct realmstr newrealm;
195     char buf[1024];
196     int i;
197
198     ef_blank(EF_NATION, cnum, natp);
199     natp->nat_stat = stat;
200     strncpy(natp->nat_cnam, name, sizeof(natp->nat_cnam) - 1);
201     strncpy(natp->nat_pnam, rep, sizeof(natp->nat_pnam) - 1);
202     if (stat == STAT_GOD)
203         natp->nat_money = 123456789;
204     for (i = 0; i < MAXNOR; i++) {
205         ef_blank(EF_REALM, i + cnum * MAXNOR, &newrealm);
206         putrealm(&newrealm);
207     }
208     mailbox_create(mailbox(buf, cnum));
209     /* FIXME natp->nat_ann = #annos */
210     natp->nat_level[NAT_HLEV] = start_happiness;
211     natp->nat_level[NAT_RLEV] = start_research;
212     natp->nat_level[NAT_TLEV] = start_technology;
213     natp->nat_level[NAT_ELEV] = start_education;
214     for (i = 0; i < MAXNOC; i++)
215         natp->nat_relate[i] = NEUTRAL;
216     natp->nat_flags =
217         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
218     return natp;
219 }