// Path: core/src/main/java/org/mini2Dx/miniscript/core/exception/ScriptSkippedException.java
// public class ScriptSkippedException extends RuntimeException {
// 	private static final long serialVersionUID = -7901574884568506426L;
// 
// }


import org.mini2Dx.miniscript.core.exception.ScriptSkippedException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Thomas Cashman
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.mini2Dx.miniscript.core;



/**
 * Represents a task that will complete in-game at a future time
 */
public abstract class GameFuture {
	private static final AtomicInteger ID_GENERATOR = new AtomicInteger(0);

	private static final int STATE_NONE = 0;
	private static final int STATE_FUTURE_SKIPPED = 1;
	private static final int STATE_FUTURE_SKIPPED_GC_READY = STATE_FUTURE_SKIPPED * 10;
	private static final int STATE_SCRIPT_SKIPPED = 2;
	private static final int STATE_SCRIPT_SKIPPED_GC_READY = STATE_SCRIPT_SKIPPED * 10;
	private static final int STATE_COMPLETED = 3;
	private static final int STATE_COMPLETED_GC_READY = STATE_COMPLETED * 10;

	private final int futureId;
	private final AtomicInteger state = new AtomicInteger(STATE_NONE);

	/**
	 * Constructor using {@link GameScriptingEngine#MOST_RECENT_INSTANCE}
	 */
	public GameFuture() {
		this(GameScriptingEngine.MOST_RECENT_INSTANCE);
	}

	/**
	 * Constructor
	 *
	 * @param gameScriptingEngine
	 *            The {@link GameScriptingEngine} this future belongs to
	 */
	public GameFuture(GameScriptingEngine gameScriptingEngine) {
		if(gameScriptingEngine == null) {
			throw new RuntimeException("Cannot pass null scripting engine to " + GameFuture.class.getSimpleName());
		}
		futureId = ID_GENERATOR.incrementAndGet();
		gameScriptingEngine.submitGameFuture(this);

		if (Thread.interrupted()) {
			state.compareAndSet(STATE_NONE, STATE_SCRIPT_SKIPPED);
// Path: core/src/main/java/org/mini2Dx/miniscript/core/exception/ScriptSkippedException.java
// public class ScriptSkippedException extends RuntimeException {
// 	private static final long serialVersionUID = -7901574884568506426L;
// 
// }


// Path: core/src/main/java/org/mini2Dx/miniscript/core/GameFuture.java
import org.mini2Dx.miniscript.core.exception.ScriptSkippedException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Thomas Cashman
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.mini2Dx.miniscript.core;



/**
 * Represents a task that will complete in-game at a future time
 */
public abstract class GameFuture {
	private static final AtomicInteger ID_GENERATOR = new AtomicInteger(0);

	private static final int STATE_NONE = 0;
	private static final int STATE_FUTURE_SKIPPED = 1;
	private static final int STATE_FUTURE_SKIPPED_GC_READY = STATE_FUTURE_SKIPPED * 10;
	private static final int STATE_SCRIPT_SKIPPED = 2;
	private static final int STATE_SCRIPT_SKIPPED_GC_READY = STATE_SCRIPT_SKIPPED * 10;
	private static final int STATE_COMPLETED = 3;
	private static final int STATE_COMPLETED_GC_READY = STATE_COMPLETED * 10;

	private final int futureId;
	private final AtomicInteger state = new AtomicInteger(STATE_NONE);

	/**
	 * Constructor using {@link GameScriptingEngine#MOST_RECENT_INSTANCE}
	 */
	public GameFuture() {
		this(GameScriptingEngine.MOST_RECENT_INSTANCE);
	}

	/**
	 * Constructor
	 *
	 * @param gameScriptingEngine
	 *            The {@link GameScriptingEngine} this future belongs to
	 */
	public GameFuture(GameScriptingEngine gameScriptingEngine) {
		if(gameScriptingEngine == null) {
			throw new RuntimeException("Cannot pass null scripting engine to " + GameFuture.class.getSimpleName());
		}
		futureId = ID_GENERATOR.incrementAndGet();
		gameScriptingEngine.submitGameFuture(this);

		if (Thread.interrupted()) {
			state.compareAndSet(STATE_NONE, STATE_SCRIPT_SKIPPED);
			throw new ScriptSkippedException();