/* torp.c -- Create a new torp with a given direction and attributes
 */
#include "copyright.h"

#include <stdio.h>
#include <sys/types.h>
#include "defs.h"
#include "struct.h"
#include "data.h"


int
torpGetVectorSpeed(pdir, pspeed, tdir, tspeed)
     u_char  pdir;
     int pspeed;
     u_char  tdir;
     int tspeed;
{
  double		vsx, vsy, vtx, vty, ntx, nty, sqrt();
  int			newspeed;

  /* ship vector */
  vsx = (double) Cos[pdir] * (pspeed * WARP1);
  vsy = (double) Sin[pdir] * (pspeed * WARP1);
  /* torp vector */
  vtx = (double) Cos[tdir] * (tspeed * WARP1);
  vty = (double) Sin[tdir] * (tspeed * WARP1);

  ntx = vsx + vtx;
  nty = vsy + vty;

  newspeed = nint(sqrt(ntx*ntx + nty*nty));
  MATH_ERR_CHECK((int)(ntx*ntx + nty*nty));

#if 0
  printf("vectortorp: ship (s=%d,d=%d,t=%d), result: %d\n", 
	 pspeed, pdir, tspeed, newspeed / WARP1); 
  fflush(stdout);
#endif
  if (newspeed > 20*WARP1)
    newspeed = 20*WARP1;

  return newspeed;
}


/*
 * If a set of given conditions are met, fire a single torp in direction
 * course.  Use different attributes 
 *
 * torp->t_fuse is the life of the torpedo.  It is set here based on
 * a random function.  Torps currently live two to five seconds.
 */
void
ntorp(course, attributes)
     u_char course;
     int attributes;   /* See struct.h */
{
  static LONG last_torp_fired_update = 0;
  struct torp *k;

  /*
   * Prevent player from firing more than one torp per update.  */
  if (me->p_updates == last_torp_fired_update)
    return;

  if (me->p_flags & PFWEP) {
    new_warning(25, "Torpedo launch tubes have exceeded maximum safe temperature!");
    if (!chaosmode)
      return;
  }
  if (me->p_ntorp == MAXTORP) {
    new_warning(26, "Our computers limit us to having 8 live torpedos at a time captain!");
    return;
  }
  if (me->p_fuel < myship->s_torpcost) {
    new_warning(27, "We don't have enough fuel to fire photon torpedos!");
    return;
  }
  if (me->p_flags & PFREPAIR) {
    new_warning(28, "We cannot fire while our vessel is in repair mode.");
    return;
  }
  if ((me->p_cloakphase) && (me->p_ship.s_type != ATT)) {
    new_warning(29, "We are unable to fire while in cloak, captain!");
    return;
  }


  /* 
   * If topgun mode,
   * Permit firing only if out of the front of the ship  */
  if (topgun && ((me->p_ship).s_type != STARBASE)) {
    int delta;
    if ((delta = ((int) me->p_dir - (int) course)) < 0)
      delta = -delta;
    if ((delta > topgun)  && (delta < (256 - topgun))) { 
      /* note: 128 = 180 degrees left/right */
      new_warning(30, "We only have forward mounted cannons.");
      return;
    }
  }


  /*
   * Find a free torp to use */
  for (k = firstTorpOf(me); k <= lastTorpOf(me); k++)
    if (k->t_status == TFREE)
      break;
#if 0
  /* Pseudo code for run-time debugging, if someone wants it. */
  if (k > lastTorpOf(me))
    error();
#endif

  last_torp_fired_update = me->p_updates;

  /*
   * Change my ship state: less fuel, more weapon temp  */
  me->p_ntorp++;
  me->p_fuel -= myship->s_torpcost;
  me->p_wtemp += (myship->s_torpcost / 10) - 10;	/* Heat weapons */

  /* 
   * Initialize torp: */
  k->t_status = TMOVE;
  k->t_type = TPHOTON;
  k->t_attribute = attributes;
  k->t_owner = me->p_no;
  k->t_x = me->p_x;
  k->t_y = me->p_y;
  k->t_turns  = myship->s_torpturns;
  k->t_damage = myship->s_torpdamage;
  k->t_gspeed = (attributes & TVECTOR) ?
    torpGetVectorSpeed(me->p_dir, me->p_speed, course, myship->s_torpspeed) :
      myship->s_torpspeed * WARP1;
  k->t_fuse   = myship->s_torpfuse + (random() % 20);
  k->t_dir    = course;
  k->t_war    = me->p_war;
  k->t_team   = me->p_team;
  k->t_whodet = NODET;
#if 0
  k->t_no = i;
  k->t_speed = k->t_gspeed / WARP1;
#endif
}

