Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Map::checkSightLine(const Position& fromPos, const Position& toPos) const
- {
- /*Position start = fromPos;
- Position end = toPos;
- int32_t x, y, z, dx = std::abs(start.x - end.x), dy = std::abs(start.y - end.y),
- dz = std::abs(start.z - end.z), sx, sy, sz, ey, ez, max = dx, dir = 0;
- if(dy > max)
- {
- max = dy;
- dir = 1;
- }
- if(dz > max)
- {
- max = dz;
- dir = 2;
- }
- switch(dir)
- {
- case 1:
- //x -> y
- //y -> x
- //z -> z
- std::swap(start.x, start.y);
- std::swap(end.x, end.y);
- std::swap(dx, dy);
- break;
- case 2:
- //x -> z
- //y -> y
- //z -> x
- std::swap(start.x, start.z);
- std::swap(end.x, end.z);
- std::swap(dx, dz);
- break;
- default:
- //x -> x
- //y -> y
- //z -> z
- break;
- }
- sx = ((start.x < end.x) ? 1 : -1);
- sy = ((start.y < end.y) ? 1 : -1);
- sz = ((start.z < end.z) ? 1 : -1);
- ey = ez = 0;
- x = start.x;
- y = start.y;
- z = start.z;
- int32_t lastrx = 0, lastry = 0, lastrz = 0;
- for(; x != end.x + sx; x += sx)
- {
- int32_t rx, ry, rz;
- switch(dir)
- {
- case 1:
- rx = y; ry = x; rz = z;
- break;
- case 2:
- rx = z; ry = y; rz = x;
- break;
- default:
- rx = x; ry = y; rz = z;
- break;
- }
- if(!lastrx && !lastry && !lastrz)
- {
- lastrx = rx;
- lastry = ry;
- lastrz = rz;
- }
- if(lastrz != rz || ((toPos.x != rx || toPos.y != ry || toPos.z != rz) && (fromPos.x != rx || fromPos.y != ry || fromPos.z != rz)))
- {
- if(lastrz != rz && const_cast<Map*>(this)->getTile(lastrx, lastry, std::min(lastrz, rz)))
- return false;
- lastrx = rx; lastry = ry; lastrz = rz;
- const Tile* tile = const_cast<Map*>(this)->getTile(rx, ry, rz);
- if(tile && tile->hasProperty(BLOCKPROJECTILE))
- return false;
- }
- ey += dy;
- ez += dz;
- if(2 * ey >= dx)
- {
- y += sy;
- ey -= dx;
- }
- if(2 * ez >= dx)
- {
- z += sz;
- ez -= dx;
- }
- }
- return true;*/
- if(Position::areInRange<0,0,15>(fromPos, toPos))
- return true;
- Position start(fromPos.z > toPos.z ? toPos : fromPos);
- Position destination(fromPos.z > toPos.z ? fromPos : toPos);
- const int8_t mx = start.x < destination.x ? 1 : start.x == destination.x ? 0 : -1;
- const int8_t my = start.y < destination.y ? 1 : start.y == destination.y ? 0 : -1;
- int32_t A = destination.y - start.y, B = start.x - destination.x,
- C = -(A * destination.x + B * destination.y);
- while(!Position::areInRange<0,0,15>(start, destination))
- {
- int32_t moveH = std::abs(A * (start.x + mx) + B * (start.y) + C),
- moveV = std::abs(A * (start.x) + B * (start.y + my) + C),
- moveX = std::abs(A * (start.x + mx) + B * (start.y + my) + C);
- if(start.y != destination.y && (start.x == destination.x || moveH > moveV || moveH > moveX))
- start.y += my;
- if(start.x != destination.x && (start.y == destination.y || moveV > moveH || moveV > moveX))
- start.x += mx;
- const Tile* tile = const_cast<Map*>(this)->getTile(start);
- if(tile && tile->hasProperty(BLOCKPROJECTILE))
- return false;
- }
- while(start.z != destination.z) // now we need to perform a jump between floors to see if everything is clear (literally)
- {
- const Tile* tile = const_cast<Map*>(this)->getTile(start);
- if(tile && tile->getThingCount() > 0)
- return false;
- start.z++;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement