]> 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-2004, 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  *  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
35 #include "misc.h"
36 #include "player.h"
37 #include "xy.h"
38 #include "sect.h"
39 #include "ship.h"
40 #include "land.h"
41 #include "plane.h"
42 #include "var.h"
43 #include "nsc.h"
44 #include "nat.h"
45 #include "file.h"
46 #include "prototypes.h"
47 #include "optlist.h"
48
49 static s_char **rad;
50 static s_char *radbuf;
51
52 void
53 satmap(int x, int y, int eff, int range, int flags, int type)
54 {
55     int acc;
56     struct sctstr sect;
57     struct shpstr ship;
58     struct lndstr land;
59     int count;
60     struct nstr_item ni;
61     struct nstr_sect ns;
62     int rx, ry;
63     int row;
64     int n;
65     int changed = 0;
66     long crackle;
67     s_char noise[100];
68     s_char selection[1024];
69
70     if (!eff)
71         return;
72
73     if (!radbuf)
74         radbuf = (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) *
75                                   sizeof(s_char));
76     if (!rad) {
77         rad = (s_char **)malloc(WORLD_Y * sizeof(s_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", xyas(x, y, player->cnum),
91        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((s_char *)radbuf, &ns.range, 1);
110         if (flags & P_S) {
111             pr("Satellite sector report\n");
112             prdate();
113             sathead();
114             acc = (flags & P_I) ? 5 : 50;
115         }
116         crackle = count = 0;
117         while (nxtsct(&ns, &sect)) {
118             if (++crackle == 100)
119                 crackle = 0;
120             if (noise[crackle])
121                 continue;
122             if (flags & P_S) {
123                 if (sect.sct_own && sect.sct_own != player->cnum) {
124                     satdisp(&sect, acc, 0);
125                     ++count;
126                 }
127                 if (opt_HIDDEN) {
128                     setcont(player->cnum, sect.sct_own, FOUND_FLY);
129                 }
130             }
131             if ((flags & P_I) ||
132                 sect.sct_type == SCT_WATER || sect.sct_type == SCT_MOUNT) {
133                 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
134             } else
135                 rad[ns.dy][ns.dx] = '?';
136             changed +=
137                 map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
138         }
139         if (changed)
140             writemap(player->cnum);
141         if (flags & P_S)
142             pr("  %d sectors\n\n", count);
143     }
144
145     if ((type == EF_BAD || type == EF_SHIP) &&
146         (flags & P_S || flags & P_I)) {
147         if (type == EF_SHIP)
148             snxtitem(&ni, EF_SHIP, selection);
149         else
150             snxtitem_dist(&ni, EF_SHIP, x, y, range);
151
152         crackle = count = 0;
153         if (flags & P_S) {
154             pr("Satellite ship report\n");
155             prdate();
156             if (opt_SHIPNAMES) {
157                 pr(" own  shp# ship type                                  sector   eff\n");
158             } else {
159                 pr(" own  shp# ship type         sector   eff\n");
160             }
161         }
162         while (nxtitem(&ni, &ship)) {
163             if (ship.shp_own == 0)
164                 continue;
165             if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
166                 ((flags & (P_S | P_I)) != (P_S | P_I)))
167                 continue;
168             if (++crackle == 100)
169                 crackle = 0;
170             if (noise[crackle])
171                 continue;
172             if (flags & P_S) {
173                 pr("%4d %4d %-16.16s ",
174                    ship.shp_own, ship.shp_uid,
175                    mchr[(int)ship.shp_type].m_name);
176                 if (opt_SHIPNAMES)
177                     pr("%-25.25s ", ship.shp_name);
178                 prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
179                 pr("%3d%%\n", ship.shp_effic);
180                 ++count;
181                 if (opt_HIDDEN) {
182                     setcont(player->cnum, ship.shp_own, FOUND_FLY);
183                 }
184             }
185             /* If we are imaging *and* drawing the map */
186             if ((flags & P_I) && (type == EF_BAD)) {
187                 /* Figure out where to put the ship */
188                 /* First, figure out the distance from the two */
189                 rx = diffx((int)ship.shp_x, x);
190                 ry = diffy((int)ship.shp_y, y);
191                 /* Next, determine which direction to add it to the center */
192                 /* We can only do this if imaging and we have gotten the center
193                    up above by imaging the sectors. */
194                 rx = deltax(x, ns.range.lx) + rx;
195                 ry = deltay(y, ns.range.ly) + ry;
196                 /* &~0x20 makes it a cap letter */
197                 rad[ry][rx] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
198             }
199         }
200         if (flags & P_S)
201             pr("  %d ships\n\n", count);
202     }
203
204     if ((type == EF_BAD || type == EF_LAND) &&
205         (flags & P_S || flags & P_I)) {
206         if (type == EF_LAND)
207             snxtitem(&ni, EF_LAND, selection);
208         else
209             snxtitem_dist(&ni, EF_LAND, x, y, range);
210
211         crackle = count = 0;
212         if (flags & P_S) {
213             pr("Satellite unit report\n");
214             prdate();
215             pr(" own  lnd# unit type         sector   eff\n");
216         }
217         while (nxtitem(&ni, &land)) {
218             if (land.lnd_own == 0)
219                 continue;
220             if (!chance((double)land.lnd_effic / 20.0))
221                 continue;
222             if (++crackle == 100)
223                 crackle = 0;
224             if (noise[crackle])
225                 continue;
226             if (flags & P_S) {
227                 pr("%4d %4d %-16.16s ",
228                    land.lnd_own, land.lnd_uid,
229                    lchr[(int)land.lnd_type].l_name);
230                 prxy("%4d,%-4d", land.lnd_x, land.lnd_y, player->cnum);
231                 pr("%3d%%\n", land.lnd_effic);
232                 ++count;
233             }
234             /* If we are imaging *and* drawing the map */
235             if ((flags & P_I) && (type == EF_BAD)) {
236                 /* Figure out where to put the unit */
237                 /* First, figure out the distance from the two */
238                 rx = diffx((int)land.lnd_x, x);
239                 ry = diffy((int)land.lnd_y, y);
240                 /* Next, determine which direction to add it to the center */
241                 /* We can only do this if imaging and we have gotten the center
242                    up above by imaging the sectors. */
243                 rx = deltax(x, ns.range.lx) + rx;
244                 ry = deltay(y, ns.range.ly) + ry;
245                 /* &~0x20 makes it a cap letter */
246                 rad[ry][rx] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
247             }
248         }
249         if (flags & P_S)
250             pr("  %d units\n\n", count);
251     }
252
253     /* Ok, have we made the map?  If so, display it */
254     if (type == EF_BAD) {
255         /*
256          * print out the map
257          * We have to make the center a '0' for ve
258          * ve needs a garbage line to terminate the map
259          */
260         rad[deltay(y, ns.range.ly)][deltax(x, ns.range.lx)] = '0';
261
262         pr("Satellite radar report\n");
263 #ifdef HAY
264         /* This is wrong for small, hitech worlds. */
265         n = deltay(ns.range.hy, ns.range.ly);
266 #else
267         /* This is already available, so why not use it. */
268         n = ns.range.height;
269 #endif
270         for (row = 0; row < n; row++)
271             pr("%s\n", rad[row]);
272         pr("\n(c) 1989 Imaginative Images Inc.\n");
273     }
274 }
275
276 void
277 sathead(void)
278 {
279     pr("                    sct rd  rl  def\n");
280     pr("   sect   type own  eff eff eff eff  civ  mil  shl  gun iron  pet  food\n");
281 }
282
283 void
284 satdisp(struct sctstr *sp, int acc, int showstuff)
285 {
286     int first;
287     struct nstr_item ni;
288     struct shpstr ship;
289     struct lndstr land;
290
291     prxy("%4d,%-4d   ", sp->sct_x, sp->sct_y, player->cnum);
292     pr("%c  %3d  %3d %3d %3d %3d %4d %4d %4d %4d %4d %4d %5d\n",
293        dchr[sp->sct_type].d_mnem,
294        sp->sct_own, roundintby((int)sp->sct_effic, acc / 2),
295        roundintby((int)sp->sct_road, acc / 2),
296        roundintby((int)sp->sct_rail, acc / 2),
297        roundintby((int)sp->sct_defense, acc / 2),
298        roundintby(sp->sct_item[I_CIVIL], acc),
299        roundintby(sp->sct_item[I_MILIT], acc),
300        roundintby(sp->sct_item[I_SHELL], acc),
301        roundintby(sp->sct_item[I_GUN], acc),
302        roundintby(sp->sct_item[I_IRON], acc),
303        roundintby(sp->sct_item[I_PETROL], acc),
304        roundintby(sp->sct_item[I_FOOD], acc));
305     map_set(player->cnum, sp->sct_x, sp->sct_y, dchr[sp->sct_type].d_mnem,
306             0);
307     if (!showstuff)
308         return;
309     snxtitem_xy(&ni, EF_SHIP, sp->sct_x, sp->sct_y);
310     first = 1;
311     while (nxtitem(&ni, &ship)) {
312         if (ship.shp_own == 0)
313             continue;
314         if (mchr[(int)ship.shp_type].m_flags & M_SUB)
315             continue;
316         if (first) {
317             if (opt_SHIPNAMES) {
318                 pr("\t own  shp# ship type                                  sector   eff\n");
319             } else {
320                 pr("\t own  shp# ship type         sector   eff\n");
321             }
322             first = 0;
323         }
324         pr("\t%4d %4d %-16.16s ", ship.shp_own, ship.shp_uid,
325            mchr[(int)ship.shp_type].m_name);
326         if (opt_SHIPNAMES)
327             pr("%-25.25s ", ship.shp_name);
328         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
329         pr("%3d%%\n", ship.shp_effic);
330     }
331
332     if (!first)
333         pr("\n");
334
335     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
336     first = 1;
337
338     while (nxtitem(&ni, &land)) {
339         if (land.lnd_own == 0)
340             continue;
341         if (!chance((double)land.lnd_effic / 20.0))
342             continue;
343
344         if (first) {
345             pr("\t own  lnd# unit type         sector   eff\n");
346             first = 0;
347         }
348
349         pr("\t%4d %4d %-16.16s ", land.lnd_own, land.lnd_uid,
350            lchr[(int)land.lnd_type].l_name);
351         prxy("%4d,%-4d ", land.lnd_x, land.lnd_y, player->cnum);
352         pr("%3d%%\n", land.lnd_effic);
353     }
354
355     if (!first)
356         pr("\n");
357 }