Advertisement
PabloGCV

The Proxymator

Jan 23rd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*ALGORITHM: The Proxymator
  2.  
  3.     The bot's strategy is to select the best approach,
  4.     making educated guesses about his enemies and acting to changes in the grid
  5.     since we have access to other player's data, we can select strategies according to their info
  6.     The bot will either attempt to get to the coin which is nearest to him, or kill the player who is nearest to him
  7.     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
  8.     it can fight the bot and win, or needs to retreat
  9.     if a coin is closer than a bot, it will attempt to grab the coin
  10.  
  11.  
  12. */
  13.  
  14.  
  15. /*
  16.  * Helper function to calculate distances
  17.  * given two locations in the grid, the function calculates the distance between them
  18.  * according to Taxicab Geometry
  19. */
  20. function distance(x1,y1,x2,y2){
  21.     return Math.abs(x1-x2) + Math.abs(y1-y2);
  22. }
  23.  
  24. /*
  25.  * Function to determine who's closest to me
  26.  * I return the position in the array that has the nearest bot
  27. */
  28. function nearestEnemy(othersData,x,y){
  29.     let min = Infinity;
  30.     let ret = 0;
  31.     for(let n = 0; n < othersData.length; n++){
  32.         let x2 = othersData[n][1];
  33.         let y2 = othersData[n][2]; //obtain the position of a bot
  34.         let d = distance(x,y,x2,y2); //calculate distance between me and bot
  35.         if (d < min){ //update minimum if required
  36.             min = d;
  37.             ret = n;
  38.         }
  39.     }
  40.     return ret;
  41. }
  42.  
  43. /*
  44.  * Function to determine the closest coin to me
  45.  * I return the position in the array that has the closest coin
  46. */
  47. function nearestCoin(coinData,x,y){
  48.     let min = Infinity;
  49.     let ret = 0;
  50.     for(let n = 0; n < coinData.length;  n++){
  51.         let x2 = coinData[n][1];
  52.         let y2 = coinData[n][2]; //obtain the position of a coin
  53.         let d = distance(x,y,x2,y2); //calculate distance between me and coin
  54.         if (d < min){ //update minimum if required
  55.             min = d;
  56.             ret = n;
  57.         }
  58.     }
  59.     return ret;
  60. }
  61.  
  62.  
  63. /*
  64.  * Function to determine if it's safe to engage upon somebody
  65. */
  66. function engage(selfData,othersData,num){
  67.     return othersData[num][0] < selfData.coins;
  68. }
  69.  
  70. /*
  71.  * Knowing what to do, the bot will plan a route
  72.  * towards its objective
  73.  * for simplicity purposes, the bot will move according to Taxicab Geometry
  74.  * the idea is to make exceptions to the rule, if there are enemies nearby who can be killed
  75. */
  76. function makePath(x1,y1,x2,y2,bound){
  77.     let ret = "";
  78.     if(x1 != x2){ //first, I move alongside the X axis
  79.         if(x1 > x2 && x1 > 0){ //my objective is to the left of me
  80.             ret = "west";
  81.         }
  82.         else{
  83.             if(x1 < x2 && x1 < (bound - 1)){ //My objective is to the right of me
  84.                 ret = "east";
  85.             }
  86.         }
  87.     }
  88.     else{ //Having aligned myself in the X axis, now I move in the Y axis
  89.         if(y1 > y2 && y1 < (bound + 1)){
  90.             ret = "south"; //if my objective is below me, I move south
  91.         }
  92.         else{
  93.             if(y1 > 0)
  94.                 ret = "north"; //If all else fails, my objective is above me
  95.         }
  96.     }
  97.     return ret;
  98. }
  99.  
  100.  
  101. function step (selfData,othersData,coinData){
  102.  
  103.     var X = selfData.locationX;
  104.     var Y = selfData.locationY;
  105.     var mov = false;
  106.     //Obtain distances to the nearest coin and enemy bot
  107.     let o1 = nearestEnemy(othersData,X,Y);
  108.     let o2 = nearestCoin(coinData,X,Y);
  109.     let d1 = distance(X,Y,othersData[o1][1],othersData[o1][2]);
  110.     let d2 = distance(X,Y,coinData[o2][0],coinData[o2][1]);
  111.     if(d1 >= d2){ //The bot is nearer to me than the coin
  112.         if(engage(selfData,othersData,o1)){ //IT IS SAFE TO ENGAGE, attempt to kill
  113.             if(o1 == 1) //enemy is at a step from me
  114.                 return makePath(X,Y,othersData[o1][1],othersData[o1][2],selfData.arenaLength);
  115.             else{
  116.                 return makePath(X,Y,coinData[o2][0],coinData[o2][1],selfData.arenaLength);//if not, go for coin
  117.             }
  118.         }
  119.         else{//It's NOT safe to engage, retreat is mandatory
  120.             if(X > othersData[o1][1] && Y > othersData[o1][2]){ //enemy is behind me
  121.                 if(Y  < (selfData.arenaLength - 1))
  122.                     return "south";
  123.                 else{
  124.                     mov = true; //I CAN'T MOVE TO THAT SIDE, look for alternatives
  125.                 }
  126.             }
  127.             else{
  128.                 if(X < othersData[o1][1] && Y > othersData[o1][2] || mov){ //enemy is the the right of me
  129.                     if(X - 1 >=  0)
  130.                         return "west";
  131.                 }
  132.                 else{
  133.                     if(X > othersData[o1][1] && Y < othersData[o1][2] || mov){ //enemy is in front of me
  134.                         if(Y - 1 >= 0)
  135.                             return "north";
  136.                     }
  137.                     else{ //enemy is to the left of me
  138.                         if(X < (selfData.arenaLength - 1))
  139.                             return "east";
  140.                         else{
  141.                             return "none"; //pray for safety
  142.                         }
  143.                     }
  144.                 }
  145.  
  146.             }
  147.         }
  148.     }
  149.     else{//in any other case, a coin is nearer to me, Go grab it
  150.         return makePath(X,Y,coinData[o2][0],coinData[o2][1],selfData.arenaLength);
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement