/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2012-2014 Chuck Ritola
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/
package org.jtrfp.trcl.beh;

import org.jtrfp.trcl.beh.DamageableBehavior.SupplyNotNeededException;
import org.jtrfp.trcl.obj.WorldObject;


public class ResetsRandomlyAfterDeath extends Behavior implements DeathListener {
private double minWaitMillis=100,maxWaitMillis=1000;
private Runnable runOnReset;

@Override
public void notifyDeath() {
   //Reset state
    final WorldObject thisObject = getParent();
    final Runnable _runOnReset = runOnReset;
    final long waitTime = (long)(2*minWaitMillis+Math.random()*.5*(maxWaitMillis-minWaitMillis));
    new Thread(){
	@Override
	public void run(){
	    try{Thread.currentThread().sleep(waitTime);}
	    catch(InterruptedException e){e.printStackTrace();}
	    thisObject.getContainingGrid().add(thisObject);
	    thisObject.setActive(true);//Is this really needed?
	    thisObject.setVisible(true);
	    try{thisObject.getBehavior().probeForBehavior(DamageableBehavior.class).unDamage();}
	    catch(SupplyNotNeededException e){e.printStackTrace();}//?!?!    
	    _runOnReset.run();
	}//end run()
    }.start();
 }//end notifyDeath

/**
 * @return the minWaitMillis
 */
public double getMinWaitMillis() {
    return minWaitMillis;
}

/**
 * @param minWaitMillis the minWaitMillis to set
 */
public ResetsRandomlyAfterDeath setMinWaitMillis(double minWaitMillis) {
    this.minWaitMillis = minWaitMillis;
    return this;
}

/**
 * @return the maxWaitMillis
 */
public double getMaxWaitMillis() {
    return maxWaitMillis;
}

/**
 * @param maxWaitMillis the maxWaitMillis to set
 */
public ResetsRandomlyAfterDeath setMaxWaitMillis(double maxWaitMillis) {
    this.maxWaitMillis = maxWaitMillis;
    return this;
}

/**
 * @return the runOnReset
 */
public Runnable getRunOnReset() {
    return runOnReset;
}

/**
 * @param runOnReset the runOnReset to set
 */
public ResetsRandomlyAfterDeath setRunOnReset(Runnable runOnReset) {
    this.runOnReset = runOnReset;
    return this;
}

}//end ResetsRandomlyAfterDeath

@@ -601,16 +601,61 @@
 l.beh;%0A%0A
+import org.jtrfp.trcl.SpacePartitioningGrid;%0A
 import o
@@ -1173,16 +1173,18 @@
 ));%0A    
+//
 new Thre
@@ -1189,16 +1189,18 @@
 read()%7B%0A
+//
 %09@Overri
@@ -1202,16 +1202,18 @@
 verride%0A
+//
 %09public 
@@ -1224,24 +1224,26 @@
  run()%7B%0A%09   
+/*
  try%7BThread.
@@ -1332,22 +1332,28 @@
 race();%7D
+*/
 %0A%09    
+try%7B
 thisObje
@@ -1362,138 +1362,256 @@
 .get
-ContainingGrid().add(thisObject
+Behavior().probeForBehavior(DamageableBehavior.class).unDamage(
 );
+%7D
 %0A%09    
-thisObject.setActive(true
+catch(SupplyNotNeededException e)%7Be.printStackTrace(
 );
+%7D
 //
-Is this really needed?%0A%09    thisObject.setVisible(true
+?!?!    %0A%09    SpacePartitioningGrid grid = thisObject.probeForBehavior(DeathBehavior.class).getGridOfLastDeath(
 );%0A%09    
 try%7B
@@ -1602,28 +1602,24 @@
 ath();%0A%09    
-try%7B
 thisObject.g
@@ -1613,38 +1613,24 @@
  thisObject.
-getBehavior().
 probeForBeha
@@ -1631,33 +1631,28 @@
 orBehavior(D
-amageable
+eath
 Behavior.cla
@@ -1659,91 +1659,192 @@
 ss).
-unDamage
+reset
 ();
-%7D
 %0A%09    
-catch(SupplyNotNeededException e)%7Be.printStackTrace(
+if(grid!=null)grid.add(thisObject);%0A%09    else throw new NullPointerException();%0A%09    thisObject.setActive(true
 );
-%7D
 //
-?!?!    
+Is this really needed?%0A%09    thisObject.setVisible(true);
 %0A%09  
@@ -1864,16 +1864,18 @@
 .run();%0A
+//
 %09%7D//end 
@@ -1880,16 +1880,18 @@
 d run()%0A
+//
     %7D.st

Refactor ResetsRandomlyAfterDeath to use DeathBehavior's location and partitioning grid info.