]> git.pond.sub.org Git - empserver/blob - src/lib/commands/spy.c
Update copyright notice.
[empserver] / src / lib / commands / spy.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  spy.c: Spy on your neighbors
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1998-2000
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "var.h"
38 #include "sect.h"
39 #include "item.h"
40 #include "nat.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include "nuke.h"
44 #include "news.h"
45 #include "path.h"
46 #include "file.h"
47 #include "land.h"
48 #include "commands.h"
49 #include "optlist.h"
50
51 /*
52  * format:  spy <SECTS>
53  *
54  */
55
56 static int check(coord *table, int *len, coord x, coord y);
57 static void insert(coord *table, int *len, coord x, coord y);
58 static int num_units(int, int);
59 static void prplanes(int, int);
60 static void prunits(int, int);
61 static void spyline(struct sctstr *sp);
62
63 int
64 spy(void)
65 {
66     int caught;
67     natid own;
68     int relat;
69     coord x, y;
70     coord nx, ny;
71     int military;
72     int savemil;
73     int btucost;
74     int i;
75     coord *table;               /* sectors already seen */
76     int t_len = 0;
77     int nrecon;
78     int nunits;
79     struct nstr_sect nstr;
80     struct nstr_item ni;
81     struct natstr *natp;
82     struct sctstr from;
83     struct sctstr dsect;
84     struct lndstr land;
85     int changed = 0;
86     int nsects;
87
88     /*
89      * first arg should be the range of sectors
90      */
91     if (!snxtsct(&nstr, player->argp[1]))
92         return RET_SYN;
93     nsects = (nstr.range.width + 1) * nstr.range.height / 2;
94     btucost = (nsects / 40) + 1;
95     natp = getnatp(player->cnum);
96     if (natp->nat_btu < btucost) {
97         pr("You don't have the BTU's for spying on that scale!\n");
98         return RET_FAIL;
99     }
100     /*
101      * set up all the goodies we need later
102      * 6 = neighbors, 2 = x,y
103      */
104     table = (coord *)malloc((nsects + 1) * 6 * 2 * sizeof(coord));
105     memset(table, 0, (nsects + 1) * 6 * 2 * sizeof(coord));
106     pr("SPY report\n");
107     prdate();
108     pr("                 old sct rd  rl  def\n");
109     pr("   sect   de own own eff eff eff eff  civ  mil  shl gun  pet food bars lnd pln\n");
110     while (nxtsct(&nstr, &from)) {
111         if (!player->owner && !player->god)
112             continue;
113         nrecon = 0;
114         nunits = 0;
115         snxtitem_xy(&ni, EF_LAND, from.sct_x, from.sct_y);
116         while (nxtitem(&ni, (s_char *)&land)) {
117             nunits++;
118             if (lchr[(int)land.lnd_type].l_flags & L_RECON)
119                 nrecon++;
120         }
121         if ((military = from.sct_item[I_MILIT]) == 0 && (nunits == 0))
122             continue;
123         x = from.sct_x;
124         y = from.sct_y;
125         /* Print out the units/planes in this sector */
126         prunits(x, y);
127         prplanes(x, y);
128         savemil = military;
129         /*
130          * check the neighboring sectors.
131          */
132         for (i = 1; i <= 6; i++) {
133             if ((military == 0) && (nunits == 0))
134                 break;
135             nx = x + diroff[i][0];
136             ny = y + diroff[i][1];
137             /*
138              * if we've already seen the
139              * sector, don't bother checking it
140              * out.
141              */
142             if (check(table, &t_len, nx, ny)) {
143                 continue;
144             }
145             getsect(nx, ny, &dsect);
146             if (player->owner
147                 || (dsect.sct_type == SCT_WATER)
148                 || (!dsect.sct_item[I_MILIT] && !dsect.sct_item[I_CIVIL]
149                     && num_units(nx, ny) == 0)) {
150                 /* mark sector as seen */
151                 insert(table, &t_len, nx, ny);
152                 continue;
153             }
154             /* catch spy N/200 chance, N = # military */
155             caught = chance((double)dsect.sct_item[I_MILIT] / 200.0);
156             own = dsect.sct_own;
157             /* determine spyee relations with spyer */
158             relat = getrel(getnatp(own), player->cnum);
159             if (relat == NEUTRAL && caught) {
160                 /* neutral spy-ee */
161                 pr("Spy deported from %s\n", xyas(nx, ny, player->cnum));
162                 if (own != 0)
163                     wu(0, own, "%s (#%d) spy deported from %s\n",
164                        cname(player->cnum), player->cnum,
165                        xyas(nx, ny, own));
166             } else if (relat < NEUTRAL && caught) {
167                 /* at-war with spy-ee */
168                 pr("BANG!! A spy was shot in %s\n",
169                    xyas(nx, ny, player->cnum));
170                 military--;
171                 if (own != 0)
172                     wu(0, own, "%s (#%d) spy caught in %s\n",
173                        cname(player->cnum), player->cnum,
174                        xyas(nx, ny, own));
175                 nreport(player->cnum, N_SPY_SHOT, own, 1);
176             } else {
177                 insert(table, &t_len, nx, ny);
178                 spyline(&dsect);
179                 changed += map_set(player->cnum, dsect.sct_x,
180                                    dsect.sct_y,
181                                    dchr[dsect.sct_type].d_mnem, 0);
182                 prunits(dsect.sct_x, dsect.sct_y);
183                 prplanes(dsect.sct_x, dsect.sct_y);
184                 if (opt_HIDDEN) {
185                     setcont(player->cnum, own, FOUND_SPY);
186                 }
187             }
188             /*
189              * If you have a recon unit, it'll
190              * see the sector anyway...
191              */
192             if (nrecon && caught) {
193                 insert(table, &t_len, nx, ny);
194                 spyline(&dsect);
195                 changed += map_set(player->cnum, dsect.sct_x,
196                                    dsect.sct_y,
197                                    dchr[dsect.sct_type].d_mnem, 0);
198                 prunits(dsect.sct_x, dsect.sct_y);
199                 prplanes(dsect.sct_x, dsect.sct_y);
200             }
201         }
202         /* subtract any military if necessary */
203         if ((savemil != military) && (savemil > 0)) {
204             if ((military < 0) || (military > savemil))
205                 military = 0;
206             from.sct_item[I_MILIT] = military;
207             putsect(&from);
208         }
209     }
210     if (changed)
211         writemap(player->cnum);
212     player->btused += btucost;
213     free((s_char *)table);
214     return RET_OK;
215 }
216
217
218 /*
219  * just a big printf.
220  */
221 static void
222 spyline(struct sctstr *sp)
223 {
224     prxy("%4d,%-4d", sp->sct_x, sp->sct_y, player->cnum);
225     pr(" %c%c %3d %3d %3d %3d %3d %3d %4d %4d %4d %3d %4d %4d %4d %3d %3d\n",
226        dchr[sp->sct_type].d_mnem,
227        (sp->sct_newtype == sp->sct_type) ? ' ' : dchr[sp->sct_newtype].d_mnem,
228        sp->sct_own,
229        sp->sct_oldown,
230        roundintby((int)sp->sct_effic, 10),
231        roundintby((int)sp->sct_road, 10),
232        roundintby((int)sp->sct_rail, 10),
233        roundintby((int)sp->sct_defense, 10),
234        roundintby(sp->sct_item[I_CIVIL], 10),
235        roundintby(sp->sct_item[I_MILIT], 10),
236        roundintby(sp->sct_item[I_SHELL], 10),
237        roundintby(sp->sct_item[I_GUN], 10),
238        roundintby(sp->sct_item[I_PETROL], 10),
239        roundintby(sp->sct_item[I_FOOD], 10),
240        roundintby(sp->sct_item[I_BAR], 10),
241        count_sect_units(sp),
242        count_sect_planes(sp));
243 }
244
245
246 /*
247  * insert a key into the table.
248  */
249 static void
250 insert(coord *table, int *len, coord x, coord y)
251 {
252     if (!check(table, len, x, y)) {
253         table[(*len)++] = x;
254         table[(*len)++] = y;
255     }
256 }
257
258 /*
259  * see if a key is in the bitmask table
260  */
261 static int
262 check(coord *table, int *len, coord x, coord y)
263 {
264     int i;
265
266     for (i = 0; i < *len; i += 2)
267         if (table[i] == x && table[i + 1] == y)
268             return 1;
269     return 0;
270 }
271
272 static int
273 num_units(int x, int y)
274 {
275     struct lndstr land;
276     struct nstr_item ni;
277     int n = 0;
278
279     snxtitem_xy(&ni, EF_LAND, x, y);
280     while (nxtitem(&ni, (s_char *)&land)) {
281         if ((land.lnd_own == player->cnum) || (land.lnd_own == 0))
282             continue;
283         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
284             continue;
285         n++;
286     }
287
288     return n;
289 }
290
291 static void
292 prunits(int x, int y)
293 {
294     struct lndstr land;
295     struct nstr_item ni;
296     s_char report[128];
297
298     snxtitem_xy(&ni, EF_LAND, x, y);
299     while (nxtitem(&ni, (s_char *)&land)) {
300         if (land.lnd_own == player->cnum || land.lnd_own == 0)
301             continue;
302         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
303             continue;
304         /* Don't always see spies */
305         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
306             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
307                 continue;
308         }
309         if ((land.lnd_own != player->cnum) && land.lnd_own) {
310             int rel;
311             s_char *format;
312
313             rel = getrel(getnatp(player->cnum), land.lnd_own);
314             if (rel == ALLIED)
315                 format = "Allied (%s) unit in %s: ";
316             else if (rel == FRIENDLY || rel == NEUTRAL)
317                 format = "Neutral (%s) unit in %s: ";
318             else
319                 format = "Enemy (%s) unit in %s: ";
320             sprintf(report, format, cname(land.lnd_own),
321                     xyas(land.lnd_x, land.lnd_y, player->cnum));
322             intelligence_report(player->cnum, &land, 3, report);
323         }
324     }
325 }
326
327 static void
328 prplanes(int x, int y)
329 {
330     struct plnstr plane;
331     struct nstr_item ni;
332     s_char report[128];
333
334     snxtitem_xy(&ni, EF_PLANE, x, y);
335     while (nxtitem(&ni, (s_char *)&plane)) {
336         if (plane.pln_own == player->cnum || plane.pln_own == 0)
337             continue;
338         if (plane.pln_ship >= 0 || plane.pln_land >= 0)
339             continue;
340         if (plane.pln_flags & PLN_LAUNCHED)
341             continue;
342         if ((plane.pln_own != player->cnum) && plane.pln_own) {
343             int rel;
344             s_char *format;
345
346             rel = getrel(getnatp(player->cnum), plane.pln_own);
347             if (rel == ALLIED)
348                 format = "Allied (%s) plane in %s: %s\n";
349             else if (rel == FRIENDLY || rel == NEUTRAL)
350                 format = "Neutral (%s) plane in %s: %s\n";
351             else
352                 format = "Enemy (%s) plane in %s: %s\n";
353             sprintf(report, format, cname(plane.pln_own),
354                     xyas(plane.pln_x, plane.pln_y, player->cnum),
355                     prplane(&plane));
356             pr(report);
357         }
358     }
359 }