]> git.pond.sub.org Git - empserver/blob - src/lib/commands/path.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / path.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  path.c: Show empire distribution paths
29  * 
30  *  Known contributors to this file:
31  *     David Muir Sharnoff, 1986
32  *     (unknown rewrite), 1989
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "xy.h"
38 #include "nsc.h"
39 #include "nat.h"
40 #include "file.h"
41 #include "sect.h"
42 #include "path.h"
43 #include "map.h"
44 #include "commands.h"
45 #include "optlist.h"
46
47 int
48 path(void)
49 {
50
51     struct nstr_sect ns;
52     struct natstr *natp;
53     struct range absrange;
54     struct range relrange;
55     struct sctstr sect, dsect;
56     coord cx, cy;
57     int i;
58     int y;
59     s_char *pp, *p;
60     /* Note this is not re-entrant anyway, so we keep the buffers
61        around */
62     static s_char *mapbuf = (s_char *)0;
63     static s_char **map = (s_char **)0;
64     double move_cost;
65     s_char buf[1024];
66
67     if (!(p = getstarg(player->argp[1], "from sector : ", buf)) ||
68         !sarg_xy(p, &cx, &cy) || !getsect(cx, cy, &sect))
69         return RET_SYN;
70     if ((sect.sct_own != player->cnum) && !player->god) {
71         pr("Not yours\n");
72         return RET_FAIL;
73     }
74     getsect(sect.sct_dist_x, sect.sct_dist_y, &dsect);
75     pp = BestDistPath(buf, &sect, &dsect, &move_cost, MOB_ROAD);
76     if (pp == (s_char *)0) {
77         pr("No path possible from %s to distribution sector %s\n",
78            xyas(sect.sct_x, sect.sct_y, player->cnum),
79            xyas(dsect.sct_x, dsect.sct_y, player->cnum));
80         return RET_FAIL;
81     }
82     if (!mapbuf)
83         mapbuf = malloc((WORLD_Y * MAPWIDTH(3)) * sizeof(s_char));
84     if (!map) {
85         map = malloc(WORLD_Y * sizeof(s_char *));
86         if (map && mapbuf) {
87             for (i = 0; i < WORLD_Y; i++)
88                 map[i] = &mapbuf[MAPWIDTH(3) * i];
89         } else if (map) {
90             free(map);
91             map = (s_char **)0;
92         }
93     }
94     if (!mapbuf || !map) {
95         pr("Memory error, tell the deity.\n");
96         logerror("malloc failed in path\n");
97         return RET_FAIL;
98     }
99     pathrange(cx, cy, pp, 1, &absrange);
100     snxtsct_area(&ns, &absrange);
101     natp = getnatp(player->cnum);
102     xyrelrange(natp, &absrange, &relrange);
103     blankfill((s_char *)mapbuf, &ns.range, 3);
104     while (*pp && (i = chkdir(*pp, DIR_STOP, DIR_LAST)) >= 0) {
105         memcpy(&map[deltay(cy, ns.range.ly)][deltax(cx, ns.range.lx) * 2],
106                routech[i][0],
107                3);
108         cx += diroff[i][0];
109         cy += diroff[i][1];
110         ++pp;
111     }
112     border(&relrange, "     ", " ");
113     while (nxtsct(&ns, &sect)) {
114         if (!player->owner)
115             continue;
116         map[ns.dy][ns.dx * 2 + 1] = dchr[sect.sct_type].d_mnem;
117     }
118     for (y = ns.range.ly, i = 0; i < ns.range.height; y++, i++) {
119         cy = yrel(natp, y);
120         pr("%4d %s %-4d\n", cy, map[i], cy);
121         if (y >= WORLD_Y)
122             y -= WORLD_Y;
123     }
124     border(&relrange, "     ", " ");
125     return RET_OK;
126 }