]> git.pond.sub.org Git - empserver/blob - src/lib/commands/coas.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / coas.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  *  coas.c: Look at all the ships in the world
29  * 
30  *  Known contributors to this file:
31  *     Keith Muller, 1983
32  *     Dave Pare, 1986 (rewrite)
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "sect.h"
38 #include "ship.h"
39 #include "nat.h"
40 #include "xy.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "commands.h"
44 #include "optlist.h"
45
46 #define TSIZE   200
47
48 struct coast {
49     struct coast *c_next;
50     int c_spotted;
51     int c_number;
52     struct shpstr c_shp;
53 };
54
55 static int showship(struct coast **cpp, int x, int y);
56
57 /*
58  * format: coastwatch [<SECTS>]
59  */
60 int
61 coas(void)
62 {
63     struct sctstr sect;
64     struct nstr_sect nstr;
65     struct coast *cp;
66     struct coast *list[TSIZE];
67     int i, k, j, n;
68     int vrange, see;
69     int x, y;
70     int mink, minj, maxk, maxj;
71     int nship = 0;
72     float tech;
73     struct nstr_item ni;
74
75     if (snxtsct(&nstr, player->argp[1]) == 0)
76         return RET_SYN;
77     for (i = 0; i < TSIZE; i++)
78         list[i] = 0;
79     cp = malloc(sizeof(*cp));
80     snxtitem_all(&ni, EF_SHIP);
81     while (nxtitem(&ni, &cp->c_shp)) {
82         if (cp->c_shp.shp_own == 0 || cp->c_shp.shp_own == player->cnum)
83             continue;
84         /*
85          * don't bother putting subs in the table...
86          * unless they're in a sector you own (harbor or such)
87          */
88         getsect(cp->c_shp.shp_x, cp->c_shp.shp_y, &sect);
89         if ((mchr[(int)cp->c_shp.shp_type].m_flags & M_SUB) &&
90             (sect.sct_own != player->cnum))
91             continue;
92         n = scthash(cp->c_shp.shp_x, cp->c_shp.shp_y, TSIZE);
93         cp->c_spotted = 0;
94         cp->c_number = i;
95         cp->c_next = list[n];
96         list[n] = cp;
97         cp = malloc(sizeof(*cp));
98         nship++;
99     }
100     /* get that last one! */
101     free(cp);
102     pr("- = [ Coastwatch report for %s ] = -\n", cname(player->cnum));
103     pr("  Country            Ship          Location\n");
104     tech = tfact(player->cnum, 1.0);
105     while (nxtsct(&nstr, &sect) && nship) {
106         if (sect.sct_own != player->cnum)
107             continue;
108         see = sect.sct_type == SCT_RADAR ? 14 : 4;
109         vrange = (int)(sect.sct_effic / 100.0 * see * tech);
110         if (vrange < 1)
111             vrange = 1;
112         maxk = vrange;
113         maxj = vrange * 2;
114         vrange *= vrange;
115         mink = -maxk;
116         minj = -maxj;
117         for (j = minj; j <= maxj && nship; j++) {
118             x = xnorm(sect.sct_x + j);
119             for (k = mink; k <= maxk && nship; k++) {
120                 if ((j + k) & 01)
121                     continue;
122                 /* quick range check to save time... */
123                 if (vrange < (j * j + 3 * k * k) / 4)
124                     continue;
125                 y = ynorm(sect.sct_y + k);
126                 n = scthash(x, y, TSIZE);
127                 if (list[n] == 0)
128                     continue;
129                 nship -= showship(&list[n], x, y);
130             }
131         }
132     }
133     /* free up the coast structs calloc'ed above */
134     for (i = 0; i < TSIZE; i++) {
135         while (NULL != (cp = list[i])) {
136             list[i] = cp->c_next;
137             free(cp);
138         }
139     }
140     return RET_OK;
141 }
142
143 static int
144 showship(struct coast **cpp, int x, int y)
145 {
146     struct coast *cp;
147     struct coast *todelete = 0;
148     struct coast **prev;
149     int nship = 0;
150
151     prev = 0;
152     cp = *cpp;
153     prev = cpp;
154     do {
155         /* we delete it, we free it. */
156         if (todelete) {
157             free(todelete);
158             todelete = 0;
159         }
160         if (cp->c_shp.shp_x != x || cp->c_shp.shp_y != y) {
161             prev = &(*prev)->c_next;
162             continue;
163         }
164         pr(" %12.12s (#%3d) %s @ %s\n",
165            cname(cp->c_shp.shp_own), cp->c_shp.shp_own,
166            prship(&cp->c_shp), xyas(x, y, player->cnum));
167         if (opt_HIDDEN) {
168             setcont(player->cnum, cp->c_shp.shp_own, FOUND_COAST);
169         }
170         *prev = cp->c_next;
171         todelete = cp;
172         nship++;
173     } while (NULL != (cp = cp->c_next));
174     /* check that last one! */
175     if (todelete)
176         free(todelete);
177     return (nship);
178 }