enbyted

Modyfikacja mapy - niestandardowe granice

Jan 12th, 2021 (edited)
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // By grabie2 on 13.01.2021
  2. var gbBordersConfig = {
  3.     overrideMode: 'single',
  4.     borders: [
  5.         {start: [500, 500], dir: 'X', length: 10, color: 'red', cap: 'round', width: 5, minimapWidth: 3},
  6.         {start: [500, 500], dir: 'Y', length: -12, color: 'blue', cap: 'square', width: 10, minimapWidth: 3},
  7.         {start: [500, 500], end: [503, 502], color: 'yellow', cap: 'butt', width: 10, minimapWidth: 3},
  8.     ]
  9. };
  10. ((cfg) => {
  11.     if (typeof cfg !== 'object') {
  12.         UI.InfoMessage('Nieprawidłowa konfiguracja skryptu', 5000, 'error');
  13.         return;
  14.     }
  15.     if (typeof TWMap !== 'object') {
  16.         UI.InfoMessage('Skrypt działa tylko na ekranach na ktróch widoczna jest mapa', 5000, 'error');
  17.         return;
  18.     }
  19.    
  20.     if (! window.gbOriginalSpawnSector)
  21.         window.gbOriginalSpawnSector = TWMap.mapHandler.spawnSector;
  22.    
  23.     if (! window.gbOriginalMinimapLoadSector)
  24.         window.gbOriginalMinimapLoadSector = TWMap.minimapHandler.loadSector;
  25.  
  26.     const originalSpawnSector = (cfg.overrideMode === 'piggyback') ? TWMap.mapHandler.spawnSector : window.gbOriginalSpawnSector;
  27.     const originalMinimapLoadSector = (cfg.overrideMode === 'piggyback') ? TWMap.minimapHandler.loadSector : window.gbOriginalMinimapLoadSector;
  28.  
  29.     let borderLines = (!Array.isArray(cfg.borders)) ? [] :
  30.         cfg.borders
  31.             .filter(b => Array.isArray(b.start) || b.start.length === 2 || typeof b.dir === 'string' || typeof b.length === 'number' || b.length !== 0)
  32.             .map(b => {
  33.                 if (b.dir == 'X')
  34.                 {
  35.                     if (b.length > 0)
  36.                         return {
  37.                             sx: b.start[0],
  38.                             sy: b.start[1],
  39.                             ex: b.start[0] + b.length,
  40.                             ey: b.start[1],
  41.                             dir: 'X',
  42.                             cap: b.cap || 'round',
  43.                             color: b.color || 'red',
  44.                             width: b.width || 5,
  45.                             minimapWidth: b.minimapWidth || 1,
  46.                         };
  47.                    else
  48.                         return {
  49.                             sx: b.start[0] + b.length,
  50.                             sy: b.start[1],
  51.                             ex: b.start[0],
  52.                             ey: b.start[1],
  53.                             dir: 'X',
  54.                             cap: b.cap || 'round',
  55.                             color: b.color || 'red',
  56.                             width: b.width || 5,
  57.                             minimapWidth: b.minimapWidth || 1,
  58.                         };
  59.                 }
  60.                 else if (b.dir == 'Y')
  61.                 {
  62.                     if (b.length > 0)
  63.                         return {
  64.                             sx: b.start[0],
  65.                             sy: b.start[1],
  66.                             ex: b.start[0],
  67.                             ey: b.start[1] + b.length,
  68.                             dir: 'Y',
  69.                             cap: b.cap || 'round',
  70.                             color: b.color || 'red',
  71.                             width: b.width || 5,
  72.                             minimapWidth: b.minimapWidth || 1,
  73.                         };
  74.                    else
  75.                         return {
  76.                             sx: b.start[0],
  77.                             sy: b.start[1],
  78.                             ex: b.start[0],
  79.                             ey: b.start[1] + b.length,
  80.                             dir: 'Y',
  81.                             cap: b.cap || 'round',
  82.                             color: b.color || 'red',
  83.                             width: b.width || 5,
  84.                             minimapWidth: b.minimapWidth || 1,
  85.                         };
  86.                 }
  87.                 else if (Array.isArray(b.end) && b.end.length == 2)
  88.                 {
  89.                         return {
  90.                             sx: b.start[0],
  91.                             sy: b.start[1],
  92.                             ex: b.end[0],
  93.                             ey: b.end[1],
  94.                             dir: 'U',
  95.                             cap: b.cap || 'round',
  96.                             color: b.color || 'red',
  97.                             width: b.width || 5,
  98.                             minimapWidth: b.minimapWidth || 1,
  99.                         };
  100.                 }
  101.                 else
  102.                 {
  103.                     console.info('Invalid border direction', b.dir)
  104.                 }
  105.             });
  106.  
  107.     TWMap.minimapHandler.loadSector = function(a) {
  108.         originalMinimapLoadSector.apply(TWMap.minimapHandler, [a]);
  109.  
  110.         const map = a._map;
  111.         const canvas = document.createElement("canvas");
  112.  
  113.         canvas.width = map.scale[0] * map.sectorSize;
  114.         canvas.height = map.scale[1] * map.sectorSize;
  115.         canvas.style.position = "absolute";
  116.         canvas.style.zIndex = "10";
  117.  
  118.         a.appendElement(canvas, 0, 0);
  119.  
  120.         const context = canvas.getContext('2d');
  121.         if (context)
  122.         {
  123.             const sector = {
  124.                 sx: a.x,
  125.                 ex: a.x + map.sectorSize,
  126.                 sy: a.y,
  127.                 ey: a.y + map.sectorSize,
  128.             };  
  129.  
  130.             for (const border of borderLines)
  131.             {
  132.                 context.strokeStyle = border.color;
  133.                 context.lineWidth = border.minimapWidth;
  134.                 context.lineCap = border.cap;
  135.  
  136.                 context.beginPath();
  137.                 context.moveTo((border.sx - sector.sx)*map.scale[0], (border.sy - sector.sy)*map.scale[1]);
  138.                 context.lineTo((border.ex - sector.sx)*map.scale[0], (border.ey - sector.sy)*map.scale[1]);
  139.                 context.stroke();
  140.             }
  141.         }
  142.     }
  143.  
  144.     TWMap.mapHandler.spawnSector = function(e, a) {
  145.         originalSpawnSector.apply(TWMap.mapHandler, [e, a]);
  146.  
  147.         let canvas = a._elements ? a._elements.find(e => e instanceof HTMLCanvasElement) : null;
  148.         if (! canvas)
  149.         {
  150.             const cavas_id = "map_canvas_" + a.x + "_" + a.y;
  151.             let canvas = document.getElementById(cavas_id);
  152.         }
  153.         if (! canvas)
  154.         {
  155.             a.spawn();
  156.             const cavas_id = "map_canvas_" + a.x + "_" + a.y;
  157.             let canvas = document.getElementById(cavas_id);
  158.         }
  159.  
  160.         if (!canvas)
  161.         {
  162.             console.log("Cannot find canvas, this is a bug, please report to ")
  163.         }
  164.  
  165.         const context = canvas.getContext('2d');
  166.         const map = a._map;
  167.         if (context)
  168.         {
  169.             const sectorBorderLines = [
  170.                 { sx: a.x, sy: a.y, ex: a.x + map.sectorSize, ey: a.y},
  171.                 { sx: a.x + map.sectorSize, sy: a.y, ex: a.x + map.sectorSize, ey: a.y + map.sectorSize},
  172.                 { sx: a.x + map.sectorSize, sy: a.y + map.sectorSize, ex: a.x, ey: a.x + map.sectorSize},
  173.                 { sx: a.x, sy: a.x + map.sectorSize, ex: a.x, ey: a.y},
  174.             ];
  175.             const sector = {
  176.                 sx: a.x,
  177.                 ex: a.x + map.sectorSize,
  178.                 sy: a.y,
  179.                 ey: a.y + map.sectorSize,
  180.             };  
  181.  
  182.             for (const border of borderLines)
  183.             {
  184.                 context.strokeStyle = border.color;
  185.                 context.lineWidth = border.width;
  186.                 context.lineCap = border.cap;
  187.  
  188.                 context.beginPath();
  189.                 context.moveTo((border.sx - sector.sx)*map.scale[0], (border.sy - sector.sy)*map.scale[1]);
  190.                 context.lineTo((border.ex - sector.sx)*map.scale[0], (border.ey - sector.sy)*map.scale[1]);
  191.                 context.stroke();
  192.             }
  193.         }
  194.     }
  195.     TWMap.reload();
  196. })(gbBordersConfig);
  197.  
Add Comment
Please, Sign In to add comment