]> git.pond.sub.org Git - empserver/blob - src/lib/commands/add.c
Fix trailing whitespace
[empserver] / src / lib / commands / add.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 #include <unistd.h>
38 #include <sys/stat.h>
39
40 #include "commands.h"
41 #include "land.h"
42 #include "optlist.h"
43 #include "plague.h"
44 #include "tel.h"
45
46 int
47 add(void)
48 {
49     struct natstr *natp;
50     struct sctstr sect;
51     struct nstr_sect nstr;
52     int i;
53     char cntryname[sizeof(natp->nat_cnam)];
54     char pname[sizeof(natp->nat_pnam)];
55     natid coun;
56     natid freecn;
57     char prompt[128];
58     char buf[1024];
59     char *p;
60     int stat;
61     struct nstr_item ni;
62     struct lndstr land;
63
64     for (freecn = 0; NULL != (natp = getnatp(freecn)); freecn++) {
65         if (natp->nat_stat == STAT_UNUSED)
66             break;
67     }
68     if (freecn < MAXNOC)
69         sprintf(prompt, "New country number? (%d is unused) ", freecn);
70     else
71         strcpy(prompt, "New country number? (they all seem to be used) ");
72     p = getstarg(player->argp[1], prompt, buf);
73     if (p == 0 || *p == 0)
74         return RET_SYN;
75     i = atoi(p);
76     if (i >= MAXNOC) {
77         pr("Max # countries is %d\n", MAXNOC);
78         return RET_FAIL;
79     }
80     coun = i;
81     if (coun == 0) {
82         pr("Not allowed to add country #0\n");
83         return RET_FAIL;
84     }
85     natp = getnatp(coun);
86     p = getstarg(player->argp[2], "Country Name? ", buf);
87     if (p == 0 || *p == 0)
88         return RET_SYN;
89     if (strlen(p) >= sizeof(cntryname)) {
90         pr("Country name too long\n");
91         return RET_FAIL;
92     }
93     strcpy(cntryname, p);
94     p = getstarg(player->argp[3], "Representative? ", buf);
95     if (p == 0 || *p == 0)
96         return RET_SYN;
97     if (strlen(p) >= sizeof(pname)) {
98         pr("Representative too long\n");
99         return RET_FAIL;
100     }
101     strcpy(pname, p);
102     p = getstarg(player->argp[4],
103                  "Status? (visitor, new, active, god, delete) ", buf);
104     if (p == 0 || *p == 0)
105         return RET_SYN;
106     switch (*p) {
107     case 'v':
108         stat = STAT_VIS;
109         break;
110     case 'n':
111         stat = STAT_NEW;
112         break;
113     case 'a':
114         stat = STAT_ACTIVE;
115         break;
116     case 'g':
117         stat = STAT_GOD;
118         break;
119     case 'd':
120         stat = STAT_UNUSED;
121         break;
122     default:
123         pr("Illegal status\n");
124         return RET_SYN;
125     }
126     p = getstarg(player->argp[5],
127                  "Check, wipe, or ignore existing sectors (c|w|i) ", buf);
128     if (p == 0)
129         return RET_SYN;
130     snxtitem_all(&ni, EF_LAND);
131     while (nxtitem(&ni, &land)) {
132         if (land.lnd_own == coun) {
133             land.lnd_effic = 0;
134             pr("Land unit %d wiped\n", land.lnd_uid);
135             putland(land.lnd_uid, &land);
136         }
137     }
138     strcpy(natp->nat_cnam, cntryname);
139     strcpy(natp->nat_pnam, pname);
140     if (*p != 'w' && *p != 'c') {
141         pr("Any existing sectors ignored\n");
142     } else {
143         pr("Checking sectors...\n");
144         snxtsct_all(&nstr);
145         while (nxtsct(&nstr, &sect)) {
146             if (sect.sct_own != coun)
147                 continue;
148             pr("%s ", xyas(nstr.x, nstr.y, player->cnum));
149             if (*p == 'w') {
150                 sect.sct_mobil = 0;
151                 sect.sct_effic = 0;
152                 sect.sct_road = 0;
153                 sect.sct_rail = 0;
154                 sect.sct_defense = 0;
155                 sect.sct_own = 0;
156                 sect.sct_oldown = 0;
157                 if (sect.sct_type == SCT_BSPAN ||
158                     sect.sct_type == SCT_BTOWER)
159                     sect.sct_newtype = sect.sct_type = SCT_WATER;
160                 else if (sect.sct_type != SCT_MOUNT &&
161                     sect.sct_type != SCT_PLAINS)
162                     sect.sct_newtype = sect.sct_type = SCT_RURAL;
163                 /* No dist path */
164                 sect.sct_dist_x = sect.sct_x;
165                 sect.sct_dist_y = sect.sct_y;
166                 memset(sect.sct_item, 0, sizeof(sect.sct_item));
167                 memset(sect.sct_del, 0, sizeof(sect.sct_del));
168                 memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
169                 sect.sct_mines = 0;
170                 sect.sct_pstage = PLG_HEALTHY;
171                 sect.sct_ptime = 0;
172                 sect.sct_che = 0;
173                 sect.sct_che_target = 0;
174                 sect.sct_fallout = 0;
175                 putsect(&sect);
176                 pr("wiped\n");
177             } else {
178                 pr("\n");
179             }
180         }
181     }
182
183     if (stat == STAT_NEW || stat == STAT_VIS)
184         nat_reset(natp, stat, 0, 0);
185     else {
186         natp->nat_stat = stat;
187         pr("No special initializations done...\n");
188     }
189     putnat(natp);
190     return 0;
191 }