package Generator;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class Generator extends JFrame {
  JSplitPane     mainPane;
  QuestionPanel  qp;
  ResultsPanel   rp;
  JTextField     status;

  public Generator() {
    super("Buck's G.A.S.L.I.G.H.T. Generator");
	 this.addWindowListener (new MyWindowListener());

	 setSize(500, 760);

	 status = new JTextField ("Status");
	 status.setEditable(false);
	 getContentPane().add ("South", status);
	 
	 rp = new ResultsPanel(status);
	 JScrollPane bottomPane = new JScrollPane(rp);

    qp = new QuestionPanel(status, rp.getTextArea());
	 JScrollPane topPane = new JScrollPane(qp);

	 mainPane = new JSplitPane (JSplitPane.VERTICAL_SPLIT, topPane, bottomPane);
	 getContentPane().add (mainPane);

	 pack();
  } /* Constructor */

  public static void main(String args[]) {
	 Generator me = new Generator();
	 me.setVisible(true);
  } /* main() */

} /* class Generator */

//********************************************************//

class QuestionPanel extends JPanel {
  JTextField   status;
  JTextArea    text;

  JLabel[]     paxLabel;
  JComboBox[]  paxList;
  JTextField[] paxNames; 
  JComboBox[]  paxWeapons;
  JLabel[]     weaponLabel;
  JComboBox[]  weaponList;
  JTextField[] weaponNames; 
  JLabel[]     vehicleLabel;
  JComboBox[]  vehicleList;
  JTextField[] vehicleNames; 
  JComboBox[]  vehicleWeapons;

  PersonnelRecord paxRecord;
  VehicleRecord   vehRecord;
  WeaponRecord    wpnRecord;

  JButton exitButton, genButton, clearButton;

  public QuestionPanel(JTextField status, JTextArea text) {
    int count;

	 this.status = status;
	 this.text   = text;

	 wpnRecord = new WeaponRecord(Constants.numWeapons);
    paxRecord = new PersonnelRecord(10, wpnRecord);
	 vehRecord = new VehicleRecord(Constants.numVehicles, paxRecord, wpnRecord);

    setLayout (new GridLayout (18,4));

    System.err.println ("QuestionPanel (Constructor): " +
	                     "Creating the QuestionPanel object...");

	 add (new JLabel ("Element"));
	 add (new JLabel ("Rating"));
	 add (new JLabel ("Name"));
	 add (new JLabel ("Weapon"));

    paxLabel   = new JLabel[Constants.numUnattached];
	 paxList    = new JComboBox[Constants.numUnattached];
	 paxNames   = new JTextField[Constants.numUnattached];
	 paxWeapons = new JComboBox[Constants.numUnattached];
	 for (count=0; count<Constants.numUnattached; count++) {
	   paxLabel[count] = new JLabel ("Unattached Main Char.");
		add (paxLabel[count]);
		paxList[count] = new JComboBox(Constants.unattachedTypes);
		add (paxList[count]);
		paxNames[count] = new JTextField("Unnamed");
		add (paxNames[count]);
		paxWeapons[count]= new JComboBox(Constants.weaponsList);
		add (paxWeapons[count]);
	 }

    vehicleLabel   = new JLabel[Constants.numVehicles];
	 vehicleList    = new JComboBox[Constants.numVehicles];
	 vehicleNames   = new JTextField[Constants.numVehicles];
	 vehicleWeapons = new JComboBox[Constants.numVehicles];
	 for (count=0; count<Constants.numVehicles; count++) {
	   vehicleLabel[count] = new JLabel ("Vehicle ");
		add (vehicleLabel[count]);
		vehicleList[count] = new JComboBox(Constants.vehicleTypes);
		add (vehicleList[count]);
		vehicleNames[count] = new JTextField("Unnamed");
		add (vehicleNames[count]);
		vehicleWeapons[count] = new JComboBox(Constants.weaponsList);
		add (vehicleWeapons[count]);
	 }

	 weaponLabel = new JLabel[Constants.numWeapons];
	 weaponList  = new JComboBox[Constants.numWeapons];
	 weaponNames = new JTextField[Constants.numWeapons];
	 for (count=0; count<Constants.numWeapons; count++) {
	   weaponLabel[count] = new JLabel ("Weapon ");
		add (weaponLabel[count]);
		weaponList[count] = new JComboBox(Constants.weaponTypes);
		add (weaponList[count]);
		weaponNames[count] = new JTextField("Unnamed");
		add (weaponNames[count]);
		add (new JButton(""));
	 }

	 genButton = new JButton ("Generate");
	 genButton.addActionListener (new genButtonL());

	 clearButton = new JButton ("Clear");
	 clearButton.addActionListener (new clearButtonL());

	 exitButton = new JButton ("Quit");
	 exitButton.addActionListener (
	   new ActionListener() {
		  public void actionPerformed (ActionEvent e) {
		    System.err.println ("QuestionPanel (Quit): Exiting Program...");
			 System.exit(0);
		  }
		}
	 );

	 add (genButton);
	 add (clearButton);
	 add (exitButton);

	 setVisible(true);
  } /* Constructor */

