]> git.pond.sub.org Git - empserver/blob - src/lib/subs/satmap.c
d5e2957de8c01b33325b7a4de8edab98ffe0f5c6
[empserver] / src / lib / subs / satmap.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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 "chance.h"
38 #include "file.h"
39 #include "land.h"
40 #include "map.h"
41 #include "misc.h"
42 #include "nat.h"
43 #include "nsc.h"
44 #include "optlist.h"
45 #include "plane.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "sect.h"
49 #include "ship.h"
50 #include "xy.h"
51
52 static char **rad;
53 static char *radbuf;
54
55 int
56 satmap(int x, int y, int eff, int range, int flags, int type)
57 {
58     struct sctstr sect;
59     struct shpstr ship;
60     struct lndstr land;
61     int count, crackle;
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     signed char noise[100];
69
70     if (!eff)
71         return RET_OK;
72
73     if (!radbuf)
74         radbuf = malloc(WORLD_Y * MAPWIDTH(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 RET_FAIL;
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     if (type == EF_BAD || type == EF_SECTOR) {
99         snxtsct_dist(&ns, x, y, range);
100         if (type == EF_SECTOR && !snxtsct_use_condarg(&ns))
101             return RET_SYN;
102
103         blankfill(radbuf, &ns.range, 1);
104         if (flags & P_S) {
105             pr("Satellite sector report\n");
106             prdate();
107             sathead();
108         }
109         crackle = count = 0;
110         while (nxtsct(&ns, &sect)) {
111             if (++crackle == 100)
112                 crackle = 0;
113             if (noise[crackle])
114                 continue;
115             if (flags & P_S) {
116                 if (sect.sct_own && sect.sct_own != player->cnum) {
117                     satdisp_sect(&sect, (flags & P_I) ? 5 : 50);
118                     changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
119                                        dchr[sect.sct_type].d_mnem, 0);
120                     ++count;
121                     if (opt_HIDDEN)
122                         setcont(player->cnum, sect.sct_own, FOUND_FLY);
123                 }
124             }
125             if ((flags & P_I) ||
126                 sect.sct_type == SCT_WATER || sect.sct_type == SCT_MOUNT) {
127                 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
128             } else
129                 rad[ns.dy][ns.dx] = '?';
130             changed +=
131                 map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
132         }
133         if (changed)
134             writemap(player->cnum);
135         if (flags & P_S)
136             pr("  %d sectors\n\n", count);
137     }
138
139     if ((type == EF_BAD || type == EF_SHIP) &&
140         (flags & P_S || flags & P_I)) {
141         snxtitem_dist(&ni, EF_SHIP, x, y, range);
142         if (type == EF_SHIP && !snxtitem_use_condarg(&ni))
143             return RET_SYN;
144
145         crackle = count = 0;
146         if (flags & P_S) {
147             pr("Satellite ship report\n");
148             prdate();
149             pr(" own  shp# ship type                                  sector   eff\n");
150         }
151         while (nxtitem(&ni, &ship)) {
152             if (ship.shp_own == 0)
153                 continue;
154             if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
155                 ((flags & (P_S | P_I)) != (P_S | P_I)))
156                 continue;
157             if (++crackle == 100)
158                 crackle = 0;
159             if (noise[crackle])
160                 continue;
161             if (flags & P_S) {
162                 pr("%4d %4d %-16.16s %-25.25s ",
163                    ship.shp_own, ship.shp_uid,
164                    mchr[(int)ship.shp_type].m_name, ship.shp_name);
165                 prxy("%4d,%-4d ", ship.shp_x, ship.shp_y);
166                 pr("%3d%%\n", ship.shp_effic);
167                 ++count;
168                 if (opt_HIDDEN)
169                     setcont(player->cnum, ship.shp_own, FOUND_FLY);
170             }
171             /* If we are imaging *and* drawing the map */
172             if ((flags & P_I) && (type == EF_BAD)) {
173                 rx = deltx(&ns.range, ship.shp_x);
174                 ry = delty(&ns.range, ship.shp_y);
175                 /* &~0x20 makes it a cap letter */
176                 rad[ry][rx] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
177             }
178         }
179         if (flags & P_S)
180             pr("  %d ships\n\n", count);
181     }
182
183     if ((type == EF_BAD || type == EF_LAND) &&
184         (flags & P_S || flags & P_I)) {
185         snxtitem_dist(&ni, EF_LAND, x, y, range);
186         if (type == EF_LAND && !snxtitem_use_condarg(&ni))
187             return RET_SYN;
188
189         crackle = count = 0;
190         if (flags & P_S) {
191             pr("Satellite unit report\n");
192             prdate();
193             pr(" own  lnd# unit type         sector   eff\n");
194         }
195         while (nxtitem(&ni, &land)) {
196             if (land.lnd_own == 0)
197                 continue;
198             if (lchr[(int)land.lnd_type].l_flags & L_SPY)
199                 continue;
200             if (!chance(land.lnd_effic / 20.0))
201                 continue;
202             if (++crackle == 100)
203                 crackle = 0;
204             if (noise[crackle])
205                 continue;
206             if (flags & P_S) {
207                 pr("%4d %4d %-16.16s ",
208                    land.lnd_own, land.lnd_uid,
209                    lchr[(int)land.lnd_type].l_name);
210                 prxy("%4d,%-4d", land.lnd_x, land.lnd_y);
211                 pr("%3d%%\n", land.lnd_effic);
212                 ++count;
213                 if (opt_HIDDEN)
214                     setcont(player->cnum, land.lnd_own, FOUND_FLY);
215             }
216             /* If we are imaging *and* drawing the map */
217             if ((flags & P_I) && (type == EF_BAD)) {
218                 rx = deltx(&ns.range, land.lnd_x);
219                 ry = delty(&ns.range, land.lnd_y);
220                 /* &~0x20 makes it a cap letter */
221                 rad[ry][rx] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
222             }
223         }
224         if (flags & P_S)
225             pr("  %d units\n\n", count);
226     }
227
228     /* Ok, have we made the map?  If so, display it */
229     if (type == EF_BAD) {
230         /*
231          * print out the map
232          * We have to make the center a '0' for ve
233          * ve needs a garbage line to terminate the map
234          */
235         rad[delty(&ns.range, y)][deltx(&ns.range, y)] = '0';
236
237         pr("Satellite radar report\n");
238         n = ns.range.height;
239         for (row = 0; row < n; row++)
240             pr("%s\n", rad[row]);
241         pr("\n(c) 1989 Imaginative Images Inc.\n");
242     }
243     return RET_OK;
244 }
245
246 void
247 sathead(void)
248 {
249     pr("                    sct rd  rl  def\n");
250     pr("   sect   type own  eff eff eff eff  civ  mil  shl  gun iron  pet  food\n");
251 }
252
253 void
254 satdisp_sect(struct sctstr *sp, int acc)
255 {
256     prxy("%4d,%-4d   ", sp->sct_x, sp->sct_y);
257     pr("%c  %3d  %3d %3d %3d %3d %4d %4d %4d %4d %4d %4d %5d\n",
258        dchr[sp->sct_type].d_mnem,
259        sp->sct_own, roundintby((int)sp->sct_effic, acc / 2),
260        roundintby((int)sp->sct_road, acc / 2),
261        opt_RAILWAYS ? !!sct_rail_track(sp) : roundintby(sp->sct_rail, acc / 2),
262        roundintby((int)sp->sct_defense, acc / 2),
263        roundintby(sp->sct_item[I_CIVIL], acc),
264        roundintby(sp->sct_item[I_MILIT], acc),
265        roundintby(sp->sct_item[I_SHELL], acc),
266        roundintby(sp->sct_item[I_GUN], acc),
267        roundintby(sp->sct_item[I_IRON], acc),
268        roundintby(sp->sct_item[I_PETROL], acc),
269        roundintby(sp->sct_item[I_FOOD], acc));
270 }
271
272 void
273 satdisp_units(coord x, coord y)
274 {
275     int first;
276     struct nstr_item ni;
277     struct shpstr ship;
278     struct lndstr land;
279
280     snxtitem_xy(&ni, EF_SHIP, x, y);
281     first = 1;
282     while (nxtitem(&ni, &ship)) {
283         if (ship.shp_own == 0)
284             continue;
285         if (mchr[(int)ship.shp_type].m_flags & M_SUB)
286             continue;
287         if (first) {
288             pr("\t own  shp# ship type                                  sector   eff\n");
289             first = 0;
290         }
291         pr("\t%4d %4d %-16.16s %-25.25s ",
292            ship.shp_own, ship.shp_uid,
293            mchr[(int)ship.shp_type].m_name, ship.shp_name);
294         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y);
295         pr("%3d%%\n", ship.shp_effic);
296     }
297
298     if (!first)
299         pr("\n");
300
301     snxtitem_xy(&ni, EF_LAND, x, y);
302     first = 1;
303
304     while (nxtitem(&ni, &land)) {
305         if (land.lnd_own == 0)
306             continue;
307         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
308             continue;
309         if (lchr[(int)land.lnd_type].l_flags & L_SPY)
310             continue;
311         if (!chance(land.lnd_effic / 20.0))
312             continue;
313
314         if (first) {
315             pr("\t own  lnd# unit type       sector   eff\n");
316             first = 0;
317         }
318
319         pr("\t%4d %4d %-16.16s ",
320            land.lnd_own, land.lnd_uid, lchr[(int)land.lnd_type].l_name);
321         prxy("%4d,%-4d ", land.lnd_x, land.lnd_y);
322         pr("%3d%%\n", land.lnd_effic);
323     }
324
325     if (!first)
326         pr("\n");
327 }