2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * spy.c: Spy on your neighbors
30 * Known contributors to this file:
32 * Steve McClure, 1998-2000
54 static int check(coord *table, int *len, coord x, coord y);
55 static void insert(coord *table, int *len, coord x, coord y);
56 static int num_units(int, int);
57 static void prplanes(int, int);
58 static void prunits(int, int);
59 static void spyline(struct sctstr *sp);
73 coord *table; /* sectors already seen */
77 struct nstr_sect nstr;
87 * first arg should be the range of sectors
89 if (!snxtsct(&nstr, player->argp[1]))
91 nsects = (nstr.range.width + 1) * nstr.range.height / 2;
92 btucost = (nsects / 40) + 1;
93 natp = getnatp(player->cnum);
94 if (natp->nat_btu < btucost) {
95 pr("You don't have the BTU's for spying on that scale!\n");
99 * set up all the goodies we need later
100 * 6 = neighbors, 2 = x,y
102 table = malloc((nsects + 1) * 6 * 2 * sizeof(coord));
103 memset(table, 0, (nsects + 1) * 6 * 2 * sizeof(coord));
106 pr(" old sct rd rl def\n");
107 pr(" sect de own own eff eff eff eff civ mil shl gun pet food bars lnd pln\n");
108 while (nxtsct(&nstr, &from)) {
109 if (!player->owner && !player->god)
113 snxtitem_xy(&ni, EF_LAND, from.sct_x, from.sct_y);
114 while (nxtitem(&ni, &land)) {
116 if (lchr[(int)land.lnd_type].l_flags & L_RECON)
119 if ((military = from.sct_item[I_MILIT]) == 0 && (nunits == 0))
123 /* Print out the units/planes in this sector */
128 * check the neighboring sectors.
130 for (i = 1; i <= 6; i++) {
131 if ((military == 0) && (nunits == 0))
133 nx = x + diroff[i][0];
134 ny = y + diroff[i][1];
136 * if we've already seen the
137 * sector, don't bother checking it
140 if (check(table, &t_len, nx, ny)) {
143 getsect(nx, ny, &dsect);
145 || (dsect.sct_type == SCT_WATER)
146 || (!dsect.sct_item[I_MILIT] && !dsect.sct_item[I_CIVIL]
147 && num_units(nx, ny) == 0)) {
148 /* mark sector as seen */
149 insert(table, &t_len, nx, ny);
152 /* catch spy N/200 chance, N = # military */
153 caught = chance((double)dsect.sct_item[I_MILIT] / 200.0);
155 /* determine spyee relations with spyer */
156 relat = getrel(getnatp(own), player->cnum);
157 if (relat == NEUTRAL && caught) {
159 pr("Spy deported from %s\n", xyas(nx, ny, player->cnum));
161 wu(0, own, "%s (#%d) spy deported from %s\n",
162 cname(player->cnum), player->cnum,
164 } else if (relat < NEUTRAL && caught) {
165 /* at-war with spy-ee */
166 pr("BANG!! A spy was shot in %s\n",
167 xyas(nx, ny, player->cnum));
170 wu(0, own, "%s (#%d) spy caught in %s\n",
171 cname(player->cnum), player->cnum,
173 nreport(player->cnum, N_SPY_SHOT, own, 1);
175 insert(table, &t_len, nx, ny);
177 changed += map_set(player->cnum, dsect.sct_x,
179 dchr[dsect.sct_type].d_mnem, 0);
180 prunits(dsect.sct_x, dsect.sct_y);
181 prplanes(dsect.sct_x, dsect.sct_y);
183 setcont(player->cnum, own, FOUND_SPY);
187 * If you have a recon unit, it'll
188 * see the sector anyway...
190 if (nrecon && caught) {
191 insert(table, &t_len, nx, ny);
193 changed += map_set(player->cnum, dsect.sct_x,
195 dchr[dsect.sct_type].d_mnem, 0);
196 prunits(dsect.sct_x, dsect.sct_y);
197 prplanes(dsect.sct_x, dsect.sct_y);
200 /* subtract any military if necessary */
201 if ((savemil != military) && (savemil > 0)) {
202 if ((military < 0) || (military > savemil))
204 from.sct_item[I_MILIT] = military;
209 writemap(player->cnum);
210 player->btused += btucost;
220 spyline(struct sctstr *sp)
222 prxy("%4d,%-4d", sp->sct_x, sp->sct_y, player->cnum);
223 pr(" %c%c %3d %3d %3d %3d %3d %3d %4d %4d %4d %3d %4d %4d %4d %3d %3d\n",
224 dchr[sp->sct_type].d_mnem,
225 (sp->sct_newtype == sp->sct_type) ? ' ' : dchr[sp->sct_newtype].d_mnem,
228 roundintby((int)sp->sct_effic, 10),
229 roundintby((int)sp->sct_road, 10),
230 roundintby((int)sp->sct_rail, 10),
231 roundintby((int)sp->sct_defense, 10),
232 roundintby(sp->sct_item[I_CIVIL], 10),
233 roundintby(sp->sct_item[I_MILIT], 10),
234 roundintby(sp->sct_item[I_SHELL], 10),
235 roundintby(sp->sct_item[I_GUN], 10),
236 roundintby(sp->sct_item[I_PETROL], 10),
237 roundintby(sp->sct_item[I_FOOD], 10),
238 roundintby(sp->sct_item[I_BAR], 10),
239 count_sect_units(sp),
240 count_sect_planes(sp));
245 * insert a key into the table.
248 insert(coord *table, int *len, coord x, coord y)
250 if (!check(table, len, x, y)) {
257 * see if a key is in the bitmask table
260 check(coord *table, int *len, coord x, coord y)
264 for (i = 0; i < *len; i += 2)
265 if (table[i] == x && table[i + 1] == y)
271 num_units(int x, int y)
277 snxtitem_xy(&ni, EF_LAND, x, y);
278 while (nxtitem(&ni, &land)) {
279 if ((land.lnd_own == player->cnum) || (land.lnd_own == 0))
281 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
290 prunits(int x, int y)
296 snxtitem_xy(&ni, EF_LAND, x, y);
297 while (nxtitem(&ni, &land)) {
298 if (land.lnd_own == player->cnum || land.lnd_own == 0)
300 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
302 /* Don't always see spies */
303 if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
304 if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
307 if ((land.lnd_own != player->cnum) && land.lnd_own) {
311 rel = getrel(getnatp(player->cnum), land.lnd_own);
313 format = "Allied (%s) unit in %s: ";
314 else if (rel == FRIENDLY || rel == NEUTRAL)
315 format = "Neutral (%s) unit in %s: ";
317 format = "Enemy (%s) unit in %s: ";
318 sprintf(report, format, cname(land.lnd_own),
319 xyas(land.lnd_x, land.lnd_y, player->cnum));
320 intelligence_report(player->cnum, &land, 3, report);
326 prplanes(int x, int y)
332 snxtitem_xy(&ni, EF_PLANE, x, y);
333 while (nxtitem(&ni, &plane)) {
334 if (plane.pln_own == player->cnum || plane.pln_own == 0)
336 if (plane.pln_ship >= 0 || plane.pln_land >= 0)
338 if (plane.pln_flags & PLN_LAUNCHED)
340 if ((plane.pln_own != player->cnum) && plane.pln_own) {
344 rel = getrel(getnatp(player->cnum), plane.pln_own);
346 format = "Allied (%s) plane in %s: %s\n";
347 else if (rel == FRIENDLY || rel == NEUTRAL)
348 format = "Neutral (%s) plane in %s: %s\n";
350 format = "Enemy (%s) plane in %s: %s\n";
351 sprintf(report, format, cname(plane.pln_own),
352 xyas(plane.pln_x, plane.pln_y, player->cnum),