Guest User

Untitled

a guest
May 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. //FOV CODE
  2.  
  3.  
  4. bool raycast(int sx, int sy, int sh, int tx, int ty, int th)
  5. {
  6.   int x = sx;
  7.   int y = sy;
  8.  
  9.   int dx = tx - sx;
  10.   int dy = ty - sy;
  11.   if (dx == 0 && dy == 0) return false;
  12.   float cdx = 10000.0;
  13.   float cdy = 10000.0;
  14.   if (dy != 0)
  15.   {
  16.    
  17.     cdx = IntToFloat(dx)/IntToFloat(dy);
  18.     if (cdx < 0.0) cdx = -cdx;
  19.   }
  20.   if (dx != 0)
  21.   {
  22.    
  23.     cdy = IntToFloat(dy)/IntToFloat(dx);
  24.     if (cdy < 0.0) cdy = -cdy;
  25.   }
  26.  
  27.   float D = Maths.Sqrt(IntToFloat(dx*dx + dy*dy));
  28.  
  29.   // distance
  30.   float fx = 0.0;
  31.   float fy = 0.0;
  32.  
  33.   // initial steps to get from the center of the tile to lines
  34.   float hdx = 0.5*cdx;
  35.   float hdy = 0.5;
  36.   float vdx = 0.5;
  37.   float vdy = 0.5*cdy;
  38.  
  39.   int xstep = 1, ystep = 1;
  40.   if (dx < 0) xstep = -1;
  41.   if (dy < 0) ystep = -1;
  42.  
  43.   bool done = false;
  44.   while (!done)
  45.   {
  46.    
  47.     if (hdx < vdx)
  48.     {
  49.       //HIT H
  50.       fx += hdx;
  51.       fy += hdy;
  52.      
  53.       vdx -= hdx;
  54.       vdy -= hdy;
  55.      
  56.       hdx = cdx;
  57.       hdy = 1.0;
  58.      
  59.       y += ystep;
  60.     }
  61.     else
  62.     {
  63.       //HIT V
  64.       fx += vdx;
  65.       fy += vdy;
  66.      
  67.       hdx -= vdx;
  68.       hdy -= vdy;
  69.      
  70.       vdx = 1.0;
  71.       vdy = cdy;
  72.      
  73.       x += xstep;
  74.     }
  75.     // dcalc
  76.     float d = Maths.Sqrt(fx*fx + fy*fy);
  77.     float h = IntToFloat(sh) + (d/D)*IntToFloat(th - sh);
  78.     int index = Map.Index(x, y, 0);
  79.     int tileType = tiles[index];
  80.     if (!TileInfo[tileType].Walkable)
  81.     {
  82.       return false;
  83.     }
  84.     else if (x == tx && y == ty)
  85.     {
  86.      
  87.       return true;
  88.      
  89.     }
  90.   }
  91. }
  92.  
  93. void DoFov(int sX, int sY, float x, float y,  int radius)
  94. {
  95.   int i;
  96.   float ox,oy;
  97.   ox = IntToFloat(sX)+0.5;
  98.   oy = IntToFloat(sY)+0.5;
  99.   while (i < radius){
  100.     int ix = FloatToInt(ox, eRoundDown);
  101.     int iy = FloatToInt(oy, eRoundDown);
  102.     if (!Map.Sane(ix, iy)) return;
  103.     int index = Map.Index(ix, iy, 0);
  104.    
  105.     //tileMask[tileIndex(FloatToInt(ox, eRoundDown), FloatToInt(oy, eRoundDown))] = 1;//Set the tile to visible.
  106.     //if(tiles[index].Height - tiles[tileIndex(pos.X, pos.Y)].Height > 1) return;
  107.     if (raycast(sX, sY, 1, ix, iy, 1)) {
  108.       tileMask[index] = 1;
  109.     }
  110.    
  111.     ox+=x;
  112.     oy+=y;
  113.     i++;
  114.   }
  115. }
  116.  
  117. static void Map::SetFOV(int sX,  int sY, int radius)
  118. {
  119.   float x,y;
  120.   int i;
  121.   ClearTileMask();
  122.   while (i < 360){
  123.     x = Maths.Cos(IntToFloat(i) * 0.01745);
  124.     y = Maths.Sin(IntToFloat(i) * 0.01745);
  125.     DoFov(sX, sY, x, y, radius);
  126.     i+=1;
  127.   }
  128. }
Add Comment
Please, Sign In to add comment