SPRMOVE.CPP
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:7k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "bspmove.h"
  4. #include "blockbsp.h"
  5. #include "rayspr.h"
  6. #include "blockmap.h"
  7. #include "forever.h"
  8. #include "abs.h"
  9. #include "sign.h"
  10. #include "fixed.h"
  11. #include "error.h"
  12. #include "collisio.h"
  13. #include "message.h"
  14. #include "objcol.h"
  15. #define MIN_DIS_FROM_WALL 1500
  16. #define MAX_LINE_DIS (128<<SHIFT)
  17. void Do_Move_Correction(pvector2 input_source_vec, pvector2 delta_vec);
  18. void Do_Block_Correction(short block_x,
  19.    short block_y, pvector2 dest_vec, plinedef closest_line, MYFIXED & min_dis);
  20. short Move_Object_Dir(pobject source_obj, angle_type move_angle, long distance)
  21. {
  22. vector2 delta_vec;
  23. delta_vec.x=(rcos_table[move_angle]*distance)>>SHIFT;
  24. delta_vec.y=(rsin_table[move_angle]*distance)>>SHIFT;
  25. return Move_Object_Vec(source_obj, &delta_vec);
  26. }
  27. short Move_Object_Vec(pobject source_obj, pvector2 delta_vec)
  28. {
  29. vector2 source_vec;
  30. source_vec.x=source_obj->x;
  31. source_vec.y=source_obj->y;
  32. wall_collision_info wal_col;
  33. wal_col.move_obj=source_obj;
  34. wal_col.delta_vec=delta_vec;
  35. checkwalls(&wal_col);
  36. if (wal_col.found_collision) {
  37.   if (Send_Specific_Message(NULL, source_obj, WALL_COLLISION, (pdata)&wal_col))
  38.     return 0;
  39. }
  40. obj_collision obj_col;
  41. obj_col.move_obj=source_obj;
  42. obj_col.delta_x=delta_vec->x;
  43. obj_col.delta_y=delta_vec->y;
  44. Check_Obj_Collision(&obj_col);
  45. if (obj_col.found_collision) {
  46.   if (Send_Specific_Message(obj_col.move_obj, obj_col.col_obj,
  47.      HIT_BY_OBJ, (pdata)&obj_col))
  48.      return 0;
  49.   if (Send_Specific_Message(obj_col.col_obj, obj_col.move_obj,
  50.      HIT_OBJ, (pdata)&obj_col))
  51.      return 0;
  52. delta_vec->x=obj_col.delta_x;
  53. delta_vec->y=obj_col.delta_y;
  54. source_obj->x+=delta_vec->x;
  55. source_obj->y+=delta_vec->y;
  56. USHORT new_block_x, new_block_y;
  57. new_block_x=Block_X(source_obj->x);
  58. new_block_y=Block_Y(source_obj->y);
  59. if ( (new_block_x!=source_obj->block_x) || (new_block_y!=source_obj->block_y) ) {
  60.    OL_Delete_Node(source_obj->block_node,
  61.       *Get_Block_Obj_List(source_obj->block_x, source_obj->block_y));
  62.    OL_Push_Node(source_obj->block_node,
  63.       *Get_Block_Obj_List(new_block_x, new_block_y));
  64.    source_obj->block_x=new_block_x;
  65.    source_obj->block_y=new_block_y;
  66.    }
  67. long starting_node_index;
  68. // Get a starting index to do bsp seaching
  69. starting_node_index=Get_Closest_Node(source_obj->x, source_obj->y);
  70. // Get the ssector
  71. pssector new_ss=Get_Object_SSector(source_obj, starting_node_index);
  72. // Have we changed sub sectors?
  73. if (new_ss!=source_obj->cur_ss) {
  74.   // Then move object from one sub sectors draw list to another's
  75.   OL_Delete_Node(source_obj->ss_node, source_obj->cur_ss->objects);
  76.   source_obj->cur_ss->num_objects--;
  77.   OL_Push_Node(source_obj->ss_node,new_ss->objects);
  78.   new_ss->num_objects++;
  79.   source_obj->cur_ss=new_ss;
  80.   // Have we changed sectors?
  81.   psector new_sec=Get_Sec_From_SSec(new_ss);
  82.   if (new_sec!=source_obj->cur_sec) {
  83.      // Then update object's sector z value
  84.      source_obj->type->Update_Z(source_obj, new_sec);
  85.      source_obj->cur_sec=new_sec;
  86.   }
  87. }
  88. return 0;
  89. }
  90. void Do_Move_Correction(pvector2 input_source_vec, pvector2 delta_vec) {
  91. vector2 source_vec, base_vec, old_vec;
  92. short cur_block_x, cur_block_y, dest_block_x, dest_block_y, next_block_x, next_block_y;
  93. long next_x_line, next_y_line, dist_major, dist_minor;
  94. source_vec.x=input_source_vec->x;
  95. source_vec.y=input_source_vec->y;
  96. base_vec.x=source_vec.x;
  97. base_vec.y=source_vec.y;
  98. old_vec.x=base_vec.x;
  99. old_vec.y=base_vec.y;
  100. FOREVER {
  101.    Do_Block_Correction(cur_block_x, cur_block_y, NULL,NULL,dist_major);
  102.    if ( (base_vec.x!=old_vec.x) || (base_vec.y!=old_vec.y) ) {
  103.       old_vec.x=base_vec.x;
  104.       old_vec.y=base_vec.y;
  105.       source_vec.x=base_vec.x;
  106.       source_vec.y=base_vec.y;
  107.       cur_block_x=Block_X(source_vec.x);
  108.       cur_block_y=Block_Y(source_vec.y);
  109.    }
  110.    dest_block_x=Block_X(base_vec.x+delta_vec->x);
  111.    dest_block_y=Block_Y(base_vec.y+delta_vec->y);
  112.    if ( (cur_block_x==dest_block_x) && (cur_block_y==dest_block_y) ) { 
  113.       break;
  114.    }
  115.    if (delta_vec->x >= 0) {
  116.       next_block_x=cur_block_x+1;
  117.       next_x_line=Block_Right_Line(source_vec.x);
  118.    } else {
  119.       next_block_x=cur_block_x-1;
  120.       next_x_line=Block_Left_Line(source_vec.x);
  121.    }
  122.    if (delta_vec->y >= 0) {
  123.       next_block_y=cur_block_y+1;
  124.       next_y_line=Block_Top_Line(source_vec.y);
  125.    } else {
  126.       next_block_y=cur_block_y-1;
  127.       next_y_line=Block_Bottom_Line(source_vec.y);
  128.    }
  129.    if (ABS(delta_vec->x)>ABS(delta_vec->y)) {
  130.       
  131.       dist_minor=(next_y_line-source_vec.y)<<SHIFT;
  132.       dist_major=fixedmd((next_x_line-source_vec.x)<<SHIFT, 
  133.          delta_vec->y, delta_vec->x); 
  134.       
  135.       if (ABS(dist_major)<ABS(dist_minor)) {
  136.          source_vec.x=next_x_line;
  137.          source_vec.y+=(dist_major>>SHIFT);
  138.          cur_block_x=next_block_x;
  139.       } else {
  140.          source_vec.y=next_y_line;
  141.          source_vec.x+=fixedmd(dist_minor, delta_vec->x, delta_vec->y)>>SHIFT;
  142.          cur_block_y=next_block_y;
  143.       }
  144.    } else {
  145.    
  146.       dist_minor=(next_x_line-source_vec.x)<<SHIFT;
  147.       dist_major=fixedmd((next_y_line-source_vec.y)<<SHIFT, 
  148.          delta_vec->x, delta_vec->y); 
  149.       
  150.       if (ABS(dist_major)<ABS(dist_minor)) {
  151.          source_vec.y=next_y_line;
  152.          source_vec.x+=(dist_major>>SHIFT);
  153.          cur_block_y=next_block_y;
  154.       } else {
  155.          source_vec.x=next_x_line;
  156.          source_vec.y+=fixedmd(dist_minor, delta_vec->y, delta_vec->x)>>SHIFT;
  157.          cur_block_x=next_block_x;
  158.       }
  159.    }
  160. }  
  161. // make delta's from original position
  162. delta_vec->x=(source_vec.x+delta_vec->x)-input_source_vec->x;
  163. delta_vec->y=(source_vec.y+delta_vec->y)-input_source_vec->y;
  164. }
  165. void Do_Block_Correction(short block_x,
  166.    short block_y, pvector2 dest_vec, plinedef closest_line, MYFIXED & min_dis)
  167. {
  168.    pline_list cur_line_list;
  169.    plinedef cur_line;
  170.    short cur_line_index;
  171.    long x1,x2,y1,y2, dest_dis, line_relation, line_dis;
  172.    cur_line_list=Get_Block_Line_List(block_x, block_y);
  173.       closest_line=NULL;
  174.       for (cur_line_index=0; cur_line_index< cur_line_list->line_count; cur_line_index++) {
  175.  
  176.          cur_line=cur_line_list->lines[cur_line_index];
  177.          x1=Vector_List[cur_line->v[0]].x;
  178.          y1=Vector_List[cur_line->v[0]].y;
  179.          x2=Vector_List[cur_line->v[1]].x;
  180.          y2=Vector_List[cur_line->v[1]].y;
  181.          //line_dis=cur_line->distance;
  182.          line_relation=fixeddiv( ((y1-y2)*(y1-dest_vec->y)-(x2-x1)*(x1-dest_vec->x)) << SHIFT,
  183.                 fixedmult(line_dis,line_dis));
  184.          if (line_relation<0 || line_relation>ONE)
  185.             dest_dis=MAX_LINE_DIS;
  186.          else {
  187.             dest_dis=fixeddiv(((x2-x1)*(dest_vec->y-y2)-(y1-y2)*(dest_vec->x-x2))<<SHIFT, line_dis);
  188.          }
  189.          // have we found a closer intersecting line?
  190.          if (dest_dis<min_dis) {
  191.             closest_line=cur_line;
  192.             min_dis=dest_dis;
  193.             }
  194.       } /* endfor */
  195. }
  196.