  class genButtonL implements ActionListener {
    public void actionPerformed (ActionEvent e) {
	   int count;
		int index;

	   System.err.println ("Generate some stuff...");
		status.setText("Generating stuff...");

		for (count=0; count<Constants.numUnattached; count++) {
		  index = paxList[count].getSelectedIndex();
		  if (index > 0) {
		    System.err.println ("  Generating Main Character of type " + 
			 							Constants.ratingNames[index]);
			 status.setText ("Generating Main Character of type " + 
			 					  Constants.ratingNames[index]);
			 paxRecord.makeCharacter(index, paxNames[count].getText(),
			                         paxWeapons[count].getSelectedIndex());
		  } /* if */
		} /* for*/

		for (count=0; count<Constants.numVehicles; count++) {
		  index = vehicleList[count].getSelectedIndex();
		  if (index > 0) {
		    System.err.println ("  Generating Vehicle of type " + 
			 							Constants.vehicleTypes[index]);
		    status.setText ("Generating Vehicle of type " + 
			 					  Constants.vehicleTypes[index]);
			 vehRecord.makeVehicle (index, vehicleNames[count].getText());
		  } /* if */
		} /* for*/

		for (count=0; count<Constants.numWeapons; count++) {
		  index = weaponList[count].getSelectedIndex();
		  if (index > 0) {
		    System.err.println ("  Generating Weapon of type " + 
			 							Constants.weaponTypes[index]);
		    status.setText ("Generating Weapon of type " + 
			 					  Constants.weaponTypes[index]);
	       wpnRecord.makeWeapon (index, weaponNames[count].getText());
		  } /* if */
		} /* for*/

		displayResults (paxRecord, wpnRecord, vehRecord, text);

		status.setText("Done generating stuff.");

	 } /* actionPerformed() */
  } /* class genButtonL */

  class clearButtonL implements ActionListener {
    public void actionPerformed (ActionEvent e) {
      paxRecord.initialize();
		vehRecord.initialize();
		wpnRecord.initialize();
	 }
  } /* class clearButtonL */

  protected void displayResults (PersonnelRecord pax, WeaponRecord wpn, 
                                 VehicleRecord veh, JTextArea text) {
    System.err.println ("  Displaying results of generation...");
	 status.setText("Displaying results of generation...");
	 text.setText("");

	 pax.displayResults(text);
	 wpn.displayResults(text);
	 veh.displayResults(text);

	 pax.displayResults(null);
	 wpn.displayResults(null);
	 veh.displayResults(null);

	 text.append("G.A.S.L.I.G.H.T.\n");
	 text.append("by Christopher Palmer and John R. \"Buck\" Surdu");

  } /* displayResults() */


} /* class QuestionPanel */

//********************************************************//

class ResultsPanel extends JPanel {
  JScrollPane results;
  JTextArea text;
  JTextField status;

  public ResultsPanel (JTextField s) {
    System.err.println ("ResultsPanel (Constructor): " +
	                     "Creating ResultsPanel...");

	 status = s;

	 text = new JTextArea (14,60);
	 text.append("");
	 add (text);
  }

