]> git.pond.sub.org Git - empserver/blob - src/lib/commands/spy.c
Let spy report on unoccupied sectors
[empserver] / src / lib / commands / spy.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <config.h>
36
37 #include "commands.h"
38 #include "item.h"
39 #include "land.h"
40 #include "map.h"
41 #include "news.h"
42 #include "optlist.h"
43 #include "path.h"
44 #include "plane.h"
45
46 /*
47  * format:  spy <SECTS>
48  */
49
50 static int check(coord *table, int *len, coord x, coord y);
51 static void insert(coord *table, int *len, coord x, coord y);
52 static void spy_report(struct sctstr *sp);
53 static void prplanes(int, int);
54 static void prunits(int, int);
55 static char *player_relstr(natid);
56
57 int
58 spy(void)
59 {
60     natid own;
61     int relat;
62     coord x, y;
63     coord nx, ny;
64     int military;
65     int savemil;
66     int btucost;
67     int i;
68     coord *table;               /* sectors already seen */
69     int t_len = 0;
70     int nrecon;
71     int nunits;
72     struct nstr_sect nstr;
73     struct nstr_item ni;
74     struct natstr *natp;
75     struct sctstr from;
76     struct sctstr dsect;
77     struct lndstr land;
78     int changed = 0;
79     int nsects;
80
81     /*
82      * first arg should be the range of sectors
83      */
84     if (!snxtsct(&nstr, player->argp[1]))
85         return RET_SYN;
86     nsects = (nstr.range.width + 1) * nstr.range.height / 2;
87     btucost = (nsects / 40) + 1;
88     natp = getnatp(player->cnum);
89     if (natp->nat_btu < btucost) {
90         pr("You don't have the BTU's for spying on that scale!\n");
91         return RET_FAIL;
92     }
93     /*
94      * set up all the goodies we need later
95      * 6 = neighbors, 2 = x,y
96      */
97     table = malloc((nsects + 1) * 6 * 2 * sizeof(coord));
98     memset(table, 0, (nsects + 1) * 6 * 2 * sizeof(coord));
99     pr("SPY report\n");
100     prdate();
101     pr("                 old sct rd  rl  def\n");
102     pr("   sect   de own own eff eff eff eff  civ  mil  shl gun  pet food bars\n");
103     while (nxtsct(&nstr, &from)) {
104         if (!player->owner && !player->god)
105             continue;
106         nrecon = 0;
107         nunits = 0;
108         snxtitem_xy(&ni, EF_LAND, from.sct_x, from.sct_y);
109         while (nxtitem(&ni, &land)) {
110             nunits++;
111             if (lchr[(int)land.lnd_type].l_flags & L_RECON)
112                 nrecon++;
113         }
114         if ((military = from.sct_item[I_MILIT]) == 0 && (nunits == 0))
115             continue;
116         x = from.sct_x;
117         y = from.sct_y;
118         /* Print out the units/planes in this sector */
119         prunits(x, y);
120         prplanes(x, y);
121         savemil = military;
122         /*
123          * check the neighboring sectors.
124          */
125         for (i = 1; i <= 6; i++) {
126             if ((military == 0) && (nunits == 0))
127                 break;
128             nx = x + diroff[i][0];
129             ny = y + diroff[i][1];
130             /*
131              * if we've already seen the
132              * sector, don't bother checking it
133              * out.
134              */
135             if (check(table, &t_len, nx, ny)) {
136                 continue;
137             }
138             getsect(nx, ny, &dsect);
139             if (player->owner || dsect.sct_type == SCT_WATER)
140                 continue;
141
142             own = dsect.sct_own;
143             relat = getrel(getnatp(own), player->cnum);
144             if (relat <= NEUTRAL
145                 && chance(dsect.sct_item[I_MILIT] / 200.0)) {
146                 /* spy caught */
147                 if (relat == NEUTRAL) {
148                     /* deport spy */
149                     pr("Spy deported from %s\n",
150                        xyas(nx, ny, player->cnum));
151                     if (own != 0)
152                         wu(0, own, "%s (#%d) spy deported from %s\n",
153                            cname(player->cnum), player->cnum,
154                            xyas(nx, ny, own));
155                 } else {
156                     /* execute spy */
157                     pr("BANG!! A spy was shot in %s\n",
158                        xyas(nx, ny, player->cnum));
159                     military--;
160                     if (own != 0)
161                         wu(0, own, "%s (#%d) spy caught in %s\n",
162                            cname(player->cnum), player->cnum,
163                            xyas(nx, ny, own));
164                 }
165                 if (opt_HIDDEN)
166                     setcont(own, player->cnum, FOUND_SPY);
167                 if (!nrecon)    /* unless you have a recon unit */
168                     continue;   /* no report from caught spy */
169             }
170
171             /* spy report */
172             insert(table, &t_len, nx, ny);
173             spy_report(&dsect);
174             changed += map_set(player->cnum, dsect.sct_x, dsect.sct_y,
175                                dchr[dsect.sct_type].d_mnem, 0);
176             if (opt_HIDDEN)
177                 setcont(player->cnum, own, FOUND_SPY);
178         }
179         /* subtract any military if necessary */
180         if ((savemil != military) && (savemil > 0)) {
181             if ((military < 0) || (military > savemil))
182                 military = 0;
183             from.sct_item[I_MILIT] = military;
184             putsect(&from);
185         }
186     }
187     if (changed)
188         writemap(player->cnum);
189     player->btused += btucost;
190     free(table);
191     return RET_OK;
192 }
193
194 static void
195 spy_report(struct sctstr *sp)
196 {
197     prxy("%4d,%-4d", sp->sct_x, sp->sct_y, player->cnum);
198     pr(" %c%c %3d %3d %3d %3d %3d %3d %4d %4d %4d %3d %4d %4d %4d\n",
199        dchr[sp->sct_type].d_mnem,
200        sp->sct_newtype == sp->sct_type ? ' ' : dchr[sp->sct_newtype].d_mnem,
201        sp->sct_own,
202        sp->sct_oldown,
203        roundintby((int)sp->sct_effic, 10),
204        roundintby((int)sp->sct_road, 10),
205        roundintby((int)sp->sct_rail, 10),
206        roundintby((int)sp->sct_defense, 10),
207        roundintby(sp->sct_item[I_CIVIL], 10),
208        roundintby(sp->sct_item[I_MILIT], 10),
209        roundintby(sp->sct_item[I_SHELL], 10),
210        roundintby(sp->sct_item[I_GUN], 10),
211        roundintby(sp->sct_item[I_PETROL], 10),
212        roundintby(sp->sct_item[I_FOOD], 10),
213        roundintby(sp->sct_item[I_BAR], 10));
214     prunits(sp->sct_x, sp->sct_y);
215     prplanes(sp->sct_x, sp->sct_y);
216 }
217
218 /*
219  * insert a key into the table.
220  */
221 static void
222 insert(coord *table, int *len, coord x, coord y)
223 {
224     if (!check(table, len, x, y)) {
225         table[(*len)++] = x;
226         table[(*len)++] = y;
227     }
228 }
229
230 /*
231  * see if a key is in the bitmask table
232  */
233 static int
234 check(coord *table, int *len, coord x, coord y)
235 {
236     int i;
237
238     for (i = 0; i < *len; i += 2)
239         if (table[i] == x && table[i + 1] == y)
240             return 1;
241     return 0;
242 }
243
244 static void
245 prunits(int x, int y)
246 {
247     struct lndstr land;
248     struct nstr_item ni;
249     char report[128];
250
251     snxtitem_xy(&ni, EF_LAND, x, y);
252     while (nxtitem(&ni, &land)) {
253         if (land.lnd_own == player->cnum || land.lnd_own == 0)
254             continue;
255         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
256             continue;
257         /* Don't always see spies */
258         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
259             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
260                 continue;
261         }
262         sprintf(report, "%s (%s) unit in %s: ",
263                 player_relstr(land.lnd_own),
264                 cname(land.lnd_own),
265                 xyas(land.lnd_x, land.lnd_y, player->cnum));
266         intelligence_report(player->cnum, &land, 3, report);
267     }
268 }
269
270 static void
271 prplanes(int x, int y)
272 {
273     struct plnstr plane;
274     struct nstr_item ni;
275
276     snxtitem_xy(&ni, EF_PLANE, x, y);
277     while (nxtitem(&ni, &plane)) {
278         if (plane.pln_own == player->cnum || plane.pln_own == 0)
279             continue;
280         if (plane.pln_ship >= 0 || plane.pln_land >= 0)
281             continue;
282         if (plane.pln_flags & PLN_LAUNCHED)
283             continue;
284         pr("%s (%s) plane in %s: %s\n",
285            player_relstr(plane.pln_own),
286            cname(plane.pln_own),
287            xyas(plane.pln_x, plane.pln_y, player->cnum),
288            prplane(&plane));
289     }
290 }
291
292 static char *
293 player_relstr(natid them)
294 {
295     int rel = getrel(getnatp(player->cnum), them);
296
297     if (rel == ALLIED)
298         return "Allied";
299     if (rel >= NEUTRAL)
300         return "Neutral";
301     return "Enemy";
302 }