-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashgen.js
99 lines (85 loc) · 1.7 KB
/
hashgen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Project Endless
// by Jonny Bursa
// April 15, 2020
var hash;
const MAPID_SIZE = 4294967296;
const HASH_SIZE = 32;
function inttobytes(a){
buffer = new ArrayBuffer(4);
view = new Int8Array(buffer);
view[3] = a && (255);
view[2] = a && (255);
view[1] = a && (255);
view[0] = a && (255);
return buffer;
}
function genMapKey(){
mapid = Math.floor(MAPID_SIZE*Math.random());
var temp = mapid.toString(16);
while(temp.length < 8){
temp = "0" + temp;
}
return temp;
}
function hashtile(mapkey, ta, tb){
var a, b, x, p;
if(ta < 0){
p = "F";
}else{
p = "0";
}
x = Math.abs(ta);
a = x.toString(16);
while(a.length < 7){
a = "0" + a;
}
a = p + a;
if(tb < 0){
p = "F";
}else{
p = "0";
}
x = Math.abs(tb);
b = x.toString(16);
while(b.length < 7){
b = "0" + b;
}
b = p + b;
// Now, a and b each 8-char long hex strings.
var input = mapkey + a + b;
return md5(input);
}
// hashgen generates unique coordinate within tile
// mapkey = map id
// a,b = tile corner cords
// returns x,y coordinates
function old_hashgen(mapkey, ta, tb){
//digest = crypto.subtle.digest("SHA-1", inttobytes(a));
//digest.then(goodhash, badhash);
var a, xa, b, xb;
if(ta < 0){
xa = Math.abs(ta);
a = "F" + xa.toString(16);
}else{
a = "0" + ta.toString(16);
}
if(tb < 0){
xb = Math.abs(tb);
b = "F" + xb.toString(16);
}else{
b = "0" + tb.toString(16);
}
var raw = mapkey + a + b;
var rlen = raw.length;
var input = parseInt(raw, 16) - Math.pow(16,rlen) / 2;
var raw_out = (Math.sin(input) + 1) / 2;
if(raw_out == 1){
raw_out = 0;
}
var temp = Math.floor(raw_out*MAPID_SIZE);
var out = temp.toString(16);
while(out.length < 8){
out = "0" + out;
}
return [out, raw, input, raw_out];
}