]> git.pond.sub.org Git - empserver/blob - src/lib/commands/skyw.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / commands / skyw.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  *  skyw.c: Look at satellites in the sky.
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  */
32
33 #include <config.h>
34
35 #include "commands.h"
36 #include "optlist.h"
37 #include "plane.h"
38
39 #define TSIZE   200
40
41 struct sky {
42     struct sky *s_next;
43     int s_spotted;
44     struct plnstr s_sat;
45 };
46
47 static int showsat(struct sky **skypp, int x, int y);
48
49 /*
50  * format: skywatch [<SECTS>]
51  */
52 int
53 skyw(void)
54 {
55     struct sctstr sect;
56     struct nstr_sect nstr;
57     struct sky *skyp;
58     struct sky *list[TSIZE];
59     int i, n;
60     int vrange, see;
61     int x, y, dx, dy, dxmax;
62     int nsat = 0;
63     double tech;
64     struct nstr_item ni;
65
66     if (!snxtsct(&nstr, player->argp[1]))
67         return RET_SYN;
68     for (i = 0; i < TSIZE; i++)
69         list[i] = NULL;
70     skyp = malloc(sizeof(*skyp));
71     snxtitem_all(&ni, EF_PLANE);
72     while (nxtitem(&ni, &skyp->s_sat)) {
73         if (!skyp->s_sat.pln_own)
74             continue;
75         if (!pln_is_in_orbit(&skyp->s_sat))
76             continue;
77         getsect(skyp->s_sat.pln_x, skyp->s_sat.pln_y, &sect);
78         n = scthash(skyp->s_sat.pln_x, skyp->s_sat.pln_y, TSIZE);
79         skyp->s_spotted = 0;
80         skyp->s_next = list[n];
81         list[n] = skyp;
82         skyp = malloc(sizeof(*skyp));
83         nsat++;
84     }
85     /* get that last one! */
86     free(skyp);
87     pr("- = [ Skywatch report for %s ] = -\n", cname(player->cnum));
88     pr("  Country            Satellite     Location\n");
89     tech = tfact(player->cnum, 1.0);
90     while (nxtsct(&nstr, &sect) && nsat) {
91         if (sect.sct_own != player->cnum)
92             continue;
93         see = sect.sct_type == SCT_RADAR ? 14 : 4;
94         vrange = (int)(sect.sct_effic / 100.0 * see * tech);
95         if (vrange < 1)
96             vrange = 1;
97         for (dy = -vrange; dy <= vrange; dy++) {
98             y = ynorm(sect.sct_y + dy);
99             dxmax = 2 * vrange - abs(dy);
100             for (dx = -dxmax; dx <= dxmax; dx += 2) {
101                 x = xnorm(sect.sct_x + dx);
102                 n = scthash(x, y, TSIZE);
103                 if (!list[n])
104                     continue;
105                 nsat -= showsat(&list[n], x, y);
106             }
107         }
108     }
109     /* free up the sky structs calloc'ed above */
110     for (i = 0; i < TSIZE; i++) {
111         while (NULL != (skyp = list[i])) {
112             list[i] = skyp->s_next;
113             free(skyp);
114         }
115     }
116     return RET_OK;
117 }
118
119 static int
120 showsat(struct sky **skypp, int x, int y)
121 {
122     struct sky *skyp;
123     struct sky *todelete = NULL;
124     struct sky **prev;
125     int nsat = 0;
126
127     prev = NULL;
128     skyp = *skypp;
129     prev = skypp;
130     do {
131         /* we delete it, we free it. */
132         if (todelete) {
133             free(todelete);
134             todelete = NULL;
135         }
136         if (skyp->s_sat.pln_x != x || skyp->s_sat.pln_y != y) {
137             prev = &(*prev)->s_next;
138             continue;
139         }
140         pr(" %12.12s (#%3d) %s @ %s\n",
141            cname(skyp->s_sat.pln_own), skyp->s_sat.pln_own,
142            prplane(&skyp->s_sat), xyas(x, y, player->cnum));
143         if (opt_HIDDEN) {
144             /* FOUND_COAST should probably be changed to FOUND_SKY -KHS */
145             setcont(player->cnum, skyp->s_sat.pln_own, FOUND_COAST);
146         }
147         *prev = skyp->s_next;
148         todelete = skyp;
149         nsat++;
150     } while (NULL != (skyp = skyp->s_next));
151     /* check that last one! */
152     if (todelete)
153         free(todelete);
154     return nsat;
155 }