Advertisement
Cavitt

Untitled

Apr 20th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. bool Map::checkSightLine(const Position& fromPos, const Position& toPos) const
  2. {
  3.         /*Position start = fromPos;
  4.         Position end = toPos;
  5.  
  6.         int32_t x, y, z, dx = std::abs(start.x - end.x), dy = std::abs(start.y - end.y),
  7.                 dz = std::abs(start.z - end.z), sx, sy, sz, ey, ez, max = dx, dir = 0;
  8.         if(dy > max)
  9.         {
  10.                 max = dy;
  11.                 dir = 1;
  12.         }
  13.  
  14.         if(dz > max)
  15.         {
  16.                 max = dz;
  17.                 dir = 2;
  18.         }
  19.  
  20.         switch(dir)
  21.         {
  22.                 case 1:
  23.                         //x -> y
  24.                         //y -> x
  25.                         //z -> z
  26.                         std::swap(start.x, start.y);
  27.                         std::swap(end.x, end.y);
  28.                         std::swap(dx, dy);
  29.                         break;
  30.                 case 2:
  31.                         //x -> z
  32.                         //y -> y
  33.                         //z -> x
  34.                         std::swap(start.x, start.z);
  35.                         std::swap(end.x, end.z);
  36.                         std::swap(dx, dz);
  37.                         break;
  38.                 default:
  39.                         //x -> x
  40.                         //y -> y
  41.                         //z -> z
  42.                         break;
  43.         }
  44.  
  45.         sx = ((start.x < end.x) ? 1 : -1);
  46.         sy = ((start.y < end.y) ? 1 : -1);
  47.         sz = ((start.z < end.z) ? 1 : -1);
  48.  
  49.         ey = ez = 0;
  50.         x = start.x;
  51.         y = start.y;
  52.         z = start.z;
  53.  
  54.         int32_t lastrx = 0, lastry = 0, lastrz = 0;
  55.         for(; x != end.x + sx; x += sx)
  56.         {
  57.                 int32_t rx, ry, rz;
  58.                 switch(dir)
  59.                 {
  60.                         case 1:
  61.                                 rx = y; ry = x; rz = z;
  62.                                 break;
  63.                         case 2:
  64.                                 rx = z; ry = y; rz = x;
  65.                                 break;
  66.                         default:
  67.                                 rx = x; ry = y; rz = z;
  68.                                 break;
  69.                 }
  70.  
  71.                 if(!lastrx && !lastry && !lastrz)
  72.                 {
  73.                         lastrx = rx;
  74.                         lastry = ry;
  75.                         lastrz = rz;
  76.                 }
  77.  
  78.                 if(lastrz != rz || ((toPos.x != rx || toPos.y != ry || toPos.z != rz) && (fromPos.x != rx || fromPos.y != ry || fromPos.z != rz)))
  79.                 {
  80.                         if(lastrz != rz && const_cast<Map*>(this)->getTile(lastrx, lastry, std::min(lastrz, rz)))
  81.                                 return false;
  82.  
  83.                         lastrx = rx; lastry = ry; lastrz = rz;
  84.                         const Tile* tile = const_cast<Map*>(this)->getTile(rx, ry, rz);
  85.                         if(tile && tile->hasProperty(BLOCKPROJECTILE))
  86.                                 return false;
  87.                 }
  88.  
  89.                 ey += dy;
  90.                 ez += dz;
  91.                 if(2 * ey >= dx)
  92.                 {
  93.                         y += sy;
  94.                         ey -= dx;
  95.                 }
  96.  
  97.                 if(2 * ez >= dx)
  98.                 {
  99.                         z += sz;
  100.                         ez -= dx;
  101.                 }
  102.         }
  103.  
  104.         return true;*/
  105.         if(Position::areInRange<0,0,15>(fromPos, toPos))
  106.                 return true;
  107.  
  108.         Position start(fromPos.z > toPos.z ? toPos : fromPos);
  109.         Position destination(fromPos.z > toPos.z ? fromPos : toPos);
  110.  
  111.         const int8_t mx = start.x < destination.x ? 1 : start.x == destination.x ? 0 : -1;
  112.         const int8_t my = start.y < destination.y ? 1 : start.y == destination.y ? 0 : -1;
  113.  
  114.         int32_t A = destination.y - start.y, B = start.x - destination.x,
  115.                 C = -(A * destination.x + B * destination.y);
  116.         while(!Position::areInRange<0,0,15>(start, destination))
  117.         {
  118.                 int32_t moveH = std::abs(A * (start.x + mx) + B * (start.y) + C),
  119.                         moveV = std::abs(A * (start.x) + B * (start.y + my) + C),
  120.                         moveX = std::abs(A * (start.x + mx) + B * (start.y + my) + C);
  121.                 if(start.y != destination.y && (start.x == destination.x || moveH > moveV || moveH > moveX))
  122.                         start.y += my;
  123.  
  124.                 if(start.x != destination.x && (start.y == destination.y || moveV > moveH || moveV > moveX))
  125.                         start.x += mx;
  126.  
  127.                 const Tile* tile = const_cast<Map*>(this)->getTile(start);
  128.                 if(tile && tile->hasProperty(BLOCKPROJECTILE))
  129.                         return false;
  130.         }
  131.  
  132.         while(start.z != destination.z) // now we need to perform a jump between floors to see if everything is clear (literally)
  133.         {
  134.                 const Tile* tile = const_cast<Map*>(this)->getTile(start);
  135.                 if(tile && tile->getThingCount() > 0)
  136.                         return false;
  137.  
  138.                 start.z++;
  139.         }
  140.  
  141.         return true;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement