]> git.pond.sub.org Git - empserver/blob - src/lib/commands/add.c
(add, disa, ef_open, logerror, main): Switch permissions to
[empserver] / src / lib / commands / add.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  *  add.c: Add a new country to the game
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  */
33
34 #include <config.h>
35
36 #include <fcntl.h>
37 #if defined(_WIN32)
38 #include <io.h>
39 #endif
40 #include <sys/stat.h>
41
42 #include "commands.h"
43 #include "land.h"
44 #include "optlist.h"
45 #include "plague.h"
46 #include "tel.h"
47
48 int
49 add(void)
50 {
51     struct natstr *natp;
52     struct sctstr sect;
53     struct nstr_sect nstr;
54     int i;
55     char cntryname[sizeof(natp->nat_cnam)];
56     char pname[sizeof(natp->nat_pnam)];
57     natid coun;
58     natid freecn;
59     char prompt[128];
60     char buf[1024];
61     char *p;
62     int stat;
63     struct nstr_item ni;
64     struct lndstr land;
65     struct realmstr realm;
66     time_t current_time = time(NULL);
67
68     for (freecn = 0; NULL != (natp = getnatp(freecn)); freecn++) {
69         if (natp->nat_stat == STAT_UNUSED)
70             break;
71     }
72     if (freecn < MAXNOC)
73         sprintf(prompt, "New country number? (%d is unused) ", freecn);
74     else
75         strcpy(prompt, "New country number? (they all seem to be used) ");
76     p = getstarg(player->argp[1], prompt, buf);
77     if (p == 0 || *p == 0)
78         return RET_SYN;
79     i = atoi(p);
80     if (i >= MAXNOC) {
81         pr("Max # countries is %d\n", MAXNOC);
82         return RET_FAIL;
83     }
84     coun = i;
85     if (coun == 0) {
86         pr("Not allowed to add country #0\n");
87         return RET_FAIL;
88     }
89     natp = getnatp(coun);
90     p = getstarg(player->argp[2], "Country Name? ", buf);
91     if (p == 0 || *p == 0)
92         return RET_SYN;
93     if (strlen(p) >= sizeof(cntryname)) {
94         pr("Country name too long\n");
95         return RET_FAIL;
96     }
97     strcpy(cntryname, p);
98     p = getstarg(player->argp[3], "Representative? ", buf);
99     if (p == 0 || *p == 0)
100         return RET_SYN;
101     if (strlen(p) >= sizeof(pname)) {
102         pr("Representative too long\n");
103         return RET_FAIL;
104     }
105     strcpy(pname, p);
106     p = getstarg(player->argp[4],
107                  "Status? (visitor, new, active, god, delete) ", buf);
108     if (p == 0 || *p == 0)
109         return RET_SYN;
110     switch (*p) {
111     case 'v':
112         stat = STAT_VIS;
113         break;
114     case 'n':
115         stat = STAT_NEW;
116         break;
117     case 'a':
118         stat = STAT_ACTIVE;
119         break;
120     case 'g':
121         stat = STAT_GOD;
122         break;
123     case 'd':
124         stat = STAT_UNUSED;
125         break;
126     default:
127         pr("Illegal status\n");
128         return RET_SYN;
129     }
130     p = getstarg(player->argp[5],
131                  "Check, wipe, or ignore existing sectors (c|w|i) ", buf);
132     if (p == 0)
133         return RET_SYN;
134     snxtitem_all(&ni, EF_LAND);
135     while (nxtitem(&ni, &land)) {
136         if (land.lnd_own == coun) {
137             land.lnd_effic = 0;
138             pr("Land unit %d wiped\n", land.lnd_uid);
139             putland(land.lnd_uid, &land);
140         }
141     }
142     natp->nat_stat = stat;
143     strcpy(natp->nat_cnam, cntryname);
144     strcpy(natp->nat_pnam, pname);
145     if (*p != 'w' && *p != 'c') {
146         pr("Any existing sectors ignored\n");
147     } else {
148         pr("Checking sectors...\n");
149         snxtsct_all(&nstr);
150         while (nxtsct(&nstr, &sect)) {
151             if (sect.sct_own != coun)
152                 continue;
153             pr("%s ", xyas(nstr.x, nstr.y, player->cnum));
154             if (*p == 'w') {
155                 sect.sct_mobil = 0;
156                 sect.sct_effic = 0;
157                 sect.sct_road = 0;
158                 sect.sct_rail = 0;
159                 sect.sct_defense = 0;
160                 sect.sct_own = 0;
161                 sect.sct_oldown = 0;
162                 if (sect.sct_type == SCT_BSPAN ||
163                     sect.sct_type == SCT_BTOWER)
164                     sect.sct_newtype = sect.sct_type = SCT_WATER;
165                 else if (sect.sct_type != SCT_MOUNT &&
166                     sect.sct_type != SCT_PLAINS)
167                     sect.sct_newtype = sect.sct_type = SCT_RURAL;
168                 /* No dist path */
169                 sect.sct_dist_x = sect.sct_x;
170                 sect.sct_dist_y = sect.sct_y;
171                 memset(sect.sct_item, 0, sizeof(sect.sct_item));
172                 memset(sect.sct_del, 0, sizeof(sect.sct_del));
173                 memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
174                 sect.sct_mines = 0;
175                 sect.sct_pstage = PLG_HEALTHY;
176                 sect.sct_ptime = 0;
177                 sect.sct_che = 0;
178                 sect.sct_che_target = 0;
179                 sect.sct_fallout = 0;
180                 putsect(&sect);
181                 pr("wiped\n");
182             } else {
183                 pr("\n");
184             }
185         }
186     }
187
188     if (natp->nat_stat == STAT_NEW || natp->nat_stat == STAT_VIS) {
189         *natp->nat_hostaddr = '\0';
190         *natp->nat_hostname = '\0';
191         *natp->nat_userid = '\0';
192         natp->nat_btu = 0;
193         natp->nat_reserve = 0;
194         natp->nat_tgms = 0;
195         natp->nat_ycap = 0;
196         natp->nat_xcap = 0;
197         natp->nat_yorg = 0;
198         natp->nat_xorg = 0;
199         natp->nat_dayno = 0;
200         natp->nat_minused = 0;
201         for (i = 0; i < MAXNOR; i++) {
202             getrealm(i, coun, &realm);
203             realm.r_xl = realm.r_xh = realm.r_yl = realm.r_yh = 0;
204             realm.r_timestamp = current_time;
205             putrealm(&realm);
206         }
207         natp->nat_last_login = natp->nat_last_login = 0;
208         natp->nat_money = 0;
209         natp->nat_level[NAT_TLEV] = start_technology;
210         natp->nat_level[NAT_RLEV] = start_research;
211         natp->nat_level[NAT_ELEV] = start_education;
212         natp->nat_level[NAT_HLEV] = start_happiness;
213         for (i = 0; i < MAXNOC; i++)
214             natp->nat_rejects[i] = 0;
215         natp->nat_newstim = 0;
216         natp->nat_annotim = 0;
217         (void)creat(mailbox(buf, coun), S_IRWUG);
218     } else
219         pr("No special initializations done...\n");
220
221     natp->nat_flags =
222         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
223     for (i = 0; i < MAXNOC; i++)
224         natp->nat_relate[i] = NEUTRAL;
225     putnat(natp);
226     return 0;
227 }