Advertisement
TyrannisUmbra

LoS code

Mar 22nd, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.25 KB | None | 0 0
  1. public boolean canSee(Tile t){
  2.     int perception=getSpecial(PERCEPTION_ID);
  3.         int dist=location.distanceToTile(t);
  4.         int xDist=location.getX()-t.getX();
  5.         int yDist=location.getY()-t.getY();
  6.         int stepDist=1;
  7.         int stepRemain=0;
  8.         Tile checkTile;
  9.         //Check for out of sight range
  10.         if(!(dist<=perception)){
  11.             return false;
  12.         }
  13.     //Can always see your current location
  14.         if(t.equals(location)){
  15.             return true;
  16.         }
  17.         if(xDist>=0){ //positive x
  18.             if(yDist>=0){ //positive y
  19.                 if(Math.abs(xDist)>=Math.abs(yDist)){ //further along x axis
  20.                     stepDist=(xDist+1)/(yDist+1);
  21.                     stepRemain=(xDist+1)%(yDist+1);
  22.                     if(stepRemain==0){
  23.                         int j=0;
  24.                         int step=stepDist;
  25.                         for(int i=0;i<(xDist+1);i++){
  26.                             checkTile=Map.getCurrentMap().getTile(location.getX()-i,location.getY()-j);
  27.                             if(checkTile.equals(t)){
  28.                                 return true;
  29.                             }
  30.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  31.                                 return false;
  32.                             }
  33.                             step--;
  34.                             if(step==0){
  35.                                 step=stepDist;
  36.                                 j++;
  37.                             }
  38.                         }
  39.                     }else{
  40.                         int j=0;
  41.                         int step=stepDist;
  42.                         for(int i=0;i<(xDist+1);i++){
  43.                             checkTile=Map.getCurrentMap().getTile(location.getX()-i,location.getY()-j);
  44.                             if(checkTile.equals(t)){
  45.                                 return true;
  46.                             }
  47.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  48.                                 return false;
  49.                             }
  50.                             step--;
  51.                             if(step==0){
  52.                                 if(i<stepDist&&j==0){
  53.                                     stepDist++;
  54.                                 }else{
  55.                                     stepRemain--;
  56.                                     if(stepRemain==0){
  57.                                         stepDist--;
  58.                                     }
  59.                                 }
  60.                                 step=stepDist;
  61.                                 j++;
  62.                             }
  63.                         }
  64.                     }
  65.                 }else{ //further along y axis
  66.                     stepDist=(yDist+1)/(xDist+1);
  67.                     stepRemain=(yDist+1)%(xDist+1);
  68.                     if(stepRemain==0){
  69.                         int j=0;
  70.                         int step=stepDist;
  71.                         for(int i=0;i<(yDist+1);i++){
  72.                             checkTile=Map.getCurrentMap().getTile(location.getX()-j,location.getY()-i);
  73.                             if(checkTile.equals(t)){
  74.                                 return true;
  75.                             }
  76.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  77.                                 return false;
  78.                             }
  79.                             step--;
  80.                             if(step==0){
  81.                                 step=stepDist;
  82.                                 j++;
  83.                             }
  84.                         }
  85.                     }else{
  86.                         int j=0;
  87.                         int step=stepDist;
  88.                         for(int i=0;i<(yDist+1);i++){
  89.                             checkTile=Map.getCurrentMap().getTile(location.getX()-j,location.getY()-i);
  90.                             if(checkTile.equals(t)){
  91.                                 return true;
  92.                             }
  93.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  94.                                 return false;
  95.                             }
  96.                             step--;
  97.                             if(step==0){
  98.                                 if(i<stepDist&&j==0){
  99.                                     stepDist++;
  100.                                 }else{
  101.                                     stepRemain--;
  102.                                     if(stepRemain==0){
  103.                                         stepDist--;
  104.                                     }
  105.                                 }
  106.                                 step=stepDist;
  107.                                 j++;
  108.                             }
  109.                         }
  110.                     }
  111.                 }
  112.             }else{ //negative y
  113.                 if(Math.abs(xDist)>=Math.abs(yDist)){ //further along x axis
  114.                     stepDist=Math.abs((xDist+1)/(yDist-1));
  115.                     stepRemain=(xDist+1)%(yDist-1);
  116.                     if(stepRemain==0){
  117.                         int j=0;
  118.                         int step=stepDist;
  119.                         for(int i=0;i<(xDist+1);i++){
  120.                             checkTile=Map.getCurrentMap().getTile(location.getX()-i,location.getY()+j);
  121.                             if(checkTile.equals(t)){
  122.                                 return true;
  123.                             }
  124.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  125.                                 return false;
  126.                             }
  127.                             step--;
  128.                             if(step==0){
  129.                                 step=stepDist;
  130.                                 j++;
  131.                             }
  132.                         }
  133.                     }else{
  134.                         int j=0;
  135.                         int step=stepDist;
  136.                         for(int i=0;i<(xDist+1);i++){
  137.                             checkTile=Map.getCurrentMap().getTile(location.getX()-i,location.getY()+j);
  138.                             if(checkTile.equals(t)){
  139.                                 return true;
  140.                             }
  141.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  142.                                 return false;
  143.                             }
  144.                             step--;
  145.                             if(step==0){
  146.                                 if(i<stepDist&&j==0){
  147.                                     stepDist++;
  148.                                 }else{
  149.                                     stepRemain--;
  150.                                     if(stepRemain==0){
  151.                                         stepDist--;
  152.                                     }
  153.                                 }
  154.                                 step=stepDist;
  155.                                 j++;
  156.                             }
  157.                         }
  158.                     }
  159.                 }else{ //further along y axis
  160.                     stepDist=Math.abs((yDist-1)/(xDist+1));
  161.                     stepRemain=(yDist-1)%(xDist+1);
  162.                     if(stepRemain==0){
  163.                         int j=0;
  164.                         int step=stepDist;
  165.                         for(int i=0;i>(yDist-1);i--){
  166.                             checkTile=Map.getCurrentMap().getTile(location.getX()-j,location.getY()-i);
  167.                             if(checkTile.equals(t)){
  168.                                 return true;
  169.                             }
  170.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  171.                                 return false;
  172.                             }
  173.                             step--;
  174.                             if(step==0){
  175.                                 step=stepDist;
  176.                                 j++;
  177.                             }
  178.                         }
  179.                     }else{
  180.                         int j=0;
  181.                         int step=stepDist;
  182.                         for(int i=0;i>(yDist-1);i--){
  183.                             checkTile=Map.getCurrentMap().getTile(location.getX()-j,location.getY()-i);
  184.                             if(checkTile.equals(t)){
  185.                                 return true;
  186.                             }
  187.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  188.                                 return false;
  189.                             }
  190.                             step--;
  191.                             if(step==0){
  192.                                 if(i<stepDist&&j==0){
  193.                                     stepDist++;
  194.                                 }else{
  195.                                     stepRemain--;
  196.                                     if(stepRemain==0){
  197.                                         stepDist--;
  198.                                     }
  199.                                 }
  200.                                 step=stepDist;
  201.                                 j++;
  202.                             }
  203.                         }
  204.                     }
  205.                 }
  206.             }
  207.         }else{ //negative x
  208.             if(yDist>=0){ //positive y
  209.                 if(Math.abs(xDist)>=Math.abs(yDist)){ //further along x axis
  210.                     stepDist=Math.abs((xDist-1)/(yDist+1));
  211.                     stepRemain=(xDist-1)%(yDist+1);
  212.                     if(stepRemain==0){
  213.                         int j=0;
  214.                         int step=stepDist;
  215.                         for(int i=0;i>(xDist-1);i--){
  216.                             checkTile=Map.getCurrentMap().getTile(location.getX()-i,location.getY()-j);
  217.                             if(checkTile.equals(t)){
  218.                                 return true;
  219.                             }
  220.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  221.                                 return false;
  222.                             }
  223.                             step--;
  224.                             if(step==0){
  225.                                 step=stepDist;
  226.                                 j++;
  227.                             }
  228.                         }
  229.                     }else{
  230.                         int j=0;
  231.                         int step=stepDist;
  232.                         for(int i=0;i>(xDist-1);i--){
  233.                             checkTile=Map.getCurrentMap().getTile(location.getX()-i,location.getY()-j);
  234.                             if(checkTile.equals(t)){
  235.                                 return true;
  236.                             }
  237.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  238.                                 return false;
  239.                             }
  240.                             step--;
  241.                             if(step==0){
  242.                                 if(i<stepDist&&j==0){
  243.                                     stepDist++;
  244.                                 }else{
  245.                                     stepRemain--;
  246.                                     if(stepRemain==0){
  247.                                         stepDist--;
  248.                                     }
  249.                                 }
  250.                                 step=stepDist;
  251.                                 j++;
  252.                             }
  253.                         }
  254.                     }
  255.                 }else{ //further along y axis
  256.                     stepDist=Math.abs((yDist+1)/(xDist-1));
  257.                     stepRemain=(yDist+1)%(xDist-1);
  258.                     if(stepRemain==0){
  259.                         int j=0;
  260.                         int step=stepDist;
  261.                         for(int i=0;i<(yDist+1);i++){
  262.                             checkTile=Map.getCurrentMap().getTile(location.getX()+j,location.getY()-i);
  263.                             if(checkTile.equals(t)){
  264.                                 return true;
  265.                             }
  266.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  267.                                 return false;
  268.                             }
  269.                             step--;
  270.                             if(step==0){
  271.                                 step=stepDist;
  272.                                 j++;
  273.                             }
  274.                         }
  275.                     }else{
  276.                         int j=0;
  277.                         int step=stepDist;
  278.                         for(int i=0;i<(yDist+1);i++){
  279.                             checkTile=Map.getCurrentMap().getTile(location.getX()+j,location.getY()-i);
  280.                             if(checkTile.equals(t)){
  281.                                 return true;
  282.                             }
  283.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  284.                                 return false;
  285.                             }
  286.                             step--;
  287.                             if(step==0){
  288.                                 if(i<stepDist&&j==0){
  289.                                     stepDist++;
  290.                                 }else{
  291.                                     stepRemain--;
  292.                                     if(stepRemain==0){
  293.                                         stepDist--;
  294.                                     }
  295.                                 }
  296.                                 step=stepDist;
  297.                                 j++;
  298.                             }
  299.                         }
  300.                     }
  301.                 }
  302.             }else{ //negative y
  303.                 if(Math.abs(xDist)>=Math.abs(yDist)){ //further along x axis
  304.                     stepDist=Math.abs((xDist-1)/(yDist-1));
  305.                     stepRemain=(xDist-1)%(yDist-1);
  306.                     if(stepRemain==0){
  307.                         int j=0;
  308.                         int step=stepDist;
  309.                         for(int i=0;i>(xDist-1);i--){
  310.                             checkTile=Map.getCurrentMap().getTile(location.getX()-i,location.getY()+j);
  311.                             if(checkTile.equals(t)){
  312.                                 return true;
  313.                             }
  314.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  315.                                 return false;
  316.                             }
  317.                             step--;
  318.                             if(step==0){
  319.                                 step=stepDist;
  320.                                 j++;
  321.                             }
  322.                         }
  323.                     }else{
  324.                         int j=0;
  325.                         int step=stepDist;
  326.                         for(int i=0;i>(xDist-1);i--){
  327.                             checkTile=Map.getCurrentMap().getTile(location.getX()-i,location.getY()+j);
  328.                             if(checkTile.equals(t)){
  329.                                 return true;
  330.                             }
  331.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  332.                                 return false;
  333.                             }
  334.                             step--;
  335.                             if(step==0){
  336.                                 if(i<stepDist&&j==0){
  337.                                     stepDist++;
  338.                                 }else{
  339.                                     stepRemain--;
  340.                                     if(stepRemain==0){
  341.                                         stepDist--;
  342.                                     }
  343.                                 }
  344.                                 step=stepDist;
  345.                                 j++;
  346.                             }
  347.                         }
  348.                     }
  349.                 }else{ //further along y axis
  350.                     stepDist=Math.abs((yDist-1)/(xDist-1));
  351.                     stepRemain=(yDist-1)%(xDist-1);
  352.                     if(stepRemain==0){
  353.                         int j=0;
  354.                         int step=stepDist;
  355.                         for(int i=0;i>(yDist-1);i--){
  356.                             checkTile=Map.getCurrentMap().getTile(location.getX()+j,location.getY()-i);
  357.                             if(checkTile.equals(t)){
  358.                                 return true;
  359.                             }
  360.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  361.                                 return false;
  362.                             }
  363.                             step--;
  364.                             if(step==0){
  365.                                 step=stepDist;
  366.                                 j++;
  367.                             }
  368.                         }
  369.                     }else{
  370.                         int j=0;
  371.                         int step=stepDist;
  372.                         for(int i=0;i>(yDist-1);i--){
  373.                             checkTile=Map.getCurrentMap().getTile(location.getX()+j,location.getY()-i);
  374.                             if(checkTile.equals(t)){
  375.                                 return true;
  376.                             }
  377.                             if(checkTile!=null&&checkTile.getTerrainObject()!=null&&!checkTile.getTerrainObject().canSeeThrough()){
  378.                                 return false;
  379.                             }
  380.                             step--;
  381.                             if(step==0){
  382.                                 if(i<stepDist&&j==0){
  383.                                     stepDist++;
  384.                                 }else{
  385.                                     stepRemain--;
  386.                                     if(stepRemain==0){
  387.                                         stepDist--;
  388.                                     }
  389.                                 }
  390.                                 step=stepDist;
  391.                                 j++;
  392.                             }
  393.                         }
  394.                     }
  395.                 }
  396.             }
  397.         }
  398.         return true;
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement