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