]> git.pond.sub.org Git - empserver/blob - src/lib/commands/path.c
6c419812d731880a4b6e88d0a3e512d77cfa6f29
[empserver] / src / lib / commands / path.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  *  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 <config.h>
36
37 #include "commands.h"
38 #include "map.h"
39 #include "optlist.h"
40 #include "path.h"
41
42 int
43 path(void)
44 {
45
46     struct nstr_sect ns;
47     struct natstr *natp;
48     struct range absrange;
49     struct range relrange;
50     struct sctstr sect, dsect;
51     coord cx, cy;
52     int i;
53     int y;
54     char *pp, *p;
55     /* Note this is not re-entrant anyway, so we keep the buffers
56        around */
57     static char *mapbuf = NULL;
58     static char **map = NULL;
59     double move_cost;
60     char buf[1024];
61
62     if (!(p = getstarg(player->argp[1], "from sector : ", buf)) ||
63         !sarg_xy(p, &cx, &cy) || !getsect(cx, cy, &sect))
64         return RET_SYN;
65     if ((sect.sct_own != player->cnum) && !player->god) {
66         pr("Not yours\n");
67         return RET_FAIL;
68     }
69     getsect(sect.sct_dist_x, sect.sct_dist_y, &dsect);
70     pp = BestDistPath(buf, &sect, &dsect, &move_cost);
71     if (!pp) {
72         pr("No path possible from %s to distribution sector %s\n",
73            xyas(sect.sct_x, sect.sct_y, player->cnum),
74            xyas(dsect.sct_x, dsect.sct_y, player->cnum));
75         return RET_FAIL;
76     }
77     if (!mapbuf)
78         mapbuf = malloc(WORLD_Y * MAPWIDTH(3));
79     if (!map) {
80         map = malloc(WORLD_Y * sizeof(char *));
81         if (map && mapbuf) {
82             for (i = 0; i < WORLD_Y; i++)
83                 map[i] = &mapbuf[MAPWIDTH(3) * i];
84         } else if (map) {
85             free(map);
86             map = NULL;
87         }
88     }
89     if (!mapbuf || !map) {
90         pr("Memory error, tell the deity.\n");
91         logerror("malloc failed in path\n");
92         return RET_FAIL;
93     }
94     pathrange(cx, cy, pp, 1, &absrange);
95     snxtsct_area(&ns, &absrange);
96     natp = getnatp(player->cnum);
97     xyrelrange(natp, &absrange, &relrange);
98     blankfill(mapbuf, &ns.range, 3);
99     for (; *pp; ++pp) {
100         i = diridx(*pp);
101         if (i == DIR_STOP)
102             break;
103         memcpy(&map[deltay(cy, ns.range.ly)][deltax(cx, ns.range.lx) * 2],
104                routech[i][0],
105                3);
106         cx += diroff[i][0];
107         cy += diroff[i][1];
108     }
109     border(&relrange, "     ", " ");
110     while (nxtsct(&ns, &sect)) {
111         if (!player->owner)
112             continue;
113         map[ns.dy][ns.dx * 2 + 1] = dchr[sect.sct_type].d_mnem;
114     }
115     for (y = ns.range.ly, i = 0; i < ns.range.height; y++, i++) {
116         cy = yrel(natp, y);
117         pr("%4d %s %-4d\n", cy, map[i], cy);
118         if (y >= WORLD_Y)
119             y -= WORLD_Y;
120     }
121     border(&relrange, "     ", " ");
122     return RET_OK;
123 }