src/lib/commands/desi.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  *  desi.c: Redesignate sectors
00029  * 
00030  *  Known contributors to this file:
00031  *      Steve McClure, 1998-2000
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "commands.h"
00037 #include "map.h"
00038 #include "optlist.h"
00039 #include "path.h"
00040 
00041 static long do_desi(struct natstr *natp, char *sects, char *deschar,
00042                     long cash, int for_real);
00043 
00044 int
00045 desi(void)
00046 {
00047     long cash;
00048     long cost;
00049     struct natstr *natp;
00050 
00051     natp = getnatp(player->cnum);
00052     cash = natp->nat_money;
00053     if (player->argp[2]) {
00054         cost = do_desi(natp, player->argp[1], player->argp[2], cash, 0);
00055         if (cost < 0)
00056             return (int)-cost;
00057         if (chkmoney(cost, cash, player->argp[3]))
00058             return RET_SYN;
00059     }
00060     return (int)do_desi(natp, player->argp[1], player->argp[2], cash, 1);
00061 }
00062 
00063 static long
00064 do_desi(struct natstr *natp, char *sects, char *deschar, long cash,
00065         int for_real)
00066 {
00067     int n;
00068     char *p;
00069     int breaksanct;
00070     int cap_x;
00071     int cap_y;
00072     int des;
00073     struct nstr_sect nstr;
00074     struct sctstr sect;
00075     struct sctstr check;
00076     char prompt[128];
00077     char buf[1024];
00078     long cost = 0;
00079     int changed = 0;
00080     int warned = 0;
00081 
00082     breaksanct = 0;
00083     if (!snxtsct(&nstr, sects)) {
00084         if (for_real)
00085             return (long)RET_SYN;
00086         else
00087             return (long)-RET_SYN;
00088     }
00089     cap_x = natp->nat_xcap;
00090     cap_y = natp->nat_ycap;
00091     while (!player->aborted && nxtsct(&nstr, &sect)) {
00092         if (!player->owner)
00093             continue;
00094         if (!player->god && dchr[sect.sct_type].d_cost < 0)
00095             continue;
00096         sprintf(prompt, "%s %d%% %s  desig? ",
00097                 xyas(sect.sct_x, sect.sct_y, player->cnum),
00098                 sect.sct_effic, dchr[sect.sct_type].d_name);
00099         if ((p = getstarg(deschar, prompt, buf)) == 0)
00100             continue;
00101 
00102         if (!check_sect_ok(&sect))
00103             continue;
00104 
00105         des = sct_typematch(p);
00106         if (des < 0 || (((des == SCT_BSPAN) || (des == SCT_BTOWER)) &&
00107                         !player->god)) {
00108             pr("No such designation\n"
00109                "See \"info Sector-types\" for possible designations\n");
00110             if (for_real)
00111                 return (long)RET_FAIL;
00112             else
00113                 return (long)-RET_FAIL;
00114         }
00115         if (!player->god) {
00116             if (des == SCT_WASTE) {
00117                 if (for_real)
00118                     pr("Only a nuclear device (or %s) can make a %s!\n",
00119                        cname(0), dchr[des].d_name);
00120                 continue;
00121             }
00122             if (dchr[des].d_cost < 0) {
00123                 if (for_real)
00124                     pr("Only %s can make a %s!\n",
00125                        cname(0), dchr[des].d_name);
00126                 continue;
00127             }
00128         }
00129         if (sect.sct_type == des && sect.sct_newtype == des)
00130             continue;
00131         if (sect.sct_type == SCT_SANCT)
00132             breaksanct++;
00133         if ((des == SCT_HARBR) || (des == SCT_BHEAD)) {
00134             for (n = 1; n <= 6; n++) {
00135                 getsect(nstr.x + diroff[n][0],
00136                         nstr.y + diroff[n][1], &check);
00137                 if (check.sct_type == SCT_WATER)
00138                     break;
00139                 if (check.sct_type == SCT_BSPAN)
00140                     break;
00141                 if (check.sct_type == SCT_BTOWER)
00142                     break;
00143             }
00144             if (n > 6) {
00145                 if (for_real)
00146                     pr("%s does not border on water.\n",
00147                        xyas(nstr.x, nstr.y, player->cnum));
00148                 if (player->god) {
00149                     if (for_real)
00150                         pr("But if it's what you want ...\n");
00151                 } else
00152                     continue;
00153             }
00154         }
00155         if (sect.sct_type == SCT_SANCT && !player->god)
00156             continue;
00157         n = sect.sct_type;
00158         if ((sect.sct_newtype != des) && (sect.sct_type != des)
00159             && dchr[des].d_cost > 0) {
00160             if (for_real) {
00161                 if (check_cost(!deschar, dchr[des].d_cost, cash, &warned,
00162                                player->argp[3]))
00163                     break;
00164             } else {
00165                 cost += dchr[des].d_cost;
00166                 continue;
00167             }
00168         }
00169         if (sect.sct_type != des && (sect.sct_effic < 5 || player->god)) {
00170             if (player->god)
00171                 set_coastal(&sect, sect.sct_type, des);
00172             sect.sct_type = des;
00173             sect.sct_effic = 0;
00174             changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
00175                                dchr[des].d_mnem, 0);
00176         }
00177         sect.sct_newtype = des;
00178         putsect(&sect);
00179         if (sect.sct_x == cap_x && sect.sct_y == cap_y
00180             && des != SCT_CAPIT && des != SCT_SANCT && des != SCT_MOUNT
00181             && for_real)
00182             pr("You have redesignated your capital!\n");
00183         if (opt_EASY_BRIDGES == 0) {    /* may cause a bridge fall */
00184             if (n != SCT_BHEAD)
00185                 continue;
00186             bridgefall(&sect, 0);
00187         }
00188     }
00189     if (for_real) {
00190         if (changed)
00191             writemap(player->cnum);
00192         if (breaksanct)
00193             bsanct();
00194         return (long)RET_OK;
00195     } else {
00196         return cost;
00197     }
00198 }

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