]> git.pond.sub.org Git - empserver/blob - src/lib/commands/add.c
ea7f6eb401053ae7458a92d29fbf88f4d02fe1f9
[empserver] / src / lib / commands / add.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  add.c: Add a new country to the game
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 2000
31  */
32
33 #include <config.h>
34
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/stat.h>
38
39 #include "commands.h"
40 #include "land.h"
41 #include "optlist.h"
42 #include "plague.h"
43
44 int
45 add(void)
46 {
47     struct natstr *natp;
48     struct sctstr sect;
49     struct nstr_sect nstr;
50     int i;
51     char cntryname[sizeof(natp->nat_cnam)];
52     char pname[sizeof(natp->nat_pnam)];
53     natid coun;
54     natid freecn;
55     char prompt[128];
56     char buf[1024];
57     char *p;
58     int stat;
59     struct nstr_item ni;
60     struct lndstr land;
61
62     for (freecn = 0; NULL != (natp = getnatp(freecn)); freecn++) {
63         if (natp->nat_stat == STAT_UNUSED)
64             break;
65     }
66     if (freecn < MAXNOC)
67         sprintf(prompt, "New country number? (%d is unused) ", freecn);
68     else
69         strcpy(prompt, "New country number? (they all seem to be used) ");
70     p = getstarg(player->argp[1], prompt, buf);
71     if (!p || !*p)
72         return RET_SYN;
73     i = atoi(p);
74     if (i >= MAXNOC) {
75         pr("Max # countries is %d\n", MAXNOC);
76         return RET_FAIL;
77     }
78     coun = i;
79     if (coun == 0) {
80         pr("Not allowed to add country #0\n");
81         return RET_FAIL;
82     }
83     natp = getnatp(coun);
84     p = getstarg(player->argp[2], "Country name? ", buf);
85     if (!p)
86         return RET_SYN;
87     if (!check_nat_name(p))
88         return RET_FAIL;
89     strcpy(cntryname, p);
90     p = getstarg(player->argp[3], "Representative? ", buf);
91     if (!p || !*p)
92         return RET_SYN;
93     if (strlen(p) >= sizeof(pname)) {
94         pr("Representative too long\n");
95         return RET_FAIL;
96     }
97     strcpy(pname, p);
98     p = getstarg(player->argp[4],
99                  "Status? (visitor, new, active, god, delete) ", buf);
100     if (!p || !*p)
101         return RET_SYN;
102     switch (*p) {
103     case 'v':
104         stat = STAT_VIS;
105         break;
106     case 'n':
107         stat = STAT_NEW;
108         break;
109     case 'a':
110         stat = STAT_ACTIVE;
111         break;
112     case 'g':
113         stat = STAT_GOD;
114         break;
115     case 'd':
116         stat = STAT_UNUSED;
117         break;
118     default:
119         pr("Illegal status\n");
120         return RET_SYN;
121     }
122     p = getstarg(player->argp[5],
123                  "Check, wipe, or ignore existing sectors (c|w|i) ", buf);
124     if (!p)
125         return RET_SYN;
126     snxtitem_all(&ni, EF_LAND);
127     while (nxtitem(&ni, &land)) {
128         if (land.lnd_own == coun) {
129             land.lnd_effic = 0;
130             pr("Land unit %d wiped\n", land.lnd_uid);
131             putland(land.lnd_uid, &land);
132         }
133     }
134     strcpy(natp->nat_cnam, cntryname);
135     strcpy(natp->nat_pnam, pname);
136     if (*p != 'w' && *p != 'c') {
137         pr("Any existing sectors ignored\n");
138     } else {
139         pr("Checking sectors...\n");
140         snxtsct_all(&nstr);
141         while (nxtsct(&nstr, &sect)) {
142             if (sect.sct_own != coun)
143                 continue;
144             pr("%s ", xyas(nstr.x, nstr.y, player->cnum));
145             if (*p == 'w') {
146                 sect.sct_mobil = 0;
147                 sect.sct_effic = 0;
148                 sect.sct_road = 0;
149                 sect.sct_rail = 0;
150                 sect.sct_defense = 0;
151                 sect.sct_own = 0;
152                 sect.sct_oldown = 0;
153                 sect.sct_newtype = sect.sct_type
154                     = dchr[sect.sct_type].d_terrain;
155                 sect.sct_dist_x = sect.sct_x;
156                 sect.sct_dist_y = sect.sct_y;
157                 memset(sect.sct_item, 0, sizeof(sect.sct_item));
158                 memset(sect.sct_del, 0, sizeof(sect.sct_del));
159                 memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
160                 sect.sct_mines = 0;
161                 sect.sct_pstage = PLG_HEALTHY;
162                 sect.sct_ptime = 0;
163                 sect.sct_che = 0;
164                 sect.sct_che_target = 0;
165                 sect.sct_fallout = 0;
166                 putsect(&sect);
167                 pr("wiped\n");
168             } else {
169                 pr("\n");
170             }
171         }
172     }
173
174     if (stat == STAT_NEW || stat == STAT_VIS)
175         nat_reset(natp, stat, 0, 0);
176     else {
177         natp->nat_stat = stat;
178         pr("No special initializations done...\n");
179     }
180     putnat(natp);
181     return 0;
182 }