/* Xwars
 * By Brett Reid
 * 
 * Xwars is a space shoot'em up, with ideas taken from Elite and Space Rogue.
 * 
 * Some programing ideas were taken from xtrek
 * The procedures in this file are to control lasers and laser damage.
 */

GLOBAL void lazers(me)
  int me;
{
  int targ_x,targ_y, targ_z, targ;
  int x, y, z, O, P;
  int laz_O, laz_P, laz_o;
  struct player player, target;
  int dummy;

  dummy = 0;
  player = &players[me];
  x = player->x;
  y = player->y;
  z = player->z;
  O = player->O;
  P = player->P;

  for (targ = 0; targ < MAXPLAYER; targ++){
    target = &players[targ];
    targ_x = target->x;
    targ_y = target->y;
    targ_z = target->z;
    if (magnitude3(targ_x - x, targ_y - y, targ_z - z) <= RANGE
        && !(magnitude3(targ_x, targ_y, targ_z) <= SAFEZONE) 
        && !(magnitude3(x, y, z) <= SAFEZONE) ) {
      laz_O = acos((targ_x - x)/ magnitude2(targ_x - x, targ_y - y));
      laz_o = asin((targ_y - y)/ magnitude2(targ_x - x, targ_y - y));
      laz_P = asin((targ_z - z)/ magnitude3(targ_x - x,targ_y - y,targ_z - z));
      if (abs(laz_O - O) < 45)
        if (abs(laz_P - P) < 45) {
          detailedlasers(me, targ); /* laser hit ship instead of pt. */
          dummy = 1;
          break;
        }
      if (magnitude3(targ_x - x,targ_y - y,targ_z - z) < 100)
        if (abs(laz_O - O) < 90)
          if (abs(laz_P - P) < 90) {
            detailedlasers(me, targ); /* laser hit ship instead of pt. */
            dummy = 1;
            break;
          }
    }
  }
  if (!dummy)
    lasermiss(me);
}

GLOBAL void detailedlasers(me, targ)
  
  int me, targ;
{
  struct player, target;
  int range;
  int longside;
  int shipwidth, shiplength, shipheight;
  int laz_x, laz_y, laz_z;
  int O, P;

  player = &players[me];
  target = &players[targ];
  O = player->O;
  P = player->P;
  x = player->x;
  y = player->y;
  z = player->z;
  targ_x = target->x;
  targ_y = target->y;
  targ_z = target->z;
  shipwidth = target->ship.shipwidth;
  shiplength = target->ship.shiplength;
  shipheight = target->ship.shipheight;

  range = magnitude3(targ_x - x, targ_y - y, targ_z - z);
O;
  wep_O = player->O - target->O;
  wep_P = player->P - target->P;
  laz_x = range * acos(wep_O) + (x - target->x);
  laz_y = range * asin(wep_O) + (y - target->y); 
  laz_z = range * asin(wep_P) + (z - target->z);
  if (abs(target->x - laz_x) < shiplength/2)
    if (abs(target->y - laz_y) < shipwidth/2)
      if (abs(target->z - laz_z) < shipheight/2) 
        damagebylasers(me, targ);
 }
  
 *  if (shipwidth > shplength)
 *    longside = shipwidth/2;
 *  else
 *    longside = shiplength/2;  
 *  laz_x = range * acos(O) + x;
 *  laz_y = range * asin(O) + y;
 *  laz_z = range * asin(P) + z;
 *  if (abs(targ_x - laz_x) < longsige)
 *    if (abs(targ_y - laz_y) < longsige)
 *      if (abs(targ_z - laz_z) < longsige) 
 *        damagebylasers(me, targ);
 * }

GLOBAL void damagebylasers(me, targ)

  int me, targ;
{
  int damage;
  struct player p, target;
  player = &players[me];
  target = &players[targ];

  damage = player->laserdamage;
  target->damage -= damage;
  damagepacket(targ,damage);
}


