Unverified Commit 342fea6f authored by Juon Kawakami's avatar Juon Kawakami 🥗
Browse files

init

parent 54f6cedf
/*
* Last change: $Date: 2005/03/16 02:30:30 $
* $Revision: 1.20 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import rescuecore.event.PropertyChangedEvent;
import rescuecore.event.PropertyListener;
import rescuecore.objects.AmbulanceCenter;
import rescuecore.objects.AmbulanceTeam;
import rescuecore.objects.Building;
import rescuecore.objects.Car;
import rescuecore.objects.Civilian;
import rescuecore.objects.FireBrigade;
import rescuecore.objects.FireStation;
import rescuecore.objects.Node;
import rescuecore.objects.PoliceForce;
import rescuecore.objects.PoliceOffice;
import rescuecore.objects.Refuge;
import rescuecore.objects.River;
import rescuecore.objects.RiverNode;
import rescuecore.objects.Road;
import rescuecore.objects.World;
/**
* This is the base class for all objects in the simulation environment
*/
public abstract class RescueObject implements java.io.Serializable {
private transient Collection listeners;
/** The kernel-assigned id */
protected int id;
private transient int[] knownProperties;
private transient HashMap annotations;
/**
* Construct a new RescueObject
**/
protected RescueObject() {
listeners = new ArrayList();
id = 0;
knownProperties = null;
annotations = null;
}
/**
* Adds an annotation to this object. This will overwrite any existing
* annotations with the same key.
*/
public void addAnnotation(String key, Object annotation) {
if (annotations == null)
annotations = new HashMap();
annotations.put(key, annotation);
}
/**
* Gets an annotation for the given String identifier. If no such annotation
* exists this will return null.
*
* @return The annotation.
*/
public Object getAnnotation(String key) {
if (annotations == null) {
// System.err.println("Could not find annotation named \""+key+"\"");
return null;
}
return annotations.get(key);
}
/**
* Get this objects type
*
* @return The type of this object
* @see RescueConstants#TYPE_CIVILIAN
* @see RescueConstants#TYPE_FIRE_BRIGADE
* @see RescueConstants#TYPE_AMBULANCE_TEAM
* @see RescueConstants#TYPE_POLICE_FORCE
* @see RescueConstants#TYPE_ROAD
* @see RescueConstants#TYPE_NODE
* @see RescueConstants#TYPE_RIVER
* @see RescueConstants#TYPE_RIVER_NODE
* @see RescueConstants#TYPE_BUILDING
* @see RescueConstants#TYPE_REFUGE
* @see RescueConstants#TYPE_FIRE_STATION
* @see RescueConstants#TYPE_AMBULANCE_CENTER
* @see RescueConstants#TYPE_POLICE_OFFICE
* @see RescueConstants#TYPE_WORLD
* @see RescueConstants#TYPE_CAR
*/
public abstract int getType();
/**
* Get this objects id
*/
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public int hashCode() {
return id;
}
public boolean equals(Object o) {
if (o instanceof RescueObject)
return this.id == ((RescueObject) o).id;
return false;
}
/**
* Add a PropertyListener
*
* @param l The listener that will be notified of changes to any properties in
* this object
*/
public void addPropertyListener(PropertyListener l) {
if (listeners == null)
listeners = new ArrayList();
synchronized (listeners) {
listeners.add(l);
}
}
/**
* Remove a PropertyListener
*
* @param l The listener that will no longer be notified of changes to any
* properties in this object
*/
public void removePropertyListener(PropertyListener l) {
if (listeners == null)
listeners = new ArrayList();
synchronized (listeners) {
listeners.remove(l);
}
}
/**
* Get a String representation of all this object's properties
*
* @return A String representation of all this object's properties
*/
protected String getPropertiesString() {
StringBuffer result = new StringBuffer();
int[] known = getKnownPropertyTypes();
for (int i = 0; i < known.length; ++i) {
if (i != 0)
result.append(", ");
result.append(getPropertyAsString(known[i]));
}
return result.toString();
}
/**
* Get all property types known by this object
*
* @return An array containing all the property types known by this object
* @see RescueConstants#PROPERTY_NULL
* @see RescueConstants#PROPERTY_START_TIME
* @see RescueConstants#PROPERTY_LONGITUDE
* @see RescueConstants#PROPERTY_LATITUDE
* @see RescueConstants#PROPERTY_WIND_FORCE
* @see RescueConstants#PROPERTY_WIND_DIRECTION
* @see RescueConstants#PROPERTY_X
* @see RescueConstants#PROPERTY_Y
* @see RescueConstants#PROPERTY_DIRECTION
* @see RescueConstants#PROPERTY_POSITION
* @see RescueConstants#PROPERTY_POSITION_HISTORY
* @see RescueConstants#PROPERTY_POSITION_EXTRA
* @see RescueConstants#PROPERTY_STAMINA
* @see RescueConstants#PROPERTY_HP
* @see RescueConstants#PROPERTY_DAMAGE
* @see RescueConstants#PROPERTY_BURIEDNESS
* @see RescueConstants#PROPERTY_FLOORS
* @see RescueConstants#PROPERTY_BUILDING_ATTRIBUTES
* @see RescueConstants#PROPERTY_IGNITION
* @see RescueConstants#PROPERTY_BROKENNESS
* @see RescueConstants#PROPERTY_FIERYNESS
* @see RescueConstants#PROPERTY_ENTRANCES
* @see RescueConstants#PROPERTY_BUILDING_SHAPE_ID
* @see RescueConstants#PROPERTY_BUILDING_CODE
* @see RescueConstants#PROPERTY_BUILDING_AREA_GROUND
* @see RescueConstants#PROPERTY_BUILDING_AREA_TOTAL
* @see RescueConstants#PROPERTY_BUILDING_APEXES
* @see RescueConstants#PROPERTY_WATER_QUANTITY
* @see RescueConstants#PROPERTY_STRETCHED_LENGTH
* @see RescueConstants#PROPERTY_HEAD
* @see RescueConstants#PROPERTY_TAIL
* @see RescueConstants#PROPERTY_LENGTH
* @see RescueConstants#PROPERTY_ROAD_KIND
* @see RescueConstants#PROPERTY_CARS_PASS_TO_HEAD
* @see RescueConstants#PROPERTY_CARS_PASS_TO_TAIL
* @see RescueConstants#PROPERTY_HUMANS_PASS_TO_HEAD
* @see RescueConstants#PROPERTY_HUMANS_PASS_TO_TAIL
* @see RescueConstants#PROPERTY_WIDTH
* @see RescueConstants#PROPERTY_BLOCK
* @see RescueConstants#PROPERTY_REPAIR_COST
* @see RescueConstants#PROPERTY_MEDIAN_STRIP
* @see RescueConstants#PROPERTY_LINES_TO_HEAD
* @see RescueConstants#PROPERTY_LINES_TO_TAIL
* @see RescueConstants#PROPERTY_WIDTH_FOR_WALKERS
* @see RescueConstants#PROPERTY_EDGES
* @see RescueConstants#PROPERTY_SIGNAL
* @see RescueConstants#PROPERTY_SIGNAL_TIMING
* @see RescueConstants#PROPERTY_SHORTCUT_TO_TURN
* @see RescueConstants#PROPERTY_POCKET_TO_TURN_ACROSS
*/
public int[] getKnownPropertyTypes() {
if (knownProperties == null) {
Collection c = new ArrayList();
for (int i = RescueConstants.PROPERTY_MIN; i <= RescueConstants.PROPERTY_MAX; ++i) {
if (propertyExists(i))
c.add(Integer.valueOf(i));
}
Object[] all = c.toArray();
knownProperties = new int[all.length];
for (int i = 0; i < all.length; ++i)
knownProperties[i] = ((Integer) all[i]).intValue();
}
return knownProperties;
}
/**
* Get whether this object has a particular property or not
*
* @param property The property in question
* @return True iff the given property exists for this object
* @see RescueConstants#PROPERTY_NULL
* @see RescueConstants#PROPERTY_START_TIME
* @see RescueConstants#PROPERTY_LONGITUDE
* @see RescueConstants#PROPERTY_LATITUDE
* @see RescueConstants#PROPERTY_WIND_FORCE
* @see RescueConstants#PROPERTY_WIND_DIRECTION
* @see RescueConstants#PROPERTY_X
* @see RescueConstants#PROPERTY_Y
* @see RescueConstants#PROPERTY_DIRECTION
* @see RescueConstants#PROPERTY_POSITION
* @see RescueConstants#PROPERTY_POSITION_HISTORY
* @see RescueConstants#PROPERTY_POSITION_EXTRA
* @see RescueConstants#PROPERTY_STAMINA
* @see RescueConstants#PROPERTY_HP
* @see RescueConstants#PROPERTY_DAMAGE
* @see RescueConstants#PROPERTY_BURIEDNESS
* @see RescueConstants#PROPERTY_FLOORS
* @see RescueConstants#PROPERTY_BUILDING_ATTRIBUTES
* @see RescueConstants#PROPERTY_IGNITION
* @see RescueConstants#PROPERTY_BROKENNESS
* @see RescueConstants#PROPERTY_FIERYNESS
* @see RescueConstants#PROPERTY_ENTRANCES
* @see RescueConstants#PROPERTY_BUILDING_SHAPE_ID
* @see RescueConstants#PROPERTY_BUILDING_CODE
* @see RescueConstants#PROPERTY_BUILDING_AREA_GROUND
* @see RescueConstants#PROPERTY_BUILDING_AREA_TOTAL
* @see RescueConstants#PROPERTY_BUILDING_APEXES
* @see RescueConstants#PROPERTY_WATER_QUANTITY
* @see RescueConstants#PROPERTY_STRETCHED_LENGTH
* @see RescueConstants#PROPERTY_HEAD
* @see RescueConstants#PROPERTY_TAIL
* @see RescueConstants#PROPERTY_LENGTH
* @see RescueConstants#PROPERTY_ROAD_KIND
* @see RescueConstants#PROPERTY_CARS_PASS_TO_HEAD
* @see RescueConstants#PROPERTY_CARS_PASS_TO_TAIL
* @see RescueConstants#PROPERTY_HUMANS_PASS_TO_HEAD
* @see RescueConstants#PROPERTY_HUMANS_PASS_TO_TAIL
* @see RescueConstants#PROPERTY_WIDTH
* @see RescueConstants#PROPERTY_BLOCK
* @see RescueConstants#PROPERTY_REPAIR_COST
* @see RescueConstants#PROPERTY_MEDIAN_STRIP
* @see RescueConstants#PROPERTY_LINES_TO_HEAD
* @see RescueConstants#PROPERTY_LINES_TO_TAIL
* @see RescueConstants#PROPERTY_WIDTH_FOR_WALKERS
* @see RescueConstants#PROPERTY_EDGES
* @see RescueConstants#PROPERTY_SIGNAL
* @see RescueConstants#PROPERTY_SIGNAL_TIMING
* @see RescueConstants#PROPERTY_SHORTCUT_TO_TURN
* @see RescueConstants#PROPERTY_POCKET_TO_TURN_ACROSS
*/
public final boolean propertyExists(int property) {
return getProperty(property) != null;
// return false;
}
public Property getProperty(int property) /* throws UnknownPropertyException */ {
// throw new UnknownPropertyException(property);
return null;
}
/*
* Get a particular property as a String
*
* @param property The property we want
*
* @return A String representation of the property
*
* @throws UnknownPropertyException if the property is unknown
*/
public String getPropertyAsString(int property) /* throws UnknownPropertyException */ {
Property p = getProperty(property);
if (p == null)
return "<unknown>";
return p.getStringValue();
}
/**
* Get the last time a property was updated
*
* @param property The property we want
* @return The last time the property was updated
* @throws UnknownPropertyException if the property is unknown
*/
public int getLastPropertyUpdate(int property) /* throws UnknownPropertyException */ {
Property p = getProperty(property);
if (p == null)
return RescueConstants.VALUE_UNKNOWN;
return p.getLastUpdate();
}
/**
* Get the source for last update to a property
*
* @param property The property we want
* @return The source of the last update to the property
* @throws UnknownPropertyException if the property is unknown
*/
public Object getLastPropertyUpdateSource(int property) /* throws UnknownPropertyException */ {
Property p = getProperty(property);
if (p == null)
return null;
return p.getLastUpdateSource();
}
/**
* Is the value of a particular property known?
*
* @param property The property we want
* @return True if and only if the value of the given property is known
* @throws UnknownPropertyException if the property does not exist
*/
public boolean isPropertyValueKnown(int property) /* throws UnknownPropertyException */ {
Property p = getProperty(property);
if (p == null)
return false;
return p.isValueKnown();
}
/**
* Is the value of a particular property assumed?
*
* @param property The property we want
* @return True if and only if the value of the given property is assumed
* @throws UnknownPropertyException if the property does not exist
*/
public boolean isPropertyValueAssumed(int property) /* throws UnknownPropertyException */ {
Property p = getProperty(property);
if (p == null)
return false;
return p.isValueAssumed();
}
/**
* Update the value of a property. All PropertyListeners will be notified if the
* value is actually updated.
*
* @param property The property to update
* @param timestamp The current time step. If this update is more recent than
* the current value of the property then the update will
* proceed
* @param newValue The new value of the property
* @param source The source of the change
* @return true if and only if the update was successful. An update will not
* occur if the new value is less recent than our current information,
* or if the current value is the same as the new value
* @throws UnknownPropertyException if the property is not recognised
*/
// public boolean updateProperty(int property, int timestamp, int newValue,
// Object source) throws UnknownPropertyException {
// return updateProperty(getProperty(property),timestamp,newValue,source);
// }
/**
* Update the value of a property. All PropertyListeners will be notified if the
* value is actually updated.
*
* @param property The property to update
* @param timestamp The current time step. If this update is more recent than
* the current value of the property then the update will
* proceed
* @param newValue The new value of the property
* @param source The source of the change
* @return true if and only if the update was successful. An update will not
* occur if the new value is less recent than our current information,
* or if the current value is the same as the new value
* @throws UnknownPropertyException if the property is not recognised
*/
// public boolean updateProperty(int property, int timestamp, int[] newValue,
// Object source) throws UnknownPropertyException {
// return updateProperty(getProperty(property),timestamp,newValue,source);
// }
/**
* Update the value of a property. All PropertyListeners will be notified if the
* value is actually updated.
*
* @param property The property to update
* @param timestamp The current time step. If this update is more recent than
* the current value of the property then the update will
* proceed
* @param newValue The new value of the property
* @param source The source of the change
* @return true if and only if the update was successful. An update will not
* occur if the new value is less recent than our current information,
* or if the current value is the same as the new value
* @throws UnknownPropertyException if the property is not recognised
*/
// public boolean updateProperty(int property, int timestamp, boolean newValue,
// Object source) throws UnknownPropertyException {
// return updateProperty(getProperty(property),timestamp,newValue,source);
// }
protected boolean setProperty(IntProperty p, int newValue, int timestamp,
Object source) /* throws UnknownPropertyException */ {
if (p.setValue(newValue, timestamp, source)) {
firePropertyChanged(p.getType(), timestamp, source);
return true;
}
return false;
}
protected boolean setProperty(ArrayProperty p, int[] newValue, int timestamp,
Object source) /* throws UnknownPropertyException */ {
if (p.setValues(newValue, timestamp, source)) {
firePropertyChanged(p.getType(), timestamp, source);
return true;
}
return false;
}
protected boolean updateProperty(IntProperty p, int newValue, int timestamp,
Object source) /* throws UnknownPropertyException */ {
if (p.updateValue(newValue, timestamp, source)) {
firePropertyChanged(p.getType(), timestamp, source);
return true;
}
return false;
}
protected boolean updateProperty(ArrayProperty p, int[] newValue, int timestamp,
Object source) /* throws UnknownPropertyException */ {
if (p.updateValues(newValue, timestamp, source)) {
firePropertyChanged(p.getType(), timestamp, source);
return true;
}
return false;
}
/*
* protected boolean updateProperty(Property p, int timestamp, boolean newValue,
* Object source) throws UnknownPropertyException { if
* (p.updateValue(newValue,timestamp,source)){
* firePropertyChanged(p.getPropertyType(),timestamp,source); return true; }
* return false; }
*/
public String toString() {
return Handy.getTypeName(getType()) + " " + id;
}
public String toLongString() {
return Handy.getTypeName(getType()) + " " + id + " [" + getPropertiesString() + "]";
}
/**
* Read from an InputBuffer
*
* @param in An InputBuffer to read data from
* @param timestamp The timestamp of the update
* @param source The source of the change
*/
public final void read(InputBuffer in, int timestamp, Object source) {
int prop;
do {
prop = in.readInt();
if (prop != RescueConstants.PROPERTY_NULL) {
int size = in.readInt();
Property p = getProperty(prop);
if (p != null) {
if (p.read(in, timestamp, source))
firePropertyChanged(prop, timestamp, source);
} else {
System.err.println("Got an unknown property (" + prop + ") from the stream");
in.skip(size);
}
}
} while (prop != RescueConstants.PROPERTY_NULL);
}
/**
* Update this RescueObject from a different one
*
* @param o The object to update from
*/
public final void merge(RescueObject o) {
int[] known = getKnownPropertyTypes();
for (int i = 0; i < known.length; ++i) {
Property oldP = getProperty(known[i]);
Property newP = o.getProperty(known[i]);
if (oldP != null && newP != null) {
if (oldP.merge(newP))
firePropertyChanged(oldP.getType(), oldP.getLastUpdate(), oldP.getLastUpdateSource());
}
}
}
/**
* Write this RescueObject to an OutputBuffer
*
* @param out The OutputBuffer to write to
*/
public void write(OutputBuffer out) {
int[] props = getKnownPropertyTypes();
for (int i = 0; i < props.length; ++i) {
Property p = getProperty(props[i]);
if (p != null && p.isValueKnown()) {
out.writeInt(p.getType());
int base = out.markBlock();
p.write(out);
out.writeBlockSize(base);
}
}
out.writeInt(RescueConstants.PROPERTY_NULL);
}
/**
* Is this object a building?
*
* @return true if and only if this object is a building
*/
public boolean isBuilding() {
// return (getInternalType() & INTERNAL_TYPE_ANY_BUILDING) != 0;
switch (getType()) {
case RescueConstants.TYPE_BUILDING:
case RescueConstants.TYPE_REFUGE:
case RescueConstants.TYPE_FIRE_STATION:
case RescueConstants.TYPE_POLICE_OFFICE:
case RescueConstants.TYPE_AMBULANCE_CENTER:
return true;
default:
return false;
}
}
public boolean isOrdinaryBuilding() {
return getType() == RescueConstants.TYPE_BUILDING;
}
/**
* Is this object a refuge?
*
* @return true if and only if this object is a refuge
*/
public boolean isRefuge() {
return getType() == RescueConstants.TYPE_REFUGE;
}
/**
* Is this object a fire station?
*
* @return true if and only if this object is a fire station
*/
public boolean isFireStation() {
return getType() == RescueConstants.TYPE_FIRE_STATION;
}
/**
* Is this object a police office?
*
* @return true if and only if this object is a police office
*/
public boolean isPoliceOffice() {
return getType() == RescueConstants.TYPE_POLICE_OFFICE;
}
/**
* Is this object an ambulance center?
*
* @return true if and only if this object is an ambulance center
*/
public boolean isAmbulanceCenter() {
return getType() == RescueConstants.TYPE_AMBULANCE_CENTER;
}
/**
* Is this object a road?
*
* @return true if and only if this object is a road
*/
public boolean isRoad() {
return getType() == RescueConstants.TYPE_ROAD;
}
/**
* Is this object a node?
*
* @return true if and only if this object is a node
*/
public boolean isNode() {
return getType() == RescueConstants.TYPE_NODE;
}
/**
* Is this object a humanoid?
*
* @return true if and only if this object is a humanoid
*/
public boolean isHumanoid() {
// return (getInternalType() & INTERNAL_TYPE_ANY_HUMANOID) != 0;
switch (getType()) {
case RescueConstants.TYPE_CIVILIAN:
case RescueConstants.TYPE_AMBULANCE_TEAM:
case RescueConstants.TYPE_FIRE_BRIGADE:
case RescueConstants.TYPE_POLICE_FORCE:
return true;
default:
return false;
}
}
/**
* Is this object a civilian?
*
* @return true if and only if this object is a civilian
*/
public boolean isCivilian() {
return getType() == RescueConstants.TYPE_CIVILIAN;
}
/**
* Is this object an ambulance team?
*
* @return true if and only if this object is an ambulance
*/
public boolean isAmbulanceTeam() {
return getType() == RescueConstants.TYPE_AMBULANCE_TEAM;
}
/**
* Is this object a police force?
*
* @return true if and only if this object is a police force
*/
public boolean isPoliceForce() {
return getType() == RescueConstants.TYPE_POLICE_FORCE;
}
/**
* Is this object a fire brigade?
*
* @return true if and only if this object is a fire brigade
*/
public boolean isFireBrigade() {
return getType() == RescueConstants.TYPE_FIRE_BRIGADE;
}
protected void firePropertyChanged(int property, int timestep, Object source) {
if (listeners == null)
return;
PropertyChangedEvent event = new PropertyChangedEvent(this, property, timestep, source);
synchronized (listeners) {
for (Iterator it = listeners.iterator(); it.hasNext();) {
((PropertyListener) it.next()).propertyChanged(event);
}
}
}
/**
* Make a deep copy of this RescueObject
*
* @return A deep copy of this object
*/
public final RescueObject copy() {
RescueObject result = newObject(getType());
result.merge(this);
result.setID(id);
return result;
}
/**
* Construct a new RescueObject
*
* @param type The type of the new object
* @return A newly constructed RescueObject
*/
public static RescueObject newObject(int type) {
switch (type) {
case RescueConstants.TYPE_WORLD:
return new World();
case RescueConstants.TYPE_CIVILIAN:
return new Civilian();
case RescueConstants.TYPE_CAR:
return new Car();
case RescueConstants.TYPE_FIRE_BRIGADE:
return new FireBrigade();
case RescueConstants.TYPE_AMBULANCE_TEAM:
return new AmbulanceTeam();
case RescueConstants.TYPE_POLICE_FORCE:
return new PoliceForce();
case RescueConstants.TYPE_ROAD:
return new Road();
case RescueConstants.TYPE_RIVER:
return new River();
case RescueConstants.TYPE_NODE:
return new Node();
case RescueConstants.TYPE_RIVER_NODE:
return new RiverNode();
case RescueConstants.TYPE_BUILDING:
return new Building();
case RescueConstants.TYPE_REFUGE:
return new Refuge();
case RescueConstants.TYPE_FIRE_STATION:
return new FireStation();
case RescueConstants.TYPE_AMBULANCE_CENTER:
return new AmbulanceCenter();
case RescueConstants.TYPE_POLICE_OFFICE:
return new PoliceOffice();
default:
System.out.println("WARNING: Unknown object type: " + type);
return null;
}
}
}
\ No newline at end of file
/*
* Last change: $Date$
* $Revision$
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore;
import java.io.*;
import java.util.*;
import rescuecore.commands.*;
//import rescuecore.debug.*;
public abstract class Simulator extends RescueComponent {
protected int timeStep;
protected Memory memory;
protected boolean running;
protected Simulator() {
running = false;
timeStep = -1;
}
public final int getComponentType() {
return RescueConstants.COMPONENT_TYPE_SIMULATOR;
}
public final Command generateConnectCommand() {
return new SKConnect(getClass().getName());
}
public final boolean handleConnectOK(Command c) {
return true;
}
public final String handleConnectError(Command c) {
return null;
}
public boolean isRunning() {
return running;
}
public void shutdown() {
running = false;
}
public void handleMessage(Command c) {
// System.out.println("Handling "+c);
switch (c.getType()) {
case RescueConstants.COMMANDS:
handleCommands(c);
break;
case RescueConstants.KS_CONNECT_OK:
// Someone obviously didn't get our SK_ACKNOWLEDGE
RescueMessage ack = new RescueMessage();
// ack.append(new SK_ACKNOWLEDGE());
sendMessage(ack);
break;
}
}
/**
Initialise this simulator. Subclasses that override this method should invoke super.initialise(knowledge) at some point.
@param knowledge This simulator's knowledge of the world
*/
protected void initialise(RescueObject[] knowledge) {
memory = generateMemory();
for (int i=0;i<knowledge.length;++i) {
memory.add(knowledge[i],0,RescueConstants.SOURCE_INITIAL);
}
}
/**
Construct a new Memory object for use by this Agent. This method allows Agents to customise their choice of Memory object. The default implementation returns an {@link HashMemory}.
@return A new Memory object
*/
protected Memory generateMemory() {
return new HashMemory();
}
private void handleCommands(Command c) {
}
}
/*
* Last change: $Date: 2005/06/14 21:55:50 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore;
import java.net.*;
import java.util.*;
import java.io.*;
/**
The TCPSocket class sends messages via TCP
*/
public class TCPConnection implements Connection {
private Socket socket;
private List q;
private ReadThread read;
private IOException toThrow;
private InetAddress destination;
private int port;
private OutputStream out;
/**
Generate a new TCPSocket with a given destination
@param destination The target machine
@param port The target port
@throws SocketException if something goes wrong
*/
public TCPConnection(InetAddress destination, int port) throws SocketException, IOException {
socket = new Socket(destination,port);
socket.setSoTimeout(1000);
q = new LinkedList();
toThrow = null;
read = new ReadThread();
read.start();
this.destination = destination;
this.port = port;
out = socket.getOutputStream();
// System.err.println("Socket opened listening on port "+socket.getLocalPort());
}
public int getLocalPort() {
return socket.getLocalPort();
}
/**
Close the socket
*/
public void close() {
read.kill();
try {
out.close();
socket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void send(byte[] bytes) throws IOException {
out.write((byte)((bytes.length>>24)&0xFF));
out.write((byte)((bytes.length>>16)&0xFF));
out.write((byte)((bytes.length>>8)&0xFF));
out.write((byte)(bytes.length&0xFF));
out.write(bytes);
out.flush();
}
public byte[] receive(int timeout) throws IOException, InterruptedException {
synchronized(q) {
if (toThrow!=null) throw toThrow;
if (q.size()==0) {
if (timeout<1) q.wait();
else q.wait(timeout);
}
if (toThrow!=null) throw toThrow;
if (q.size()==0) return null;
return (byte[])q.remove(0);
}
}
private class ReadThread extends Thread {
private boolean running;
private boolean alive;
private final Object aliveLock = new Object();
private InputStream in;
ReadThread() throws IOException {
running = true;
alive = true;
in = socket.getInputStream();
}
public void kill() {
running = false;
synchronized(aliveLock) {
while (alive) try {aliveLock.wait(1000);} catch (InterruptedException e) {}
}
}
public void run() {
while (running) {
try {
int length = in.read()<<24 | in.read()<<16 | in.read()<<8 | in.read();
if (length<0) {
running = false;
continue;
}
byte[] data = new byte[length];
int count = 0;
while (count<length) {
int amount = in.read(data,count,length-count);
count += amount;
}
synchronized(q) {
q.add(data);
q.notifyAll();
}
}
catch (InterruptedIOException e) {}
catch (SocketException e) {
running = false;
}
catch (IOException e) {
e.printStackTrace();
synchronized(q) {
toThrow = e;
}
}
}
try {
in.close();
}
catch (IOException e) {
e.printStackTrace();
}
synchronized(aliveLock) {
alive = false;
aliveLock.notifyAll();
}
}
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKAcknowledge extends Command {
private int requestID;
private int agentID;
public AKAcknowledge(int requestID, int agentID) {
super(RescueConstants.AK_ACKNOWLEDGE);
this.requestID = requestID;
this.agentID = agentID;
}
public AKAcknowledge(InputBuffer in) {
super(RescueConstants.AK_ACKNOWLEDGE);
read(in);
}
public void write(OutputBuffer out) {
out.writeInt(requestID);
out.writeInt(agentID);
}
public void read(InputBuffer in) {
requestID = in.readInt();
agentID = in.readInt();
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKChannel extends AgentCommand {
private byte[] channels;
public AKChannel(int senderID, int time, byte channel) {
this(senderID, time, new byte[] {channel});
}
public AKChannel(int senderID, int time, byte[] channels) {
super(RescueConstants.AK_CHANNEL, senderID, time);
this.channels = new byte[channels.length];
System.arraycopy(channels,0,this.channels,0,channels.length);
}
public AKChannel(InputBuffer in) {
super(RescueConstants.AK_CHANNEL,0,0);
read(in);
}
public void write(OutputBuffer out) {
super.write(out);
out.writeInt(channels.length);
for (int i=0;i<channels.length;++i)
out.writeByte(channels[i]);
}
public void read(InputBuffer in) {
super.read(in);
channels = new byte[in.readInt()];
for (int i=0;i<channels.length;++i)
channels[i] = in.readByte();
}
public byte[] getChannels() {
return channels;
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKClear extends AgentCommand {
private int target;
public AKClear(int senderID, int time, int target) {
super(RescueConstants.AK_CLEAR,senderID,time);
this.target = target;
}
public AKClear(InputBuffer in) {
super(RescueConstants.AK_CLEAR,0,0);
read(in);
}
/*
public AKClear(int senderID, byte[] data) {
super(AK_CLEAR,senderID);
target = Handy.decodeInt(data,0);
}
*/
public void write(OutputBuffer out) {
super.write(out);
out.writeInt(target);
}
public void read(InputBuffer in) {
super.read(in);
target = in.readInt();
}
public int getTarget() {
return target;
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKConnect extends Command {
private int version;
private int[] agentTypes;
private int requestID;
private String name;
public AKConnect(int version, int requestID, String name, int... types) {
super(RescueConstants.AK_CONNECT);
this.agentTypes = types;
this.version = version;
this.requestID = requestID;
}
public AKConnect(InputBuffer in) {
super(RescueConstants.AK_CONNECT);
read(in);
}
public void write(OutputBuffer out) {
out.writeInt(requestID);
out.writeInt(version);
out.writeString(name);
out.writeInt(agentTypes.length);
for (int next : agentTypes) {
out.writeInt(next);
}
}
public void read(InputBuffer in) {
requestID = in.readInt();
version = in.readInt();
name = in.readString();
agentTypes = new int[in.readInt()];
for (int i = 0; i < agentTypes.length; ++i) {
agentTypes[i] = in.readInt();
}
}
public int getVersion() {
return version;
}
public int[] getAgentTypes() {
return agentTypes;
}
public int getRequestID() {
return requestID;
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
import java.util.List;
import java.util.ArrayList;
public class AKExtinguish extends AgentCommand {
private Nozzle[] nozzles;
public AKExtinguish(int senderID,int time, int targetID, int direction, int x, int y, int water) {
super(RescueConstants.AK_EXTINGUISH,senderID,time);
nozzles = new Nozzle[1];
nozzles[0] = new Nozzle(targetID,direction,x,y,water);
}
public AKExtinguish(int senderID, int time, Nozzle[] nozzles) {
super(RescueConstants.AK_EXTINGUISH,senderID,time);
this.nozzles = nozzles;
}
public AKExtinguish(InputBuffer in) {
super(RescueConstants.AK_EXTINGUISH,0,0);
read(in);
}
/*
public AKExtinguish(int senderID, byte[] data) {
super(AK_EXTINGUISH,senderID);
readNozzles(data,0);
}
*/
public void read(InputBuffer in) {
super.read(in);
List allNozzles = new ArrayList();
int target = 0;
do {
target = in.readInt();
if (target!=0) {
allNozzles.add(new Nozzle(target,in.readInt(),in.readInt(),in.readInt(),in.readInt()));
}
} while (target!=0);
nozzles = new Nozzle[allNozzles.size()];
allNozzles.toArray(nozzles);
}
public void write(OutputBuffer out) {
super.write(out);
for (int i=0;i<nozzles.length;++i) {
out.writeInt(nozzles[i].getTarget());
out.writeInt(nozzles[i].getDirection());
out.writeInt(nozzles[i].getX());
out.writeInt(nozzles[i].getY());
out.writeInt(nozzles[i].getWater());
}
out.writeInt(0);
}
public Nozzle[] getNozzles() {
return nozzles;
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKLoad extends AgentCommand {
private int target;
public AKLoad(int senderID, int time, int target) {
super(RescueConstants.AK_LOAD,senderID,time);
this.target = target;
}
public AKLoad(InputBuffer in) {
super(RescueConstants.AK_LOAD,0,0);
read(in);
}
/*
public AKLoad(int senderID, byte[] data) {
super(AK_LOAD,senderID);
target = Handy.decodeInt(data,0);
}
*/
public void write(OutputBuffer out) {
super.write(out);
out.writeInt(target);
}
public void read(InputBuffer in) {
super.read(in);
target = in.readInt();
}
public int getTarget() {
return target;
}
}
/*
* Last change: $Date: 2004/07/11 22:26:28 $
* $Revision: 1.2 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
import rescuecore.Handy;
public class AKMove extends AgentCommand {
private int[] path;
public AKMove(int senderID, int time, int[] path) {
super(RescueConstants.AK_MOVE,senderID, time);
this.path = new int[path.length];
System.arraycopy(path,0,this.path,0,path.length);
}
public AKMove(InputBuffer in) {
super(RescueConstants.AK_MOVE,0,0);
read(in);
}
public void read(InputBuffer in) {
super.read(in);
path = new int[in.readInt()];
// System.out.println("Reading AK_MOVE of length "+path.length);
for (int i=0;i<path.length;++i) {
path[i] = in.readInt();
// System.out.println(path[i]);
}
}
public void write(OutputBuffer out) {
super.write(out);
out.writeInt(path.length);
for (int i=0;i<path.length;++i) out.writeInt(path[i]);
}
public int[] getPath() {
return path;
}
public String toString() {
return "AK_MOVE ("+Handy.arrayAsString(path)+")";
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKRescue extends AgentCommand {
private int target;
public AKRescue(int senderID, int time, int target) {
super(RescueConstants.AK_RESCUE,senderID,time);
this.target = target;
}
public AKRescue(InputBuffer in) {
super(RescueConstants.AK_RESCUE,0,0);
read(in);
}
/*
public AKRescue(int senderID, byte[] data) {
super(AK_RESCUE,senderID);
target = Handy.decodeInt(data,0);
}
*/
public void write(OutputBuffer out) {
super.write(out);
out.writeInt(target);
}
public void read(InputBuffer in) {
super.read(in);
target = in.readInt();
}
public int getTarget() {
return target;
}
}
/*
* Copyright (c) 2006, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKRest extends AgentCommand {
public AKRest(int senderID, int time) {
super(RescueConstants.AK_REST,senderID,time);
}
public AKRest(InputBuffer in) {
super(RescueConstants.AK_REST,0,0);
read(in);
}
public void write(OutputBuffer out) {
super.write(out);
}
public void read(InputBuffer in) {
super.read(in);
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKSay extends AgentCommand {
private byte[] msg;
public AKSay(int senderID, int time, byte[] msg, int length) {
super(RescueConstants.AK_SAY,senderID,time);
this.msg = new byte[length];
System.arraycopy(msg,0,this.msg,0,length);
}
public AKSay(InputBuffer in) {
super(RescueConstants.AK_SAY,0,0);
read(in);
}
/*
public AKSay(int senderID, byte[] data) {
super(AK_SAY,senderID);
int length = Handy.decodeInt(data,0);
msg = Handy.decodeBytes(data,INT_SIZE,length);
}
*/
public void write(OutputBuffer out) {
super.write(out);
out.writeInt(msg.length);
out.writeBytes(msg);
}
public void read(InputBuffer in) {
super.read(in);
msg = new byte[in.readInt()];
in.readBytes(msg);
}
public byte[] getMessage() {
return msg;
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKTell extends AgentCommand {
private byte[] msg;
private byte channel;
public AKTell(int senderID, int time, byte[] msg, int length, byte channel) {
super(RescueConstants.AK_TELL,senderID,time);
this.msg = new byte[length];
System.arraycopy(msg,0,this.msg,0,length);
this.channel = channel;
}
public AKTell(InputBuffer in) {
super(RescueConstants.AK_TELL,0,0);
read(in);
}
public void write(OutputBuffer out) {
super.write(out);
out.writeInt(channel);
out.writeInt(msg.length);
out.writeBytes(msg);
}
public void read(InputBuffer in) {
super.read(in);
channel = (byte)in.readInt();
msg = new byte[in.readInt()];
in.readBytes(msg);
}
public byte[] getMessage() {
return msg;
}
public byte getChannel() {
return channel;
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class AKUnload extends AgentCommand {
public AKUnload(int senderID, int time) {
super(RescueConstants.AK_UNLOAD,senderID,time);
}
public AKUnload(InputBuffer in) {
super(RescueConstants.AK_UNLOAD,0,0);
read(in);
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.Handy;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
/**
This class encapsulates a command.
@see RescueConstants#COMMAND_MOVE
@see RescueConstants#COMMAND_EXTINGUISH
@see RescueConstants#COMMAND_CLEAR
@see RescueConstants#COMMAND_LOAD
@see RescueConstants#COMMAND_UNLOAD
@see RescueConstants#COMMAND_RESCUE
*/
public abstract class AgentCommand extends Command {
protected int senderID;
protected int time;
protected AgentCommand(int type, int senderID, int time) {
super(type);
this.senderID = senderID;
this.time = time;
}
public final int getSender() {
return senderID;
}
public final int getTime() {
return time;
}
public void write(OutputBuffer out) {
out.writeInt(senderID);
out.writeInt(time);
}
public void read(InputBuffer in) {
senderID = in.readInt();
time = in.readInt();
}
public String toString() {
return Handy.getCommandTypeName(type)+" from agent "+senderID+" at time " + time;
}
}
/*
* Last change: $Date: 2005/06/14 21:55:52 $
* $Revision: 1.3 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.RescueConstants;
import rescuecore.Handy;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
/**
This class encapsulates a command.
@see RescueConstants#COMMAND_MOVE
@see RescueConstants#COMMAND_EXTINGUISH
@see RescueConstants#COMMAND_CLEAR
@see RescueConstants#COMMAND_LOAD
@see RescueConstants#COMMAND_UNLOAD
@see RescueConstants#COMMAND_RESCUE
*/
public abstract class Command implements java.io.Serializable {
protected int type;
/**
Construct a new Command from a byte array
@param type The type of this command
@see RescueConstants#COMMAND_MOVE
@see RescueConstants#COMMAND_EXTINGUISH
@see RescueConstants#COMMAND_CLEAR
@see RescueConstants#COMMAND_LOAD
@see RescueConstants#COMMAND_UNLOAD
@see RescueConstants#COMMAND_RESCUE
*/
protected Command(int type) {
this.type = type;
}
/**
Get the type of this command
@return This command's type
@see RescueConstants#COMMAND_MOVE
@see RescueConstants#COMMAND_EXTINGUISH
@see RescueConstants#COMMAND_CLEAR
@see RescueConstants#COMMAND_LOAD
@see RescueConstants#COMMAND_UNLOAD
@see RescueConstants#COMMAND_RESCUE
*/
public final int getType() {
return type;
}
/**
Write this command to an OutputBuffer
@param The OutputBuffer to write to
*/
public abstract void write(OutputBuffer out);
/**
Read command data from an InputBuffer
@param The InputBuffer to read from
*/
public abstract void read(InputBuffer in);
public String toString() {
return Handy.getCommandTypeName(type);
}
/**
Create an EXTINGUISH command that will use the maximum extinguish power and ignore the direction,x and y parameters
@param agentID The id of the agent sending the extinguish command
@param time The time of the command.
@param targetID The id of the burning building
@return A filled-in EXTINGUISH command
*/
public static Command EXTINGUISH(int agentID, int time, int targetID) {
return EXTINGUISH(agentID,time,targetID,0,0,0,RescueConstants.MAX_EXTINGUISH_POWER);
}
/**
Create an EXTINGUISH command that will use the maximum extinguish power
@param agentID The id of the agent sending the extinguish command
@param time The time of the command.
@param targetID The id of the burning building
@param direction The direction from the nozzle to the building
@param x The x coordinate of the nozzle
@param y The y coordinate of the nozzle
@return A filled-in EXTINGUISH command
*/
public static Command EXTINGUISH(int agentID, int time, int targetID, int direction, int x, int y) {
return EXTINGUISH(agentID,time,targetID,direction,x,y,RescueConstants.MAX_EXTINGUISH_POWER);
}
/**
Create an EXTINGUISH command
@param agentID The id of the agent sending the extinguish command
@param time The time of the command.
@param targetID The id of the burning building
@param direction The direction from the nozzle to the building
@param x The x coordinate of the nozzle
@param y The y coordinate of the nozzle
@param water The amount of water to extinguish with, in 1/1000 m^3/min
@return A filled-in EXTINGUISH command
*/
public static Command EXTINGUISH(int agentID, int time, int targetID, int direction, int x, int y, int water) {
return new AKExtinguish(agentID, time, targetID, direction, x, y, water);
}
/**
Create a CLEAR command
@param id The id of the agent sending the clear command
@param time The time of the command.
@param target The id of the road to be cleared
@return A filled-in CLEAR command
*/
public static Command CLEAR(int id, int time, int target) {
return new AKClear(id,time,target);
}
/**
Create a RESCUE command
@param id The id of the agent sending the rescue command
@param time The time of the command.
@param target The id of the civilian to be rescued
@return A filled-in RESCUE command
*/
public static Command RESCUE(int id, int time, int target) {
return new AKRescue(id,time,target);
}
/**
Create a LOAD command
@param id The id of the agent sending the load command
@param time The time of the command.
@param target The id of the civilian to be loaded
@return A filled-in LOAD command
*/
public static Command LOAD(int id, int time, int target) {
return new AKLoad(id,time,target);
}
/**
Create an UNLOAD command
@param id The id of the agent sending the unload command
@param time The time of the command.
@return A filled-in UNLOAD command
*/
public static Command UNLOAD(int id, int time) {
return new AKUnload(id,time);
}
/**
Create a MOVE command
@param id The id of the agent sending the move command
@param time The time of the command.
@param path A list of ids (nodes, roads, rivers, rivernodes, buildings) the describe the path
@return A filled-in MOVE command
*/
public static Command MOVE(int id, int time, int[] path) {
return new AKMove(id,time,path);
}
/**
Create a SAY command
@param id The id of the agent saying something
@param time The time of the command.
@param msg The message
@return A filled-in SAY command
*/
public static Command SAY(int id, int time, byte[] msg, int length) {
return new AKSay(id,time,msg,length);
}
/**
Create an TELL command
@param id The id of the agent saying something
@param time The time of the command.
@param msg The message
@param channel The channel to use
@return A filled-in TELL command
*/
public static Command TELL(int id, int time, byte[] msg, int length, byte channel) {
return new AKTell(id,time,msg,length,channel);
}
}
/*
* Last change: $Date: 2004/05/20 23:42:00 $
* $Revision: 1.1 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
import java.util.List;
import java.util.ArrayList;
public class Commands extends Command {
private int time;
private AgentCommand[] commands;
public Commands(int time, AgentCommand[] commands) {
super(RescueConstants.COMMANDS);
this.time = time;
this.commands = commands;
}
public Commands(InputBuffer in) {
super(RescueConstants.COMMANDS);
read(in);
}
public void read(InputBuffer in) {
time = in.readInt();
Command[] all = in.readCommands();
List allCommands = new ArrayList();
for (Command next : all) {
if (next instanceof AgentCommand) allCommands.add(next);
}
commands = new AgentCommand[allCommands.size()];
allCommands.toArray(commands);
}
public void write(OutputBuffer out) {
out.writeInt(time);
out.writeCommands(commands);
}
public int getTime() {
return time;
}
public AgentCommand[] getCommands() {
return commands;
}
}
/*
* Last change: $Date: 2005/06/14 21:55:52 $
* $Revision: 1.2 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueConstants;
public class KAConnectError extends Command {
private int requestID;
private String reason;
public KAConnectError(int requestID, String reason) {
super(RescueConstants.KA_CONNECT_ERROR);
this.requestID = requestID;
this.reason = reason;
}
public KAConnectError(InputBuffer in) {
super(RescueConstants.KA_CONNECT_ERROR);
read(in);
}
public void read(InputBuffer in) {
requestID = in.readInt();
reason = in.readString();
}
public void write(OutputBuffer out) {
out.writeInt(requestID);
out.writeString(reason);
}
public int getRequestID() {
return requestID;
}
public String getReason() {
return reason;
}
}
/*
* Last change: $Date: 2005/06/14 21:55:52 $
* $Revision: 1.2 $
*
* Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rescuecore.commands;
import rescuecore.InputBuffer;
import rescuecore.OutputBuffer;
import rescuecore.RescueObject;
import rescuecore.RescueConstants;
public class KAConnectOK extends Command {
private RescueObject[] knowledge;
private int requestID;
private int agentID;
public KAConnectOK(int requestID, int agentID, RescueObject[] knowledge) {
super(RescueConstants.KA_CONNECT_OK);
this.knowledge = knowledge;
this.requestID = requestID;
this.agentID = agentID;
}
public KAConnectOK(InputBuffer in) {
super(RescueConstants.KA_CONNECT_OK);
read(in);
}
public void read(InputBuffer in) {
requestID = in.readInt();
agentID = in.readInt();
knowledge = in.readObjects(0,RescueConstants.SOURCE_INITIAL);
}
public void write(OutputBuffer out) {
out.writeInt(requestID);
out.writeInt(agentID);
out.writeObjects(knowledge);
}
public int getRequestID() {
return requestID;
}
public int getAgentID() {
return agentID;
}
public RescueObject[] getKnowledge() {
return knowledge;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment