Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*ALGORITHM: The Proxymator
- The bot's strategy is to select the best approach,
- making educated guesses about his enemies and acting to changes in the grid
- since we have access to other player's data, we can select strategies according to their info
- The bot will either attempt to get to the coin which is nearest to him, or kill the player who is nearest to him
- the bot scans the field for the nearest object to him and attempts to get to it, if it's a bot, it determines if
- it can fight the bot and win, or needs to retreat
- if a coin is closer than a bot, it will attempt to grab the coin
- */
- /*
- * Helper function to calculate distances
- * given two locations in the grid, the function calculates the distance between them
- * according to Taxicab Geometry
- */
- function distance(x1,y1,x2,y2){
- return Math.abs(x1-x2) + Math.abs(y1-y2);
- }
- /*
- * Function to determine who's closest to me
- * I return the position in the array that has the nearest bot
- */
- function nearestEnemy(othersData,x,y){
- let min = Infinity;
- let ret = 0;
- for(let n = 0; n < othersData.length; n++){
- let x2 = othersData[n][1];
- let y2 = othersData[n][2]; //obtain the position of a bot
- let d = distance(x,y,x2,y2); //calculate distance between me and bot
- if (d < min){ //update minimum if required
- min = d;
- ret = n;
- }
- }
- return ret;
- }
- /*
- * Function to determine the closest coin to me
- * I return the position in the array that has the closest coin
- */
- function nearestCoin(coinData,x,y){
- let min = Infinity;
- let ret = 0;
- for(let n = 0; n < coinData.length; n++){
- let x2 = coinData[n][1];
- let y2 = coinData[n][2]; //obtain the position of a coin
- let d = distance(x,y,x2,y2); //calculate distance between me and coin
- if (d < min){ //update minimum if required
- min = d;
- ret = n;
- }
- }
- return ret;
- }
- /*
- * Function to determine if it's safe to engage upon somebody
- */
- function engage(selfData,othersData,num){
- return othersData[num][0] < selfData.coins;
- }
- /*
- * Knowing what to do, the bot will plan a route
- * towards its objective
- * for simplicity purposes, the bot will move according to Taxicab Geometry
- * the idea is to make exceptions to the rule, if there are enemies nearby who can be killed
- */
- function makePath(x1,y1,x2,y2,bound){
- let ret = "";
- if(x1 != x2){ //first, I move alongside the X axis
- if(x1 > x2 && x1 > 0){ //my objective is to the left of me
- ret = "west";
- }
- else{
- if(x1 < x2 && x1 < (bound - 1)){ //My objective is to the right of me
- ret = "east";
- }
- }
- }
- else{ //Having aligned myself in the X axis, now I move in the Y axis
- if(y1 > y2 && y1 < (bound + 1)){
- ret = "south"; //if my objective is below me, I move south
- }
- else{
- if(y1 > 0)
- ret = "north"; //If all else fails, my objective is above me
- }
- }
- return ret;
- }
- function step (selfData,othersData,coinData){
- var X = selfData.locationX;
- var Y = selfData.locationY;
- var mov = false;
- //Obtain distances to the nearest coin and enemy bot
- let o1 = nearestEnemy(othersData,X,Y);
- let o2 = nearestCoin(coinData,X,Y);
- let d1 = distance(X,Y,othersData[o1][1],othersData[o1][2]);
- let d2 = distance(X,Y,coinData[o2][0],coinData[o2][1]);
- if(d1 >= d2){ //The bot is nearer to me than the coin
- if(engage(selfData,othersData,o1)){ //IT IS SAFE TO ENGAGE, attempt to kill
- if(o1 == 1) //enemy is at a step from me
- return makePath(X,Y,othersData[o1][1],othersData[o1][2],selfData.arenaLength);
- else{
- return makePath(X,Y,coinData[o2][0],coinData[o2][1],selfData.arenaLength);//if not, go for coin
- }
- }
- else{//It's NOT safe to engage, retreat is mandatory
- if(X > othersData[o1][1] && Y > othersData[o1][2]){ //enemy is behind me
- if(Y < (selfData.arenaLength - 1))
- return "south";
- else{
- mov = true; //I CAN'T MOVE TO THAT SIDE, look for alternatives
- }
- }
- else{
- if(X < othersData[o1][1] && Y > othersData[o1][2] || mov){ //enemy is the the right of me
- if(X - 1 >= 0)
- return "west";
- }
- else{
- if(X > othersData[o1][1] && Y < othersData[o1][2] || mov){ //enemy is in front of me
- if(Y - 1 >= 0)
- return "north";
- }
- else{ //enemy is to the left of me
- if(X < (selfData.arenaLength - 1))
- return "east";
- else{
- return "none"; //pray for safety
- }
- }
- }
- }
- }
- }
- else{//in any other case, a coin is nearer to me, Go grab it
- return makePath(X,Y,coinData[o2][0],coinData[o2][1],selfData.arenaLength);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement