]> 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-2016, 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 natstate(struct natstr *np)
68 {
69     static char *stnam[] = {
70         /* must match nat_status */
71         "FREE", "VISITOR", "VISITOR", "SANCTUARY", "ACTIVE", "DEITY"
72     };
73     return stnam[np->nat_stat];
74 }
75
76 /* This returns the relations that np has with them */
77 int
78 getrel(struct natstr *np, natid them)
79 {
80     return np->nat_relate[them];
81 }
82
83 /*
84  * Return relations @us has with @them.
85  * Countries are considered allied to themselves.
86  */
87 int
88 relations_with(natid us, natid them)
89 {
90     return us == them ? ALLIED : getrel(getnatp(us), them);
91 }
92
93 int
94 getrejects(natid them, struct natstr *np)
95 {
96     return np->nat_rejects[them];
97 }
98
99 void
100 agecontact(struct natstr *np)
101 {
102     int them;
103
104     for (them = 1; them < MAXNOC; ++them) {
105         if (them != np->nat_cnum && np->nat_contact[them]) {
106             --np->nat_contact[them];
107         }
108     }
109 }
110
111 int
112 getcontact(struct natstr *np, natid them)
113 {
114     return np->nat_contact[them];
115 }
116
117 void
118 putrel(struct natstr *np, natid them, int relate)
119 {
120     np->nat_relate[them] = relate;
121 }
122
123 void
124 putreject(struct natstr *np, natid them, int how, int what)
125 {
126     if (how)
127         np->nat_rejects[them] |= what;
128     else
129         np->nat_rejects[them] &= ~what;
130 }
131
132 void
133 putcontact(struct natstr *np, natid them, int contact)
134 {
135     if (CANT_HAPPEN(contact < 0))
136         contact = 0;
137     if (CANT_HAPPEN(contact > 255))
138         contact = 255;
139
140     if (np->nat_contact[them] < contact)
141         np->nat_contact[them] = contact;
142 }
143
144 int
145 influx(struct natstr *np)
146 {
147     struct sctstr sect;
148
149     getsect(np->nat_xcap, np->nat_ycap, &sect);
150     if (sect.sct_own != np->nat_cnum ||
151         (sect.sct_type != SCT_CAPIT && sect.sct_type != SCT_MOUNT &&
152          sect.sct_type != SCT_SANCT) ||
153         (np->nat_flags & NF_SACKED))
154         return 1;
155     else
156         return 0;
157 }
158
159 /*
160  * Initialize @natp for country #@cnum in status @stat.
161  * @stat must be STAT_UNUSED, STAT_NEW, STAT_VIS or STAT_GOD.
162  * Also wipe realms and telegrams.
163  */
164 struct natstr *
165 nat_reset(struct natstr *natp, natid cnum, char *name, char *rep,
166           enum nat_status stat)
167 {
168     struct realmstr newrealm;
169     char buf[1024];
170     int i;
171
172     ef_blank(EF_NATION, cnum, natp);
173     natp->nat_stat = stat;
174     strncpy(natp->nat_cnam, name, sizeof(natp->nat_cnam) - 1);
175     strncpy(natp->nat_pnam, rep, sizeof(natp->nat_pnam) - 1);
176     if (stat == STAT_GOD)
177         natp->nat_money = 123456789;
178     for (i = 0; i < MAXNOR; i++) {
179         ef_blank(EF_REALM, i + cnum * MAXNOR, &newrealm);
180         putrealm(&newrealm);
181     }
182     mailbox_create(mailbox(buf, cnum));
183     /* FIXME natp->nat_ann = #annos */
184     natp->nat_level[NAT_HLEV] = start_happiness;
185     natp->nat_level[NAT_RLEV] = start_research;
186     natp->nat_level[NAT_TLEV] = start_technology;
187     natp->nat_level[NAT_ELEV] = start_education;
188     for (i = 0; i < MAXNOC; i++)
189         natp->nat_relate[i] = NEUTRAL;
190     natp->nat_flags =
191         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
192     return natp;
193 }