]> git.pond.sub.org Git - empserver/blob - src/lib/commands/dist.c
Update copyright notice.
[empserver] / src / lib / commands / dist.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  *  dist.c: Name distribution sector for a given range of sectors
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1993 (rewritten) 
33  *     Steve McClure, 1998
34  */
35
36 #include "misc.h"
37 #include "player.h"
38 #include "xy.h"
39 #include "sect.h"
40 #include "nsc.h"
41 #include "nat.h"
42 #include "file.h"
43 #include "path.h"
44 #include "commands.h"
45
46 /*
47  * distribute <SECT> <DISTSECT|.|h>
48  */
49 int
50 dist(void)
51 {
52     struct sctstr sect, dsect, tsect;
53     struct nstr_sect nstr;
54     s_char *path, *p;
55     double move_cost = 0.0;
56     coord dstx, dsty;
57     s_char buf[1024];
58
59     if (!snxtsct(&nstr, player->argp[1]))
60         return RET_SYN;
61     while (!player->aborted && nxtsct(&nstr, &sect)) {
62         if (!player->owner)
63             continue;
64         pr("%s at %s ", dchr[sect.sct_type].d_name,
65            xyas(nstr.x, nstr.y, player->cnum));
66         if ((sect.sct_dist_x != sect.sct_x) ||
67             (sect.sct_dist_y != sect.sct_y)) {
68             getsect(sect.sct_dist_x, sect.sct_dist_y, &tsect);
69             if (tsect.sct_own != player->cnum)
70                 pr("distributes to %s, not owned by you.\n",
71                    xyas(tsect.sct_x, tsect.sct_y, player->cnum));
72             else
73                 pr("distributes to %s. \n",
74                    xyas(tsect.sct_x, tsect.sct_y, player->cnum));
75         } else
76             pr("has no dist sector. \n");
77         p = getstarg(player->argp[2], "Distribution sector? ", buf);
78         if (p && (*p == 0))
79             continue;
80
81         if (!check_sect_ok(&sect))
82             continue;
83
84         if (p && (*p != '.') && (*p != 'h') && (!sarg_xy(p, &dstx, &dsty)))
85             return RET_SYN;
86
87         if (p && ((*p == '.') || (*p == 'h'))) {
88             dstx = sect.sct_x;
89             dsty = sect.sct_y;
90         }
91
92         if (!getsect(dstx, dsty, &dsect)) {
93             pr("Bad sector.\n");
94             return RET_FAIL;
95         }
96
97         if (dsect.sct_own != player->cnum)
98             pr("Warning: you don't own %s!\n",
99                xyas(dsect.sct_x, dsect.sct_y, player->cnum));
100
101         path = BestDistPath(buf, &sect, &dsect, &move_cost, MOB_ROAD);
102
103         if (path == (s_char *)0) {
104             pr("No owned path from %s to %s.\n",
105                xyas(dsect.sct_x, dsect.sct_y, player->cnum),
106                xyas(sect.sct_x, sect.sct_y, player->cnum));
107             continue;
108         }
109
110         if ((dsect.sct_x == sect.sct_x) && (dsect.sct_y == sect.sct_y)) {
111             pr("Distribution from and to %s %s terminated\n",
112                dchr[sect.sct_type].d_name,
113                xyas(sect.sct_x, sect.sct_y, player->cnum));
114         } else {
115             pr("%s %s now distributes to %s (cost %1.3f)\n",
116                dchr[sect.sct_type].d_name,
117                xyas(sect.sct_x, sect.sct_y, player->cnum),
118                xyas(dsect.sct_x, dsect.sct_y, player->cnum), move_cost);
119         }
120         pr("\n");
121         /* Only change and write out if we are really changing where it
122            distributes to.  Otherwise, it's a waste of time (since nothing
123            changed.) */
124         if ((sect.sct_dist_x != dsect.sct_x) ||
125             (sect.sct_dist_y != dsect.sct_y)) {
126             sect.sct_dist_x = dsect.sct_x;
127             sect.sct_dist_y = dsect.sct_y;
128             putsect(&sect);
129         }
130     }
131     return RET_OK;
132 }