]> git.pond.sub.org Git - empserver/blob - src/lib/commands/add.c
456110a77ebec6723d2b29eceae68ff2db99052c
[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  *     Markus Armbruster, 2004-2011
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37
38 int
39 add(void)
40 {
41     struct natstr *natp;
42     int i;
43     char cntryname[sizeof(natp->nat_cnam)];
44     char pname[sizeof(natp->nat_pnam)];
45     natid coun;
46     natid freecn;
47     char prompt[128];
48     char buf[1024];
49     char *p;
50     int stat;
51
52     for (freecn = 0; NULL != (natp = getnatp(freecn)); freecn++) {
53         if (natp->nat_stat == STAT_UNUSED)
54             break;
55     }
56     if (freecn < MAXNOC)
57         sprintf(prompt, "New country number? (%d is unused) ", freecn);
58     else
59         strcpy(prompt, "New country number? (they all seem to be used) ");
60     p = getstarg(player->argp[1], prompt, buf);
61     if (!p || !*p)
62         return RET_SYN;
63     i = atoi(p);
64     if (i >= MAXNOC) {
65         pr("Max # countries is %d\n", MAXNOC);
66         return RET_FAIL;
67     }
68     coun = i;
69     if (coun == 0) {
70         pr("Not allowed to add country #0\n");
71         return RET_FAIL;
72     }
73     natp = getnatp(coun);
74     p = getstarg(player->argp[2], "Country name? ", buf);
75     if (!p)
76         return RET_SYN;
77     if (!check_nat_name(p, coun))
78         return RET_FAIL;
79     strcpy(cntryname, p);
80     p = getstarg(player->argp[3], "Representative? ", buf);
81     if (!p || !*p)
82         return RET_SYN;
83     if (strlen(p) >= sizeof(pname)) {
84         pr("Representative too long\n");
85         return RET_FAIL;
86     }
87     strcpy(pname, p);
88     p = getstarg(player->argp[4],
89                  "Status? (visitor, new, god, delete) ", buf);
90     if (!p || !*p)
91         return RET_SYN;
92     switch (*p) {
93     case 'v':
94         stat = STAT_VIS;
95         break;
96     case 'n':
97         stat = STAT_NEW;
98         break;
99     case 'g':
100         stat = STAT_GOD;
101         break;
102     case 'd':
103         stat = STAT_UNUSED;
104         break;
105     default:
106         pr("Illegal status\n");
107         return RET_SYN;
108     }
109     strcpy(natp->nat_cnam, cntryname);
110     strcpy(natp->nat_pnam, pname);
111     if (stat == STAT_NEW || stat == STAT_VIS)
112         nat_reset(natp, stat, 0, 0);
113     else {
114         natp->nat_stat = stat;
115         pr("No special initializations done...\n");
116     }
117     putnat(natp);
118     return 0;
119 }