Vanilla List Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vector Torps




----- Original Message -----
From: James Cameron <quozl@us.netrek.org>
To: <mike@miridea.com>
Sent: Saturday, July 24, 1999 4:19 AM
Subject: Re: Vector Torps


| Mike Moore wrote:
| > I was bored and decided just for the hell of it to complete the
| > implementation of vector torps in Vanilla-2.9pl2 because I wanted to
| > see what it would look like.
|
| Excellent, well done.  Could you please submit the patches to
| vanilla-list@us.netrek.org so we can merge them into the code base?


I have several server hacks underway, so I'm not sending diffs.  All of the
needed changes can be made in ntserv/torp.c, so I will just send that file.
This implementation ignores the TVECTOR torp attribute and uses the
vectortorps global.

Note that hosers cannot aim vector torps very well at all.  You can stand
still and they will miss, unless they're right on top of you or not moving.
I'll work out the math and provide a patch eventually.

>>> ntserv/torp.c >>>

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

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

u_char
torpGetVectorTorpHeading(pdir, pspeed, tdir, tspeed)
     u_char pdir;
     int pspeed;
     u_char tdir;
     int tspeed;
{
  double                vsx, vsy, vtx, vty, ntx, nty, atan2();
  u_char                newheading;

  /* ship vector */
  vsx = (double) Sin[pdir] * pspeed;
  vsy = (double) Cos[pdir] * pspeed;

  /* torp vector */
  vtx = (double) Sin[tdir] * tspeed;
  vty = (double) Cos[tdir] * tspeed;

  /* new vector */
  ntx = vsx + vtx;
  nty = vsy + vty;

  /* new heading */
  if (ntx == 0.0 && nty == 0.0) /* It could happen with weird ship types in
.sysdef */
    newheading = 0;
  else
    newheading = ((atan2(ntx, nty) / M_PI) + 0.5) * 128.;

  return newheading;
}


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) Sin[pdir] * (pspeed * WARP1);
  vsy = (double) Cos[pdir] * (pspeed * WARP1);
  /* torp vector */
  vtx = (double) Sin[tdir] * (tspeed * WARP1);
  vty = (double) Cos[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

/* This will screw up the heading for fast torps.
  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 = (vectortorps) ?
    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    = (vectortorps) ?
    torpGetVectorTorpHeading(me->p_dir, me->p_speed, course,
myship->s_torpspeed) :
      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
}

<<< ntserv/torp.c <<<