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