  JTextArea getTextArea() { return text; }

} /* ResultsPanel */

//************************************************************//
//** Record Containers for Personnel, Vehicles, and Weapons **//
//************************************************************//

class PersonnelRecord {
  String[] name;
  int[] rating;
  String[] notes;
  int[] shoot;
  int[] scuffle;
  int[] save;
  int[] weapon;
  int numPersonnel;
  int numPersonnelAssigned = 0;

  Random random;
  WeaponRecord wpnRecord;

  public PersonnelRecord (int num, WeaponRecord wpn) {
    if (num <= 0) {
	   System.err.println ("PersonnelRecord (constructor): " +
								  "You tried to create a personnel record with too few " +
								  "elements.  You must have at least one person.");
		System.exit(-1);
	 }
	 else if (num > 10) {
	   System.err.println ("PersonnelRecord (constructor): " +
								  "You tried to create a personnel record with too many " +
								  "elements.  You may have no more than ten personnel.");
		System.exit(-1);
	 }

	 wpnRecord = wpn;

	 initialize();

	 System.err.println ("PersonnelRecord (constructor): " +
	 	                  "Created PersonnelRecrod for " + numPersonnel + 
								" personnel");

  } /* Constructor */

  protected void initialize() {
    numPersonnelAssigned = 0;

	 numPersonnel = 20;
	 name    = new String [numPersonnel];
	 rating  = new int    [numPersonnel];
	 notes   = new String [numPersonnel];
	 shoot   = new int    [numPersonnel];
	 scuffle = new int    [numPersonnel];
	 save    = new int    [numPersonnel];
	 weapon  = new int    [numPersonnel];

	 random = new Random();
  } /* initialize() */

  protected int makeCharacter (int type) {
    return makeCharacter (type, "Unassigned", "Unnamed", random.nextInt(2));
  }

  protected int makeCharacter (int type, String name) {
    return makeCharacter (type, "Unassigned", name, random.nextInt(2));
  }

  protected int makeCharacter (int type, String note, String name) {
    return makeCharacter (type, note, name, random.nextInt(2));
  }

  protected int makeCharacter (int type, String name, int wpnType) {
    return makeCharacter (type, "Unassigned", name, wpnType);
  }

  protected int makeCharacter (int type, String note, String n, int wpnType) {
    if (numPersonnelAssigned >= numPersonnel) return -1;

	 if (n.equals("Unnamed"))
      name[numPersonnelAssigned]   = new String ("Character " + (numPersonnelAssigned+1));
	 else 
	   name[numPersonnelAssigned]   = new String (n);
	 rating[numPersonnelAssigned] = type;
	 notes[numPersonnelAssigned]  = new String (note);
	 shoot[numPersonnelAssigned]  = Constants.attributes[type][random.nextInt(20)];
	 scuffle[numPersonnelAssigned]= Constants.attributes[type][random.nextInt(20)];
	 save[numPersonnelAssigned]   = Constants.attributes[type][random.nextInt(20)];
	 if (wpnType > Constants.wpnListOffset)
	   weapon[numPersonnelAssigned] = wpnType - Constants.wpnListOffset;
	 else
	   weapon[numPersonnelAssigned] = wpnRecord.makeWeapon(wpnType);

    //displayCharacter(numPersonnelAssigned);

	 numPersonnelAssigned++;
	 return (numPersonnelAssigned-1);
  } /* makeCharacter */

  void displayCharacter (int index) {
    System.err.println ("      CHARACTER " + index);
    System.err.println ("      Name\t"    + name   [index]);
    System.err.println ("      Rating\t"  + Constants.ratingNames[rating[index]]);
    System.err.println ("      Notes\t"   + notes  [index]);
    System.err.println ("      Shoot\t"   + shoot  [index]);
    System.err.println ("      Scuffle\t" + scuffle[index]);
    System.err.println ("      Save\t"    + save   [index]);
	 System.err.println ("      Weapon\t"  + weapon [index]);
  }

