{"id":464015,"date":"2025-06-20T21:01:13","date_gmt":"2025-06-20T21:01:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=464015"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=464015","title":{"rendered":"<span>\u041a\u0430\u043a \u044f \u0434\u0435\u043b\u0430\u043b \u0430\u0432\u0442\u043e\u043f\u0443\u0442\u044c \u0434\u043b\u044f \u0438\u0433\u0440\u044b \u043d\u0430 Phaser (TypeScript)<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/18b\/e16\/230\/18be16230aefdef38a7755ec49330f96.gif\" width=\"598\" height=\"450\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/18b\/e16\/230\/18be16230aefdef38a7755ec49330f96.gif 780w,&#10;       https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/18b\/e16\/230\/18be16230aefdef38a7755ec49330f96.gif 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<h3>\u041f\u0440\u043e\u043b\u043e\u0433<\/h3>\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0421\u0440\u0430\u0437\u0443 \u0445\u043e\u0447\u0443 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0433\u0430\u0439\u0434, \u0438 \u044f \u043d\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e, \u043a\u0430\u043a <strong>\u043d\u0443\u0436\u043d\u043e<\/strong> \u043a\u043e\u0434\u0438\u0442\u044c \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c, \u0438 \u0447\u0442\u043e \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>\u042f \u043d\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0442, \u0438 \u0432\u0441\u0451, \u043e \u0447\u0451\u043c \u044f \u043f\u0438\u0448\u0443 \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u0441\u0430\u043c \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u041c\u043e\u0435\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u0431\u044b\u043b\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0442\u043e\u0447\u043d\u0443\u044e \u043a\u0430\u0440\u0442\u0443 \u0438 \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u0435\u0440\u0441\u043e\u043d\u0430\u0436\u0430 \u0438\u0441\u043a\u0430\u0442\u044c \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u0434\u043e \u0442\u043e\u0447\u043a\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u044f \u043d\u0430\u0436\u0430\u043b, \u0438 \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u0439.<\/p>\n<p>\u041f\u043e\u0437\u0436\u0435 \u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b NPC \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c AI: \u043e\u043d\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0435\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0438\u0433\u0440\u043e\u043a\u0430, \u0435\u0441\u043b\u0438 \u0442\u043e\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0440\u044f\u0434\u043e\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0451\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0438 \u043f\u0443\u0442\u0438.<\/p>\n<p>\u0414\u043b\u044f\u00a0\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0441\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u0430\u043a\u00a0\u0438 \u0434\u043b\u044f\u00a0\u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447 \u0433\u0434\u0435 \u0435\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0430 \u0441\u00a0\u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0447\u0435\u0433\u043e \u043b\u0438\u0431\u043e. \u0412\u00a0\u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u0442 \u043c\u043d\u0435 \u043d\u0435\u00a0\u043d\u0443\u0436\u043d\u043e\u00a0\u0431\u044b\u043b\u043e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c A*.<\/p>\n<blockquote>\n<p><strong>A*<\/strong> \u2014 \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0442\u0438, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u0441\u0435\u0442\u043a\u0430\u0445 \u0441 \u043f\u0440\u0435\u043f\u044f\u0442\u0441\u0442\u0432\u0438\u044f\u043c\u0438. \u041e\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u043f\u0443\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u043e\u0447\u043d\u044b\u043c. <\/p>\n<\/blockquote>\n<p>\u0412\u043a\u0440\u0430\u0442\u0446\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c <strong>A*<\/strong>.<\/p>\n<p>A* \u0441\u043e\u0447\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435:<\/p>\n<ul>\n<li>\n<p><strong>\u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 (<\/strong><code><strong>g<\/strong><\/code><strong>)<\/strong> \u2014 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043e;<\/p>\n<\/li>\n<li>\n<p><strong>\u044d\u0432\u0440\u0438\u0441\u0442\u0438\u043a\u0443 (<\/strong><code><strong>h<\/strong><\/code><strong>)<\/strong> \u2014 \u043e\u0446\u0435\u043d\u043a\u0430, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0435\u0449\u0451 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c;<\/p>\n<\/li>\n<li>\n<p>\u0438 <strong>\u043e\u0431\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (<\/strong><code><strong>f = g + h<\/strong><\/code><strong>)<\/strong> \u2014 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u0432\u044b\u0431\u043e\u0440\u0430.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u0430\u0440\u0442\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u043b\u0430 \u0441\u043e\u0431\u043e\u0439 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 <code>Tile[][]<\/code>, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430 \u043c\u043e\u0433\u043b\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439:<\/p>\n<ul>\n<li>\n<p><strong>walkable<\/strong> \u2014 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430 (\u0435\u0441\u043b\u0438 \u044f \u043d\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0442\u0443\u0434\u0430 \u043e\u0431\u044a\u0435\u043a\u0442);<\/p>\n<\/li>\n<li>\n<p><strong>occupant<\/strong> \u2014 \u0437\u0430\u043d\u044f\u0442\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430 (\u0435\u0441\u043b\u0438 \u0432 \u043d\u0435\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f NPC \u0438\u043b\u0438 \u0438\u0433\u0440\u043e\u043a);<\/p>\n<\/li>\n<li>\n<p><strong>reservedBy<\/strong> \u2014 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u043f\u0443\u0442\u044f\u043c\u0438 NPC \u0438 \u0438\u0433\u0440\u043e\u043a\u0430.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 TypeScript \u0434\u043b\u044f \u0442\u0435\u0445 \u043a\u043e\u043c\u0443 \u0441\u0440\u0430\u0437\u0443 \u043d\u0443\u0434\u0435\u043d \u043a\u043e\u0434<\/h3>\n<pre><code class=\"javascript\">interface PathNode {     x: number;     y: number;     g: number;     f: number;     cameFrom?: PathNode; }  export class Pathfinder {     private grid: Tile[][];      constructor(grid: Tile[][], private readonly ignoreReservedBy?: unknown) {         this.grid = grid;     }      public findPath(start: { x: number; y: number }, goal: { x: number; y: number }): { x: number; y: number }[] {         if (!this.isWalkable(goal.x, goal.y)) return [];          const key = (x: number, y: number) =&gt; `${x},${y}`;         const h = (a: { x: number; y: number }, b: { x: number; y: number }) =&gt;             Math.abs(a.x - b.x) + Math.abs(a.y - b.y);          const openSet: PathNode[] = [];         const openMap = new Map&lt;string, PathNode&gt;();         const closedSet = new Set&lt;string&gt;();          const startNode: PathNode = { ...start, g: 0, f: h(start, goal) };         openSet.push(startNode);         openMap.set(key(start.x, start.y), startNode);          while (openSet.length &gt; 0) {             let currentIndex = 0;             for (let i = 1; i &lt; openSet.length; i++) {                 if (openSet[i].f &lt; openSet[currentIndex].f) {                     currentIndex = i;                 }             }              const current = openSet.splice(currentIndex, 1)[0];             openMap.delete(key(current.x, current.y));\u0439              closedSet.add(key(current.x, current.y));              if (current.x === goal.x &amp;&amp; current.y === goal.y) {                 const path: { x: number; y: number }[] = [];                 let node: PathNode | undefined = current;                 while (node) {                     path.push({ x: node.x, y: node.y });                     node = node.cameFrom;                 }                 return path.reverse();             }              const directions = [                 { x: 1, y: 0 }, { x: -1, y: 0 },                 { x: 0, y: 1 }, { x: 0, y: -1 },             ];              for (const offset of directions) {                 const nx = current.x + offset.x;                 const ny = current.y + offset.y;                 const neighborKey = key(nx, ny);                  if (!this.isWalkable(nx, ny) || closedSet.has(neighborKey)) continue;                  const tentativeG = current.g + 1;                 const existing = openMap.get(neighborKey);                  if (!existing || tentativeG &lt; existing.g) {                     const node: PathNode = {                         x: nx,                         y: ny,                         g: tentativeG,                         f: tentativeG + h({ x: nx, y: ny }, goal),                         cameFrom: current,                     };                      if (!existing) {                         openSet.push(node);                         openMap.set(neighborKey, node);                     } else {                         Object.assign(existing, node);                     }                 }             }         }          return [];     }      private isWalkable(x: number, y: number): boolean {         const tile = this.grid[y]?.[x];         if (!tile || !tile.walkable) return false;         if (tile.occupant) return false;         if (tile.reservedBy &amp;&amp; tile.reservedBy !== this.ignoreReservedBy) return false;         return true;     } } <\/code><\/pre>\n<h3>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043b\u044f \u0442\u0435\u0445 \u043a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e: \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u0432\u0441\u0442\u0430\u0442\u044c \u0432 \u043a\u043b\u0435\u0442\u043a\u0443? \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u2014 \u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0445\u043e\u0436\u0443.<\/p>\n<ul>\n<li>\n<p><code>key<\/code> \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438 \u0443\u0437\u043b\u043e\u0432 \u0432 <code>Map<\/code> \u0438 <code>Set<\/code><\/p>\n<\/li>\n<li>\n<p><code>h<\/code> \u2014 \u043c\u0430\u043d\u0445\u044d\u0442\u0442\u0435\u043d\u0441\u043a\u0430\u044f \u044d\u0432\u0440\u0438\u0441\u0442\u0438\u043a\u0430: |\u04451 &#8212; \u04452| + |\u04431 &#8212; \u04432| (\u043c\u0430\u043d\u0445\u044d\u0442\u0442\u0435\u043d\u0441\u043a\u0430\u044f , \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0433\u043e\u0440\u043e\u0434 \u0432\u044b\u0441\u0442\u0440\u043e\u0435\u043d \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0430\u043c\u0438 \u0438 \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0445\u043e\u0434\u0438\u0442\u044c \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044f\u043c\u0438).<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u04451 \u0438 y1 \u044d\u0442\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430<\/p>\n<p>x2 \u0438 y2 \u044d\u0442\u043e \u0442\u043e\u0447\u043a\u0430 \u043a\u0443\u0434\u0430 \u043d\u0443\u0434\u043d\u043e \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f<\/p>\n<p>\u042d\u0442\u043e \u043c\u043e\u0434\u0443\u043b\u044c \u0447\u0438\u0441\u043b\u0430 \u2014 \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u043d\u0443\u043b\u044f, \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435.<\/p>\n<\/blockquote>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u0437\u0430\u0432\u0451\u043b \u0442\u0440\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\n<p><code>openSet[]<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0437\u043b\u043e\u0432, \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438;<\/p>\n<\/li>\n<li>\n<p><code>openMap<\/code> \u2014 <code>Map<\/code> \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u0437\u043b\u043e\u0432 \u043f\u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c;<\/p>\n<\/li>\n<li>\n<p><code>closedSet<\/code> \u2014 <code>Set<\/code> \u0441 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u042f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e \u0435\u0451 \u0432 <code>openSet<\/code>.<\/p>\n<p>\u0412 \u0433\u043b\u0430\u0432\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435\u00a0<code>While<\/code>\u00a0\u044f \u0438\u0449\u0443 \u0443\u0437\u0435\u043b \u0441 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u043c <code>f<\/code> \u0438 \u0443\u0434\u0430\u043b\u044f\u044e \u0435\u0433\u043e \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0437\u0430\u043d\u043e\u0448\u0443 \u0432 \u0437\u0430\u043a\u0440\u044b\u0442\u043e\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e.<\/p>\n<pre><code class=\"javascript\">let currentIndex = 0; for (let i = 1; i &lt; openSet.length; i++) {     if (openSet[i].f &lt; openSet[currentIndex].f) {         currentIndex = i;     } }  const current = openSet.splice(currentIndex, 1)[0]; openMap.delete(key(current.x, current.y)); closedSet.add(key(current.x, current.y));<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u2014 \u044d\u0442\u043e \u0446\u0435\u043b\u044c, \u0441\u0442\u0440\u043e\u044e \u043f\u0443\u0442\u044c, \u0434\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u043f\u043e <code>cameFrom<\/code>, \u0438 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e \u0435\u0433\u043e. <code>cameFrom<\/code> \u2014 \u044d\u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443, \u043e\u0442\u043a\u0443\u0434\u0430 \u043f\u0440\u0438\u0448\u0451\u043b \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0443\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435: \u043e\u0442 \u0446\u0435\u043b\u0438 \u043a \u0441\u0442\u0430\u0440\u0442\u0443. \u041c\u0435\u0442\u043e\u0434 <code>.reverse()<\/code> \u043d\u0443\u0436\u0435\u043d, \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u0443\u0442\u044c \u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u2014 \u043e\u0442 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a \u0446\u0435\u043b\u0438.<\/p>\n<pre><code class=\"javascript\">if (current.x === goal.x &amp;&amp; current.y === goal.y) {     const path: { x: number; y: number }[] = [];     let node: PathNode | undefined = current;     while (node) {         path.push({ x: node.x, y: node.y });         node = node.cameFrom;     }     return path.reverse(); }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u043f\u0440\u043e\u0445\u043e\u0436\u0443 \u043f\u043e \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c \u043a\u043b\u0435\u0442\u043a\u0430\u043c (\u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0432\u0435\u0440\u0445, \u0432\u043d\u0438\u0437, \u0432\u043b\u0435\u0432\u043e \u0438 \u0432\u043f\u0440\u0430\u0432\u043e). \u041f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u044e \u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0435 (<code>closedSet<\/code>) \u0438 \u043d\u0435\u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u044b\u0435 (<code>!isWalkable<\/code>) \u043a\u043b\u0435\u0442\u043a\u0438.<\/p>\n<pre><code class=\"javascript\">const tentativeG = current.g + 1; const existing = openMap.get(neighborKey);  if (!existing || tentativeG &lt; existing.g) {     const node: PathNode = {         x: nx,         y: ny,         g: tentativeG,         f: tentativeG + h({ x: nx, y: ny }, goal),         cameFrom: current,     };      if (!existing) {         openSet.push(node);         openMap.set(neighborKey, node);     } else {         Object.assign(existing, node);     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u043f\u0443\u0442\u044c \u043a \u0441\u043e\u0441\u0435\u0434\u0443 \u2014 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e \u0435\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e \u0432 <code>openSet<\/code>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u043f\u0443\u0441\u0442\u0435\u043b\u0430, \u0430 \u0446\u0435\u043b\u044c \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442\u0430 \u2014 \u0432\u044b\u0445\u043e\u0434\u0438\u043c.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043d\u0430\u0448\u043b\u0438 \u043b\u0443\u0447\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u043a \u0441\u043e\u0441\u0435\u0434\u0443 \u2014 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u044d\u0442\u043e \u0432\u0441\u0451 \u043f\u043e \u043c\u0430\u0442\u0447\u0430\u0441\u0442\u0438. \u0414\u0430\u043b\u044c\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0438\u0430\u0440.<\/p>\n<p>\u042f \u0437\u0430\u0432\u0451\u043b <a href=\"https:\/\/t.me\/GameDFS\" rel=\"noopener noreferrer nofollow\">Telegram-\u043a\u0430\u043d\u0430\u043b<\/a>, \u0433\u0434\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u044e, \u0434\u0435\u043b\u044e\u0441\u044c \u043f\u0440\u0438\u043a\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u043c\u0438 \u0438 \u043a\u043e\u0434\u043e\u043c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/920366\/\"> https:\/\/habr.com\/ru\/articles\/920366\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<h3>\u041f\u0440\u043e\u043b\u043e\u0433<\/h3>\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0421\u0440\u0430\u0437\u0443 \u0445\u043e\u0447\u0443 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0433\u0430\u0439\u0434, \u0438 \u044f \u043d\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e, \u043a\u0430\u043a <strong>\u043d\u0443\u0436\u043d\u043e<\/strong> \u043a\u043e\u0434\u0438\u0442\u044c \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c, \u0438 \u0447\u0442\u043e \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>\u042f \u043d\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0442, \u0438 \u0432\u0441\u0451, \u043e \u0447\u0451\u043c \u044f \u043f\u0438\u0448\u0443 \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u0441\u0430\u043c \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u041c\u043e\u0435\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u0431\u044b\u043b\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0442\u043e\u0447\u043d\u0443\u044e \u043a\u0430\u0440\u0442\u0443 \u0438 \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u0435\u0440\u0441\u043e\u043d\u0430\u0436\u0430 \u0438\u0441\u043a\u0430\u0442\u044c \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u0434\u043e \u0442\u043e\u0447\u043a\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u044f \u043d\u0430\u0436\u0430\u043b, \u0438 \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u0439.<\/p>\n<p>\u041f\u043e\u0437\u0436\u0435 \u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b NPC \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c AI: \u043e\u043d\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0435\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0438\u0433\u0440\u043e\u043a\u0430, \u0435\u0441\u043b\u0438 \u0442\u043e\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0440\u044f\u0434\u043e\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0451\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0438 \u043f\u0443\u0442\u0438.<\/p>\n<p>\u0414\u043b\u044f\u00a0\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0441\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u0430\u043a\u00a0\u0438 \u0434\u043b\u044f\u00a0\u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447 \u0433\u0434\u0435 \u0435\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0430 \u0441\u00a0\u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0447\u0435\u0433\u043e \u043b\u0438\u0431\u043e. \u0412\u00a0\u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u0442 \u043c\u043d\u0435 \u043d\u0435\u00a0\u043d\u0443\u0436\u043d\u043e\u00a0\u0431\u044b\u043b\u043e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c A*.<\/p>\n<blockquote>\n<p><strong>A*<\/strong> \u2014 \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0442\u0438, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u0441\u0435\u0442\u043a\u0430\u0445 \u0441 \u043f\u0440\u0435\u043f\u044f\u0442\u0441\u0442\u0432\u0438\u044f\u043c\u0438. \u041e\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u043f\u0443\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u043e\u0447\u043d\u044b\u043c. <\/p>\n<\/blockquote>\n<p>\u0412\u043a\u0440\u0430\u0442\u0446\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c <strong>A*<\/strong>.<\/p>\n<p>A* \u0441\u043e\u0447\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435:<\/p>\n<ul>\n<li>\n<p><strong>\u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 (<\/strong><code><strong>g<\/strong><\/code><strong>)<\/strong> \u2014 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043e;<\/p>\n<\/li>\n<li>\n<p><strong>\u044d\u0432\u0440\u0438\u0441\u0442\u0438\u043a\u0443 (<\/strong><code><strong>h<\/strong><\/code><strong>)<\/strong> \u2014 \u043e\u0446\u0435\u043d\u043a\u0430, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0435\u0449\u0451 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c;<\/p>\n<\/li>\n<li>\n<p>\u0438 <strong>\u043e\u0431\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (<\/strong><code><strong>f = g + h<\/strong><\/code><strong>)<\/strong> \u2014 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u0432\u044b\u0431\u043e\u0440\u0430.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u0430\u0440\u0442\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u043b\u0430 \u0441\u043e\u0431\u043e\u0439 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 <code>Tile[][]<\/code>, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430 \u043c\u043e\u0433\u043b\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439:<\/p>\n<ul>\n<li>\n<p><strong>walkable<\/strong> \u2014 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430 (\u0435\u0441\u043b\u0438 \u044f \u043d\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0442\u0443\u0434\u0430 \u043e\u0431\u044a\u0435\u043a\u0442);<\/p>\n<\/li>\n<li>\n<p><strong>occupant<\/strong> \u2014 \u0437\u0430\u043d\u044f\u0442\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430 (\u0435\u0441\u043b\u0438 \u0432 \u043d\u0435\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f NPC \u0438\u043b\u0438 \u0438\u0433\u0440\u043e\u043a);<\/p>\n<\/li>\n<li>\n<p><strong>reservedBy<\/strong> \u2014 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u043f\u0443\u0442\u044f\u043c\u0438 NPC \u0438 \u0438\u0433\u0440\u043e\u043a\u0430.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 TypeScript \u0434\u043b\u044f \u0442\u0435\u0445 \u043a\u043e\u043c\u0443 \u0441\u0440\u0430\u0437\u0443 \u043d\u0443\u0434\u0435\u043d \u043a\u043e\u0434<\/h3>\n<pre><code class=\"javascript\">interface PathNode {     x: number;     y: number;     g: number;     f: number;     cameFrom?: PathNode; }  export class Pathfinder {     private grid: Tile[][];      constructor(grid: Tile[][], private readonly ignoreReservedBy?: unknown) {         this.grid = grid;     }      public findPath(start: { x: number; y: number }, goal: { x: number; y: number }): { x: number; y: number }[] {         if (!this.isWalkable(goal.x, goal.y)) return [];          const key = (x: number, y: number) =&gt; `${x},${y}`;         const h = (a: { x: number; y: number }, b: { x: number; y: number }) =&gt;             Math.abs(a.x - b.x) + Math.abs(a.y - b.y);          const openSet: PathNode[] = [];         const openMap = new Map&lt;string, PathNode&gt;();         const closedSet = new Set&lt;string&gt;();          const startNode: PathNode = { ...start, g: 0, f: h(start, goal) };         openSet.push(startNode);         openMap.set(key(start.x, start.y), startNode);          while (openSet.length &gt; 0) {             let currentIndex = 0;             for (let i = 1; i &lt; openSet.length; i++) {                 if (openSet[i].f &lt; openSet[currentIndex].f) {                     currentIndex = i;                 }             }              const current = openSet.splice(currentIndex, 1)[0];             openMap.delete(key(current.x, current.y));\u0439              closedSet.add(key(current.x, current.y));              if (current.x === goal.x &amp;&amp; current.y === goal.y) {                 const path: { x: number; y: number }[] = [];                 let node: PathNode | undefined = current;                 while (node) {                     path.push({ x: node.x, y: node.y });                     node = node.cameFrom;                 }                 return path.reverse();             }              const directions = [                 { x: 1, y: 0 }, { x: -1, y: 0 },                 { x: 0, y: 1 }, { x: 0, y: -1 },             ];              for (const offset of directions) {                 const nx = current.x + offset.x;                 const ny = current.y + offset.y;                 const neighborKey = key(nx, ny);                  if (!this.isWalkable(nx, ny) || closedSet.has(neighborKey)) continue;                  const tentativeG = current.g + 1;                 const existing = openMap.get(neighborKey);                  if (!existing || tentativeG &lt; existing.g) {                     const node: PathNode = {                         x: nx,                         y: ny,                         g: tentativeG,                         f: tentativeG + h({ x: nx, y: ny }, goal),                         cameFrom: current,                     };                      if (!existing) {                         openSet.push(node);                         openMap.set(neighborKey, node);                     } else {                         Object.assign(existing, node);                     }                 }             }         }          return [];     }      private isWalkable(x: number, y: number): boolean {         const tile = this.grid[y]?.[x];         if (!tile || !tile.walkable) return false;         if (tile.occupant) return false;         if (tile.reservedBy &amp;&amp; tile.reservedBy !== this.ignoreReservedBy) return false;         return true;     } } <\/code><\/pre>\n<h3>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043b\u044f \u0442\u0435\u0445 \u043a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e: \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u0432\u0441\u0442\u0430\u0442\u044c \u0432 \u043a\u043b\u0435\u0442\u043a\u0443? \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u2014 \u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0445\u043e\u0436\u0443.<\/p>\n<ul>\n<li>\n<p><code>key<\/code> \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438 \u0443\u0437\u043b\u043e\u0432 \u0432 <code>Map<\/code> \u0438 <code>Set<\/code><\/p>\n<\/li>\n<li>\n<p><code>h<\/code> \u2014 \u043c\u0430\u043d\u0445\u044d\u0442\u0442\u0435\u043d\u0441\u043a\u0430\u044f \u044d\u0432\u0440\u0438\u0441\u0442\u0438\u043a\u0430: |\u04451 &#8212; \u04452| + |\u04431 &#8212; \u04432| (\u043c\u0430\u043d\u0445\u044d\u0442\u0442\u0435\u043d\u0441\u043a\u0430\u044f , \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0433\u043e\u0440\u043e\u0434 \u0432\u044b\u0441\u0442\u0440\u043e\u0435\u043d \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0430\u043c\u0438 \u0438 \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0445\u043e\u0434\u0438\u0442\u044c \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044f\u043c\u0438).<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u04451 \u0438 y1 \u044d\u0442\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430<\/p>\n<p>x2 \u0438 y2 \u044d\u0442\u043e \u0442\u043e\u0447\u043a\u0430 \u043a\u0443\u0434\u0430 \u043d\u0443\u0434\u043d\u043e \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f<\/p>\n<p>\u042d\u0442\u043e \u043c\u043e\u0434\u0443\u043b\u044c \u0447\u0438\u0441\u043b\u0430 \u2014 \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u043d\u0443\u043b\u044f, \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435.<\/p>\n<\/blockquote>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u0437\u0430\u0432\u0451\u043b \u0442\u0440\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\n<p><code>openSet[]<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0437\u043b\u043e\u0432, \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438;<\/p>\n<\/li>\n<li>\n<p><code>openMap<\/code> \u2014 <code>Map<\/code> \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u0437\u043b\u043e\u0432 \u043f\u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c;<\/p>\n<\/li>\n<li>\n<p><code>closedSet<\/code> \u2014 <code>Set<\/code> \u0441 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u042f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e \u0435\u0451 \u0432 <code>openSet<\/code>.<\/p>\n<p>\u0412 \u0433\u043b\u0430\u0432\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435\u00a0<code>While<\/code>\u00a0\u044f \u0438\u0449\u0443 \u0443\u0437\u0435\u043b \u0441 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u043c <code>f<\/code> \u0438 \u0443\u0434\u0430\u043b\u044f\u044e \u0435\u0433\u043e \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0437\u0430\u043d\u043e\u0448\u0443 \u0432 \u0437\u0430\u043a\u0440\u044b\u0442\u043e\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e.<\/p>\n<pre><code class=\"javascript\">let currentIndex = 0; for (let i = 1; i &lt; openSet.length; i++) {     if (openSet[i].f &lt; openSet[currentIndex].f) {         currentIndex = i;     } }  const current = openSet.splice(currentIndex, 1)[0]; openMap.delete(key(current.x, current.y)); closedSet.add(key(current.x, current.y));<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u2014 \u044d\u0442\u043e \u0446\u0435\u043b\u044c, \u0441\u0442\u0440\u043e\u044e \u043f\u0443\u0442\u044c, \u0434\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u043f\u043e <code>cameFrom<\/code>, \u0438 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e \u0435\u0433\u043e. <code>cameFrom<\/code> \u2014 \u044d\u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443, \u043e\u0442\u043a\u0443\u0434\u0430 \u043f\u0440\u0438\u0448\u0451\u043b \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0443\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435: \u043e\u0442 \u0446\u0435\u043b\u0438 \u043a \u0441\u0442\u0430\u0440\u0442\u0443. \u041c\u0435\u0442\u043e\u0434 <code>.reverse()<\/code> \u043d\u0443\u0436\u0435\u043d, \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u0443\u0442\u044c \u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u2014 \u043e\u0442 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a \u0446\u0435\u043b\u0438.<\/p>\n<pre><code class=\"javascript\">if (current.x === goal.x &amp;&amp; current.y === goal.y) {     const path: { x: number; y: number }[] = [];     let node: PathNode | undefined = current;     while (node) {         path.push({ x: node.x, y: node.y });         node = node.cameFrom;     }     return path.reverse(); }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u043f\u0440\u043e\u0445\u043e\u0436\u0443 \u043f\u043e \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c \u043a\u043b\u0435\u0442\u043a\u0430\u043c (\u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0432\u0435\u0440\u0445, \u0432\u043d\u0438\u0437, \u0432\u043b\u0435\u0432\u043e \u0438 \u0432\u043f\u0440\u0430\u0432\u043e). \u041f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u044e \u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0435 (<code>closedSet<\/code>) \u0438 \u043d\u0435\u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u044b\u0435 (<code>!isWalkable<\/code>) \u043a\u043b\u0435\u0442\u043a\u0438.<\/p>\n<pre><code class=\"javascript\">const tentativeG = current.g + 1; const existing = openMap.get(neighborKey);  if (!existing || tentativeG &lt; existing.g) {     const node: PathNode = {         x: nx,         y: ny,         g: tentativeG,         f: tentativeG + h({ x: nx, y: ny }, goal),         cameFrom: current,     };      if (!existing) {         openSet.push(node);         openMap.set(neighborKey, node);     } else {         Object.assign(existing, node);     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u043f\u0443\u0442\u044c \u043a \u0441\u043e\u0441\u0435\u0434\u0443 \u2014 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e \u0435\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e \u0432 <code>openSet<\/code>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u043f\u0443\u0441\u0442\u0435\u043b\u0430, \u0430 \u0446\u0435\u043b\u044c \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442\u0430 \u2014 \u0432\u044b\u0445\u043e\u0434\u0438\u043c.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043d\u0430\u0448\u043b\u0438 \u043b\u0443\u0447\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u043a \u0441\u043e\u0441\u0435\u0434\u0443 \u2014 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u044d\u0442\u043e \u0432\u0441\u0451 \u043f\u043e \u043c\u0430\u0442\u0447\u0430\u0441\u0442\u0438. \u0414\u0430\u043b\u044c\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0438\u0430\u0440.<\/p>\n<p>\u042f \u0437\u0430\u0432\u0451\u043b <a href=\"https:\/\/t.me\/GameDFS\" rel=\"noopener noreferrer nofollow\">Telegram-\u043a\u0430\u043d\u0430\u043b<\/a>, \u0433\u0434\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u044e, \u0434\u0435\u043b\u044e\u0441\u044c \u043f\u0440\u0438\u043a\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u043c\u0438 \u0438 \u043a\u043e\u0434\u043e\u043c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/920366\/\"> https:\/\/habr.com\/ru\/articles\/920366\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-464015","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/464015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=464015"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/464015\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=464015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=464015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=464015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}