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