]> git.pond.sub.org Git - empserver/blob - src/lib/commands/spy.c
deity.h is redundant, remove it.
[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", 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));
233 }
234
235
236 /*
237  * insert a key into the table.
238  */
239 static void
240 insert(coord *table, int *len, coord x, coord y)
241 {
242     if (!check(table, len, x, y)) {
243         table[(*len)++] = x;
244         table[(*len)++] = y;
245     }
246 }
247
248 /*
249  * see if a key is in the bitmask table
250  */
251 static int
252 check(coord *table, int *len, coord x, coord y)
253 {
254     int i;
255
256     for (i = 0; i < *len; i += 2)
257         if (table[i] == x && table[i + 1] == y)
258             return 1;
259     return 0;
260 }
261
262 static int
263 num_units(int x, int y)
264 {
265     struct lndstr land;
266     struct nstr_item ni;
267     int n = 0;
268
269     snxtitem_xy(&ni, EF_LAND, x, y);
270     while (nxtitem(&ni, (s_char *)&land)) {
271         if ((land.lnd_own == player->cnum) || (land.lnd_own == 0))
272             continue;
273         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
274             continue;
275         n++;
276     }
277
278     return n;
279 }
280
281 static void
282 prunits(int x, int y)
283 {
284     struct lndstr land;
285     struct nstr_item ni;
286     s_char report[128];
287
288     snxtitem_xy(&ni, EF_LAND, x, y);
289     while (nxtitem(&ni, (s_char *)&land)) {
290         if (land.lnd_own == player->cnum || land.lnd_own == 0)
291             continue;
292         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
293             continue;
294         /* Don't always see spies */
295         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
296             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
297                 continue;
298         }
299         if ((land.lnd_own != player->cnum) && land.lnd_own) {
300             int rel;
301             s_char *format;
302
303             rel = getrel(getnatp(player->cnum), land.lnd_own);
304             if (rel == ALLIED)
305                 format = "Allied (%s) unit in %s: ";
306             else if (rel == FRIENDLY || rel == NEUTRAL)
307                 format = "Neutral (%s) unit in %s: ";
308             else
309                 format = "Enemy (%s) unit in %s: ";
310             sprintf(report, format, cname(land.lnd_own),
311                     xyas(land.lnd_x, land.lnd_y, player->cnum));
312             intelligence_report(player->cnum, &land, 3, report);
313         }
314     }
315 }
316
317 static void
318 prplanes(int x, int y)
319 {
320     struct plnstr plane;
321     struct nstr_item ni;
322     s_char report[128];
323
324     snxtitem_xy(&ni, EF_PLANE, x, y);
325     while (nxtitem(&ni, (s_char *)&plane)) {
326         if (plane.pln_own == player->cnum || plane.pln_own == 0)
327             continue;
328         if (plane.pln_ship >= 0 || plane.pln_land >= 0)
329             continue;
330         if (plane.pln_flags & PLN_LAUNCHED)
331             continue;
332         if ((plane.pln_own != player->cnum) && plane.pln_own) {
333             int rel;
334             s_char *format;
335
336             rel = getrel(getnatp(player->cnum), plane.pln_own);
337             if (rel == ALLIED)
338                 format = "Allied (%s) plane in %s: %s\n";
339             else if (rel == FRIENDLY || rel == NEUTRAL)
340                 format = "Neutral (%s) plane in %s: %s\n";
341             else
342                 format = "Enemy (%s) plane in %s: %s\n";
343             sprintf(report, format, cname(plane.pln_own),
344                     xyas(plane.pln_x, plane.pln_y, player->cnum),
345                     prplane(&plane));
346             pr(report);
347         }
348     }
349 }