  void displayResults (JTextArea text) {
    String lines[] = new String[8];
	 int count;
	 
	 lines[0] = new String ("Number:\t");
	 lines[1] = new String ("Name:\t");
	 lines[2] = new String ("Rating:\t");
	 lines[3] = new String ("Notes:\t");
	 lines[4] = new String ("Shoot:\t");
	 lines[5] = new String ("Scuffle:\t");
	 lines[6] = new String ("Save:\t");
	 lines[7] = new String ("Weapon:\t");

	 for (count = 0; count < numPersonnelAssigned; count++) {
	   if (name[count] != null) {
		  lines[0] += count + "\t";
		  lines[1] += name[count] + "\t";
		  lines[2] += Constants.ratingNames[rating[count]] + "\t";
		  lines[3] += notes[count] + "\t";
		  lines[4] += shoot[count] + "\t";
		  lines[5] += scuffle[count] + "\t";
		  lines[6] += save[count] + "\t";
		  lines[7] += (weapon[count]+1) + "\t";
		}
	 } /* for */

	 if (text == null) {
	   System.out.println("PERSONNEL RECORD:\n");
	   for (count = 0; count < 8; count++)
		  System.out.println(lines[count]);
		System.out.println ("\n");
	 } else {
  	   text.append("PERSONNEL RECORD:\n");
      for (count = 0; count < 8; count++)
	     text.append(lines[count] + "\n");
	   text.append("\n\n");
	 }

  } /* displayResults() */

} /* class PersonnelRecord */

class VehicleRecord {
  String[] name;
  int[]    shoot;
  int[]    scuffle;
  int[]    numCrew;
  int[]    leader;
  int[]    weapon;
  int[]    save;
  int[]    speed;
  int[]    start;
  int[]    sustain;
  int[]    spin;
  String[] vehType;

  int numVehiclesCreated = 0;
  PersonnelRecord paxRecord;
  WeaponRecord wpnRecord;

  Random random;

  public VehicleRecord (int num, PersonnelRecord pax, WeaponRecord wpn) {
    if (num <= 0) {
	   System.err.println ("VehicleRecord (constructor): " +
								  "You tried to create a personnel record with too few " +
								  "elements.  You must have at least one person.");
		System.exit(-1);
	 }
	 else if (num > Constants.numVehicles) {
	   System.err.println ("PersonnelRecord (constructor): " +
								  "You tried to create a personnel record with too many " +
								  "elements.  You may have no more than " +
							     Constants.numVehicles + " vehicles.");
		System.exit(-1);
	 }

	 paxRecord = pax;
	 wpnRecord = wpn;

    initialize();
	 
	 System.err.println ("PersonnelRecord (constructor): " +
	 	                  "Created VehicleRecrod for " + Constants.numVehicles + 
								" vehicles");
  } /* Constructor */

  protected void initialize() {
    numVehiclesCreated = 0;

    name    = new String[Constants.numVehicles];
	 vehType = new String[Constants.numVehicles];
    shoot   = new int[Constants.numVehicles];
    scuffle = new int[Constants.numVehicles];
    numCrew = new int[Constants.numVehicles];
    leader  = new int[Constants.numVehicles];
    weapon  = new int[Constants.numVehicles];
    save    = new int[Constants.numVehicles];
    speed   = new int[Constants.numVehicles];
    start   = new int[Constants.numVehicles];
    sustain = new int[Constants.numVehicles];
    spin    = new int[Constants.numVehicles];

	 random = new Random();
  } /* initialize() */

  protected int makeVehicle (int type) {
    return makeVehicle (type, "Unnamed", (random.nextInt(2)+2));
  }

  protected int makeVehicle (int type, int wpnType) {
    return makeVehicle (type, "Unnamed", wpnType);
  }

