package bvyy.database; // Copyright(C)1998 Brian Yap // 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. //import java.lang.*; import java.util.*; import java.io.*; import bvyy.util.*; import bvyy.command.*; /** * Make a general purpose capability model. *

* Intent *

* This is a capability class. It records the capabilities that a thing may * have. These capabilities are organised into multiple trees as required by * the game world builder. *

* Motivation *

* Allow the game desingers to specify capabilities that I could not * have envisionaged. Maked the game customisable by a non-computer literate * author with no need to write code. *

* @version 0.0.2 26 October 1998 * @author (c) 1998 Brian Voon Yee Yap * */ public class Capability implements Serializable{ Vector branches = new Vector(); String name = "No name"; boolean root = true; Capability parent = null; private transient Vector generalEventListeners; private Capability parent; private Command command; private String nounForm; /** * Creates a new root Capability. * * @param newName The name for this capability. */ public Capability(String newName) { name = newName; } /** * Creates a new Capability. * * @param newName The name for this capability. * @param myParentCapability The name of the parent capability. */ public Capability(String newName, Capability myParentCapability) { name = newName; parent = myParentCapability; root = false; } /** * Test if this capability is the root. * * @return true if this capability is at the root. */ public boolean getRoot() { return root; } /** * Sets this node to be a root node.

* If the parent has been set, then the paret is reset to null. * */ public void setRoot(boolean newRoot) { root = newRoot; if (root && parent) parent = null; } /** * Returns an enumeration of the components of this vector. * * @return an enumeration of the components of this vector. */ public synchronized Enumeration elements() { return branches.elements(); } /** * Sets the name for this capability. */ public synchronized void setName(String newName) { name = newName; } /** * returns the Name for this capability. * * @return the name of this capability. */ public synchronized String getName() { return name; } /** * Adds a new capability as a branch. * * @param newCapability the new capability to be added to the node. */ public synchronized void addCapability(String newCapability) { Capability c = new Capability(newCapability,this); branches.addElement(c); } /** * removes a capability from the branches. * * @param oldCapability the capability to be removed. * @return false if the object spcified did not exist. true if it did. */ public synchronized boolean removeCapability(String oldCapability) { Enumeration e = branches.elements(); boolean stopper = e.hasMoreElements(); String n; Capability c = null; while (e.hasMoreElements() && stopper) { c = ((Capability)e.nextElement()); n = c.getName(); if (n.equals(oldCapability)) { stopper = false; } } if (stopper && (c != null)) { return branches.removeElement(c); } else { return false; } } /** * Saves this object to the specified output stream. * * @param oos the output stream for saving. * @exception IOException if the save fails. */ void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); } /** * reads this class from the spceified input stream. * * @param ois the input stream for reading. * @exception ClassNotFoundException if the class cannot be found. * @exception IOException if some other IO excpetion occurs. */ void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); } /** * */ public synchronized void removeGeneralEventListener(GeneralEventListener l) { if (generalEventListeners != null && generalEventListeners.contains(l)) { Vector v = (Vector) generalEventListeners.clone(); v.removeElement(l); generalEventListeners = v; } } /** * */ public synchronized void addGeneralEventListener(GeneralEventListener l) { Vector v = generalEventListeners == null ? new Vector(2) : (Vector) generalEventListeners.clone(); if (!v.contains(l)) { v.addElement(l); generalEventListeners = v; } } /** * */ protected void fireReceiveEvent(GeneralEvent e) { if (generalEventListeners != null) { Vector listeners = generalEventListeners; int count = listeners.size(); for (int i = 0; i < count; i++) ((GeneralEventListener) listeners.elementAt(i)).receiveEvent(e); } } /** * */ public void setParent(Capability newParent) { if (getRoot()) { parent = null; } else { parent = newParent; } } /* * Returns a list of parents from the closest to the farthest. * * @return a list of parents from the closest to the farthest. */ public Capability getParent() { return parent; } /** * */ public void setCommand(Command newCommand) { command = newCommand; } /** * */ public Command getCommand() throws NoCommandAssignedException{ return command; } /** * */ public void setNounForm(String newNounForm) { nounForm = newNounForm; } /** * */ public String getNounForm() { return nounForm; } }