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