import java.awt.*;
import java.applet.*;

public class Cable {
   
   int c1, c2;		/*computers connected */
   int x1,y1,x2,y2;	/*endpoints of line representing cable */
   int x, y;		/*current location of spark*/
   float fx, fy;		/*needed for line drawing*/
   int delay;		/*how much time until spark leaves*/
   boolean active;		/*is spark moving and from which end*/
   int index;
   JBill2 game;

   public Cable(JBill2 a) {
      game=a;
   }
   
   void setup() {
      c1 = game.RAND(0, game.net.units-1);
      do {
	 c2 = game.RAND(0, game.net.units-1);
      } while (c2 == c1);
      active = false;
      index = 0;
      delay = game.spark.delay(game.level);
      x1 = game.net.computers[c1].x + game.net.width/3;
      x2 = game.net.computers[c2].x + game.net.width/3;
      y1 = game.net.computers[c1].y + game.net.height/2;
      y2 = game.net.computers[c2].y + game.net.height/2;
   }

   void update () {
      if (active) {
	 if ((game.net.computers[c1].os == game.OS.WINGDOWS) ==
	     (game.net.computers[c2].os == game.OS.WINGDOWS))
	   active=false;
	 else if (game.net.computers[c1].os == game.OS.WINGDOWS ||
		  game.net.computers[c2].os == game.OS.WINGDOWS)
	   {
	      boolean dir;
	      int xdist, ydist,c;
	      float sx, sy;
	      dir = (game.net.computers[c2].os == game.OS.WINGDOWS);
	      if (dir)
		{xdist=x1-x; ydist=y1-y;}
	      else
		{xdist=x2-x; ydist=y2-y;}
	      sx = xdist >= 0 ? (float)1.0 : (float)-1.0;
	      sy = ydist >= 0 ? (float)1.0 : (float)-1.0;
	      xdist = Math.abs(xdist);
	      ydist = Math.abs(ydist);
	      if (xdist==0 && ydist==0) {
		 if (!dir) c=c2; else c=c1;
		 if (!game.net.computers[c].busy) {
		    if (game.net.computers[c].os == game.OS.OFF)
		      game.net.off--;
		    else
		      game.net.base--;
		    game.net.win++;
		    game.net.computers[c].os=game.OS.WINGDOWS;
		 }
		 active=false;
	      }
	      else if (game.MAX (xdist, ydist) < game.spark.speed) {
		 if (dir)
		   {x = x1; y = y1;}
		 else
		   {x = x2; y = y2;}
	      }
	      else {
		 fx+=(xdist*game.spark.speed*sx)/(xdist+ydist);
		 fy+=(ydist*game.spark.speed*sy)/(xdist+ydist);
		 x = (int)fx;
		 y = (int)fy;
	      }
	      index = 1 - index;
	   }
      }
      else {
	 if ((game.net.computers[c1].os == game.OS.WINGDOWS) ==
	     (game.net.computers[c2].os == game.OS.WINGDOWS))
	   delay = game.spark.delay(game.level);
	 else if (game.net.computers[c1].os == game.OS.WINGDOWS ||
		  game.net.computers[c2].os == game.OS.WINGDOWS)
	   {
	      if (delay>0) delay--;
	      else {
		 active = true;
		 if (game.net.computers[c1].os == game.OS.WINGDOWS)
		   {fx=x=x1; fy=y=y1;}
		 else
		   {fx=x=x2; fy=y=y2;}
	      }
	   }
      }
   }
   
   boolean onspark (int locx, int locy) {
      if (!active) return false;
      return (Math.abs(locx-x) < game.spark.width
	      && Math.abs(locy-y) < game.spark.height);
   }
   
   void draw() {
      int rx = x - game.spark.width/2;
      int ry = y - game.spark.height/2;
      game.ui.draw_line(x1,y1,x2,y2);
      if (active)
	game.ui.draw(game.spark.pictures[index], rx, ry);
   }
}
   
