<?php
include_once $PATH_TO_CODE."/script/block.class.php";
include_once $PATH_TO_CODE."/modules/dungeon/dungeonconstante.class.php";
include_once $PATH_TO_CODE."/modules/dungeon/dungeonzone.class.php";
class DungeonBlock extends Block {
public function __construct($parBlockId) {
parent::__construct($parBlockId);
$this->rockModifierImageCount = 0;
}
public function setRockImagePathNum($parPathNum) {
$this->setInfo(
DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE,
DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_PATH_NUM,
$parPathNum);
}
public function getRockImagePathNum() {
return $this->getInfo(
DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE,
DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_PATH_NUM);
}
public function setRockModifierImageCount($parRockModifierImageCount) {
return $this->setInfo(
DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE,
DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_MODIFIER_COUNT,
$parRockModifierImageCount);
}
public function getRockModifierImageCount() {
return $this->getInfo(
DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE,
DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_MODIFIER_COUNT);
}
public function getImagePathIdentifier() {
return "0-".ImageManager::$ROCK_IMAGE_LETTER.$this->getRockImagePathNum()."-".$this->getRockModifierImageCount();
}
public function createRockImage($parSize) {
$this->debug("createImage", "parSize=$parSize");
Log::debug("locBlock->x $this->x");
Log::debug("locBlock->y $this->y");
Log::debug("locBlock->z $this->z");
$parArrayOfDataType = array();
$locBlockUniqueString = $this->getUniqueString($parSize, $parArrayOfDataType);
$locImageManager = ImageManager::getInstance();
global $gloObjectManager;
$locImgBlock = imagecreatetruecolor($parSize, $parSize);
imagealphablending($locImgBlock, false);
imagesavealpha($locImgBlock, true);
$locImageColorTransparent = imagecolorallocatealpha($locImgBlock, 0, 255, 0, 0);
imagefill($locImgBlock, 0, 0, $locImageColorTransparent);
//imagealphablending($locImgBlock, true);
$locZoneImageSize = $parSize/Constante::$BLOCK_SIZE;
$locTextColor = imagecolorallocatealpha($locImgBlock, 0, 0, 255, 0);
$res = executer("SELECT zoneId, value FROM zoneInfo WHERE zoneId IN ".getWhereClauseList($this->getAllZoneId())." AND type1=".DungeonConstante::$ZONE_INFO_TYPE1_ROCK_STRENGTH." AND type2=".DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_ROCK_STRENGTH);
$resDig = executer("SELECT zoneId, value FROM zoneInfo WHERE zoneId IN ".getWhereClauseList($this->getAllZoneId())." AND type1=".DungeonConstante::$ZONE_INFO_TYPE1_DIG." AND type2=".DungeonConstante::$ZONE_INFO_TYPE2_CURRENT_DIG);
while($row = mysql_fetch_array($res)) {
$rowDig = mysql_fetch_array($resDig);
$locZoneId = $row[0];
$locStrength = DungeonZone::staticGetDisplayedRockStrength($row[1], $rowDig[1]);
list($locX, $locY, $locZ) = Zone::getZoneCoord($locZoneId);
$locXPixelCurrent = ($locX-($this->x - Constante::$HALF_BLOCK_SIZE))*$locZoneImageSize;
$locYPixelCurrent = (($this->y + Constante::$HALF_BLOCK_SIZE)-$locY)*$locZoneImageSize;
if($locStrength >= 100) {
$locImageColorTransparent = imagecolorallocatealpha($locImgBlock, 0, 255, 0, 90);
} else {
$locImageColorTransparent = imagecolorallocatealpha($locImgBlock, 255, 0, 0, max(0, min(127, $locStrength-20)));
}
imagefilledrectangle($locImgBlock, $locXPixelCurrent, $locYPixelCurrent, $locXPixelCurrent+$locZoneImageSize-1, $locYPixelCurrent+$locZoneImageSize-1, $locImageColorTransparent);
if($locStrength >= 1000) {
$locStrength = (round($locStrength/100)/10)."k";
}
//bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring($locImgBlock, 3, $locXPixelCurrent+$locZoneImageSize/8, $locYPixelCurrent+$locZoneImageSize/3, "$locStrength", $locTextColor);
}
Log::debug("Block($this->blockId)->getImage|setImage");
$locImageManager->setRockBlockImage($locImgBlock, $this);
}
/**
* @return always false for allianceId<2
*/
public function containsGoldAndIsOwnableForAllianceId($parAllianceId) {
if($parAllianceId < 2) {
//Monster or neutral
return false;
}
//Search gold
if(count($this->getAllZoneIdOfZoneType(DungeonConstante::$ZONE_TYPE_GOLD)) > 0) {
return !$this->testIsInLostOfControlForAllianceId($parAllianceId);
}
}
public function getGold() {
global $gloObjectManager;
$locGold = 0;
$locAllZoneId = $this->getAllZoneId();
foreach($locAllZoneId as $locZoneId) {
$locZone = $gloObjectManager->getZone($locZoneId);
$locGold += $locZone->gold;
}
return $locGold;
}
public function setPlayerId($parPlayerId) {
parent::setPlayerId($parPlayerId);
global $gloObjectManager;
if($parPlayerId == 0) {
//Unactivated all gold mine
$locGoldExploitedList = $this->getAllZoneIdOfZoneType(DungeonConstante::$ZONE_TYPE_GOLD_EXPLOITED);
foreach($locGoldExploitedList as $locZoneId) {
$gloObjectManager->getZone($locZoneId)->setZoneType(DungeonConstante::$ZONE_TYPE_GOLD);
}
$locTreasureAndOtherList = $this->getAllZoneIdOfZoneType(DungeonConstante::$ZONE_TYPE_TREASURE);
foreach($locTreasureAndOtherList as $locZoneId) {
$gloObjectManager->getZone($locZoneId)->setGold(0);
}
//Reset population
$locTreasureAndOtherList = $this->getAllZoneIdOfZoneType(DungeonConstante::$ZONE_TYPE_CITY);
foreach($locTreasureAndOtherList as $locZoneId) {
$gloObjectManager->getZone($locZoneId)->setGold(0);
$gloObjectManager->getZone($locZoneId)->setPopulation(0);
}
$locTreasureAndOtherList = $this->getAllZoneIdOfZoneType(DungeonConstante::$ZONE_TYPE_NECROPOLIS);
foreach($locTreasureAndOtherList as $locZoneId) {
$gloObjectManager->getZone($locZoneId)->setGold(0);
$gloObjectManager->getZone($locZoneId)->setPopulation(0);
}
//Remove barrack
$locTreasureAndOtherList = $this->getAllZoneIdOfZoneType(DungeonConstante::$ZONE_TYPE_BARRACK);
foreach($locTreasureAndOtherList as $locZoneId) {
$gloObjectManager->getZone($locZoneId)->setZoneType(Constante::$ZONE_TYPE_PLAIN);
}
} else {
//Activated all gold mine
$locGoldExploitedList = $this->getAllZoneIdOfZoneType(DungeonConstante::$ZONE_TYPE_GOLD);
foreach($locGoldExploitedList as $locZoneId) {
$gloObjectManager->getZone($locZoneId)->setZoneType(DungeonConstante::$ZONE_TYPE_GOLD_EXPLOITED);
}
}
}
public function removeLostOfControlTime($parTimeToRemove)
{
$this->lostOfControlTimer -= $parTimeToRemove;
executer("UPDATE block SET lostOfControlTimer = $this->lostOfControlTimer WHERE blockId = $this->blockId");
}
public function setLostOfControl($initialTimer)
{
$this->lostOfControlTimer = $initialTimer;
if($this->isInLostOfControl == 0) {
PlayerMessage::getInstance()->addPlayerInfo($this->playerId,
"one of your block is conquering by an another player", PlayerMessage::$BAD, $this->blockId);
}
$this->isInLostOfControl = 1;
executer("UPDATE block SET isInLostOfControl = 1, lostOfControlTimer = $this->lostOfControlTimer WHERE blockId = $this->blockId");
$this->blockHasBeenModified();
}
public function removeLostOfControl()
{
if($this->isInLostOfControl == 1) {
PlayerMessage::getInstance()->addPlayerInfo($this->playerId, "block is fully in your control", PlayerMessage::$GOOD, $this->blockId);
}
$this->lostOfControlTimer = 0;
$this->isInLostOfControl = 0;
executer("UPDATE block SET isInLostOfControl = 0, lostOfControlTimer = 0 WHERE blockId = $this->blockId");
$this->blockHasBeenModified();
}
/**Test if no unit of the alliance and if unit of other alliance of the owner alliance
*@return if owner block is military in lost of control
*/
public function testIsInLostOfControl() {
return $this->testIsInLostOfControlForAllianceId($this->allianceId);
}
/**Test if no unit of the alliance and if unit of other alliance of the owner alliance
*@param $parAllianceId is to test lost of control for this allianceId
*@return if block is military in lost of control
*/
public function testIsInLostOfControlForAllianceId($parAllianceId) {
$locIsOneUnitOfAlliance = executer("SELECT 1 FROM object WHERE isBackground=0 AND isLobotomize=0 AND allianceId=$parAllianceId AND blockId=$this->blockId AND canTakeControl>0 LIMIT 1");
if(!$this->atLeastOneUnitCanTakeControl($parAllianceId)) {
$isOneUnitOfOtherAlliance = executer("SELECT 1 FROM object WHERE isBackground=0 AND isLobotomize=0 AND allianceId<>$parAllianceId AND allianceId > 0 AND blockId=$this->blockId AND canTakeControl>0 LIMIT 1");
return mysql_fetch_array($isOneUnitOfOtherAlliance);
}
return false;
}
public function atLeastOneUnitCanTakeControl($parAllianceId) {
$locIsOneUnitOfAlliance = executer("SELECT 1 FROM object WHERE isBackground=0 AND isLobotomize=0 AND allianceId=$parAllianceId AND blockId=$this->blockId AND canTakeControl>0 LIMIT 1");
return mysql_fetch_array($locIsOneUnitOfAlliance);
}
public function getPopulation() {
$res = executer("SELECT SUM(population) FROM zone WHERE blockId=$this->blockId");
if(!$row = mysql_fetch_array($res)) {
//TODO : exit ?
return 0;
}
return $row[0];
}
public function getAveragePopulation() {
$res = executer("SELECT AVG(population) FROM zone WHERE blockId=$this->blockId AND population > 0");
if(!$row = mysql_fetch_array($res)) {
return 0;
}
return $row[0];
}
public function getAllZoneWithPopulation() {
$locZoneArrayWithPopulation = array();
$locZoneArray = parent::getAllZone();
foreach($locZoneArray as $locZone) {
if($locZone->getPopulation() > 0) {
array_push($locZoneArrayWithPopulation, $locZone);
}
}
return $locZoneArrayWithPopulation;
}
public function isInLostOfControl()
{
return $this->isInLostOfControl == 1;
}
public function getLostOfControlTime()
{
return $this->lostOfControlTimer;
}
public function lostOfControl()
{
global $gloObjectManager;
$locPlayerOfBlock = $gloObjectManager->getPlayer($this->playerId);
$locPlayerOfBlock->addCash(-$this->getGold()*DungeonConstante::$PILLAGE_RATIO_FOR_TARGET_PLAYER);
//TODO : create a sette
$this->isInLostOfControl=0;
executer("UPDATE block SET isInLostOfControl = 0, lostOfControlTimer = 0 WHERE blockId = $this->blockId");
$this->setPlayerId(0);
$this->blockHasBeenModified();
}
public function blockRockHasBeenModified() {
executer("UPDATE blockInfo
SET value=1
WHERE blockId=$this->blockId
AND type1=".DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE."
AND type2=".DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_HAS_BEEN_MODIFIED);
}
public static function create($parBlockId, $parZoneGenerator) {
list($x, $y, $z) = Zone::getZoneCoord($parBlockId);
Block::create($parBlockId, $parZoneGenerator);
Block::insertInfo(
$parBlockId,
DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE,
DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_HAS_BEEN_MODIFIED,
1);
Block::insertInfo(
$parBlockId,
DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE,
DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_PATH_NUM,
0);
Block::insertInfo(
$parBlockId,
DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE,
DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_MODIFIER_COUNT,
0);
Block::insertInfo(
$parBlockId,
DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE,
DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_TIMESTAMP,
0);
}
}
?>