Location: PHPKode > projects > PhpBlock > A9.8/script/block.class.php
<?php
include_once $PATH_TO_CODE."/script/fonction.php";
include_once $PATH_TO_CODE."/script/zone.class.php";
include_once $PATH_TO_CODE."/script/player.class.php";

include_once $PATH_TO_CODE."/script/imagemanager.class.php";

include_once $PATH_TO_CODE."/script/log.class.php";


class Block {
	public static $TYPE1_LOST_OF_CONTROL=1;
	public static $TYPE2_TIME_BEFORE_END=2;

	public function __construct($parBlockId) {
		$this->blockId = $parBlockId;
		Log::debug("Block::__construct this->blockId: $this->blockId");
		$this->initBlock();
	}

	private function initBlock() {
		$res = executer("SELECT x, y, z, playerId, allianceId, modifierImageCount, timestamp, isInLostOfControl, lostOfControlTimer, imagePath FROM block WHERE blockId=$this->blockId");
		$row = mysql_fetch_array($res);
		if(!$row) {
			die("Block->initBlock(): $this->blockId doesn't exist in database");
		}
		Log::debug("Block $this->blockId initBlock ".$row[0]);
		$this->x = $row[0];
		$this->y = $row[1];
		$this->z = $row[2];
		$this->playerId = $row[3];
		$this->allianceId = $row[4];
		$this->modifierImageCount = $row[5];
		$this->timestamp=$row[6];
		$this->isInLostOfControl=$row[7];
		$this->lostOfControlTimer=$row[8];
		$this->imagePath=$row[9];
	}

	public static function insertInfo($parBlockId, $parType1, $parType2, $parValue) {
		executer("REPLACE INTO blockInfo (blockId, type1, type2, value) VALUES ($parBlockId, $parType1, $parType2, $parValue)");
	}

	public function getInfo($parType1, $parType2) {
		$res = executer("SELECT value FROM blockInfo WHERE type1=$parType1 AND type2=$parType2 AND blockId=$this->blockId");
		$row = mysql_fetch_array($res);
		return $row[0];
	}
	public function setInfo($parType1, $parType2, $parValue) {
		executer("UPDATE blockInfo SET value=$parValue WHERE blockId=$this->blockId AND type1=$parType1 AND type2=$parType2");
	}
	public function addInfo($parType1, $parType2, $parValue) {
		executer("UPDATE blockInfo SET value=value+$parValue WHERE blockId=$this->blockId AND type1=$parType1 AND type2=$parType2");
	}

	public function createImage($parSize) {
		$this->debug("createImage", "parSize=$parSize");
		$parArrayOfDataType = array();

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

		$locImageManager = ImageManager::getInstance();
		global $gloObjectManager;
		$locImgBlock = imagecreatetruecolor($parSize, $parSize);

		$locZoneImageSize = $parSize/Constante::$BLOCK_SIZE;

		Log::debug("locBlock->x $this->x");
		Log::debug("locBlock->y $this->y");
		Log::debug("locBlock->z $this->z");

		$locXPixelCurrent = 0;

		for($xZone = $this->x - Constante::$HALF_BLOCK_SIZE;
		$xZone <= $this->x + Constante::$HALF_BLOCK_SIZE;
		$xZone++) {
			$locYPixelCurrent = 0;
			for($yZone = $this->y + Constante::$HALF_BLOCK_SIZE;
			$yZone >= $this->y - Constante::$HALF_BLOCK_SIZE;
			$yZone--) {
				Log::debug("Block::getImage $xZone $yZone $this->z");
				$locZoneId = Zone::getZoneId((int)$xZone, (int)$yZone, $this->z);

				Log::debug("Block::getImage $xZone $yZone => $locZoneId");

				$locZone = $gloObjectManager->getZone($locZoneId);
				Log::debug("Block::getImage $xZone $yZone<br>");

				$locImgZone = $locZone->getImage($locZoneImageSize, $parArrayOfDataType);
				if($locImgZone) {
					imagecopy($locImgBlock, $locImgZone, $locXPixelCurrent, $locYPixelCurrent, 0, 0, $locZoneImageSize, $locZoneImageSize);
					ImageDestroy($locImgZone);
				}

				$locYPixelCurrent += $locZoneImageSize;
			}
			$locXPixelCurrent += $locZoneImageSize;
		}

		Log::debug("Block($this->blockId)->getImage|setImage");
		$locImageManager->setBlockImage($locImgBlock, $this);
	}

