/*
 *  Initiative - A role playing utility to track turns
 *  Copyright (C) 2002 Devon D Jones
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The author of this program grants you the ability to use this code
 *  in conjunction with code that is covered under the Open Gaming License
 *
 * InitHolderVector.java
 *
 * Created on January 16, 2002, 1:08 PM
 */

package org.evilsoft.gaming.Init;

import org.evilsoft.gaming.*;
import java.util.*;

/**
 *
 * @author  devon
 * @version
 */

public class InitHolderVector extends Vector {

    public boolean add(InitHolder user) {
    boolean result = super.add(user);
    if(result) {
        this.sort();
    }
    return result;
    }

    public void sort() {
    Collections.sort(this, new InitHolderComperator());
    }

    public void check() {
        Dice d20 = new Dice(1, 20);

        for(int i = 0; i < this.size(); i++) {
            InitHolder c = (InitHolder)this.elementAt(i);
            int roll = d20.roll();
            c.getInitiative().checkExtRoll(roll);
        }
        this.sort();

        int j = 1;
        for(int i = 0; i < this.size(); i++) {
        InitHolder c = (InitHolder)this.elementAt(i);
            String type = c.getHolderType();
            if(type.equals("XMLCombatant")) {
                XMLCombatant xmlcbt = (XMLCombatant)c;
                xmlcbt.setNumber(j);
                j++;
            }
        }
    }

    public int getMaxInit() {
        int maxInit = 20;
    for(int i = 0; i < this.size(); i++) {
        InitHolder c = (InitHolder)this.elementAt(i);
            int cInit = c.getInitiative().getCurrentInitiative();
            if(cInit > maxInit) {
                maxInit = cInit;
            }
        }
        return maxInit;
    }

    public boolean initValid(int init) {
        for(int i = 0; i < this.size(); i++) {
        InitHolder c = (InitHolder)this.elementAt(i);
            if(!c.getStatus().equals("Dead")) {
                int cInit = c.getInitiative().getCurrentInitiative();

                if(cInit == init) {
                    return true;
                }
            }
        }
        return false;
    }

    public Vector getRowVector(int i, Vector columnOrder) {
        InitHolder iH = (InitHolder)this.elementAt(i);
        return iH.getRowVector(columnOrder);
    }

    public boolean isUniqueName(String name) {
        for(int i = 0; i < this.size(); i++) {
            InitHolder c = (InitHolder)this.elementAt(i);
            if(c.getName().equals(name)) {
                return false;
            }
        }
        return true;
    }

}
