home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / MONSTER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-13  |  5.9 KB  |  163 lines

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "rayspr.h"
  4. #include "sprfunc.h"
  5. #include "sfvars.h"
  6. #include "sprinter.h"
  7. #include "fixed.h"
  8. #include "rayfile.h"
  9. #include "message.h"
  10. #include "classes.h"
  11. #include "defobj.h"
  12. #include "getangle.h"
  13. #include "visible.h"
  14. #include "abs.h"
  15.  
  16. #define MAX_TIME_TO_LOSE_INTEREST 500
  17. #define GUN_SPEED 50
  18. #define MIN_OBJ_DIS (15*ONE)
  19. #define ANIM_SPEED 31
  20. #define ANIM_SHIFT 4
  21. #define GUN_HEIGHT (cur_object->type->eye_height-10)
  22. typedef struct MONSTER_DATA * pmonster_data;
  23. typedef struct MONSTER_DATA {
  24.    pobject cur_target;
  25.    pvector2 patrol;
  26.    short patrol_count;
  27.    short time_to_lose_interest;
  28.    short time_to_bullet;
  29.    BOOL need_new_point;
  30.    short cur_point;
  31. } monster_data;
  32.  
  33. void Monster_Create(pobject cur_object, long extra_data_offset);
  34. void Monster_Update(pobject cur_object, long update_num);
  35. ULONG Monster_Message(pobject send_obj, pobject receive_obj, ULONG message, pdata extra_data);
  36.  
  37. void Init_Monsters(func_index index) {
  38.    update_funcs[index]=Monster_Update;
  39.    load_extra_funcs[index]=Monster_Create;
  40.    message_funcs[index]=Monster_Message;
  41. }
  42.  
  43. void Monster_Create(pobject cur_object, long extra_data_offset)
  44. {
  45.    pmonster_data new_data;               
  46.    new_data=(pmonster_data)NewPtr(sizeof(monster_data));
  47.  
  48.    if (extra_data_offset==BAD_LOAD_OFFSET) {
  49.       new_data->patrol_count=0;
  50.       new_data->patrol=NULL;
  51.    } else {                
  52.       F_Seek(extra_data_offset);
  53.       F_Get_Short(new_data->patrol_count);
  54.       new_data->patrol=(pvector2)NewPtr(new_data->patrol_count * sizeof(vector2));
  55.       for (short cur_point=0; cur_point<new_data->patrol_count; cur_point++) {
  56.          F_Get_Long(new_data->patrol[cur_point].x);
  57.          F_Get_Long(new_data->patrol[cur_point].y);
  58.       } /* endfor */
  59.    }
  60.  
  61.    new_data->cur_point=0;
  62.    new_data->need_new_point=TRUE;
  63.    new_data->cur_target=NULL;
  64.    cur_object->extra_data=(pdata)new_data;
  65. }
  66.  
  67. void Monster_Update(pobject cur_object, long update_num) {
  68.    vector2 move_vec;
  69.    pmonster_data obj_data=(pmonster_data)cur_object->extra_data;
  70.  
  71.    if (obj_data->cur_target!=NULL) {
  72.       cur_object->angle=Get_Angle_Towards(obj_data->cur_target->x-cur_object->x,
  73.                                                                 obj_data->cur_target->y-cur_object->y);
  74.       if (obj_data->time_to_bullet==0) {
  75.          Create_Object(cur_object->x, cur_object->y, GUN_HEIGHT, 
  76.             cur_object->angle, BULLET_TYPE, cur_object, cur_object->team);
  77.          obj_data->time_to_bullet=GUN_SPEED;
  78.       } else {
  79.          obj_data->time_to_bullet--;
  80.       }                          
  81.  
  82.       if ( (ABS(obj_data->cur_target->x-cur_object->x) > MIN_OBJ_DIS) ||
  83.            (ABS(obj_data->cur_target->y-cur_object->y) > MIN_OBJ_DIS) ) {
  84.          move_vec.x=cur_object->type->stats.base_speed*rcos_table[cur_object->angle];
  85.          move_vec.y=cur_object->type->stats.base_speed*rsin_table[cur_object->angle];
  86.          Move_Object_Vec(cur_object, &move_vec);
  87.          cur_object->cur_frame=(update_num & ANIM_SPEED)>>ANIM_SHIFT;
  88.       }
  89.  
  90.       if ((obj_data->time_to_bullet<4) || (obj_data->time_to_bullet>(GUN_SPEED-4)) ) {
  91.          cur_object->cur_frame=4;
  92.       }
  93.  
  94.       if (--obj_data->time_to_lose_interest==0) {
  95.          Release_Object_Messages(obj_data->cur_target, cur_object);
  96.          obj_data->cur_target=NULL;
  97.       }
  98.    } else {
  99.  
  100.       if (obj_data->patrol_count!=0) {
  101.                           
  102.       if (obj_data->need_new_point) {
  103.          if ((++obj_data->cur_point)>=obj_data->patrol_count) {
  104.             obj_data->cur_point=0;
  105.          }
  106.                                  
  107.          cur_object->angle=Get_Angle_Towards(obj_data->patrol[obj_data->cur_point].x-
  108.                 cur_object->x, obj_data->patrol[obj_data->cur_point].y-cur_object->y);
  109.          obj_data->need_new_point=FALSE;
  110.          }
  111.  
  112.       move_vec.x=cur_object->type->stats.base_speed*rsin_table[cur_object->angle];
  113.       move_vec.y=cur_object->type->stats.base_speed*rcos_table[cur_object->angle];
  114.       
  115.       if ((obj_data->patrol[obj_data->cur_point].x-cur_object->x)<=move_vec.x) {
  116.          move_vec.x=obj_data->patrol[obj_data->cur_point].x-cur_object->x;
  117.          move_vec.y=obj_data->patrol[obj_data->cur_point].y-cur_object->y;
  118.          obj_data->need_new_point=TRUE;
  119.       } /* endif */
  120.  
  121.       if (Move_Object_Vec(cur_object, &move_vec)) {
  122.          obj_data->need_new_point=TRUE;
  123.       }
  124.  
  125.       cur_object->cur_frame=(update_num & ANIM_SPEED)>>ANIM_SHIFT;
  126.  
  127.       }
  128.  
  129.       obj_data->cur_target=Check_For_Visible_Enemies(cur_object);
  130.       if (obj_data->cur_target!=NULL) {
  131.          obj_data->time_to_bullet=GUN_SPEED;
  132.          obj_data->time_to_lose_interest=MAX_TIME_TO_LOSE_INTEREST;
  133.          Request_Object_Messages(obj_data->cur_target, cur_object);
  134.       }
  135.    }
  136. }
  137.  
  138. ULONG Monster_Message(pobject send_obj, pobject receive_obj, ULONG message, pdata extra_data) {
  139.    pmonster_data obj_data=(pmonster_data)receive_obj->extra_data;
  140.  
  141.    switch (message) {
  142.       case KILLED_MESSAGE:
  143.          if (send_obj==(obj_data->cur_target))
  144.             obj_data->cur_target=NULL;
  145.          return NORMAL_MESSAGE;
  146.       case HIT_OBJ:
  147.       case HIT_BY_OBJ:
  148.          if ((send_obj->type->obj_class==BULLET_CLASS)&&(send_obj->owner!=NULL)) {
  149.             if (obj_data->cur_target!=NULL)
  150.                Release_Object_Messages(obj_data->cur_target, receive_obj);
  151.             Request_Object_Messages(send_obj->owner, receive_obj);
  152.             obj_data->time_to_bullet=GUN_SPEED;
  153.             obj_data->time_to_lose_interest=MAX_TIME_TO_LOSE_INTEREST;
  154.             obj_data->cur_target=send_obj->owner;
  155.             return NORMAL_MESSAGE;
  156.          }
  157.          break;
  158.       default: 
  159.          return Default_Message(send_obj, receive_obj, message, extra_data);
  160.    } /* end switch */
  161. return Default_Message(send_obj, receive_obj, message, extra_data);  
  162. }
  163.