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