]> git.pond.sub.org Git - empserver/blob - src/lib/subs/satmap.c
Update copyright notice
[empserver] / src / lib / subs / satmap.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  *  satmap.c: Do a satellite map given an x,y location, effic and other
29  *
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  */
33
34 #include <config.h>
35
36 #include <stdlib.h>
37 #include "file.h"
38 #include "land.h"
39 #include "map.h"
40 #include "misc.h"
41 #include "nat.h"
42 #include "nsc.h"
43 #include "optlist.h"
44 #include "plane.h"
45 #include "player.h"
46 #include "prototypes.h"
47 #include "sect.h"
48 #include "ship.h"
49 #include "xy.h"
50
51 static char **rad;
52 static char *radbuf;
53
54 void
55 satmap(int x, int y, int eff, int range, int flags, int type)
56 {
57     struct sctstr sect;
58     struct shpstr ship;
59     struct lndstr land;
60     int count;
61     struct nstr_item ni;
62     struct nstr_sect ns;
63     int rx, ry;
64     int row;
65     int n;
66     int changed = 0;
67     long crackle;
68     signed char noise[100];
69     char selection[1024];
70
71     if (!eff)
72         return;
73
74     if (!radbuf)
75         radbuf = malloc(WORLD_Y * MAPWIDTH(1));
76     if (!rad) {
77         rad = malloc(WORLD_Y * sizeof(char *));
78         if (rad && radbuf) {
79             for (rx = 0; rx < WORLD_Y; rx++)
80                 rad[rx] = &radbuf[(WORLD_X + 1) * rx];
81         }
82     }
83
84     if (!radbuf || !rad) {
85         pr("Memory error in satmap, tell the deity.\n");
86         return;
87     }
88
89     range = range * (eff / 100.0);
90     pr("%s efficiency %d%%, max range %d\n",
91        xyas(x, y, player->cnum), eff, range);
92     memset(noise, 0, sizeof(noise));
93     if (eff < 100) {
94         pr("Some noise on the transmission...\n");
95         for (n = 0; n < (100 - eff); ++n)
96             noise[100 * n / (100 - eff)] = 1;
97     }
98
99     /* Have to convert to player coords, since it gets converted
100        back from there */
101     sprintf(selection, "@%s:%d", xyas(x, y, player->cnum), range);
102
103     if (type == EF_BAD || type == EF_SECTOR) {
104         if (type == EF_SECTOR)  /* Use ?conditionals */
105             snxtsct(&ns, selection);
106         else
107             snxtsct_dist(&ns, x, y, range);
108
109         blankfill(radbuf, &ns.range, 1);
110         if (flags & P_S) {
111             pr("Satellite sector report\n");
112             prdate();
113             sathead();
114         }
115         crackle = count = 0;
116         while (nxtsct(&ns, &sect)) {
117             if (++crackle == 100)
118                 crackle = 0;
119             if (noise[crackle])
120                 continue;
121             if (flags & P_S) {
122                 if (sect.sct_own && sect.sct_own != player->cnum) {
123                     satdisp_sect(&sect, (flags & P_I) ? 5 : 50);
124                     ++count;
125                     if (opt_HIDDEN)
126                         setcont(player->cnum, sect.sct_own, FOUND_FLY);
127                 }
128             }
129             if ((flags & P_I) ||
130                 sect.sct_type == SCT_WATER || sect.sct_type == SCT_MOUNT) {
131                 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
132             } else
133                 rad[ns.dy][ns.dx] = '?';
134             changed +=
135                 map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
136         }
137         if (changed)
138             writemap(player->cnum);
139         if (flags & P_S)
140             pr("  %d sectors\n\n", count);
141     }
142
143     if ((type == EF_BAD || type == EF_SHIP) &&
144         (flags & P_S || flags & P_I)) {
145         if (type == EF_SHIP)
146             snxtitem(&ni, EF_SHIP, selection, NULL);
147         else
148             snxtitem_dist(&ni, EF_SHIP, x, y, range);
149
150         crackle = count = 0;
151         if (flags & P_S) {
152             pr("Satellite ship report\n");
153             prdate();
154             pr(" own  shp# ship type                                  sector   eff\n");
155         }
156         while (nxtitem(&ni, &ship)) {
157             if (ship.shp_own == 0)
158                 continue;
159             if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
160                 ((flags & (P_S | P_I)) != (P_S | P_I)))
161                 continue;
162             if (++crackle == 100)
163                 crackle = 0;
164             if (noise[crackle])
165                 continue;
166             if (flags & P_S) {
167                 pr("%4d %4d %-16.16s %-25.25s ",
168                    ship.shp_own, ship.shp_uid,
169                    mchr[(int)ship.shp_type].m_name, ship.shp_name);
170                 prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
171                 pr("%3d%%\n", ship.shp_effic);
172                 ++count;
173                 if (opt_HIDDEN)
174                     setcont(player->cnum, ship.shp_own, FOUND_FLY);
175             }
176             /* If we are imaging *and* drawing the map */
177             if ((flags & P_I) && (type == EF_BAD)) {
178                 rx = deltx(&ns.range, ship.shp_x);
179                 ry = delty(&ns.range, ship.shp_y);
180                 /* &~0x20 makes it a cap letter */
181                 rad[ry][rx] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
182             }
183         }
184         if (flags & P_S)
185             pr("  %d ships\n\n", count);
186     }
187
188     if ((type == EF_BAD || type == EF_LAND) &&
189         (flags & P_S || flags & P_I)) {
190         if (type == EF_LAND)
191             snxtitem(&ni, EF_LAND, selection, NULL);
192         else
193             snxtitem_dist(&ni, EF_LAND, x, y, range);
194
195         crackle = count = 0;
196         if (flags & P_S) {
197             pr("Satellite unit report\n");
198             prdate();
199             pr(" own  lnd# unit type         sector   eff\n");
200         }
201         while (nxtitem(&ni, &land)) {
202             if (land.lnd_own == 0)
203                 continue;
204             if (lchr[(int)land.lnd_type].l_flags & L_SPY)
205                 continue;
206             if (!chance(land.lnd_effic / 20.0))
207                 continue;
208             if (++crackle == 100)
209                 crackle = 0;
210             if (noise[crackle])
211                 continue;
212             if (flags & P_S) {
213                 pr("%4d %4d %-16.16s ",
214                    land.lnd_own, land.lnd_uid,
215                    lchr[(int)land.lnd_type].l_name);
216                 prxy("%4d,%-4d", land.lnd_x, land.lnd_y, player->cnum);
217                 pr("%3d%%\n", land.lnd_effic);
218                 ++count;
219                 if (opt_HIDDEN)
220                     setcont(player->cnum, land.lnd_own, FOUND_FLY);
221             }
222             /* If we are imaging *and* drawing the map */
223             if ((flags & P_I) && (type == EF_BAD)) {
224                 rx = deltx(&ns.range, land.lnd_x);
225                 ry = delty(&ns.range, land.lnd_y);
226                 /* &~0x20 makes it a cap letter */
227                 rad[ry][rx] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
228             }
229         }
230         if (flags & P_S)
231             pr("  %d units\n\n", count);
232     }
233
234     /* Ok, have we made the map?  If so, display it */
235     if (type == EF_BAD) {
236         /*
237          * print out the map
238          * We have to make the center a '0' for ve
239          * ve needs a garbage line to terminate the map
240          */
241         rad[delty(&ns.range, y)][deltx(&ns.range, y)] = '0';
242
243         pr("Satellite radar report\n");
244         n = ns.range.height;
245         for (row = 0; row < n; row++)
246             pr("%s\n", rad[row]);
247         pr("\n(c) 1989 Imaginative Images Inc.\n");
248     }
249 }
250
251 void
252 sathead(void)
253 {
254     pr("                    sct rd  rl  def\n");
255     pr("   sect   type own  eff eff eff eff  civ  mil  shl  gun iron  pet  food\n");
256 }
257
258 void
259 satdisp_sect(struct sctstr *sp, int acc)
260 {
261     prxy("%4d,%-4d   ", sp->sct_x, sp->sct_y, player->cnum);
262     pr("%c  %3d  %3d %3d %3d %3d %4d %4d %4d %4d %4d %4d %5d\n",
263        dchr[sp->sct_type].d_mnem,
264        sp->sct_own, roundintby((int)sp->sct_effic, acc / 2),
265        roundintby((int)sp->sct_road, acc / 2),
266        opt_RAILWAYS ? !!sct_rail_track(sp) : roundintby(sp->sct_rail, acc / 2),
267        roundintby((int)sp->sct_defense, acc / 2),
268        roundintby(sp->sct_item[I_CIVIL], acc),
269        roundintby(sp->sct_item[I_MILIT], acc),
270        roundintby(sp->sct_item[I_SHELL], acc),
271        roundintby(sp->sct_item[I_GUN], acc),
272        roundintby(sp->sct_item[I_IRON], acc),
273        roundintby(sp->sct_item[I_PETROL], acc),
274        roundintby(sp->sct_item[I_FOOD], acc));
275     map_set(player->cnum, sp->sct_x, sp->sct_y, dchr[sp->sct_type].d_mnem,
276             0);
277 }
278
279 void
280 satdisp_units(coord x, coord y)
281 {
282     int first;
283     struct nstr_item ni;
284     struct shpstr ship;
285     struct lndstr land;
286
287     snxtitem_xy(&ni, EF_SHIP, x, y);
288     first = 1;
289     while (nxtitem(&ni, &ship)) {
290         if (ship.shp_own == 0)
291             continue;
292         if (mchr[(int)ship.shp_type].m_flags & M_SUB)
293             continue;
294         if (first) {
295             pr("\t own  shp# ship type                                  sector   eff\n");
296             first = 0;
297         }
298         pr("\t%4d %4d %-16.16s %-25.25s ",
299            ship.shp_own, ship.shp_uid,
300            mchr[(int)ship.shp_type].m_name, ship.shp_name);
301         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
302         pr("%3d%%\n", ship.shp_effic);
303     }
304
305     if (!first)
306         pr("\n");
307
308     snxtitem_xy(&ni, EF_LAND, x, y);
309     first = 1;
310
311     while (nxtitem(&ni, &land)) {
312         if (land.lnd_own == 0)
313             continue;
314         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
315             continue;
316         if (lchr[(int)land.lnd_type].l_flags & L_SPY)
317             continue;
318         if (!chance(land.lnd_effic / 20.0))
319             continue;
320
321         if (first) {
322             pr("\t own  lnd# unit type       sector   eff\n");
323             first = 0;
324         }
325
326         pr("\t%4d %4d %-16.16s ",
327            land.lnd_own, land.lnd_uid, lchr[(int)land.lnd_type].l_name);
328         prxy("%4d,%-4d ", land.lnd_x, land.lnd_y, player->cnum);
329         pr("%3d%%\n", land.lnd_effic);
330     }
331
332     if (!first)
333         pr("\n");
334 }