Advertisement
TyrannisUmbra

Trimmed LoS code

Mar 25th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. // new canSee code
  2. public boolean canSee(Tile t){
  3.     int perception=getSpecial(PERCEPTION_ID);
  4.     int absoluteDist=location.distanceToTile(t);
  5.     int xDist=location.getX()-t.getX();
  6.     int yDist=location.getY()-t.getY();
  7.     int dist;
  8.     int stepDist=1;
  9.     int stepRemain=0;
  10.     int xNeg;
  11.     int yNeg;
  12.     Tile checkTile;
  13.     //Check for out of sight range
  14.     if(!(absoluteDist<=perception)){
  15.         return false;
  16.     }
  17.     //Can always see your current location
  18.     if(t.equals(location)){
  19.         return true;
  20.     }
  21.     //check if distance for each coord is positive or negative
  22.     if(xDist>=0){ //positive x
  23.         xNeg=1;
  24.     }else{ //negative x
  25.         xNeg=-1;
  26.     }
  27.     if(yDist>=0){ //positive y
  28.         yNeg=1;
  29.     }else{ //negative y
  30.         yNeg=-1;
  31.     }
  32.     //check if tile is further along X or Y coords and set variables
  33.     boolean furtherX;
  34.     if(Math.abs(xDist)>=Math.abs(yDist)){ //further along x axis
  35.         stepDist=Math.abs((xDist+xNeg)/(yDist+yNeg));
  36.         stepRemain=Math.abs((xDist+xNeg)%(yDist+yNeg));
  37.         dist=xDist+xNeg;
  38.         furtherX=true;
  39.     }else{ //further along y axis
  40.         stepDist=Math.abs((yDist+yNeg)/(xDist+xNeg));
  41.         stepRemain=Math.abs((yDist+yNeg)%(xDist+xNeg));
  42.         dist=yDist+yNeg;
  43.         furtherX=false;
  44.     }
  45.     //check for if stepRemain is >0
  46.     boolean complex;
  47.     if(stepRemain==0){
  48.         complex=false;
  49.     }else{
  50.         complex=true;
  51.     }
  52.     //actual loop to determine if the Tile is visible to the Creature or not
  53.     int j=0;
  54.     int step=stepDist;
  55.     for(int i=0;i<Math.abs(dist);i++){
  56.         if(furtherX){
  57.             checkTile=Map.getCurrentMap().getTile(location.getX()-(xNeg*i),location.getY()-(yNeg*j));
  58.         }else{
  59.             checkTile=Map.getCurrentMap().getTile(location.getX()-(xNeg*j),location.getY()-(yNeg*i));
  60.         }
  61.         //if the tile you're currently checking is the target tile, then you can see it
  62.         if(checkTile.equals(t)){
  63.             return true;
  64.         }
  65.         //If the tile you're currently checking is not the target tile, and there's objects you can't see through on the tile, you can't see the target
  66.         if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  67.             return false;
  68.         }
  69.         step--;
  70.         if(step==0){
  71.             if(complex){
  72.                 if(j==0){
  73.                     stepDist++;
  74.                 }else{
  75.                     stepRemain--;
  76.                     if(stepRemain==0){
  77.                         stepDist--;
  78.                     }
  79.                 }
  80.             }
  81.             step=stepDist;
  82.             j++;
  83.         }
  84.     }
  85.     return true;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement