Advertisement
goodwin64

HWM - show cells within range

Jul 16th, 2023
1,341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var CellCurrentStatus;
  2. (function (CellCurrentStatus) {
  3.     CellCurrentStatus[CellCurrentStatus["NOT_AVAILABLE"] = 0] = "NOT_AVAILABLE";
  4.     CellCurrentStatus[CellCurrentStatus["MYSELF"] = 200] = "MYSELF";
  5.     CellCurrentStatus[CellCurrentStatus["SHOOTER_CAN_REACH_WITHOUT_PENALTY"] = 210] = "SHOOTER_CAN_REACH_WITHOUT_PENALTY";
  6.     CellCurrentStatus[CellCurrentStatus["OCCUPIED"] = 211] = "OCCUPIED";
  7.     CellCurrentStatus[CellCurrentStatus["AVAILABLE"] = 250] = "AVAILABLE";
  8. })(CellCurrentStatus || (CellCurrentStatus = {}));
  9. var UnitType;
  10. (function (UnitType) {
  11.     UnitType["barbarianChar"] = "pers_barbani";
  12.     UnitType["barbarianBirds"] = "rocani";
  13.     UnitType["barbarianOrcs"] = "orcani";
  14.     UnitType["barbarianOgres"] = "ogreani";
  15.     UnitType["barbarianHobgoblins"] = "hobgoblinani";
  16.     UnitType["barbarianWolvesUpgraded"] = "hobwolfriderani";
  17.     UnitType["necromancerChar"] = "pers_necrani";
  18.     UnitType["necromancerSkeletons"] = "sceletonani";
  19.     UnitType["necromancerGhosts"] = "ghostani";
  20.     UnitType["necromancerVampires"] = "vampireani";
  21.     UnitType["demonChar"] = "wpers_demonani";
  22.     UnitType["demonDogs"] = "demondogani";
  23.     UnitType["stepGoblin"] = "goblinusani";
  24.     UnitType["demonImp"] = "familiarani";
  25.     UnitType["demonHorned"] = "hdemonani";
  26.     UnitType["demonSuccub"] = "succubani";
  27. })(UnitType || (UnitType = {}));
  28. function getClosestStack(selectedStack, otherStacks) {
  29.     let distanceToClosest = Infinity;
  30.     let closestStack = otherStacks[0];
  31.     otherStacks.forEach((s) => {
  32.         const distanceToStack = calculateDistance(selectedStack, s);
  33.         if (distanceToStack < distanceToClosest) {
  34.             distanceToClosest = distanceToStack;
  35.             closestStack = s;
  36.         }
  37.     });
  38.     return closestStack;
  39. }
  40. function getReachableEnemyCell(enemyStacks, currentStack) {
  41.     const closestEnemy = getClosestStack(currentStack, enemyStacks);
  42.     if (!closestEnemy) {
  43.         return null;
  44.     }
  45.     const closestEnemySurroundingCells = getSurroundingCells(closestEnemy.x, closestEnemy.y);
  46.     const isEnemyReachable = closestEnemySurroundingCells.some(c => getCellCurrentStatus(c.x, c.y) === CellCurrentStatus.AVAILABLE);
  47.     if (isEnemyReachable) {
  48.         return {
  49.             x: closestEnemy.x,
  50.             y: closestEnemy.y,
  51.         };
  52.     }
  53.     return null;
  54. }
  55. function calculateDistance(cell1, cell2, powFactor = 2) {
  56.     const xDiff = Math.abs(cell1.x - cell2.x);
  57.     const yDiff = Math.abs(cell1.y - cell2.y);
  58.     return +Math.pow(xDiff ** powFactor + yDiff ** powFactor, 1 / powFactor).toFixed(2);
  59. }
  60. function getSurroundingCells(x, y, fieldWidth = 12, fieldHeight = 10) {
  61.     let result = [];
  62.     for (let i = x - 1; i <= x + 1; i++) {
  63.         for (let j = y - 1; j <= y + 1; j++) {
  64.             if (i < 1 || j < 1 || i > fieldWidth || j > fieldHeight) {
  65.                 continue;
  66.             }
  67.             result.push({ x: i, y: j });
  68.         }
  69.     }
  70.     return result;
  71. }
  72. function getCellCurrentStatus(x, y, battlefieldFullWidth = 12, battlefieldVisibleHeight = 10, battlefieldArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 211, 211, 0, 0, 0, 0, 0, 0, 0, 0, 0, 210, 0, 211, 0, 211, 250, 250, 250, 250, 0, 0, 0, 0, 0, 0, 0, 0, 250, 250, 250, 250, 250, 250, 0, 0, 0, 0, 0, 0, 0, 0, 250, 250, 250, 250, 250, 211, 0, 0, 0, 0, 0, 0, 0, 0, 250, 250, 250, 250, 250, 211, 0, 0, 0, 0, 0, 0, 0, 0, 250, 250, 250, 250, 250, 211, 210, 210, 0, 211, 250, 250, 0, 0, 250, 250, 250, 250, 250, 200, 200, 210, 250, 250, 250, 250, 0, 0, 250, 250, 250, 250, 250, 200, 200, 250, 250, 250, 250, 250, 0, 0, 250, 250, 211, 250, 250, 250, 250, 250, 250, 250, 250, 250, 0, 0, 250, 250, 0, 250, 250, 250, 250, 250, 250, 250, 250, 250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) {
  73.     if ((y < 1) || (y > battlefieldVisibleHeight) || (x < 0) || (x > battlefieldFullWidth - 1)) {
  74.         return 0;
  75.     }
  76.     return battlefieldArray[y * battlefieldFullWidth + x];
  77. }
  78. function convertCellIdToCoordinates(cellId) {
  79.     const [x, y] = cellId.split(':');
  80.     return {
  81.         x: Number(x),
  82.         y: Number(y),
  83.     };
  84. }
  85.  
  86. var row = '';
  87.  
  88. [
  89.     '1:1',    '2:1',   '3:1',   '4:1',   '5:1',   '6:1',   '7:1',   '8:1',   '9:1',   '10:1',   '11:1',   '12:1',
  90.     '1:2',    '2:2',   '3:2',   '4:2',   '5:2',   '6:2',   '7:2',   '8:2',   '9:2',   '10:2',   '11:2',   '12:2',
  91.     '1:3',    '2:3',   '3:3',   '4:3',   '5:3',   '6:3',   '7:3',   '8:3',   '9:3',   '10:3',   '11:3',   '12:3',
  92.     '1:4',    '2:4',   '3:4',   '4:4',   '5:4',   '6:4',   '7:4',   '8:4',   '9:4',   '10:4',   '11:4',   '12:4',
  93.     '1:5',    '2:5',   '3:5',   '4:5',   '5:5',   '6:5',   '7:5',   '8:5',   '9:5',   '10:5',   '11:5',   '12:5',
  94.     '1:6',    '2:6',   '3:6',   '4:6',   '5:6',   '6:6',   '7:6',   '8:6',   '9:6',   '10:6',   '11:6',   '12:6',
  95.     '1:7',    '2:7',   '3:7',   '4:7',   '5:7',   '6:7',   '7:7',   '8:7',   '9:7',   '10:7',   '11:7',   '12:7',
  96.     '1:8',    '2:8',   '3:8',   '4:8',   '5:8',   '6:8',   '7:8',   '8:8',   '9:8',   '10:8',   '11:8',   '12:8',
  97.     '1:9',    '2:9',   '3:9',   '4:9',   '5:9',   '6:9',   '7:9',   '8:9',   '9:9',   '10:9',   '11:9',   '12:9',
  98.     '1:10',  '2:10',  '3:10',  '4:10',  '5:10',  '6:10',  '7:10',  '8:10',  '9:10',  '10:10',  '11:10',  '12:10',
  99. ].map(convertCellIdToCoordinates).map(c => calculateDistance(c, {x:11, y:8}, 2.5)).forEach((v, i) => {
  100.     if (i % 12 === 0) {
  101.         console.log();
  102.         console.log(row);
  103.         row = '';
  104.     }
  105.     row += String(v).padEnd(10);
  106. });
  107.  
  108. /*
  109. output:
  110.  
  111. 11.47     10.68     9.93      9.24      8.61      8.08      7.65      7.33      7.12      7.02      7         7.02      
  112. 11.03     10.19     9.38      8.61      7.92      7.3       6.79      6.4       6.15      6.03      6         6.03      
  113. 10.67     9.78      8.91      8.08      7.3       6.6       5.99      5.52      5.2       5.04      5         5.04      
  114. 10.39     9.46      8.54      7.65      6.79      5.99      5.28      4.69      4.27      4.05      4         4.05      
  115. 10.19     9.23      8.27      7.33      6.4       5.52      4.69      3.96      3.4       3.08      3         3.08      
  116. 10.07     9.08      8.1       7.12      6.15      5.2       4.27      3.4       2.64      2.13      2         2.13      
  117. 10.01     9.01      8.02      7.02      6.03      5.04      4.05      3.08      2.13      1.32      1         1.32      
  118. 10        9         8         7         6         5         4         3         2         1         0         1        
  119. 10.01     9.01      8.02      7.02      6.03      5.04      4.05      3.08      2.13      1.32      1         1.32  
  120. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement