]> 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-2012, 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  *     Markus Armbruster, 2008-2011
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "path.h"
40
41 /*
42  * distribute <SECT> <DISTSECT|.|h>
43  */
44 int
45 dist(void)
46 {
47     struct sctstr sect, dsect, tsect;
48     struct nstr_sect nstr;
49     char *p;
50     double move_cost;
51     coord dstx, dsty;
52     char buf[1024];
53
54     if (!snxtsct(&nstr, player->argp[1]))
55         return RET_SYN;
56     while (nxtsct(&nstr, &sect)) {
57         if (!player->owner)
58             continue;
59         pr("%s at %s ", dchr[sect.sct_type].d_name,
60            xyas(nstr.x, nstr.y, player->cnum));
61         if ((sect.sct_dist_x != sect.sct_x) ||
62             (sect.sct_dist_y != sect.sct_y)) {
63             getsect(sect.sct_dist_x, sect.sct_dist_y, &tsect);
64             if (tsect.sct_own != player->cnum)
65                 pr("distributes to %s, not owned by you.\n",
66                    xyas(tsect.sct_x, tsect.sct_y, player->cnum));
67             else
68                 pr("distributes to %s. \n",
69                    xyas(tsect.sct_x, tsect.sct_y, player->cnum));
70         } else
71             pr("has no dist sector. \n");
72         p = getstarg(player->argp[2], "Distribution sector? ", buf);
73         if (!p)
74             return RET_SYN;
75         if (!*p)
76             continue;
77         if (!check_sect_ok(&sect))
78             continue;
79
80         if (*p == '.' || *p == 'h') {
81             dstx = sect.sct_x;
82             dsty = sect.sct_y;
83         } else if (!sarg_xy(p, &dstx, &dsty))
84             return RET_SYN;
85         if (!getsect(dstx, dsty, &dsect)) {
86             pr("Bad sector.\n");
87             return RET_FAIL;
88         }
89
90         if (dsect.sct_own != player->cnum)
91             pr("Warning: you don't own %s!\n",
92                xyas(dsect.sct_x, dsect.sct_y, player->cnum));
93
94         move_cost = path_find(sect.sct_x, sect.sct_y, dstx, dsty,
95                               player->cnum, MOB_MOVE);
96         if (move_cost < 0) {
97             pr("No owned path from %s to %s.\n",
98                xyas(dsect.sct_x, dsect.sct_y, player->cnum),
99                xyas(sect.sct_x, sect.sct_y, player->cnum));
100             continue;
101         }
102
103         if ((dsect.sct_x == sect.sct_x) && (dsect.sct_y == sect.sct_y)) {
104             pr("Distribution from and to %s %s terminated\n",
105                dchr[sect.sct_type].d_name,
106                xyas(sect.sct_x, sect.sct_y, player->cnum));
107         } else {
108             pr("%s %s now distributes to %s (cost %1.3f)\n",
109                dchr[sect.sct_type].d_name,
110                xyas(sect.sct_x, sect.sct_y, player->cnum),
111                xyas(dsect.sct_x, dsect.sct_y, player->cnum), move_cost);
112         }
113         pr("\n");
114         /* Only change and write out if we are really changing where it
115            distributes to.  Otherwise, it's a waste of time (since nothing
116            changed.) */
117         if ((sect.sct_dist_x != dsect.sct_x) ||
118             (sect.sct_dist_y != dsect.sct_y)) {
119             sect.sct_dist_x = dsect.sct_x;
120             sect.sct_dist_y = dsect.sct_y;
121             putsect(&sect);
122         }
123     }
124     return RET_OK;
125 }