	public function setImagePath($parImagePath) {
		$this->imagePath=$parImagePath;
		executer("UPDATE block SET imagePath='$parImagePath' WHERE blockId=".$this->blockId);
	}
	public function isSurface() {
		return $this->z == Site::$ZONE_COORD_Z_GROUND_LEVEL;
	}
	public static function getImageColor($parSize, $parColor, $parAlpha, $parLostOfControlTickRemaining=-1) {
		$locImgColor = imagecreatetruecolor($parSize, $parSize);
		imagealphablending( $locImgColor, false );
		imagesavealpha($locImgColor, true);
		$locImageColor1 = &getImageColorFromHexWithAlpha($locImgColor,
		Constante::getColor(intval(substr($parColor, 0, 2))), $parAlpha);
		$locImageColor2 = &getImageColorFromHexWithAlpha($locImgColor,
		Constante::getColor(intval(substr($parColor, 2, 2))), $parAlpha);
		$locImageColor3 = &getImageColorFromHexWithAlpha($locImgColor,
		Constante::getColor(intval(substr($parColor, 4, 2))), $parAlpha);
		imagefilledrectangle($locImgColor, 0, 0, $parSize*1/3, $parSize,$locImageColor1);
		imagefilledrectangle($locImgColor, $parSize*1/3, 0, $parSize*2/3, $parSize,$locImageColor2);
		imagefilledrectangle($locImgColor, $parSize*2/3, 0, $parSize, $parSize,$locImageColor3);

		if($parLostOfControlTickRemaining > 0) {
			$locImagePath = Constante::getPathToImage()."/lostofcontrol.png";
			$locPirate = imagecreatefrompng($locImagePath);

			list($locWidthImageFromFile, $locHeightImageFromFile, $locType, $locAttr) = getimagesize($locImagePath);

			$locColorText= imagecolorallocate($locPirate, 0, 0, 255);
			imagestring($locPirate, 100, 1, 1, "$parLostOfControlTickRemaining", $locColorText);
			if($parLostOfControlTickRemaining > 9) {
				imagestring($locPirate, 100, $locWidthImageFromFile-18, 1, "$parLostOfControlTickRemaining", $locColorText);
			} else {
				imagestring($locPirate, 100, $locWidthImageFromFile-10, 1, "$parLostOfControlTickRemaining", $locColorText);
			}

			imagecopyresampled($locImgColor, $locPirate,
			($parSize-$locWidthImageFromFile)/2,
			($parSize-$locHeightImageFromFile)/2,
			0, 0, $locWidthImageFromFile, $locHeightImageFromFile,
			$locWidthImageFromFile, $locHeightImageFromFile);
		}
		imagealphablending( $locImgColor, true);
		return $locImgColor;
	}

	public function getUniqueString($parSize, &$parArrayOfDataType) {
		if(in_array(Constante::$MAP_MODE_SATELLITE_VIEW, $parArrayOfDataType))
		{
			//return "$modifierImageCount".Constante::$SEP_URL_UNIQUE_STRING."$parSize".Constante::$SEP_URL_UNIQUE_STRING.Constante::$MAP_MODE_SATELLITE_VIEW;
			return "$parSize".Constante::$SEP_URL_UNIQUE_STRING.Constante::$MAP_MODE_SATELLITE_VIEW;
		} else
		{
			//return "$modifierImageCount".Constante::$SEP_URL_UNIQUE_STRING."$parSize";
			return "$parSize";
		}
	}

