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
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 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.
28 * spy.c: Spy on your neighbors
30 * Known contributors to this file:
32 * Steve McClure, 1998-2000
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 int num_units(int, int);
53 static void prplanes(int, int);
54 static void prunits(int, int);
55 static void spyline(struct sctstr *sp);
68 coord *table; /* sectors already seen */
72 struct nstr_sect nstr;
82 * first arg should be the range of sectors
84 if (!snxtsct(&nstr, player->argp[1]))
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");
94 * set up all the goodies we need later
95 * 6 = neighbors, 2 = x,y
97 table = malloc((nsects + 1) * 6 * 2 * sizeof(coord));
98 memset(table, 0, (nsects + 1) * 6 * 2 * sizeof(coord));
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 lnd pln\n");
103 while (nxtsct(&nstr, &from)) {
104 if (!player->owner && !player->god)
108 snxtitem_xy(&ni, EF_LAND, from.sct_x, from.sct_y);
109 while (nxtitem(&ni, &land)) {
111 if (lchr[(int)land.lnd_type].l_flags & L_RECON)
114 if ((military = from.sct_item[I_MILIT]) == 0 && (nunits == 0))
118 /* Print out the units/planes in this sector */
123 * check the neighboring sectors.
125 for (i = 1; i <= 6; i++) {
126 if ((military == 0) && (nunits == 0))
128 nx = x + diroff[i][0];
129 ny = y + diroff[i][1];
131 * if we've already seen the
132 * sector, don't bother checking it
135 if (check(table, &t_len, nx, ny)) {
138 getsect(nx, ny, &dsect);
140 || (dsect.sct_type == SCT_WATER)
141 || (!dsect.sct_item[I_MILIT] && !dsect.sct_item[I_CIVIL]
142 && num_units(nx, ny) == 0)) {
143 /* mark sector as seen */
144 insert(table, &t_len, nx, ny);
149 relat = getrel(getnatp(own), player->cnum);
151 && chance(dsect.sct_item[I_MILIT] / 200.0)) {
153 if (relat == NEUTRAL) {
155 pr("Spy deported from %s\n",
156 xyas(nx, ny, player->cnum));
158 wu(0, own, "%s (#%d) spy deported from %s\n",
159 cname(player->cnum), player->cnum,
163 pr("BANG!! A spy was shot in %s\n",
164 xyas(nx, ny, player->cnum));
167 wu(0, own, "%s (#%d) spy caught in %s\n",
168 cname(player->cnum), player->cnum,
172 setcont(own, player->cnum, FOUND_SPY);
173 if (!nrecon) /* unless you have a recon unit */
174 continue; /* no report from caught spy */
178 insert(table, &t_len, nx, ny);
180 changed += map_set(player->cnum, dsect.sct_x, dsect.sct_y,
181 dchr[dsect.sct_type].d_mnem, 0);
182 prunits(dsect.sct_x, dsect.sct_y);
183 prplanes(dsect.sct_x, dsect.sct_y);
185 setcont(player->cnum, own, FOUND_SPY);
187 /* subtract any military if necessary */
188 if ((savemil != military) && (savemil > 0)) {
189 if ((military < 0) || (military > savemil))
191 from.sct_item[I_MILIT] = military;
196 writemap(player->cnum);
197 player->btused += btucost;
207 spyline(struct sctstr *sp)
209 prxy("%4d,%-4d", sp->sct_x, sp->sct_y, player->cnum);
210 pr(" %c%c %3d %3d %3d %3d %3d %3d %4d %4d %4d %3d %4d %4d %4d %3d %3d\n",
211 dchr[sp->sct_type].d_mnem,
212 sp->sct_newtype == sp->sct_type ? ' ' : dchr[sp->sct_newtype].d_mnem,
215 roundintby((int)sp->sct_effic, 10),
216 roundintby((int)sp->sct_road, 10),
217 roundintby((int)sp->sct_rail, 10),
218 roundintby((int)sp->sct_defense, 10),
219 roundintby(sp->sct_item[I_CIVIL], 10),
220 roundintby(sp->sct_item[I_MILIT], 10),
221 roundintby(sp->sct_item[I_SHELL], 10),
222 roundintby(sp->sct_item[I_GUN], 10),
223 roundintby(sp->sct_item[I_PETROL], 10),
224 roundintby(sp->sct_item[I_FOOD], 10),
225 roundintby(sp->sct_item[I_BAR], 10),
226 count_sect_units(sp),
227 count_sect_planes(sp));
232 * insert a key into the table.
235 insert(coord *table, int *len, coord x, coord y)
237 if (!check(table, len, x, y)) {
244 * see if a key is in the bitmask table
247 check(coord *table, int *len, coord x, coord y)
251 for (i = 0; i < *len; i += 2)
252 if (table[i] == x && table[i + 1] == y)
258 num_units(int x, int y)
264 snxtitem_xy(&ni, EF_LAND, x, y);
265 while (nxtitem(&ni, &land)) {
266 if ((land.lnd_own == player->cnum) || (land.lnd_own == 0))
268 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
277 prunits(int x, int y)
283 snxtitem_xy(&ni, EF_LAND, x, y);
284 while (nxtitem(&ni, &land)) {
285 if (land.lnd_own == player->cnum || land.lnd_own == 0)
287 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
289 /* Don't always see spies */
290 if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
291 if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
294 if ((land.lnd_own != player->cnum) && land.lnd_own) {
298 rel = getrel(getnatp(player->cnum), land.lnd_own);
301 else if (rel == FRIENDLY || rel == NEUTRAL)
305 sprintf(report, "%s (%s) unit in %s: ",
306 relstr, cname(land.lnd_own),
307 xyas(land.lnd_x, land.lnd_y, player->cnum));
308 intelligence_report(player->cnum, &land, 3, report);
314 prplanes(int x, int y)
319 snxtitem_xy(&ni, EF_PLANE, x, y);
320 while (nxtitem(&ni, &plane)) {
321 if (plane.pln_own == player->cnum || plane.pln_own == 0)
323 if (plane.pln_ship >= 0 || plane.pln_land >= 0)
325 if (plane.pln_flags & PLN_LAUNCHED)
327 if ((plane.pln_own != player->cnum) && plane.pln_own) {
331 rel = getrel(getnatp(player->cnum), plane.pln_own);
334 else if (rel == FRIENDLY || rel == NEUTRAL)
338 pr("%s (%s) plane in %s: %s\n",
339 relstr, cname(plane.pln_own),
340 xyas(plane.pln_x, plane.pln_y, player->cnum),