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
56 static void spyline(struct sctstr *sp);
57 static void insert(coord *table, int *len, coord x, coord y);
58 static int check(coord *table, int *len, coord x, coord y);
72 coord *table; /* sectors already seen */
78 struct nstr_sect nstr;
88 * first arg should be the range of sectors
90 if (!snxtsct(&nstr, player->argp[1]))
92 nsects = (nstr.range.width + 1) * nstr.range.height / 2;
93 btucost = (nsects / 40) + 1;
94 natp = getnatp(player->cnum);
95 if (natp->nat_btu < btucost) {
96 pr("You don't have the BTU's for spying on that scale!\n");
100 * set up all the goodies we need later
101 * 6 = neighbors, 2 = x,y
103 table = (coord *)malloc((nsects + 1) * 6 * 2 * sizeof(coord));
104 bzero((s_char *)table, (nsects + 1) * 6 * 2 * sizeof(coord));
107 pr(" old sct rd rl def\n");
108 pr(" sect de own own eff eff eff eff civ mil shl gun pet food bars lnd pln\n");
109 while (nxtsct(&nstr, &from)) {
110 if (!player->owner && !player->god)
112 getvec(VT_ITEM, vec, (s_char *)&from, EF_SECTOR);
115 snxtitem_xy(&ni, EF_LAND, from.sct_x, from.sct_y);
116 while (nxtitem(&ni, (s_char *)&land)) {
118 if (lchr[(int)land.lnd_type].l_flags & L_RECON)
121 if ((military = vec[I_MILIT]) == 0 && (nunits == 0))
125 /* Print out the units/planes in this sector */
130 * check the neighboring sectors.
132 for (i = 1; i <= 6; i++) {
133 if ((military == 0) && (nunits == 0))
135 nx = x + diroff[i][0];
136 ny = y + diroff[i][1];
138 * if we've already seen the
139 * sector, don't bother checking it
142 if (check(table, &t_len, nx, ny)) {
145 getsect(nx, ny, &dsect);
146 getvec(VT_ITEM, dvec, (s_char *)&dsect, EF_SECTOR);
147 if (player->owner || (dsect.sct_type == SCT_WATER) ||
148 (!dvec[I_MILIT] && !dvec[I_CIVIL] &&
149 (num_units(nx, ny) == 0))) {
150 /* mark sector as seen */
151 insert(table, &t_len, nx, ny);
154 /* catch spy N/200 chance, N = # military */
155 caught = chance((double)dvec[I_MILIT] / 200.0);
157 /* determine spyee relations with spyer */
158 relat = getrel(getnatp(own), player->cnum);
159 if (relat == NEUTRAL && caught) {
161 pr("Spy deported from %s\n", xyas(nx, ny, player->cnum));
163 wu(0, own, "%s (#%d) spy deported from %s\n",
164 cname(player->cnum), player->cnum,
166 } else if (relat < NEUTRAL && caught) {
167 /* at-war with spy-ee */
168 pr("BANG!! A spy was shot in %s\n",
169 xyas(nx, ny, player->cnum));
172 wu(0, own, "%s (#%d) spy caught in %s\n",
173 cname(player->cnum), player->cnum,
175 nreport(player->cnum, N_SPY_SHOT, own, 1);
177 insert(table, &t_len, nx, ny);
179 changed += map_set(player->cnum, dsect.sct_x,
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);
189 * If you have a recon unit, it'll
190 * see the sector anyway...
192 if (nrecon && caught) {
193 insert(table, &t_len, nx, ny);
195 changed += map_set(player->cnum, dsect.sct_x,
197 dchr[dsect.sct_type].d_mnem, 0);
198 prunits(dsect.sct_x, dsect.sct_y);
199 prplanes(dsect.sct_x, dsect.sct_y);
202 /* subtract any military if necessary */
203 if ((savemil != military) && (savemil > 0)) {
204 if ((military < 0) || (military > savemil))
206 vec[I_MILIT] = military;
207 putvec(VT_ITEM, vec, (s_char *)&from, EF_SECTOR);
212 writemap(player->cnum);
213 player->btused += btucost;
214 free((s_char *)table);
223 spyline(struct sctstr *sp)
227 getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
228 prxy("%4d,%-4d", sp->sct_x, sp->sct_y, player->cnum);
229 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));
234 * insert a key into the table.
237 insert(coord *table, int *len, coord x, coord y)
239 if (!check(table, len, x, y)) {
246 * see if a key is in the bitmask table
249 check(coord *table, int *len, coord x, coord y)
253 for (i = 0; i < *len; i += 2)
254 if (table[i] == x && table[i + 1] == y)
260 num_units(int x, int y)
266 snxtitem_xy(&ni, EF_LAND, x, y);
267 while (nxtitem(&ni, (s_char *)&land)) {
268 if ((land.lnd_own == player->cnum) || (land.lnd_own == 0))
270 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
279 prunits(int x, int y)
286 snxtitem_xy(&ni, EF_LAND, x, y);
287 while (nxtitem(&ni, (s_char *)&land)) {
288 if (land.lnd_own == player->cnum || land.lnd_own == 0)
290 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
292 /* Don't always see spies */
293 if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
294 odds = (double)(100 - land.lnd_effic) + 0.10;
298 if ((land.lnd_own != player->cnum) && land.lnd_own) {
302 rel = getrel(getnatp(player->cnum), land.lnd_own);
304 format = "Allied (%s) unit in %s: ";
305 else if (rel == FRIENDLY || rel == NEUTRAL)
306 format = "Neutral (%s) unit in %s: ";
308 format = "Enemy (%s) unit in %s: ";
309 sprintf(report, format, cname(land.lnd_own),
310 xyas(land.lnd_x, land.lnd_y, player->cnum));
311 intelligence_report(player->cnum, &land, 3, report);
317 prplanes(int x, int y)
323 snxtitem_xy(&ni, EF_PLANE, x, y);
324 while (nxtitem(&ni, (s_char *)&plane)) {
325 if (plane.pln_own == player->cnum || plane.pln_own == 0)
327 if (plane.pln_ship >= 0 || plane.pln_land >= 0)
329 if (plane.pln_flags & PLN_LAUNCHED)
331 if ((plane.pln_own != player->cnum) && plane.pln_own) {
335 rel = getrel(getnatp(player->cnum), plane.pln_own);
337 format = "Allied (%s) plane in %s: %s\n";
338 else if (rel == FRIENDLY || rel == NEUTRAL)
339 format = "Neutral (%s) plane in %s: %s\n";
341 format = "Enemy (%s) plane in %s: %s\n";
342 sprintf(report, format, cname(plane.pln_own),
343 xyas(plane.pln_x, plane.pln_y, player->cnum),