Skip to content

Instantly share code, notes, and snippets.

@XMPPwocky
Created April 30, 2015 19:32
Show Gist options
  • Select an option

  • Save XMPPwocky/dc8c7cc203ba8281f910 to your computer and use it in GitHub Desktop.

Select an option

Save XMPPwocky/dc8c7cc203ba8281f910 to your computer and use it in GitHub Desktop.
(function(n, z) {
function ea() {
x = J = document.getElementById("canvas");
d = x.getContext("2d");
x.onmousedown = function(a) {
K = a.clientX;
L = a.clientY;
M();
V()
};
x.onmousemove = function(a) {
K = a.clientX;
L = a.clientY;
M()
};
x.onmouseup = function(a) {};
n.onkeydown = function(a) {
32 == a.keyCode && null != g && g.readyState == g.OPEN && (a = new ArrayBuffer(1), (new DataView(a)).setUint8(0, 17), g.send(a))
};
n.onresize = W;
W();
n.requestAnimationFrame ? n.requestAnimationFrame(X) : setInterval(N, 1E3 / 60);
setInterval(V, 100);
Y(z("#region").val())
}
function fa() {
for (var a =
Number.POSITIVE_INFINITY, c = Number.POSITIVE_INFINITY, b = Number.NEGATIVE_INFINITY, f = Number.NEGATIVE_INFINITY, A = 0, e = 0; e < l.length; e++) A = Math.max(l[e].size, A), a = Math.min(l[e].x, a), c = Math.min(l[e].y, c), b = Math.max(l[e].x, b), f = Math.max(l[e].y, f);
O = QUAD.init({
minX: a - (A + 100),
minY: c - (A + 100),
maxX: b + (A + 100),
maxY: f + (A + 100)
});
for (e = 0; e < l.length; e++)
for (a = l[e], c = 0; c < a.points.length; ++c) O.insert(a.points[c])
}
function M() {
F = (K - q / 2) / p + t;
G = (L - r / 2) / p + u
}
function Z() {
z.ajax("http://m.agar.io/", {
error: function() {
setTimeout(Z,
1E3)
},
success: function(a) {
a = a.split("\n");
$("ws://" + a[0])
},
dataType: "text",
method: "POST",
cache: !1,
crossDomain: !0,
data: P || "?"
})
}
function Q() {
g && (g.onopen = null, g.onmessage = null, g.onclose = null, g.close());
z("#connecting").show();
Z()
}
function $(a) {
D = [];
h = [];
v = {};
l = [];
B = [];
w = [];
console.log("Connecting to " + a);
g = new WebSocket(a);
g.binaryType = "arraybuffer";
g.onopen = ga;
g.onmessage = ha;
g.onclose = ia;
g.onerror = function() {
console.log("socket error")
}
}
function ga(a) {
z("#connecting").hide();
console.log("socket open");
a = new ArrayBuffer(5);
var c = new DataView(a);
c.setUint8(0, 255);
c.setUint32(1, 1, !0);
g.send(a);
aa()
}
function ia(a) {
console.log("socket close");
setTimeout(Q, 500)
}
function ha(a) {
function c() {
for (var a = "";;) {
var c = f.getUint16(b, !0);
b += 2;
if (0 == c) break;
a += String.fromCharCode(c)
}
return a
}
var b = 1,
f = new DataView(a.data);
switch (f.getUint8(0)) {
case 16:
ja(f);
break;
case 32:
D.push(f.getUint32(1, !0));
break;
case 48:
for (w = []; b < f.byteLength;) w.push(c());
ka();
break;
case 64:
R = f.getFloat64(1, !0), S = f.getFloat64(9, !0), T = f.getFloat64(17, !0), U = f.getFloat64(25, !0), 0 == h.length && (t = (T + R) / 2, u = (U + S) / 2)
}
}
function ja(a) {
C = +new Date;
for (var c = Math.random(), b = 1, f = a.getUint16(b, !0), b = b + 2, d = 0; d < f; ++d) {
var e = v[a.getUint32(b, !0)],
k = v[a.getUint32(b + 4, !0)],
b = b + 8;
e && k && (-1 != h.indexOf(k) && 1 == h.length && z("#overlays").fadeIn(3E3), k.destroy(), k.ox = k.x, k.oy = k.y, k.oSize = k.size, k.nx = e.x, k.ny = e.y, k.nSize = 1, k.updateTime = C)
}
for (;;) {
f = a.getUint32(b, !0);
b += 4;
if (0 == f) break;
var d = a.getFloat64(b, !0),
b = b + 8,
e = a.getFloat64(b, !0),
b = b + 8,
k = a.getFloat64(b, !0),
b = b + 8,
g =
a.getUint8(b++),
H;
for (H = "";;) {
var m = a.getUint16(b, !0),
b = b + 2;
if (0 == m) break;
H += String.fromCharCode(m)
}
m = null;
v.hasOwnProperty(f) ? (m = v[f], m.updatePos(), m.ox = m.x, m.oy = m.y, m.oSize = m.size) : m = new ba(f, d, e, k, g, H);
m.nx = d;
m.ny = e;
m.nSize = k;
m.updateCode = c;
m.updateTime = C; - 1 != D.indexOf(f) && -1 == h.indexOf(m) && (document.getElementById("overlays").style.display = "none", h.push(m), 1 == h.length && (t = m.x, u = m.y))
}
a.getUint16(b, !0);
b += 2;
e = a.getUint32(b, !0);
b += 4;
for (d = 0; d < e; d++) f = a.getUint32(b, !0), b += 4, v[f] && (v[f].updateCode =
c);
for (d = 0; d < l.length; d++) l[d].updateCode != c && l[d--].destroy()
}
function V() {
if (null != g && g.readyState == g.OPEN && (ca != F || da != G)) {
ca = F;
da = G;
var a = new ArrayBuffer(21),
c = new DataView(a);
c.setUint8(0, 16);
c.setFloat64(1, F, !0);
c.setFloat64(9, G, !0);
c.setUint32(17, 0, !0);
g.send(a)
}
}
function aa() {
if (null != g && g.readyState == g.OPEN && null != E) {
var a = new ArrayBuffer(1 + 2 * E.length),
c = new DataView(a);
c.setUint8(0, 0);
for (var b = 0; b < E.length; ++b) c.setUint16(1 + 2 * b, E.charCodeAt(b), !0);
g.send(a)
}
}
function Y(a) {
a && a != P && (P = a,
Q())
}
function X() {
N();
n.requestAnimationFrame(X)
}
function W() {
q = n.innerWidth;
r = n.innerHeight;
J.width = x.width = q;
J.height = x.height = r;
N()
}
function la() {
for (var a = 0, c = 0; c < h.length; c++) a += h[c].size;
a = Math.pow(Math.min(64 / a, 1), .25) * Math.max(r / 965, q / 1920);
p = (9 * p + a) / 10
}
function N() {
var a = +new Date;
++ma;
la();
C = +new Date;
fa();
if (0 < h.length) {
for (var c = 0, b = 0, f = 0; f < h.length; f++) h[f].updatePos(), c += h[f].x / h.length, b += h[f].y / h.length;
t = (t + c) / 2;
u = (u + b) / 2
}
M();
d.clearRect(0, 0, q, r);
d.fillStyle = "#F2FBFF";
d.fillRect(0,
0, q, r);
d.save();
d.fillStyle = "#000000";
d.strokeStyle = "#000000";
d.globalAlpha = .2;
d.scale(p, p);
c = q / p;
b = r / p;
for (f = -.5 + (-t + c / 2) % 50; f < c; f += 50) d.beginPath(), d.moveTo(f, 0), d.lineTo(f, b), d.stroke();
for (f = -.5 + (-u + b / 2) % 50; f < b; f += 50) d.beginPath(), d.moveTo(0, f), d.lineTo(c, f), d.stroke();
d.restore();
l.sort(function(a, b) {
return a.size == b.size ? a.id - b.id : a.size - b.size
});
d.save();
d.translate(q / 2, r / 2);
d.scale(p, p);
d.translate(-t, -u);
for (f = 0; f < B.length; f++) B[f].draw();
for (f = 0; f < l.length; f++) l[f].draw();
d.restore();
y &&
0 != w.length && d.drawImage(y, q - y.width - 10, 10);
a = +new Date - a;
a > 1E3 / 60 ? s -= .01 : a < 1E3 / 65 && (s += .01);.4 > s && (s = .4);
1 < s && (s = 1)
}
function ka() {
if (0 != w.length) {
y = document.createElement("canvas");
var a = y.getContext("2d"),
c = 60 + 24 * w.length;
y.width = 200;
y.height = c;
a.globalAlpha = .4;
a.fillStyle = "#000000";
a.fillRect(0, 0, 200, c);
a.globalAlpha = 1;
a.fillStyle = "#FFFFFF";
c = null;
c = "Scoreboard";
a.font = "30px Ubuntu";
a.fillText(c, 100 - a.measureText(c).width / 2, 40);
a.font = "20px Ubuntu";
for (var b = 0; b < w.length; ++b) c = b + 1 + ". " + (w[b] ||
"An unnamed cell"), a.fillText(c, 100 - a.measureText(c).width / 2, 70 + 24 * b)
}
}
function ba(a, c, b, f, d, e) {
l.push(this);
v[a] = this;
this.id = a;
this.ox = this.x = c;
this.oy = this.y = b;
this.oSize = this.size = f;
if (0 == d) this.isVirus = !0, this.color = "#33FF33";
else {
a = 63487 | d << 16;
b = (a >> 16 & 255) / 255 * 360;
d = (a >> 8 & 255) / 255;
a = (a >> 0 & 255) / 255;
if (0 == d) a = a << 16 | a << 8 | a << 0;
else {
b /= 60;
c = ~~b;
var k = b - c;
b = a * (1 - d);
f = a * (1 - d * k);
d = a * (1 - d * (1 - k));
var g = k = 0,
h = 0;
switch (c % 6) {
case 0:
k = a;
g = d;
h = b;
break;
case 1:
k = f;
g = a;
h = b;
break;
case 2:
k = b;
g = a;
h = d;
break;
case 3:
k =
b;
g = f;
h = a;
break;
case 4:
k = d;
g = b;
h = a;
break;
case 5:
k = a, g = b, h = f
}
a = (~~(255 * k) & 255) << 16 | (~~(255 * g) & 255) << 8 | ~~(255 * h) & 255
}
for (a = a.toString(16); 6 > a.length;) a = "0" + a;
this.color = "#" + a
}
this.points = [];
this.pointsAcc = [];
this.createPoints();
this.setName(e)
}
if ("agar.io" != n.location.hostname && "localhost" != n.location.hostname) n.location = "http://agar.io/";
else {
var J, d, x, q, r, O = null,
g = null,
t = 0,
u = 0,
D = [],
h = [],
v = {},
l = [],
B = [],
w = [],
K = 0,
L = 0,
F = -1,
G = -1,
ma = 0,
C = 0,
E = null,
R = 0,
S = 0,
T = 1E4,
U = 1E4,
p = 1,
P = null;
n.setNick = function(a) {
E = a;
aa();
z("#overlays").hide()
};
n.connect = $;
var ca = -1,
da = -1;
n.setRegion = Y;
n.forceConnect = function() {
(!g || g.readyState != g.CONNECTING && g.readyState != g.OPEN) && Q()
};
var y = null,
s = 1,
I = {};
ba.prototype = {
id: 0,
points: null,
pointsAcc: null,
name: null,
cachedName: null,
x: 0,
y: 0,
size: 0,
ox: 0,
oy: 0,
oSize: 0,
nx: 0,
ny: 0,
nSize: 0,
updateTime: 0,
updateCode: 0,
drawTime: 0,
destroyed: !1,
isVirus: !1,
destroy: function() {
var a;
for (a = 0; a < l.length; a++)
if (l[a] == this) {
l.splice(a, 1);
break
}
delete v[this.id];
a = h.indexOf(this); - 1 != a && h.splice(a, 1);
a = D.indexOf(this.id); - 1 != a && D.splice(a, 1);
this.destroyed = !0;
B.push(this)
},
getNameSize: function() {
return Math.max(~~(.3 * this.size), 24)
},
setName: function(a) {
if (this.name = a) {
var c = document.createElement("canvas"),
b = c.getContext("2d"),
d = this.getNameSize(),
g = d - 2 + "px Ubuntu";
b.font = g;
var e = b.measureText(a).width;
c.width = e + 6;
c.height = d + 10;
b.font = g;
b.globalAlpha = 1;
b.lineWidth = 3;
b.strokeStyle = "#000000";
b.fillStyle = "#FFFFFF";
b.strokeText(a, 3, d - 5);
b.fillText(a, 3, d - 5);
this.cachedName = c
}
},
createPoints: function() {
for (var a =
this.getNumPoints(); this.points.length > a;) this.points.splice(~~(Math.random() * this.points.length), 1);
0 == this.points.length && 0 < a && (this.points.push({
c: this,
v: this.size,
x: this.x,
y: this.y
}), this.pointsAcc.push(Math.random() - .5));
for (; this.points.length < a;) {
var c = ~~(Math.random() * this.points.length),
b = this.points[c];
this.points.splice(c, 0, {
c: this,
v: b.v,
x: b.x,
y: b.y
});
this.pointsAcc.splice(c, 0, this.pointsAcc[c])
}
},
getNumPoints: function() {
return ~~Math.max(this.size * p * (this.isVirus ? Math.min(2 * s, 1) : s), this.isVirus ?
10 : 5)
},
movePoints: function() {
this.createPoints();
for (var a = this.points, c = this.pointsAcc, b = c.concat(), d = a.concat(), g = d.length, e = 0; e < g; ++e) {
var k = b[(e - 1 + g) % g],
h = b[(e + 1) % g];
c[e] += Math.random() - .5;
c[e] *= .7;
10 < c[e] && (c[e] = 10); - 10 > c[e] && (c[e] = -10);
c[e] = (k + h + 8 * c[e]) / 10
}
for (e = 0; e < g; ++e) {
var b = d[e].v,
k = d[(e - 1 + g) % g].v,
h = d[(e + 1) % g].v,
l = !1,
m = this,
n = a[e].x,
p = a[e].y;
O.retrieve({
x: n - 5,
y: p - 5,
w: 10,
h: 10
}, function(a) {
a.c != m && 25 > (n - a.x) * (n - a.x) + (p - a.y) * (p - a.y) && (l = !0)
});
!l && (a[e].x < R || a[e].y < S || a[e].x > T || a[e].y > U) &&
(l = !0);
l && (0 < c[e] && (c[e] = 0), c[e] -= 1);
b += c[e];
0 > b && (b = 0);
b = (9 * b + this.size) / 10;
a[e].v = (k + h + 8 * b) / 10;
k = 2 * Math.PI / g;
h = this.points[e].v;
this.isVirus && 0 == e % 2 && (h += 5);
a[e].x = this.x + Math.cos(k * e) * h;
a[e].y = this.y + Math.sin(k * e) * h
}
},
updatePos: function() {
var a;
a = (C - this.updateTime) / 120;
a = 0 > a ? 0 : 1 < a ? 1 : a;
a = a * a * (3 - 2 * a);
var c = this.getNameSize();
if (this.destroyed && 1 <= a) {
var b = B.indexOf(this); - 1 != b && B.splice(b, 1)
}
this.x = a * (this.nx - this.ox) + this.ox;
this.y = a * (this.ny - this.oy) + this.oy;
this.size = a * (this.nSize - this.oSize) +
this.oSize;
this.destroyed || c == this.getNameSize() || this.setName(this.name);
return a
},
draw: function() {
if (!(this.x + this.size + 20 < t - q / 2 / p || this.y + this.size + 20 < u - r / 2 / p || this.x - this.size - 20 > t + q / 2 / p || this.y - this.size - 20 > u + r / 2 / p)) {
d.save();
this.drawTime = C;
var a = this.updatePos();
this.destroyed && (d.globalAlpha *= 1 - a);
this.movePoints();
d.fillStyle = this.color;
d.strokeStyle = this.color;
d.beginPath();
d.lineWidth = 10;
d.lineCap = "round";
d.lineJoin = this.isVirus ? "mitter" : "round";
for (var a = this.getNumPoints(), c = 0; c <= a; ++c) {
var b =
c % a;
0 == c ? d.moveTo(this.points[b].x, this.points[b].y) : d.lineTo(this.points[b].x, this.points[b].y)
}
d.closePath();
a = this.name;
a = a.toLowerCase(); - 1 != "poland;usa;china;russia;canada;australia;spain;brazil;germany;ukraine;france;sweden;north korea;south korea;japan;united kingdom;earth;greece;latvia;lithuania;estonia;finland;norway".split(";").indexOf(a) ? (I.hasOwnProperty(a) || (I[a] = new Image, I[a].src = "skins/" + a + ".png"), a = I[a]) : a = null;
d.stroke();
d.fill();
null != a && 0 < a.width && (d.save(), d.clip(), d.drawImage(a,
this.x - this.size, this.y - this.size, 2 * this.size, 2 * this.size), d.restore());
15 < this.size && (d.strokeStyle = "#000000", d.globalAlpha *= .1, d.stroke());
this.name && this.cachedName && (d.globalAlpha = 1, d.drawImage(this.cachedName, ~~this.x - ~~(this.cachedName.width / 2), ~~this.y - ~~(this.cachedName.height / 2)));
d.restore()
}
}
};
n.onload = ea
}
})(window, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment