]> git.pond.sub.org Git - empserver/blob - src/lib/subs/satmap.c
Fix satellite to fail on bad conditional
[empserver] / src / lib / subs / satmap.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  satmap.c: Do a satellite map given an x,y location, effic and other
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 2000
31  *     Markus Armbruster, 2004-2011
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 int
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 RET_OK;
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 RET_FAIL;
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             if (!snxtsct(&ns, selection))
106                 return RET_SYN;
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         }
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(&sect, (flags & P_I) ? 5 : 50);
125                     changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
126                                        dchr[sect.sct_type].d_mnem, 0);
127                     ++count;
128                     if (opt_HIDDEN)
129                         setcont(player->cnum, sect.sct_own, FOUND_FLY);
130                 }
131             }
132             if ((flags & P_I) ||
133                 sect.sct_type == SCT_WATER || sect.sct_type == SCT_MOUNT) {
134                 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
135             } else
136                 rad[ns.dy][ns.dx] = '?';
137             changed +=
138                 map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
139         }
140         if (changed)
141             writemap(player->cnum);
142         if (flags & P_S)
143             pr("  %d sectors\n\n", count);
144     }
145
146     if ((type == EF_BAD || type == EF_SHIP) &&
147         (flags & P_S || flags & P_I)) {
148         if (type == EF_SHIP) {
149             if (!snxtitem(&ni, EF_SHIP, selection, NULL))
150                 return RET_SYN;
151         } else
152             snxtitem_dist(&ni, EF_SHIP, x, y, range);
153
154         crackle = count = 0;
155         if (flags & P_S) {
156             pr("Satellite ship report\n");
157             prdate();
158             pr(" own  shp# ship type                                  sector   eff\n");
159         }
160         while (nxtitem(&ni, &ship)) {
161             if (ship.shp_own == 0)
162                 continue;
163             if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
164                 ((flags & (P_S | P_I)) != (P_S | P_I)))
165                 continue;
166             if (++crackle == 100)
167                 crackle = 0;
168             if (noise[crackle])
169                 continue;
170             if (flags & P_S) {
171                 pr("%4d %4d %-16.16s %-25.25s ",
172                    ship.shp_own, ship.shp_uid,
173                    mchr[(int)ship.shp_type].m_name, ship.shp_name);
174                 prxy("%4d,%-4d ", ship.shp_x, ship.shp_y);
175                 pr("%3d%%\n", ship.shp_effic);
176                 ++count;
177                 if (opt_HIDDEN)
178                     setcont(player->cnum, ship.shp_own, FOUND_FLY);
179             }
180             /* If we are imaging *and* drawing the map */
181             if ((flags & P_I) && (type == EF_BAD)) {
182                 rx = deltx(&ns.range, ship.shp_x);
183                 ry = delty(&ns.range, ship.shp_y);
184                 /* &~0x20 makes it a cap letter */
185                 rad[ry][rx] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
186             }
187         }
188         if (flags & P_S)
189             pr("  %d ships\n\n", count);
190     }
191
192     if ((type == EF_BAD || type == EF_LAND) &&
193         (flags & P_S || flags & P_I)) {
194         if (type == EF_LAND) {
195             if (!snxtitem(&ni, EF_LAND, selection, NULL))
196                 return RET_SYN;
197         } else
198             snxtitem_dist(&ni, EF_LAND, x, y, range);
199
200         crackle = count = 0;
201         if (flags & P_S) {
202             pr("Satellite unit report\n");
203             prdate();
204             pr(" own  lnd# unit type         sector   eff\n");
205         }
206         while (nxtitem(&ni, &land)) {
207             if (land.lnd_own == 0)
208                 continue;
209             if (lchr[(int)land.lnd_type].l_flags & L_SPY)
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);
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                 rx = deltx(&ns.range, land.lnd_x);
230                 ry = delty(&ns.range, land.lnd_y);
231                 /* &~0x20 makes it a cap letter */
232                 rad[ry][rx] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
233             }
234         }
235         if (flags & P_S)
236             pr("  %d units\n\n", count);
237     }
238
239     /* Ok, have we made the map?  If so, display it */
240     if (type == EF_BAD) {
241         /*
242          * print out the map
243          * We have to make the center a '0' for ve
244          * ve needs a garbage line to terminate the map
245          */
246         rad[delty(&ns.range, y)][deltx(&ns.range, y)] = '0';
247
248         pr("Satellite radar report\n");
249         n = ns.range.height;
250         for (row = 0; row < n; row++)
251             pr("%s\n", rad[row]);
252         pr("\n(c) 1989 Imaginative Images Inc.\n");
253     }
254     return RET_OK;
255 }
256
257 void
258 sathead(void)
259 {
260     pr("                    sct rd  rl  def\n");
261     pr("   sect   type own  eff eff eff eff  civ  mil  shl  gun iron  pet  food\n");
262 }
263
264 void
265 satdisp_sect(struct sctstr *sp, int acc)
266 {
267     prxy("%4d,%-4d   ", sp->sct_x, sp->sct_y);
268     pr("%c  %3d  %3d %3d %3d %3d %4d %4d %4d %4d %4d %4d %5d\n",
269        dchr[sp->sct_type].d_mnem,
270        sp->sct_own, roundintby((int)sp->sct_effic, acc / 2),
271        roundintby((int)sp->sct_road, acc / 2),
272        opt_RAILWAYS ? !!sct_rail_track(sp) : roundintby(sp->sct_rail, acc / 2),
273        roundintby((int)sp->sct_defense, acc / 2),
274        roundintby(sp->sct_item[I_CIVIL], acc),
275        roundintby(sp->sct_item[I_MILIT], acc),
276        roundintby(sp->sct_item[I_SHELL], acc),
277        roundintby(sp->sct_item[I_GUN], acc),
278        roundintby(sp->sct_item[I_IRON], acc),
279        roundintby(sp->sct_item[I_PETROL], acc),
280        roundintby(sp->sct_item[I_FOOD], acc));
281 }
282
283 void
284 satdisp_units(coord x, coord y)
285 {
286     int first;
287     struct nstr_item ni;
288     struct shpstr ship;
289     struct lndstr land;
290
291     snxtitem_xy(&ni, EF_SHIP, x, y);
292     first = 1;
293     while (nxtitem(&ni, &ship)) {
294         if (ship.shp_own == 0)
295             continue;
296         if (mchr[(int)ship.shp_type].m_flags & M_SUB)
297             continue;
298         if (first) {
299             pr("\t own  shp# ship type                                  sector   eff\n");
300             first = 0;
301         }
302         pr("\t%4d %4d %-16.16s %-25.25s ",
303            ship.shp_own, ship.shp_uid,
304            mchr[(int)ship.shp_type].m_name, ship.shp_name);
305         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y);
306         pr("%3d%%\n", ship.shp_effic);
307     }
308
309     if (!first)
310         pr("\n");
311
312     snxtitem_xy(&ni, EF_LAND, x, y);
313     first = 1;
314
315     while (nxtitem(&ni, &land)) {
316         if (land.lnd_own == 0)
317             continue;
318         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
319             continue;
320         if (lchr[(int)land.lnd_type].l_flags & L_SPY)
321             continue;
322         if (!chance(land.lnd_effic / 20.0))
323             continue;
324
325         if (first) {
326             pr("\t own  lnd# unit type       sector   eff\n");
327             first = 0;
328         }
329
330         pr("\t%4d %4d %-16.16s ",
331            land.lnd_own, land.lnd_uid, lchr[(int)land.lnd_type].l_name);
332         prxy("%4d,%-4d ", land.lnd_x, land.lnd_y);
333         pr("%3d%%\n", land.lnd_effic);
334     }
335
336     if (!first)
337         pr("\n");
338 }