	public function setPlayerId($parPlayerId) {
		if($parPlayerId != $this->playerId) {
			if($parPlayerId == 0) {
				$this->allianceId = 0;
				$this->playerId = 0;
			} else {
				$locPlayer = ObjectManager::getInstance()->getPlayer($parPlayerId);
				$this->allianceId = $locPlayer->allianceId;
				$this->playerId = $locPlayer->playerId;
			}
			$this->info("setPlayerId", "allianceId:".$this->allianceId."  playerId: ".$this->playerId);
			executer("UPDATE block SET playerId = $parPlayerId, allianceId=$this->allianceId WHERE blockId=$this->blockId ");
			$this->blockHasBeenModified();
		}
	}

	public function blockHasBeenModified() {
		$locImageManager = ImageManager::getInstance();
		$locImageManager->addImageBlockToGenerate($this->blockId);
		RealObject::forceTakeDecisionNearInBlock($this->blockId);
	}

	public function getAllZoneIdOfZoneType($parZoneType) {
		$locZoneIds = array();
		$res = executer("SELECT zoneId FROM zone WHERE blockId = $this->blockId AND zoneType=$parZoneType");
		while($row = mysql_fetch_array($res)) {
			array_push($locZoneIds, $row[0]);
		}
		return $locZoneIds;
	}

	public function setAllianceId($parAllianceId) {
		$this->allianceId = $parAllianceId;
		executer("UPDATE block SET allianceId = $parAllianceId WHERE blockId=$this->blockId");
		$this->blockHasBeenModified();
	}

	public function getBlockIdsWhereClauseList($parBlockRangeX, $parBlockRangeY)
	{
		$locBlockIds = Block::getStaticBlockIdsFromXY($this->blockId, $parBlockRangeX, $parBlockRangeY);
		$locRequestPieceWithBlockIdIn = " (";
		foreach($locBlockIds as $locBlockId)
		{
			$locRequestPieceWithBlockIdIn.="$locBlockId,";
		}
		$locRequestPieceWithBlockIdIn.="-1)";
		return $locRequestPieceWithBlockIdIn;
	}

	public function getAllRealObjectId() {
		$locRealObjectIdArray = array();
		$res = executer("SELECT objectId FROM object WHERE blockId=$this->blockId AND canTakeControl > 0 AND isLobotomize<>".RealObject::$UNIT_IS_DEAD);
		while($row=mysql_fetch_array($res)) {
			array_push($locRealObjectIdArray, $row[0]);
		}
		return $locRealObjectIdArray;
	}

	public static function getStaticBlockIdsWhereClauseList($parBlockId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ=0)
	{
		return getWhereClauseList(Block::getStaticBlockIdsFromXYZ($parBlockId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ));
	}

	public static function getStaticBlockIdsFromXY($parBlockId, $parBlockRangeX, $parBlockRangeY) {
		return Block::getStaticBlockIdsFromXYZ($parBlockId, $parBlockRangeX, $parBlockRangeY, 0);
	}

	public static function getStaticBlockIdsFromXYZ($parBlockId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ=0)
	{
		list($locBlockX, $locBlockY, $locBlockZ) = Block::getBlockCoord($parBlockId);
		$locBlockArray = array();
		for($locX = max(Constante::getMinBlockX(), $locBlockX - $parBlockRangeX*Constante::$BLOCK_SIZE);
		$locX <= min(Constante::getMaxBlockX(), $locBlockX + $parBlockRangeX*Constante::$BLOCK_SIZE);
		$locX += Constante::$BLOCK_SIZE) {
			for($locY = max(Constante::getMinBlockY(), $locBlockY - $parBlockRangeY*Constante::$BLOCK_SIZE);
			$locY <= min(Constante::getMaxBlockY(), $locBlockY + $parBlockRangeY*Constante::$BLOCK_SIZE);
			$locY += Constante::$BLOCK_SIZE) {
				for($locZ = max(Constante::getMinZ(), $locBlockZ - $parBlockRangeZ);
				$locZ <= min(Constante::getMaxZ(), $locBlockZ + $parBlockRangeZ);
				$locZ ++) {
					if(Zone::staticCanExistXYZ($locX, $locY, $locZ)) {
						array_push($locBlockArray, Block::getBlockIdWithCenterCoord($locX, $locY, $locZ));
					}
				}
			}
		}
		return $locBlockArray;
	}

