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