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