Skip to content

Commit

Permalink
Make test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
yishn committed May 15, 2018
1 parent 2d396f3 commit d22efef
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/influenceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,33 @@ module.exports = function(data, {discrete = false, maxDistance = 6, minRadiance

for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
let neighbors = getNeighbors([x, y]).filter(([i, j]) => data[j] && data[j][i] != null)
if (neighbors.length === 0) continue
if (data[y][x] !== 0) continue

// Fix holes and prevent single point areas
// Prevent single point areas

let [i, j] = neighbors[0]
let sign = Math.sign(map[y][x])
let s = sign === 0 ? Math.sign(map[j][i]) : 0

if (sign === 0 && s !== 0 && neighbors.every(([i, j]) => Math.sign(map[j][i]) === s)) {
map[y][x] = neighbors.reduce((sum, [i, j]) => sum + map[j][i], 0) / neighbors.length
if (sign !== 0) {
let neighbors = getNeighbors([x, y])
.filter(([i, j]) => data[j] && data[j][i] != null)

if (discrete) map[y][x] = Math.sign(map[y][x])
continue
} else if (sign !== 0 && data[y][x] === 0 && neighbors.every(([i, j]) => Math.sign(map[j][i]) !== sign)) {
map[y][x] = 0
continue
if (neighbors.length >= 2) {
let [i, j] = neighbors[0]

if (neighbors.every(([i, j]) => Math.sign(map[j][i]) !== sign)) {
map[y][x] = 0
continue
}
}
}

// Fix ragged areas

let distance = Math.min(x, y, width - x - 1, height - y - 1)

if (distance <= 3 && data[y][x] === 0 && sign !== 0) {
let friendlyNeighbors = neighbors
.filter(([i, j]) => Math.sign(map[j][i]) === sign)
if (distance <= 3 && sign !== 0) {
let friendlyNeighbors = getNeighbors([x, y])
.filter(([i, j]) => map[j] && Math.sign(map[j][i]) === sign)

if (friendlyNeighbors.length === 1) {
let [i, j] = friendlyNeighbors[0]
Expand All @@ -68,26 +69,25 @@ module.exports = function(data, {discrete = false, maxDistance = 6, minRadiance

// Fix empty pillars

if (distance <= 2 && map[y][x] === 0) {
let signedNeighbors = neighbors.filter(([i, j]) => map[j][i] !== 0)
if (distance <= 2 && sign === 0) {
let signedNeighbors = getNeighbors([x, y])
.filter(([i, j]) => map[j] && map[j][i] !== 0)

if (signedNeighbors.length >= 2) {
let [[i1, j1], [i2, j2]] = signedNeighbors
let sign = Math.sign(map[j1][i1])
let s = Math.sign(map[j1][i1])

if ((signedNeighbors.length >= 3 || i1 === i2 || j1 === j2)
&& signedNeighbors.every(([i, j]) => Math.sign(map[j][i]) === sign)) {
map[y][x] = signedNeighbors.reduce((sum, [i, j]) => (
sum + map[j][i]
), 0) / signedNeighbors.length

if (discrete) map[y][x] = Math.sign(map[y][x])
&& signedNeighbors.every(([i, j]) => Math.sign(map[j][i]) === s)) {
map[y][x] = !discrete
? signedNeighbors.reduce((sum, [i, j]) => sum + map[j][i], 0) / signedNeighbors.length
: s
continue
}
}
}
}
}

return map
return areaMap(map)
}

0 comments on commit d22efef

Please sign in to comment.