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