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