  protected int makeVehicle (int type, String n) {
     return makeVehicle (type, n, (random.nextInt(Constants.wpnListOffset)+1));
  }
 
  
  protected int makeVehicle (int type, String n, int wpnType) {
    boolean powered;
	 int     speedRow;

    if (numVehiclesCreated >= Constants.numVehicles) return -1;

	 vehType[numVehiclesCreated] = new String (Constants.vehicleTypes[type]);

	 if (n.equals("Unnamed"))
      name[numVehiclesCreated]  = new String("Vehicle " + numVehiclesCreated);
	 else
	   name[numVehiclesCreated]  = new String(n);

    shoot[numVehiclesCreated]   = Constants.attributes[0][random.nextInt(20)];
    scuffle[numVehiclesCreated] = Constants.attributes[0][random.nextInt(20)];
    numCrew[numVehiclesCreated] = random.nextInt(4)+1;


	 double numberRolled = random.nextDouble();
	 if (numberRolled <= 0.75) {
	   /** generate a leader with a common weapon **/
      leader[numVehiclesCreated]= 
		  paxRecord.makeCharacter(random.nextInt(4)+1,
	                             name[numVehiclesCreated],
										  "Veh. Cdr.",
										  (random.nextInt(8)+5+Constants.wpnListOffset));
	 } else {
      leader[numVehiclesCreated]= 
		  paxRecord.makeCharacter(random.nextInt(4)+1,
	                             name[numVehiclesCreated],
										  "Veh. Cdr.");
	 } /* else */

	 if (wpnType > Constants.wpnListOffset)
	   weapon[numVehiclesCreated] = wpnType - Constants.wpnListOffset;
	 else
	   weapon[numVehiclesCreated] = wpnRecord.makeWeapon(wpnType);
    
	 save[numVehiclesCreated]     = Constants.saveNumber[random.nextInt(20)];

	 if ((type == 1) || (type == 2) || (type==3)) powered = true;
	 else powered = false;
	 if ((type == 1) || (type == 4)) speedRow = 0;
	 else if ((type == 2) || (type == 5)) speedRow = 1;
	 else speedRow = 2;
	 if (powered)
      speed[numVehiclesCreated] = Constants.speedAttributes[speedRow][random.nextInt(20)] +
		                            Constants.speedAttributes[speedRow][random.nextInt(20)];
    else
	   speed[numVehiclesCreated] = Constants.speedAttributes[speedRow][random.nextInt(20)];

    start[numVehiclesCreated]   = Constants.startNumber[random.nextInt(20)];
    sustain[numVehiclesCreated] = Constants.sustainNumber[random.nextInt(20)];
    spin[numVehiclesCreated]    = Constants.spinNumber[random.nextInt(20)];

    //displayVehicle(numVehiclesCreated);

	 numVehiclesCreated++;
	 return (numVehiclesCreated-1);
  } /* makeCharacter */

  void displayVehicle (int index) {
    System.err.println ("      VEHICLE " + index);
	 System.err.println ("      Name\t"    + name[index]);
	 System.err.println ("      Shoot\t"   + shoot[index]);
  	 System.err.println ("      Scuffle\t" + scuffle[index]);
  	 System.err.println ("      # Crew\t"  + numCrew[index]);
	 //if (leader[index] >= 0)
  	 //  paxRecord.displayCharacter(leader[index]);
	 //else System.err.println ("      Leader\tNONE!");
	 System.err.println ("      Leader\t"  + leader[index]);
  	 System.err.println ("      Weapon\t"  + weapon[index]);
    System.err.println ("      Save\t"    + save[index]);
    System.err.println ("      Speed\t"   + speed[index]);
	 System.err.println ("      Start\t"   + start[index]);
	 System.err.println ("      Sustain\t" + sustain[index]);
	 System.err.println ("      Spin\t"    + spin[index]);
  } /* displayVehicle() */

