]> git.pond.sub.org Git - empserver/blob - src/lib/subs/natsub.c
Update copyright notice
[empserver] / src / lib / subs / natsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  nat.c: Nation subroutines
29  *
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2009
32  *     Ron Koenderink, 2008-2009
33  */
34
35 #include <config.h>
36
37 #include <string.h>
38 #include <ctype.h>
39 #include <fcntl.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include "file.h"
43 #include "game.h"
44 #include "nat.h"
45 #include "optlist.h"
46 #include "prototypes.h"
47 #include "tel.h"
48 #include "xy.h"
49
50 /*
51  * Reset NATP for status STAT with origin/capital at X,Y.
52  * Wipes everything but nat_cnum, nat_cnam and nat_pnam.
53  * Also wipes realms and telegrams.
54  */
55 struct natstr *
56 nat_reset(struct natstr *natp, enum nat_status stat, coord x, coord y)
57 {
58     static struct range defrealm = { -8, -5, 10, 5, 0, 0 };
59     struct realmstr newrealm;
60     struct range absrealm;
61     char buf[1024];
62     int i;
63
64     natp->nat_stat = stat;
65     *natp->nat_hostaddr = '\0';
66     *natp->nat_hostname = '\0';
67     *natp->nat_userid = '\0';
68
69     natp->nat_xcap = natp->nat_xorg = x;
70     natp->nat_ycap = natp->nat_yorg = y;
71     if (stat == STAT_SANCT)
72         xyabsrange(natp, &defrealm, &absrealm);
73     else
74         memset(&absrealm, 0, sizeof(absrealm));
75     for (i = 0; i < MAXNOR; i++) {
76         ef_blank(EF_REALM, i + natp->nat_cnum * MAXNOR, &newrealm);
77         newrealm.r_cnum = natp->nat_cnum;
78         newrealm.r_realm = i;
79         newrealm.r_xl = absrealm.lx;
80         newrealm.r_xh = absrealm.hx;
81         newrealm.r_yl = absrealm.ly;
82         newrealm.r_yh = absrealm.hy;
83         putrealm(&newrealm);
84     }
85     if (players_at_00) {
86         natp->nat_xorg = 0;
87         natp->nat_yorg = 0;
88     }
89
90     natp->nat_timeused = 0;
91     natp->nat_update = 0;
92
93     natp->nat_tgms = 0;
94     close(creat(mailbox(buf, natp->nat_cnum),
95                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
96     natp->nat_ann = 0;          /* FIXME number of annos */
97
98     natp->nat_btu = stat == STAT_SANCT ? max_btus : 0;
99     natp->nat_access = 0;
100     game_tick_to_now(&natp->nat_access);
101     natp->nat_reserve = 0;
102     natp->nat_money = stat == STAT_SANCT ? start_cash : 0;
103     natp->nat_last_login = natp->nat_last_logout = 0;
104     natp->nat_newstim = 0;
105     natp->nat_annotim = 0;
106     natp->nat_level[NAT_HLEV] = start_happiness;
107     natp->nat_level[NAT_RLEV] = start_research;
108     natp->nat_level[NAT_TLEV] = start_technology;
109     natp->nat_level[NAT_ELEV] = start_education;
110     for (i = 0; i < MAXNOC; i++)
111         natp->nat_relate[i] = NEUTRAL;
112     memset(natp->nat_contact, 0, sizeof(natp->nat_rejects));
113     memset(natp->nat_rejects, 0, sizeof(natp->nat_rejects));
114     natp->nat_flags =
115         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
116
117     return natp;
118 }
119
120 int
121 check_nat_name(char *cname)
122 {
123     struct natstr *natp;
124     natid cn;
125     int allblank;
126     char *p;
127
128     if (strlen(cname) >= sizeof(natp->nat_cnam)) {
129         pr("Country name too long\n");
130         return 0;
131     }
132
133     allblank = 1;
134     for (p = cname; *p != '\0'; p++) {
135         if (iscntrl(*p)) {
136             pr("No control characters allowed in country names!\n");
137             return 0;
138         } else if (!isspace(*p))
139             allblank = 0;
140     }
141     if (allblank) {
142         pr("Country name can't be all blank\n");
143         return 0;
144     }
145
146     for (cn = 0; NULL != (natp = getnatp(cn)); cn++) {
147         if (!strcmp(cname, natp->nat_cnam)) {
148             pr("Country #%d is already called `%s'\n", cn, cname);
149             return 0;
150         }
151     }
152     return 1;
153 }