]> git.pond.sub.org Git - empserver/blob - src/lib/commands/coas.c
d8a2dc190b69edc711c3e82b4c31dd6b076b9764
[empserver] / src / lib / commands / coas.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  coas.c: Look at all the ships in the world
28  *
29  *  Known contributors to this file:
30  *     Keith Muller, 1983
31  *     Dave Pare, 1986 (rewrite)
32  *     Markus Armbruster, 2010
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, n;
63     int vrange, see;
64     int x, y, dx, dy, dxmax;
65     int nship = 0;
66     double tech;
67     struct nstr_item ni;
68
69     if (!snxtsct(&nstr, player->argp[1]))
70         return RET_SYN;
71     for (i = 0; i < TSIZE; i++)
72         list[i] = NULL;
73     cp = malloc(sizeof(*cp));
74     snxtitem_all(&ni, EF_SHIP);
75     while (nxtitem(&ni, &cp->c_shp)) {
76         if (cp->c_shp.shp_own == 0 || cp->c_shp.shp_own == player->cnum)
77             continue;
78         /*
79          * don't bother putting subs in the table...
80          * unless they're in a sector you own (harbor or such)
81          */
82         getsect(cp->c_shp.shp_x, cp->c_shp.shp_y, &sect);
83         if ((mchr[(int)cp->c_shp.shp_type].m_flags & M_SUB) &&
84             (sect.sct_own != player->cnum))
85             continue;
86         n = scthash(cp->c_shp.shp_x, cp->c_shp.shp_y, TSIZE);
87         cp->c_spotted = 0;
88         cp->c_number = i;
89         cp->c_next = list[n];
90         list[n] = cp;
91         cp = malloc(sizeof(*cp));
92         nship++;
93     }
94     /* get that last one! */
95     free(cp);
96     pr("- = [ Coastwatch report for %s ] = -\n", cname(player->cnum));
97     pr("  Country            Ship          Location\n");
98     tech = tfact(player->cnum, 1.0);
99     while (nxtsct(&nstr, &sect) && nship) {
100         if (sect.sct_own != player->cnum)
101             continue;
102         see = sect.sct_type == SCT_RADAR ? 14 : 4;
103         vrange = (int)(sect.sct_effic / 100.0 * see * tech);
104         if (vrange < 1)
105             vrange = 1;
106         for (dy = -vrange; dy <= vrange; dy++) {
107             y = ynorm(sect.sct_y + dy);
108             dxmax = 2 * vrange - abs(dy);
109             for (dx = -dxmax; dx <= dxmax; dx += 2) {
110                 x = xnorm(sect.sct_x + dx);
111                 n = scthash(x, y, TSIZE);
112                 if (!list[n])
113                     continue;
114                 nship -= showship(&list[n], x, y);
115             }
116         }
117     }
118     /* free up the coast structs calloc'ed above */
119     for (i = 0; i < TSIZE; i++) {
120         while (NULL != (cp = list[i])) {
121             list[i] = cp->c_next;
122             free(cp);
123         }
124     }
125     return RET_OK;
126 }
127
128 static int
129 showship(struct coast **cpp, int x, int y)
130 {
131     struct coast *cp;
132     struct coast *todelete = NULL;
133     struct coast **prev;
134     int nship = 0;
135
136     prev = NULL;
137     cp = *cpp;
138     prev = cpp;
139     do {
140         /* we delete it, we free it. */
141         if (todelete) {
142             free(todelete);
143             todelete = NULL;
144         }
145         if (cp->c_shp.shp_x != x || cp->c_shp.shp_y != y) {
146             prev = &(*prev)->c_next;
147             continue;
148         }
149         pr(" %12.12s (#%3d) %s @ %s\n",
150            cname(cp->c_shp.shp_own), cp->c_shp.shp_own,
151            prship(&cp->c_shp), xyas(x, y, player->cnum));
152         if (opt_HIDDEN) {
153             setcont(player->cnum, cp->c_shp.shp_own, FOUND_COAST);
154         }
155         *prev = cp->c_next;
156         todelete = cp;
157         nship++;
158     } while (NULL != (cp = cp->c_next));
159     /* check that last one! */
160     if (todelete)
161         free(todelete);
162     return nship;
163 }