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