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