Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. // ==UserScript==
  2. // @name 279
  3. // @version 0.1
  4. // @description spk dochodzara
  5. // @author You
  6. // @match http://tarhuna.margonem.pl
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. (() => {
  11. const maps = ["Tuzmer", "Stare Sioło", "Sucha Dolina", "Płaskowyż Arpan", "Oaza Siedmiu Wichrów", "Ruiny Pustynnych Burz", "Pustynne Katakumby", "Pustynne Katakumby - sala 2", "Komnaty Bezdusznych - sala 1", "Komnaty Bezdusznych - sala 2", "Katakumby Gwałtownej Śmierci", "Wschodni Tunel Jaźni", "Katakumby Krwawych Wypraw", "Katakumby Antycznego Gniewu - przedsionek"];
  12.  
  13. const getCoords = name => {
  14. const [x, y] = window.g.gwIds[getMapId(name)].split(".");
  15. return [x, y];
  16. }
  17.  
  18. const getMapId = name2 => {
  19. for (let [id, name] of Object.entries(window.g.townname)) {
  20. if (name2 === name) return id;
  21. }
  22. }
  23.  
  24. const nextMap = () => {
  25. if (window.map.name) {
  26. const mapka = maps.indexOf(window.map.name);
  27. if (maps[mapka + 1]) {
  28. const [x, y] = getCoords(maps[mapka + 1]);
  29. if (Number(x) === window.hero.x && Number(y) === window.hero.y) {
  30. window._g("walk");
  31. } else {
  32. goTo(x, y);
  33. }
  34. setTimeout(nextMap, 2000);
  35. }
  36. } else setTimeout(nextMap, 100);
  37. }
  38.  
  39. window.g.loadQueue.push({
  40. fun() {
  41. nextMap();
  42. }
  43. })
  44. })()
  45. class AStar {
  46. constructor(collisionsString, width, height, start, end, additionalCollisions) {
  47. this.width = width;
  48. this.height = height;
  49. this.collisions = this.parseCollisions(collisionsString, width, height);
  50. this.additionalCollisions = additionalCollisions || {};
  51. this.start = this.collisions[start.x][start.y];
  52. this.end = this.collisions[end.x][end.y];
  53. this.start.beginning = true;
  54. this.start.g = 0;
  55. this.start.f = heuristic(this.start, this.end);
  56. this.end.target = true;
  57. this.end.g = 0;
  58. this.addNeighbours();
  59. this.openSet = [];
  60. this.closedSet = [];
  61. this.openSet.push(this.start);
  62. }
  63.  
  64. parseCollisions(collisionsString, width, height) {
  65. const collisions = new Array(width);
  66. for (let w = 0; w < width; w++) {
  67. collisions[w] = new Array(height);
  68. for (let h = 0; h < height; h++) {
  69. collisions[w][h] = new Point(w, h, collisionsString.charAt(w + h * width) === '1');
  70. }
  71. }
  72. return collisions;
  73. }
  74.  
  75. addNeighbours() {
  76. for (let i = 0; i < this.width; i++) {
  77. for (let j = 0; j < this.height; j++) {
  78. this.addPointNeighbours(this.collisions[i][j])
  79. }
  80. }
  81. }
  82.  
  83. addPointNeighbours(point) {
  84. const x = point.x,
  85. y = point.y;
  86. const neighbours = [];
  87. if (x > 0) neighbours.push(this.collisions[x - 1][y]);
  88. if (y > 0) neighbours.push(this.collisions[x][y - 1]);
  89. if (x < this.width - 1) neighbours.push(this.collisions[x + 1][y]);
  90. if (y < this.height - 1) neighbours.push(this.collisions[x][y + 1]);
  91. point.neighbours = neighbours;
  92. }
  93.  
  94. anotherFindPath() {
  95. while (this.openSet.length > 0) {
  96. let currentIndex = this.getLowestF();
  97. let current = this.openSet[currentIndex];
  98. if (current === this.end) return this.reconstructPath();
  99. else {
  100. this.openSet.splice(currentIndex, 1);
  101. this.closedSet.push(current);
  102. for (const neighbour of current.neighbours) {
  103. if (this.closedSet.includes(neighbour)) continue;
  104. else {
  105. const tentative_score = current.g + 1;
  106. let isBetter = false;
  107. if (this.end == this.collisions[neighbour.x][neighbour.y] || (!this.openSet.includes(neighbour) && !neighbour.collision && !this.additionalCollisions[neighbour.x + 256 * neighbour.y])) {
  108. this.openSet.push(neighbour);
  109. neighbour.h = heuristic(neighbour, this.end);
  110. isBetter = true;
  111. } else if (tentative_score < neighbour.g && !neighbour.collision) {
  112. isBetter = true;
  113. }
  114. if (isBetter) {
  115. neighbour.previous = current;
  116. neighbour.g = tentative_score;
  117. neighbour.f = neighbour.g + neighbour.h;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124.  
  125. getLowestF() {
  126. let lowestFIndex = 0;
  127. for (let i = 0; i < this.openSet.length; i++) {
  128. if (this.openSet[i].f < this.openSet[lowestFIndex].f) lowestFIndex = i;
  129. }
  130. return lowestFIndex;
  131. }
  132.  
  133. reconstructPath() {
  134. const path = [];
  135. let currentNode = this.end;
  136. while (currentNode !== this.start) {
  137. path.push(currentNode);
  138. currentNode = currentNode.previous;
  139. }
  140. return path;
  141. }
  142. }
  143. class Point {
  144. constructor(x, y, collision) {
  145. this.x = x;
  146. this.y = y;
  147. this.collision = collision;
  148. this.g = 10000000;
  149. this.f = 10000000;
  150. this.neighbours = [];
  151. this.beginning = false;
  152. this.target = false;
  153. this.previous = undefined;
  154. }
  155. }
  156. const heuristic = (p1, p2) => {
  157. return Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y);
  158. }
  159. const getWay = (x, y) => {
  160. return (new AStar(window.map.col, window.map.x, window.map.y, {
  161. x: window.hero.x,
  162. y: window.hero.y
  163. }, {
  164. x: x,
  165. y: y
  166. }, window.g.npccol)).anotherFindPath();
  167. }
  168. const debug = false;
  169. const goTo = (x, y) => {
  170. let _road_ = getWay(x, y);
  171. if (debug) console.log(_road_);
  172. if (!Array.isArray(_road_)) return;
  173. window.road = _road_;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement