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
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
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);
76 coord *table; /* sectors already seen */
82 struct nstr_sect nstr;
92 * first arg should be the range of sectors
94 if (!snxtsct(&nstr, player->argp[1]))
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");
104 * set up all the goodies we need later
105 * 6 = neighbors, 2 = x,y
107 table = (coord *)malloc((nsects + 1) * 6 * 2 * sizeof(coord));
108 bzero((s_char *)table, (nsects + 1) * 6 * 2 * sizeof(coord));
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)
116 getvec(VT_ITEM, vec, (s_char *)&from, EF_SECTOR);
119 snxtitem_xy(&ni, EF_LAND, from.sct_x, from.sct_y);
120 while (nxtitem(&ni, (s_char *)&land)) {
122 if (lchr[(int)land.lnd_type].l_flags & L_RECON)
125 if ((military = vec[I_MILIT]) == 0 && (nunits == 0))
129 /* Print out the units/planes in this sector */
134 * check the neighboring sectors.
136 for (i = 1; i <= 6; i++) {
137 if ((military == 0) && (nunits == 0))
139 nx = x + diroff[i][0];
140 ny = y + diroff[i][1];
142 * if we've already seen the
143 * sector, don't bother checking it
146 if (check(table, &t_len, nx, ny)) {
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);
158 /* catch spy N/200 chance, N = # military */
159 caught = chance((double)dvec[I_MILIT] / 200.0);
161 /* determine spyee relations with spyer */
162 relat = getrel(getnatp(own), player->cnum);
163 if (relat == NEUTRAL && caught) {
165 pr("Spy deported from %s\n", xyas(nx, ny, player->cnum));
167 wu(0, own, "%s (#%d) spy deported from %s\n",
168 cname(player->cnum), player->cnum,
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));
176 wu(0, own, "%s (#%d) spy caught in %s\n",
177 cname(player->cnum), player->cnum,
179 nreport(player->cnum, N_SPY_SHOT, own, 1);
181 insert(table, &t_len, nx, ny);
183 changed += map_set(player->cnum, dsect.sct_x,
185 dchr[dsect.sct_type].d_mnem, 0);
186 prunits(dsect.sct_x, dsect.sct_y);
187 prplanes(dsect.sct_x, dsect.sct_y);
189 setcont(player->cnum, own, FOUND_SPY);
193 * If you have a recon unit, it'll
194 * see the sector anyway...
196 if (nrecon && caught) {
197 insert(table, &t_len, nx, ny);
199 changed += map_set(player->cnum, dsect.sct_x,
201 dchr[dsect.sct_type].d_mnem, 0);
202 prunits(dsect.sct_x, dsect.sct_y);
203 prplanes(dsect.sct_x, dsect.sct_y);
206 /* subtract any military if necessary */
207 if ((savemil != military) && (savemil > 0)) {
208 if ((military < 0) || (military > savemil))
210 vec[I_MILIT] = military;
211 putvec(VT_ITEM, vec, (s_char *)&from, EF_SECTOR);
216 writemap(player->cnum);
217 player->btused += btucost;
218 free((s_char *)table);
227 spyline(struct sctstr *sp)
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));
238 * insert a key into the table.
241 insert(coord *table, int *len, coord x, coord y)
243 if (!check(table, len, x, y)) {
250 * see if a key is in the bitmask table
253 check(coord *table, int *len, coord x, coord y)
257 for (i = 0; i < *len; i += 2)
258 if (table[i] == x && table[i + 1] == y)
264 num_units(int x, int y)
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))
274 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
283 prunits(int x, int y)
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)
293 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
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))))
300 if ((land.lnd_own != player->cnum) && land.lnd_own) {
304 rel = getrel(getnatp(player->cnum), land.lnd_own);
306 format = "Allied (%s) unit in %s: ";
307 else if (rel == FRIENDLY || rel == NEUTRAL)
308 format = "Neutral (%s) unit in %s: ";
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);
319 prplanes(int x, int y)
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)
329 if (plane.pln_ship >= 0 || plane.pln_land >= 0)
331 if (plane.pln_flags & PLN_LAUNCHED)
333 if ((plane.pln_own != player->cnum) && plane.pln_own) {
337 rel = getrel(getnatp(player->cnum), plane.pln_own);
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";
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),