/*******************************************************************************
 * 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 java.util.concurrent.Callable;

import org.jtrfp.trcl.SpacePartitioningGrid;
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)(minWaitMillis+Math.random()*(maxWaitMillis-minWaitMillis));
    new Thread(){
	@Override
	public void run(){
	    try{Thread.currentThread().sleep(waitTime);}
	    catch(InterruptedException e){e.printStackTrace();}
	    getParent().getTr().getThreadManager().submitToGPUMemAccess(new Callable<Void>(){
		@Override
		public Void call() throws Exception {
		    try{thisObject.getBehavior().probeForBehavior(DamageableBehavior.class).unDamage();}
		    catch(SupplyNotNeededException e){e.printStackTrace();}//?!?!    
		    SpacePartitioningGrid grid = thisObject.probeForBehavior(DeathBehavior.class).getGridOfLastDeath();
		    thisObject.probeForBehavior(DeathBehavior.class).reset();
		    if(grid!=null)grid.add(thisObject);
		    else throw new NullPointerException();
		    thisObject.setActive(true);
		    thisObject.setVisible(true);
		    _runOnReset.run();
		    return null;
		}}).get();
	}//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

@@ -1211,18 +1211,83 @@
     
-new Thread
+thisObject.getTr().getThreadManager().submitToThreadPool(new Callable%3CVoid%3E
 ()%7B%0A
@@ -1309,18 +1309,37 @@
 lic 
-v
+V
 oid 
-run()
+call() throws Exception 
 %7B%0A%09 
@@ -1582,16 +1582,140 @@
 ption %7B%0A
+%09%09    unDamage();%0A%09%09    reset();%0A%09%09    reIntroduce();%0A%09%09    runOnReset();%0A%09%09    return null;%0A%09%09%7D%0A%09%09private void unDamage()%7B%0A
 %09%09    tr
@@ -1864,20 +1864,47 @@
 ;%7D//?!?!
-    
+%0A%09%09%7D%0A%09%09%0A%09%09private void reset()%7B
 %0A%09%09    S
@@ -2149,24 +2149,58 @@
 xception();%0A
+%09%09%7D%0A%09%09private void reIntroduce()%7B%0A
 %09%09    thisOb
@@ -2262,13 +2262,25 @@
 ;%0A%09%09
-    _
+%7D%0A%09%09private void 
 runO
@@ -2289,74 +2289,77 @@
 eset
-.run
 ()
-;
+%7B
 %0A%09%09    
-return null;%0A%09%09%7D%7D).get();%0A%09%7D//end run()%0A    %7D.start(
+_runOnReset.run();%0A%09%09%7D%0A%09    %7D).get();%0A%09    return null;%0A%09%7D%7D
 );%0A 

Use thread pool instead of creating new Thread.