DFProfiler Path Finder

Find the fastest path in DFProfiler

当前为 2023-11-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name DFProfiler Path Finder
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Find the fastest path in DFProfiler
  6. // @author Runonstof
  7. // @match https://*.dfprofiler.com/bossmap
  8. // @match https://*.dfprofiler.com/profile/view/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=dfprofiler.com
  10. // @grant unsafeWindow
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // === Utility functions ===
  18.  
  19. function GM_addStyle(css) {
  20. var style = document.getElementById("GM_addStyleBy8626") || (function() {
  21. var style = document.createElement('style');
  22. style.type = 'text/css';
  23. style.id = "GM_addStyleBy8626";
  24. document.head.appendChild(style);
  25. return style;
  26. })();
  27. var sheet = style.sheet;
  28. console.log(sheet);
  29. sheet.insertRule(css, (sheet.rules || sheet.cssRules || []).length);
  30. }
  31.  
  32. function GM_addStyle_object(selector, styles) {
  33. var css = selector + "{";
  34. for (var key in styles) {
  35. css += key + ":" + styles[key] + ";";
  36. }
  37. css += "}";
  38. GM_addStyle(css);
  39. }
  40.  
  41.  
  42. function ready(fn) {
  43. if (document.readyState != 'loading'){
  44. fn();
  45. } else {
  46. document.addEventListener('DOMContentLoaded', fn);
  47. }
  48. }
  49.  
  50. function AStar(emptyCells) {
  51. this.emptyCells == emptyCells || [];
  52.  
  53. this.Node = function Node(x, y) {
  54. this.x = parseInt(x);
  55. this.y = parseInt(y);
  56. this.g = 0; // cost from start node
  57. this.h = 0; // heuristic (estimated cost to target)
  58. this.f = 0; // total cost (g + h)
  59. this.parent = null;
  60. }
  61.  
  62. this.isInsideMap = function(x, y) {
  63. return x >= 1000 && x <= 1058 && y >= 981 && y <= 1019;
  64. };
  65.  
  66. this.isCellEmpty = function(x, y) {
  67. return emptyCells.some(function(cell) {
  68. return cell.x == x && cell.y == y;
  69. });
  70. };
  71.  
  72. this.heuristic = function(node, target) {
  73. // Manhattan distance heuristic
  74. return Math.abs(node.x - target.x) + Math.abs(node.y - target.y);
  75. };
  76.  
  77. this.find = function(startPos, endPos) {
  78. var openList = [];
  79. var closedList = [];
  80.  
  81. var startNode = new this.Node(startPos.x, startPos.y);
  82. var endNode = new this.Node(endPos.x, endPos.y);
  83.  
  84. openList.push(startNode);
  85.  
  86. while (openList.length > 0) {
  87. var currentNode = openList[0];
  88. var currentIndex = 0;
  89.  
  90. for (var i = 1; i < openList.length; i++) {
  91. if (openList[i].f < currentNode.f) {
  92. currentNode = openList[i];
  93. currentIndex = i;
  94. }
  95. }
  96.  
  97. openList.splice(currentIndex, 1);
  98. closedList.push(currentNode);
  99.  
  100. if (currentNode.x === endNode.x && currentNode.y === endNode.y) {
  101. var path = [];
  102. var current = currentNode;
  103. while (current !== null) {
  104. path.push({ x: current.x, y: current.y });
  105. current = current.parent;
  106. }
  107. return path.reverse();
  108. }
  109.  
  110. var neighbors = [
  111. { x: 0, y: 1 },
  112. { x: 1, y: 0 },
  113. { x: 0, y: -1 },
  114. { x: -1, y: 0 },
  115.  
  116. { x: 1, y: -1 },
  117. { x: 1, y: 1 },
  118. { x: -1, y: 1 },
  119. { x: -1, y: -1 },
  120. ];
  121.  
  122. for (var neighbourIndex in neighbors) {
  123. var neighborDelta = neighbors[neighbourIndex];
  124. var neighborX = currentNode.x + neighborDelta.x;
  125. var neighborY = currentNode.y + neighborDelta.y;
  126.  
  127. if (!this.isInsideMap(neighborX, neighborY) || this.isCellEmpty(neighborX, neighborY)) {
  128. // console.log('cell is empty or outside map:', neighborX, neighborY);
  129. continue;
  130. }
  131.  
  132. var neighborNode = new this.Node(neighborX, neighborY);
  133.  
  134. var checkNeighbor = function(node) {
  135. return node.x === neighborX && node.y === neighborY;
  136. };
  137.  
  138. if (closedList.some(checkNeighbor)) {
  139. continue;
  140. }
  141.  
  142. var tentativeG = currentNode.g + 1; // Assuming each step costs 1
  143.  
  144. if (!openList.some(checkNeighbor) || tentativeG < neighborNode.g) {
  145. neighborNode.g = tentativeG;
  146. neighborNode.h = this.heuristic(neighborNode, endNode);
  147. neighborNode.f = neighborNode.g + neighborNode.h;
  148. neighborNode.parent = currentNode;
  149.  
  150. if (!openList.some(checkNeighbor)) {
  151. openList.push(neighborNode);
  152. }
  153. }
  154. }
  155. }
  156.  
  157. // console.log(closedList);
  158.  
  159. return null; // No path found
  160. }
  161. }
  162.  
  163. // === CSS styles ===
  164.  
  165. GM_addStyle_object('#boss-data-section #mission-info, #bossmap-page #mission-info', {
  166. 'border-radius': '25px 25px 0 0',
  167. });
  168. GM_addStyle_object('#boss-data-section #mission-info-distance-viewer, #bossmap-page #mission-info-distance-viewer', {
  169. 'position': 'absolute !important',
  170. 'background-color': 'hsla(0,0%,5%,.8)',
  171. 'border-radius': '0 0 25px 25px',
  172. 'padding': '5px',
  173. 'top': '770px',
  174. 'left': 'calc(50% - 16pt * 20)',
  175. 'right': 'calc(50% - 16pt * 20)',
  176. });
  177.  
  178. GM_addStyle_object('#boss-data-section #mission-info-buttons-title, #bossmap-page #mission-info-buttons-title', {
  179. 'color': 'white',
  180. 'font-size': '20px',
  181. });
  182. GM_addStyle_object('#boss-data-section #mission-info-buttons-subtitle, #bossmap-page #mission-info-buttons-subtitle', {
  183. 'color': 'white',
  184. 'font-size': '14px',
  185. });
  186. GM_addStyle_object('#boss-data-section button.mission-info-button, #bossmap-page button.mission-info-button', {
  187. 'background-color': 'gray',
  188. 'color': 'black',
  189. 'padding': '0.25em 0.5em',
  190. });
  191. GM_addStyle_object('#boss-data-section button.mission-info-button:hover, #bossmap-page button.mission-info-button:hover', {
  192. 'color': 'white',
  193. });
  194. GM_addStyle_object('#boss-data-section #dist-buttons, #bossmap-page #dist-buttons', {
  195. 'display': 'flex',
  196. 'gap': '10px',
  197. 'justify-content': 'center',
  198. });
  199. GM_addStyle_object('#boss-data-section td.coord.path, #bossmap-page td.coord.path', {
  200. 'background-color': 'yellow !important',
  201. 'color': 'black !important',
  202. });
  203.  
  204. ready(function () {
  205.  
  206. // === Create Elements ===
  207. var missionHolder = document.getElementById('mission-holder');
  208.  
  209. var container = document.createElement('div');
  210. container.id = 'mission-info-distance-viewer';
  211.  
  212. container.innerHTML = '<div id="mission-info-buttons-title">Path finder</div>';
  213. container.innerHTML += '<div id="mission-info-buttons-subtitle">No path selected</div>';
  214. container.innerHTML += '<div id="dist-buttons"><button id="dist-set-start" class="mission-info-button">Set start cell</button><button id="dist-set-end" class="mission-info-button">Set end cell</button><button id="dist-clear" style="display: none;" class="mission-info-button">Clear path</button></div>';
  215.  
  216. missionHolder.appendChild(container);
  217.  
  218.  
  219. unsafeWindow.closeMissionHolder = function (event) {
  220. if (event.target.closest('#mission-info-distance-viewer')) return;
  221.  
  222. missionHolder.style.display = 'none';
  223. };
  224.  
  225. missionHolder.setAttribute('onclick', 'closeMissionHolder(event)');
  226.  
  227. var startCellButton = document.getElementById('dist-set-start');
  228. var endCellButton = document.getElementById('dist-set-end');
  229. var clearPathButton = document.getElementById('dist-clear');
  230.  
  231. var subtitle = document.getElementById('mission-info-buttons-subtitle');
  232.  
  233. // === Scan empty cells
  234.  
  235. var emptyCells = Array.from(document.querySelectorAll('td.coord'))
  236. .filter(function (el) {
  237. return el.computedStyleMap().get('opacity').toString() == '0';
  238. })
  239. .map(function (el) {
  240. return {
  241. x: el.classList[1].replace('x', ''),
  242. y: el.classList[2].replace('y', ''),
  243. };
  244. });
  245.  
  246. var startCell = null;
  247. var endCell = null;
  248.  
  249. var pathFinder = new AStar(emptyCells);
  250. function maybeUpdatePath() {
  251. if (!startCell || !endCell) return;
  252.  
  253. var path = pathFinder.find(startCell, endCell);
  254.  
  255. // Clear existing path cells
  256. var pathCells = unsafeWindow.document.querySelectorAll('td.coord.path');
  257. for (var i = 0; i < pathCells.length; i++) {
  258. pathCells[i].classList.remove('path');
  259. }
  260.  
  261. // console.log(path);
  262. if (!path) return;
  263.  
  264. for(var i = 0; i < path.length; i++) {
  265. var cellCoord = path[i];
  266. // console.log(cellCoord);
  267. var cell = unsafeWindow.document.querySelector('td.coord.x' + cellCoord.x + '.y' + cellCoord.y);
  268. cell.classList.add('path');
  269. }
  270. clearPathButton.style.display = 'initial';
  271.  
  272. subtitle.innerHTML = 'Path length: ' + path.length + ' cells';
  273. }
  274.  
  275.  
  276. startCellButton.onclick = function () {
  277. // current pos
  278. var img = unsafeWindow.document.querySelector('#mission-info img');
  279. var matches = img.src.match(/Fairview_(\d+)x(\d+)/);
  280. var x = matches[1];
  281. var y = matches[2];
  282.  
  283. startCell = { x: x, y: y };
  284.  
  285. unsafeWindow.document.querySelector('td.coord.x' + x + '.y' + y).classList.add('path');
  286.  
  287. missionHolder.style.display = 'none';
  288. maybeUpdatePath();
  289. };
  290.  
  291. endCellButton.onclick = function () {
  292. // current pos
  293. var img = document.querySelector('#mission-info img');
  294. var matches = img.src.match(/Fairview_(\d+)x(\d+)/);
  295. var x = matches[1];
  296. var y = matches[2];
  297.  
  298. endCell = { x: x, y: y };
  299.  
  300. unsafeWindow.document.querySelector('td.coord.x' + x + '.y' + y).classList.add('path');
  301.  
  302. missionHolder.style.display = 'none';
  303. maybeUpdatePath();
  304. };
  305.  
  306. clearPathButton.onclick = function () {
  307. startCell = null;
  308. endCell = null;
  309.  
  310. clearPathButton.style.display = 'none';
  311.  
  312. // Clear existing path cells
  313. var pathCells = unsafeWindow.document.querySelectorAll('td.coord.path');
  314. for (var i = 0; i < pathCells.length; i++) {
  315. pathCells[i].classList.remove('path');
  316. }
  317.  
  318. subtitle.innerHTML = 'No path selected';
  319. missionHolder.style.display = 'none';
  320. };
  321. });
  322. })();