Skip to content

bug(latent): spider.ts encodes hunt tile key with hardcoded <<7 instead of SURFACE_GRID_WIDTH #156

@LightAxe

Description

@LightAxe

Location

src/sim/spider.ts lines ~93 and ~109

Description

findHuntTarget encodes and decodes hunt tile keys using a hardcoded 7-bit shift:

const key = (ay << 7) + ax;   // encode
bestX = k & 127;               // decode ax
bestY = k >> 7;                // decode ay

The inline comment acknowledges the assumption: // ay * SURFACE_GRID_WIDTH(128) + ax; 128 = 2^7.

tile-key.ts imports SURFACE_GRID_WIDTH from constants.ts and derives its stride constant from it:

import { SURFACE_GRID_WIDTH } from './constants.js';
const TILE_KEY_STRIDE = SURFACE_GRID_WIDTH;

spider.ts does not — it hardcodes 128 = 2^7 directly.

Impact

Currently benign: SURFACE_GRID_WIDTH = 128 and 128 = 2^7. If SURFACE_GRID_WIDTH is ever changed (e.g. a larger map config), spider.ts silently computes wrong tile coordinates from the packed key. The spider would evaluate ant density at incorrect tiles, misidentifying hunt targets. No compile-time error, no assertion failure, no test failure — silent wrong behaviour.

Fix

Replace the literal shift with a constant derived from SURFACE_GRID_WIDTH, or use the same TILE_KEY_STRIDE approach as tile-key.ts. Add a compile-time assertion (as tile-key.ts does for its own encoding) so future changes to SURFACE_GRID_WIDTH cause a build error here rather than a silent bug.

Severity

Medium — latent bug, currently safe, no behavioural change until map dimensions change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions