]> git.pond.sub.org Git - empserver/blob - src/lib/commands/skyw.c
Update copyright notice
[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, k, j, n;
61     int vrange, see;
62     int x, y;
63     int mink, minj, maxk, maxj;
64     int nsat = 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     skyp = malloc(sizeof(*skyp));
73     snxtitem_all(&ni, EF_PLANE);
74     while (nxtitem(&ni, &skyp->s_sat)) {
75         if (!skyp->s_sat.pln_own)
76             continue;
77         if (!pln_is_in_orbit(&skyp->s_sat))
78             continue;
79         getsect(skyp->s_sat.pln_x, skyp->s_sat.pln_y, &sect);
80         n = scthash(skyp->s_sat.pln_x, skyp->s_sat.pln_y, TSIZE);
81         skyp->s_spotted = 0;
82         skyp->s_next = list[n];
83         list[n] = skyp;
84         skyp = malloc(sizeof(*skyp));
85         nsat++;
86     }
87     /* get that last one! */
88     free(skyp);
89     pr("- = [ Skywatch report for %s ] = -\n", cname(player->cnum));
90     pr(" %18s%20s        %s\n", "Country", "Satellite", "Location");
91     tech = tfact(player->cnum, 1.0);
92     while (nxtsct(&nstr, &sect) && nsat) {
93         if (sect.sct_own != player->cnum)
94             continue;
95         see = sect.sct_type == SCT_RADAR ? 14 : 4;
96         vrange = (int)(sect.sct_effic / 100.0 * see * tech);
97         if (vrange < 1)
98             vrange = 1;
99         maxk = vrange;
100         maxj = vrange * 2;
101         vrange *= vrange;
102         mink = -maxk;
103         minj = -maxj;
104         for (j = minj; j <= maxj && nsat; j++) {
105             x = xnorm(sect.sct_x + j);
106             for (k = mink; k <= maxk && nsat; k++) {
107                 if ((j + k) & 01)
108                     continue;
109                 /* quick range check to save time... */
110                 if (vrange < (j * j + 3 * k * k) / 4)
111                     continue;
112                 y = ynorm(sect.sct_y + k);
113                 n = scthash(x, y, TSIZE);
114                 if (!list[n])
115                     continue;
116                 nsat -= showsat(&list[n], x, y);
117             }
118         }
119     }
120     /* free up the sky structs calloc'ed above */
121     for (i = 0; i < TSIZE; i++) {
122         while (NULL != (skyp = list[i])) {
123             list[i] = skyp->s_next;
124             free(skyp);
125         }
126     }
127     return RET_OK;
128 }
129
130 static int
131 showsat(struct sky **skypp, int x, int y)
132 {
133     struct sky *skyp;
134     struct sky *todelete = NULL;
135     struct sky **prev;
136     struct plchrstr *pcp;
137     char *name;
138     int nsat = 0;
139
140     prev = NULL;
141     skyp = *skypp;
142     prev = skypp;
143     do {
144         /* we delete it, we free it. */
145         if (todelete) {
146             free(todelete);
147             todelete = NULL;
148         }
149         if (skyp->s_sat.pln_x != x || skyp->s_sat.pln_y != y) {
150             prev = &(*prev)->s_next;
151             continue;
152         }
153         pcp = &plchr[(int)skyp->s_sat.pln_type];
154         name = pcp->pl_name;
155         pr(" %12.12s (#%d) %20s            %s\n",
156            cname(skyp->s_sat.pln_own), skyp->s_sat.pln_own,
157            name, xyas(x, y, player->cnum));
158         if (opt_HIDDEN) {
159             /* FOUND_COAST should probably be changed to FOUND_SKY -KHS */
160             setcont(player->cnum, skyp->s_sat.pln_own, FOUND_COAST);
161         }
162         *prev = skyp->s_next;
163         todelete = skyp;
164         nsat++;
165     } while (NULL != (skyp = skyp->s_next));
166     /* check that last one! */
167     if (todelete)
168         free(todelete);
169     return nsat;
170 }