Guest User

Agario better glitchy bot with toggle mid work version

a guest
May 11th, 2015
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2.  
  3. // @name       Vawlt Agar.io Bot
  4. // @namespace   Vawlt
  5. // @include     http://agar.io/*
  6. // @version     1
  7. // @grant       none
  8. // @author      twitch.tv/apostolique
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. var changed = 0; // script need to be edited with
  13.  
  14. window.addEventListener('beforescriptexecute', function (e) {
  15.  
  16.     //for external script:
  17.     src = e.target.src;
  18.     if (src.search(/main_out\.js..../) != -1) {
  19.         console.log('event listener fired, main_out found');
  20.         changed++;
  21.         e.preventDefault();
  22.         e.stopPropagation();
  23.         append(main_out);
  24.         //init();
  25.     };
  26.  
  27.     //when done, remove the listener:
  28.     if (changed == 1)
  29.         window.removeEventListener(e.type, arguments.callee, true);
  30.  
  31. }, true);
  32.  
  33. ////// append with new block function:
  34. function append(s) {
  35.     console.log('append fired');
  36.     document.head.appendChild(document.createElement('script'))
  37.     .innerHTML = s.toString().replace(/^function.*{|}$/g, '');
  38. }
  39.  
  40. ////////////////////////////////////////////////
  41. function main_out() {
  42. $.getScript('https://raw.githubusercontent.com/maxkueng/victor/master/build/victor.js', function()
  43. {
  44.     (function (self, $) {
  45.         /**
  46.          * @return {undefined}
  47.          */
  48.         function init() {
  49.             console.log("init fired");
  50.             getRegions();
  51.             setInterval(getRegions, 18E4);
  52.             /** @type {(HTMLElement|null)} */
  53.             canvas = tempCanvas = document.getElementById("canvas");
  54.             canvas.width *= 2;
  55.             canvas.height *= 2;
  56.             canvasContext = canvas.getContext("2d");
  57.             canvas.onmousedown = function (e) {
  58.                 if (options) {
  59.                     var z0 = e.clientX - (5 + width / 5 / 2);
  60.                     var z1 = e.clientY - (5 + width / 5 / 2);
  61.  
  62.                     if (Math.sqrt(z0 * z0 + z1 * z1) <= width / 5 / 2) {
  63.                         sendMovementToServer();
  64.                         sendServerCommand(17);
  65.                         return;
  66.                     }
  67.                 }
  68.                 currentMouseX = e.clientX;
  69.                 currentMouseY = e.clientY;
  70.                 recalculateDestination();
  71.                 sendMovementToServer();
  72.             };
  73.             canvas.onmousemove = function (e) {
  74.                 currentMouseX = e.clientX;
  75.                 currentMouseY = e.clientY;
  76.                
  77.                 useMouseX = (currentMouseX - width/2 + cellX*screenRatio) / screenRatio;
  78.                 useMouseY = (currentMouseY - height/2 + cellY*screenRatio) / screenRatio;
  79.                
  80.                 tempPoint = [useMouseX, useMouseY, 1];
  81.                
  82.                 recalculateDestination();
  83.             };
  84.             canvas.onmouseup = function (evt) {};
  85.             var b = false;
  86.             var a = false;
  87.             var all = false;
  88.             self.onkeydown = function (e) {
  89.                 if (84 == e.keyCode) {
  90.                     console.log("Toggle");
  91.                     toggle = !toggle;
  92.                 }
  93.                 if (!(32 != e.keyCode)) {
  94.                     if (!b) {
  95.                         sendMovementToServer();
  96.                         sendServerCommand(17);
  97.                         b = true;
  98.                     }
  99.                 }
  100.                 if (!(69 != e.keyCode)) {
  101.                         sendServerCommand(1);
  102.                 }
  103.                 if (!(81 != e.keyCode)) {
  104.                     if (!a) {
  105.                         sendServerCommand(18);
  106.                         a = true;
  107.                     }
  108.                 }
  109.                 if (!(87 != e.keyCode)) {
  110.                     if (!all) {
  111.                         sendMovementToServer();
  112.                         sendServerCommand(21);
  113.                         all = true;
  114.                     }
  115.                 }
  116.             };
  117.             self.onkeyup = function (event) {
  118.                 if (32 == event.keyCode) {
  119.                     b = false;
  120.                 }
  121.                 if (87 == event.keyCode) {
  122.                     all = false;
  123.                 }
  124.                 if (81 == event.keyCode) {
  125.                     if (a) {
  126.                         sendServerCommand(19);
  127.                         a = false;
  128.                     }
  129.                 }
  130.             };
  131.             self.onblur = function () {
  132.                 sendServerCommand(19);
  133.                 all = a = b = false;
  134.             };
  135.             self.onresize = onResize;
  136.             onResize();
  137.             if (self.requestAnimationFrame) {
  138.                 self.requestAnimationFrame(anim);
  139.             } else {
  140.                 setInterval(draw, 1E3 / 60);
  141.             }
  142.             setInterval(sendMovementToServer, 5);
  143.             done($("#region").val());
  144.         }
  145.         function processData() {
  146.             var v = Number.POSITIVE_INFINITY;
  147.             var j = Number.POSITIVE_INFINITY;
  148.             var bottom = Number.NEGATIVE_INFINITY;
  149.             var maxY = Number.NEGATIVE_INFINITY;
  150.             var newDuration = 0;
  151.             var i = 0;
  152.             for (; i < items.length; i++) {
  153.                 newDuration = Math.max(items[i].size, newDuration);
  154.                 v = Math.min(items[i].x, v);
  155.                 j = Math.min(items[i].y, j);
  156.                 bottom = Math.max(items[i].x, bottom);
  157.                 maxY = Math.max(items[i].y, maxY);
  158.             }
  159.             context = QUAD.init({
  160.                     minX : v - (newDuration + 100),
  161.                     minY : j - (newDuration + 100),
  162.                     maxX : bottom + (newDuration + 100),
  163.                     maxY : maxY + (newDuration + 100)
  164.                 });
  165.             i = 0;
  166.             for (; i < items.length; i++) {
  167.                 if (v = items[i], v.shouldRender()) {
  168.                     j = 0;
  169.                     for (; j < v.points.length; ++j) {
  170.                         context.insert(v.points[j]);
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.         function recalculateDestination() {
  176.             moveX = (currentMouseX - width / 2) / screenRatio + cellX;
  177.             moveY = (currentMouseY - height / 2) / screenRatio + cellY;
  178.         }
  179.         function getRegions() {
  180.             if (null == old) {
  181.                 old = {};
  182.                 $("#region").children().each(function () {
  183.                     var option = $(this);
  184.                     var name = option.val();
  185.                     if (name) {
  186.                         old[name] = option.text();
  187.                     }
  188.                 });
  189.             }
  190.             $.get("http://m.agar.io/info", function (b) {
  191.                 var name;
  192.                 for (name in b.regions) {
  193.                     $('#region option[value="' + name + '"]').text(old[name] + " (" + b.regions[name].numPlayers + " players)");
  194.                 }
  195.             }, "json");
  196.         }
  197.         function done(mat) {
  198.             if (mat) {
  199.                 if (mat != dest) {
  200.                     dest = mat;
  201.                     after();
  202.                 }
  203.             }
  204.         }
  205.         function next() {
  206.             $.ajax("http://m.agar.io/", {
  207.                 error : function () {
  208.                     setTimeout(next, 1E3);
  209.                 },
  210.                 success : function (status) {
  211.                     status = status.split("\n");
  212.                     open("ws://" + status[0]);
  213.                 },
  214.                 dataType : "text",
  215.                 method : "POST",
  216.                 cache : false,
  217.                 crossDomain : true,
  218.                 data : dest || "?"
  219.             });
  220.         }
  221.         function after() {
  222.             $("#connecting").show();
  223.             next();
  224.         }
  225.         function open(url) {
  226.             //Verify existing connection.
  227.             if (ws) {
  228.                 ws.onopen = null;
  229.                 ws.onmessage = null;
  230.                 ws.onclose = null;
  231.                 ws.close();
  232.                 ws = null;
  233.             }
  234.             //Start new connection.
  235.             bucket = [];
  236.             playerCell = [];
  237.             nodes = {};
  238.             items = [];
  239.             destroyedCells = [];
  240.             leaderboardPlayers = [];
  241.             console.log("Connecting to " + url);
  242.             ws = new WebSocket(url);
  243.             ws.binaryType = "arraybuffer";
  244.             ws.onopen = listener;
  245.             ws.onmessage = parse;
  246.             ws.onclose = report;
  247.             ws.onerror = function () {
  248.                 console.log("socket error");
  249.             };
  250.         }
  251.         function listener(data) {
  252.             $("#connecting").hide();
  253.             console.log("socket open");
  254.             data = new ArrayBuffer(5);
  255.             var view = new DataView(data);
  256.             view.setUint8(0, 255);
  257.             view.setUint32(1, 1, true);
  258.             ws.send(data);
  259.             close();
  260.         }
  261.         function report(failing_message) {
  262.             console.log("socket close");
  263.             setTimeout(after, 500);
  264.         }
  265.         function parse(target) {
  266.             function encode() {
  267.                 var utftext = "";
  268.                 for (; ; ) {
  269.                     var c = d.getUint16(i, true);
  270.                     i += 2;
  271.                     if (0 == c) {
  272.                         break;
  273.                     }
  274.                     utftext += String.fromCharCode(c);
  275.                 }
  276.                 return utftext;
  277.             }
  278.             var i = 1;
  279.             var d = new DataView(target.data);
  280.             switch (d.getUint8(0)) {
  281.             case 16:
  282.                 //console.log("Code 16");
  283.                 run(d);
  284.                 break;
  285.             case 20:
  286.                 //Likely for when the server resets.
  287.                 console.log("Code 20");
  288.                 playerCell = [];
  289.                 bucket = [];
  290.                 break;
  291.             case 32:
  292.                 console.log("Code 32");
  293.                 bucket.push(d.getUint32(1, true));
  294.                 break;
  295.             case 48:
  296.                 console.log("Code 48");
  297.                 leaderboardPlayers = [];
  298.                 for (; i < d.byteLength; ) {
  299.                     tempName = encode();
  300.                     console.log("Name: " + tempName);
  301.                     leaderboardPlayers.push({
  302.                         id : 0,
  303.                         name : tempName
  304.                     });
  305.                 }
  306.                 drawLeaderboard();
  307.                 break;
  308.             case 49:
  309.                 //Parses the leaderboard.
  310.                 target = d.getUint32(i, true);
  311.                 i += 4;
  312.                 leaderboardPlayers = [];
  313.                 var seek = 0;
  314.                 for (; seek < target; ++seek) {
  315.                     var r = d.getUint32(i, true);
  316.                     i = i + 4;
  317.                     leaderboardPlayers.push({
  318.                         id : r,
  319.                         name : encode()
  320.                     });
  321.                 }
  322.                 drawLeaderboard();
  323.                 break;
  324.             case 64:
  325.                 console.log("Code 64");
  326.                 left = d.getFloat64(1, true);
  327.                 bottom = d.getFloat64(9, true);
  328.                 right = d.getFloat64(17, true);
  329.                 top = d.getFloat64(25, true);
  330.                 if (0 == playerCell.length) {
  331.                     cellX = (right + left) / 2;
  332.                     cellY = (top + bottom) / 2;
  333.                 };
  334.             }
  335.         }
  336.         function run(d) {
  337.            
  338.             e = +new Date;
  339.             var key = Math.random();
  340.             var offset = 1;
  341.             aa = false;
  342.  
  343.             var cellID = d.getUint16(offset, true);
  344.             offset = offset + 2;
  345.             var i = 0;
  346.             for (; i < cellID; ++i) {
  347.                 var current = nodes[d.getUint32(offset, true)];
  348.                 var that = nodes[d.getUint32(offset + 4, true)];
  349.                 offset = offset + 8;
  350.                 if (current) {
  351.                     if (that) {
  352.                         that.destroy();
  353.                         that.ox = that.x;
  354.                         that.oy = that.y;
  355.                         that.oSize = that.size;
  356.                         that.nx = current.x;
  357.                         that.ny = current.y;
  358.                         that.nSize = that.size;
  359.                         that.updateTime = e;
  360.                     }
  361.                 }
  362.             }
  363.             for (; ; ) {
  364.                 cellID = d.getUint32(offset, true);
  365.                 offset += 4;
  366.                 if (0 == cellID) {
  367.                     break;
  368.                 }
  369.                 i = d.getFloat64(offset, true);
  370.                 offset = offset + 8;
  371.                 current = d.getFloat64(offset, true);
  372.                 offset = offset + 8;
  373.                 that = d.getFloat64(offset, true);
  374.                 offset = offset + 8;
  375.                 var color = d.getUint8(offset++);
  376.                 var second = false;
  377.                 if (0 == color) {
  378.                     second = true;
  379.                     color = "#33FF33";
  380.                 } else {
  381.                     if (255 == color) {
  382.                         second = d.getUint8(offset++);
  383.                         color = d.getUint8(offset++);
  384.                         var cellName = d.getUint8(offset++);
  385.                         color = isArray(second << 16 | color << 8 | cellName);
  386.                         cellName = d.getUint8(offset++);
  387.                         second = !!(cellName & 1);
  388.                         if (cellName & 2) {
  389.                             offset += 4;
  390.                         }
  391.                         if (cellName & 4) {
  392.                             offset += 8;
  393.                         }
  394.                         if (cellName & 8) {
  395.                             offset += 16;
  396.                         }
  397.                     } else {
  398.                         color = 63487 | color << 16;
  399.                         var data = (color >> 16 & 255) / 255 * 360;
  400.                         var params = (color >> 8 & 255) / 255;
  401.                         color = (color >> 0 & 255) / 255;
  402.                         if (0 == params) {
  403.                             color = color << 16 | color << 8 | color << 0;
  404.                         } else {
  405.                             data = data / 60;
  406.                             cellName = ~~data;
  407.                             var callback = data - cellName;
  408.                             data = color * (1 - params);
  409.                             var tmp = color * (1 - params * callback);
  410.                             params = color * (1 - params * (1 - callback));
  411.                             var fn = callback = 0;
  412.                             var result = 0;
  413.                             switch (cellName % 6) {
  414.                             case 0:
  415.                                 callback = color;
  416.                                 fn = params;
  417.                                 result = data;
  418.                                 break;
  419.                             case 1:
  420.                                 callback = tmp;
  421.                                 fn = color;
  422.                                 result = data;
  423.                                 break;
  424.                             case 2:
  425.                                 callback = data;
  426.                                 fn = color;
  427.                                 result = params;
  428.                                 break;
  429.                             case 3:
  430.                                 callback = data;
  431.                                 fn = tmp;
  432.                                 result = color;
  433.                                 break;
  434.                             case 4:
  435.                                 callback = params;
  436.                                 fn = data;
  437.                                 result = color;
  438.                                 break;
  439.                             case 5:
  440.                                 callback = color;
  441.                                 fn = data;
  442.                                 result = tmp;
  443.                             }
  444.                             callback = ~~(255 * callback) & 255;
  445.                             fn = ~~(255 * fn) & 255;
  446.                             result = ~~(255 * result) & 255;
  447.                             color = callback << 16 | fn << 8 | result;
  448.                         }
  449.                         color = isArray(color);
  450.                     }
  451.                 }
  452.                 cellName = "";
  453.                 for (; ; ) {
  454.                     data = d.getUint16(offset, true);
  455.                     offset += 2;
  456.                     if (0 == data) {
  457.                         break;
  458.                     }
  459.                     cellName += String.fromCharCode(data);
  460.                 }
  461.                 var cellData = null;
  462.                 if (nodes.hasOwnProperty(cellID)) {
  463.                     cellData = nodes[cellID];
  464.                     cellData.updatePos();
  465.                     cellData.ox = cellData.x;
  466.                     cellData.oy = cellData.y;
  467.                     cellData.oSize = cellData.size;
  468.                     cellData.color = color;
  469.                     if (cellData.name != "") {
  470.                         //console.log("Text: " + cellData.name);
  471.                     }
  472.                 } else {
  473.                     cellData = new cell(cellID, i, current, that, color, second, cellName);
  474.                     cellData.pX = i;
  475.                     cellData.pY = current;
  476.                     if (cellName != "") {
  477.                         //console.log("Text: " + cellData.name + " Bool: " + second);
  478.                     }
  479.                 }
  480.                 cellData.nx = i;
  481.                 cellData.ny = current;
  482.                 cellData.nSize = that;
  483.                 cellData.updateCode = key;
  484.                 cellData.updateTime = e;
  485.                 if (-1 != bucket.indexOf(cellID)) {
  486.                     if (-1 == playerCell.indexOf(cellData)) {
  487.                         document.getElementById("overlays").style.display = "none";
  488.                         playerCell.push(cellData);
  489.                         if (1 == playerCell.length) {
  490.                             cellX = cellData.x;
  491.                             cellY = cellData.y;
  492.                         }
  493.                     }
  494.                 }
  495.             }
  496.             d.getUint16(offset, true);
  497.             offset += 2;
  498.             current = d.getUint32(offset, true);
  499.             offset += 4;
  500.             i = 0;
  501.             for (; i < current; i++) {
  502.                 cellID = d.getUint32(offset, true);
  503.                 offset += 4;
  504.                 if (nodes[cellID]) {
  505.                     nodes[cellID].updateCode = key;
  506.                 }
  507.             }
  508.             i = 0;
  509.             for (; i < items.length; i++) {
  510.                 if (items[i].updateCode != key) {
  511.                     items[i--].destroy();
  512.                 }
  513.             }
  514.             if (aa) {
  515.                 if (0 == playerCell.length) {
  516.                     $("#overlays").fadeIn(3E3);
  517.                     //window.connect("ws://45.33.48.113:443");
  518.                     setNick(originalName); // <---- TROLL hahaha
  519.                 }
  520.             }
  521.         }
  522.          
  523.         function computeDistance(x1, y1, x2, y2) {
  524.             var xdis = Math.abs(x1 - x2);
  525.             var ydis = Math.abs(y1 - y2);
  526.             var distance = Math.sqrt(xdis * xdis + ydis * ydis);
  527.            
  528.             return distance;
  529.         }
  530.        
  531.         function getListBasedOnFunction(booleanFunction, listToUse) {
  532.             var dotList = [];
  533.             Object.keys(listToUse).forEach(function (element, index) {
  534.                 if (booleanFunction(element)){
  535.                     dotList.push(nodes[element]);
  536.                 }
  537.             });
  538.            
  539.             return dotList;
  540.         }
  541.        
  542.         //TODO: Make it only go to a virus if it's big enough. If it shrinks, it shouldn't only grab a single dot and go back in.
  543.         function getAllNiceViruses() {
  544.             var dotList = [];
  545.            
  546.             if (playerCell.length == 1) {
  547.                 dotList = getListBasedOnFunction(function (element){
  548.                     if (nodes[element].isVirus && (nodes[element].size *1.05 <= playerCell[0].size) && nodes[element].size * 1.15 >= playerCell[0].size) {
  549.                             return true;
  550.                     }
  551.                     return false;
  552.                 }, nodes);
  553.             }
  554.  
  555.            
  556.             return dotList;
  557.         }
  558.  
  559.         function getAllThreats() {
  560.             var dotList = [];
  561.            
  562.             dotList = getListBasedOnFunction(function (element){
  563.                 var isMe = false;
  564.                
  565.                 for (var i = 0; i < playerCell.length; i++) {
  566.                     if (nodes[element].id == playerCell[i].id) {
  567.                         isMe = true;
  568.                         break;
  569.                     }
  570.                 }
  571.                
  572.                 for (var i = 0; i < playerCell.length; i++) {
  573.                     if (!isMe && (!nodes[element].isVirus && (nodes[element].size >= playerCell[i].oSize * 1.15))) {
  574.                         return true;
  575.                     } else if (nodes[element].isVirus && (nodes[element].size * 1.15 <= playerCell[i].oSize)) {
  576.                         return true;
  577.                     }
  578.                     return false;
  579.                 }
  580.             }, nodes);
  581.            
  582.             return dotList;
  583.         }
  584.        
  585.         function getAllFood() {
  586.             var elementList = [];
  587.             var dotList = [];
  588.            
  589.             elementList = getListBasedOnFunction(function (element){
  590.                 var isMe = false;
  591.                
  592.                 for (var i = 0; i < playerCell.length; i++) {
  593.                     if (nodes[element].id == playerCell[i].id) {
  594.                         isMe = true;
  595.                         break;
  596.                     }
  597.                 }
  598.                
  599.                 for (var i = 0; i < playerCell.length; i++) {
  600.                     if (!isMe && !nodes[element].isVirus && (nodes[element].size * 1.25 <= playerCell[i].size)  || (nodes[element].size <= 11)){return true;} else{return false;}
  601.                 }
  602.             }, nodes);
  603.            
  604.             for (var i = 0; i < elementList.length; i++) {
  605.                 dotList.push([elementList[i].x, elementList[i].y, elementList[i].size]);
  606.             }
  607.            
  608.             return dotList;
  609.         }
  610.        
  611.         function clusterFood(foodList, blobSize) {
  612.             var clusters = [];
  613.             var addedCluster = false;
  614.             for (var i = 0; i < foodList.length; i++) {
  615.                 for (var j = 0; j < clusters.length; j++) {
  616.                     if (computeDistance(foodList[i][0], foodList[i][1], clusters[j][0], clusters[j][1]) < blobSize * 1.5) {
  617.                         clusters[j][0] = (foodList[i][0] + clusters[j][0]) / 2;
  618.                         clusters[j][1] = (foodList[i][1] + clusters[j][1]) / 2;
  619.                         clusters[j][2] += foodList[i][2];
  620.                         addedCluster = true;
  621.                         break;
  622.                     }
  623.                 }
  624.                 if (!addedCluster) {
  625.                     clusters.push([foodList[i][0], foodList[i][1], foodList[i][2]]);
  626.                 }
  627.                 addedCluster = false;
  628.             }
  629.             return clusters;
  630.         }
  631.        
  632.         //Given two points on a line, finds the slope of a perpendicular line crossing it.
  633.         function inverseSlope(x1, y1, x2, y2) {
  634.             var m = (y1 - y2) / (x1 - x2);
  635.             return (-1) / m;
  636.         }
  637.        
  638.         //Given a slope and an offset, returns two points on that line.
  639.         function pointsOnLine(slope, useX, useY) {
  640.            
  641.             var b = useY - slope * useX;
  642.            
  643.             return [[useX - 100, slope * (useX - 100) + b], [useX + 100, slope * (useX + 100) + b]];
  644.         }
  645.        
  646.         //Using a line formed from point a to b, tells if point c is on left side of that line.
  647.         function isSideLine(a, b, c) {
  648.             if ((b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]) > 0) {
  649.                 return true;
  650.             }
  651.             return false;
  652.         }
  653.  
  654.         function findDestination() {
  655.             dPoints = [];
  656.             lines = [];
  657.            
  658.             var tempMoveX = moveX;
  659.             var tempMoveY = moveY;
  660.            
  661.             if (playerCell[0] != null) {
  662.                 var allPossibleFood = null;
  663.                 allPossibleFood = getAllFood(); // #1
  664.                
  665.                 /*for (var i = -1000; i < 1000; i += playerCell[0].size) {
  666.                     for (var j = -1000; j < 1000; j += playerCell[0].size) {
  667.                         allPossibleFood.push([playerCell[0].x + i, playerCell[0].y + j, -200]);
  668.                     }
  669.                 }*/
  670.                
  671.                 var allPossibleThreats = getAllThreats();
  672.                
  673.                 var allPossibleNiceViruses = getAllNiceViruses();
  674.                 var closestNiceViruse = null;
  675.                 if (allPossibleNiceViruses.length != 0) {
  676.                     closestNiceViruse = [allPossibleNiceViruses[0], computeDistance(allPossibleNiceViruses[0].x, allPossibleNiceViruses[0].y, playerCell[0].x, playerCell[0].y)];
  677.                
  678.                     for (var i = 1; i < allPossibleNiceViruses.length; i++) {
  679.                         var testD = computeDistance(allPossibleNiceViruses[i].x, allPossibleNiceViruses[i].y, playerCell[0].x, playerCell[0].y)
  680.                         if (testD < closestNiceViruse[1]) {
  681.                             closestNiceViruse = [allPossibleNiceViruses[i], testD];
  682.                         }
  683.                     }
  684.                    
  685.                     console.log("NO WAY!!! LET THE TROLLING BEGIN!");
  686.                 }
  687.                
  688.                 var allThreatLines = [];
  689.                 var allThreatLinesBool = [];
  690.                 var allFallbackPoints = [];
  691.                 var allFallbackBool = [];
  692.                 var allFallbackCount = [];
  693.                
  694.                 var isSafeSpot = true;
  695.                
  696.                 var clusterAllFood = clusterFood(allPossibleFood, playerCell[0].oSize);
  697.                
  698.                 for (var i = 0; i < allPossibleThreats.length; i++) {
  699.                    
  700.                     var tempD = computeDistance(allPossibleThreats[i].x, allPossibleThreats[i].y, playerCell[0].x, playerCell[0].y);
  701.                    
  702.                     var ratioX =  tempD / (allPossibleThreats[i].x - playerCell[0].x);
  703.                     var ratioY =  tempD / (allPossibleThreats[i].y - playerCell[0].y);
  704.                    
  705.                     var offsetX = 0;
  706.                     var offsetY = 0;
  707.                    
  708.                     var offsetLeftX = 0;
  709.                     var offsetLeftY = 0;
  710.  
  711.                     var offsetRightX = 0;
  712.                     var offsetRightY = 0;
  713.                    
  714.                     var offsetEscapeLeftX = 0;
  715.                     var offsetEscapeLeftY = 0;
  716.  
  717.                     var offsetEscapeRightX = 0;
  718.                     var offsetEscapeRightY = 0;
  719.                    
  720.                     iSlope = inverseSlope(allPossibleThreats[i].x, allPossibleThreats[i].y, playerCell[0].x, playerCell[0].y);
  721.                    
  722.                     var sidePoints = pointsOnLine(iSlope, allPossibleThreats[i].x, allPossibleThreats[i].y);
  723.                    
  724.                     var leftD = computeDistance(allPossibleThreats[i].x, allPossibleThreats[i].y, sidePoints[0][0], sidePoints[0][1]);
  725.  
  726.                     var ratioLeftX = leftD / (allPossibleThreats[i].x - sidePoints[0][0]);
  727.                     var ratioLeftY = leftD / (allPossibleThreats[i].y - sidePoints[0][1]);
  728.                    
  729.                     if (allPossibleThreats[i].size >= playerCell[0].size * 30) {
  730.                         offsetX = allPossibleThreats[i].x - (allPossibleThreats[i].size / ratioX * 1.5);
  731.                         offsetY = allPossibleThreats[i].y - (allPossibleThreats[i].size / ratioY * 1.5);
  732.                        
  733.                         offsetLeftX = allPossibleThreats[i].x - (allPossibleThreats[i].size / ratioLeftX * 3);
  734.                         offsetLeftY = allPossibleThreats[i].y - (allPossibleThreats[i].size / ratioLeftY * 3);
  735.                        
  736.                         offsetRightX = allPossibleThreats[i].x + (allPossibleThreats[i].size / ratioLeftX * 3);
  737.                         offsetRightY = allPossibleThreats[i].y + (allPossibleThreats[i].size / ratioLeftY * 3);
  738.                        
  739.                         offsetEscapeLeftX = offsetX - (allPossibleThreats[i].size / ratioLeftX * 3);
  740.                         offsetEscapeLeftY = offsetY - (allPossibleThreats[i].size / ratioLeftY * 3);
  741.  
  742.                         offsetEscapeRightX = offsetX + (allPossibleThreats[i].size / ratioLeftX * 3);
  743.                         offsetEscapeRightY = offsetY + (allPossibleThreats[i].size / ratioLeftY * 3);
  744.                        
  745.                     } else if (allPossibleThreats[i].size >= playerCell[0].size * 2.1) {
  746.                         offsetX = allPossibleThreats[i].x - (allPossibleThreats[i].size / ratioX * 4);
  747.                         offsetY = allPossibleThreats[i].y - (allPossibleThreats[i].size / ratioY * 4);
  748.                        
  749.                         offsetLeftX = allPossibleThreats[i].x - (allPossibleThreats[i].size / ratioLeftX * 4);
  750.                         offsetLeftY = allPossibleThreats[i].y - (allPossibleThreats[i].size / ratioLeftY * 4);
  751.                        
  752.                         offsetRightX = allPossibleThreats[i].x + (allPossibleThreats[i].size / ratioLeftX * 4);
  753.                         offsetRightY = allPossibleThreats[i].y + (allPossibleThreats[i].size / ratioLeftY * 4);
  754.                        
  755.                         offsetEscapeLeftX = offsetX - (allPossibleThreats[i].size / ratioLeftX * 4);
  756.                         offsetEscapeLeftY = offsetY - (allPossibleThreats[i].size / ratioLeftY * 4);
  757.  
  758.                         offsetEscapeRightX = offsetX + (allPossibleThreats[i].size / ratioLeftX * 4);
  759.                         offsetEscapeRightY = offsetY + (allPossibleThreats[i].size / ratioLeftY * 4);
  760.                     } else {
  761.                         offsetX = allPossibleThreats[i].x - (allPossibleThreats[i].size / ratioX * 2);
  762.                         offsetY = allPossibleThreats[i].y - (allPossibleThreats[i].size / ratioY * 2);
  763.                        
  764.                         offsetLeftX = allPossibleThreats[i].x - (allPossibleThreats[i].size / ratioLeftX * 3);
  765.                         offsetLeftY = allPossibleThreats[i].y - (allPossibleThreats[i].size / ratioLeftY * 3);
  766.                        
  767.                         offsetRightX = allPossibleThreats[i].x + (allPossibleThreats[i].size / ratioLeftX * 3);
  768.                         offsetRightY = allPossibleThreats[i].y + (allPossibleThreats[i].size / ratioLeftY * 3);
  769.                        
  770.                         offsetEscapeLeftX = offsetX - (allPossibleThreats[i].size / ratioLeftX * 3);
  771.                         offsetEscapeLeftY = offsetY - (allPossibleThreats[i].size / ratioLeftY * 3);
  772.  
  773.                         offsetEscapeRightX = offsetX + (allPossibleThreats[i].size / ratioLeftX * 3);
  774.                         offsetEscapeRightY = offsetY + (allPossibleThreats[i].size / ratioLeftY * 3);
  775.                     }
  776.                    
  777.                     //offsetX = ((allPossibleThreats[i].x + playerCell[0].x) / 2);
  778.                     //offsetY = ((allPossibleThreats[i].y + playerCell[0].y) / 2);
  779.  
  780.                     drawPoint(offsetX, offsetY, 2);
  781.                    
  782.                     drawPoint(offsetLeftX, offsetLeftY, 3);
  783.                     drawPoint(offsetRightX, offsetRightY, 3);
  784.                    
  785.                     var leftSlope = inverseSlope(allPossibleThreats[i].x, allPossibleThreats[i].y, sidePoints[0][0], sidePoints[0][1]);
  786.                    
  787.                     threatLineLeft = [[offsetLeftX, offsetLeftY], [offsetX, offsetY]];
  788.                     threatLineRight = [[offsetRightX, offsetRightY], [offsetX, offsetY]];
  789.                    
  790.                     threatLine = pointsOnLine(iSlope, offsetX, offsetY);
  791.                    
  792.                     drawLine(allPossibleThreats[i].x, allPossibleThreats[i].y, playerCell[0].x, playerCell[0].y, 3);
  793.                    
  794.                     //drawLine(threatLine[0][0], threatLine[0][1], threatLine[1][0], threatLine[1][1], 0);
  795.                    
  796.                     drawLine(threatLineLeft[0][0], threatLineLeft[0][1], threatLineLeft[1][0], threatLineLeft[1][1], 0);
  797.                     drawLine(threatLineRight[0][0], threatLineRight[0][1], threatLineRight[1][0], threatLineRight[1][1], 0);
  798.                    
  799.                     allThreatLines.push([threatLineLeft, threatLineRight]);
  800.                    
  801.                     drawPoint(offsetEscapeLeftX, offsetEscapeLeftY, 4);
  802.                     drawPoint(offsetEscapeRightX, offsetEscapeRightY, 4);
  803.                    
  804.                     allFallbackPoints.push([offsetEscapeLeftX, offsetEscapeLeftY]);
  805.                     //allFallbackPoints.push([offsetEscapeRightX, offsetEscapeRightY]);
  806.                    
  807.                     allFallbackBool.push(true);
  808.                     //allFallbackBool.push(true);
  809.                    
  810.                     allFallbackCount.push(0);
  811.                     //allFallbackCount.push(0);
  812.                    
  813.                     var badSide = isSideLine(threatLine[0], threatLine[1], [allPossibleThreats[i].x, allPossibleThreats[i].y]);
  814.                    
  815.                     var badSideLeft = isSideLine(threatLineLeft[0], threatLineLeft[1], [allPossibleThreats[i].x, allPossibleThreats[i].y]);
  816.                     var badSideRight = isSideLine(threatLineRight[0], threatLineRight[1], [allPossibleThreats[i].x, allPossibleThreats[i].y]);
  817.                    
  818.                     allThreatLinesBool.push([badSideLeft, badSideRight]);
  819.                    
  820.                     isSafeSpot = (
  821.                             badSideLeft != isSideLine(threatLineLeft[0], threatLineLeft[1], [playerCell[0].x, playerCell[0].y]) &&
  822.                             badSideRight != isSideLine(threatLineRight[0], threatLineRight[1], [playerCell[0].x, playerCell[0].y]) && isSafeSpot
  823.                     );
  824.                    
  825.                     var removeClusterList = [];
  826.                    
  827.                     for (var j = 0; j < clusterAllFood.length; j++) {
  828.                         if (
  829.                             badSideLeft == isSideLine(threatLineLeft[0], threatLineLeft[1], [clusterAllFood[j][0], clusterAllFood[j][1]]) &&
  830.                             badSideRight == isSideLine(threatLineRight[0], threatLineRight[1], [clusterAllFood[j][0], clusterAllFood[j][1]])
  831.                         ) {
  832.                             removeClusterList.push(j);
  833.                         }
  834.                     }
  835.                     for (var j = removeClusterList.length - 1; j >= 0; j--) {
  836.                         drawPoint(clusterAllFood[j][0], clusterAllFood[j][1], 0);
  837.                         clusterAllFood.splice(removeClusterList[j], 1);
  838.                     }
  839.                    
  840.                     if (
  841.                         badSideLeft == isSideLine(threatLineLeft[0], threatLineLeft[1], [tempPoint[0], tempPoint[1]]) &&
  842.                         badSideRight == isSideLine(threatLineRight[0], threatLineRight[1], [tempPoint[0], tempPoint[1]])
  843.                     ) {
  844.                         tempPoint[2] = 0;
  845.                     }
  846.                 }
  847.                
  848.                 for (var i = 0; i < clusterAllFood.length; i++) {
  849.                     //console.log("Before: " + clusterAllFood[i][2]);
  850.                     clusterAllFood[i][2] = clusterAllFood[i][2] * 6 - computeDistance(clusterAllFood[i][0], clusterAllFood[i][1], playerCell[0].ox, playerCell[0].oy);
  851.                     drawPoint(clusterAllFood[i][0], clusterAllFood[i][1], 1);
  852.                     //console.log("After: " + clusterAllFood[i][2]);
  853.                 }
  854.                
  855.                 if (clusterAllFood.length != 0 && isSafeSpot) {
  856.                     biggestCluster = clusterAllFood[0];
  857.                     for (var i = 1; i < clusterAllFood.length; i++) {
  858.                         if (clusterAllFood[i][2] > biggestCluster[2]) {
  859.                             biggestCluster = clusterAllFood[i];
  860.                         }
  861.                     }
  862.                    
  863.                     /**
  864.                      * #1 Get a list of all the food.
  865.                      * #2 Get a list of all the threats.
  866.                      * #3 Remove all the food near threats.
  867.                      * #4 Find closest food after the filter.
  868.                      */
  869.                    
  870.                     if (closestNiceViruse != null) {
  871.                         for (var i = 0; i < playerCell.length; i++) {
  872.                             drawLine(playerCell[i].ox, playerCell[i].oy, closestNiceViruse[0].x, closestNiceViruse[0].y, 5);
  873.                         }
  874.          
  875.                         tempMoveX = closestNiceViruse[0].x;
  876.                         tempMoveY = closestNiceViruse[0].y;
  877.                     } else {
  878.                         for (var i = 0; i < playerCell.length; i++) {
  879.                             drawLine(playerCell[i].ox, playerCell[i].oy, biggestCluster[0], biggestCluster[1], 1);
  880.                         }
  881.          
  882.                         tempMoveX = biggestCluster[0];
  883.                         tempMoveY = biggestCluster[1];
  884.                     }
  885.                    
  886.                     //console.log("X: " + moveX + " Y: " + moveY);
  887.                    
  888.                    
  889.                     if (playerCell.length > 1 && splitted) {
  890.                         splitted = false;
  891.                         tempMoveX = biggestCluster[0];
  892.                         tempMoveY = biggestCluster[1];
  893.                         return;
  894.                     }
  895.                     if (splitting) {
  896.                         tempMoveX = biggestCluster[0];
  897.                         tempMoveY = biggestCluster[1];
  898.                         sendServerCommand(17);
  899.                         splitting = false;
  900.                         splitted = true;
  901.                         return;
  902.                     }
  903.                    
  904.                     if (biggestCluster[2] * 1.20 < playerCell[0].size && biggestCluster[2] * 1.20 > playerCell[0].size / 5 &&  biggestCluster[2] > 11 && !splitted) {
  905.                         drawLine(playerCell[0].x, playerCell[0].y, biggestCluster[0], biggestCluster[1], 4);
  906.                        
  907.                         var worthyTargetDistance = computeDistance(playerCell[0].x, playerCell[0].y, biggestCluster[0], biggestCluster[1]);
  908.                        
  909.                         console.log("I want to split.");
  910.                        
  911.                         if ((worthyTargetDistance < playerCell[0].size * 3) && playerCell.length == 1) {
  912.                             tempMoveX = biggestCluster[0];
  913.                             tempMoveY = biggestCluster[1];
  914.                             splitting = true;
  915.                             return;
  916.                         }
  917.                     }
  918.                 } else {
  919.                     //console.log("I'm lost, where do I go?");
  920.                     for (var i = 0; i < allFallbackPoints.length; i++) {
  921.                         for (var j = 0; j < allThreatLines.length; j++) {
  922.                             var badSideLeft = allThreatLinesBool[0];
  923.                             var badSideRight = allThreatLinesBool[1];
  924.                            
  925.                             if (allFallbackBool[i] &&
  926.                                 badSideLeft != isSideLine(allThreatLines[j][0][0], allThreatLines[j][0][1], [allFallbackPoints[i][0], allFallbackPoints[i][1]]) &&
  927.                                 badSideRight != isSideLine(allThreatLines[j][1][0], allThreatLines[j][1][1], [allFallbackPoints[i][0], allFallbackPoints[i][1]])
  928.                             ) {
  929.                                 allFallbackBool[i] = true;
  930.                                 //console.log("Step 1");
  931.                             } else {
  932.                                 //console.log("Failed Step 1");
  933.                                 allFallbackBool[i] = false;
  934.                                 allFallbackCount[i] += 1;
  935.                             }
  936.                         }
  937.                     }
  938.                    
  939.                     var closestFallback = null;
  940.                     var fallbackDistance = null;
  941.                     for (var i = 1; i < allFallbackPoints.length; i++) {
  942.                         if (allFallbackBool[i]) {
  943.                             var tempDistance = computeDistance(playerCell[0].x, playerCell[0].y, allFallbackPoints[i][0], allFallbackPoints[i][1]);
  944.                             if (closestFallback != null) {
  945.                                 if (tempDistance < fallbackDistance) {
  946.                                     closestFallback = allFallbackPoints[i];
  947.                                     fallbackDistance = tempDistance;
  948.                                 }
  949.                             } else {
  950.                                 console.log("FOUND CHILL SPOT!");
  951.                                 closestFallback = allFallbackPoints[i];
  952.                                 fallbackDistance = tempDistance;
  953.                             }
  954.                         }
  955.                     }
  956.                    
  957.                     if (closestFallback != null) {
  958.                         console.log("ESCAPING");
  959.                         tempMoveX = closestFallback[0];
  960.                         tempMoveY = closestFallback[1];
  961.                         drawLine(playerCell[0].x, playerCell[0].y, tempMoveX, tempMoveY, 6);
  962.                     } else {
  963.                         console.log("NOPE! NEVER RUNNING AWAY!");
  964.                     }
  965.                    
  966.                     //#1 Loop through fallbackpoints
  967.                     //#2 Loop through threatlines
  968.                     //#3 Verify if a point is fine. If not, add counter to point's overlaps
  969.                     //#4 Go to closest safe point, otherwise find point with lowest counter.
  970.                 }
  971.                
  972.                 drawPoint(tempPoint[0], tempPoint[1], tempPoint[2]);
  973.                 tempPoint[2] = 1;
  974.             }
  975.            
  976.             if (!toggle) {
  977.                 moveX = tempMoveX;
  978.                 moveY = tempMoveY;
  979.             }
  980.         }
  981.  
  982.         function drawPoint(x_1, y_1, drawColor) {
  983.        
  984.        
  985.             var x1 = ((x_1 - cellX) * screenRatio) + width/2;
  986.             var y1 = ((y_1 - cellY) * screenRatio) + height/2;
  987.            
  988.             //console.log("\tdX: " + x1 + " dY: " + y1);
  989.            
  990.             dPoints.push([x1, y1, drawColor]);
  991.         }
  992.        
  993.         function drawLine(x_1, y_1, x_2, y_2, drawColor) {
  994.             var x1 = ((x_1 - cellX) * screenRatio) + width/2;
  995.             var y1 = ((y_1 - cellY) * screenRatio) + height/2;
  996.             var x2 = ((x_2 - cellX) * screenRatio) + width/2;
  997.             var y2 = ((y_2 - cellY) * screenRatio) + height/2;
  998.             lines.push([x1, y1, x2, y2, drawColor]);
  999.         }
  1000.        
  1001.         function sendMovementToServer() { //Sends server mouse position or movement
  1002.             //console.log("Nodes Length: " + Object.keys(nodes).length);
  1003.             findDestination();
  1004.            
  1005.             if (null != ws && ws.readyState == ws.OPEN) {
  1006.                 var z0 = currentMouseX - width / 2; //z0 and z1 are mouse position relative to the player cell
  1007.                 var z1 = currentMouseY - height / 2;
  1008.                 if (!(64 > z0 * z0 + z1 * z1)) { //To keep ball still if the mouse position is in the center of the ball
  1009.                     if (!(val == moveX && min == moveY)) {
  1010.                         val = moveX;
  1011.                         min = moveY;
  1012.                         z0 = new ArrayBuffer(21);
  1013.                         z1 = new DataView(z0);
  1014.                         z1.setUint8(0, 16);
  1015.                         z1.setFloat64(1, moveX, true);
  1016.                         z1.setFloat64(9, moveY, true);
  1017.                         z1.setUint32(17, 0, true);
  1018.                         ws.send(z0);
  1019.                     }
  1020.                 }
  1021.             }
  1022.         }
  1023.         function close() {
  1024.             if (null != ws && (ws.readyState == ws.OPEN && null != result)) {
  1025.                 var buf = new ArrayBuffer(1 + 2 * result.length);
  1026.                 var view = new DataView(buf);
  1027.                 view.setUint8(0, 0);
  1028.                 var i = 0;
  1029.                 for (; i < result.length; ++i) {
  1030.                     view.setUint16(1 + 2 * i, result.charCodeAt(i), true);
  1031.                 }
  1032.                 ws.send(buf);
  1033.             }
  1034.         }
  1035.         function sendServerCommand(opt_attributes) { //opt_attributes appears to be a server-side command, I don't know what the exact details are
  1036.             if (null != ws && ws.readyState == ws.OPEN) {
  1037.                 /** @type {ArrayBuffer} */
  1038.                 var buf = new ArrayBuffer(1);
  1039.                 (new DataView(buf)).setUint8(0, opt_attributes);
  1040.                 ws.send(buf);
  1041.             }
  1042.         }
  1043.         function anim() {
  1044.            
  1045.             draw();
  1046.             self.requestAnimationFrame(anim);
  1047.         }
  1048.         /**
  1049.          * @return {undefined}
  1050.          */
  1051.         function onResize() {
  1052.             width = self.innerWidth;
  1053.             height = self.innerHeight;
  1054.             tempCanvas.width = canvas.width = width;
  1055.             tempCanvas.height = canvas.height = height;
  1056.             draw();
  1057.         }
  1058.         /**
  1059.          * @return {undefined}
  1060.          */
  1061.         function calculateScreenRatio() {
  1062.             if (0 != playerCell.length) {
  1063.                 var offset = 0;
  1064.                 var fileIndex = 0;
  1065.                 for (; fileIndex < playerCell.length; fileIndex++) {
  1066.                     offset += playerCell[fileIndex].size;
  1067.                 }
  1068.                 offset = Math.pow(Math.min(64 / offset, 1), 0.4) * Math.max(height / (970), width / (1920));
  1069.                 screenRatio = (9 * screenRatio + offset) / 10; //Ratio is used to scale the ball size based on screen size, so that you can still see pretty much the same amount no matter how big your screen is
  1070.             }
  1071.         }
  1072.         /**
  1073.          * @return {undefined}
  1074.          */
  1075.         function draw() {
  1076.            
  1077.             var tick = +new Date;
  1078.             ++Ba;
  1079.             calculateScreenRatio();
  1080.             e = +new Date;
  1081.             if (0 < playerCell.length) {
  1082.                 var w = 0;
  1083.                 var d = 0;
  1084.                 var i = 0;
  1085.                 for (; i < playerCell.length; i++) {
  1086.                     playerCell[i].updatePos();
  1087.                     w += playerCell[i].x / playerCell.length;
  1088.                     d += playerCell[i].y / playerCell.length;
  1089.                 }
  1090.                 cellX = (cellX + w) / 2;
  1091.                 cellY = (cellY + d) / 2;
  1092.             }
  1093.             processData();
  1094.             recalculateDestination();
  1095.             canvasContext.clearRect(0, 0, width, height);
  1096.             canvasContext.fillStyle = color ? "#111111" : "#F2FBFF";
  1097.             canvasContext.fillRect(0, 0, width, height);
  1098.             canvasContext.save();
  1099.             canvasContext.strokeStyle = color ? "#AAAAAA" : "#000000";
  1100.             canvasContext.globalAlpha = 0.2;
  1101.             canvasContext.scale(screenRatio, screenRatio);
  1102.             w = width / screenRatio;
  1103.             d = height / screenRatio;
  1104.             i = -0.5 + (-cellX + w / 2) % 50;
  1105.             for (; i < w; i += 50) { //Draw grid lines vertical
  1106.                 canvasContext.beginPath();
  1107.                 canvasContext.moveTo(i, 0);
  1108.                 canvasContext.lineTo(i, d);
  1109.                 canvasContext.stroke();
  1110.             }
  1111.             i = -0.5 + (-cellY + d / 2) % 50;
  1112.             for (; i < d; i += 50) { //Draw grid lines horizontal
  1113.                 canvasContext.beginPath();
  1114.                 canvasContext.moveTo(0, i);
  1115.                 canvasContext.lineTo(w, i);
  1116.                 canvasContext.stroke();
  1117.             }
  1118.             canvasContext.restore();
  1119.             items.sort(function (a, b) {
  1120.                 return a.size == b.size ? a.id - b.id : a.size - b.size;
  1121.             });
  1122.             canvasContext.save();
  1123.             canvasContext.translate(width / 2, height / 2);
  1124.             canvasContext.scale(screenRatio, screenRatio);
  1125.             canvasContext.translate(-cellX, -cellY);
  1126.             i = 0;
  1127.             for (; i < destroyedCells.length; i++) {
  1128.                 destroyedCells[i].draw();
  1129.             }
  1130.             i = 0;
  1131.             for (; i < items.length; i++) {
  1132.                 items[i].draw();
  1133.             }
  1134.             canvasContext.restore();
  1135.             if (img) {
  1136.                 if (0 != leaderboardPlayers.length) {
  1137.  
  1138.                     canvasContext.drawImage(img, width - img.width - 10, 10);
  1139.                 }
  1140.             }
  1141.             closingAnimationTime = Math.max(closingAnimationTime, getHeight());
  1142.             if (0 != closingAnimationTime) {
  1143.                 if (null == button) {
  1144.                     button = new SVGPlotFunction(24, "#FFFFFF");
  1145.                 }
  1146.                 button.setValue("Score: " + ~~(closingAnimationTime / 100));
  1147.                 d = button.render();
  1148.                 w = d.width;
  1149.                 canvasContext.globalAlpha = 0.2;
  1150.                 /** @type {string} */
  1151.                 canvasContext.fillStyle = "#000000";
  1152.                 canvasContext.fillRect(10, height - 10 - 24 - 10, w + 10, 34);
  1153.                 canvasContext.globalAlpha = 1;
  1154.                 canvasContext.drawImage(d, 15, height - 10 - 24 - 5);
  1155.             }
  1156.             clear();
  1157.             tick = +new Date - tick;
  1158.             if (tick > 1E3 / 60) {
  1159.                 n_players -= 0.01;
  1160.             } else {
  1161.                 if (tick < 1E3 / 65) {
  1162.                     n_players += 0.01;
  1163.                 }
  1164.             }
  1165.             if (0.4 > n_players) {
  1166.                 n_players = 0.4;
  1167.             }
  1168.             if (1 < n_players) {
  1169.                 n_players = 1;
  1170.             }
  1171.            
  1172.             for (var i = 0; i < dPoints.length; i++) {
  1173.                 var radius = 10;
  1174.  
  1175.                 canvasContext.beginPath();
  1176.                 canvasContext.arc(dPoints[i][0], dPoints[i][1], radius, 0, 2 * Math.PI, false);
  1177.  
  1178.                 if (dPoints[i][2] == 0) {
  1179.                     canvasContext.fillStyle = "black";
  1180.                 } else if (dPoints[i][2] == 1) {
  1181.                     canvasContext.fillStyle = "yellow";
  1182.                 } else if (dPoints[i][2] == 2) {
  1183.                     canvasContext.fillStyle = "blue";
  1184.                 } else if (dPoints[i][2] == 3) {
  1185.                     canvasContext.fillStyle = "red";
  1186.                 } else if (dPoints[i][2] == 4) {
  1187.                     canvasContext.fillStyle = "#008080";
  1188.                 } else {
  1189.                     canvasContext.fillStyle = "#000000";
  1190.                 }
  1191.                  
  1192.                 canvasContext.fill();
  1193.                 canvasContext.lineWidth = 2;
  1194.                 canvasContext.strokeStyle = '#003300';
  1195.                 canvasContext.stroke();
  1196.             }
  1197.             canvasContext.lineWidth = 1;
  1198.            
  1199.             for(var i = 0; i < lines.length; i++) {
  1200.                
  1201.                 canvasContext.beginPath();
  1202.                
  1203.                 canvasContext.lineWidth = 5;
  1204.                
  1205.                 if (lines[i][4] == 0) {
  1206.                     canvasContext.strokeStyle = "#FF0000";
  1207.                 } else if (lines[i][4] == 1) {
  1208.                     canvasContext.strokeStyle = "#00FF00";
  1209.                 } else if (lines[i][4] == 2) {
  1210.                     canvasContext.strokeStyle = "#0000FF";
  1211.                 } else if (lines[i][4] == 3) {
  1212.                     canvasContext.strokeStyle = "#FF8000";
  1213.                 } else if (lines[i][4] == 4) {
  1214.                     canvasContext.strokeStyle = "#8A2BE2";
  1215.                 } else if (lines[i][4] == 5) {
  1216.                     canvasContext.strokeStyle = "#FF69B4";
  1217.                 } else if (lines[i][4] == 6) {
  1218.                     canvasContext.strokeStyle = "#008080";
  1219.                 } else {
  1220.                     canvasContext.strokeStyle = "#000000";
  1221.                 }
  1222.                
  1223.                 canvasContext.moveTo(lines[i][0], lines[i][1]);
  1224.                 canvasContext.lineTo(lines[i][2], lines[i][3]);
  1225.                
  1226.                 canvasContext.stroke();
  1227.             }
  1228.             canvasContext.lineWidth = 1;
  1229.         }
  1230.         /**
  1231.          * @return {undefined}
  1232.          */
  1233.         function clear() {
  1234.             if (options && skin_image.width) {
  1235.                 var dim = width / 5;
  1236.                 canvasContext.drawImage(skin_image, 5, 5, dim, dim);
  1237.             }
  1238.         }
  1239.         /**
  1240.          * @return {?}
  1241.          */
  1242.         function getHeight() {
  1243.             var value = 0;
  1244.             var second = 0;
  1245.             for (; second < playerCell.length; second++) {
  1246.                 value += playerCell[second].nSize * playerCell[second].nSize;
  1247.             }
  1248.             return value;
  1249.         }
  1250.         function drawLeaderboard() {
  1251.             if (0 != leaderboardPlayers.length) {
  1252.                 if (v) {
  1253.                     img = document.createElement("canvas");
  1254.                     var canvasContext = img.getContext("2d");
  1255.                     var s = 60 + 24 * leaderboardPlayers.length;
  1256.                     var n = Math.min(200, 0.3 * width) / 200;
  1257.                     img.width = 200 * n;
  1258.                     img.height = s * n;
  1259.                     canvasContext.scale(n, n);
  1260.                     canvasContext.globalAlpha = 0.4;
  1261.                     canvasContext.fillStyle = "#000000";
  1262.                     canvasContext.fillRect(0, 0, 200, s);
  1263.                     canvasContext.globalAlpha = 1;
  1264.                     canvasContext.fillStyle = "#FFFFFF";
  1265.                     n = null;
  1266.                     n = "Leaderboard"; //Draws leaderboard from here on
  1267.                     canvasContext.font = "30px Ubuntu";
  1268.                     canvasContext.fillText(n, 100 - canvasContext.measureText(n).width / 2, 40);
  1269.                     canvasContext.font = "20px Ubuntu";
  1270.                     s = 0;
  1271.                     for (; s < leaderboardPlayers.length; ++s) {
  1272.                         n = leaderboardPlayers[s].name || "An unnamed cell";
  1273.                         if (-1 != playerCell.indexOf(leaderboardPlayers[s].id)) {
  1274.                             n = playerCell[0].name;
  1275.                         }
  1276.                         if (!v) {
  1277.                             if (!(0 != playerCell.length && playerCell[0].name == n)) {
  1278.                                 /** @type {string} */
  1279.                                 n = "An unnamed cell";
  1280.                             }
  1281.                         }
  1282.                         /** @type {string} */
  1283.                         n = s + 1 + ". " + n;
  1284.                         canvasContext.fillText(n, 100 - canvasContext.measureText(n).width / 2, 70 + 24 * s);
  1285.                     }
  1286.                 } else {
  1287.                     img = null;
  1288.                 }
  1289.             }
  1290.         }
  1291.  
  1292.         function cell(id, x, y, size, color, isVirus, i) {
  1293.             items.push(this);
  1294.             nodes[id] = this;
  1295.             this.id = id;
  1296.             this.ox = this.x = x;
  1297.             this.oy = this.y = y;
  1298.             this.oSize = this.size = size;
  1299.             this.color = color;
  1300.             /** @type {string} */
  1301.             this.isVirus = isVirus;
  1302.             this.points = [];
  1303.             this.pointsAcc = [];
  1304.             this.createPoints();
  1305.             this.setName(i);
  1306.         }
  1307.         /**
  1308.          * @param {?} val
  1309.          * @return {?}
  1310.          */
  1311.         function isArray(val) {
  1312.             val = val.toString(16);
  1313.             for (; 6 > val.length; ) {
  1314.                 /** @type {string} */
  1315.                 val = "0" + val;
  1316.             }
  1317.             return "#" + val;
  1318.         }
  1319.         /**
  1320.          * @param {number} n
  1321.          * @param {number} Var
  1322.          * @param {?} stroke
  1323.          * @param {?} plot
  1324.          * @return {undefined}
  1325.          */
  1326.         function SVGPlotFunction(n, Var, stroke, plot) {
  1327.             if (n) {
  1328.                 this._size = n;
  1329.             }
  1330.             if (Var) {
  1331.                 this._color = Var;
  1332.             }
  1333.             this._stroke = !!stroke;
  1334.             if (plot) {
  1335.                 this._strokeColor = plot;
  1336.             }
  1337.         }
  1338.         //if ("agar.io" != self.location.hostname && ("localhost" != self.location.hostname && "10.10.2.13" != self.location.hostname)) { //Antihack, probably going to have to remove this at some stage
  1339.         /** @type {string} */
  1340.         // self.location = "http://agar.io/";
  1341.         // } else {
  1342.         //Global variables I think
  1343.         var originalName; //For storing the original name for use when the bot restarts
  1344.         var splitted = false;
  1345.         var splitting = false;
  1346.  
  1347.         var tempPoint = [0, 0, 1];
  1348.        
  1349.         var splitted = false;
  1350.         var splitting = false;
  1351.        
  1352.         var toggle = false;
  1353.        
  1354.         var dPoints = [];
  1355.         var lines = [];
  1356.         var tempCanvas;
  1357.         var canvasContext;
  1358.         var canvas;
  1359.         var width;
  1360.         var height;
  1361.         var context = null;
  1362.         var ws = null;
  1363.         var cellX = 0;
  1364.         var cellY = 0;
  1365.         var bucket = [];
  1366.         var playerCell = [];
  1367.         var nodes = {};
  1368.         var items = [];
  1369.         var destroyedCells = [];
  1370.         var leaderboardPlayers = [];
  1371.         var currentMouseX = 0;
  1372.         var currentMouseY = 0;
  1373.         var moveX = -1;
  1374.         var moveY = -1;
  1375.         var Ba = 0;
  1376.         var e = 0;
  1377.         var result = null;
  1378.         var left = 0;
  1379.         var bottom = 0;
  1380.         var right = 1E4;
  1381.         var top = 1E4;
  1382.         var screenRatio = 1;
  1383.         var dest = null;
  1384.         var $timeout = true;
  1385.         var v = true;
  1386.         var doneResults = false;
  1387.         var aa = false;
  1388.         var closingAnimationTime = 0;
  1389.         var color = false;
  1390.         var text = false;
  1391.         var options = "ontouchstart" in self && /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  1392.         var skin_image = new Image;
  1393.         skin_image.src = "img/split.png";
  1394.         var old = null;
  1395.         self.setNick = function (subKey) {
  1396.             originalName = subKey;
  1397.             $("#adsBottom").hide();
  1398.             result = subKey;
  1399.             console.log(subKey);
  1400.             close();
  1401.             $("#overlays").hide();
  1402.             closingAnimationTime = 0;
  1403.            
  1404.             setDarkTheme(true);
  1405.             setShowMass(true);
  1406.         };
  1407.         /** @type {function (?): undefined} */
  1408.         self.setRegion = done;
  1409.         /**
  1410.          * @param {boolean} _$timeout_
  1411.          * @return {undefined}
  1412.          */
  1413.         self.setSkins = function (_$timeout_) {
  1414.             /** @type {boolean} */
  1415.             $timeout = _$timeout_;
  1416.         };
  1417.         /**
  1418.          * @param {boolean} o2
  1419.          * @return {undefined}
  1420.          */
  1421.         self.setNames = function (o2) {
  1422.             /** @type {boolean} */
  1423.             v = o2;
  1424.         };
  1425.         /**
  1426.          * @param {boolean} newColor
  1427.          * @return {undefined}
  1428.          */
  1429.         self.setDarkTheme = function (newColor) {
  1430.             /** @type {boolean} */
  1431.             color = newColor;
  1432.         };
  1433.         /**
  1434.          * @param {boolean} data
  1435.          * @return {undefined}
  1436.          */
  1437.         self.setColors = function (data) {
  1438.             /** @type {boolean} */
  1439.             doneResults = data;
  1440.         };
  1441.         /**
  1442.          * @param {boolean} textAlt
  1443.          * @return {undefined}
  1444.          */
  1445.         self.setShowMass = function (textAlt) {
  1446.             /** @type {boolean} */
  1447.             text = textAlt;
  1448.         };
  1449.         /** @type {function (string): undefined} */
  1450.         self.connect = open;
  1451.         var val = -1;
  1452.         var min = -1;
  1453.         var img = null;
  1454.         var n_players = 1;
  1455.         var button = null;
  1456.         var sources = {};
  1457.         /** @type {Array.<string>} */
  1458.         var excludes = "a;canada;australia;spain;brazil;germany;ukraine;france;sweden;hitler;north korea;south korea;japan;united kingdom;earth;greece;latvia;lithuania;estonia;finland;norway;cia;maldivas;austria;nigeria;reddit;yaranaika;confederate;9gag;indiana;4chan;italy;ussr;pewdiepie;bulgaria;tumblr;2ch.hk;hong kong;portugal;jamaica;german empire;mexico;sanik;switzerland;croatia;chile;indonesia;bangladesh;thailand;iran;iraq;peru;moon;botswana;bosnia;netherlands;european union;taiwan;pakistan;hungary;satanist;qing dynasty;nazi;matriarchy;patriarchy;feminism;ireland;texas;facepunch;prodota;cambodia;steam;piccolo;ea;india;kc;denmark;quebec;ayy lmao;sealand;bait;tsarist russia;origin;vinesauce;stalin;belgium;luxembourg;stussy;prussia;8ch;argentina;scotland;sir;romania;belarus;wojak;isis;doge;notreallyabot".split(";");
  1459.         var names = ["m'blob"];
  1460.         cell.prototype = {
  1461.             id : 0,
  1462.             points : null,
  1463.             pointsAcc : null,
  1464.             name : null,
  1465.             nameCache : null,
  1466.             sizeCache : null,
  1467.             x : 0,
  1468.             y : 0,
  1469.             size : 0,
  1470.             ox : 0,
  1471.             oy : 0,
  1472.             oSize : 0,
  1473.             nx : 0,
  1474.             ny : 0,
  1475.             nSize : 0,
  1476.             updateTime : 0,
  1477.             updateCode : 0,
  1478.             drawTime : 0,
  1479.             destroyed : false,
  1480.             isVirus : false,
  1481.             splitThreat : false,
  1482.             /**
  1483.              * @return {undefined}
  1484.              */
  1485.             destroy : function () {
  1486.                 var i;
  1487.                 i = 0;
  1488.                 for (; i < items.length; i++) {
  1489.                     if (items[i] == this) {
  1490.                         items.splice(i, 1);
  1491.                         break;
  1492.                     }
  1493.                 }
  1494.                 delete nodes[this.id];
  1495.                 i = playerCell.indexOf(this);
  1496.                 if (-1 != i) {
  1497.                     /** @type {boolean} */
  1498.                     aa = true;
  1499.                     playerCell.splice(i, 1);
  1500.                 }
  1501.                 i = bucket.indexOf(this.id);
  1502.                 if (-1 != i) {
  1503.                     bucket.splice(i, 1);
  1504.                 }
  1505.                 this.destroyed = true;
  1506.                 destroyedCells.push(this);
  1507.             },
  1508.             getNameSize : function () {
  1509.                 return Math.max(~~(0.3 * this.size), 24);
  1510.             },
  1511.             setName : function (name) {
  1512.                 if (this.name = name) {
  1513.                     if (null == this.nameCache) {
  1514.                         this.nameCache = new SVGPlotFunction(this.getNameSize(), "#FFFFFF", true, "#000000");
  1515.                     } else {
  1516.                         this.nameCache.setSize(this.getNameSize());
  1517.                     }
  1518.                     this.nameCache.setValue(this.name);
  1519.                 }
  1520.             },
  1521.             createPoints : function () {
  1522.                 var max = this.getNumPoints();
  1523.                 for (; this.points.length > max; ) {
  1524.                     var i = ~~(Math.random() * this.points.length);
  1525.                     this.points.splice(i, 1);
  1526.                     this.pointsAcc.splice(i, 1);
  1527.                 }
  1528.                 if (0 == this.points.length) {
  1529.                     if (0 < max) {
  1530.                         this.points.push({
  1531.                             c : this,
  1532.                             v : this.size,
  1533.                             x : this.x,
  1534.                             y : this.y
  1535.                         });
  1536.                         this.pointsAcc.push(Math.random() - 0.5);
  1537.                     }
  1538.                 }
  1539.                 for (; this.points.length < max; ) {
  1540.                     i = ~~(Math.random() * this.points.length);
  1541.                     var pt = this.points[i];
  1542.                     this.points.splice(i, 0, {
  1543.                         c : this,
  1544.                         v : pt.v,
  1545.                         x : pt.x,
  1546.                         y : pt.y
  1547.                     });
  1548.                     this.pointsAcc.splice(i, 0, this.pointsAcc[i]);
  1549.                 }
  1550.             },
  1551.             /**
  1552.              * @return {?}
  1553.              */
  1554.             getNumPoints : function () {
  1555.                 return ~~Math.max(this.size * screenRatio * (this.isVirus ? Math.min(2 * n_players, 1) : n_players), this.isVirus ? 10 : 5);
  1556.             },
  1557.             /**
  1558.              * @return {undefined}
  1559.              */
  1560.             movePoints : function () {
  1561.                 this.createPoints();
  1562.                 var points = this.points;
  1563.                 var chars = this.pointsAcc;
  1564.                 var value = chars.concat();
  1565.                 var rows = points.concat();
  1566.                 var l = rows.length;
  1567.                 var i = 0;
  1568.                 for (; i < l; ++i) {
  1569.                     var y = value[(i - 1 + l) % l];
  1570.                     var v = value[(i + 1) % l];
  1571.                     chars[i] += Math.random() - 0.5;
  1572.                     chars[i] *= 0.7;
  1573.                     if (10 < chars[i]) {
  1574.                         chars[i] = 10;
  1575.                     }
  1576.                     if (-10 > chars[i]) {
  1577.                         chars[i] = -10;
  1578.                     }
  1579.                     chars[i] = (y + v + 8 * chars[i]) / 10;
  1580.                 }
  1581.                 var flipped = this;
  1582.                 i = 0;
  1583.                 for (; i < l; ++i) {
  1584.                     value = rows[i].v;
  1585.                     y = rows[(i - 1 + l) % l].v;
  1586.                     v = rows[(i + 1) % l].v;
  1587.                     if (15 < this.size) {
  1588.                         var m = false;
  1589.                         var startX = points[i].x;
  1590.                         var startY = points[i].y;
  1591.                         context.retrieve2(startX - 5, startY - 5, 10, 10, function (vars) {
  1592.                             if (vars.c != flipped) {
  1593.                                 if (25 > (startX - vars.x) * (startX - vars.x) + (startY - vars.y) * (startY - vars.y)) {
  1594.                                     /** @type {boolean} */
  1595.                                     m = true;
  1596.                                 }
  1597.                             }
  1598.                         });
  1599.                         if (!m) {
  1600.                             if (points[i].x < left || (points[i].y < bottom || (points[i].x > right || points[i].y > top))) {
  1601.                                 /** @type {boolean} */
  1602.                                 m = true;
  1603.                             }
  1604.                         }
  1605.                         if (m) {
  1606.                             if (0 < chars[i]) {
  1607.                                 chars[i] = 0;
  1608.                             }
  1609.                             chars[i] -= 1;
  1610.                         }
  1611.                     }
  1612.                     value += chars[i];
  1613.                     if (0 > value) {
  1614.                         value = 0;
  1615.                     }
  1616.                     value = (12 * value + this.size) / 13;
  1617.                     points[i].v = (y + v + 8 * value) / 10;
  1618.                     y = 2 * Math.PI / l;
  1619.                     v = this.points[i].v;
  1620.                     if (this.isVirus) {
  1621.                         if (0 == i % 2) {
  1622.                             v += 5;
  1623.                         }
  1624.                     }
  1625.                     points[i].x = this.x + Math.cos(y * i) * v;
  1626.                     points[i].y = this.y + Math.sin(y * i) * v;
  1627.                 }
  1628.             },
  1629.             /**
  1630.              * @return {?}
  1631.              */
  1632.             updatePos : function () {
  1633.                 var A;
  1634.                 A = (e - this.updateTime) / 120;
  1635.                 A = 0 > A ? 0 : 1 < A ? 1 : A;
  1636.                 A = A * A * (3 - 2 * A);
  1637.                 var getNameSize = this.getNameSize();
  1638.                 if (this.destroyed && 1 <= A) {
  1639.                     var idx = destroyedCells.indexOf(this);
  1640.                     if (-1 != idx) {
  1641.                         destroyedCells.splice(idx, 1);
  1642.                     }
  1643.                 }
  1644.                 this.x = A * (this.nx - this.ox) + this.ox;
  1645.                 this.y = A * (this.ny - this.oy) + this.oy;
  1646.                 this.size = A * (this.nSize - this.oSize) + this.oSize;
  1647.                 if (!this.destroyed) {
  1648.                     if (!(getNameSize == this.getNameSize())) {
  1649.                         this.setName(this.name);
  1650.                     }
  1651.                 }
  1652.                 return A;
  1653.             },
  1654.             shouldRender : function () {
  1655.                 return this.x + this.size + 40 < cellX - width / 2 / screenRatio || (this.y + this.size + 40 < cellY - height / 2 / screenRatio || (this.x - this.size - 40 > cellX + width / 2 / screenRatio || this.y - this.size - 40 > cellY + height / 2 / screenRatio)) ? false : true;
  1656.             },
  1657.             draw : function () {
  1658.                 if (this.shouldRender()) {
  1659.                     canvasContext.save();
  1660.                     this.drawTime = e;
  1661.                     var key = this.updatePos();
  1662.                     if (this.destroyed) {
  1663.                         canvasContext.globalAlpha *= 1 - key;
  1664.                     }
  1665.                     this.movePoints();
  1666.                     if (doneResults) {
  1667.                         canvasContext.fillStyle = "#FFFFFF";
  1668.                         canvasContext.strokeStyle = "#AAAAAA";
  1669.                     } else {
  1670.                         canvasContext.fillStyle = this.color;
  1671.                         canvasContext.strokeStyle = this.color;
  1672.                     }
  1673.                     canvasContext.beginPath();
  1674.                     canvasContext.lineWidth = 10;
  1675.                     canvasContext.lineCap = "round";
  1676.                     canvasContext.lineJoin = this.isVirus ? "mitter" : "round";
  1677.                     key = this.getNumPoints();
  1678.                     canvasContext.moveTo(this.points[0].x, this.points[0].y);
  1679.                     var src = 1;
  1680.                     for (; src <= key; ++src) {
  1681.                         var i = src % key;
  1682.                         canvasContext.lineTo(this.points[i].x, this.points[i].y);
  1683.                     }
  1684.                     canvasContext.closePath();
  1685.                     key = this.name.toLowerCase();
  1686.                     if ($timeout) {
  1687.                         if (-1 != excludes.indexOf(key)) {
  1688.                             if (!sources.hasOwnProperty(key)) {
  1689.                                 sources[key] = new Image;
  1690.                                 if (key == "notreallyabot") {
  1691.                                     sources[key].src = "http://i.imgur.com/ZW5T4cd.png";
  1692.                                 } else {
  1693.                                     sources[key].src = "skins/" + key + ".png";
  1694.                                 }
  1695.                             }
  1696.                             src = sources[key];
  1697.                         } else {
  1698.                             src = null;
  1699.                         }
  1700.                     } else {
  1701.                         src = null;
  1702.                     }
  1703.                     key = src ? -1 != names.indexOf(key) : false;
  1704.                     canvasContext.stroke();
  1705.                     canvasContext.fill();
  1706.                     if (null != src) {
  1707.                         if (0 < src.width) {
  1708.                             if (!key) {
  1709.                                 canvasContext.save();
  1710.                                 canvasContext.clip();
  1711.                                 canvasContext.drawImage(src, this.x - this.size, this.y - this.size, 2 * this.size, 2 * this.size);
  1712.                                 canvasContext.restore();
  1713.                             }
  1714.                         }
  1715.                     }
  1716.                     if (doneResults || 15 < this.size) {
  1717.                         canvasContext.strokeStyle = "#000000";
  1718.                         canvasContext.globalAlpha *= 0.1;
  1719.                         canvasContext.stroke();
  1720.                     }
  1721.                     canvasContext.globalAlpha = 1;
  1722.                     if (null != src) {
  1723.                         if (0 < src.width) {
  1724.                             if (key) {
  1725.                                 canvasContext.drawImage(src, this.x - 2 * this.size, this.y - 2 * this.size, 4 * this.size, 4 * this.size);
  1726.                             }
  1727.                         }
  1728.                     }
  1729.                     key = true;//-1 != playerCell.indexOf(this);
  1730.                     src = ~~this.y;
  1731.                     if (v || key) {
  1732.                         if (this.name) {
  1733.                             if (this.nameCache) {
  1734.                                 i = this.nameCache.render();
  1735.                                 canvasContext.drawImage(i, ~~this.x - ~~(i.width / 2), src - ~~(i.height / 2));
  1736.                                 src += i.height / 2 + 4;
  1737.                             }
  1738.                         }
  1739.                     }
  1740.                     if (text) {
  1741.                         if (key) {
  1742.                             if (null == this.sizeCache) {
  1743.                                 this.sizeCache = new SVGPlotFunction(this.getNameSize() / 2, "#FFFFFF", true, "#000000");
  1744.                             }
  1745.                             this.sizeCache.setSize(this.getNameSize() / 2);
  1746.                             this.sizeCache.setValue(~~(this.size * this.size / 100));
  1747.                             i = this.sizeCache.render();
  1748.                             canvasContext.drawImage(i, ~~this.x - ~~(i.width / 2), src - ~~(i.height / 2));
  1749.                         }
  1750.                     }
  1751.                    
  1752.                     canvasContext.restore();
  1753.                 }
  1754.             }
  1755.         };
  1756.         SVGPlotFunction.prototype = {
  1757.             _value : "",
  1758.             _color : "#000000",
  1759.             _stroke : false,
  1760.             _strokeColor : "#000000",
  1761.             _size : 16,
  1762.             _canvas : null,
  1763.             _ctx : null,
  1764.             _dirty : false,
  1765.             /**
  1766.              * @param {number} size
  1767.              * @return {undefined}
  1768.              */
  1769.             setSize : function (size) {
  1770.                 if (this._size != size) {
  1771.                     this._size = size;
  1772.                     /** @type {boolean} */
  1773.                     this._dirty = true;
  1774.                 }
  1775.             },
  1776.             /**
  1777.              * @param {number} color
  1778.              * @return {undefined}
  1779.              */
  1780.             setColor : function (color) {
  1781.                 if (this._color != color) {
  1782.                     this._color = color;
  1783.                     /** @type {boolean} */
  1784.                     this._dirty = true;
  1785.                 }
  1786.             },
  1787.             /**
  1788.              * @param {boolean} stroke
  1789.              * @return {undefined}
  1790.              */
  1791.             setStroke : function (stroke) {
  1792.                 if (this._stroke != stroke) {
  1793.                     /** @type {boolean} */
  1794.                     this._stroke = stroke;
  1795.                     /** @type {boolean} */
  1796.                     this._dirty = true;
  1797.                 }
  1798.             },
  1799.             /**
  1800.              * @param {number} b
  1801.              * @return {undefined}
  1802.              */
  1803.             setStrokeColor : function (b) {
  1804.                 if (this._strokeColor != b) {
  1805.                     this._strokeColor = b;
  1806.                     /** @type {boolean} */
  1807.                     this._dirty = true;
  1808.                 }
  1809.             },
  1810.             /**
  1811.              * @param {number} value
  1812.              * @return {undefined}
  1813.              */
  1814.             setValue : function (value) {
  1815.                 if (value != this._value) {
  1816.                     this._value = value;
  1817.                     this._dirty = true;
  1818.                 }
  1819.             },
  1820.             /**
  1821.              * @return {?}
  1822.              */
  1823.             render : function () {
  1824.                 if (null == this._canvas) {
  1825.                     /** @type {Element} */
  1826.                     this._canvas = document.createElement("canvas");
  1827.                     this._ctx = this._canvas.getContext("2d");
  1828.                 }
  1829.                 if (this._dirty) {
  1830.                     var style = this._canvas;
  1831.                     var canvasContext = this._ctx;
  1832.                     var caracter = this._value;
  1833.                     var size = this._size;
  1834.                     /** @type {string} */
  1835.                     var text = size + "px Ubuntu";
  1836.                     /** @type {string} */
  1837.                     canvasContext.font = text;
  1838.                     var parentWidth = canvasContext.measureText(caracter).width;
  1839.                     var PX = ~~(0.2 * size);
  1840.                     style.width = parentWidth + 6;
  1841.                     style.height = size + PX;
  1842.                     /** @type {string} */
  1843.                     canvasContext.font = text;
  1844.                     canvasContext.globalAlpha = 1;
  1845.                     canvasContext.lineWidth = 3;
  1846.                     canvasContext.strokeStyle = this._strokeColor;
  1847.                     canvasContext.fillStyle = this._color;
  1848.                     if (this._stroke) {
  1849.                         canvasContext.strokeText(caracter, 3, size - PX / 2);
  1850.                     }
  1851.                     canvasContext.fillText(caracter, 3, size - PX / 2);
  1852.                 }
  1853.                 return this._canvas;
  1854.             }
  1855.         };
  1856.         /** @type {function (): undefined} */
  1857.         self.onload = init;
  1858.  
  1859.     })(window, jQuery);
  1860. });
  1861. };
Advertisement
Add Comment
Please, Sign In to add comment