Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Event gwiazdka 2019 nithal/podgrodzie/nizina/gosciniec
  3. // @version      0.03
  4. // @description  Szuka i tak lepiej niż noobki :)
  5. // @author       Thinker
  6. // @match        http://gefion.margonem.pl/
  7. // @grant        none
  8. // ==/UserScript==
  9.  
  10. $('#tutorial').hide();
  11. ceb = {};
  12. ceb.bestxy = 9999;
  13. ceb.run = 1;
  14. ceb.id = 0;
  15. ceb.block = "";
  16. ceb.xxxx = 0;
  17. ceb.blocked = 1;
  18. ceb.check2 = 0;
  19. ceb.check2 = 0;
  20. ceb.interv1 = "";
  21. ceb.interv2 = "";
  22. ceb.interv3 = "";
  23.  
  24. var mapki = [];
  25. var nazwamapki;
  26.  
  27.  
  28. class AStar {
  29.     constructor(
  30.       collisionsString,
  31.       width,
  32.       height,
  33.       start,
  34.       end,
  35.       additionalCollisions
  36.     ) {
  37.       this.width = width;
  38.       this.height = height;
  39.       this.collisions = this.parseCollisions(collisionsString, width, height);
  40.       this.additionalCollisions = additionalCollisions || {};
  41.       this.start = this.collisions[start.x][start.y];
  42.       this.end = this.collisions[end.x][end.y];
  43.       this.start.beginning = true;
  44.       this.start.g = 0;
  45.       this.start.f = heuristic(this.start, this.end);
  46.       this.end.target = true;
  47.       this.end.g = 0;
  48.       this.addNeighbours();
  49.       this.openSet = [];
  50.       this.closedSet = [];
  51.       this.openSet.push(this.start);
  52.     }
  53.  
  54.     parseCollisions(collisionsString, width, height) {
  55.       const collisions = new Array(width);
  56.       for (let w = 0; w < width; w++) {
  57.         collisions[w] = new Array(height);
  58.         for (let h = 0; h < height; h++) {
  59.           collisions[w][h] = new Point(
  60.             w,
  61.             h,
  62.             collisionsString.charAt(w + h * width) === "1"
  63.           );
  64.         }
  65.       }
  66.       return collisions;
  67.     }
  68.  
  69.     addNeighbours() {
  70.       for (let i = 0; i < this.width; i++) {
  71.         for (let j = 0; j < this.height; j++) {
  72.           this.addPointNeighbours(this.collisions[i][j]);
  73.         }
  74.       }
  75.     }
  76.  
  77.     addPointNeighbours(point) {
  78.       const x = point.x,
  79.         y = point.y;
  80.       const neighbours = [];
  81.       if (x > 0) neighbours.push(this.collisions[x - 1][y]);
  82.       if (y > 0) neighbours.push(this.collisions[x][y - 1]);
  83.       if (x < this.width - 1) neighbours.push(this.collisions[x + 1][y]);
  84.       if (y < this.height - 1) neighbours.push(this.collisions[x][y + 1]);
  85.       point.neighbours = neighbours;
  86.     }
  87.  
  88.     anotherFindPath() {
  89.       while (this.openSet.length > 0) {
  90.         let currentIndex = this.getLowestF();
  91.         let current = this.openSet[currentIndex];
  92.         if (current === this.end) return this.reconstructPath();
  93.         else {
  94.           this.openSet.splice(currentIndex, 1);
  95.           this.closedSet.push(current);
  96.           for (const neighbour of current.neighbours) {
  97.             if (this.closedSet.includes(neighbour)) continue;
  98.             else {
  99.               const tentative_score = current.g + 1;
  100.               let isBetter = false;
  101.               if (
  102.                 this.end == this.collisions[neighbour.x][neighbour.y] ||
  103.                 (!this.openSet.includes(neighbour) &&
  104.                   !neighbour.collision &&
  105.                   !this.additionalCollisions[neighbour.x + 256 * neighbour.y])
  106.               ) {
  107.                 this.openSet.push(neighbour);
  108.                 neighbour.h = heuristic(neighbour, this.end);
  109.                 isBetter = true;
  110.               } else if (
  111.                 tentative_score < neighbour.g &&
  112.                 !neighbour.collision
  113.               ) {
  114.                 isBetter = true;
  115.               }
  116.               if (isBetter) {
  117.                 neighbour.previous = current;
  118.                 neighbour.g = tentative_score;
  119.                 neighbour.f = neighbour.g + neighbour.h;
  120.               }
  121.             }
  122.           }
  123.         }
  124.       }
  125.     }
  126.  
  127.     getLowestF() {
  128.       let lowestFIndex = 0;
  129.       for (let i = 0; i < this.openSet.length; i++) {
  130.         if (this.openSet[i].f < this.openSet[lowestFIndex].f) lowestFIndex = i;
  131.       }
  132.       return lowestFIndex;
  133.     }
  134.  
  135.     reconstructPath() {
  136.       const path = [];
  137.       let currentNode = this.end;
  138.       while (currentNode !== this.start) {
  139.         path.push(currentNode);
  140.         currentNode = currentNode.previous;
  141.       }
  142.       return path;
  143.     }
  144.   }
  145.  
  146.   class Point {
  147.     constructor(x, y, collision) {
  148.       this.x = x;
  149.       this.y = y;
  150.       this.collision = collision;
  151.       this.g = 10000000;
  152.       this.f = 10000000;
  153.       this.neighbours = [];
  154.       this.beginning = false;
  155.       this.target = false;
  156.       this.previous = undefined;
  157.     }
  158.   }
  159.  
  160.   function heuristic(p1, p2) {
  161.     return Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y);
  162.   }
  163.  
  164. function a_getWay(x, y) {
  165.     return new AStar(
  166.       map.col,
  167.       map.x,
  168.       map.y, {
  169.         x: hero.x,
  170.         y: hero.y
  171.       }, {
  172.         x: x,
  173.         y: y
  174.       },
  175.       g.npccol
  176.     ).anotherFindPath();
  177.   }
  178.  
  179. function a_goTo(x, y) {
  180.     let _road_ = a_getWay(x, y);
  181.     if (!Array.isArray(_road_)) return;
  182.     window.road = _road_;
  183. }
  184.  
  185. var nithal = [[44, 8], [44, 7]];
  186. var podgrodzie = [[0, 6], [26, 63]];
  187. var nizina = [[28, 0], [95, 6]];
  188. var gosciniec = [[27, 94], [27, 95]];
  189.  
  190. var koordy;
  191. var numer = 0;
  192. var nazwa_mapka;
  193.  
  194. ceb.f1 = function() {
  195.     nazwa_mapka = map.name;
  196.     hero.nextx = '';
  197.     hero.nexty = '';
  198.     hero.nextx = koordy[numer][0];
  199.     hero.nexty = koordy[numer][1];
  200.     ceb.run = 1;
  201.     ceb.blocked = 0;
  202.     a_goTo(hero.nextx, hero.nexty);
  203.     if((Math.abs(hero.rx - koordy[numer][0]) == 0 && Math.abs(hero.ry - koordy[numer][1]) == 0)) {
  204.         numer++;
  205.     }
  206. };
  207.  
  208. ceb.f2 = function() {
  209.         switch(map.name) {
  210.             case "Nithal":
  211.                 koordy = nithal;
  212.                 if(nazwa_mapka == "Podgrodzie Nithal") {
  213.                     numer = 0;
  214.                 }
  215.                 break;
  216.             case "Podgrodzie Nithal":
  217.                 koordy = podgrodzie;
  218.                 if(nazwa_mapka == "Nithal") {
  219.                     numer = 0;
  220.                 } else if (nazwa_mapka == "Nizina Wieśniaków") {
  221.                     numer = 1;
  222.                 }
  223.                 break;
  224.             case "Nizina Wieśniaków":
  225.                 koordy = nizina;
  226.                 if(nazwa_mapka == "Podgrodzie Nithal") {
  227.                     numer = 0;
  228.                 } else if (nazwa_mapka == "Gościniec Bardów") {
  229.                     numer = 1;
  230.                 }
  231.                 break;
  232.             case "Gościniec Bardów":
  233.                 koordy = gosciniec;
  234.                 if(nazwa_mapka == "Nizina Wieśniaków") {
  235.                     numer = 0;
  236.                 }
  237.                 break;
  238.         }
  239. };
  240.  
  241. ceb.f4 = function() {
  242.     for (var i in g.npc) {
  243.         var npc = g.npc[i];
  244.         if (!isset(npc.del) && ( (npc.wt > 79 && npc.wt < 100)) ) {
  245.             clearInterval(ceb.interv1);
  246.             clearInterval(ceb.interv2);
  247.             clearInterval(ceb.interv3);
  248.             a_goTo(npc.x, npc.y);
  249.             let audio = new Audio("https://www.myinstants.com/media/sounds/mlg-airhorn.mp3");
  250.             audio.play();
  251.             clearInterval(ceb.interv4);
  252.         }
  253.  
  254.     }
  255. }
  256.  
  257.  
  258. nazwa_mapka = map.name;
  259. ceb.interv1 = setInterval(ceb.f1, 600);
  260. ceb.interv2 = setInterval(ceb.f2, 5);
  261. ceb.interv4 =  setInterval(ceb.f4, 700);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement