Fix spy to reliably avoid spying same sector more than once

spy() stored coordinates of sectors successfully spied in an array.
Since it didn't bother to normalize coordinates, it could spy sectors
near the "seams" of the world more than once.  The array was also
wastefully large.

Fix by using a sector bitmap instead, like do_look().
This commit is contained in:
Markus Armbruster 2008-08-23 15:17:44 -04:00
parent e9c7491449
commit dda0a4c82f

View file

@ -48,8 +48,6 @@
* format: spy <SECTS> * format: spy <SECTS>
*/ */
static int check(coord *table, int *len, coord x, coord y);
static void insert(coord *table, int *len, coord x, coord y);
static void spy_report(struct sctstr *sp); static void spy_report(struct sctstr *sp);
static void prplanes(int, int); static void prplanes(int, int);
static void prunits(int, int); static void prunits(int, int);
@ -65,8 +63,7 @@ spy(void)
int military; int military;
int btucost; int btucost;
int i; int i;
coord *table; /* sectors already seen */ unsigned char *bitmap;
int t_len = 0;
int nrecon; int nrecon;
struct nstr_sect nstr; struct nstr_sect nstr;
struct nstr_item ni; struct nstr_item ni;
@ -89,12 +86,13 @@ spy(void)
pr("You don't have the BTU's for spying on that scale!\n"); pr("You don't have the BTU's for spying on that scale!\n");
return RET_FAIL; return RET_FAIL;
} }
/* bitmap = calloc(WORLD_SZ() / 8, 1);
* set up all the goodies we need later if (!bitmap) {
* 6 = neighbors, 2 = x,y logerror("malloc failed in do_look\n");
*/ pr("Memory error. Tell the deity.\n");
table = malloc((nsects + 1) * 6 * 2 * sizeof(coord)); return RET_FAIL;
memset(table, 0, (nsects + 1) * 6 * 2 * sizeof(coord)); }
pr("SPY report\n"); pr("SPY report\n");
prdate(); prdate();
pr(" old sct rd rl def\n"); pr(" old sct rd rl def\n");
@ -124,14 +122,8 @@ spy(void)
break; break;
nx = x + diroff[i][0]; nx = x + diroff[i][0];
ny = y + diroff[i][1]; ny = y + diroff[i][1];
/* if (emp_getbit(nx, ny, bitmap))
* if we've already seen the continue; /* spied already */
* sector, don't bother checking it
* out.
*/
if (check(table, &t_len, nx, ny)) {
continue;
}
getsect(nx, ny, &dsect); getsect(nx, ny, &dsect);
if (player->owner || dsect.sct_type == SCT_WATER) if (player->owner || dsect.sct_type == SCT_WATER)
continue; continue;
@ -166,7 +158,7 @@ spy(void)
} }
/* spy report */ /* spy report */
insert(table, &t_len, nx, ny); emp_setbit(nx, ny, bitmap);
spy_report(&dsect); spy_report(&dsect);
changed += map_set(player->cnum, dsect.sct_x, dsect.sct_y, changed += map_set(player->cnum, dsect.sct_x, dsect.sct_y,
dchr[dsect.sct_type].d_mnem, 0); dchr[dsect.sct_type].d_mnem, 0);
@ -182,7 +174,7 @@ spy(void)
if (changed) if (changed)
writemap(player->cnum); writemap(player->cnum);
player->btused += btucost; player->btused += btucost;
free(table); free(bitmap);
return RET_OK; return RET_OK;
} }
@ -210,32 +202,6 @@ spy_report(struct sctstr *sp)
prplanes(sp->sct_x, sp->sct_y); prplanes(sp->sct_x, sp->sct_y);
} }
/*
* insert a key into the table.
*/
static void
insert(coord *table, int *len, coord x, coord y)
{
if (!check(table, len, x, y)) {
table[(*len)++] = x;
table[(*len)++] = y;
}
}
/*
* see if a key is in the bitmask table
*/
static int
check(coord *table, int *len, coord x, coord y)
{
int i;
for (i = 0; i < *len; i += 2)
if (table[i] == x && table[i + 1] == y)
return 1;
return 0;
}
static void static void
prunits(int x, int y) prunits(int x, int y)
{ {