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