Guest User

ScanrangeCalc.cpp

a guest
Dec 19th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include "ScanrangeCalc.hpp"
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5.  
  6. CScanrangeCalc::CScanrangeCalc()
  7. {
  8.     ulScanShift = 7;
  9. }
  10.  
  11. void CScanrangeCalc::InitializeTables()
  12. {
  13.     uint32_t ulScanShifted = 1 << ulScanShift;
  14.     memset(pTablesCost, 0, sizeof(pTablesCost));
  15.     memset(pTablesOffset, 0, sizeof(pTablesOffset));
  16.  
  17.     for(int32_t i = 0; i <= 20; ++i)
  18.     {
  19.         for(int32_t j = 0; j <= 20; ++j)
  20.         {
  21.             if (j >= (i / 2))
  22.             {
  23.                 if (j <= (i * 2))
  24.                 {
  25.                     pTablesOffset[20+i][20+j][0] = -1;
  26.                     pTablesOffset[20+i][20+j][1] = -1;
  27.                     pTablesOffset[20-i][20-j][0] = 1;
  28.                     pTablesOffset[20-i][20-j][1] = 1;
  29.                     pTablesOffset[20+i][20-j][0] = -1;
  30.                     pTablesOffset[20+i][20-j][1] = 1;
  31.                     pTablesOffset[20-i][20+j][0] = 1;
  32.                     pTablesOffset[20-i][20+j][1] = -1;
  33.                 }
  34.                 else
  35.                 {
  36.                     pTablesOffset[20+i][20+j][0] = 0;
  37.                     pTablesOffset[20+i][20+j][1] = -1;
  38.                     pTablesOffset[20-i][20-j][0] = 0;
  39.                     pTablesOffset[20-i][20-j][1] = 1;
  40.                     pTablesOffset[20+i][20-j][0] = 0;
  41.                     pTablesOffset[20+i][20-j][1] = 1;
  42.                     pTablesOffset[20-i][20+j][0] = 0;
  43.                     pTablesOffset[20-i][20+j][1] = -1;
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 pTablesOffset[20+i][20+j][0] = -1;
  49.                 pTablesOffset[20+i][20+j][1] = 0;
  50.                 pTablesOffset[20-i][20-j][0] = 1;
  51.                 pTablesOffset[20-i][20-j][1] = 0;
  52.                 pTablesOffset[20+i][20-j][0] = -1;
  53.                 pTablesOffset[20+i][20-j][1] = 0;
  54.                 pTablesOffset[20-i][20+j][0] = 1;
  55.                 pTablesOffset[20-i][20+j][1] = 0;
  56.             }
  57.  
  58.             int32_t v4;
  59.             if(i > j) v4 = i;
  60.             else v4 = j;
  61.  
  62.             int16_t v1 = (int16_t)(sqrt(double(j*j + i*i)) * (double)ulScanShifted / (double)v4);
  63.             pTablesCost[20+i][20+j] = v1;
  64.             pTablesCost[20+i][20-j] = v1;
  65.             pTablesCost[20-i][20+j] = v1;
  66.             pTablesCost[20-i][20-j] = v1;
  67.         }
  68.     }
  69.  
  70.     pTablesCost[20][20] = 0;
  71. }
  72.  
  73. bool CScanrangeCalc::SetCell(uint8_t x, uint8_t y, uint8_t height_origin, uint8_t height_cell)
  74. {
  75.     int16_t vision_previous = pTablesVision[x+pTablesOffset[x][y][0]][y+pTablesOffset[x][y][1]];
  76.     uint16_t cost = pTablesCost[x][y];
  77.     //if(bDiv2) cost -= 18;
  78.     pTablesVision[x][y] = vision_previous - (height_cell - height_origin + cost);
  79.     return (pTablesVision[x][y] >= 0);
  80. }
  81.  
  82. void CScanrangeCalc::CalculateVision(int32_t x, int32_t y, uint16_t scanrange, uint8_t (*func_GetHeight)(int16_t,int16_t), bool (*func_CheckValid)(int16_t,int16_t))
  83. {
  84.     memset(pTablesVision, 0, sizeof(pTablesVision));
  85.     if(!func_GetHeight) return;
  86.     if(!func_CheckValid) return;
  87.  
  88.     uint16_t vision = scanrange;
  89.  
  90.     uint16_t vision2 = (1 << (ulScanShift - 1)) + (vision >> (8 - ulScanShift));
  91.  
  92.     int32_t genX = x - 20;
  93.     int32_t genY = y - 20;
  94.     uint8_t ht_origin = func_GetHeight(x, y);
  95.  
  96.     uint32_t result = 0;
  97.     pTablesVision[20][20] = vision2;
  98.     for(int32_t i = 1; i < 20; i++)
  99.     {
  100.         bool fdisp = false;
  101.         for(int32_t j = -i; j < i+1; j++)
  102.         {
  103.             bDiv2 = false;
  104.             if(func_CheckValid(genX+(20+j), genY+(20-i)) &&
  105.                SetCell(20+j, 20-i, ht_origin, func_GetHeight(genX+(20+j), genY+(20-i))))
  106.                 fdisp = true;
  107.             if(func_CheckValid(genX+(20+j), genY+(20+i)) &&
  108.                SetCell(20+j, 20+i, ht_origin, func_GetHeight(genX+(20+j), genY+(20+i))))
  109.                 fdisp = true;
  110.             bDiv2 = true;
  111.             if(func_CheckValid(genX+(20-i), genY+(20+j)) &&
  112.                SetCell(20-i, 20+j, ht_origin, func_GetHeight(genX+(20-i), genY+(20+j))))
  113.                 fdisp = true;
  114.             if(func_CheckValid(genX+(20+i), genY+(20-j)) &&
  115.                SetCell(20+i, 20-j, ht_origin, func_GetHeight(genX+(20+i), genY+(20-j))))
  116.                 fdisp = true;
  117.         }
  118.  
  119.         if(!fdisp) break;
  120.     }
  121. }
Add Comment
Please, Sign In to add comment