]> git.pond.sub.org Git - empserver/blob - src/lib/commands/path.c
Update copyright notice
[empserver] / src / lib / commands / path.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  path.c: Show empire distribution paths
28  *
29  *  Known contributors to this file:
30  *     David Muir Sharnoff, 1986
31  *     (unknown rewrite), 1989
32  *     Markus Armbruster, 2005-2011
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;
51     coord cx, cy;
52     int i;
53     int y;
54     char *pp, *p;
55     size_t len;
56     /* Note this is not re-entrant anyway, so we keep the buffers
57        around */
58     static char *mapbuf = NULL;
59     static char **map = NULL;
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     if (path_find(sect.sct_x, sect.sct_y, sect.sct_dist_x, sect.sct_dist_y,
70                   player->cnum, MOB_MOVE) < 0) {
71         pr("No path possible from %s to distribution sector %s\n",
72            xyas(sect.sct_x, sect.sct_y, player->cnum),
73            xyas(sect.sct_dist_x, sect.sct_dist_y, player->cnum));
74         return RET_FAIL;
75     }
76     len = path_find_route(buf, sizeof(buf), sect.sct_x, sect.sct_y,
77                           sect.sct_dist_x, sect.sct_dist_y);
78     if (len >= sizeof(buf)) {
79         pr("Can't handle path from %s to distribution sector %s, it's too long, sorry.\n",
80            xyas(sect.sct_x, sect.sct_y, player->cnum),
81            xyas(sect.sct_dist_x, sect.sct_dist_y, player->cnum));
82         return RET_FAIL;
83     }
84     if (!mapbuf)
85         mapbuf = malloc(WORLD_Y * MAPWIDTH(3));
86     if (!map) {
87         map = malloc(WORLD_Y * sizeof(char *));
88         if (map && mapbuf) {
89             for (i = 0; i < WORLD_Y; i++)
90                 map[i] = &mapbuf[MAPWIDTH(3) * i];
91         } else if (map) {
92             free(map);
93             map = NULL;
94         }
95     }
96     if (!mapbuf || !map) {
97         pr("Memory error, tell the deity.\n");
98         logerror("malloc failed in path\n");
99         return RET_FAIL;
100     }
101     pathrange(cx, cy, buf, 1, &absrange);
102     snxtsct_area(&ns, &absrange);
103     natp = getnatp(player->cnum);
104     xyrelrange(natp, &absrange, &relrange);
105     blankfill(mapbuf, &ns.range, 3);
106     for (pp = buf; *pp; ++pp) {
107         i = diridx(*pp);
108         if (CANT_HAPPEN(i == DIR_STOP))
109             break;
110         memcpy(&map[delty(&ns.range, cy)][deltx(&ns.range, cx) * 2],
111                routech[i], 3);
112         cx = xnorm(cx + diroff[i][0]);
113         cy = ynorm(cy + diroff[i][1]);
114     }
115     border(&relrange, "     ", " ");
116     while (nxtsct(&ns, &sect)) {
117         if (!player->owner)
118             continue;
119         map[ns.dy][ns.dx * 2 + 1] = dchr[sect.sct_type].d_mnem;
120     }
121     for (y = ns.range.ly, i = 0; i < ns.range.height; y++, i++) {
122         cy = yrel(natp, y);
123         pr("%4d %s %-4d\n", cy, map[i], cy);
124         if (y >= WORLD_Y)
125             y -= WORLD_Y;
126     }
127     border(&relrange, "     ", " ");
128     return RET_OK;
129 }