
// Copyright (c) 2004 
package uk.co.patrickhaston.mars;


import uk.co.patrickhaston.pdhdata.PdhData;

/**
 * A Class class.
 * <P>
 * @author Patrick Haston
 */
public class MarsResources extends MarsArray {

  /**
   * Constructor
   */
  public MarsResources() {
  }

  public void readData(String line)
  {
    int idx = readIndex(line);
    if (idx>0)
    {
      MarsResource r = new MarsResource(line);

      this.setElementAt(r, idx);
    }
  }

  public int addResource()
  {
    MarsResource r = new MarsResource(0);
//    this.addElement(r);
//    return this.elementCount - 1;
    return this.addData( (PdhData) r);
  }
  
  public MarsResource getResource(int id)
  {
    return (MarsResource) elementAt(id);
  }
  
  public int addNewMineralResource()
  {
    MarsResource r = new MarsResource(0);
    r.setNewMineralResource((float) (1000000 * Math.random())); // the mineralDensity is a game variable that is set at new game parameters
    // for bulk loading data we don't need to use the addData(r) function
    // we can just use the faster addElement function
    r.setId(size());
    this.addElement(r);
    return this.size()-1;
  }
  
  public boolean sufficientResources(int i, MarsResource r)
  {
    if (i > size() ) return false;
    if (elementAt(i) == null) return false;
    return ((MarsResource) elementAt(i)).greaterThan(r);
  }

  public float resourceFraction(int i, MarsResource r)
  {
    if (i > size() ) return 0;
    if (elementAt(i) == null) return 0;
    return ((MarsResource) elementAt(i)).resourceFraction(r);
  }
  
  public void subtractResources(int i, MarsResource r)
  {
    if (i > size() ) return;
    if (elementAt(i) == null) return;
    ((MarsResource) elementAt(i)).subtractResource(r);
  }

  public void addResources(int i, MarsResource r)
  {
    if (i > size() ) return;
    if (elementAt(i) == null) return;
    ((MarsResource) elementAt(i)).addResource(r);
  }
}


