]> git.pond.sub.org Git - empserver/blob - src/lib/commands/skyw.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / skyw.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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  *  skyw.c: Look at satellites in the sky.
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "sect.h"
37 #include "plane.h"
38 #include "nat.h"
39 #include "xy.h"
40 #include "nsc.h"
41 #include "file.h"
42 #include "commands.h"
43 #include "optlist.h"
44
45 #define TSIZE   200
46
47 struct sky {
48     struct sky *s_next;
49     int s_spotted;
50     struct plnstr s_sat;
51 };
52
53 static int showsat(struct sky **skypp, int x, int y);
54
55 /*
56  * format: skywatch [<SECTS>]
57  */
58 int
59 skyw(void)
60 {
61     struct sctstr sect;
62     struct nstr_sect nstr;
63     struct sky *skyp;
64     struct sky *list[TSIZE];
65     int i, k, j, n;
66     int vrange, see;
67     int x, y;
68     int mink, minj, maxk, maxj;
69     int nsat = 0;
70     float tech;
71     struct nstr_item ni;
72
73     if (snxtsct(&nstr, player->argp[1]) == 0)
74         return RET_SYN;
75     for (i = 0; i < TSIZE; i++)
76         list[i] = 0;
77     skyp = malloc(sizeof(*skyp));
78     snxtitem_all(&ni, EF_PLANE);
79     while (nxtitem(&ni, &skyp->s_sat)) {
80         if (!skyp->s_sat.pln_own)
81             continue;
82         if (!(skyp->s_sat.pln_flags & PLN_LAUNCHED))
83             continue;
84         getsect(skyp->s_sat.pln_x, skyp->s_sat.pln_y, &sect);
85         n = scthash(skyp->s_sat.pln_x, skyp->s_sat.pln_y, TSIZE);
86         skyp->s_spotted = 0;
87         skyp->s_next = list[n];
88         list[n] = skyp;
89         skyp = malloc(sizeof(*skyp));
90         nsat++;
91     }
92     /* get that last one! */
93     free(skyp);
94     pr("- = [ Skywatch report for %s ] = -\n", cname(player->cnum));
95     pr(" %18s%20s        %s\n", "Country", "Satellite", "Location");
96     tech = tfact(player->cnum, 1.0);
97     while (nxtsct(&nstr, &sect) && nsat) {
98         if (sect.sct_own != player->cnum)
99             continue;
100         see = sect.sct_type == SCT_RADAR ? 14 : 4;
101         vrange = (int)(sect.sct_effic / 100.0 * see * tech);
102         if (vrange < 1)
103             vrange = 1;
104         maxk = vrange;
105         maxj = vrange * 2;
106         vrange *= vrange;
107         mink = -maxk;
108         minj = -maxj;
109         for (j = minj; j <= maxj && nsat; j++) {
110             x = xnorm(sect.sct_x + j);
111             for (k = mink; k <= maxk && nsat; k++) {
112                 if ((j + k) & 01)
113                     continue;
114                 /* quick range check to save time... */
115                 if (vrange < (j * j + 3 * k * k) / 4)
116                     continue;
117                 y = ynorm(sect.sct_y + k);
118                 n = scthash(x, y, TSIZE);
119                 if (list[n] == 0)
120                     continue;
121                 nsat -= showsat(&list[n], x, y);
122             }
123         }
124     }
125     /* free up the sky structs calloc'ed above */
126     for (i = 0; i < TSIZE; i++) {
127         while (NULL != (skyp = list[i])) {
128             list[i] = skyp->s_next;
129             free(skyp);
130         }
131     }
132     return RET_OK;
133 }
134
135 static int
136 showsat(struct sky **skypp, int x, int y)
137 {
138     struct sky *skyp;
139     struct sky *todelete = 0;
140     struct sky **prev;
141     struct plchrstr *pcp;
142     s_char *name;
143     int nsat = 0;
144
145     prev = 0;
146     skyp = *skypp;
147     prev = skypp;
148     do {
149         /* we delete it, we free it. */
150         if (todelete) {
151             free(todelete);
152             todelete = 0;
153         }
154         if (skyp->s_sat.pln_x != x || skyp->s_sat.pln_y != y) {
155             prev = &(*prev)->s_next;
156             continue;
157         }
158         pcp = &plchr[(int)skyp->s_sat.pln_type];
159         name = pcp->pl_name;
160         pr(" %12.12s (#%d) %20s            %s\n",
161            cname(skyp->s_sat.pln_own), skyp->s_sat.pln_own,
162            name, xyas(x, y, player->cnum));
163         if (opt_HIDDEN) {
164             /* FOUND_COAST should probably be changed to FOUND_SKY -KHS */
165             setcont(player->cnum, skyp->s_sat.pln_own, FOUND_COAST);
166         }
167         *prev = skyp->s_next;
168         todelete = skyp;
169         nsat++;
170     } while (NULL != (skyp = skyp->s_next));
171     /* check that last one! */
172     if (todelete)
173         free(todelete);
174     return (nsat);
175 }