Skip to main content
GameDev.net gamedev.net
🔒 Locked

Optimising LOS Algorithm

Started by morfe Sep 8, 2000 at 12:51 PM 0 replies 450+ views
Original Post
morfe
morfe
The code as it is works fine, but I would like to make it faster ... what can I do? I'm not afraid of ASM, but I can't really see what I could do that the compiler doesn't already do. BTW, the sight_blocked call has been replaced with a (map[x,y].flags && OPAQUE) == false, but I wanted to leave the original intact ... just in case
#define SGN(a)		((a<0) ? -1 : 1)

BOOL los(coord mx, coord my)
{
int t, x, y, ax, ay, sx, sy, dx, dy;
   dx = d.px - mx;
   dy = d.py - my;
   ax = abs(dx) << 1;
   ay = abs(dy) << 1;
   sx = SGN(dx);
   sy = SGN(dy);
   x = mx;
   y = my;
   if(ax > ay)
   {
      t = ay - (ax >> 1);
      do
      {
         if(t >= 0)
         {
            y += sy;
            t -= ax;
         }
         
         x += sx;
         t += ay;
         
         if x == d.px && y == d.py)
         {
            return TRUE;
         }
      while(sight_blocked(x,y) == FALSE);

      return FALSE;
   }
   else
   {
      // Y dominant loop

      t = ax - (ay >> 1);
      do
      {
         if(t >= 0)
         {
            x += sx;
            t -= ay;
         }
         y += sy;
         t += ax;
         if(x == d.px && y == d.py)
         {
            return TRUE;
         }
      }
      while(sight_blocked(x,y) == FALSE);
      return FALSE;
   }
}
------------------------------------------------- Mindphuq Software : "Who do you want to do today?" Edited by - morfe on 9/8/00 12:52:54 PM
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.