  void displayResults (JTextArea text) {
    String lines[] = new String[13];
	 int count;
	 
	 lines[0] = new String ("Number:\t");
	 lines[1] = new String ("Name:\t");
	 lines[2] = new String ("Type:\t");
	 lines[3] = new String ("Shoot:\t");
	 lines[4] = new String ("Scuffle:\t");
	 lines[5] = new String ("# of Crew:\t");
	 lines[6] = new String ("Leader:\t");
	 lines[7] = new String ("Weapon:\t");
	 lines[8] = new String ("Save:\t");
	 lines[9] = new String ("Speed:\t");
	 lines[10] = new String ("Start:\t");
	 lines[11] = new String ("Sustain:\t");
	 lines[12] = new String ("Spin:\t");

	 for (count = 0; count < numVehiclesCreated; count++) {
	   if (name[count] != null) {
		  lines[0] += count + "\t";
		  lines[1] += name[count] + "\t";
		  lines[2] += vehType[count] + "\t";
		  lines[3] += shoot[count] + "\t";
		  lines[4] += scuffle[count] + "\t";
		  lines[5] += numCrew[count] + "\t";
		  lines[6] += leader[count] + "\t";
		  lines[7] += (weapon[count]+1) + "\t";
		  lines[8] += save[count] + "\t";
		  lines[9] += speed[count] + "\t";
		  lines[10] += start[count] + "\t";
		  lines[11] += sustain[count] + "\t";
		  lines[12] += spin[count] + "\t";
		}
	 } /* for */

	 if (text == null) {
	   System.out.println ("VEHICLE RECORD:\n");
	   for (count = 0; count < 13; count++)
		  System.out.println(lines[count]);
		System.out.println ("\n");
	 } else {
  	   text.append("VEHICLE RECORD:\n");
      for (count = 0; count < 13; count++)
	     text.append(lines[count] + "\n");
	   text.append("\n\n");
	 }

  } /* displayResults() */

} /* class VehicleRecord */

class WeaponRecord {
  String[]  name;
  int[]     range;
  String[]  reload;
  int[]     srm;
  int[]     numShots;
  boolean[] heavyWeapon;
  String[]  notes;
  String[]  wpnType;

  int numWeaponsCreated = Constants.numWpnTypes;

  Random random;

  public WeaponRecord (int num) {
    if (num <= 0) {
	   System.err.println ("PersonnelRecord (constructor): " +
								  "You tried to create a weapon record with too few " +
								  "elements.  You must have at least one person.");
		System.exit(-1);
	 }
	 else if (num > 10) {
	   System.err.println ("PersonnelRecord (constructor): " +
								  "You tried to create a personnel record with too many " +
								  "elements.  You may have no more than " +
							     Constants.numWeapons + " weapons.");
		System.exit(-1);
	 }

	 initialize();
	 
	 System.err.println ("PersonnelRecord (constructor): " +
	 	                  "Created WeaponRecrod for " + Constants.numWeapons + 
								" personnel");
  } /* Constructor */

  protected void initialize() {
    numWeaponsCreated = Constants.numWpnTypes;

	 name        = new String [(Constants.numWeapons+Constants.numWpnTypes)*2];
	 range       = new int    [(Constants.numWeapons+Constants.numWpnTypes)*2];
	 reload      = new String [(Constants.numWeapons+Constants.numWpnTypes)*2];
	 srm         = new int    [(Constants.numWeapons+Constants.numWpnTypes)*2];
	 numShots    = new int    [(Constants.numWeapons+Constants.numWpnTypes)*2];
	 heavyWeapon = new boolean[(Constants.numWeapons+Constants.numWpnTypes)*2];
	 notes       = new String [(Constants.numWeapons+Constants.numWpnTypes)*2];
	 wpnType     = new String [(Constants.numWeapons+Constants.numWpnTypes)*2];

	 random = new Random();
  }

  protected int makeWeapon (int type) {
    return makeWeapon (type, "Unnamed");
  }

