Location: PHPKode > projects > PhpBlock > A9.8/modules/dungeon/dungeonzone.class.php
<?php
include_once $PATH_TO_CODE."/script/zone.class.php";
include_once $PATH_TO_CODE."/modules/dungeon/dungeonconstante.class.php";
include_once $PATH_TO_CODE."/modules/dungeon/dungeonblock.class.php";

class DungeonZone extends Zone{

	public function __construct($parZoneId=false) {
		parent::__construct($parZoneId);
	}

	public function initZoneWithRow($zone, $row) {
		DungeonZone::staticGetZoneWithRow($this, $row);
	}

	public static function staticGetZoneWithRow($zone, $row) {
		Zone::staticGetZoneWithRow($zone, $row);
		$zone->population = $row[9];
	}
	public static function staticGetResForBlockId($parBlockId) {
		return executer("SELECT zoneId, x, y, z, zoneType, blockId, occuped, occupedWithMovable, gold, population FROM zone WHERE blockId=$parBlockId");
	}
	public function getDig() {
		return $this->getInfo(
		DungeonConstante::$ZONE_INFO_TYPE1_DIG,
		DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_DIG);
	}
	public function dig($parValue) {
		if($parValue <> 0) {
			$this->addInfo(
			DungeonConstante::$ZONE_INFO_TYPE1_DIG,
			DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_DIG,
			$parValue);
			if($this->getDig() >= ($this->getRockStrength() - DungeonConstante::$ZONE_DIG_UNIT_PER_ACTION)) {
				$this->setZoneType(Constante::$ZONE_TYPE_PLAIN);
			}
			$this->zoneRockHasBeenModified();
		}
	}
	public function resetDig() {
		$this->dig(-$this->getDig());
	}
	public function setRockStrength($parValue) {
		$this->setInfo(
		DungeonConstante::$ZONE_INFO_TYPE1_ROCK_STRENGTH,
		DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_ROCK_STRENGTH,
		$parValue);
		$this->zoneRockHasBeenModified();
	}
	private function addRockStrength($parValue) {
		$this->addInfo(
		DungeonConstante::$ZONE_INFO_TYPE1_ROCK_STRENGTH,
		DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_ROCK_STRENGTH,
		$parValue);
		$this->zoneRockHasBeenModified();
	}
	public function getRockStrength() {
		return $this->getInfo(
		DungeonConstante::$ZONE_INFO_TYPE1_ROCK_STRENGTH,
		DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_ROCK_STRENGTH);
	}

	public function addLocalZoneStrength($parValue, $parRangeX=1, $parRangeY=1) {
		$this->addRockStrength($parValue);
		/*
		 global $gloObjectManager;
		 $listOfZoneIdAround = Zone::getStaticZoneIdsFromXYZ($this->zoneId, $parRangeX, $parRangeY, 0);
		 $listOfZoneIdUpAndDown = Zone::getStaticZoneIdsFromXYZ($this->zoneId, 0, 0, 1);
		 foreach($listOfZoneIdUpAndDown as $locZoneId) {
			array_push($listOfZoneIdAround, $locZoneId);
			}
			$listOfZoneIdAround = array_unique($listOfZoneIdAround);
			foreach($listOfZoneIdAround as $locZoneId) {
			if($this->zoneId == $locZoneId) {
			$this->addRockStrength($parValue);
			} else {
			//Use this test to avoid during world expansion some problem
			$locZone = $gloObjectManager->getZone($locZoneId);
			if($locZone) {
			$locValueToAdd = $parValue/(Zone::getRange3d($locZoneId, $this->zoneId));
			if($locValueToAdd > 0) {
			$locValueToAdd = sqrt($locValueToAdd);
			} else {
			$locValueToAdd = -sqrt(-$locValueToAdd);
			}
			$locZone->addRockStrength(round(1.25*$locValueToAdd));
			}
			}
			}*/
	}

