src/lib/commands/sect.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  *  sect.c: Show sectors in map-like format with conditionals.
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1986
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "commands.h"
00037 #include "map.h"
00038 #include "optlist.h"
00039 
00040 int
00041 sct(void)
00042 {
00043     struct nstr_sect ns;
00044     struct sctstr sect;
00045     struct nscstr cond[NS_NCOND];
00046     struct range range;
00047     struct natstr *np;
00048     int ncond;
00049     int nsect;
00050     char *ptr;
00051     coord y, yval;
00052     int i;
00053     /* Note this is not re-entrant anyway, so we keep the buffers
00054        around */
00055     static char *mapbuf = NULL;
00056     static char **map = NULL;
00057 
00058     nsect = 0;
00059     if (!snxtsct(&ns, player->argp[1]))
00060         return RET_SYN;
00061     if (!mapbuf)
00062         mapbuf = malloc(WORLD_Y * MAPWIDTH(1));
00063     if (!map) {
00064         map = malloc(WORLD_Y * sizeof(char *));
00065         if (map && mapbuf) {
00066             for (i = 0; i < WORLD_Y; i++)
00067                 map[i] = &mapbuf[MAPWIDTH(1) * i];
00068         } else if (map) {
00069             free(map);
00070             map = NULL;
00071         }
00072     }
00073     if (!mapbuf || !map) {
00074         pr("Memory error, tell the deity.\n");
00075         logerror("malloc failed in sect\n");
00076         return RET_FAIL;
00077     }
00078     np = getnatp(player->cnum);
00079     ncond = ns.ncond;
00080     memcpy(cond, ns.cond, sizeof(*cond) * ncond);
00081     ns.ncond = 0;
00082     xyrelrange(getnatp(player->cnum), &ns.range, &range);
00083     border(&range, "    ", "");
00084     blankfill(mapbuf, &ns.range, 1);
00085     while (nxtsct(&ns, &sect)) {
00086         if (!player->owner)
00087             continue;
00088         ptr = &map[ns.dy][ns.dx];
00089         *ptr = dchr[sect.sct_type].d_mnem;
00090         if (nstr_exec(cond, ncond, &sect)) {
00091             ++nsect;
00092             *ptr |= 0x80;
00093         }
00094     }
00095     for (i = 0, y = ns.range.ly; i < ns.range.height; y++, i++) {
00096         yval = yrel(np, y);
00097         pr("%3d %s %-3d\n", yval, map[i], yval);
00098         if (y >= WORLD_Y)
00099             y -= WORLD_Y;
00100     }
00101     border(&range, "    ", "");
00102     if (nsect == 0) {
00103         if (player->argp[1])
00104             pr("%s: No sector(s)\n", player->argp[1]);
00105         else
00106             pr("%s: No sector(s)\n", "");
00107         return RET_FAIL;
00108     } else
00109         pr("%d sector%s\n", nsect, splur(nsect));
00110     return RET_OK;
00111 }

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