1 /*
2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, 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 *
cname(natid n)45 cname(natid n)
46 {
47 struct natstr *np;
48
49 if (!(np = getnatp(n)))
50 return NULL;
51 return np->nat_cnam;
52 }
53
54 char *
natstate(struct natstr * np)55 natstate(struct natstr *np)
56 {
57 static char *stnam[] = {
58 /* must match nat_status */
59 "FREE", "VISITOR", "VISITOR", "SANCTUARY", "ACTIVE", "DEITY"
60 };
61 return stnam[np->nat_stat];
62 }
63
64 /*
65 * Return relations @us has with @them.
66 * Countries are considered allied to themselves.
67 */
68 enum relations
relations_with(natid us,natid them)69 relations_with(natid us, natid them)
70 {
71 return us == them ? ALLIED : getrelatp(us)->rel_relate[them];
72 }
73
74 char *
relations_string(enum relations rel)75 relations_string(enum relations rel)
76 {
77 static char *relates[] = {
78 /* must match enum relations */
79 "At War", "Hostile", "Neutral", "Friendly", "Allied"
80 };
81
82 return relates[rel];
83 }
84
85 int
nat_accepts(natid us,natid them,enum rej_comm what)86 nat_accepts(natid us, natid them, enum rej_comm what)
87 {
88 return getnatp(them)->nat_stat == STAT_GOD
89 || !(getrejectp(us)->rej_rejects[them] & bit(what));
90 }
91
92 void
agecontact(struct natstr * np)93 agecontact(struct natstr *np)
94 {
95 struct contactstr con;
96 int them;
97
98 getcontact(np->nat_cnum, &con);
99
100 for (them = 1; them < MAXNOC; ++them) {
101 if (them != np->nat_cnum && con.con_contact[them])
102 con.con_contact[them]--;
103 }
104 }
105
106 int
in_contact(natid us,natid them)107 in_contact(natid us, natid them)
108 {
109 return getcontactp(us)->con_contact[them];
110 }
111
112 int
influx(struct natstr * np)113 influx(struct natstr *np)
114 {
115 struct sctstr sect;
116
117 getsect(np->nat_xcap, np->nat_ycap, §);
118 if (sect.sct_own != np->nat_cnum ||
119 (sect.sct_type != SCT_CAPIT && sect.sct_type != SCT_MOUNT &&
120 sect.sct_type != SCT_SANCT) ||
121 (np->nat_flags & NF_SACKED))
122 return 1;
123 else
124 return 0;
125 }
126
127 /*
128 * Initialize country #@cnum in status @stat.
129 * @stat must be STAT_UNUSED, STAT_NEW, STAT_VIS or STAT_GOD.
130 * Also wipe realms and telegrams.
131 */
132 void
nat_reset(natid cnum,char * name,char * rep,enum nat_status stat)133 nat_reset(natid cnum, char *name, char *rep, enum nat_status stat)
134 {
135 struct natstr nat;
136 struct relatstr relat;
137 struct realmstr newrealm;
138 char buf[1024];
139 int i;
140
141 ef_blank(EF_NATION, cnum, &nat);
142 ef_blank(EF_RELAT, cnum, &relat);
143 nat.nat_stat = stat;
144 strncpy(nat.nat_cnam, name, sizeof(nat.nat_cnam) - 1);
145 strncpy(nat.nat_pnam, rep, sizeof(nat.nat_pnam) - 1);
146 if (stat == STAT_GOD)
147 nat.nat_money = 123456789;
148 for (i = 0; i < MAXNOR; i++) {
149 ef_blank(EF_REALM, i + cnum * MAXNOR, &newrealm);
150 putrealm(&newrealm);
151 }
152 mailbox_create(mailbox(buf, cnum));
153 /* FIXME nat.nat_ann = #annos */
154 nat.nat_level[NAT_HLEV] = start_happiness;
155 nat.nat_level[NAT_RLEV] = start_research;
156 nat.nat_level[NAT_TLEV] = start_technology;
157 nat.nat_level[NAT_ELEV] = start_education;
158 for (i = 0; i < MAXNOC; i++)
159 relat.rel_relate[i] = NEUTRAL;
160 nat.nat_flags =
161 NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
162 putnat(&nat);
163 putrelat(&relat);
164 }
165