Location: PHPKode > projects > PhpBlock > A9.8/modules/dungeon/algorithm/dungeon-a-star.class.php
<?php
include_once $PATH_TO_CODE."/script/algorithm/a-star.class.php";

class DungeonAStar extends AStar{

    public function __construct($parMaxStep, $parMaxRange) {
        parent::__construct($parMaxStep, $parMaxRange);
    }

    protected function getAStarElement($parObject, $parZoneId, $parZoneStart, $parZoneGoal) {
        return new DungeonAStarPathElement($this, $parObject, $parZoneId, $parZoneStart, $parZoneGoal);
    }

	protected function getListOfCoordForSuccessors() {
		$listOfCoordForSuccessors = array();
		array_push($listOfCoordForSuccessors, array( 0, 0, -1));
		array_push($listOfCoordForSuccessors, array( 0, 0, 1));
		array_push($listOfCoordForSuccessors, array( 0, 1, 0));
		array_push($listOfCoordForSuccessors, array( 0,-1, 0));
		array_push($listOfCoordForSuccessors, array( 1, 0, 0));
		array_push($listOfCoordForSuccessors, array(-1, 0, 0));
		array_push($listOfCoordForSuccessors, array(-1, 1, 0));
		array_push($listOfCoordForSuccessors, array(-1,-1, 0));
		array_push($listOfCoordForSuccessors, array( 1, 1, 0));
		array_push($listOfCoordForSuccessors, array( 1,-1, 0));
		return $listOfCoordForSuccessors;
	}
	
    public function getPathZoneId($parRealObject, $parZoneStart, $parZoneGoal, &$parErrorMessage, &$parResult) {
    	//echo "<br>goal: ".$parZoneGoal->zoneId."<br>";
    	if($parZoneGoal && $parZoneGoal->isOccupedWithOnlyNonMovable()) {
    		if(!$parZoneGoal->isDigableForUnit($parRealObject)) {
	            $parErrorMessage = "goal zone is not digable";
	            return false;
    		}
        }
        return parent::getPathZoneId($parRealObject, $parZoneStart, $parZoneGoal, $parErrorMessage, $parResult);
    }
    
}
class DungeonAStarPathElement extends AStarPathElement {
    public function __construct(DungeonAStar $parAStar, $parObject, $parZoneId, $parZoneStart, $parZoneGoal) {
        $this->digCostMux = $parObject->getDigCost()/$parObject->getMoveCost();
    	parent::__construct($parAStar, $parObject, $parZoneId, $parZoneStart, $parZoneGoal);
    }
    
    protected function initCostForZone() {
        parent::initCostForZone();
        switch($this->zone->zoneType) {
        	//FIXME : this is a huge approximation, add object and dig cost !
        	case DungeonConstante::$ZONE_TYPE_DOOR:
	            global $gloObjectManager;
	            $locBlock = $gloObjectManager->getBlock($this->zone->blockId);
	            if($locBlock->allianceId == 0 || $locBlock->allianceId == $this->objectAllianceId) {
	            	break;
	            }
	            //Unit must dig
            case DungeonConstante::$ZONE_TYPE_ROCK :
            case DungeonConstante::$ZONE_TYPE_STONE :
            	$this->extraCost += $this->digCostMux*
            		max(1, 
            			ceil(
            			($this->zone->getRockStrength()-$this->zone->getDig() 
            			- DungeonConstante::$ZONE_DIG_UNIT_PER_ACTION)
            		/DungeonConstante::$ZONE_DIG_UNIT_PER_ACTION));
                break;
        }
        $this->extraCost = max(0, $this->extraCost);
    }

    protected function isADefinitiveClose() {
    	$locIsClose = parent::isADefinitiveClose();
    	if($locIsClose) {
    		if($this->zone->isDigableForUnit($this->object)) {
    			$locIsClose = false;
    		}
    	}
    	return $locIsClose;
    }
}
?>
Return current item: PhpBlock