  protected int makeWeapon (int type, String n) {
    int numberRolled;
    if (numWeaponsCreated >= name.length) return -1;

	 wpnType[numWeaponsCreated] = new String (Constants.weaponTypes[type]);

	 if ((type == 1) || (type == 2)) heavyWeapon[numWeaponsCreated] = false;
	 else heavyWeapon[numWeaponsCreated] = true;

	 if (n.equals("Unnamed"))
      name[numWeaponsCreated]   = new String ("Weapon " + (numWeaponsCreated+1));
	 else 
	   name[numWeaponsCreated]   = new String (n);

	 numberRolled = random.nextInt(20);
	 range[numWeaponsCreated] = Constants.rangeNumbers[type][numberRolled];
	 if (!heavyWeapon[numWeaponsCreated]) {
	   if (numberRolled == 0)
		  notes[numWeaponsCreated] = new String ("30 deg. arc");
		else if (numberRolled == 19)
		  notes[numWeaponsCreated] = new String ("Grenade Proj.");
		else
		  notes[numWeaponsCreated] = new String (" ");
	 }
	 else notes[numWeaponsCreated] = new String (" ");

	 srm[numWeaponsCreated]  = Constants.srmNumbers[type][random.nextInt(20)];

	 if (heavyWeapon[numWeaponsCreated]) {
	   reload[numWeaponsCreated]  = Constants.reloadTypes[2][random.nextInt(20)];
		numShots[numWeaponsCreated]= Constants.numShots[2][random.nextInt(20)];
	 }
	 else {
	   reload[numWeaponsCreated]  = Constants.reloadTypes[1][random.nextInt(20)];
		numShots[numWeaponsCreated]= Constants.numShots[1][random.nextInt(20)];
	 }

    //displayWeapon(numWeaponsCreated);

	 numWeaponsCreated++;
	 return (numWeaponsCreated-1);
  } /* makeCharacter */

  void displayWeapon (int index) {
    System.err.println ("      WEAPON "    + index);
    System.err.println ("      Name\t"     + name[index]);
    System.err.println ("      Range\t"    + range[index]);
    System.err.println ("      SRM\t"      + srm[index]);
    System.err.println ("      Reload\t"   + reload[index]);
    System.err.println ("      NumShots\t" + numShots[index]);
	 System.err.println ("      Notes\t"    + notes[index]);
  }

  void displayResults (JTextArea text) {
    String lines[] = new String[8];
	 int count;
	 
	 lines[0] = new String ("Number:\t");
	 lines[1] = new String ("Name:\t");
	 lines[2] = new String ("Type:\t");
	 lines[3] = new String ("Range:\t");
	 lines[4] = new String ("# Shots:\t");
	 lines[5] = new String ("SRM:\t");
	 lines[6] = new String ("Reload:\t");
	 lines[7] = new String ("Notes:\t");

	 for (count = 0; count < numWeaponsCreated; count++) {
	   if (name[count] != null) {
		  lines[0] += (count+1) + "\t";
		  lines[1] += name[count] + "\t";
		  lines[2] += wpnType[count] + "\t";
		  lines[3] += range[count] + "\t";
		  lines[4] += numShots[count] + "\t";
		  lines[5] += srm[count] + "\t";
		  lines[6] += reload[count] + "\t";
		  lines[7] += notes[count] + "\t";
		}
	 } /* for */

	 if (text == null) {
	   System.out.println ("WEAPON RECORD:\n");
	   for (count = 0; count < 7; count++)
		  System.out.println(lines[count]);
		System.out.println ("\n");
	 } else {
     text.append("WEAPON RECORD:\n");
     for (count = 0; count < 7; count++)
	     text.append(lines[count] + "\n");
	   text.append("\n\n");
	 }

  } /* displayResults() */

} /* class WeaponRecord */

//********************************************************//
//** Listeners and Adapters                             **//
//********************************************************//

class MyWindowListener extends WindowAdapter {
  public void windowClosing (WindowEvent e) {
    System.exit(0);
  }
} /* MyWindowListener */

//********************************************************//
//** Utilities                                          **//
//********************************************************//

class Constants {
  protected static final int numUnattached = 7;
  protected static final String[] unattachedTypes = 
    {"Don't Generate", "Veteran", "Leader", "Adventurer", "Hero"};
  protected static final String[] ratingNames = 
    {"Extra", "Veteran", "Leader", "Adventurer", "Hero"};
  protected static final int[][] attributes =
    {
	   { 3,  4,  4,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9, 10}, // Extra
		{ 5,  6,  6,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9, 10, 10, 10, 11, 11, 12}, // Veteran
		{ 7,  8,  8,  9,  9,  9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 14}, // Leader
		{ 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16}, // Adventurer
		{11, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 17, 17, 18} // Hero
    };