	public function getBlockIds($parBlockRange)
	{
		return Block::getStaticBlockIdsFromXY($this->blockId, $parBlockRange, $parBlockRange);
	}

	public static function getBlockIdWithCenterCoord($parX, $parY, $parZ)
	{
		return Zone::getZoneId($parX, $parY, $parZ);
	}

	public static function getBlockCoord($parBlockId)
	{
		return Zone::getZoneCoord($parBlockId);
	}

	public static function exist($parBlockId)
	{
		if($parBlockId) {
			$res = executer("SELECT blockId FROM block WHERE blockId=$parBlockId");
			$row = mysql_fetch_array($res);
			if($row) {
				return $row[0];
			}
		}
		return false;
	}

	public function debug($parMethodName, $parMessage) {
		Log::debug("Block($this->blockId)::$parMethodName => $parMessage");
	}
	public function info($parMethodName, $parMessage) {
		Log::info("Block($this->blockId)::$parMethodName => $parMessage");
	}

	public static function getStaticAllZoneId($parBlockId) {
		return Zone::getStaticZoneIdsFromXY($parBlockId, Constante::$HALF_BLOCK_SIZE, Constante::$HALF_BLOCK_SIZE);
	}

	public function getAllZoneId() {
		return Zone::getStaticZoneIdsFromXY($this->blockId, Constante::$HALF_BLOCK_SIZE, Constante::$HALF_BLOCK_SIZE);
	}
	public function getAllZone() {
		global $gloObjectManager;
		$locZoneArray = array();
		$locZoneIdArray = Zone::getStaticZoneIdsFromXY($this->blockId, Constante::$HALF_BLOCK_SIZE, Constante::$HALF_BLOCK_SIZE);
		foreach($locZoneIdArray as $locZoneId) {
			$locZoneArray[$locZoneId] = $gloObjectManager->getZone($locZoneId);
		}
		return $locZoneArray;
	}
	public static function getAllBlocks($parPlayerId) {
		global $gloObjectManager;
		$locBlockArray = array();
		$locBlockIdArray = Block::getAllBlocksId($parPlayerId);
		foreach ($locBlockIdArray as $locBlockId) {
			array_push($locBlockArray, $gloObjectManager->getBlock($locBlockId));
		}
		return $locBlockArray;
	}
	public static function getAllBlocksId($parPlayerId) {
		$locBlockIdArray = array();
		$res = executer("SELECT blockId FROM block WHERE playerId=$parPlayerId");
		while($row=mysql_fetch_array($res)) {
			array_push($locBlockIdArray, $row[0]);
		}
		return $locBlockIdArray;
	}

