]> 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-2008, 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     int acc;
58     struct sctstr sect;
59     struct shpstr ship;
60     struct lndstr land;
61     int count;
62     struct nstr_item ni;
63     struct nstr_sect ns;
64     int rx, ry;
65     int row;
66     int n;
67     int changed = 0;
68     long crackle;
69     signed char noise[100];
70     char selection[1024];
71
72     if (!eff)
73         return;
74
75     if (!radbuf)
76         radbuf = malloc(WORLD_Y * (WORLD_X + 1));
77     if (!rad) {
78         rad = malloc(WORLD_Y * sizeof(char *));
79         if (rad && radbuf) {
80             for (rx = 0; rx < WORLD_Y; rx++)
81                 rad[rx] = &radbuf[(WORLD_X + 1) * rx];
82         }
83     }
84
85     if (!radbuf || !rad) {
86         pr("Memory error in satmap, tell the deity.\n");
87         return;
88     }
89
90     range = range * (eff / 100.0);
91     pr("%s efficiency %d%%, max range %d\n",
92        xyas(x, y, player->cnum), eff, range);
93     memset(noise, 0, sizeof(noise));
94     if (eff < 100) {
95         pr("Some noise on the transmission...\n");
96         for (n = 0; n < (100 - eff); ++n)
97             noise[100 * n / (100 - eff)] = 1;
98     }
99
100     /* Have to convert to player coords, since it gets converted
101        back from there */
102     sprintf(selection, "@%s:%d", xyas(x, y, player->cnum), range);
103
104     if (type == EF_BAD || type == EF_SECTOR) {
105         if (type == EF_SECTOR)  /* Use ?conditionals */
106             snxtsct(&ns, selection);
107         else
108             snxtsct_dist(&ns, x, y, range);
109
110         blankfill(radbuf, &ns.range, 1);
111         if (flags & P_S) {
112             pr("Satellite sector report\n");
113             prdate();
114             sathead();
115             acc = (flags & P_I) ? 5 : 50;
116         }
117         crackle = count = 0;
118         while (nxtsct(&ns, &sect)) {
119             if (++crackle == 100)
120                 crackle = 0;
121             if (noise[crackle])
122                 continue;
123             if (flags & P_S) {
124                 if (sect.sct_own && sect.sct_own != player->cnum) {
125                     satdisp_sect(&sect, acc);
126                     ++count;
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             pr(" own  shp# ship type                                  sector   eff\n");
157         }
158         while (nxtitem(&ni, &ship)) {
159             if (ship.shp_own == 0)
160                 continue;
161             if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
162                 ((flags & (P_S | P_I)) != (P_S | P_I)))
163                 continue;
164             if (++crackle == 100)
165                 crackle = 0;
166             if (noise[crackle])
167                 continue;
168             if (flags & P_S) {
169                 pr("%4d %4d %-16.16s %-25.25s ",
170                    ship.shp_own, ship.shp_uid,
171                    mchr[(int)ship.shp_type].m_name, ship.shp_name);
172                 prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
173                 pr("%3d%%\n", ship.shp_effic);
174                 ++count;
175                 if (opt_HIDDEN)
176                     setcont(player->cnum, ship.shp_own, FOUND_FLY);
177             }
178             /* If we are imaging *and* drawing the map */
179             if ((flags & P_I) && (type == EF_BAD)) {
180                 /* Figure out where to put the ship */
181                 /* First, figure out the distance from the two */
182                 rx = diffx((int)ship.shp_x, x);
183                 ry = diffy((int)ship.shp_y, y);
184                 /* Next, determine which direction to add it to the center */
185                 /* We can only do this if imaging and we have gotten the center
186                    up above by imaging the sectors. */
187                 rx = deltax(x, ns.range.lx) + rx;
188                 ry = deltay(y, ns.range.ly) + ry;
189                 /* &~0x20 makes it a cap letter */
190                 rad[ry][rx] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
191             }
192         }
193         if (flags & P_S)
194             pr("  %d ships\n\n", count);
195     }
196
197     if ((type == EF_BAD || type == EF_LAND) &&
198         (flags & P_S || flags & P_I)) {
199         if (type == EF_LAND)
200             snxtitem(&ni, EF_LAND, selection);
201         else
202             snxtitem_dist(&ni, EF_LAND, x, y, range);
203
204         crackle = count = 0;
205         if (flags & P_S) {
206             pr("Satellite unit report\n");
207             prdate();
208             pr(" own  lnd# unit type         sector   eff\n");
209         }
210         while (nxtitem(&ni, &land)) {
211             if (land.lnd_own == 0)
212                 continue;
213             if (lchr[(int)land.lnd_type].l_flags & L_SPY)
214                 continue;
215             if (!chance(land.lnd_effic / 20.0))
216                 continue;
217             if (++crackle == 100)
218                 crackle = 0;
219             if (noise[crackle])
220                 continue;
221             if (flags & P_S) {
222                 pr("%4d %4d %-16.16s ",
223                    land.lnd_own, land.lnd_uid,
224                    lchr[(int)land.lnd_type].l_name);
225                 prxy("%4d,%-4d", land.lnd_x, land.lnd_y, player->cnum);
226                 pr("%3d%%\n", land.lnd_effic);
227                 ++count;
228                 if (opt_HIDDEN)
229                     setcont(player->cnum, land.lnd_own, FOUND_FLY);
230             }
231             /* If we are imaging *and* drawing the map */
232             if ((flags & P_I) && (type == EF_BAD)) {
233                 /* Figure out where to put the unit */
234                 /* First, figure out the distance from the two */
235                 rx = diffx((int)land.lnd_x, x);
236                 ry = diffy((int)land.lnd_y, y);
237                 /* Next, determine which direction to add it to the center */
238                 /* We can only do this if imaging and we have gotten the center
239                    up above by imaging the sectors. */
240                 rx = deltax(x, ns.range.lx) + rx;
241                 ry = deltay(y, ns.range.ly) + ry;
242                 /* &~0x20 makes it a cap letter */
243                 rad[ry][rx] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
244             }
245         }
246         if (flags & P_S)
247             pr("  %d units\n\n", count);
248     }
249
250     /* Ok, have we made the map?  If so, display it */
251     if (type == EF_BAD) {
252         /*
253          * print out the map
254          * We have to make the center a '0' for ve
255          * ve needs a garbage line to terminate the map
256          */
257         rad[deltay(y, ns.range.ly)][deltax(x, ns.range.lx)] = '0';
258
259         pr("Satellite radar report\n");
260 #ifdef HAY
261         /* This is wrong for small, hitech worlds. */
262         n = deltay(ns.range.hy, ns.range.ly);
263 #else
264         /* This is already available, so why not use it. */
265         n = ns.range.height;
266 #endif
267         for (row = 0; row < n; row++)
268             pr("%s\n", rad[row]);
269         pr("\n(c) 1989 Imaginative Images Inc.\n");
270     }
271 }
272
273 void
274 sathead(void)
275 {
276     pr("                    sct rd  rl  def\n");
277     pr("   sect   type own  eff eff eff eff  civ  mil  shl  gun iron  pet  food\n");
278 }
279
280 void
281 satdisp_sect(struct sctstr *sp, int acc)
282 {
283     prxy("%4d,%-4d   ", sp->sct_x, sp->sct_y, player->cnum);
284     pr("%c  %3d  %3d %3d %3d %3d %4d %4d %4d %4d %4d %4d %5d\n",
285        dchr[sp->sct_type].d_mnem,
286        sp->sct_own, roundintby((int)sp->sct_effic, acc / 2),
287        roundintby((int)sp->sct_road, acc / 2),
288        roundintby((int)sp->sct_rail, acc / 2),
289        roundintby((int)sp->sct_defense, acc / 2),
290        roundintby(sp->sct_item[I_CIVIL], acc),
291        roundintby(sp->sct_item[I_MILIT], acc),
292        roundintby(sp->sct_item[I_SHELL], acc),
293        roundintby(sp->sct_item[I_GUN], acc),
294        roundintby(sp->sct_item[I_IRON], acc),
295        roundintby(sp->sct_item[I_PETROL], acc),
296        roundintby(sp->sct_item[I_FOOD], acc));
297     map_set(player->cnum, sp->sct_x, sp->sct_y, dchr[sp->sct_type].d_mnem,
298             0);
299 }
300
301 void
302 satdisp_units(coord x, coord y)
303 {
304     int first;
305     struct nstr_item ni;
306     struct shpstr ship;
307     struct lndstr land;
308
309     snxtitem_xy(&ni, EF_SHIP, x, 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             pr("\t own  shp# ship type                                  sector   eff\n");
318             first = 0;
319         }
320         pr("\t%4d %4d %-16.16s %-25.25s ",
321            ship.shp_own, ship.shp_uid,
322            mchr[(int)ship.shp_type].m_name, ship.shp_name);
323         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
324         pr("%3d%%\n", ship.shp_effic);
325     }
326
327     if (!first)
328         pr("\n");
329
330     snxtitem_xy(&ni, EF_LAND, x, y);
331     first = 1;
332
333     while (nxtitem(&ni, &land)) {
334         if (land.lnd_own == 0)
335             continue;
336         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
337             continue;
338         if (lchr[(int)land.lnd_type].l_flags & L_SPY)
339             continue;
340         if (!chance(land.lnd_effic / 20.0))
341             continue;
342
343         if (first) {
344             pr("\t own  lnd# unit type       sector   eff\n");
345             first = 0;
346         }
347
348         pr("\t%4d %4d %-16.16s ",
349            land.lnd_own, land.lnd_uid, lchr[(int)land.lnd_type].l_name);
350         prxy("%4d,%-4d ", land.lnd_x, land.lnd_y, player->cnum);
351         pr("%3d%%\n", land.lnd_effic);
352     }
353
354     if (!first)
355         pr("\n");
356 }