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