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