]> git.pond.sub.org Git - empserver/blob - src/lib/common/nat.c
reject: New enum rej_comm for REJ_TELE & friends
[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-2016
33  */
34
35 #include <config.h>
36
37 #include <string.h>
38 #include "misc.h"
39 #include "nat.h"
40 #include "optlist.h"
41 #include "sect.h"
42 #include "tel.h"
43
44 char *relates[] = {
45     /* must follow nation relation defines in nat.h */
46     "At War", "Hostile", "Neutral", "Friendly", "Allied"
47 };
48
49 char *
50 cname(natid n)
51 {
52     struct natstr *np;
53
54     if (!(np = getnatp(n)))
55         return NULL;
56     return np->nat_cnam;
57 }
58
59 char *
60 relatename(struct natstr *np, natid other)
61 {
62     return relates[getrel(np, other)];
63 }
64
65 char *
66 natstate(struct natstr *np)
67 {
68     static char *stnam[] = {
69         /* must match nat_status */
70         "FREE", "VISITOR", "VISITOR", "SANCTUARY", "ACTIVE", "DEITY"
71     };
72     return stnam[np->nat_stat];
73 }
74
75 /* This returns the relations that np has with them */
76 int
77 getrel(struct natstr *np, natid them)
78 {
79     return np->nat_relate[them];
80 }
81
82 /*
83  * Return relations @us has with @them.
84  * Countries are considered allied to themselves.
85  */
86 int
87 relations_with(natid us, natid them)
88 {
89     return us == them ? ALLIED : getrel(getnatp(us), them);
90 }
91
92 int
93 nat_accepts(struct natstr *np, natid them, enum rej_comm what)
94 {
95     return getnatp(them)->nat_stat == STAT_GOD
96         || !(np->nat_rejects[them] & bit(what));
97 }
98
99 void
100 agecontact(struct natstr *np)
101 {
102     struct contactstr con;
103     int them;
104
105     getcontact(np->nat_cnum, &con);
106
107     for (them = 1; them < MAXNOC; ++them) {
108         if (them != np->nat_cnum && con.con_contact[them])
109             con.con_contact[them]--;
110     }
111 }
112
113 int
114 in_contact(natid us, natid them)
115 {
116     return getcontactp(us)->con_contact[them];
117 }
118
119 void
120 putrel(struct natstr *np, natid them, int relate)
121 {
122     np->nat_relate[them] = relate;
123 }
124
125 int
126 influx(struct natstr *np)
127 {
128     struct sctstr sect;
129
130     getsect(np->nat_xcap, np->nat_ycap, &sect);
131     if (sect.sct_own != np->nat_cnum ||
132         (sect.sct_type != SCT_CAPIT && sect.sct_type != SCT_MOUNT &&
133          sect.sct_type != SCT_SANCT) ||
134         (np->nat_flags & NF_SACKED))
135         return 1;
136     else
137         return 0;
138 }
139
140 /*
141  * Initialize @natp for country #@cnum in status @stat.
142  * @stat must be STAT_UNUSED, STAT_NEW, STAT_VIS or STAT_GOD.
143  * Also wipe realms and telegrams.
144  */
145 struct natstr *
146 nat_reset(struct natstr *natp, natid cnum, char *name, char *rep,
147           enum nat_status stat)
148 {
149     struct realmstr newrealm;
150     char buf[1024];
151     int i;
152
153     ef_blank(EF_NATION, cnum, natp);
154     natp->nat_stat = stat;
155     strncpy(natp->nat_cnam, name, sizeof(natp->nat_cnam) - 1);
156     strncpy(natp->nat_pnam, rep, sizeof(natp->nat_pnam) - 1);
157     if (stat == STAT_GOD)
158         natp->nat_money = 123456789;
159     for (i = 0; i < MAXNOR; i++) {
160         ef_blank(EF_REALM, i + cnum * MAXNOR, &newrealm);
161         putrealm(&newrealm);
162     }
163     mailbox_create(mailbox(buf, cnum));
164     /* FIXME natp->nat_ann = #annos */
165     natp->nat_level[NAT_HLEV] = start_happiness;
166     natp->nat_level[NAT_RLEV] = start_research;
167     natp->nat_level[NAT_TLEV] = start_technology;
168     natp->nat_level[NAT_ELEV] = start_education;
169     for (i = 0; i < MAXNOC; i++)
170         natp->nat_relate[i] = NEUTRAL;
171     natp->nat_flags =
172         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
173     return natp;
174 }