src/lib/commands/rout.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  *  rout.c: Show empire deliver routes
00029  * 
00030  *  Known contributors to this file:
00031  *     
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "commands.h"
00037 #include "item.h"
00038 #include "map.h"
00039 #include "optlist.h"
00040 #include "path.h"
00041 
00042 int
00043 rout(void)
00044 {
00045     struct ichrstr *ip;
00046     struct nstr_sect ns;
00047     struct natstr *natp;
00048     struct sctstr sect;
00049     struct nscstr cond[NS_NCOND];
00050     int ncond;
00051     struct range relrange;
00052     int row;
00053     int y;
00054     int ry;
00055     i_type i_del;
00056     int dir;
00057     char *p;
00058     /* Note this is not re-entrant anyway, so we keep the buffers
00059        around */
00060     static char *mapbuf = NULL;
00061     static char **map = NULL;
00062     static char *buf = NULL;
00063     int i;
00064 
00065     if ((ip = whatitem(player->argp[1], "What item? ")) == 0)
00066         return RET_SYN;
00067     i_del = ip->i_uid;;
00068     if (!snxtsct(&ns, player->argp[2]))
00069         return RET_FAIL;
00070     if (!mapbuf)
00071         mapbuf = malloc(WORLD_Y * MAPWIDTH(3));
00072     if (!map) {
00073         map = malloc(WORLD_Y * sizeof(char *));
00074         if (map && mapbuf) {
00075             for (i = 0; i < WORLD_Y; i++)
00076                 map[i] = &mapbuf[MAPWIDTH(3) * i];
00077         } else if (map) {
00078             free(map);
00079             map = NULL;
00080         }
00081     }
00082     if (!buf)
00083         buf = malloc(MAPWIDTH(3) + 12);
00084     if (!mapbuf || !map || !buf) {
00085         pr("Memory error, tell the deity.\n");
00086         logerror("malloc failed in rout\n");
00087         return RET_FAIL;
00088     }
00089     ncond = ns.ncond;
00090     memcpy(cond, ns.cond, sizeof(struct nscstr) * ncond);
00091     ns.ncond = 0;
00092 
00093     natp = getnatp(player->cnum);
00094     xyrelrange(natp, &ns.range, &relrange);
00095     memset(mapbuf, 0, ((WORLD_Y * MAPWIDTH(3))));
00096     blankfill(mapbuf, &ns.range, 3);
00097     border(&relrange, "     ", " ");
00098 
00099     while (nxtsct(&ns, &sect)) {
00100         if (!player->owner)
00101             continue;
00102         p = &map[ns.dy][ns.dx * 2];
00103         if ((dir = sect.sct_del[i_del] & 0x7) &&
00104             nstr_exec(cond, ncond, &sect))
00105             memcpy(p, routech[dir][0], 3);
00106         p[1] = dchr[sect.sct_type].d_mnem;
00107     }
00108     for (row = 0, y = ns.range.ly; row < ns.range.height; y++, row++) {
00109         ry = yrel(natp, y);
00110         memset(buf, 0, (MAPWIDTH(3) + 10));
00111         sprintf(buf, "%4d ", ry);
00112         memcpy(buf + 5, map[row], ns.range.width * 2 + 1);
00113         sprintf(buf + 5 + ns.range.width * 2 + 1, " %-4d\n", ry);
00114         pr("%s", buf);
00115         if (y >= WORLD_Y)
00116             y -= WORLD_Y;
00117     }
00118     border(&relrange, "     ", " ");
00119     return RET_OK;
00120 }

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