Skip to content

Commit 54e507a

Browse files
authored
Merge pull request #403 from Mygod/cyrb53-offset
Use pseudorandom offsets
2 parents 0f99878 + fabddc0 commit 54e507a

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

src/components/tiles/Pokemon.jsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-bitwise */
12
import React, { memo, useRef, useState } from 'react'
23
import { Marker, Popup, Circle } from 'react-leaflet'
34

@@ -16,12 +17,28 @@ const operator = {
1617
'>=': (a, b) => a >= b,
1718
}
1819

19-
const getOffset = (coords, type) => coords.map(coord => {
20-
let offset = Math.random() * 0.0002 - 0.0001
20+
const cyrb53 = (str, seed = 0) => {
21+
let h1 = 0xdeadbeef ^ seed
22+
let h2 = 0x41c6ce57 ^ seed
23+
for (let i = 0, ch; i < str.length; i += 1) {
24+
ch = str.charCodeAt(i)
25+
h1 = Math.imul(h1 ^ ch, 2654435761)
26+
h2 = Math.imul(h2 ^ ch, 1597334677)
27+
}
28+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909)
29+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909)
30+
return [h1, h2]
31+
}
32+
33+
const getOffset = (coords, type, seed) => {
2134
const offOffset = type === 'nearby_cell' ? 0.0002 : 0.00015
22-
offset += offset >= 0 ? -offOffset : offOffset
23-
return (coord + offset)
24-
})
35+
const rand = cyrb53(seed)
36+
return [0, 1].map(i => {
37+
let offset = rand[i] * (0.0002 / 4294967296) - 0.0001
38+
offset += offset >= 0 ? -offOffset : offOffset
39+
return (coords[i] + offset)
40+
})
41+
}
2542

2643
const getGlowStatus = (item, userSettings, staticUserSettings) => {
2744
let glowCount = 0
@@ -58,7 +75,7 @@ const PokemonTile = ({
5875
const weatherCheck = item.weather && userSettings.weatherIndicator
5976

6077
const finalLocation = item.seen_type?.startsWith('nearby')
61-
? getOffset([item.lat, item.lon], item.seen_type)
78+
? getOffset([item.lat, item.lon], item.seen_type, item.id)
6279
: [item.lat, item.lon]
6380

6481
useForcePopup(item.id, markerRef, params, setParams, done)

0 commit comments

Comments
 (0)