]> git.pond.sub.org Git - empserver/blob - src/lib/commands/add.c
(add): When an argument is bad, do not loop prompting, just fail.
[empserver] / src / lib / commands / add.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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
35 #include <stdio.h>
36 #include <string.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "plague.h"
40 #include "sect.h"
41 #include "nat.h"
42 #include "xy.h"
43 #include "nsc.h"
44 #include "file.h"
45 #include "tel.h"
46 #include "land.h"
47 #include "commands.h"
48 #include "optlist.h"
49
50 int
51 add(void)
52 {
53     struct natstr *natp;
54     struct sctstr sect;
55     struct nstr_sect nstr;
56     int i;
57     char cntryname[sizeof(natp->nat_cnam)];
58     char pname[sizeof(natp->nat_pnam)];
59     natid coun;
60     natid freecn;
61     char prompt[128];
62     char buf[1024];
63     char *p;
64     int stat;
65     struct nstr_item ni;
66     struct lndstr land;
67
68     for (freecn = 0; NULL != (natp = getnatp(freecn)); freecn++) {
69         if ((natp->nat_stat & STAT_INUSE) == 0)
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_INUSE;
113         break;
114     case 'n':
115         stat = STAT_NEW | STAT_INUSE;
116         break;
117     case 'a':
118         stat = STAT_NORM | STAT_INUSE;
119         break;
120     case 'g':
121         stat = STAT_GOD | STAT_NORM | STAT_INUSE;
122         break;
123     case 'd':
124         stat = 0;
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             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
138                      land.lnd_y);
139             land.lnd_own = 0;
140             pr("Land unit %d wiped\n", land.lnd_uid);
141             putland(land.lnd_uid, &land);
142         }
143     }
144     natp->nat_stat = stat;
145     strcpy(natp->nat_cnam, cntryname);
146     strcpy(natp->nat_pnam, pname);
147     if (*p != 'w' && *p != 'c') {
148         pr("Any existing sectors ignored\n");
149     } else {
150         pr("Checking sectors...\n");
151         snxtsct_all(&nstr);
152         while (nxtsct(&nstr, &sect)) {
153             if (sect.sct_own != coun)
154                 continue;
155             pr("%s ", xyas(nstr.x, nstr.y, player->cnum));
156             if (*p == 'w') {
157                 sect.sct_mobil = 0;
158                 sect.sct_effic = 0;
159                 sect.sct_road = 0;
160                 sect.sct_rail = 0;
161                 sect.sct_defense = 0;
162                 sect.sct_own = 0;
163                 sect.sct_oldown = 0;
164                 if (sect.sct_type == SCT_BSPAN ||
165                     sect.sct_type == SCT_BTOWER)
166                     sect.sct_newtype = sect.sct_type = SCT_WATER;
167                 else if (sect.sct_type != SCT_MOUNT &&
168                     sect.sct_type != SCT_PLAINS)
169                     sect.sct_newtype = sect.sct_type = SCT_RURAL;
170                 /* No dist path */
171                 sect.sct_dist_x = sect.sct_x;
172                 sect.sct_dist_y = sect.sct_y;
173                 memset(sect.sct_item, 0, sizeof(sect.sct_item));
174                 memset(sect.sct_del, 0, sizeof(sect.sct_del));
175                 memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
176                 sect.sct_mines = 0;
177                 sect.sct_pstage = PLG_HEALTHY;
178                 sect.sct_ptime = 0;
179                 sect.sct_che = 0;
180                 sect.sct_che_target = 0;
181                 sect.sct_fallout = 0;
182                 putsect(&sect);
183                 pr("wiped\n");
184             } else {
185                 pr("\n");
186             }
187         }
188     }
189
190     if ((natp->nat_stat & (STAT_INUSE | STAT_NORM | STAT_GOD)) ==
191         STAT_INUSE) {
192         *natp->nat_hostaddr = '\0';
193         *natp->nat_hostname = '\0';
194         *natp->nat_userid = '\0';
195         natp->nat_btu = 0;
196         natp->nat_reserve = 0;
197         natp->nat_tgms = 0;
198         natp->nat_ystart = 0;
199         natp->nat_xstart = 0;
200         natp->nat_ycap = 0;
201         natp->nat_xcap = 0;
202         natp->nat_yorg = 0;
203         natp->nat_xorg = 0;
204         natp->nat_dayno = 0;
205         natp->nat_minused = 0;
206         memset(natp->nat_b, 0, sizeof(natp->nat_b));
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 / 4; i++)
214             natp->nat_rejects[i] = 0;
215         natp->nat_newstim = 0;
216         natp->nat_annotim = 0;
217         (void)creat(mailbox(buf, coun), 0660);
218     } else
219         pr("No special initializations done...\n");
220
221     for (i = 0; i <= PRI_MAX; i++)
222         natp->nat_priorities[i] = -1;
223     natp->nat_flags =
224         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
225     for (i = 0; i < MAXNOC; i++)
226         natp->nat_relate[i] = NEUTRAL;
227     putnat(natp);
228     return 0;
229 }