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