	public function getDisplayedRockStrength() {
		return DungeonZone::staticGetDisplayedRockStrength($this->getRockStrength(), $this->getDig());
	}
	public static function staticGetDisplayedRockStrength($parRockStrengh, $parDig) {
		return round(($parRockStrengh - $parDig)/100);
	}
	/**@deprecated*/
	public function createRockImage($parSize) {
		$this->debug("createImage", "parSize=$parSize");
		$parArrayOfDataType = array();

		$locZoneUniqueString = $this->getUniqueString($parSize, $parArrayOfDataType);

		$locImageManager = ImageManager::getInstance();
		global $gloObjectManager;

		$locImgZone = imagecreatetruecolor($parSize, $parSize);
		imagealphablending($locImgZone, false);
		imagesavealpha($locImgZone, true);

		if($this->getDisplayedRockStrength() >= 100) {
			$locImageColorTransparent = imagecolorallocatealpha($locImgZone, 0, 255, 0, 90);
		} else {
			$locImageColorTransparent = imagecolorallocatealpha($locImgZone, 255, 0, 0, max(0, min(127, $this->getDisplayedRockStrength()-20)));
		}

		imagefill($locImgZone, 0, 0, $locImageColorTransparent);
		imagealphablending($locImgZone, true);

		$locTextColor = imagecolorallocate($locImgZone, 0, 0, 255);
		//bool imagestring ( resource image, int font, int x, int y, string s, int col )
		imagestring($locImgZone, 3, $parSize/8, $parSize/3, $this->getDisplayedRockStrength(), $locTextColor);

		$locImageManager->setRockZoneImage($locImgZone, $this->blockId, $this->zoneId, $locZoneUniqueString);
	}

	public function getRockImage($parSize) {
		Log::debug("Zone($this->zoneId)::getImage");
		$locImageManager = ImageManager::getInstance();
		$locZoneUniqueString = $this->getUniqueString($parSize);
		$locImgZone = $locImageManager->getRockImage($this->blockId, $this->zoneId, $locZoneUniqueString);

		return $locImgZone;
	}

	public function getUniqueString($parSize) {
		return "$parSize";
	}

	public function initZoneType($parNewZoneType) {
		if(DungeonConstante::$ZONE_TYPE_CITY != $parNewZoneType
		&&  DungeonConstante::$ZONE_TYPE_NECROPOLIS != $parNewZoneType
		&& $this->population > 0) {
			$this->setPopulation(0);
		}
		$this->debug("initZoneType", "parNewZoneType=$parNewZoneType");
		switch($parNewZoneType) {
			case DungeonConstante::$ZONE_TYPE_RUINS:
				break;
			case DungeonConstante::$ZONE_TYPE_GOLD_EXPLOITED:
			case DungeonConstante::$ZONE_TYPE_GOLD:
			case DungeonConstante::$ZONE_TYPE_ROCK:
				$this->addLocalZoneStrength(DungeonConstante::$ZONE_ROCK_STRENGTH_FOR_ROCK);
				$this->occuped();
				$this->resetDig();
				break;
			case DungeonConstante::$ZONE_TYPE_DOOR:
				$this->addLocalZoneStrength(DungeonConstante::$ZONE_ROCK_STRENGTH_FOR_DOOR, 1, 1);
				$this->resetDig();
				break;
			case DungeonConstante::$ZONE_TYPE_STONE:
				$this->addLocalZoneStrength(DungeonConstante::$ZONE_ROCK_STRENGTH_FOR_STONE, 2, 2);
				$this->occuped();
				$this->resetDig();
				break;
			case DungeonConstante::$ZONE_TYPE_BARRACK:
				$this->occuped();
				break;
			default:
				return parent::initZoneType($parNewZoneType);
		}
	}

	public function isFullZoneType() {
		return $this->isGold()
		|| $this->zoneType == DungeonConstante::$ZONE_TYPE_STONE
		|| $this->zoneType == DungeonConstante::$ZONE_TYPE_ROCK;
	}
	public function isGold() {
		return $this->zoneType == DungeonConstante::$ZONE_TYPE_GOLD_EXPLOITED
		|| $this->zoneType == DungeonConstante::$ZONE_TYPE_GOLD;
	}
	public function isEarthquakable() {
		return !($this->isFullZoneType()
		|| $this->zoneType == DungeonConstante::$ZONE_TYPE_BARRACK
		|| $this->zoneType == DungeonConstante::$ZONE_TYPE_STAIR
		||
		(($this->zoneType == DungeonConstante::$ZONE_TYPE_TREASURE
		|| $this->zoneType == DungeonConstante::$ZONE_TYPE_CITY
		|| $this->zoneType == DungeonConstante::$ZONE_TYPE_NECROPOLIS)
		&& $this->population > 0));
	}
	public function isDigableForUnit($parUnit) {
		if($this->zoneType != DungeonConstante::$ZONE_TYPE_ROCK
		&& $this->zoneType != DungeonConstante::$ZONE_TYPE_STONE
		&& $this->zoneType != DungeonConstante::$ZONE_TYPE_DOOR) {
			return false;
		}
			
		global $gloObjectManager;
		$locBlock = $gloObjectManager->getBlock($this->blockId);
		if($parUnit->playerId == $locBlock->playerId) {
			return $this->zoneType == DungeonConstante::$ZONE_TYPE_ROCK
			|| $this->zoneType == DungeonConstante::$ZONE_TYPE_STONE;
		} else {
			return $this->zoneType == DungeonConstante::$ZONE_TYPE_ROCK
			|| $this->zoneType == DungeonConstante::$ZONE_TYPE_STONE
			|| $this->zoneType == DungeonConstante::$ZONE_TYPE_DOOR;
		}
	}

