turret.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #include "sound_man.hh"
  9. #include "objs/model_id.hh"
  10. #include "objs/model_draw.hh"
  11. #include "objs/turret.hh"
  12. #include "input.hh"
  13. #include "math/pi.hh"
  14. #include "math/angle.hh"
  15. #include "math/trig.hh"
  16. #include "g1_rand.hh"
  17. #include "resources.hh"
  18. #include "saver.hh"
  19. #include "map_cell.hh"
  20. #include "map.hh"
  21. #include "map_man.hh"
  22. #include "object_definer.hh"
  23. #include "lisp/lisp.hh"
  24. #include "objs/fire.hh"
  25. enum {DATA_VERSION=1};
  26. static g1_model_ref model_ref("aagun_base"),
  27. turret_ref("aagun_turret"),
  28. barrels_ref("aagun_barrels");
  29. static g1_object_type bullet;
  30. static i4_3d_vector turret_attach, muzzle_attach;
  31. void g1_turret_init()
  32. {
  33. bullet = g1_get_object_type("bullet");
  34. turret_attach.set(0,0,0);
  35. model_ref()->get_mount_point("Turret", turret_attach);
  36. muzzle_attach.set(0,0,0.15);
  37. model_ref()->get_mount_point("Muzzle", muzzle_attach);
  38. }
  39. g1_object_definer<g1_turret_class>
  40. g1_turret_def("turret", g1_object_definition_class::EDITOR_SELECTABLE, g1_turret_init);
  41. void g1_turret_class::setup(i4_float _x, i4_float _y, g1_object_class *creator)
  42. {
  43. x = x;
  44. y = y;
  45. player_num = creator->player_num;
  46. occupy_location();
  47. request_think();
  48. grab_old();
  49. }
  50. g1_turret_class::g1_turret_class(g1_object_type id,
  51. g1_loader_class *fp)
  52. : g1_map_piece_class(id,fp)
  53. {
  54. defaults = g1_turret_def.defaults;
  55. draw_params.setup(model_ref.id());
  56. allocate_mini_objects(2,"turret mini objects");
  57. muzzle = &mini_objects[0];
  58. muzzle->defmodeltype = barrels_ref.id();
  59. muzzle->position(muzzle_attach);
  60. muzzle->rotation.set(0,0,0);
  61. top = &mini_objects[1];
  62. top->defmodeltype = turret_ref.id();
  63. top->position(turret_attach);
  64. top->rotation.set(0,0,0);
  65. top->grab_old();
  66. if (fp && fp->check_version(DATA_VERSION))
  67. {
  68. fp->end_version(I4_LF);
  69. }
  70. else
  71. {
  72. health = defaults->health;
  73. }
  74. guard_angle = 0;
  75. guard_time = 0;
  76. radar_type=G1_RADAR_BUILDING;
  77. set_flag(BLOCKING |
  78. SELECTABLE |
  79. TARGETABLE |
  80. GROUND |
  81. HIT_GROUND |
  82. HIT_AERIAL |
  83. DANGEROUS |
  84. SHADOWED, 1);
  85. }
  86. void g1_turret_class::save(g1_saver_class *fp)
  87. {
  88. g1_map_piece_class::save(fp);
  89. fp->start_version(DATA_VERSION);
  90. fp->end_version();
  91. }
  92. void g1_turret_class::fire()
  93. {
  94. fire_delay = defaults->fire_delay;
  95. //recoil
  96. if (muzzle->offset.x != -0.2)
  97. muzzle->offset.x = -0.2;
  98. i4_3d_point_class tpos, bpos, bvel;
  99. i4_transform_class btrans;
  100. btrans.rotate_x_y_z(muzzle->rotation.x,muzzle->rotation.y,muzzle->rotation.z,i4_T);
  101. btrans.t = i4_3d_vector(muzzle->x,muzzle->y,muzzle->h);
  102. btrans.t += i4_3d_vector(x,y,h);
  103. btrans.transform(i4_3d_point_class(1.0,order?-0.15:0.15,0),bpos);
  104. btrans.transform_3x3(i4_3d_point_class(g1_resources.bullet_speed,0,0),bvel);
  105. order = !order;
  106. g1_fire(defaults->fire_type, this, attack_target.get(), bpos, bvel);
  107. }
  108. void g1_turret_class::think()
  109. {
  110. //undo the recoil
  111. if (muzzle->offset.x < 0)
  112. muzzle->offset.x += 0.01;
  113. if (muzzle->offset.x > 0)
  114. muzzle->offset.x = 0;
  115. find_target();
  116. if (health < defaults->health)
  117. health++;
  118. if (fire_delay>0)
  119. fire_delay--;
  120. pitch = 0;//groundpitch*cos(theta) - groundroll *sin(theta);
  121. roll = 0;//groundroll *cos(theta) + groundpitch*sin(theta);
  122. h = terrain_height;
  123. //aim the turet
  124. if (attack_target.valid())
  125. {
  126. request_think();
  127. i4_3d_point_class d,pos;
  128. lead_target(d);
  129. pos.set(x,y,h);
  130. d -= pos;
  131. //aim the turet
  132. guard_angle = i4_atan2(d.y,d.x);
  133. guard_pitch = i4_atan2(-d.z,sqrt(d.x*d.x+d.y*d.y));
  134. i4_normalize_angle(guard_angle);
  135. i4_normalize_angle(guard_pitch);
  136. int aimed=i4_F;
  137. i4_float dangle;
  138. dangle = i4_rotate_to(muzzle->rotation.z,guard_angle,defaults->turn_speed);
  139. aimed = (dangle<defaults->turn_speed && dangle>-defaults->turn_speed);
  140. dangle = i4_rotate_to(muzzle->rotation.y,guard_pitch,defaults->turn_speed);
  141. aimed &= (dangle<defaults->turn_speed && dangle>-defaults->turn_speed);
  142. top->rotation.z = muzzle->rotation.z;
  143. if (aimed)
  144. if (!fire_delay)
  145. fire();
  146. guard_time = 30;
  147. }
  148. else
  149. {
  150. request_think(); // move this to draw function
  151. if (guard_time<=0)
  152. {
  153. guard_angle = g1_float_rand(4)*i4_2pi();
  154. guard_pitch = -g1_float_rand(5)*i4_pi()*0.5;
  155. guard_time = 20;
  156. }
  157. int aimed;
  158. aimed = (i4_rotate_to(muzzle->rotation.z,guard_angle,defaults->turn_speed)==0.0);
  159. aimed &= (i4_rotate_to(muzzle->rotation.y,guard_pitch,defaults->turn_speed)==0.0);
  160. top->rotation.z = muzzle->rotation.z;
  161. if (aimed)
  162. if (guard_time>0)
  163. guard_time--;
  164. }
  165. }