  protected static final int numWeapons = 4;
  protected static final String[] weaponTypes = 
    {"Don't Generate", "Side Arm", "Small Arm", "Small Hvy. Wpn.",
	  "Med. Hvy. Wpn.", "Lg. Hvy. Wpn."};
  protected static final String[] weaponsList =
    {"None", "Gen. Side Arm", "Gen. Small Arm", "Gen. Small Hvy. Wpn.", 
	  "Gen. Medium Hvy. Wpn.", "Gen. Large Hvy. Wpn.", "Thrown Stuff",
	  "Bow", "Crossbow", "Spear", "Small Catapult", "Musket/Carbine",
	  "Pistol", "Muzzle Ld. Rifle", "Repeater", "Sjotgun", "Bolt-Action Rifle",
	  "Semi-Auto Rifle", "SMG", "Gattling", "LMG", "MMG", "HMG",
	  "Swivel Gun", "Gren. Launch (old)", "Gren. Launch (new)", "Grenade Blast"};
  protected static final int wpnListOffset = 5;
  protected static final int numWpnTypes = 21;
  protected static final int[][] rangeNumbers =
    {
	   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // None
		{ 6, 12, 12, 12, 12, 12, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 36, 36, 24}, // Side Arm
		{12, 24, 24, 36, 36, 36, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 60, 60, 36}, // Small Arm
		{ 6, 12, 12, 24, 24, 24, 24, 24, 24, 24, 36, 36, 36, 36, 36, 36, 36, 48, 48, 48}, // Small Hvy. Wpn.
		{12, 24, 24, 24, 24, 24, 36, 36, 36, 36, 48, 48, 48, 48, 48, 48, 48, 60, 60, 72}, // Med. Hvy. Wpn.
		{24, 36, 36, 36, 36, 36, 48, 48, 48, 48, 60, 60, 60, 60, 60, 60, 60, 72, 72, 1000} // Lg. Hvy. Wpn.
	 };
  protected static final int[][] srmNumbers =
    {
	   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // None
		{-9, -8, -8, -7, -7, -7, -6, -6, -6, -6, -5, -5, -5, -5, -4, -4, -4, -3, -3, -2}, // Side Arm
		{-8, -7, -7, -6, -6, -6, -5, -5, -5, -5, -4, -4, -4, -4, -3, -3, -3, -2, -2, -1}, // Small Arm
		{ 0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  4,  4,  5}, // Small Hvy. Wpn.
		{ 1,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5}, // Med. Hvy. Wpn.
		{ 2,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  6,  6,  100} // Lg. Hvy. Wpn.
	 };
  protected static final String[][] reloadTypes =
    {
		{"N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A"}, // None
		{"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"}, // Side Arm and Small Arm
		{"Yes/No", "Yes/No", "Yes/No",  "Yes/Yes", "Yes/Yes", "Yes/Yes", "No", "No", "No", "No", "No", "No", "No", "No", "Yes/Yes", "Yes/Yes", "Yes/Yes", "Yes/No", "Yes/No", "Yes/No"} // Hvy. Wpn.
	 };
  protected static final int numShots[][] =
    {
	   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // None
		{2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4}, // Side Arm and Small Arm
		{4, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3} // Hvy. Wpn.
	 };

  protected static final int numVehicles = 5;
  protected static final String[] vehicleTypes =
    {"Don't Generate", "Pwr. Sail", "Pwr. Steam", "Pwr. Swim",
	  "Sail", "Steam", "Swim"};
  protected static final int[][] speedAttributes =
    {
		{7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 14}, // Sail
		{3, 4, 4, 5, 5, 5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9, 10}, // Steam
		{1, 2, 2, 3, 3, 3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,  7,  7,  8}  // Swim
	 };
  protected static final int[] startNumber = 
    { 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16};
  protected static final int[] sustainNumber = 
    {15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19};
  protected static final int[] spinNumber =
    {45, 180, 180, 90, 90, 90, 45, 45, 45, 45, 45, 45, 45, 45, 90, 90, 90, 180, 180, 45};
  protected static final int[] saveNumber = 
    {9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16};
} /* Constants */


