]> 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-2004, 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 "var.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     register int i;
57     s_char cntryname[21];
58     s_char pname[21];
59     natid coun;
60     natid freecn;
61     s_char prompt[128];
62     s_char buf[1024];
63     s_char *p;
64     s_char loopflag;
65     int stat;
66     struct nstr_item ni;
67     struct lndstr land;
68
69     for (freecn = 0; NULL != (natp = getnatp(freecn)); freecn++) {
70         if ((natp->nat_stat & STAT_INUSE) == 0)
71             break;
72     }
73     if (freecn < MAXNOC)
74         sprintf(prompt, "New country number? (%d is unused) ", freecn);
75     else
76         strcpy(prompt, "New country number? (they all seem to be used) ");
77     while ((p = getstarg(player->argp[1], prompt, buf)) && *p) {
78         coun = atoi(p);
79         if (coun < MAXNOC)
80             break;
81         pr("Max # countries is %d\n", MAXNOC);
82         player->argp[1] = 0;
83     }
84     if (p == 0 || *p == 0)
85         return RET_FAIL;
86     if (coun == 0) {
87         pr("Not allowed to add country #0\n");
88         return RET_FAIL;
89     }
90     natp = getnatp(coun);
91     while ((p = getstarg(player->argp[2], "Country Name? ", buf)) && *p) {
92         if (strlen(p) < 20) {
93             (void)strcpy(cntryname, p);
94             break;
95         }
96         pr("Too long.\n");
97         player->argp[2] = 0;
98     }
99     if (p == 0 || *p == 0)
100         return RET_OK;
101     while ((p = getstarg(player->argp[3], "Representative? ", buf)) && *p) {
102         if (strlen(p) < 20) {
103             (void)strcpy(pname, p);
104             break;
105         }
106         pr("Too long.\n");
107         player->argp[3] = 0;
108     }
109     if (p == 0 || *p == 0)
110         return RET_OK;
111     loopflag = 1;
112     stat = natp->nat_stat;
113     strcpy(prompt, "Status? (visitor, new, active, god, delete) ");
114     while (loopflag && (p = getstarg(player->argp[4], prompt, buf))) {
115         loopflag = 0;
116         switch (*p) {
117         case 'v':
118             stat = STAT_INUSE;
119             break;
120         case 'n':
121             stat = STAT_NEW | STAT_INUSE;
122             break;
123         case 'a':
124             stat = STAT_NORM | STAT_INUSE;
125             break;
126         case 'g':
127             stat = STAT_GOD | STAT_NORM | STAT_INUSE;
128             break;
129         case 'd':
130             stat = 0;
131             break;
132         default:
133             pr("Illegal selection\n");
134             loopflag = 1;
135             break;
136         }
137         player->argp[4] = 0;
138     }
139     if (p == 0)
140         return RET_OK;
141     p = getstarg(player->argp[5],
142                  "Check, wipe, or ignore existing sectors (c|w|i) ", buf);
143     if (p == 0)
144         return RET_OK;
145     snxtitem_all(&ni, EF_LAND);
146     while (nxtitem(&ni, (s_char *)&land)) {
147         if (land.lnd_own == coun) {
148             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
149                      land.lnd_y);
150             land.lnd_own = 0;
151             pr("Land unit %d wiped\n", land.lnd_uid);
152             putland(land.lnd_uid, &land);
153         }
154     }
155     natp->nat_stat = stat;
156     strcpy(natp->nat_cnam, cntryname);
157     strcpy(natp->nat_pnam, pname);
158     if (*p != 'w' && *p != 'c') {
159         pr("Any existing sectors ignored\n");
160     } else {
161         pr("Checking sectors...\n");
162         snxtsct_all(&nstr);
163         while (nxtsct(&nstr, &sect)) {
164             if (sect.sct_own != coun)
165                 continue;
166             pr("%s ", xyas(nstr.x, nstr.y, player->cnum));
167             if (*p == 'w') {
168                 sect.sct_mobil = 0;
169                 sect.sct_effic = 0;
170                 sect.sct_road = 0;
171                 sect.sct_rail = 0;
172                 sect.sct_defense = 0;
173                 sect.sct_own = 0;
174                 sect.sct_oldown = 0;
175                 if (sect.sct_type != SCT_MOUNT &&
176                     sect.sct_type != SCT_PLAINS) {
177                     sect.sct_type = SCT_RURAL;
178                     sect.sct_newtype = SCT_RURAL;
179                 }
180                 /* No dist path */
181                 sect.sct_dist_x = sect.sct_x;
182                 sect.sct_dist_y = sect.sct_y;
183                 memset(sect.sct_item, 0, sizeof(sect.sct_item));
184                 memset(sect.sct_del, 0, sizeof(sect.sct_del));
185                 memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
186                 sect.sct_mines = 0;
187                 sect.sct_pstage = PLG_HEALTHY;
188                 sect.sct_ptime = 0;
189                 sect.sct_che = 0;
190                 sect.sct_che_target = 0;
191                 sect.sct_fallout = 0;
192                 putsect(&sect);
193                 pr("wiped\n");
194             } else {
195                 pr("\n");
196             }
197         }
198     }
199
200     if ((natp->nat_stat & (STAT_INUSE | STAT_NORM | STAT_GOD)) ==
201         STAT_INUSE) {
202         *natp->nat_hostaddr = '\0';
203         *natp->nat_hostname = '\0';
204         *natp->nat_userid = '\0';
205         natp->nat_btu = 0;
206         natp->nat_connected = 0;
207         natp->nat_reserve = 0;
208         natp->nat_tgms = 0;
209         natp->nat_ystart = 0;
210         natp->nat_xstart = 0;
211         natp->nat_ycap = 0;
212         natp->nat_xcap = 0;
213         natp->nat_yorg = 0;
214         natp->nat_xorg = 0;
215         natp->nat_dayno = 0;
216         natp->nat_minused = 0;
217         memset(natp->nat_b, 0, sizeof(natp->nat_b));
218         (void)time(&natp->nat_last_login);
219         (void)time(&natp->nat_last_logout);
220         natp->nat_money = 0;
221         natp->nat_level[NAT_TLEV] = start_technology;
222         natp->nat_level[NAT_RLEV] = start_research;
223         natp->nat_level[NAT_ELEV] = start_education;
224         natp->nat_level[NAT_HLEV] = start_happiness;
225         for (i = 0; i < MAXNOC / 4; i++)
226             natp->nat_rejects[i] = 0;
227         natp->nat_newstim = 0;
228         natp->nat_annotim = 0;
229         (void)creat(mailbox(buf, coun), 0660);
230     } else
231         pr("No special initializations done...\n");
232
233     for (i = 0; i <= PRI_MAX; i++)
234         natp->nat_priorities[i] = -1;
235     natp->nat_flags =
236         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
237     for (i = 0; i < MAXNOC; i++)
238         natp->nat_relate[i] = NEUTRAL;
239     putnat(natp);
240     return 0;
241 }