src/lib/commands/retr.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  *  retr.c: Set retreat conditionals for ships
00029  * 
00030  *  Known contributors to this file:
00031  *     Ken Stevens, 1995
00032  *     Steve McClure, 2000
00033  */
00034 
00035 #include <config.h>
00036 
00037 #include <ctype.h>
00038 #include "commands.h"
00039 #include "land.h"
00040 #include "path.h"
00041 #include "retreat.h"
00042 #include "ship.h"
00043 
00044 int
00045 retr(void)
00046 {
00047     char *pq, *fl;
00048     int nships;
00049     struct nstr_item ni;
00050     struct shpstr ship;
00051     int isfleet = 0;
00052     int rflags = -2;
00053     int zero;
00054     unsigned i;
00055     char buf1[1024];
00056     char buf2[1024];
00057 
00058     if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
00059         return RET_SYN;
00060     nships = 0;
00061     if (player->argp[1] != NULL)
00062         if (isalpha(player->argp[1][0]))
00063             isfleet = RET_GROUP;
00064     if (player->argp[2] != NULL)
00065         pq = getstarg(player->argp[2], "Retreat path? ", buf1);
00066     else
00067         pq = NULL;
00068 
00069     if (pq != NULL) {
00070         fl = getstarg(player->argp[3],
00071                       "Retreat conditions [i|t|s|h|b|d|u|c]? ", buf2);
00072         if (!fl)
00073             return RET_SYN;
00074         rflags = 0 | isfleet;
00075 
00076         for (i = 0; i < strlen(fl); i++) {
00077             switch (fl[i]) {
00078             case 'I':
00079             case 'i':
00080                 rflags |= RET_INJURED;
00081                 break;
00082             case 'T':
00083             case 't':
00084                 rflags |= RET_TORPED;
00085                 break;
00086             case 'S':
00087             case 's':
00088                 rflags |= RET_SONARED;
00089                 break;
00090             case 'H':
00091             case 'h':
00092                 rflags |= RET_HELPLESS;
00093                 break;
00094             case 'B':
00095             case 'b':
00096                 rflags |= RET_BOMBED;
00097                 break;
00098             case 'D':
00099             case 'd':
00100                 rflags |= RET_DCHRGED;
00101                 break;
00102             case 'U':
00103             case 'u':
00104                 rflags |= RET_BOARDED;
00105                 break;
00106             case 'C':
00107             case 'c':
00108                 rflags = -1;
00109                 break;
00110             default:
00111                 pr("bad condition\n");
00112                 /* fall through */
00113             case '?':
00114                 pr("i\tretreat when injured\n");
00115                 pr("t\tretreat when torped\n");
00116                 pr("s\tretreat when sonared\n");
00117                 pr("h\tretreat when helpless\n");
00118                 pr("b\tretreat when bombed\n");
00119                 pr("d\tretreat when depth-charged\n");
00120                 pr("u\tretreat when boarded\n");
00121             }
00122         }
00123         if (rflags == isfleet) {
00124             pr("Must give retreat conditions!\n");
00125             return RET_FAIL;
00126         }
00127     }
00128 
00129     if (rflags == -1)
00130         pq = NULL;
00131 
00132     zero = (rflags == -1);
00133     if (zero)
00134         rflags = 0;
00135 
00136     while (nxtitem(&ni, &ship)) {
00137         if (!player->owner || ship.shp_own == 0)
00138             continue;
00139         if (zero)
00140             memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
00141 
00142         if (pq != NULL) {
00143             strncpy(ship.shp_rpath, pq, sizeof(ship.shp_rpath) - 1);
00144             putship(ship.shp_uid, &ship);
00145         }
00146         if (rflags >= 0) {
00147             ship.shp_rflags = rflags;
00148             putship(ship.shp_uid, &ship);
00149         }
00150         if (nships++ == 0) {
00151             if (player->god)
00152                 pr("own ");
00153             pr("shp#     ship type       x,y   fl path       as flt? flags\n");
00154         }
00155         if (player->god)
00156             pr("%3d ", ship.shp_own);
00157         pr("%4d ", ni.cur);
00158         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
00159         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
00160         pr("%1.1s", &ship.shp_fleet);
00161         pr(" %-11s", ship.shp_rpath);
00162         if (ship.shp_rflags & RET_GROUP)
00163             pr("Yes     ");
00164         else
00165             pr("        ");
00166         if (ship.shp_rflags & RET_INJURED)
00167             pr("I");
00168         if (ship.shp_rflags & RET_TORPED)
00169             pr("T");
00170         if (ship.shp_rflags & RET_SONARED)
00171             pr("S");
00172         if (ship.shp_rflags & RET_HELPLESS)
00173             pr("H");
00174         if (ship.shp_rflags & RET_BOMBED)
00175             pr("B");
00176         if (ship.shp_rflags & RET_DCHRGED)
00177             pr("D");
00178         if (ship.shp_rflags & RET_BOARDED)
00179             pr("U");
00180         pr("\n");
00181     }
00182     if (nships == 0) {
00183         if (player->argp[1])
00184             pr("%s: No ship(s)\n", player->argp[1]);
00185         else
00186             pr("%s: No ship(s)\n", "");
00187         return RET_FAIL;
00188     } else
00189         pr("%d ship%s\n", nships, splur(nships));
00190     return RET_OK;
00191 }
00192 
00193 int
00194 lretr(void)
00195 {
00196     char *pq, *fl;
00197     int nunits;
00198     struct nstr_item ni;
00199     struct lndstr land;
00200     int isarmy = 0;
00201     int rflags = -2;
00202     int zero;
00203     char buf1[1024];
00204     char buf2[1024];
00205     unsigned i;
00206 
00207     if (!snxtitem(&ni, EF_LAND, player->argp[1]))
00208         return RET_SYN;
00209     nunits = 0;
00210     if (player->argp[1] != NULL)
00211         if (isalpha(player->argp[1][0]))
00212             isarmy = RET_GROUP;
00213     if (player->argp[2] != NULL)
00214         pq = getstarg(player->argp[2], "Retreat path? ", buf1);
00215     else
00216         pq = NULL;
00217     if (pq != NULL) {
00218         fl = getstarg(player->argp[3], "Retreat conditions [i|h|b|c]? ",
00219                       buf2);
00220         if (!fl)
00221             return RET_SYN;
00222         rflags = 0 | isarmy;
00223 
00224         for (i = 0; i < strlen(fl); i++) {
00225             switch (fl[i]) {
00226             case 'I':
00227             case 'i':
00228                 rflags |= RET_INJURED;
00229                 break;
00230             case 'H':
00231             case 'h':
00232                 rflags |= RET_HELPLESS;
00233                 break;
00234             case 'B':
00235             case 'b':
00236                 rflags |= RET_BOMBED;
00237                 break;
00238             case 'C':
00239             case 'c':
00240                 rflags = -1;
00241                 break;
00242             default:
00243                 pr("bad condition\n");
00244                 /* fall through */
00245             case '?':
00246                 pr("i\tretreat when injured\n");
00247                 pr("h\tretreat when helpless\n");
00248                 pr("b\tretreat when bombed\n");
00249             }
00250         }
00251         if (rflags == isarmy) {
00252             pr("Must give retreat conditions!\n");
00253             return RET_FAIL;
00254         }
00255     }
00256 
00257     if (rflags == -1)
00258         pq = NULL;
00259 
00260     zero = (rflags == -1);
00261     if (zero)
00262         rflags = 0;
00263 
00264     while (nxtitem(&ni, &land)) {
00265         if (!player->owner || land.lnd_own == 0)
00266             continue;
00267         if (zero)
00268             memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
00269 
00270         if (pq != NULL) {
00271             strncpy(land.lnd_rpath, pq, sizeof(land.lnd_rpath) - 1);
00272             putland(land.lnd_uid, &land);
00273         }
00274         if (rflags >= 0) {
00275             land.lnd_rflags = rflags;
00276             putland(land.lnd_uid, &land);
00277         }
00278 
00279         if (nunits++ == 0) {
00280             if (player->god)
00281                 pr("own ");
00282             pr("lnd#     unit type       x,y   ar path       as army? flags\n");
00283         }
00284         if (player->god)
00285             pr("%3d ", land.lnd_own);
00286         pr("%4d ", ni.cur);
00287         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
00288         prxy("%4d,%-4d ", land.lnd_x, land.lnd_y, player->cnum);
00289         pr("%1.1s", &land.lnd_army);
00290         pr(" %-11s", land.lnd_rpath);
00291         if (land.lnd_rflags & RET_GROUP)
00292             pr("Yes      ");
00293         else
00294             pr("         ");
00295         if (land.lnd_rflags & RET_INJURED)
00296             pr("I");
00297         if (land.lnd_rflags & RET_TORPED)
00298             pr("T");
00299         if (land.lnd_rflags & RET_SONARED)
00300             pr("S");
00301         if (land.lnd_rflags & RET_HELPLESS)
00302             pr("H");
00303         if (land.lnd_rflags & RET_BOMBED)
00304             pr("B");
00305         if (land.lnd_rflags & RET_DCHRGED)
00306             pr("D");
00307         if (land.lnd_rflags & RET_BOARDED)
00308             pr("U");
00309         pr("\n");
00310     }
00311     if (nunits == 0) {
00312         if (player->argp[1])
00313             pr("%s: No unit(s)\n", player->argp[1]);
00314         else
00315             pr("%s: No unit(s)\n", "");
00316         return RET_FAIL;
00317     }
00318     pr("%d unit%s\n", nunits, splur(nunits));
00319     return RET_OK;
00320 }

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