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