src/lib/commands/add.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  add.c: Add a new country to the game
00029  * 
00030  *  Known contributors to this file:
00031  *     Steve McClure, 2000
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include <fcntl.h>
00037 #include <unistd.h>
00038 #include <sys/stat.h>
00039 
00040 #include "commands.h"
00041 #include "land.h"
00042 #include "optlist.h"
00043 #include "plague.h"
00044 #include "tel.h"
00045 
00046 int
00047 add(void)
00048 {
00049     struct natstr *natp;
00050     struct sctstr sect;
00051     struct nstr_sect nstr;
00052     int i;
00053     char cntryname[sizeof(natp->nat_cnam)];
00054     char pname[sizeof(natp->nat_pnam)];
00055     natid coun;
00056     natid freecn;
00057     char prompt[128];
00058     char buf[1024];
00059     char *p;
00060     int stat;
00061     struct nstr_item ni;
00062     struct lndstr land;
00063     struct realmstr realm;
00064     time_t current_time = time(NULL);
00065 
00066     for (freecn = 0; NULL != (natp = getnatp(freecn)); freecn++) {
00067         if (natp->nat_stat == STAT_UNUSED)
00068             break;
00069     }
00070     if (freecn < MAXNOC)
00071         sprintf(prompt, "New country number? (%d is unused) ", freecn);
00072     else
00073         strcpy(prompt, "New country number? (they all seem to be used) ");
00074     p = getstarg(player->argp[1], prompt, buf);
00075     if (p == 0 || *p == 0)
00076         return RET_SYN;
00077     i = atoi(p);
00078     if (i >= MAXNOC) {
00079         pr("Max # countries is %d\n", MAXNOC);
00080         return RET_FAIL;
00081     }
00082     coun = i;
00083     if (coun == 0) {
00084         pr("Not allowed to add country #0\n");
00085         return RET_FAIL;
00086     }
00087     natp = getnatp(coun);
00088     p = getstarg(player->argp[2], "Country Name? ", buf);
00089     if (p == 0 || *p == 0)
00090         return RET_SYN;
00091     if (strlen(p) >= sizeof(cntryname)) {
00092         pr("Country name too long\n");
00093         return RET_FAIL;
00094     }
00095     strcpy(cntryname, p);
00096     p = getstarg(player->argp[3], "Representative? ", buf);
00097     if (p == 0 || *p == 0)
00098         return RET_SYN;
00099     if (strlen(p) >= sizeof(pname)) {
00100         pr("Representative too long\n");
00101         return RET_FAIL;
00102     }
00103     strcpy(pname, p);
00104     p = getstarg(player->argp[4],
00105                  "Status? (visitor, new, active, god, delete) ", buf);
00106     if (p == 0 || *p == 0)
00107         return RET_SYN;
00108     switch (*p) {
00109     case 'v':
00110         stat = STAT_VIS;
00111         break;
00112     case 'n':
00113         stat = STAT_NEW;
00114         break;
00115     case 'a':
00116         stat = STAT_ACTIVE;
00117         break;
00118     case 'g':
00119         stat = STAT_GOD;
00120         break;
00121     case 'd':
00122         stat = STAT_UNUSED;
00123         break;
00124     default:
00125         pr("Illegal status\n");
00126         return RET_SYN;
00127     }
00128     p = getstarg(player->argp[5],
00129                  "Check, wipe, or ignore existing sectors (c|w|i) ", buf);
00130     if (p == 0)
00131         return RET_SYN;
00132     snxtitem_all(&ni, EF_LAND);
00133     while (nxtitem(&ni, &land)) {
00134         if (land.lnd_own == coun) {
00135             land.lnd_effic = 0;
00136             pr("Land unit %d wiped\n", land.lnd_uid);
00137             putland(land.lnd_uid, &land);
00138         }
00139     }
00140     natp->nat_stat = stat;
00141     strcpy(natp->nat_cnam, cntryname);
00142     strcpy(natp->nat_pnam, pname);
00143     if (*p != 'w' && *p != 'c') {
00144         pr("Any existing sectors ignored\n");
00145     } else {
00146         pr("Checking sectors...\n");
00147         snxtsct_all(&nstr);
00148         while (nxtsct(&nstr, &sect)) {
00149             if (sect.sct_own != coun)
00150                 continue;
00151             pr("%s ", xyas(nstr.x, nstr.y, player->cnum));
00152             if (*p == 'w') {
00153                 sect.sct_mobil = 0;
00154                 sect.sct_effic = 0;
00155                 sect.sct_road = 0;
00156                 sect.sct_rail = 0;
00157                 sect.sct_defense = 0;
00158                 sect.sct_own = 0;
00159                 sect.sct_oldown = 0;
00160                 if (sect.sct_type == SCT_BSPAN ||
00161                     sect.sct_type == SCT_BTOWER)
00162                     sect.sct_newtype = sect.sct_type = SCT_WATER;
00163                 else if (sect.sct_type != SCT_MOUNT &&
00164                     sect.sct_type != SCT_PLAINS)
00165                     sect.sct_newtype = sect.sct_type = SCT_RURAL;
00166                 /* No dist path */
00167                 sect.sct_dist_x = sect.sct_x;
00168                 sect.sct_dist_y = sect.sct_y;
00169                 memset(sect.sct_item, 0, sizeof(sect.sct_item));
00170                 memset(sect.sct_del, 0, sizeof(sect.sct_del));
00171                 memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
00172                 sect.sct_mines = 0;
00173                 sect.sct_pstage = PLG_HEALTHY;
00174                 sect.sct_ptime = 0;
00175                 sect.sct_che = 0;
00176                 sect.sct_che_target = 0;
00177                 sect.sct_fallout = 0;
00178                 putsect(&sect);
00179                 pr("wiped\n");
00180             } else {
00181                 pr("\n");
00182             }
00183         }
00184     }
00185 
00186     if (natp->nat_stat == STAT_NEW || natp->nat_stat == STAT_VIS) {
00187         *natp->nat_hostaddr = '\0';
00188         *natp->nat_hostname = '\0';
00189         *natp->nat_userid = '\0';
00190         natp->nat_btu = 0;
00191         natp->nat_reserve = 0;
00192         natp->nat_tgms = 0;
00193         natp->nat_ycap = 0;
00194         natp->nat_xcap = 0;
00195         natp->nat_yorg = 0;
00196         natp->nat_xorg = 0;
00197         natp->nat_dayno = 0;
00198         natp->nat_minused = 0;
00199         for (i = 0; i < MAXNOR; i++) {
00200             getrealm(i, coun, &realm);
00201             realm.r_xl = realm.r_xh = realm.r_yl = realm.r_yh = 0;
00202             realm.r_timestamp = current_time;
00203             putrealm(&realm);
00204         }
00205         natp->nat_last_login = natp->nat_last_login = 0;
00206         natp->nat_money = 0;
00207         natp->nat_level[NAT_TLEV] = start_technology;
00208         natp->nat_level[NAT_RLEV] = start_research;
00209         natp->nat_level[NAT_ELEV] = start_education;
00210         natp->nat_level[NAT_HLEV] = start_happiness;
00211         for (i = 0; i < MAXNOC; i++)
00212             natp->nat_rejects[i] = 0;
00213         natp->nat_newstim = 0;
00214         natp->nat_annotim = 0;
00215         close(creat(mailbox(buf, coun), S_IRWUG));
00216     } else
00217         pr("No special initializations done...\n");
00218 
00219     natp->nat_flags =
00220         NF_FLASH | NF_BEEP | NF_COASTWATCH | NF_SONAR | NF_TECHLISTS;
00221     for (i = 0; i < MAXNOC; i++)
00222         natp->nat_relate[i] = NEUTRAL;
00223     putnat(natp);
00224     return 0;
00225 }

Generated on Fri Mar 28 11:01:11 2008 for empserver by  doxygen 1.5.2