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