	/**@return a mysql res with objectId*/
	public function getAllianceObjectNear($parAllianceId, $parBlockRangeX=0, $parBlockRangeY=0, $parBlockRangeZ=0, $parObjectType=-1) {
		return executer("SELECT objectId, zoneId
		FROM object
		WHERE object.allianceId=$parAllianceId ".
		($parObjectType==-1?"":" AND objectType=$parObjectType")."
			AND isLobotomize<>".RealObject::$UNIT_IS_DEAD." 
			AND canTakeControl>0 
			AND object.blockId IN ".Block::getStaticBlockIdsWhereClauseList($this->blockId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ));
	}
	public function getAllianceObjectNearCount($parAllianceId, $parBlockRangeX=0, $parBlockRangeY=0, $parBlockRangeZ=0, $parObjectType=-1) {
		$this->getAllianceObjectNear($parAllianceId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ, $parObjectType);
		return mysql_affected_rows();
	}
	/**
	 * @return mysql res with objectId, zoneId, isBattling
	 */
	public function getOtherAllianceObjectNear($parAllianceId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ) {
		return executer("SELECT objectId, zoneId, isBattling
		FROM object
		WHERE object.allianceId > 0
		AND object.allianceId<>$parAllianceId
		AND isLobotomize<>".RealObject::$UNIT_IS_DEAD."
			AND canTakeControl>0 
			AND object.blockId IN ".Block::getStaticBlockIdsWhereClauseList($this->blockId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ));
	}
	public function isOtherAllianceObjectNear($parAllianceId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ) {
		$res = $this->getOtherAllianceObjectNear($parAllianceId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ);
		return mysql_fetch_array($res);
	}

	public function getOtherAllianceBlockNear($parAllianceId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ, $parLostOfControlValue=false) {
		$req = "SELECT blockId FROM block
		WHERE block.allianceId > 0 AND block.allianceId<>$parAllianceId
		AND block.blockId IN ".Block::getStaticBlockIdsWhereClauseList($this->blockId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ);
		if($parLostOfControlValue) {
			$req .= " AND block.isInLostOfControl > 0";
		} else {
			$req .= " AND block.isInLostOfControl = 0";
		}
		return executer($req);
	}
	public function getAllianceBlockNear($parAllianceId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ, $parLostOfControl=false) {
		$req = "SELECT blockId FROM block
		WHERE block.allianceId > 0 AND block.allianceId=$parAllianceId
		AND block.blockId IN ".Block::getStaticBlockIdsWhereClauseList($this->blockId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ);
		if($parLostOfControl) {
			$req .= " AND block.isInLostOfControl > 0";
		} else {
			$req .= " AND block.isInLostOfControl = 0";
		}
		return executer($req);
	}
	public function isOtherAllianceBlockNear($parAllianceId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ) {
		$res = $this->getOtherAllianceBlockNear($parAllianceId, $parBlockRangeX, $parBlockRangeY, $parBlockRangeZ);
		return mysql_fetch_array($res);
	}

	public function getPlayersIdAround($parRangeX, $parRangeY, $parRangeZ) {
		return Block::staticGetPlayersIdAround($this->blockId, $parRangeX, $parRangeY, $parRangeZ);
	}
	public static function staticGetBlockIdWithZoneId($parZoneId) {
		list($x, $y, $z) = Zone::getZoneCoord($parZoneId);
		$locBlockX = floor(($x+Constante::$HALF_BLOCK_SIZE)/Constante::$BLOCK_SIZE)*Constante::$BLOCK_SIZE;
		$locBlockY = floor(($y+Constante::$HALF_BLOCK_SIZE)/Constante::$BLOCK_SIZE)*Constante::$BLOCK_SIZE;
		return Block::getBlockIdWithCenterCoord($locBlockX, $locBlockY, $z);
	}
	public static function staticGetPlayersIdAround($parBlockId, $parRangeX, $parRangeY, $parRangeZ) {
		$locPlayersId = array();
		global $gloObjectManager;

		$locBlockArray = Block::getStaticBlockIdsFromXYZ($parBlockId, $parRangeX, $parRangeY, $parRangeZ);
		foreach($locBlockArray as $locBlockId) {
			$locPlayerId = $gloObjectManager->getBlock($locBlockId)->playerId;
			if($locPlayerId > 0 && !in_array($locPlayerId, $locPlayersId)) {
				array_push($locPlayersId, $locPlayerId);
			}
		}
		return $locPlayersId;
	}

	public static function create($parBlockId, $parZoneGenerator) {
		list($xBlock, $yBlock, $zBlock) = Block::getBlockCoord($parBlockId);
		executer("INSERT INTO block(playerId, blockId, x, y, z, modified) VALUES(0, $parBlockId, $xBlock, $yBlock, $zBlock, 1)");
		$locImageManager = ImageManager::getInstance();
		$locImageManager->createDirectories($parBlockId, 0);

		for($x=-Constante::$HALF_BLOCK_SIZE+$xBlock; $x<=Constante::$HALF_BLOCK_SIZE+$xBlock; $x++) {
			for($y=-Constante::$HALF_BLOCK_SIZE+$yBlock; $y<=Constante::$HALF_BLOCK_SIZE+$yBlock; $y++) {
				$locZoneId = Zone::getZoneId($x, $y, $zBlock);
				$parZoneGenerator->createZone($parBlockId, $locZoneId);
			}
		}
	}
}
?>
Return current item: PhpBlock