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