]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sect.c
0d7f2b8b7594c0375e3b9b0f9b30126946321eaf
[empserver] / src / lib / commands / sect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  sect.c: Show sectors in map-like format with conditionals.
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37 #include "map.h"
38 #include "optlist.h"
39
40 int
41 sct(void)
42 {
43     struct nstr_sect ns;
44     struct sctstr sect;
45     struct nscstr cond[NS_NCOND];
46     struct range range;
47     struct natstr *np;
48     int ncond;
49     int nsect;
50     char *ptr;
51     coord y, yval;
52     int i;
53     /* Note this is not re-entrant anyway, so we keep the buffers
54        around */
55     static char *mapbuf = NULL;
56     static char **map = NULL;
57
58     nsect = 0;
59     if (!snxtsct(&ns, player->argp[1]))
60         return RET_SYN;
61     if (!mapbuf)
62         mapbuf = malloc(WORLD_Y * MAPWIDTH(1));
63     if (!map) {
64         map = malloc(WORLD_Y * sizeof(char *));
65         if (map && mapbuf) {
66             for (i = 0; i < WORLD_Y; i++)
67                 map[i] = &mapbuf[MAPWIDTH(1) * i];
68         } else if (map) {
69             free(map);
70             map = NULL;
71         }
72     }
73     if (!mapbuf || !map) {
74         pr("Memory error, tell the deity.\n");
75         logerror("malloc failed in sect\n");
76         return RET_FAIL;
77     }
78     np = getnatp(player->cnum);
79     ncond = ns.ncond;
80     memcpy(cond, ns.cond, sizeof(*cond) * ncond);
81     ns.ncond = 0;
82     xyrelrange(getnatp(player->cnum), &ns.range, &range);
83     border(&range, "    ", "");
84     blankfill(mapbuf, &ns.range, 1);
85     while (nxtsct(&ns, &sect)) {
86         if (!player->owner)
87             continue;
88         ptr = &map[ns.dy][ns.dx];
89         *ptr = dchr[sect.sct_type].d_mnem;
90         if (nstr_exec(cond, ncond, &sect)) {
91             ++nsect;
92             *ptr |= 0x80;
93         }
94     }
95     for (i = 0, y = ns.range.ly; i < ns.range.height; y++, i++) {
96         yval = yrel(np, y);
97         pr("%3d %s %-3d\n", yval, map[i], yval);
98         if (y >= WORLD_Y)
99             y -= WORLD_Y;
100     }
101     border(&range, "    ", "");
102     if (nsect == 0) {
103         if (player->argp[1])
104             pr("%s: No sector(s)\n", player->argp[1]);
105         else
106             pr("%s: No sector(s)\n", "");
107         return RET_FAIL;
108     } else
109         pr("%d sector%s\n", nsect, splur(nsect));
110     return RET_OK;
111 }