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