	protected function eraseCurrentZoneType() {
		$this->resetDig();
		$this->debug("eraseCurrentZoneType", "this->zoneType=".DungeonZone::staticGetZoneTypeToString($this->zoneType));
		global $gloObjectManager;
		switch($this->zoneType) {
			case DungeonConstante::$ZONE_TYPE_RUINS:
				//Nothing todo
				break;
			case DungeonConstante::$ZONE_TYPE_GOLD_EXPLOITED:
			case DungeonConstante::$ZONE_TYPE_GOLD:
			case DungeonConstante::$ZONE_TYPE_ROCK:
				$this->addLocalZoneStrength(-DungeonConstante::$ZONE_ROCK_STRENGTH_FOR_ROCK);
				$this->leave();
				$this->resetDig();
				break;
			case DungeonConstante::$ZONE_TYPE_DOOR:
				$this->addLocalZoneStrength(-DungeonConstante::$ZONE_ROCK_STRENGTH_FOR_DOOR, 1, 1);
				$this->resetDig();
				break;
			case DungeonConstante::$ZONE_TYPE_STONE:
				$this->addLocalZoneStrength(-DungeonConstante::$ZONE_ROCK_STRENGTH_FOR_STONE, 2, 2);
				$this->leave();
				$this->resetDig();
				break;
			case DungeonConstante::$ZONE_TYPE_BARRACK:
				$this->leave();
				break;
			default:
				return parent::eraseCurrentZoneType();
		}
	}

	/**
	 * set population, maxmum at 10000
	 * @parPopulation
	 */
	public function setPopulation($parPopulation) {
		if($parPopulation != $this->population) {
			$locDecreasePopulation = $parPopulation < $this->population;
			$this->population = max(0, min(DungeonConstante::$MAX_POPULATION_PER_ZONE, $parPopulation));
			executer("update zone set population=$this->population where zoneId=$this->zoneId");
			if(0 == $this->population) {
				$this->setZoneType(Constante::$ZONE_TYPE_PLAIN);
			}
			$this->zoneHasBeenModified();
			if($locDecreasePopulation) {
				if($this->population <= 0) {//no more population on this block
					global $gloObjectManager;
					$locBlock = $gloObjectManager->getBlock($this->blockId);
					if($locBlock->getPopulation() == 0) {
						$locBlock->setPlayerId(0);
					}
				}
			}
		}
	}

	/**
	 * @return population for this zone
	 */
	public function getPopulation() {
		return $this->population;
	}

