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