]> git.pond.sub.org Git - empserver/blob - src/lib/commands/add.c
Update copyright notice
[empserver] / src / lib / commands / add.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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)
88         return RET_SYN;
89     if (!check_nat_name(p))
90         return RET_FAIL;
91     strcpy(cntryname, p);
92     p = getstarg(player->argp[3], "Representative? ", buf);
93     if (p == 0 || *p == 0)
94         return RET_SYN;
95     if (strlen(p) >= sizeof(pname)) {
96         pr("Representative too long\n");
97         return RET_FAIL;
98     }
99     strcpy(pname, p);
100     p = getstarg(player->argp[4],
101                  "Status? (visitor, new, active, god, delete) ", buf);
102     if (p == 0 || *p == 0)
103         return RET_SYN;
104     switch (*p) {
105     case 'v':
106         stat = STAT_VIS;
107         break;
108     case 'n':
109         stat = STAT_NEW;
110         break;
111     case 'a':
112         stat = STAT_ACTIVE;
113         break;
114     case 'g':
115         stat = STAT_GOD;
116         break;
117     case 'd':
118         stat = STAT_UNUSED;
119         break;
120     default:
121         pr("Illegal status\n");
122         return RET_SYN;
123     }
124     p = getstarg(player->argp[5],
125                  "Check, wipe, or ignore existing sectors (c|w|i) ", buf);
126     if (p == 0)
127         return RET_SYN;
128     snxtitem_all(&ni, EF_LAND);
129     while (nxtitem(&ni, &land)) {
130         if (land.lnd_own == coun) {
131             land.lnd_effic = 0;
132             pr("Land unit %d wiped\n", land.lnd_uid);
133             putland(land.lnd_uid, &land);
134         }
135     }
136     strcpy(natp->nat_cnam, cntryname);
137     strcpy(natp->nat_pnam, pname);
138     if (*p != 'w' && *p != 'c') {
139         pr("Any existing sectors ignored\n");
140     } else {
141         pr("Checking sectors...\n");
142         snxtsct_all(&nstr);
143         while (nxtsct(&nstr, &sect)) {
144             if (sect.sct_own != coun)
145                 continue;
146             pr("%s ", xyas(nstr.x, nstr.y, player->cnum));
147             if (*p == 'w') {
148                 sect.sct_mobil = 0;
149                 sect.sct_effic = 0;
150                 sect.sct_road = 0;
151                 sect.sct_rail = 0;
152                 sect.sct_defense = 0;
153                 sect.sct_own = 0;
154                 sect.sct_oldown = 0;
155                 sect.sct_newtype = sect.sct_type
156                     = dchr[sect.sct_type].d_terrain;
157                 sect.sct_dist_x = sect.sct_x;
158                 sect.sct_dist_y = sect.sct_y;
159                 memset(sect.sct_item, 0, sizeof(sect.sct_item));
160                 memset(sect.sct_del, 0, sizeof(sect.sct_del));
161                 memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
162                 sect.sct_mines = 0;
163                 sect.sct_pstage = PLG_HEALTHY;
164                 sect.sct_ptime = 0;
165                 sect.sct_che = 0;
166                 sect.sct_che_target = 0;
167                 sect.sct_fallout = 0;
168                 putsect(&sect);
169                 pr("wiped\n");
170             } else {
171                 pr("\n");
172             }
173         }
174     }
175
176     if (stat == STAT_NEW || stat == STAT_VIS)
177         nat_reset(natp, stat, 0, 0);
178     else {
179         natp->nat_stat = stat;
180         pr("No special initializations done...\n");
181     }
182     putnat(natp);
183     return 0;
184 }