From 8378cd02c140a29ddaddc1c5c4d26f7d07defb47 Mon Sep 17 00:00:00 2001 From: stefnotch Date: Fri, 21 Aug 2015 16:14:21 +0400 Subject: [PATCH 001/110] isItMe and getTeam improved Made the 2 slightly faster --- bot.user.js | 58 ++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/bot.user.js b/bot.user.js index 626c58fe..7e86ebb0 100644 --- a/bot.user.js +++ b/bot.user.js @@ -164,49 +164,47 @@ console.log("Running Apos Bot!"); function canSplit(player1, player2) { return compareSize(player1, player2, 2.30) && !compareSize(player1, player2, 7); } - function isItMe(player, cell) { - if (getMode() == ":teams") { - var currentColor = player[0].color; + if (getMode() == ":teams") { + var currentColor = player[0].color; + var currentRed = currentColor.substring(1,3); + var currentGreen = currentColor.substring(3,5); + var currentBlue = currentColor.substring(5,7); + + var currentTeam = getTeam(currentRed, currentGreen, currentBlue); - var currentRed = parseInt(currentColor.substring(1,3), 16); - var currentGreen = parseInt(currentColor.substring(3,5), 16); - var currentBlue = parseInt(currentColor.substring(5,7), 16); + var cellColor = cell.color; - var currentTeam = getTeam(currentRed, currentGreen, currentBlue); + var cellRed = cellColor.substring(1,3); + var cellGreen = cellColor.substring(3,5); + var cellBlue = cellColor.substring(5,7); - var cellColor = cell.color; + var cellTeam = getTeam(cellRed, cellGreen, cellBlue); - var cellRed = parseInt(cellColor.substring(1,3), 16); - var cellGreen = parseInt(cellColor.substring(3,5), 16); - var cellBlue = parseInt(cellColor.substring(5,7), 16); + if (currentTeam == cellTeam && !cell.isVirus()) { + return true; + } - var cellTeam = getTeam(cellRed, cellGreen, cellBlue); + //console.log("COLOR: " + color); - if (currentTeam == cellTeam && !cell.isVirus()) { + } else { + for (var i = 0; i < player.length; i++) { + if (cell.id == player[i].id) { return true; } - - //console.log("COLOR: " + color); - - } else { - for (var i = 0; i < player.length; i++) { - if (cell.id == player[i].id) { - return true; - } - } } - return false; } + return false; +} - function getTeam(red, green, blue) { - if (red > green && red > blue) { - return 0; - } else if (green > red && green > blue) { - return 1; - } - return 2; +function getTeam(red, green, blue) { + if (red == "ff") { + return 0; + } else if (green == "ff") { + return 1; } + return 2; +} function isFood(blob, cell) { if (!cell.isVirus() && compareSize(cell, blob, 1.30) || (cell.size <= 11)) { From 4915750873bfa593881fb580b051c4a6f571e659 Mon Sep 17 00:00:00 2001 From: Apostolique Date: Fri, 21 Aug 2015 11:58:19 -0400 Subject: [PATCH 002/110] Where is your indentation man? --- bot.user.js | 70 ++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/bot.user.js b/bot.user.js index 7e86ebb0..915da7d2 100644 --- a/bot.user.js +++ b/bot.user.js @@ -165,46 +165,46 @@ console.log("Running Apos Bot!"); return compareSize(player1, player2, 2.30) && !compareSize(player1, player2, 7); } function isItMe(player, cell) { - if (getMode() == ":teams") { - var currentColor = player[0].color; - var currentRed = currentColor.substring(1,3); - var currentGreen = currentColor.substring(3,5); - var currentBlue = currentColor.substring(5,7); - - var currentTeam = getTeam(currentRed, currentGreen, currentBlue); - - var cellColor = cell.color; - - var cellRed = cellColor.substring(1,3); - var cellGreen = cellColor.substring(3,5); - var cellBlue = cellColor.substring(5,7); - - var cellTeam = getTeam(cellRed, cellGreen, cellBlue); - - if (currentTeam == cellTeam && !cell.isVirus()) { - return true; - } - - //console.log("COLOR: " + color); - - } else { - for (var i = 0; i < player.length; i++) { - if (cell.id == player[i].id) { + if (getMode() == ":teams") { + var currentColor = player[0].color; + var currentRed = currentColor.substring(1,3); + var currentGreen = currentColor.substring(3,5); + var currentBlue = currentColor.substring(5,7); + + var currentTeam = getTeam(currentRed, currentGreen, currentBlue); + + var cellColor = cell.color; + + var cellRed = cellColor.substring(1,3); + var cellGreen = cellColor.substring(3,5); + var cellBlue = cellColor.substring(5,7); + + var cellTeam = getTeam(cellRed, cellGreen, cellBlue); + + if (currentTeam == cellTeam && !cell.isVirus()) { return true; } + + //console.log("COLOR: " + color); + + } else { + for (var i = 0; i < player.length; i++) { + if (cell.id == player[i].id) { + return true; + } + } } + return false; } - return false; -} - -function getTeam(red, green, blue) { - if (red == "ff") { - return 0; - } else if (green == "ff") { - return 1; + + function getTeam(red, green, blue) { + if (red == "ff") { + return 0; + } else if (green == "ff") { + return 1; + } + return 2; } - return 2; -} function isFood(blob, cell) { if (!cell.isVirus() && compareSize(cell, blob, 1.30) || (cell.size <= 11)) { From f69486450ff15f24b67475ce5d5af0638520c637 Mon Sep 17 00:00:00 2001 From: Apostolique Date: Fri, 21 Aug 2015 15:33:08 -0400 Subject: [PATCH 003/110] Value tuning --- bot.user.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/bot.user.js b/bot.user.js index 915da7d2..9235a781 100644 --- a/bot.user.js +++ b/bot.user.js @@ -24,12 +24,12 @@ SOFTWARE.*/ // @name AposBot // @namespace AposBot // @include http://agar.io/* -// @version 3.564 +// @version 3.565 // @grant none // @author http://www.twitch.tv/apostolique // ==/UserScript== -var aposBotVersion = 3.564; +var aposBotVersion = 3.565; //TODO: Team mode // Detect when people are merging @@ -162,8 +162,9 @@ console.log("Running Apos Bot!"); } function canSplit(player1, player2) { - return compareSize(player1, player2, 2.30) && !compareSize(player1, player2, 7); + return compareSize(player1, player2, 2.66) && !compareSize(player1, player2, 7); } + function isItMe(player, cell) { if (getMode() == ":teams") { var currentColor = player[0].color; @@ -207,17 +208,21 @@ console.log("Running Apos Bot!"); } function isFood(blob, cell) { - if (!cell.isVirus() && compareSize(cell, blob, 1.30) || (cell.size <= 11)) { + if (!cell.isVirus() && compareSize(cell, blob, 1.33) || (cell.size <= 11)) { return true; } return false; } function isThreat(blob, cell) { - if (!cell.isVirus() && compareSize(blob, cell, 1.30)) { + if (!cell.isVirus() && isFood(cell, blob)) { return true; } return false; + /*if (!cell.isVirus() && compareSize(blob, cell, 1.33)) { + return true; + } + return false;*/ } function isVirus(blob, cell) { From 90ce2bfa774d8b1a27c7e2e7b53c8a9d6a90231a Mon Sep 17 00:00:00 2001 From: Apostolique Date: Sun, 23 Aug 2015 13:40:58 -0400 Subject: [PATCH 004/110] Only controls a single cell now --- bot.user.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/bot.user.js b/bot.user.js index 9235a781..bccfad4c 100644 --- a/bot.user.js +++ b/bot.user.js @@ -24,12 +24,12 @@ SOFTWARE.*/ // @name AposBot // @namespace AposBot // @include http://agar.io/* -// @version 3.565 +// @version 3.566 // @grant none // @author http://www.twitch.tv/apostolique // ==/UserScript== -var aposBotVersion = 3.565; +var aposBotVersion = 3.566; //TODO: Team mode // Detect when people are merging @@ -628,8 +628,8 @@ console.log("Running Apos Bot!"); //LEFT //console.log("Left"); listToUse.push([ - [90, true], - [270, false], computeDistance(getMapStartX(), blob.y, blob.x, blob.y) + [89, true], + [271, false], computeDistance(getMapStartX(), blob.y, blob.x, blob.y) ]); var lineLeft = followAngle(90, blob.x, blob.y, 190 + blob.size); var lineRight = followAngle(270, blob.x, blob.y, 190 + blob.size); @@ -641,8 +641,8 @@ console.log("Running Apos Bot!"); //TOP //console.log("TOP"); listToUse.push([ - [180, true], - [0, false], computeDistance(blob.x, getMapStartY, blob.x, blob.y) + [179, true], + [1, false], computeDistance(blob.x, getMapStartY, blob.x, blob.y) ]); var lineLeft = followAngle(180, blob.x, blob.y, 190 + blob.size); var lineRight = followAngle(360, blob.x, blob.y, 190 + blob.size); @@ -654,8 +654,8 @@ console.log("Running Apos Bot!"); //RIGHT //console.log("RIGHT"); listToUse.push([ - [270, true], - [90, false], computeDistance(getMapEndX(), blob.y, blob.x, blob.y) + [269, true], + [91, false], computeDistance(getMapEndX(), blob.y, blob.x, blob.y) ]); var lineLeft = followAngle(270, blob.x, blob.y, 190 + blob.size); var lineRight = followAngle(90, blob.x, blob.y, 190 + blob.size); @@ -667,8 +667,8 @@ console.log("Running Apos Bot!"); //BOTTOM //console.log("BOTTOM"); listToUse.push([ - [0, true], - [180, false], computeDistance(blob.x, getMapEndY, blob.x, blob.y) + [359, true], + [181, false], computeDistance(blob.x, getMapEndY, blob.x, blob.y) ]); var lineLeft = followAngle(0, blob.x, blob.y, 190 + blob.size); var lineRight = followAngle(180, blob.x, blob.y, 190 + blob.size); @@ -856,6 +856,14 @@ console.log("Running Apos Bot!"); //Loop through all the player's cells. for (var k = 0; k < player.length; k++) { + if (true) { + drawPoint(player[k].x, player[k].y + player[k].size, 0, "" + (getLastUpdate() - player[k].birth) + " / " + (30000 + (player[k].birthMass * 57) - (getLastUpdate() - player[k].birth)) + " / " + player[k].birthMass); + } + } + + + //Loops only for one cell for now. + for (var k = 0; /*k < player.length*/ k < 1; k++) { //console.log("Working on blob: " + k); @@ -1110,7 +1118,7 @@ console.log("Running Apos Bot!"); var destination = followAngle(shiftedAngle, player[k].x, player[k].y, distance); - destinationChoices.push(destination); + destinationChoices = destination; drawLine(player[k].x, player[k].y, destination[0], destination[1], 1); //tempMoveX = destination[0]; //tempMoveY = destination[1]; @@ -1131,7 +1139,7 @@ console.log("Running Apos Bot!"); var line1 = followAngle(perfectAngle, player[k].x, player[k].y, f.verticalDistance()); - destinationChoices.push(line1); + destinationChoices = line1; drawLine(player[k].x, player[k].y, line1[0], line1[1], 7); //tempMoveX = line1[0]; //tempMoveY = line1[1]; @@ -1140,7 +1148,7 @@ console.log("Running Apos Bot!"); //You're likely screwed. (This should never happen.) console.log("Failed"); - destinationChoices.push([tempMoveX, tempMoveY]); + destinationChoices = [tempMoveX, tempMoveY]; /*var angleWeights = [] //Put weights on the angles according to enemy distance for (var i = 0; i < allPossibleThreats.length; i++){ var dist = computeDistance(player[k].x, player[k].y, allPossibleThreats[i].x, allPossibleThreats[i].y); @@ -1193,13 +1201,13 @@ console.log("Running Apos Bot!"); var destination = followAngle(shiftedAngle, player[k].x, player[k].y, distance); - destinationChoices.push(destination); + destinationChoices = destination; //tempMoveX = destination[0]; //tempMoveY = destination[1]; drawLine(player[k].x, player[k].y, destination[0], destination[1], 1); } else { //If there are no enemies around and no food to eat. - destinationChoices.push([tempMoveX, tempMoveY]); + destinationChoices = [tempMoveX, tempMoveY]; } drawPoint(tempPoint[0], tempPoint[1], tempPoint[2], ""); @@ -1418,7 +1426,7 @@ console.log("Running Apos Bot!"); } /** - * The game's current mode. (FFA is "", ":experimental", ":teams". ":party") + * The game's current mode. (":ffa", ":experimental", ":teams". ":party") * @return {[type]} [description] */ function getMode() { From 0199a3a39f02fe7b3de0341f51907d936fb11877 Mon Sep 17 00:00:00 2001 From: Apostolique Date: Sun, 23 Aug 2015 13:41:20 -0400 Subject: [PATCH 005/110] Only controls a single cell now --- launcher.user.js | 75 ++++++++++-------------------------------------- 1 file changed, 15 insertions(+), 60 deletions(-) diff --git a/launcher.user.js b/launcher.user.js index 891f4e75..a0dc1191 100644 --- a/launcher.user.js +++ b/launcher.user.js @@ -24,12 +24,12 @@ SOFTWARE.*/ // @name AposLauncher // @namespace AposLauncher // @include http://agar.io/* -// @version 3.068 +// @version 3.069 // @grant none // @author http://www.twitch.tv/apostolique // ==/UserScript== -var aposLauncherVersion = 3.068; +var aposLauncherVersion = 3.069; Number.prototype.mod = function(n) { return ((this % n) + n) % n; @@ -92,10 +92,6 @@ console.log("Running Bot Launcher!"); console.log("ToggleDraw"); toggleDraw = !toggleDraw; } - if (83 == e.keyCode) { - selectedCell = (selectedCell + 1).mod(getPlayer().length + 1); - console.log("Next Cell " + selectedCell); - } if (68 == e.keyCode) { window.setDarkTheme(!getDarkBool()); } @@ -120,15 +116,7 @@ console.log("Running Bot Launcher!"); function humanPlayer() { //Don't need to do anything. - var player = getPlayer(); - - var destination = []; - - for (var i = 0; i < player.length; i++) { - destination.push([getPointX(), getPointY()]) - } - - return destination; + return [getPointX(), getPointY()]; } function pb() { @@ -252,18 +240,8 @@ console.log("Running Bot Launcher!"); function Aa() { //UPDATE - if (selectedCell > 0 && selectedCell <= getPlayer().length) { - setPoint(((fa - m / 2) / h + s), ((ga - r / 2) / h + t), selectedCell - 1); - drawCircle(getPlayer()[selectedCell - 1].x, getPlayer()[selectedCell - 1].y, getPlayer()[selectedCell - 1].size, 8); - drawCircle(getPlayer()[selectedCell - 1].x, getPlayer()[selectedCell - 1].y, getPlayer()[selectedCell - 1].size / 2, 8); - } else if (selectedCell > getPlayer().length) { - selectedCell = 0; - } if (toggle || window.botList[botIndex][0] == "Human") { - var startIndex = (selectedCell == 0 ? 0 : selectedCell - 1); - for (var i = 0; i < getPlayer().length - (selectedCell == 0 ? 0 : 1); i++) { - setPoint(((fa - m / 2) / h + s) + i, ((ga - r / 2) / h + t) + i, (i + startIndex).mod(getPlayer().length)); - } + setPoint(((fa - m / 2) / h + s), ((ga - r / 2) / h + t)); } } @@ -567,7 +545,7 @@ console.log("Running Bot Launcher!"); n.sa = c; n.Q = C; n.ba = w; - q && n.B(q); - 1 != M.indexOf(d) && -1 == k.indexOf(n) && (document.getElementById("overlays").style.display = "none", k.push(n), 1 == k.length && (s = n.x, t = n.y, db())) + q && n.B(q); - 1 != M.indexOf(d) && -1 == k.indexOf(n) && (document.getElementById("overlays").style.display = "none", k.push(n), n.birth = getLastUpdate(), n.birthMass = (n.size * n.size / 100), 1 == k.length && (s = n.x, t = n.y, db())) //UPDATE interNodes[d] = window.getCells()[d]; @@ -578,6 +556,7 @@ console.log("Running Bot Launcher!"); //console.log("start: " + interNodes[element].updateTime + " current: " + D + " life: " + (D - interNodes[element].updateTime)); var isRemoved = !window.getCells().hasOwnProperty(element); + //console.log("Time not updated: " + (window.getLastUpdate() - interNodes[element].getUptimeTime())); if (isRemoved && (window.getLastUpdate() - interNodes[element].getUptimeTime()) > 3000) { delete interNodes[element]; } else { @@ -669,11 +648,8 @@ console.log("Running Bot Launcher!"); if (T()) { var a = fa - m / 2; var b = ga - r / 2; - for (var i = 0; i < getPlayer().length; i++) { - var tempID = getPlayer()[i].id; - 64 > a * a + b * b || .01 > Math.abs(eb - ia[i]) && - .01 > Math.abs(fb - ja[i]) || (eb = ia[i], fb = ja[i], a = N(13), a.setUint8(0, 16), a.setInt32(1, ia[i], !0), a.setInt32(5, ja[i], !0), a.setUint32(9, tempID, !0), O(a)) - } + 64 > a * a + b * b || .01 > Math.abs(eb - ia) && + .01 > Math.abs(fb - ja) || (eb = ia, fb = ja, a = N(13), a.setUint8(0, 16), a.setInt32(1, ia, !0), a.setInt32(5, ja, !0), a.setUint32(9, 0, !0), O(a)) } } @@ -795,14 +771,8 @@ console.log("Running Bot Launcher!"); //UPDATE if (getPlayer().length > 0) { var moveLoc = window.botList[botIndex][1](toggleFollow); - if (selectedCell > 0) { - Aa(); - } if (!toggle) { - var startIndex = (selectedCell == 0 ? 0 : selectedCell); - for (var i = 0; i < getPlayer().length - (selectedCell == 0 ? 0 : 1); i++) { - setPoint(moveLoc[(i + startIndex).mod(getPlayer().length)][0], moveLoc[(i + startIndex).mod(getPlayer().length)][1], (i + startIndex).mod(getPlayer().length)); - } + setPoint(moveLoc[0], moveLoc[1]); } } customRender(f); @@ -985,7 +955,7 @@ console.log("Running Bot Launcher!"); text.C(dText[i]); var textRender = text.L(); - d.drawImage(textRender, dPoints[i][0], dPoints[i][1]); + d.drawImage(textRender, dPoints[i][0] - (textRender.width / 2), dPoints[i][1] - (textRender.height / 2)); } } @@ -1002,7 +972,6 @@ console.log("Running Bot Launcher!"); debugStrings.push("T - Bot: " + (!toggle ? "On" : "Off")); debugStrings.push("R - Lines: " + (!toggleDraw ? "On" : "Off")); debugStrings.push("Q - Follow Mouse: " + (toggleFollow ? "On" : "Off")); - debugStrings.push("S - Manual Cell: " + (selectedCell == 0 ? "None" : selectedCell) + " of " + getPlayer().length); debugStrings.push(""); debugStrings.push("Best Score: " + ~~(sessionScore / 100)); debugStrings.push("Best Time: " + bestTime + " seconds"); @@ -1331,7 +1300,6 @@ console.log("Running Bot Launcher!"); botIndex = 0, reviving = false, message = [], - selectedCell = 0, q = null, s = 0, @@ -1346,8 +1314,8 @@ console.log("Running Bot Launcher!"); ga = 0, //UPDATE - ia = [-1], - ja = [-1], + ia = -1, + ja = -1, zb = 0, C = 0, @@ -1809,22 +1777,9 @@ console.log("Running Bot Launcher!"); return P; } - window.setPoint = function(x, y, index) { - while (ia.length > getPlayer().length) { - ia.pop(); - ja.pop(); - } - if (index < ia.length) { - ia[index] = x; - ja[index] = y; - } else { - while (index < ia.length - 1) { - ia.push(-1); - ja.push(-1); - } - ia.push(x); - ja.push(y); - } + window.setPoint = function(x, y) { + ia = x; + ja = y; } window.setScore = function(a) { From 43ee61134c49c572720041d7bb727ec7b45f897c Mon Sep 17 00:00:00 2001 From: Apostolique Date: Sun, 23 Aug 2015 14:33:17 -0400 Subject: [PATCH 006/110] Hotfix for a crash after last update --- launcher.user.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/launcher.user.js b/launcher.user.js index a0dc1191..b9742019 100644 --- a/launcher.user.js +++ b/launcher.user.js @@ -24,12 +24,12 @@ SOFTWARE.*/ // @name AposLauncher // @namespace AposLauncher // @include http://agar.io/* -// @version 3.069 +// @version 3.0691 // @grant none // @author http://www.twitch.tv/apostolique // ==/UserScript== -var aposLauncherVersion = 3.069; +var aposLauncherVersion = 3.0691; Number.prototype.mod = function(n) { return ((this % n) + n) % n; @@ -1730,11 +1730,11 @@ console.log("Running Bot Launcher!"); } window.getPointX = function() { - return ia[0]; + return ia; } window.getPointY = function() { - return ja[0]; + return ja; } window.getMouseX = function() { From 547ce2467a103dcf2d084b60b5ae5611eb668cf3 Mon Sep 17 00:00:00 2001 From: Apostolique Date: Sun, 23 Aug 2015 22:03:25 -0400 Subject: [PATCH 007/110] Started refactoring --- bot.user.js | 2116 +++++++++++++++++++++++++-------------------------- 1 file changed, 1036 insertions(+), 1080 deletions(-) diff --git a/bot.user.js b/bot.user.js index bccfad4c..3a16d2de 100644 --- a/bot.user.js +++ b/bot.user.js @@ -24,12 +24,12 @@ SOFTWARE.*/ // @name AposBot // @namespace AposBot // @include http://agar.io/* -// @version 3.566 +// @version 3.567 // @grant none // @author http://www.twitch.tv/apostolique // ==/UserScript== -var aposBotVersion = 3.566; +var aposBotVersion = 3.567; //TODO: Team mode // Detect when people are merging @@ -53,7 +53,7 @@ function getLatestCommit() { cache: false, dataType: "jsonp" }).done(function(data) { - console.dir(data["data"]) + console.dir(data["data"]); console.log("hmm: " + data["data"]["object"]["sha"]); sha = data["data"]["object"]["sha"]; @@ -88,1348 +88,1304 @@ function getLatestCommit() { getLatestCommit(); console.log("Running Apos Bot!"); -(function(f, g) { - var splitDistance = 710; - console.log("Apos Bot!"); - - if (f.botList == null) { - f.botList = []; - g('#locationUnknown').append(g('')); + g('#locationUnknown').addClass('form-group'); +} - //Given an angle value that was gotten from valueAndleBased(), - //returns a new value that scales it appropriately. - function paraAngleValue(angleValue, range) { - return (15 / (range[1])) * (angleValue * angleValue) - (range[1] / 6); +for (var i = f.botList.length - 1; i >= 0; i--) { + if (f.botList[i][0] == "Human") { + f.botList.splice(i, 1); } +} - function valueAngleBased(angle, range) { - var leftValue = (angle - range[0]).mod(360); - var rightValue = (rangeToAngle(range) - angle).mod(360); - var bestValue = Math.min(leftValue, rightValue); +//Given an angle value that was gotten from valueAndleBased(), +//returns a new value that scales it appropriately. +u.paraAngleValue = function(angleValue, range) { + return (15 / (range[1])) * (angleValue * angleValue) - (range[1] / 6); +}; - if (bestValue <= range[1]) { - return paraAngleValue(bestValue, range); - } - var banana = -1; - return banana; +u.valueAngleBased = function(angle, range) { + var leftValue = (angle - range[0]).mod(360); + var rightValue = (u.rangeToAngle(range) - angle).mod(360); + + var bestValue = Math.min(leftValue, rightValue); + if (bestValue <= range[1]) { + return u.paraAngleValue(bestValue, range); } + return -1; +}; - function computeDistance(x1, y1, x2, y2) { - var xdis = x1 - x2; // <--- FAKE AmS OF COURSE! - var ydis = y1 - y2; - var distance = Math.sqrt(xdis * xdis + ydis * ydis); +u.computeDistance = function(x1, y1, x2, y2) { + var xdis = x1 - x2; // <--- FAKE AmS OF COURSE! + var ydis = y1 - y2; + var distance = Math.sqrt(xdis * xdis + ydis * ydis); - return distance; - } + return distance; +}; - function computeDistanceFromCircleEdge(x1, y1, x2, y2, s2) { - var tempD = computeDistance(x1, y1, x2, y2); +u.computeDistanceFromCircleEdge = function(x1, y1, x2, y2, s2) { + var tempD = u.computeDistance(x1, y1, x2, y2); - var offsetX = 0; - var offsetY = 0; + var offsetX = 0; + var offsetY = 0; - var ratioX = tempD / (x1 - x2); - var ratioY = tempD / (y1 - y2); + var ratioX = tempD / (x1 - x2); + var ratioY = tempD / (y1 - y2); - offsetX = x1 - (s2 / ratioX); - offsetY = y1 - (s2 / ratioY); + offsetX = x1 - (s2 / ratioX); + offsetY = y1 - (s2 / ratioY); - drawPoint(offsetX, offsetY, 5, ""); + u.drawPoint(offsetX, offsetY, 5, ""); + + return u.computeDistance(x2, y2, offsetX, offsetY); +}; - return computeDistance(x2, y2, offsetX, offsetY); +u.compareSize = function(player1, player2, ratio) { + if (player1.size * player1.size * ratio < player2.size * player2.size) { + return true; } + return false; +}; + +u.canSplit = function(player1, player2) { + return u.compareSize(player1, player2, 2.66) && !u.compareSize(player1, player2, 7); +}; + +u.isItMe = function(player, cell) { + if (u.getMode() == ":teams") { + var currentColor = player[0].color; + var currentRed = currentColor.substring(1,3); + var currentGreen = currentColor.substring(3,5); + var currentBlue = currentColor.substring(5,7); + + var currentTeam = u.getTeam(currentRed, currentGreen, currentBlue); - function compareSize(player1, player2, ratio) { - if (player1.size * player1.size * ratio < player2.size * player2.size) { + var cellColor = cell.color; + + var cellRed = cellColor.substring(1,3); + var cellGreen = cellColor.substring(3,5); + var cellBlue = cellColor.substring(5,7); + + var cellTeam = getTeam(cellRed, cellGreen, cellBlue); + + if (currentTeam == cellTeam && !cell.isVirus()) { return true; } - return false; - } - function canSplit(player1, player2) { - return compareSize(player1, player2, 2.66) && !compareSize(player1, player2, 7); - } + //console.log("COLOR: " + color); - function isItMe(player, cell) { - if (getMode() == ":teams") { - var currentColor = player[0].color; - var currentRed = currentColor.substring(1,3); - var currentGreen = currentColor.substring(3,5); - var currentBlue = currentColor.substring(5,7); - - var currentTeam = getTeam(currentRed, currentGreen, currentBlue); - - var cellColor = cell.color; - - var cellRed = cellColor.substring(1,3); - var cellGreen = cellColor.substring(3,5); - var cellBlue = cellColor.substring(5,7); - - var cellTeam = getTeam(cellRed, cellGreen, cellBlue); - - if (currentTeam == cellTeam && !cell.isVirus()) { + } else { + for (var i = 0; i < player.length; i++) { + if (cell.id == player[i].id) { return true; } - - //console.log("COLOR: " + color); - - } else { - for (var i = 0; i < player.length; i++) { - if (cell.id == player[i].id) { - return true; - } - } - } - return false; - } - - function getTeam(red, green, blue) { - if (red == "ff") { - return 0; - } else if (green == "ff") { - return 1; } - return 2; - } - - function isFood(blob, cell) { - if (!cell.isVirus() && compareSize(cell, blob, 1.33) || (cell.size <= 11)) { - return true; - } - return false; } + return false; +}; - function isThreat(blob, cell) { - if (!cell.isVirus() && isFood(cell, blob)) { - return true; - } - return false; - /*if (!cell.isVirus() && compareSize(blob, cell, 1.33)) { - return true; - } - return false;*/ +u.getTeam = function(red, green, blue) { + if (red == "ff") { + return 0; + } else if (green == "ff") { + return 1; } + return 2; +}; - function isVirus(blob, cell) { - if (cell.isVirus() && compareSize(cell, blob, 1.30)) { - return true; - } else if (cell.isVirus() && cell.color.substring(3,5).toLowerCase() != "ff") { - return true; - } - return false; +u.isFood = function(blob, cell) { + if (!cell.isVirus() && u.compareSize(cell, blob, 1.33) || (cell.size <= 11)) { + return true; } - - function isSplitTarget(blob, cell) { - /*if (canSplit(cell, blob)) { - return true; - }*/ - return false; + return false; +} +; +u.isThreat = function(blob, cell) { + if (!cell.isVirus() && u.isFood(cell, blob)) { + return true; } - - function getMass(blob){ - setShowMass(true); - //This var is only declared while showMass=true. - return blob.O.F; + return false; + /*if (!cell.isVirus() && compareSize(blob, cell, 1.33)) { + return true; } + return false;*/ +}; - function getTimeToRemerge(mass){ - return ((mass*0.02) +30); +u.isVirus = function(blob, cell) { + if (cell.isVirus() && u.compareSize(cell, blob, 1.30)) { + return true; + } else if (cell.isVirus() && cell.color.substring(3,5).toLowerCase() != "ff") { + return true; } + return false; +}; - function getBlobCount(player){ - var count = 0; - Object.keys(player).forEach(function(item, i) { - count++; - }); - //return Object.keys(player).length - 1; - return count - 1; - } +u.isSplitTarget = function(blob, cell) { + /*if (canSplit(cell, blob)) { + return true; + }*/ + return false; +}; - function separateListBasedOnFunction(listToUse, blob) { - var foodElementList = []; - var threatList = []; - var virusList = []; - var splitTargetList = []; +u.getTimeToRemerge = function(mass){ + return ((mass*0.02) + 30); +}; - var player = getPlayer(); +u.separateListBasedOnFunction = function(listToUse, blob) { + var foodElementList = []; + var threatList = []; + var virusList = []; + var splitTargetList = []; - Object.keys(listToUse).forEach(function(element, index) { - var isMe = isItMe(player, listToUse[element]); + var player = u.getPlayer(); - if (!isMe) { - if (isFood(blob, listToUse[element])) { - //IT'S FOOD! - foodElementList.push(listToUse[element]); + Object.keys(listToUse).forEach(function(element, index) { + var isMe = u.isItMe(player, listToUse[element]); - if (isSplitTarget(blob, listToUse[element])) { - drawCircle(listToUse[element].x, listToUse[element].y, listToUse[element].size + 50, 7); - splitTargetList.push(listToUse[element]) - } - } else if (isThreat(blob, listToUse[element])) { - //IT'S DANGER! - threatList.push(listToUse[element]); - } else if (isVirus(blob, listToUse[element])) { - //IT'S VIRUS! - virusList.push(listToUse[element]); - } - }/*else if(isMe && (getBlobCount(getPlayer()) > 0)){ - //Attempt to make the other cell follow the mother one + if (!isMe) { + if (u.isFood(blob, listToUse[element])) { + //IT'S FOOD! foodElementList.push(listToUse[element]); - }*/ - }); - foodList = []; - for (var i = 0; i < foodElementList.length; i++) { - foodList.push([foodElementList[i].x, foodElementList[i].y, foodElementList[i].size]); - } + if (u.isSplitTarget(blob, listToUse[element])) { + u.drawCircle(listToUse[element].x, listToUse[element].y, listToUse[element].size + 50, 7); + u.splitTargetList.push(listToUse[element]); + } + } else if (u.isThreat(blob, listToUse[element])) { + //IT'S DANGER! + threatList.push(listToUse[element]); + } else if (u.isVirus(blob, listToUse[element])) { + //IT'S VIRUS! + virusList.push(listToUse[element]); + } + }/*else if(isMe && (getBlobCount(getPlayer()) > 0)){ + //Attempt to make the other cell follow the mother one + foodElementList.push(listToUse[element]); + }*/ + }); - return [foodList, threatList, virusList, splitTargetList]; + foodList = []; + for (var i = 0; i < foodElementList.length; i++) { + foodList.push([foodElementList[i].x, foodElementList[i].y, foodElementList[i].size]); } - function getAll(blob) { - var dotList = []; - var player = getPlayer(); - var interNodes = getMemoryCells(); + return [foodList, threatList, virusList, splitTargetList]; +}; - dotList = separateListBasedOnFunction(interNodes, blob); +u.getAll = function(blob) { + var dotList = []; + var player = u.getPlayer(); + var interNodes = u.getMemoryCells(); - return dotList; - } + dotList = u.separateListBasedOnFunction(interNodes, blob); - function clusterFood(foodList, blobSize) { - var clusters = []; - var addedCluster = false; - - //1: x - //2: y - //3: size or value - //4: Angle, not set here. - - for (var i = 0; i < foodList.length; i++) { - for (var j = 0; j < clusters.length; j++) { - if (computeDistance(foodList[i][0], foodList[i][1], clusters[j][0], clusters[j][1]) < blobSize * 1.5) { - clusters[j][0] = (foodList[i][0] + clusters[j][0]) / 2; - clusters[j][1] = (foodList[i][1] + clusters[j][1]) / 2; - clusters[j][2] += foodList[i][2]; - addedCluster = true; - break; - } - } - if (!addedCluster) { - clusters.push([foodList[i][0], foodList[i][1], foodList[i][2], 0]); + return dotList; +}; + +u.clusterFood = function(foodList, blobSize) { + var clusters = []; + var addedCluster = false; + + //1: x + //2: y + //3: size or value + //4: Angle, not set here. + + for (var i = 0; i < foodList.length; i++) { + for (var j = 0; j < clusters.length; j++) { + if (u.computeDistance(foodList[i][0], foodList[i][1], clusters[j][0], clusters[j][1]) < blobSize * 1.5) { + clusters[j][0] = (foodList[i][0] + clusters[j][0]) / 2; + clusters[j][1] = (foodList[i][1] + clusters[j][1]) / 2; + clusters[j][2] += foodList[i][2]; + addedCluster = true; + break; } - addedCluster = false; } - return clusters; + if (!addedCluster) { + clusters.push([foodList[i][0], foodList[i][1], foodList[i][2], 0]); + } + addedCluster = false; } + return clusters; +}; - function getAngle(x1, y1, x2, y2) { - //Handle vertical and horizontal lines. +u.getAngle = function(x1, y1, x2, y2) { + //Handle vertical and horizontal lines. - if (x1 == x2) { - if (y1 < y2) { - return 271; - //return 89; - } else { - return 89; - } + if (x1 == x2) { + if (y1 < y2) { + return 271; + //return 89; + } else { + return 89; } - - return (Math.round(Math.atan2(-(y1 - y2), -(x1 - x2)) / Math.PI * 180 + 180)); } - function slope(x1, y1, x2, y2) { - var m = (y1 - y2) / (x1 - x2); + return (Math.round(Math.atan2(-(y1 - y2), -(x1 - x2)) / Math.PI * 180 + 180)); +}; - return m; - } +u.slope = function(x1, y1, x2, y2) { + var m = (y1 - y2) / (x1 - x2); - function slopeFromAngle(degree) { - if (degree == 270) { - degree = 271; - } else if (degree == 90) { - degree = 91; - } - return Math.tan((degree - 180) / 180 * Math.PI); - } + return m; +}; - //Given two points on a line, finds the slope of a perpendicular line crossing it. - function inverseSlope(x1, y1, x2, y2) { - var m = slope(x1, y1, x2, y2); - return (-1) / m; +u.slopeFromAngle = function(degree) { + if (degree == 270) { + degree = 271; + } else if (degree == 90) { + degree = 91; } + return Math.tan((degree - 180) / 180 * Math.PI); +}; - //Given a slope and an offset, returns two points on that line. - function pointsOnLine(slope, useX, useY, distance) { - var b = useY - slope * useX; - var r = Math.sqrt(1 + slope * slope); +//Given two points on a line, finds the slope of a perpendicular line crossing it. +u.inverseSlope = function(x1, y1, x2, y2) { + var m = slope(x1, y1, x2, y2); + return (-1) / m; +}; - var newX1 = (useX + (distance / r)); - var newY1 = (useY + ((distance * slope) / r)); - var newX2 = (useX + ((-distance) / r)); - var newY2 = (useY + (((-distance) * slope) / r)); +//Given a slope and an offset, returns two points on that line. +u.pointsOnLine = function(slope, useX, useY, distance) { + var b = useY - slope * useX; + var r = Math.sqrt(1 + slope * slope); - return [ - [newX1, newY1], - [newX2, newY2] - ]; - } + var newX1 = (useX + (distance / r)); + var newY1 = (useY + ((distance * slope) / r)); + var newX2 = (useX + ((-distance) / r)); + var newY2 = (useY + (((-distance) * slope) / r)); - function followAngle(angle, useX, useY, distance) { - var slope = slopeFromAngle(angle); - var coords = pointsOnLine(slope, useX, useY, distance); + return [ + [newX1, newY1], + [newX2, newY2] + ]; +}; - var side = (angle - 90).mod(360); - if (side < 180) { - return coords[1]; - } else { - return coords[0]; - } - } +u.followAngle = function(angle, useX, useY, distance) { + var slope = u.slopeFromAngle(angle); + var coords = u.pointsOnLine(slope, useX, useY, distance); - //Using a line formed from point a to b, tells if point c is on S side of that line. - function isSideLine(a, b, c) { - if ((b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]) > 0) { - return true; - } - return false; + var side = (angle - 90).mod(360); + if (side < 180) { + return coords[1]; + } else { + return coords[0]; } +}; - //angle range2 is within angle range2 - //an Angle is a point and a distance between an other point [5, 40] - function angleRangeIsWithin(range1, range2) { - if (range2[0] == (range2[0] + range2[1]).mod(360)) { - return true; - } - //console.log("r1: " + range1[0] + ", " + range1[1] + " ... r2: " + range2[0] + ", " + range2[1]); - - var distanceFrom0 = (range1[0] - range2[0]).mod(360); - var distanceFrom1 = (range1[1] - range2[0]).mod(360); +//Using a line formed from point a to b, tells if point c is on S side of that line. +u.isSideLine = function(a, b, c) { + if ((b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]) > 0) { + return true; + } + return false; +}; - if (distanceFrom0 < range2[1] && distanceFrom1 < range2[1] && distanceFrom0 < distanceFrom1) { - return true; - } - return false; +//angle range2 is within angle range2 +//an Angle is a point and a distance between an other point [5, 40] +u.angleRangeIsWithin = function(range1, range2) { + if (range2[0] == (range2[0] + range2[1]).mod(360)) { + return true; } + //console.log("r1: " + range1[0] + ", " + range1[1] + " ... r2: " + range2[0] + ", " + range2[1]); - function angleRangeIsWithinInverted(range1, range2) { - var distanceFrom0 = (range1[0] - range2[0]).mod(360); - var distanceFrom1 = (range1[1] - range2[0]).mod(360); + var distanceFrom0 = (range1[0] - range2[0]).mod(360); + var distanceFrom1 = (range1[1] - range2[0]).mod(360); - if (distanceFrom0 < range2[1] && distanceFrom1 < range2[1] && distanceFrom0 > distanceFrom1) { - return true; - } - return false; + if (distanceFrom0 < range2[1] && distanceFrom1 < range2[1] && distanceFrom0 < distanceFrom1) { + return true; } + return false; +}; - function angleIsWithin(angle, range) { - var diff = (rangeToAngle(range) - angle).mod(360); - if (diff >= 0 && diff <= range[1]) { - return true; - } - return false; - } +u.angleRangeIsWithinInverted = function(range1, range2) { + var distanceFrom0 = (range1[0] - range2[0]).mod(360); + var distanceFrom1 = (range1[1] - range2[0]).mod(360); - function rangeToAngle(range) { - return (range[0] + range[1]).mod(360); + if (distanceFrom0 < range2[1] && distanceFrom1 < range2[1] && distanceFrom0 > distanceFrom1) { + return true; } + return false; +}; - function anglePair(range) { - return (range[0] + ", " + rangeToAngle(range) + " range: " + range[1]); +u.angleIsWithin = function(angle, range) { + var diff = (u.rangeToAngle(range) - angle).mod(360); + if (diff >= 0 && diff <= range[1]) { + return true; } + return false; +}; - function computeAngleRanges(blob1, blob2) { - var mainAngle = getAngle(blob1.x, blob1.y, blob2.x, blob2.y); - var leftAngle = (mainAngle - 90).mod(360); - var rightAngle = (mainAngle + 90).mod(360); +u.rangeToAngle = function(range) { + return (range[0] + range[1]).mod(360); +}; - var blob1Left = followAngle(leftAngle, blob1.x, blob1.y, blob1.size); - var blob1Right = followAngle(rightAngle, blob1.x, blob1.y, blob1.size); +u.anglePair = function(range) { + return (range[0] + ", " + u.rangeToAngle(range) + " range: " + range[1]); +}; - var blob2Left = followAngle(rightAngle, blob2.x, blob2.y, blob2.size); - var blob2Right = followAngle(leftAngle, blob2.x, blob2.y, blob2.size); +u.computeAngleRanges = function(blob1, blob2) { + var mainAngle = u.getAngle(blob1.x, blob1.y, blob2.x, blob2.y); + var leftAngle = (mainAngle - 90).mod(360); + var rightAngle = (mainAngle + 90).mod(360); - var blob1AngleLeft = getAngle(blob2.x, blob2.y, blob1Left[0], blob1Left[1]); - var blob1AngleRight = getAngle(blob2.x, blob2.y, blob1Right[0], blob1Right[1]); + var blob1Left = u.followAngle(leftAngle, blob1.x, blob1.y, blob1.size); + var blob1Right = u.followAngle(rightAngle, blob1.x, blob1.y, blob1.size); - var blob2AngleLeft = getAngle(blob1.x, blob1.y, blob2Left[0], blob2Left[1]); - var blob2AngleRight = getAngle(blob1.x, blob1.y, blob2Right[0], blob2Right[1]); + var blob2Left = u.followAngle(rightAngle, blob2.x, blob2.y, blob2.size); + var blob2Right = u.followAngle(leftAngle, blob2.x, blob2.y, blob2.size); - var blob1Range = (blob1AngleRight - blob1AngleLeft).mod(360); - var blob2Range = (blob2AngleRight - blob2AngleLeft).mod(360); + var blob1AngleLeft = u.getAngle(blob2.x, blob2.y, blob1Left[0], blob1Left[1]); + var blob1AngleRight = u.getAngle(blob2.x, blob2.y, blob1Right[0], blob1Right[1]); - var tempLine = followAngle(blob2AngleLeft, blob2Left[0], blob2Left[1], 400); - //drawLine(blob2Left[0], blob2Left[1], tempLine[0], tempLine[1], 0); + var blob2AngleLeft = u.getAngle(blob1.x, blob1.y, blob2Left[0], blob2Left[1]); + var blob2AngleRight = u.getAngle(blob1.x, blob1.y, blob2Right[0], blob2Right[1]); - if ((blob1Range / blob2Range) > 1) { - drawPoint(blob1Left[0], blob1Left[1], 3, ""); - drawPoint(blob1Right[0], blob1Right[1], 3, ""); - drawPoint(blob1.x, blob1.y, 3, "" + blob1Range + ", " + blob2Range + " R: " + (Math.round((blob1Range / blob2Range) * 1000) / 1000)); - } + var blob1Range = (blob1AngleRight - blob1AngleLeft).mod(360); + var blob2Range = (blob2AngleRight - blob2AngleLeft).mod(360); - //drawPoint(blob2.x, blob2.y, 3, "" + blob1Range); - } + var tempLine = u.followAngle(blob2AngleLeft, blob2Left[0], blob2Left[1], 400); + //drawLine(blob2Left[0], blob2Left[1], tempLine[0], tempLine[1], 0); - function debugAngle(angle, text) { - var player = getPlayer(); - var line1 = followAngle(angle, player[0].x, player[0].y, 300); - drawLine(player[0].x, player[0].y, line1[0], line1[1], 5); - drawPoint(line1[0], line1[1], 5, "" + text); + if ((blob1Range / blob2Range) > 1) { + drawPoint(blob1Left[0], blob1Left[1], 3, ""); + drawPoint(blob1Right[0], blob1Right[1], 3, ""); + drawPoint(blob1.x, blob1.y, 3, "" + blob1Range + ", " + blob2Range + " R: " + (Math.round((blob1Range / blob2Range) * 1000) / 1000)); } - //TODO: Don't let this function do the radius math. - function getEdgeLinesFromPoint(blob1, blob2, radius) { - var px = blob1.x; - var py = blob1.y; - - var cx = blob2.x; - var cy = blob2.y; - - //var radius = blob2.size; + //drawPoint(blob2.x, blob2.y, 3, "" + blob1Range); +}; - /*if (blob2.isVirus()) { - radius = blob1.size; - } else if(canSplit(blob1, blob2)) { - radius += splitDistance; - } else { - radius += blob1.size * 2; - }*/ +u.debugAngle = function(angle, text) { + var player = u.getPlayer(); + var line1 = u.followAngle(angle, player[0].x, player[0].y, 300); + u.drawLine(player[0].x, player[0].y, line1[0], line1[1], 5); + u.drawPoint(line1[0], line1[1], 5, "" + text); +}; - var shouldInvert = false; +//TODO: Don't let this function do the radius math. +u.getEdgeLinesFromPoint = function(blob1, blob2, radius) { + var px = blob1.x; + var py = blob1.y; + + var cx = blob2.x; + var cy = blob2.y; + + //var radius = blob2.size; + + /*if (blob2.isVirus()) { + radius = blob1.size; + } else if(canSplit(blob1, blob2)) { + radius += splitDistance; + } else { + radius += blob1.size * 2; + }*/ + + var shouldInvert = false; + + var tempRadius = u.computeDistance(px, py, cx, cy); + if (tempRadius <= radius) { + radius = tempRadius - 5; + shouldInvert = true; + } + + var dx = cx - px; + var dy = cy - py; + var dd = Math.sqrt(dx * dx + dy * dy); + var a = Math.asin(radius / dd); + var b = Math.atan2(dy, dx); + + var t = b - a; + var ta = { + x: radius * Math.sin(t), + y: radius * -Math.cos(t) + }; + + t = b + a; + var tb = { + x: radius * -Math.sin(t), + y: radius * Math.cos(t) + }; + + var angleLeft = u.getAngle(cx + ta.x, cy + ta.y, px, py); + var angleRight = u.getAngle(cx + tb.x, cy + tb.y, px, py); + var angleDistance = (angleRight - angleLeft).mod(360); + + /*if (shouldInvert) { + var temp = angleLeft; + angleLeft = (angleRight + 180).mod(360); + angleRight = (temp + 180).mod(360); + angleDistance = (angleRight - angleLeft).mod(360); + }*/ + + return [angleLeft, angleDistance, [cx + tb.x, cy + tb.y], + [cx + ta.x, cy + ta.y] + ]; +}; - if (computeDistance(px, py, cx, cy) <= radius) { - radius = computeDistance(px, py, cx, cy) - 5; - shouldInvert = true; - } +u.invertAngle = function(range) { + var angle1 = u.rangeToAngle(badAngles[i]); + var angle2 = (badAngles[i][0] - angle1).mod(360); + return [angle1, angle2]; +}; - var dx = cx - px; - var dy = cy - py; - var dd = Math.sqrt(dx * dx + dy * dy); - var a = Math.asin(radius / dd); - var b = Math.atan2(dy, dx); - - var t = b - a - var ta = { - x: radius * Math.sin(t), - y: radius * -Math.cos(t) - }; - - t = b + a - var tb = { - x: radius * -Math.sin(t), - y: radius * Math.cos(t) - }; - - var angleLeft = getAngle(cx + ta.x, cy + ta.y, px, py); - var angleRight = getAngle(cx + tb.x, cy + tb.y, px, py); - var angleDistance = (angleRight - angleLeft).mod(360); - - /*if (shouldInvert) { - var temp = angleLeft; - angleLeft = (angleRight + 180).mod(360); - angleRight = (temp + 180).mod(360); - angleDistance = (angleRight - angleLeft).mod(360); - }*/ +u.addWall = function(listToUse, blob) { + //var mapSizeX = Math.abs(f.getMapStartX - f.getMapEndX); + //var mapSizeY = Math.abs(f.getMapStartY - f.getMapEndY); + //var distanceFromWallX = mapSizeX/3; + //var distanceFromWallY = mapSizeY/3; + var distanceFromWallY = 2000; + var distanceFromWallX = 2000; + if (blob.x < f.getMapStartX() + distanceFromWallX) { + //LEFT + //console.log("Left"); + listToUse.push([ + [90, true], + [270, false], u.computeDistance(getMapStartX(), blob.y, blob.x, blob.y) + ]); + var lineLeft = u.followAngle(90, blob.x, blob.y, 190 + blob.size); + var lineRight = u.followAngle(270, blob.x, blob.y, 190 + blob.size); + u.drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); + u.drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); + u.drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); + } + if (blob.y < getMapStartY() + distanceFromWallY) { + //TOP + //console.log("TOP"); + listToUse.push([ + [180, true], + [0, false], u.computeDistance(blob.x, getMapStartY, blob.x, blob.y) + ]); + var lineLeft = u.followAngle(180, blob.x, blob.y, 190 + blob.size); + var lineRight = u.followAngle(360, blob.x, blob.y, 190 + blob.size); + u.drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); + u.drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); + u.drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); + } + if (blob.x > getMapEndX() - distanceFromWallX) { + //RIGHT + //console.log("RIGHT"); + listToUse.push([ + [270, true], + [90, false], u.computeDistance(getMapEndX(), blob.y, blob.x, blob.y) + ]); + var lineLeft = u.followAngle(270, blob.x, blob.y, 190 + blob.size); + var lineRight = u.followAngle(90, blob.x, blob.y, 190 + blob.size); + u.drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); + u.drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); + u.drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); + } + if (blob.y > getMapEndY() - distanceFromWallY) { + //BOTTOM + //console.log("BOTTOM"); + listToUse.push([ + [0, true], + [180, false], u.computeDistance(blob.x, getMapEndY(), blob.x, blob.y) + ]); + var lineLeft = u.followAngle(0, blob.x, blob.y, 190 + blob.size); + var lineRight = u.followAngle(180, blob.x, blob.y, 190 + blob.size); + u.drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); + u.drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); + u.drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); + } + return listToUse; +}; - return [angleLeft, angleDistance, [cx + tb.x, cy + tb.y], - [cx + ta.x, cy + ta.y] - ]; - } +//listToUse contains angles in the form of [angle, boolean]. +//boolean is true when the range is starting. False when it's ending. +//range = [[angle1, true], [angle2, false]] - function invertAngle(range) { - var angle1 = rangeToAngle(badAngles[i]); - var angle2 = (badAngles[i][0] - angle1).mod(360); - return [angle1, angle2]; +u.getAngleIndex = function(listToUse, angle) { + if (listToUse.length == 0) { + return 0; } - function addWall(listToUse, blob) { - //var mapSizeX = Math.abs(f.getMapStartX - f.getMapEndX); - //var mapSizeY = Math.abs(f.getMapStartY - f.getMapEndY); - //var distanceFromWallX = mapSizeX/3; - //var distanceFromWallY = mapSizeY/3; - var distanceFromWallY = 2000; - var distanceFromWallX = 2000; - if (Math.abs(f.getMapEndY - f.getMapStartY) < distanceFromWallY || Math.abs(f.getMapEndX - f.getMapEndX) < distanceFromWallX){ - //MAPTOOSMALL - //console.log("Map Too Small!"); - listToUse.push([ - [0, true], - [0, false], 0 - ]); - } - /*else if (blob.y < f.getMapStartY() + distanceFromWallY && blob.x < f.getMapStartX() + distanceFromWallX){ - //TOPLEFT - //console.log("Top Left"); - listToUse.push([ - [90, true], - [360, false], computeDistance(getMapStartX(), f.getMapStartY(), blob.x, blob.y) - ]); - var lineLeft = followAngle(90, blob.x, blob.y, 190 + blob.size); - var lineRight = followAngle(360, blob.x, blob.y, 190 + blob.size); - drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); - drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); - } - else if (blob.y < f.getMapStartY() + distanceFromWallY && blob.x > f.getMapEndX() - distanceFromWallX){ - //TOPRIGHT - //console.log("Top Right"); - listToUse.push([ - [180, true], - [90, false], computeDistance(getMapEndX(), f.getMapStartY(), blob.x, blob.y) - ]); - var lineLeft = followAngle(180, blob.x, blob.y, 190 + blob.size); - var lineRight = followAngle(90, blob.x, blob.y, 190 + blob.size); - drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); - drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); + for (var i = 0; i < listToUse.length; i++) { + if (angle <= listToUse[i][0]) { + return i; } - else if (blob.y > f.getMapEndY() - distanceFromWallY && blob.x < f.getMapStartX() + distanceFromWallX){ - //BOTTOMLEFT - //console.log("Bottom Left"); - listToUse.push([ - [0, true], - [270, false], computeDistance(getMapStartX(), f.getMapEndY(), blob.x, blob.y) - ]); - var lineLeft = followAngle(0, blob.x, blob.y, 190 + blob.size); - var lineRight = followAngle(270, blob.x, blob.y, 190 + blob.size); - drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); - drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); - } - else if (blob.y > f.getMapEndY() - distanceFromWallY && blob.x > f.getMapEndX() - distanceFromWallX){ - //BOTTOMRIGHT - //console.log("Bottom Right"); - listToUse.push([ - [270, true], - [180, false], computeDistance(getMapEndX(), f.getMapEndY(), blob.x, blob.y) - ]); - var lineLeft = followAngle(270, blob.x, blob.y, 190 + blob.size); - var lineRight = followAngle(180, blob.x, blob.y, 190 + blob.size); - drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); - drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); - }*/ - if (blob.x < f.getMapStartX() + distanceFromWallX) { - //LEFT - //console.log("Left"); - listToUse.push([ - [89, true], - [271, false], computeDistance(getMapStartX(), blob.y, blob.x, blob.y) - ]); - var lineLeft = followAngle(90, blob.x, blob.y, 190 + blob.size); - var lineRight = followAngle(270, blob.x, blob.y, 190 + blob.size); - drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); - drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); - } - if (blob.y < f.getMapStartY() + distanceFromWallY) { - //TOP - //console.log("TOP"); - listToUse.push([ - [179, true], - [1, false], computeDistance(blob.x, getMapStartY, blob.x, blob.y) - ]); - var lineLeft = followAngle(180, blob.x, blob.y, 190 + blob.size); - var lineRight = followAngle(360, blob.x, blob.y, 190 + blob.size); - drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); - drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); - } - if (blob.x > f.getMapEndX() - distanceFromWallX) { - //RIGHT - //console.log("RIGHT"); - listToUse.push([ - [269, true], - [91, false], computeDistance(getMapEndX(), blob.y, blob.x, blob.y) - ]); - var lineLeft = followAngle(270, blob.x, blob.y, 190 + blob.size); - var lineRight = followAngle(90, blob.x, blob.y, 190 + blob.size); - drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); - drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); - } - if (blob.y > f.getMapEndY() - distanceFromWallY) { - //BOTTOM - //console.log("BOTTOM"); - listToUse.push([ - [359, true], - [181, false], computeDistance(blob.x, getMapEndY, blob.x, blob.y) - ]); - var lineLeft = followAngle(0, blob.x, blob.y, 190 + blob.size); - var lineRight = followAngle(180, blob.x, blob.y, 190 + blob.size); - drawLine(blob.x, blob.y, lineLeft[0], lineLeft[1], 5); - drawLine(blob.x, blob.y, lineRight[0], lineRight[1], 5); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob.x, blob.y, 5); - } - return listToUse; } - //listToUse contains angles in the form of [angle, boolean]. - //boolean is true when the range is starting. False when it's ending. - //range = [[angle1, true], [angle2, false]] - - function getAngleIndex(listToUse, angle) { - if (listToUse.length == 0) { - return 0; - } + return listToUse.length; +}; - for (var i = 0; i < listToUse.length; i++) { - if (angle <= listToUse[i][0]) { - return i; - } - } +u.addAngle = function(listToUse, range) { + //#1 Find first open element + //#2 Try to add range1 to the list. If it is within other range, don't add it, set a boolean. + //#3 Try to add range2 to the list. If it is withing other range, don't add it, set a boolean. - return listToUse.length; - } - - function addAngle(listToUse, range) { - //#1 Find first open element - //#2 Try to add range1 to the list. If it is within other range, don't add it, set a boolean. - //#3 Try to add range2 to the list. If it is withing other range, don't add it, set a boolean. + //TODO: Only add the new range at the end after the right stuff has been removed. - //TODO: Only add the new range at the end after the right stuff has been removed. + var newListToUse = listToUse.slice(); - var newListToUse = listToUse.slice(); + var startIndex = 1; - var startIndex = 1; - - if (newListToUse.length > 0 && !newListToUse[0][1]) { - startIndex = 0; - } + if (newListToUse.length > 0 && !newListToUse[0][1]) { + startIndex = 0; + } - var startMark = getAngleIndex(newListToUse, range[0][0]); - var startBool = startMark.mod(2) != startIndex; + var startMark = u.getAngleIndex(newListToUse, range[0][0]); + var startBool = startMark.mod(2) != startIndex; - var endMark = getAngleIndex(newListToUse, range[1][0]); - var endBool = endMark.mod(2) != startIndex; + var endMark = u.getAngleIndex(newListToUse, range[1][0]); + var endBool = endMark.mod(2) != startIndex; - var removeList = []; + var removeList = []; - if (startMark != endMark) { - //Note: If there is still an error, this would be it. - var biggerList = 0; - if (endMark == newListToUse.length) { - biggerList = 1; - } + if (startMark != endMark) { + //Note: If there is still an error, this would be it. + var biggerList = 0; + if (endMark == newListToUse.length) { + biggerList = 1; + } - for (var i = startMark; i < startMark + (endMark - startMark).mod(newListToUse.length + biggerList); i++) { - removeList.push((i).mod(newListToUse.length)); - } - } else if (startMark < newListToUse.length && endMark < newListToUse.length) { - var startDist = (newListToUse[startMark][0] - range[0][0]).mod(360); - var endDist = (newListToUse[endMark][0] - range[1][0]).mod(360); + for (var i = startMark; i < startMark + (endMark - startMark).mod(newListToUse.length + biggerList); i++) { + removeList.push((i).mod(newListToUse.length)); + } + } else if (startMark < newListToUse.length && endMark < newListToUse.length) { + var startDist = (newListToUse[startMark][0] - range[0][0]).mod(360); + var endDist = (newListToUse[endMark][0] - range[1][0]).mod(360); - if (startDist < endDist) { - for (var i = 0; i < newListToUse.length; i++) { - removeList.push(i); - } + if (startDist < endDist) { + for (var i = 0; i < newListToUse.length; i++) { + removeList.push(i); } } + } - removeList.sort(function(a, b){return b-a}); + removeList.sort(function(a, b){return b-a;}); - for (var i = 0; i < removeList.length; i++) { - newListToUse.splice(removeList[i], 1); - } + for (var i = 0; i < removeList.length; i++) { + newListToUse.splice(removeList[i], 1); + } - if (startBool) { - newListToUse.splice(getAngleIndex(newListToUse, range[0][0]), 0, range[0]); - } - if (endBool) { - newListToUse.splice(getAngleIndex(newListToUse, range[1][0]), 0, range[1]); - } + if (startBool) { + newListToUse.splice(u.getAngleIndex(newListToUse, range[0][0]), 0, range[0]); + } + if (endBool) { + newListToUse.splice(u.getAngleIndex(newListToUse, range[1][0]), 0, range[1]); + } - return newListToUse; + console.log("***********" + range[0] + ", " + range[1]); + for (var i = 0; i < newListToUse.length; i++) { + console.log(newListToUse[i][0] + ", " + newListToUse[i][1]); } - function getAngleRange(blob1, blob2, index, radius) { - var angleStuff = getEdgeLinesFromPoint(blob1, blob2, radius); + return newListToUse; +}; - var leftAngle = angleStuff[0]; - var rightAngle = rangeToAngle(angleStuff); - var difference = angleStuff[1]; +u.getAngleRange = function(blob1, blob2, index, radius) { + var angleStuff = u.getEdgeLinesFromPoint(blob1, blob2, radius); - drawPoint(angleStuff[2][0], angleStuff[2][1], 3, ""); - drawPoint(angleStuff[3][0], angleStuff[3][1], 3, ""); + var leftAngle = angleStuff[0]; + var rightAngle = u.rangeToAngle(angleStuff); + var difference = angleStuff[1]; - //console.log("Adding badAngles: " + leftAngle + ", " + rightAngle + " diff: " + difference); + u.drawPoint(angleStuff[2][0], angleStuff[2][1], 3, ""); + u.drawPoint(angleStuff[3][0], angleStuff[3][1], 3, ""); - var lineLeft = followAngle(leftAngle, blob1.x, blob1.y, 150 + blob1.size - index * 10); - var lineRight = followAngle(rightAngle, blob1.x, blob1.y, 150 + blob1.size - index * 10); + //console.log("Adding badAngles: " + leftAngle + ", " + rightAngle + " diff: " + difference); - if (blob2.isVirus()) { - drawLine(blob1.x, blob1.y, lineLeft[0], lineLeft[1], 6); - drawLine(blob1.x, blob1.y, lineRight[0], lineRight[1], 6); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob1.x, blob1.y, 6); - } else if(getCells().hasOwnProperty(blob2.id)) { - drawLine(blob1.x, blob1.y, lineLeft[0], lineLeft[1], 0); - drawLine(blob1.x, blob1.y, lineRight[0], lineRight[1], 0); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob1.x, blob1.y, 0); - } else { - drawLine(blob1.x, blob1.y, lineLeft[0], lineLeft[1], 3); - drawLine(blob1.x, blob1.y, lineRight[0], lineRight[1], 3); - drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob1.x, blob1.y, 3); - } + var lineLeft = u.followAngle(leftAngle, blob1.x, blob1.y, 150 + blob1.size - index * 10); + var lineRight = u.followAngle(rightAngle, blob1.x, blob1.y, 150 + blob1.size - index * 10); - return [leftAngle, difference]; + if (blob2.isVirus()) { + u.drawLine(blob1.x, blob1.y, lineLeft[0], lineLeft[1], 6); + u.drawLine(blob1.x, blob1.y, lineRight[0], lineRight[1], 6); + u.drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob1.x, blob1.y, 6); + } else if(getCells().hasOwnProperty(blob2.id)) { + u.drawLine(blob1.x, blob1.y, lineLeft[0], lineLeft[1], 0); + u.drawLine(blob1.x, blob1.y, lineRight[0], lineRight[1], 0); + u.drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob1.x, blob1.y, 0); + } else { + u.drawLine(blob1.x, blob1.y, lineLeft[0], lineLeft[1], 3); + u.drawLine(blob1.x, blob1.y, lineRight[0], lineRight[1], 3); + u.drawArc(lineLeft[0], lineLeft[1], lineRight[0], lineRight[1], blob1.x, blob1.y, 3); } - //Given a list of conditions, shift the angle to the closest available spot respecting the range given. - function shiftAngle(listToUse, angle, range) { - //TODO: shiftAngle needs to respect the range! DONE? - for (var i = 0; i < listToUse.length; i++) { - if (angleIsWithin(angle, listToUse[i])) { - //console.log("Shifting needed!"); + return [leftAngle, difference]; +}; - var angle1 = listToUse[i][0]; - var angle2 = rangeToAngle(listToUse[i]); +//Given a list of conditions, shift the angle to the closest available spot respecting the range given. +u.shiftAngle = function(listToUse, angle, range) { + //TODO: shiftAngle needs to respect the range! DONE? + for (var i = 0; i < listToUse.length; i++) { + if (u.angleIsWithin(angle, listToUse[i])) { + //console.log("Shifting needed!"); - var dist1 = (angle - angle1).mod(360); - var dist2 = (angle2 - angle).mod(360); + var angle1 = listToUse[i][0]; + var angle2 = u.rangeToAngle(listToUse[i]); - if (dist1 < dist2) { - if (angleIsWithin(angle1, range)) { - return angle1; - } else { - return angle2; - } + var dist1 = (angle - angle1).mod(360); + var dist2 = (angle2 - angle).mod(360); + + if (dist1 < dist2) { + if (u.angleIsWithin(angle1, range)) { + return angle1; } else { - if (angleIsWithin(angle2, range)) { - return angle2; - } else { - return angle1; - } + return angle2; + } + } else { + if (u.angleIsWithin(angle2, range)) { + return angle2; + } else { + return angle1; } } } - //console.log("No Shifting Was needed!"); - return angle; } + //console.log("No Shifting Was needed!"); + return angle; +}; - /** - * This is the main bot logic. This is called quite often. - * @param followMouse Is a boolean. If set to true, it means the user is asking for the bot to follow the mouse coordinates. - * @return A 2 dimensional array with coordinates for every cells. [[x, y], [x, y]] - */ - function findDestination(followMouse) { - var player = getPlayer(); - var interNodes = getMemoryCells(); - //console.warn("findDestination(followMouse) was called from line " + arguments.callee.caller.toString()); - - if ( /*!toggle*/ 1) { - - //The following code converts the mouse position into an - //absolute game coordinate. - var useMouseX = screenToGameX(getMouseX()); - var useMouseY = screenToGameY(getMouseY()); - tempPoint = [useMouseX, useMouseY, 1]; - - //The current destination that the cells where going towards. - var tempMoveX = getPointX(); - var tempMoveY = getPointY(); - - //This variable will be returned at the end. - //It will contain the destination choices for all the cells. - //BTW!!! ERROR ERROR ABORT MISSION!!!!!!! READ BELOW ----------- - // - //SINCE IT'S STUPID NOW TO ASK EACH CELL WHERE THEY WANT TO GO, - //THE BOT SHOULD SIMPLY PICK ONE AND THAT'S IT, I MEAN WTF.... - var destinationChoices = []; //destination, size, danger - - //Just to make sure the player is alive. - if (player.length > 0) { - - //Loop through all the player's cells. - for (var k = 0; k < player.length; k++) { - if (true) { - drawPoint(player[k].x, player[k].y + player[k].size, 0, "" + (getLastUpdate() - player[k].birth) + " / " + (30000 + (player[k].birthMass * 57) - (getLastUpdate() - player[k].birth)) + " / " + player[k].birthMass); - } +/** + * This is the main bot logic. This is called quite often. + * @param followMouse Is a boolean. If set to true, it means the user is asking for the bot to follow the mouse coordinates. + * @return A 2 dimensional array with coordinates for every cells. [[x, y], [x, y]] + */ +u.findDestination = function(followMouse) { + var player = getPlayer(); + var interNodes = getMemoryCells(); + //console.warn("findDestination(followMouse) was called from line " + arguments.callee.caller.toString()); + + if ( /*!toggle*/ 1) { + + //The following code converts the mouse position into an + //absolute game coordinate. + var useMouseX = u.screenToGameX(getMouseX()); + var useMouseY = u.screenToGameY(getMouseY()); + tempPoint = [useMouseX, useMouseY, 1]; + + //The current destination that the cells were going towards. + var tempMoveX = getPointX(); + var tempMoveY = getPointY(); + + //This variable will be returned at the end. + //It will contain the destination choices for all the cells. + //BTW!!! ERROR ERROR ABORT MISSION!!!!!!! READ BELOW ----------- + // + //SINCE IT'S STUPID NOW TO ASK EACH CELL WHERE THEY WANT TO GO, + //THE BOT SHOULD SIMPLY PICK ONE AND THAT'S IT, I MEAN WTF.... + var destinationChoices = []; //destination, size, danger + + //Just to make sure the player is alive. + if (player.length > 0) { + + //Loop through all the player's cells. + for (var k = 0; k < player.length; k++) { + if (true) { + u.drawPoint(player[k].x, player[k].y + player[k].size, 0, "" + (getLastUpdate() - player[k].birth) + " / " + (30000 + (player[k].birthMass * 57) - (getLastUpdate() - player[k].birth)) + " / " + player[k].birthMass); } + } - //Loops only for one cell for now. - for (var k = 0; /*k < player.length*/ k < 1; k++) { + //Loops only for one cell for now. + for (var k = 0; /*k < player.length*/ k < 1; k++) { - //console.log("Working on blob: " + k); + //console.log("Working on blob: " + k); - drawCircle(player[k].x, player[k].y, player[k].size + splitDistance, 5); - //drawPoint(player[0].x, player[0].y - player[0].size, 3, "" + Math.floor(player[0].x) + ", " + Math.floor(player[0].y)); + u.drawCircle(player[k].x, player[k].y, player[k].size + u.splitDistance, 5); + //drawPoint(player[0].x, player[0].y - player[0].size, 3, "" + Math.floor(player[0].x) + ", " + Math.floor(player[0].y)); - //var allDots = processEverything(interNodes); + //var allDots = processEverything(interNodes); - //loop through everything that is on the screen and - //separate everything in it's own category. - var allIsAll = getAll(player[k]); + //loop through everything that is on the screen and + //separate everything in it's own category. + var allIsAll = u.getAll(player[k]); - //The food stored in element 0 of allIsAll - var allPossibleFood = allIsAll[0]; - //The threats are stored in element 1 of allIsAll - var allPossibleThreats = allIsAll[1]; - //The viruses are stored in element 2 of allIsAll - var allPossibleViruses = allIsAll[2]; + //The food stored in element 0 of allIsAll + var allPossibleFood = allIsAll[0]; + //The threats are stored in element 1 of allIsAll + var allPossibleThreats = allIsAll[1]; + //The viruses are stored in element 2 of allIsAll + var allPossibleViruses = allIsAll[2]; - //The bot works by removing angles in which it is too - //dangerous to travel towards to. - var badAngles = []; - var obstacleList = []; + //The bot works by removing angles in which it is too + //dangerous to travel towards to. + var badAngles = []; + var obstacleList = []; - var isSafeSpot = true; - var isMouseSafe = true; + var isSafeSpot = true; + var isMouseSafe = true; - var clusterAllFood = clusterFood(allPossibleFood, player[k].size); + var clusterAllFood = u.clusterFood(allPossibleFood, player[k].size); - //console.log("Looking for enemies!"); + //console.log("Looking for enemies!"); - //Loop through all the cells that were identified as threats. - for (var i = 0; i < allPossibleThreats.length; i++) { + //Loop through all the cells that were identified as threats. + for (var i = 0; i < allPossibleThreats.length; i++) { - var enemyDistance = computeDistanceFromCircleEdge(allPossibleThreats[i].x, allPossibleThreats[i].y, player[k].x, player[k].y, allPossibleThreats[i].size); + var enemyDistance = u.computeDistanceFromCircleEdge(allPossibleThreats[i].x, allPossibleThreats[i].y, player[k].x, player[k].y, allPossibleThreats[i].size); - allPossibleThreats[i].enemyDist = enemyDistance; - } + allPossibleThreats[i].enemyDist = enemyDistance; + } - /*allPossibleThreats.sort(function(a, b){ - return a.enemyDist-b.enemyDist; - })*/ + /*allPossibleThreats.sort(function(a, b){ + return a.enemyDist-b.enemyDist; + })*/ - for (var i = 0; i < allPossibleThreats.length; i++) { + for (var i = 0; i < allPossibleThreats.length; i++) { - var enemyDistance = computeDistance(allPossibleThreats[i].x, allPossibleThreats[i].y, player[k].x, player[k].y); + var enemyDistance = u.computeDistance(allPossibleThreats[i].x, allPossibleThreats[i].y, player[k].x, player[k].y); - var splitDangerDistance = allPossibleThreats[i].size + splitDistance + 150; + var splitDangerDistance = allPossibleThreats[i].size + u.splitDistance + 150; - var normalDangerDistance = allPossibleThreats[i].size + 150; + var normalDangerDistance = allPossibleThreats[i].size + 150; - var shiftDistance = player[k].size; + var shiftDistance = player[k].size; - //console.log("Found distance."); + //console.log("Found distance."); - var enemyCanSplit = canSplit(player[k], allPossibleThreats[i]); - - for (var j = clusterAllFood.length - 1; j >= 0 ; j--) { - var secureDistance = (enemyCanSplit ? splitDangerDistance : normalDangerDistance); - if (computeDistance(allPossibleThreats[i].x, allPossibleThreats[i].y, clusterAllFood[j][0], clusterAllFood[j][1]) < secureDistance) - clusterAllFood.splice(j, 1); - } + var enemyCanSplit = u.canSplit(player[k], allPossibleThreats[i]); + + for (var j = clusterAllFood.length - 1; j >= 0 ; j--) { + var secureDistance = (enemyCanSplit ? splitDangerDistance : normalDangerDistance); + if (u.computeDistance(allPossibleThreats[i].x, allPossibleThreats[i].y, clusterAllFood[j][0], clusterAllFood[j][1]) < secureDistance) + clusterAllFood.splice(j, 1); + } - //console.log("Removed some food."); + //console.log("Removed some food."); - if (enemyCanSplit) { - drawCircle(allPossibleThreats[i].x, allPossibleThreats[i].y, splitDangerDistance, 0); - drawCircle(allPossibleThreats[i].x, allPossibleThreats[i].y, splitDangerDistance + shiftDistance, 6); - } else { - drawCircle(allPossibleThreats[i].x, allPossibleThreats[i].y, normalDangerDistance, 3); - drawCircle(allPossibleThreats[i].x, allPossibleThreats[i].y, normalDangerDistance + shiftDistance, 6); - } + if (enemyCanSplit) { + u.drawCircle(allPossibleThreats[i].x, allPossibleThreats[i].y, splitDangerDistance, 0); + u.drawCircle(allPossibleThreats[i].x, allPossibleThreats[i].y, splitDangerDistance + shiftDistance, 6); + } else { + u.drawCircle(allPossibleThreats[i].x, allPossibleThreats[i].y, normalDangerDistance, 3); + u.drawCircle(allPossibleThreats[i].x, allPossibleThreats[i].y, normalDangerDistance + shiftDistance, 6); + } - if (allPossibleThreats[i].danger && f.getLastUpdate() - allPossibleThreats[i].dangerTimeOut > 1000) { + if (allPossibleThreats[i].danger && getLastUpdate() - allPossibleThreats[i].dangerTimeOut > 1000) { - allPossibleThreats[i].danger = false; - } + allPossibleThreats[i].danger = false; + } - /*if ((enemyCanSplit && enemyDistance < splitDangerDistance) || - (!enemyCanSplit && enemyDistance < normalDangerDistance)) { + /*if ((enemyCanSplit && enemyDistance < splitDangerDistance) || + (!enemyCanSplit && enemyDistance < normalDangerDistance)) { - allPossibleThreats[i].danger = true; - allPossibleThreats[i].dangerTimeOut = f.getLastUpdate(); - }*/ + allPossibleThreats[i].danger = true; + allPossibleThreats[i].dangerTimeOut = f.getLastUpdate(); + }*/ - //console.log("Figured out who was important."); + //console.log("Figured out who was important."); - if ((enemyCanSplit && enemyDistance < splitDangerDistance) || (enemyCanSplit && allPossibleThreats[i].danger)) { + if ((enemyCanSplit && enemyDistance < splitDangerDistance) || (enemyCanSplit && allPossibleThreats[i].danger)) { - badAngles.push(getAngleRange(player[k], allPossibleThreats[i], i, splitDangerDistance).concat(allPossibleThreats[i].enemyDist)); + badAngles.push(u.getAngleRange(player[k], allPossibleThreats[i], i, splitDangerDistance).concat(allPossibleThreats[i].enemyDist)); - } else if ((!enemyCanSplit && enemyDistance < normalDangerDistance) || (!enemyCanSplit && allPossibleThreats[i].danger)) { + } else if ((!enemyCanSplit && enemyDistance < normalDangerDistance) || (!enemyCanSplit && allPossibleThreats[i].danger)) { - badAngles.push(getAngleRange(player[k], allPossibleThreats[i], i, normalDangerDistance).concat(allPossibleThreats[i].enemyDist)); + badAngles.push(u.getAngleRange(player[k], allPossibleThreats[i], i, normalDangerDistance).concat(allPossibleThreats[i].enemyDist)); - } else if (enemyCanSplit && enemyDistance < splitDangerDistance + shiftDistance) { - var tempOb = getAngleRange(player[k], allPossibleThreats[i], i, splitDangerDistance + shiftDistance); - var angle1 = tempOb[0]; - var angle2 = rangeToAngle(tempOb); + } else if (enemyCanSplit && enemyDistance < splitDangerDistance + shiftDistance) { + var tempOb = u.getAngleRange(player[k], allPossibleThreats[i], i, splitDangerDistance + shiftDistance); + var angle1 = tempOb[0]; + var angle2 = u.rangeToAngle(tempOb); - obstacleList.push([[angle1, true], [angle2, false]]); - } else if (!enemyCanSplit && enemyDistance < normalDangerDistance + shiftDistance) { - var tempOb = getAngleRange(player[k], allPossibleThreats[i], i, normalDangerDistance + shiftDistance); - var angle1 = tempOb[0]; - var angle2 = rangeToAngle(tempOb); + obstacleList.push([[angle1, true], [angle2, false]]); + } else if (!enemyCanSplit && enemyDistance < normalDangerDistance + shiftDistance) { + var tempOb = u.getAngleRange(player[k], allPossibleThreats[i], i, normalDangerDistance + shiftDistance); + var angle1 = tempOb[0]; + var angle2 = u.rangeToAngle(tempOb); - obstacleList.push([[angle1, true], [angle2, false]]); - } - //console.log("Done with enemy: " + i); + obstacleList.push([[angle1, true], [angle2, false]]); } + //console.log("Done with enemy: " + i); + } - //console.log("Done looking for enemies!"); + //console.log("Done looking for enemies!"); - var goodAngles = []; - var stupidList = []; + var goodAngles = []; + var stupidList = []; - for (var i = 0; i < allPossibleViruses.length; i++) { - if (player[k].size < allPossibleViruses[i].size) { - drawCircle(allPossibleViruses[i].x, allPossibleViruses[i].y, allPossibleViruses[i].size + 10, 3); - drawCircle(allPossibleViruses[i].x, allPossibleViruses[i].y, allPossibleViruses[i].size * 2, 6); + for (var i = 0; i < allPossibleViruses.length; i++) { + if (player[k].size < allPossibleViruses[i].size) { + u.drawCircle(allPossibleViruses[i].x, allPossibleViruses[i].y, allPossibleViruses[i].size + 10, 3); + u.drawCircle(allPossibleViruses[i].x, allPossibleViruses[i].y, allPossibleViruses[i].size * 2, 6); - } else { - drawCircle(allPossibleViruses[i].x, allPossibleViruses[i].y, player[k].size + 50, 3); - drawCircle(allPossibleViruses[i].x, allPossibleViruses[i].y, player[k].size * 2, 6); - } + } else { + u.drawCircle(allPossibleViruses[i].x, allPossibleViruses[i].y, player[k].size + 50, 3); + u.drawCircle(allPossibleViruses[i].x, allPossibleViruses[i].y, player[k].size * 2, 6); } + } - for (var i = 0; i < allPossibleViruses.length; i++) { - var virusDistance = computeDistance(allPossibleViruses[i].x, allPossibleViruses[i].y, player[k].x, player[k].y); - if (player[k].size < allPossibleViruses[i].size) { - if (virusDistance < (allPossibleViruses[i].size * 2)) { - var tempOb = getAngleRange(player[k], allPossibleViruses[i], i, allPossibleViruses[i].size + 10); - var angle1 = tempOb[0]; - var angle2 = rangeToAngle(tempOb); - obstacleList.push([[angle1, true], [angle2, false]]); - } - } else { - if (virusDistance < (player[k].size * 2)) { - var tempOb = getAngleRange(player[k], allPossibleViruses[i], i, player[k].size + 50); - var angle1 = tempOb[0]; - var angle2 = rangeToAngle(tempOb); - obstacleList.push([[angle1, true], [angle2, false]]); - } + for (var i = 0; i < allPossibleViruses.length; i++) { + var virusDistance = u.computeDistance(allPossibleViruses[i].x, allPossibleViruses[i].y, player[k].x, player[k].y); + if (player[k].size < allPossibleViruses[i].size) { + if (virusDistance < (allPossibleViruses[i].size * 2)) { + var tempOb = u.getAngleRange(player[k], allPossibleViruses[i], i, allPossibleViruses[i].size + 10); + var angle1 = tempOb[0]; + var angle2 = u.rangeToAngle(tempOb); + obstacleList.push([[angle1, true], [angle2, false]]); + } + } else { + if (virusDistance < (player[k].size * 2)) { + var tempOb = u.getAngleRange(player[k], allPossibleViruses[i], i, player[k].size + 50); + var angle1 = tempOb[0]; + var angle2 = u.rangeToAngle(tempOb); + obstacleList.push([[angle1, true], [angle2, false]]); } } + } - if (badAngles.length > 0) { - //NOTE: This is only bandaid wall code. It's not the best way to do it. - stupidList = addWall(stupidList, player[k]); - } + if (badAngles.length > 0) { + //NOTE: This is only bandaid wall code. It's not the best way to do it. + stupidList = u.addWall(stupidList, player[k]); + } - for (var i = 0; i < badAngles.length; i++) { - var angle1 = badAngles[i][0]; - var angle2 = rangeToAngle(badAngles[i]); - stupidList.push([[angle1, true], [angle2, false], badAngles[i][2]]); - //console.log("\tSome value: " + badAngles[i][2] + " but: " + badAngles[i][0]); - } + for (var i = 0; i < stupidList.length; i++) { + console.log("AddWallDone: " + stupidList[i][0] + ", " + stupidList[i][1]); + } - //stupidList.push([[45, true], [135, false]]); - //stupidList.push([[10, true], [200, false]]); + for (var i = 0; i < badAngles.length; i++) { + var angle1 = badAngles[i][0]; + var angle2 = u.rangeToAngle(badAngles[i]); + stupidList.push([[angle1, true], [angle2, false], badAngles[i][2]]); + } - stupidList.sort(function(a, b){ - //console.log("Distance: " + a[2] + ", " + b[2]); - return a[2]-b[2]; - }) + //stupidList.push([[45, true], [135, false]]); + //stupidList.push([[10, true], [200, false]]); - //console.log("Added random noob stuff."); + for (var i = 0; i < stupidList.length; i++) { + console.log("HelloWorld: " + stupidList[i][0] + ", " + stupidList[i][1]); + } - var sortedInterList = []; - var sortedObList = []; + stupidList.sort(function(a, b){ + //console.log("Distance: " + a[2] + ", " + b[2]); + return a[2]-b[2]; + }); - for (var i = 0; i < stupidList.length; i++) { - //console.log("Adding to sorted: " + stupidList[i][0][0] + ", " + stupidList[i][1][0]); - var tempList = addAngle(sortedInterList, stupidList[i]); + for (var i = 0; i < stupidList.length; i++) { + console.log("zzzzzzzzzzz: " + stupidList[i][0] + ", " + stupidList[i][1]); + } - if (tempList.length == 0) { - console.log("MAYDAY IT'S HAPPENING!"); - break; - } else { - sortedInterList = tempList; - } - } + //console.log("Added random noob stuff."); - for (var i = 0; i < obstacleList.length; i++) { - sortedObList = addAngle(sortedObList, obstacleList[i]) + var sortedInterList = []; + var sortedObList = []; - if (sortedObList.length == 0) { - break; - } + for (var i = 0; i < stupidList.length; i++) { + //console.log("Adding to sorted: " + stupidList[i][0][0] + ", " + stupidList[i][1][0]); + var tempList = u.addAngle(sortedInterList, stupidList[i]); + + if (tempList.length == 0) { + console.log("MAYDAY IT'S HAPPENING!"); + break; + } else { + sortedInterList = tempList; } + } - var offsetI = 0; - var obOffsetI = 1; + console.log("_________"); + for (var i = 0; i < sortedInterList.length; i++) { + console.log(sortedInterList[i][0] + ", " + sortedInterList[i][1]); + } + console.log("_________"); - if (sortedInterList.length > 0 && sortedInterList[0][1]) { - offsetI = 1; - } - if (sortedObList.length > 0 && sortedObList[0][1]) { - obOffsetI = 0; + for (var i = 0; i < obstacleList.length; i++) { + sortedObList = u.addAngle(sortedObList, obstacleList[i]); + + if (sortedObList.length == 0) { + break; } + } - var goodAngles = []; - var obstacleAngles = []; + var offsetI = 0; + var obOffsetI = 1; - for (var i = 0; i < sortedInterList.length; i += 2) { - var angle1 = sortedInterList[(i + offsetI).mod(sortedInterList.length)][0]; - var angle2 = sortedInterList[(i + 1 + offsetI).mod(sortedInterList.length)][0]; - var diff = (angle2 - angle1).mod(360); - goodAngles.push([angle1, diff]); - } + if (sortedInterList.length > 0 && sortedInterList[0][1]) { + offsetI = 1; + } + if (sortedObList.length > 0 && sortedObList[0][1]) { + obOffsetI = 0; + } - for (var i = 0; i < sortedObList.length; i += 2) { - var angle1 = sortedObList[(i + obOffsetI).mod(sortedObList.length)][0]; - var angle2 = sortedObList[(i + 1 + obOffsetI).mod(sortedObList.length)][0]; - var diff = (angle2 - angle1).mod(360); - obstacleAngles.push([angle1, diff]); - } + var goodAngles = []; + var obstacleAngles = []; - for (var i = 0; i < goodAngles.length; i++) { - var line1 = followAngle(goodAngles[i][0], player[k].x, player[k].y, 100 + player[k].size); - var line2 = followAngle((goodAngles[i][0] + goodAngles[i][1]).mod(360), player[k].x, player[k].y, 100 + player[k].size); - drawLine(player[k].x, player[k].y, line1[0], line1[1], 1); - drawLine(player[k].x, player[k].y, line2[0], line2[1], 1); + for (var i = 0; i < sortedInterList.length; i += 2) { + var angle1 = sortedInterList[(i + offsetI).mod(sortedInterList.length)][0]; + var angle2 = sortedInterList[(i + 1 + offsetI).mod(sortedInterList.length)][0]; + var diff = (angle2 - angle1).mod(360); + goodAngles.push([angle1, diff]); + } + + for (var i = 0; i < sortedObList.length; i += 2) { + var angle1 = sortedObList[(i + obOffsetI).mod(sortedObList.length)][0]; + var angle2 = sortedObList[(i + 1 + obOffsetI).mod(sortedObList.length)][0]; + var diff = (angle2 - angle1).mod(360); + obstacleAngles.push([angle1, diff]); + } - drawArc(line1[0], line1[1], line2[0], line2[1], player[k].x, player[k].y, 1); + for (var i = 0; i < goodAngles.length; i++) { + var line1 = u.followAngle(goodAngles[i][0], player[k].x, player[k].y, 100 + player[k].size); + var line2 = u.followAngle((goodAngles[i][0] + goodAngles[i][1]).mod(360), player[k].x, player[k].y, 100 + player[k].size); + u.drawLine(player[k].x, player[k].y, line1[0], line1[1], 1); + u.drawLine(player[k].x, player[k].y, line2[0], line2[1], 1); - //drawPoint(player[0].x, player[0].y, 2, ""); + u.drawArc(line1[0], line1[1], line2[0], line2[1], player[k].x, player[k].y, 1); - drawPoint(line1[0], line1[1], 0, "" + i + ": 0"); - drawPoint(line2[0], line2[1], 0, "" + i + ": 1"); - } + //drawPoint(player[0].x, player[0].y, 2, ""); - for (var i = 0; i < obstacleAngles.length; i++) { - var line1 = followAngle(obstacleAngles[i][0], player[k].x, player[k].y, 50 + player[k].size); - var line2 = followAngle((obstacleAngles[i][0] + obstacleAngles[i][1]).mod(360), player[k].x, player[k].y, 50 + player[k].size); - drawLine(player[k].x, player[k].y, line1[0], line1[1], 6); - drawLine(player[k].x, player[k].y, line2[0], line2[1], 6); + u.drawPoint(line1[0], line1[1], 0, "" + i + ": 0"); + u.drawPoint(line2[0], line2[1], 0, "" + i + ": 1"); + } - drawArc(line1[0], line1[1], line2[0], line2[1], player[k].x, player[k].y, 6); + for (var i = 0; i < obstacleAngles.length; i++) { + var line1 = u.followAngle(obstacleAngles[i][0], player[k].x, player[k].y, 50 + player[k].size); + var line2 = u.followAngle((obstacleAngles[i][0] + obstacleAngles[i][1]).mod(360), player[k].x, player[k].y, 50 + player[k].size); + u.drawLine(player[k].x, player[k].y, line1[0], line1[1], 6); + u.drawLine(player[k].x, player[k].y, line2[0], line2[1], 6); - //drawPoint(player[0].x, player[0].y, 2, ""); + u.drawArc(line1[0], line1[1], line2[0], line2[1], player[k].x, player[k].y, 6); - drawPoint(line1[0], line1[1], 0, "" + i + ": 0"); - drawPoint(line2[0], line2[1], 0, "" + i + ": 1"); - } + //drawPoint(player[0].x, player[0].y, 2, ""); - if (followMouse && goodAngles.length == 0) { - //This is the follow the mouse mode - var distance = computeDistance(player[k].x, player[k].y, tempPoint[0], tempPoint[1]); + u.drawPoint(line1[0], line1[1], 0, "" + i + ": 0"); + u.drawPoint(line2[0], line2[1], 0, "" + i + ": 1"); + } - var shiftedAngle = shiftAngle(obstacleAngles, getAngle(tempPoint[0], tempPoint[1], player[k].x, player[k].y), [0, 360]); + if (followMouse && goodAngles.length == 0) { + //This is the follow the mouse mode + var distance = u.computeDistance(player[k].x, player[k].y, tempPoint[0], tempPoint[1]); - var destination = followAngle(shiftedAngle, player[k].x, player[k].y, distance); + var shiftedAngle = u.shiftAngle(obstacleAngles, getAngle(tempPoint[0], tempPoint[1], player[k].x, player[k].y), [0, 360]); - destinationChoices = destination; - drawLine(player[k].x, player[k].y, destination[0], destination[1], 1); - //tempMoveX = destination[0]; - //tempMoveY = destination[1]; + var destination = u.followAngle(shiftedAngle, player[k].x, player[k].y, distance); - } else if (goodAngles.length > 0) { - var bIndex = goodAngles[0]; - var biggest = goodAngles[0][1]; - for (var i = 1; i < goodAngles.length; i++) { - var size = goodAngles[i][1]; - if (size > biggest) { - biggest = size; - bIndex = goodAngles[i]; - } - } - var perfectAngle = (bIndex[0] + bIndex[1] / 2).mod(360); - - perfectAngle = shiftAngle(obstacleAngles, perfectAngle, bIndex); - - var line1 = followAngle(perfectAngle, player[k].x, player[k].y, f.verticalDistance()); - - destinationChoices = line1; - drawLine(player[k].x, player[k].y, line1[0], line1[1], 7); - //tempMoveX = line1[0]; - //tempMoveY = line1[1]; - } else if (badAngles.length > 0 && goodAngles == 0) { - //When there are enemies around but no good angles - //You're likely screwed. (This should never happen.) - - console.log("Failed"); - destinationChoices = [tempMoveX, tempMoveY]; - /*var angleWeights = [] //Put weights on the angles according to enemy distance - for (var i = 0; i < allPossibleThreats.length; i++){ - var dist = computeDistance(player[k].x, player[k].y, allPossibleThreats[i].x, allPossibleThreats[i].y); - var angle = getAngle(allPossibleThreats[i].x, allPossibleThreats[i].y, player[k].x, player[k].y); - angleWeights.push([angle,dist]); + destinationChoices = destination; + u.drawLine(player[k].x, player[k].y, destination[0], destination[1], 1); + //tempMoveX = destination[0]; + //tempMoveY = destination[1]; + + } else if (goodAngles.length > 0) { + var bIndex = goodAngles[0]; + var biggest = goodAngles[0][1]; + for (var i = 1; i < goodAngles.length; i++) { + var size = goodAngles[i][1]; + if (size > biggest) { + biggest = size; + bIndex = goodAngles[i]; } - var maxDist = 0; - var finalAngle = 0; - for (var i = 0; i < angleWeights.length; i++){ - if (angleWeights[i][1] > maxDist){ - maxDist = angleWeights[i][1]; - finalAngle = (angleWeights[i][0] + 180).mod(360); - } + } + var perfectAngle = (bIndex[0] + bIndex[1] / 2).mod(360); + + perfectAngle = u.shiftAngle(obstacleAngles, perfectAngle, bIndex); + + var line1 = u.followAngle(perfectAngle, player[k].x, player[k].y, verticalDistance()); + + destinationChoices = line1; + u.drawLine(player[k].x, player[k].y, line1[0], line1[1], 7); + //tempMoveX = line1[0]; + //tempMoveY = line1[1]; + } else if (badAngles.length > 0 && goodAngles == 0) { + //When there are enemies around but no good angles + //You're likely screwed. (This should never happen.) + + console.log("Failed"); + destinationChoices = [tempMoveX, tempMoveY]; + /*var angleWeights = [] //Put weights on the angles according to enemy distance + for (var i = 0; i < allPossibleThreats.length; i++){ + var dist = computeDistance(player[k].x, player[k].y, allPossibleThreats[i].x, allPossibleThreats[i].y); + var angle = getAngle(allPossibleThreats[i].x, allPossibleThreats[i].y, player[k].x, player[k].y); + angleWeights.push([angle,dist]); + } + var maxDist = 0; + var finalAngle = 0; + for (var i = 0; i < angleWeights.length; i++){ + if (angleWeights[i][1] > maxDist){ + maxDist = angleWeights[i][1]; + finalAngle = (angleWeights[i][0] + 180).mod(360); } - var line1 = followAngle(finalAngle,player[k].x,player[k].y,f.verticalDistance()); - drawLine(player[k].x, player[k].y, line1[0], line1[1], 2); - destinationChoices.push(line1);*/ - } else if (clusterAllFood.length > 0) { - for (var i = 0; i < clusterAllFood.length; i++) { - //console.log("mefore: " + clusterAllFood[i][2]); - //This is the cost function. Higher is better. + } + var line1 = followAngle(finalAngle,player[k].x,player[k].y,f.verticalDistance()); + drawLine(player[k].x, player[k].y, line1[0], line1[1], 2); + destinationChoices.push(line1);*/ + } else if (clusterAllFood.length > 0) { + for (var i = 0; i < clusterAllFood.length; i++) { + //console.log("mefore: " + clusterAllFood[i][2]); + //This is the cost function. Higher is better. - var clusterAngle = getAngle(clusterAllFood[i][0], clusterAllFood[i][1], player[k].x, player[k].y); + var clusterAngle = u.getAngle(clusterAllFood[i][0], clusterAllFood[i][1], player[k].x, player[k].y); - clusterAllFood[i][2] = clusterAllFood[i][2] * 6 - computeDistance(clusterAllFood[i][0], clusterAllFood[i][1], player[k].x, player[k].y); - //console.log("Current Value: " + clusterAllFood[i][2]); + clusterAllFood[i][2] = clusterAllFood[i][2] * 6 - u.computeDistance(clusterAllFood[i][0], clusterAllFood[i][1], player[k].x, player[k].y); + //console.log("Current Value: " + clusterAllFood[i][2]); - //(goodAngles[bIndex][1] / 2 - (Math.abs(perfectAngle - clusterAngle))); + //(goodAngles[bIndex][1] / 2 - (Math.abs(perfectAngle - clusterAngle))); - clusterAllFood[i][3] = clusterAngle; + clusterAllFood[i][3] = clusterAngle; - drawPoint(clusterAllFood[i][0], clusterAllFood[i][1], 1, ""); - //console.log("After: " + clusterAllFood[i][2]); - } + u.drawPoint(clusterAllFood[i][0], clusterAllFood[i][1], 1, ""); + //console.log("After: " + clusterAllFood[i][2]); + } - var bestFoodI = 0; - var bestFood = clusterAllFood[0][2]; - for (var i = 1; i < clusterAllFood.length; i++) { - if (bestFood < clusterAllFood[i][2]) { - bestFood = clusterAllFood[i][2]; - bestFoodI = i; - } + var bestFoodI = 0; + var bestFood = clusterAllFood[0][2]; + for (var i = 1; i < clusterAllFood.length; i++) { + if (bestFood < clusterAllFood[i][2]) { + bestFood = clusterAllFood[i][2]; + bestFoodI = i; } + } - //console.log("Best Value: " + clusterAllFood[bestFoodI][2]); + //console.log("Best Value: " + clusterAllFood[bestFoodI][2]); - var distance = computeDistance(player[k].x, player[k].y, clusterAllFood[bestFoodI][0], clusterAllFood[bestFoodI][1]); + var distance = u.computeDistance(player[k].x, player[k].y, clusterAllFood[bestFoodI][0], clusterAllFood[bestFoodI][1]); - var shiftedAngle = shiftAngle(obstacleAngles, getAngle(clusterAllFood[bestFoodI][0], clusterAllFood[bestFoodI][1], player[k].x, player[k].y), [0, 360]); + var shiftedAngle = u.shiftAngle(obstacleAngles, u.getAngle(clusterAllFood[bestFoodI][0], clusterAllFood[bestFoodI][1], player[k].x, player[k].y), [0, 360]); - var destination = followAngle(shiftedAngle, player[k].x, player[k].y, distance); + var destination = u.followAngle(shiftedAngle, player[k].x, player[k].y, distance); - destinationChoices = destination; - //tempMoveX = destination[0]; - //tempMoveY = destination[1]; - drawLine(player[k].x, player[k].y, destination[0], destination[1], 1); - } else { - //If there are no enemies around and no food to eat. - destinationChoices = [tempMoveX, tempMoveY]; - } + destinationChoices = destination; + //tempMoveX = destination[0]; + //tempMoveY = destination[1]; + u.drawLine(player[k].x, player[k].y, destination[0], destination[1], 1); + } else { + //If there are no enemies around and no food to eat. + destinationChoices = [tempMoveX, tempMoveY]; + } + + u.drawPoint(tempPoint[0], tempPoint[1], tempPoint[2], ""); + //drawPoint(tempPoint[0], tempPoint[1], tempPoint[2], "" + Math.floor(computeDistance(tempPoint[0], tempPoint[1], I, J))); + //drawLine(tempPoint[0], tempPoint[1], player[0].x, player[0].y, 6); + //console.log("Slope: " + slope(tempPoint[0], tempPoint[1], player[0].x, player[0].y) + " Angle: " + getAngle(tempPoint[0], tempPoint[1], player[0].x, player[0].y) + " Side: " + (getAngle(tempPoint[0], tempPoint[1], player[0].x, player[0].y) - 90).mod(360)); + tempPoint[2] = 1; - drawPoint(tempPoint[0], tempPoint[1], tempPoint[2], ""); - //drawPoint(tempPoint[0], tempPoint[1], tempPoint[2], "" + Math.floor(computeDistance(tempPoint[0], tempPoint[1], I, J))); - //drawLine(tempPoint[0], tempPoint[1], player[0].x, player[0].y, 6); - //console.log("Slope: " + slope(tempPoint[0], tempPoint[1], player[0].x, player[0].y) + " Angle: " + getAngle(tempPoint[0], tempPoint[1], player[0].x, player[0].y) + " Side: " + (getAngle(tempPoint[0], tempPoint[1], player[0].x, player[0].y) - 90).mod(360)); - tempPoint[2] = 1; + //console.log("Done working on blob: " + i); + } - //console.log("Done working on blob: " + i); + //TODO: Find where to go based on destinationChoices. + /*var dangerFound = false; + for (var i = 0; i < destinationChoices.length; i++) { + if (destinationChoices[i][2]) { + dangerFound = true; + break; } + } - //TODO: Find where to go based on destinationChoices. - /*var dangerFound = false; + destinationChoices.sort(function(a, b){return b[1] - a[1]}); + + if (dangerFound) { for (var i = 0; i < destinationChoices.length; i++) { if (destinationChoices[i][2]) { - dangerFound = true; + tempMoveX = destinationChoices[i][0][0]; + tempMoveY = destinationChoices[i][0][1]; break; } } + } else { + tempMoveX = destinationChoices.peek()[0][0]; + tempMoveY = destinationChoices.peek()[0][1]; + //console.log("Done " + tempMoveX + ", " + tempMoveY); + }*/ + } + //console.log("MOVING RIGHT NOW!"); - destinationChoices.sort(function(a, b){return b[1] - a[1]}); + //console.log("______Never lied ever in my life."); - if (dangerFound) { - for (var i = 0; i < destinationChoices.length; i++) { - if (destinationChoices[i][2]) { - tempMoveX = destinationChoices[i][0][0]; - tempMoveY = destinationChoices[i][0][1]; - break; - } - } - } else { - tempMoveX = destinationChoices.peek()[0][0]; - tempMoveY = destinationChoices.peek()[0][1]; - //console.log("Done " + tempMoveX + ", " + tempMoveY); - }*/ - } - //console.log("MOVING RIGHT NOW!"); + return destinationChoices; + } +}; - //console.log("______Never lied ever in my life."); +/** + * A conversion from the screen's horizontal coordinate system + * to the game's horizontal coordinate system. + * @param x in the screen's coordinate system + * @return x in the game's coordinate system + */ +u.screenToGameX = function(x) { + return (x - getWidth() / 2) / getRatio() + getX(); +}; - return destinationChoices; - } - } +/** + * A conversion from the screen's vertical coordinate system + * to the game's vertical coordinate system. + * @param y in the screen's coordinate system + * @return y in the game's coordinate system + */ +u.screenToGameY = function(y) { + return (y - getHeight() / 2) / getRatio() + getY(); +}; - /** - * A conversion from the screen's horizontal coordinate system - * to the game's horizontal coordinate system. - * @param x in the screen's coordinate system - * @return x in the game's coordinate system - */ - function screenToGameX(x) { - return (x - getWidth() / 2) / getRatio() + getX(); - } +u.drawPoint = function(x_1, y_1, drawColor, text) { + f.drawPoint(x_1, y_1, drawColor, text); +}; - /** - * A conversion from the screen's vertical coordinate system - * to the game's vertical coordinate system. - * @param y in the screen's coordinate system - * @return y in the game's coordinate system - */ - function screenToGameY(y) { - return (y - getHeight() / 2) / getRatio() + getY(); - } +u.drawArc = function(x_1, y_1, x_2, y_2, x_3, y_3, drawColor) { + f.drawArc(x_1, y_1, x_2, y_2, x_3, y_3, drawColor); +}; - function drawPoint(x_1, y_1, drawColor, text) { - f.drawPoint(x_1, y_1, drawColor, text); - } +u.drawLine = function(x_1, y_1, x_2, y_2, drawColor) { + f.drawLine(x_1, y_1, x_2, y_2, drawColor); +}; - function drawArc(x_1, y_1, x_2, y_2, x_3, y_3, drawColor) { - f.drawArc(x_1, y_1, x_2, y_2, x_3, y_3, drawColor); - } +u.drawCircle = function(x_1, y_1, radius, drawColor) { + f.drawCircle(x_1, y_1, radius, drawColor); +}; - function drawLine(x_1, y_1, x_2, y_2, drawColor) { - f.drawLine(x_1, y_1, x_2, y_2, drawColor); - } +/** + * Some horse shit of some sort. + * @return Horse Shit + */ +u.screenDistance = function() { + var temp = f.getScreenDistance(); + return temp; +}; - function drawCircle(x_1, y_1, radius, drawColor) { - f.drawCircle(x_1, y_1, radius, drawColor); - } +/** + * Tells you if the game is in Dark mode. + * @return Boolean for dark mode. + */ +u.getDarkBool = function() { + return f.getDarkBool(); +}; - /** - * Some horse shit of some sort. - * @return Horse Shit - */ - function screenDistance() { - var temp = f.getScreenDistance(); - return temp; - } +/** + * Tells you if the mass is shown. + * @return Boolean for player's mass. + */ +u.getMassBool = function() { + return f.getMassBool(); +}; - /** - * Tells you if the game is in Dark mode. - * @return Boolean for dark mode. - */ - function getDarkBool() { - return f.getDarkBool(); - } +/** + * This is a copy of everything that is shown on screen. + * Normally stuff will time out when off the screen, this + * memorizes everything that leaves the screen for a little + * while longer. + * @return The memory object. + */ +u.getMemoryCells = function() { + return f.getMemoryCells(); +}; - /** - * Tells you if the mass is shown. - * @return Boolean for player's mass. - */ - function getMassBool() { - return f.getMassBool(); - } +/** + * [getCellsArray description] + * @return {[type]} [description] + */ +u.getCellsArray = function() { + return f.getCellsArray(); +}; - /** - * This is a copy of everything that is shown on screen. - * Normally stuff will time out when off the screen, this - * memorizes everything that leaves the screen for a little - * while longer. - * @return The memory object. - */ - function getMemoryCells() { - return f.getMemoryCells(); - } +/** + * This is the original "getMemoryCells" without the memory part. + * @return Non memorized object. + */ +u.getCells = function() { + return f.getCells(); +}; - /** - * [getCellsArray description] - * @return {[type]} [description] - */ - function getCellsArray() { - return f.getCellsArray(); - } +/** + * Returns an array with all the player's cells. + * @return Player's cells + */ +u.getPlayer = function () { + return f.getPlayer(); +}; - /** - * This is the original "getMemoryCells" without the memory part. - * @return Non memorized object. - */ - function getCells() { - return f.getCells(); - } +/** + * The canvas' width. + * @return Integer Width + */ +u.getWidth = function() { + return f.getWidth(); +}; - /** - * Returns an array with all the player's cells. - * @return Player's cells - */ - function getPlayer() { - return f.getPlayer(); - } +/** + * The canvas' height + * @return Integer Height + */ +u.getHeight = function() { + return f.getHeight(); +}; - /** - * The canvas' width. - * @return Integer Width - */ - function getWidth() { - return f.getWidth(); - } +/** + * Scaling ratio of the canvas. The bigger this ration, + * the further that you see. + * @return Screen scaling ratio. + */ +u.getRatio = function() { + return f.getRatio(); +}; - /** - * The canvas' height - * @return Integer Height - */ - function getHeight() { - return f.getHeight(); - } +/** + * [getOffsetX description] + * @return {[type]} [description] + */ +u.getOffsetX = function() { + return f.getOffsetX(); +}; - /** - * Scaling ratio of the canvas. The bigger this ration, - * the further that you see. - * @return Screen scaling ratio. - */ - function getRatio() { - return f.getRatio(); - } +u.getOffsetY = function() { + return f.getOffsetY(); +}; - /** - * [getOffsetX description] - * @return {[type]} [description] - */ - function getOffsetX() { - return f.getOffsetX(); - } +u.getX = function() { + return f.getX(); +}; - function getOffsetY() { - return f.getOffsetY(); - } +u.getY = function() { + return f.getY(); +}; - function getX() { - return f.getX(); - } +u.getPointX = function() { + return f.getPointX(); +}; - function getY() { - return f.getY(); - } +u.getPointY = function() { + return f.getPointY(); +}; - function getPointX() { - return f.getPointX(); - } +/** + * The X location of the mouse. + * @return Integer X + */ +u.getMouseX = function() { + return f.getMouseX(); +}; - function getPointY() { - return f.getPointY(); - } +/** + * The Y location of the mouse. + * @return Integer Y + */ +u.getMouseY = function() { + return f.getMouseY(); +}; - /** - * The X location of the mouse. - * @return Integer X - */ - function getMouseX() { - return f.getMouseX(); - } +/** + * A timestamp since the last time the server sent any data. + * @return Last update timestamp + */ +u.getUpdate = function() { + return f.getLastUpdate(); +}; - /** - * The Y location of the mouse. - * @return Integer Y - */ - function getMouseY() { - return f.getMouseY(); - } +/** + * The game's current mode. (":ffa", ":experimental", ":teams". ":party") + * @return {[type]} [description] + */ +u.getMode = function() { + return f.getMode(); +}; - /** - * A timestamp since the last time the server sent any data. - * @return Last update timestamp - */ - function getUpdate() { - return f.getLastUpdate(); - } +f.botList.push(["AposBot " + aposBotVersion, u.findDestination]); - /** - * The game's current mode. (":ffa", ":experimental", ":teams". ":party") - * @return {[type]} [description] - */ - function getMode() { - return f.getMode(); - } -})(window, jQuery); +u.bList = g('#bList'); +g('