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