home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / VISIBLE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-21  |  4.7 KB  |  168 lines

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "visible.h"
  4. #include "blockmap.h"
  5. #include "rayrend.h"
  6. #include "isect.h"
  7. #include "forever.h"
  8.  
  9. BOOL Check_Object_Visibility(pobject seeing_obj, pobject check_object);
  10. BOOL Do_Cone_Check(pobject seeing_obj, pobject check_obj);
  11. BOOL Do_Wall_Check(pobject seeing_obj, pobject check_obj);
  12.  
  13. pobject Check_For_Visible_Enemies(pobject seeing_obj) {
  14.    if (Check_Object_Visibility(seeing_obj, the_player))
  15.       return the_player;
  16.    else return NULL;
  17. }
  18.  
  19. BOOL Check_Object_Visibility(pobject seeing_obj, pobject check_obj) {
  20.    if (Do_Cone_Check(seeing_obj, check_obj)) {
  21.       return Do_Wall_Check(seeing_obj, check_obj);
  22.    } else return FALSE;
  23. }
  24.  
  25. BOOL Do_Cone_Check(pobject seeing_obj, pobject check_obj) {
  26.  
  27.    MYFIXED trans_x, trans_y;
  28.  
  29.    rotate_x=check_obj->x-seeing_obj->x;
  30.    rotate_y=check_obj->y-seeing_obj->y;
  31.    rotate_angle=seeing_obj->angle;
  32.  
  33.    trans_x=FixedRotateY();
  34.    trans_y=FixedRotateX();
  35.  
  36.    // is object in front of seeing object but not too far away
  37.    if ((trans_y<=0) || (trans_y>(seeing_obj->type->stats.sight_dis<<SHIFT)))
  38.       return FALSE;
  39.  
  40.    // is object within 90 degree view cone
  41.    if (ABS(trans_x)>trans_y)
  42.       return FALSE;
  43.  
  44.    return TRUE;
  45. }
  46.  
  47. BOOL Do_Wall_Check(pobject seeing_obj, pobject check_obj) {
  48.    vector2 source_vec, dest_vec, cur_vec;
  49.    pvector2 sl_vec, dl_vec;
  50.    short cur_block_x, cur_block_y, dest_block_x, dest_block_y, next_block_x, next_block_y;
  51.    long dist_major, dist_minor, next_x_line, next_y_line;
  52.    pline_list cur_line_list;
  53.    short cur_line_index;
  54.    BOOL y_done, x_done;
  55.  
  56.    source_vec.x=seeing_obj->x>>SHIFT;
  57.    source_vec.y=seeing_obj->y>>SHIFT;
  58.    dest_vec.x=check_obj->x>>SHIFT;
  59.    dest_vec.y=check_obj->y>>SHIFT;
  60.    cur_vec.x=seeing_obj->x;
  61.    cur_vec.y=seeing_obj->y;
  62.  
  63.    cur_block_x=Block_X(seeing_obj->x);
  64.    cur_block_y=Block_Y(seeing_obj->y);
  65.    dest_block_x=Block_X(check_obj->x);
  66.    dest_block_y=Block_Y(check_obj->y);
  67.            
  68.    FOREVER {
  69.       cur_line_list=Get_Block_Line_List(cur_block_x, cur_block_y);
  70.       for (cur_line_index=0; cur_line_index< cur_line_list->line_count; cur_line_index++) {
  71.          sl_vec=Vector_List+cur_line_list->lines[cur_line_index]->v[0];
  72.          dl_vec=Vector_List+cur_line_list->lines[cur_line_index]->v[1];
  73.          if (Intersect_Abs(&source_vec, &dest_vec, sl_vec, dl_vec)) {
  74.             return FALSE;
  75.          }
  76.       }
  77.  
  78.    if (dest_vec.x>source_vec.x) {
  79.       if (cur_block_x>=dest_block_x) {
  80.          x_done=TRUE;
  81.       } else {
  82.          x_done=FALSE;
  83.       }
  84.    } else {
  85.       if (cur_block_x<=dest_block_x) {
  86.          x_done=TRUE;
  87.       } else {
  88.          x_done=FALSE;
  89.       }
  90.    }
  91.  
  92.    if (dest_vec.y>source_vec.y) {
  93.       if (cur_block_y>=dest_block_y) {
  94.          y_done=TRUE;
  95.       } else {
  96.          y_done=FALSE;
  97.       }
  98.    } else {
  99.       if (cur_block_y<=dest_block_y) {
  100.          y_done=TRUE;
  101.       } else {
  102.          y_done=FALSE;
  103.       }
  104.    }
  105.  
  106.    if (x_done && y_done) {
  107.       break;
  108.    }
  109.  
  110.    if ( (dest_vec.x-source_vec.x) >= 0) {
  111.       next_block_x=cur_block_x+1;
  112.       next_x_line=Block_Right_Line(cur_vec.x);
  113.    } else {
  114.       next_block_x=cur_block_x-1;
  115.       next_x_line=Block_Left_Line(cur_vec.x)-1;
  116.    }
  117.  
  118.    if ( (dest_vec.y-source_vec.y) >= 0) {
  119.       next_block_y=cur_block_y+1;
  120.       next_y_line=Block_Top_Line(cur_vec.y);
  121.    } else {
  122.       next_block_y=cur_block_y-1;
  123.       next_y_line=Block_Bottom_Line(cur_vec.y)-1;
  124.    }
  125.  
  126.    if (ABS(dest_vec.x-source_vec.x)>ABS(dest_vec.y-source_vec.y)) {
  127.       
  128.       dist_minor=next_y_line-cur_vec.y;
  129.       dist_major=fixedmd(next_x_line-cur_vec.x,
  130.          dest_vec.y-source_vec.y, dest_vec.x-source_vec.x); 
  131.       
  132.       if (ABS(dist_major)<ABS(dist_minor)) {
  133.          cur_vec.x=next_x_line;
  134.          cur_vec.y+=dist_major;
  135.          cur_block_x=next_block_x;
  136.          cur_block_y=Block_Y(cur_vec.y);
  137.       } else {
  138.          cur_vec.y=next_y_line;
  139.          cur_vec.x+=fixedmd(dist_minor, dest_vec.x-source_vec.x, dest_vec.y-source_vec.y);
  140.          cur_block_y=next_block_y;
  141.          cur_block_x=Block_X(cur_vec.x);
  142.       }
  143.  
  144.    } else {
  145.    
  146.       dist_minor=next_x_line-cur_vec.x;
  147.       dist_major=fixedmd((next_y_line-cur_vec.y),
  148.          dest_vec.x-source_vec.x, dest_vec.y-source_vec.y); 
  149.       
  150.       if (ABS(dist_major)<ABS(dist_minor)) {
  151.          cur_vec.y=next_y_line;
  152.          cur_vec.x+=dist_major;
  153.          cur_block_y=next_block_y;
  154.          cur_block_x=Block_X(cur_vec.x);
  155.       } else {
  156.          cur_vec.x=next_x_line;
  157.          cur_vec.y+=fixedmd(dist_minor, dest_vec.y-source_vec.y, dest_vec.x-source_vec.x);
  158.          cur_block_x=next_block_x;
  159.          cur_block_y=Block_Y(cur_vec.y);
  160.       }
  161.  
  162.    }
  163.  
  164.    }
  165.  
  166. return TRUE;
  167. }
  168.