	public function getZoneTypeImagePath() {
		switch($this->zoneType) {
			case DungeonConstante::$ZONE_TYPE_CITY:
			case DungeonConstante::$ZONE_TYPE_NECROPOLIS:
				if($this->zoneType == DungeonConstante::$ZONE_TYPE_CITY) {
					$locImagePath = "city";
					if(!$this->isSurface()) {
						$locImagePath .= "underground";
					}
				} else {
					$locImagePath = "necropolis";
				}
				$locPopulation = $this->getPopulation();
				if($locPopulation <= 0) {
					$locImagePath .= "0";
				} elseif ($locPopulation <= 2000) {
					$locImagePath .= "1";
				} elseif ($locPopulation <= 4000) {
					$locImagePath .= "2";
				} elseif ($locPopulation <= 6000) {
					$locImagePath .= "3";
				} elseif ($locPopulation <= 8000) {
					$locImagePath .= "4";
				} else {
					$locImagePath .= "5";
				}
				$locImagePath .= ".png";
				break;
			case DungeonConstante::$ZONE_TYPE_TREASURE:
				$locImagePath = "necropolis";
				$locMaxGold = $this->getMaxGold();
				if($this->gold == 0) {
					$locImagePath .= "0";
				} elseif ($this->gold < 1/5*$locMaxGold) {
					$locImagePath .= "1";
				} elseif ($this->gold < 2/5*$locMaxGold) {
					$locImagePath .= "2";
				} elseif ($this->gold < 3/5*$locMaxGold) {
					$locImagePath .= "3";
				} elseif ($this->gold < 4/5*$locMaxGold) {
					$locImagePath .= "4";
				} else {
					$locImagePath .= "5";
				}
				$locImagePath .= ".png";
				break;
			case DungeonConstante::$ZONE_TYPE_DOOR:
				$locImagePath = "zonedoor.png";
				break;
			case DungeonConstante::$ZONE_TYPE_STAIR:
				$locImagePath = "zonestair.png";
				break;
			case DungeonConstante::$ZONE_TYPE_RUINS:
				$locImagePath = "zoneruins.png";
				break;
			case DungeonConstante::$ZONE_TYPE_ROCK:
				$locImagePath = "zonerock.png";
				break;
			case DungeonConstante::$ZONE_TYPE_GOLD:
				$locImagePath = "zonegold.png";
				break;
			case DungeonConstante::$ZONE_TYPE_GOLD_EXPLOITED:
				$locImagePath = "zonegoldexploited.png";
				break;
			case DungeonConstante::$ZONE_TYPE_BARRACK:
				if($this->isSurface()) {
					$locImagePath = "zonebarracksurface.png";
				} else {
					$locImagePath = "zonebarrackunderground.png";
				}
				break;
			case DungeonConstante::$ZONE_TYPE_ROAD:
				if($this->isSurface()) {
					$locImagePath = "zoneplainroad.png";
				} else {
					$locImagePath = "zoneundergroundroad.png";
				}
				break;
			case DungeonConstante::$ZONE_TYPE_STONE:
				$locImagePath = "zonestone.png";
				break;
			case Constante::$ZONE_TYPE_PLAIN:
				if($this->isSurface()) {
					$locImagePath = "zoneplain.png";
				} else {
					$locImagePath = "zoneunderground.png";
				}
				break;
			default:
				return parent::getZoneTypeImagePath();
		}
		global $PATH_TO_IMAGE;
		$locImagePath = $PATH_TO_IMAGE."/$locImagePath";
		return $locImagePath;
	}
	public function getMaxGold() {
		//FIXME use player type or an another variable
		if($this->zoneType == DungeonConstante::$ZONE_TYPE_TREASURE) {
			return DungeonConstante::$TREASURE_MAX_GOLD;
		} else if($this->zoneType == DungeonConstante::$ZONE_TYPE_CITY) {
			return DungeonConstante::$CITY_MAX_GOLD;
		} else {
			return DungeonConstante::$NECROPOLIS_MAX_GOLD;
		}
	}

	/**use addGold*/
	public function setGold($parNewValue) {
		$this->addGold(-$this->gold+$parNewValue);
	}
	/**If new gold is null then new zone type is plain*/
	public function addGold($parFlux) {
		$locImagePath = $this->getZoneTypeImagePath();
		$this->gold+=$parFlux;
		executer("UPDATE zone SET gold=$this->gold WHERE zoneId=$this->zoneId");
		if($this->gold <= 0) {
			$this->setZoneType(Constante::$ZONE_TYPE_PLAIN);
			$this->zoneHasBeenModified();
		} else {
			if($locImagePath != $this->getZoneTypeImagePath()) {
				$this->zoneHasBeenModified();
			}
		}
	}

	public static function staticGetZoneTypeToString($parZoneType) {
		switch($parZoneType) {
			case DungeonConstante::$ZONE_TYPE_RUINS:
				$locResult.= "ruins";
				break;
			case DungeonConstante::$ZONE_TYPE_GOLD_EXPLOITED:
				$locResult.= "exploited gold mine";
				break;
			case DungeonConstante::$ZONE_TYPE_GOLD:
				$locResult.= "gold mine";
				break;
			case DungeonConstante::$ZONE_TYPE_ROCK:
				$locResult.= "rock";
				break;
			case DungeonConstante::$ZONE_TYPE_STONE:
				$locResult.= "stone";
				break;
			case DungeonConstante::$ZONE_TYPE_BARRACK:
				$locResult.= "barrack";
				break;
			case DungeonConstante::$ZONE_TYPE_TREASURE:
				$locResult.= "treasure";
				break;
			case DungeonConstante::$ZONE_TYPE_CITY:
				$locResult.= "city";
				break;
			case DungeonConstante::$ZONE_TYPE_NECROPOLIS:
				$locResult.= "necropolis";
				break;
			case DungeonConstante::$ZONE_TYPE_ROAD:
				$locResult.= "road";
				break;
			case DungeonConstante::$ZONE_TYPE_DOOR:
				$locResult.= "door";
				break;
			case DungeonConstante::$ZONE_TYPE_STAIR:
				$locResult.= "stair";
				break;
			default:
				return Zone::staticGetZoneTypeToString($parZoneType);
		}
		return $locResult;
	}
	public function getZoneTypeToString() {
		return DungeonZone::staticGetZoneTypeToString($this->zoneType);
	}

