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


public class Network {
   
   static final int MAX_COMPUTERS = 20;	/* max computers on screen */
   static final int NUM_SYS = 6;		/* number of computer types */
   Picture[] pictures=new Picture[NUM_SYS+1];		/* array of cpu pictures */
   int width, height;			/* size of cpu picture */
   int units;				/* number of cpus in network */
   int win, base, off;			/* number in each state */
   Computer[] computers= new Computer[MAX_COMPUTERS];	/* array of cpu info */
   Cable[] cables=new Cable[MAX_COMPUTERS];
   int ncables;
   JBill2 game;
   public Network(JBill2 a) {
      game=a;
      for (int i=0;i<MAX_COMPUTERS;i++) {
	 cables[i] = new Cable(game);
	 computers[i] = new Computer(game);
      }
   }
   
   /*sets up network for each level*/
   void setup() {
      int i;	
      units = on (game.level);
      for (i=0; i<units; i++)
	if (computers[i].setup(i)==0) {
	   units = i-1;
	   break;
	}
      base = units;
      off = win = 0;
      ncables = game.MIN(game.level, units/2);
      for (i=0; i<ncables; i++) cables[i].setup();
   }
   
   /*redraws the computers at their location with the proper image*/
   void draw () {
      int i;
      for (i=0; i<ncables; i++) cables[i].draw();
      for (i=0; i<units; i++) computers[i].draw();
   }
   
   void update () {
      for (int i=0; i<ncables; i++) cables[i].update();
   }
   
   void toasters () {
      for (int i=0; i<units; i++) {
	 computers[i].type = computers[i].TOASTER;
	 computers[i].os = game.OS.OFF;
      }
      ncables=0;
   }
   
   int on(int lev) {
      return game.MIN(8+lev, MAX_COMPUTERS);
   }

   void load_pix () {
      int i;
      String[] name = {"toaster", "maccpu", "nextcpu", "sgicpu", "suncpu",
	 "os2cpu", "bsdcpu"};
      for (i=0; i <= NUM_SYS; i++) {
	 pictures[i] = new Picture(game);
	 pictures[i].load(name[i]);
      }
      width = pictures[0].width;
      height = pictures[0].height;
   }
}
