Skip to content

Commit

Permalink
Merge pull request FreezingMoon#2468 from andretchen0/hashing
Browse files Browse the repository at this point in the history
refactor: hashing, offsetNeighbors
  • Loading branch information
DreadKnight authored Aug 8, 2023
2 parents d7801cb + a6e3f89 commit 683b5f4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
33 changes: 33 additions & 0 deletions src/utility/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,42 @@ import { Point } from './pointfacade';

export const HEX_WIDTH_PX = 90;
export const HEX_HEIGHT_PX = (HEX_WIDTH_PX / Math.sqrt(3)) * 2 * 0.75;

export function offsetCoordsToPx(point: Point) {
return {
x: (point.y % 2 === 0 ? point.x + 0.5 : point.x) * HEX_WIDTH_PX,
y: point.y * HEX_HEIGHT_PX,
};
}

const n2_16 = Math.pow(2, 16);

function isValid(point: Point) {
return 0 <= point.x && point.x < n2_16 && 0 <= point.y && point.y < n2_16;
}

export function offsetNeighbors(point: Point): Point[] {
if (point.y % 2 === 0) {
return [
{ x: point.x + 1, y: point.y },
{ x: point.x + 1, y: point.y + 1 },
{ x: point.x, y: point.y + 1 },
{ x: point.x - 1, y: point.y },
{ x: point.x, y: point.y - 1 },
{ x: point.x + 1, y: point.y - 1 },
].filter(isValid);
} else {
return [
{ x: point.x + 1, y: point.y },
{ x: point.x, y: point.y + 1 },
{ x: point.x - 1, y: point.y + 1 },
{ x: point.x - 1, y: point.y },
{ x: point.x - 1, y: point.y - 1 },
{ x: point.x, y: point.y - 1 },
].filter(isValid);
}
}

export function hashOffsetCoords(point: Point) {
return (point.x << 16) ^ point.y;
}
35 changes: 19 additions & 16 deletions src/utility/pointfacade.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Creature } from '../creature';
import { Drop } from '../drop';
import { hashOffsetCoords as hash } from './const';
import { Trap } from './trap';

export type Point = {
Expand All @@ -22,10 +23,10 @@ type PointFacadeConfig = {
};

class PointSet {
s: Set<string>;
s: Set<number>;
config: PointFacadeConfig;

constructor(s: Set<string>, config: PointFacadeConfig) {
constructor(s: Set<number>, config: PointFacadeConfig) {
this.s = s;
this.config = config;
}
Expand Down Expand Up @@ -60,7 +61,7 @@ export class PointFacade {
}

getBlockedSet(): PointSet {
const blockedSet = new Set<string>();
const blockedSet = new Set<number>();
for (const c of this.config.getCreatures()) {
for (const point of this.config.getCreatureBlockedPoints(c)) {
blockedSet.add(hash(point));
Expand All @@ -87,9 +88,11 @@ export class PointFacade {
getCreaturesAt(point: Point | Creature | Point[] | number, y = 0) {
const config = this.config;

const points: Point[] = normalize(point, y, this.config);
const pointsToStr = points.map(hash).join('');
const hasPoint = (p: Point) => pointsToStr.indexOf(hash(p)) !== -1;
const points: Set<number> = normalize(point, y, this.config).reduce((s, p) => {
s.add(hash(p));
return s;
}, new Set<number>());
const hasPoint = (p: Point) => points.has(hash(p));

return config
.getCreatures()
Expand All @@ -103,9 +106,11 @@ export class PointFacade {
getTrapsAt(point: Point | Creature | Point[] | number, y = 0) {
const config = this.config;

const points: Point[] = normalize(point, y, this.config);
const pointsToStr = points.map(hash).join('');
const hasPoint = (p: Point) => pointsToStr.indexOf(hash(p)) !== -1;
const points: Set<number> = normalize(point, y, this.config).reduce((s, p) => {
s.add(hash(p));
return s;
}, new Set<number>());
const hasPoint = (p: Point) => points.has(hash(p));

return config
.getTraps()
Expand All @@ -119,9 +124,11 @@ export class PointFacade {
getDropsAt(point: Point | Creature | Point[] | number, y = 0) {
const config = this.config;

const points: Point[] = normalize(point, y, this.config);
const pointsToStr = points.map(hash).join('');
const hasPoint = (p: Point) => pointsToStr.indexOf(hash(p)) !== -1;
const points: Set<number> = normalize(point, y, this.config).reduce((s, p) => {
s.add(hash(p));
return s;
}, new Set<number>());
const hasPoint = (p: Point) => points.has(hash(p));

return config
.getDrops()
Expand All @@ -133,10 +140,6 @@ export class PointFacade {
}
}

function hash(point: Point) {
return `(${point.x},${point.y})`;
}

function getMissingConfigRequirements(config: PointFacadeConfig): string[] {
const missing: string[] = [];
if (!config.getCreatures) {
Expand Down

0 comments on commit 683b5f4

Please sign in to comment.