	public static function create($parZoneId, $parBlockId, $parZoneType, $parFree) {
		list($x, $y, $z) = Zone::getZoneCoord($parZoneId);

		Zone::create($parZoneId, $parBlockId, $parZoneType, $parFree);

		Zone::insertInfo($parZoneId,
		DungeonConstante::$ZONE_INFO_TYPE1_ROCK_STRENGTH,
		DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_ROCK_STRENGTH,
		DungeonConstante::$ZONE_ROCK_STRENGTH_START);
		/*
		 Zone::insertInfo($parZoneId,
		 DungeonConstante::$ZONE_INFO_TYPE1_ROCK_IMAGE,
		 DungeonConstante::$ZONE_INFO_TYPE2_ROCK_HAS_BEEN_MODIFIED,
		 1);
		 */
		Zone::insertInfo($parZoneId,
		DungeonConstante::$ZONE_INFO_TYPE1_DIG,
		DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_DIG,
		0);
	}


	/**
	 *  @param parBlock is present to modify it
	 */
	public function zoneRockHasBeenModified() {
		global $gloObjectManager;
		$locBlock = $gloObjectManager->getBlock($this->blockId);
		$locBlock->blockRockHasBeenModified();
		//$this->setInfo(DungeonConstante::$ZONE_INFO_TYPE1_ROCK_IMAGE, DungeonConstante::$ZONE_INFO_TYPE2_ROCK_HAS_BEEN_MODIFIED, 1);
	}
	public function toStringForAlliance($parAllianceId) {
		$locResult = parent::toStringForAlliance($parAllianceId)."\nrock strength=".$this->getDisplayedRockStrength();
		if($this->gold > 0) {
			$locResult.= ", gold: $this->gold (max: ".$this->getMaxGold().")";
		}
		if($this->population > 0) {
			$locResult.= ", population: ".round($this->population/100);
		}
		return $locResult;
	}

	//TODO : add as paramater in depend of type somewhere !
	public function isHole() {
		if($this->zoneType == Constante::$ZONE_TYPE_PLAIN) {
			return $this->isAboveEmpty();
		}
		return false;
	}

	public function isAboveEmpty() {
		$locBottomZone = $this->getBottomZone();
		if($locBottomZone) {
			return ($locBottomZone->zoneType == DungeonConstante::$ZONE_TYPE_ROAD
			|| $locBottomZone->zoneType == DungeonConstante::$ZONE_TYPE_STAIR
			|| $locBottomZone->zoneType == Constante::$ZONE_TYPE_PLAIN);
		}
	}
	public function isAffectedByUpAndDownCost() {
		if($this->zoneType == DungeonConstante::$ZONE_TYPE_STAIR) {
			return false;
		}
		return parent::isAffectedByUpAndDownCost();
	}

	public function getMoveCostFactor() {
		$locResult = parent::getMoveCostFactor();
		switch($this->zoneType) {
			case DungeonConstante::$ZONE_TYPE_NECROPOLIS:
			case DungeonConstante::$ZONE_TYPE_CITY:
			case DungeonConstante::$ZONE_TYPE_TREASURE:
				$locResult = DungeonConstante::$GOLD_ROOM_MOVE_COST;
				break;
			case DungeonConstante::$ZONE_TYPE_STAIR :
				$locResult *= DungeonConstante::$STAIR_ZONE_MOVE_COST_FACTOR;
				break;
			case DungeonConstante::$ZONE_TYPE_ROAD :
				$locResult *= DungeonConstante::$ROAD_ZONE_MOVE_COST_FACTOR;
				break;
			case DungeonConstante::$ZONE_TYPE_DOOR :
				$locResult *= DungeonConstante::$DOOR_ZONE_MOVE_COST_FACTOR;
			default:
		}
		if($this->isSurface()) {
			$locResult*=DungeonConstante::$SURFACE_ZONE_MOVE_COST_FACTOR;
		}
		return $locResult;
	}
	public function isAccessibleForIt($parRealObject) {
		if($this->zoneType == DungeonConstante::$ZONE_TYPE_DOOR) {
			global $gloObjectManager;
			$locBlock = $gloObjectManager->getBlock($this->blockId);
			if($locBlock->allianceId > 0) {
				if($locBlock->allianceId != $parRealObject->allianceId) {
					return false;
				}
			}
		}
		return parent::isAccessibleForIt($parRealObject);
	}
}
?>
Return current item: PhpBlock