All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.badlogic.gdx.ai.pfa.PathFinderQueue Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright 2014 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.ai.pfa;

import com.badlogic.gdx.ai.msg.Telegram;
import com.badlogic.gdx.ai.msg.Telegraph;
import com.badlogic.gdx.ai.sched.Schedulable;
import com.badlogic.gdx.ai.utils.CircularBuffer;
import org.mini2Dx.gdx.utils.TimeUtils;

/** @param  Type of node
 * 
 * @author davebaol */
public class PathFinderQueue implements Schedulable, Telegraph {

	public static final long TIME_TOLERANCE = 100L;

	CircularBuffer> requestQueue;

	PathFinder pathFinder;

	PathFinderRequest currentRequest;

	PathFinderRequestControl requestControl;

	public PathFinderQueue (PathFinder pathFinder) {
		this.pathFinder = pathFinder;
		this.requestQueue = new CircularBuffer>(16);
		this.currentRequest = null;
		this.requestControl = new PathFinderRequestControl();
	}

	@Override
	public void run (long timeToRun) {
		// Keep track of the current time
		requestControl.lastTime = TimeUtils.nanoTime();
		requestControl.timeToRun = timeToRun;

		requestControl.timeTolerance = TIME_TOLERANCE;
		requestControl.pathFinder = pathFinder;
		requestControl.server = this;

		// If no search in progress, take the next from the queue
		if (currentRequest == null) currentRequest = requestQueue.read();

		while (currentRequest != null) {

			boolean finished = requestControl.execute(currentRequest);

			if (!finished) return;

			// Read next request from the queue
			currentRequest = requestQueue.read();
		}
	}

	@Override
	public boolean handleMessage (Telegram telegram) {
		@SuppressWarnings("unchecked")
		PathFinderRequest pfr = (PathFinderRequest)telegram.extraInfo;
		pfr.client = telegram.sender; // set the client to be notified once the request has completed
		pfr.status = PathFinderRequest.SEARCH_NEW; // Reset status
		pfr.statusChanged = true; // Status has just changed
		pfr.executionFrames = 0; // Reset execution frames counter
		requestQueue.store(pfr);
		return true;
	}

	public int size () {
		return requestQueue.size();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy