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