Location: PHPKode > projects > PhpBlock > A9.8/modules/dungeon/dungeonfunction.php
<?php
include_once $PATH_TO_CODE."/script/message/playermessage.class.php";

include_once $PATH_TO_CODE."/modules/dungeon/dungeonserver.class.php";
function initializeAccountBuildDoor($firstX, $firstY, $secondX, $secondY, $z) {
	global $gloObjectManager;
	for($x = $firstX; $x <= $secondX; $x++) {
		for($y = $firstY; $y <= $secondY; $y++) {
			$locZoneToModify = $gloObjectManager->getZone(Zone::getZoneId($x, $y, $z));
			if($locZoneToModify && !$locZoneToModify->isGold()) {
				$locZoneToModify->setZoneType(DungeonConstante::$ZONE_TYPE_DOOR);
			}
		}
	}
}
function initializeAccountBuildStair($x, $y, $parZ) {
	global $gloObjectManager;
	for($z = $parZ - 2; $z <= $parZ+2; $z++) {
		$locZoneToModify = $gloObjectManager->getZone(Zone::getZoneId($x, $y, $z));
		if($locZoneToModify && !$locZoneToModify->isGold()) {
			$locZoneToModify->setZoneType(DungeonConstante::$ZONE_TYPE_STAIR);
		}
	}
}

function setZoneToPopulationArea($locZoneIdToModify, $parPlayerType, $parPopulation) {
	global $gloObjectManager;
	$locZoneToModify = $gloObjectManager->getZone($locZoneIdToModify);
	switch($parPlayerType) {
		case DungeonConstante::$PLAYER_TYPE_UNDEAD:
			$locZoneToModify->setZoneType(DungeonConstante::$ZONE_TYPE_NECROPOLIS);
			break;
		case DungeonConstante::$PLAYER_TYPE_DUNGEON_MASTER:
		case DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT:
			$locZoneToModify->setZoneType(DungeonConstante::$ZONE_TYPE_CITY);
			break;
	}
	$locZoneToModify->setPopulation($parPopulation);
}
/**
 *0 and -1 for human, fortress is at 0
 *-2 is neutral
 *-3 and -4 for ork or undead, fortress is at -4
 *=> 5 level
 */
function initializeAccount($parPlayerId, $parPlayerType) {
	global $gloObjectManager;
	$locPlayer = $gloObjectManager->getPlayer($parPlayerId);
	$locPlayer->setAllianceId($parPlayerId);
	$locPlayer->setPlayerType($parPlayerType);
	dungeonInitLanguageWithPlayer($locPlayer);

	$locServer = DungeonServer::getInstance();
	if(isset($_POST["startX"]) && $_POST["startX"]!="") {
		$locWillStartBlockId = Block::getBlockIdWithCenterCoord(
		getGetPost("startX", 0),
		getGetPost("startY", 0),
		getGetPost("startZ", 0)+Site::$ZONE_COORD_Z_GROUND_LEVEL);
		if(Block::exist($locWillStartBlockId)) {
			$locCenterZoneId = $locServer->getAnHappyHomeNearBlockId($parPlayerType, $locWillStartBlockId);
		} else {
			echo "$locWillStartBlockId".dgettext("dungeon", " is not a correct block to start because is out of the world")."<br>";
			exit(0);
		}
	} else {
		$locCenterZoneId = $locServer->getAnHappyHomeBlockId($parPlayerType);
	}

	//Verify that zone exist
	if(!Zone::staticCanExist($locCenterZoneId)) {
		echo dgettext("dungeon", "No account can be created for the moment, please retry in 10 minutes or contact administrator site.")."<br>";
		exit(0);
	}

	//Delete all already existing unit
	$locAlreadyExistingObject = Player::staticGetAllRealObject($parPlayerId, true);
	foreach($locAlreadyExistingObject as $locObject) {
		$locObject->deleteObject();
	}

	$locBlock = $gloObjectManager->getBlock($locCenterZoneId);
	list($locXCenter, $locYCenter, $locZ) = Block::getBlockCoord($locBlock->blockId);
	$locCenterZone = $gloObjectManager->getZone($locCenterZoneId);

	switch($parPlayerType) {
		case DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT:
			$locOtherZ = $locZ-1;
			break;
		default:
			$locOtherZ = $locZ+1;
	}
	$locOtherCenterZone = $gloObjectManager->getZone(Zone::getZoneId($locBlock->x, $locBlock->y, $locOtherZ));

	//All block in a range of 1 is taken
	$locTargetPopulationForBlockAround = DungeonConstante::$MAX_POPULATION_PER_ZONE;

	$locBlocksToConquerId = Block::getStaticBlockIdsFromXYZ($locCenterZoneId, 1, 1, 0);
	$locBlocksToConquerId = array_merge($locBlocksToConquerId, Block::getStaticBlockIdsFromXYZ($locOtherCenterZone->zoneId, 1, 1, 0));
	foreach($locBlocksToConquerId as $locBlockToConquerId) {
		$gloObjectManager->getBlock($locBlockToConquerId)->setPlayerId($parPlayerId);
		setZoneToPopulationArea($locBlockToConquerId, $parPlayerType, $locTargetPopulationForBlockAround);
	}

	///Forteress building
	for($x = $locXCenter - Constante::$HALF_BLOCK_SIZE-1; $x <= $locXCenter + Constante::$HALF_BLOCK_SIZE+1; $x++) {
		for($y = $locYCenter - Constante::$HALF_BLOCK_SIZE-1; $y <= $locYCenter + Constante::$HALF_BLOCK_SIZE+1; $y++) {
			for($z = $locZ - 1; $z <= $locZ+1; $z++) {
				$locZoneToModify = $gloObjectManager->getZone(Zone::getZoneId($x, $y, $z));
				if($locZoneToModify && !$locZoneToModify->isGold()) {
					$locZoneToModify->setZoneType(DungeonConstante::$ZONE_TYPE_STONE);
				}
			}
		}
	}

	//Door
	initializeAccountBuildDoor($locXCenter - Constante::$HALF_BLOCK_SIZE,
	$locYCenter - Constante::$HALF_BLOCK_SIZE,
	$locXCenter - Constante::$HALF_BLOCK_SIZE,
	$locYCenter + Constante::$HALF_BLOCK_SIZE, $locZ);
	initializeAccountBuildDoor($locXCenter + Constante::$HALF_BLOCK_SIZE,
	$locYCenter - Constante::$HALF_BLOCK_SIZE,
	$locXCenter + Constante::$HALF_BLOCK_SIZE,
	$locYCenter + Constante::$HALF_BLOCK_SIZE, $locZ);
	initializeAccountBuildDoor($locXCenter - Constante::$HALF_BLOCK_SIZE,
	$locYCenter - Constante::$HALF_BLOCK_SIZE,
	$locXCenter + Constante::$HALF_BLOCK_SIZE,
	$locYCenter - Constante::$HALF_BLOCK_SIZE, $locZ);
	initializeAccountBuildDoor($locXCenter - Constante::$HALF_BLOCK_SIZE,
	$locYCenter + Constante::$HALF_BLOCK_SIZE,
	$locXCenter + Constante::$HALF_BLOCK_SIZE,
	$locYCenter + Constante::$HALF_BLOCK_SIZE, $locZ);

	//Stair
	initializeAccountBuildStair($locXCenter + Constante::$HALF_BLOCK_SIZE+1, $locYCenter, $locZ);
	initializeAccountBuildStair($locXCenter - Constante::$HALF_BLOCK_SIZE-1, $locYCenter, $locZ);
	initializeAccountBuildStair($locXCenter, $locYCenter - Constante::$HALF_BLOCK_SIZE-1, $locZ);
	initializeAccountBuildStair($locXCenter, $locYCenter + Constante::$HALF_BLOCK_SIZE+1, $locZ);
	//Stair corner
	initializeAccountBuildStair($locXCenter + Constante::$HALF_BLOCK_SIZE+1, $locYCenter + Constante::$HALF_BLOCK_SIZE+1, $locZ);
	initializeAccountBuildStair($locXCenter - Constante::$HALF_BLOCK_SIZE-1, $locYCenter - Constante::$HALF_BLOCK_SIZE-1, $locZ);
	initializeAccountBuildStair($locXCenter + Constante::$HALF_BLOCK_SIZE+1, $locYCenter - Constante::$HALF_BLOCK_SIZE-1, $locZ);
	initializeAccountBuildStair($locXCenter - Constante::$HALF_BLOCK_SIZE-1, $locYCenter + Constante::$HALF_BLOCK_SIZE+1, $locZ);
	

	$locNearZoneCenterId=Zone::getZoneId($locXCenter+1, $locYCenter, $locZ);
	$locNearCenterZone = $gloObjectManager->getZone($locNearZoneCenterId);
	$locUnitZone1 = $gloObjectManager->getZone(Zone::getZoneId($locXCenter+Constante::$HALF_BLOCK_SIZE, $locYCenter, $locZ));
	$locUnitZone2 = $gloObjectManager->getZone(Zone::getZoneId($locXCenter-Constante::$HALF_BLOCK_SIZE, $locYCenter, $locZ));
	$locUnitZone3 = $gloObjectManager->getZone(Zone::getZoneId($locXCenter, $locYCenter+Constante::$HALF_BLOCK_SIZE, $locZ));
	$locUnitZone4 = $gloObjectManager->getZone(Zone::getZoneId($locXCenter, $locYCenter-Constante::$HALF_BLOCK_SIZE, $locZ));

	$locZoneBuildUnitList = array($locNearCenterZone, $locUnitZone1, $locUnitZone2, $locUnitZone3, $locUnitZone4);

	//Free center of start location
	$locListZoneIdToModify = Zone::getStaticZoneIdsFromXY($locCenterZoneId, 1, 1);
	foreach($locListZoneIdToModify as $locZoneIdToModify) {
		setZoneToPopulationArea($locZoneIdToModify, $parPlayerType, DungeonConstante::$MAX_POPULATION_PER_ZONE);
	}
	$i=0;
	foreach($locZoneBuildUnitList as $locZoneBuildUnit) {
		$i++;
		switch($parPlayerType) {
			case DungeonConstante::$PLAYER_TYPE_UNDEAD:
				if($i <= 2) {
					$locObjectId = Spectre::create($locZoneBuildUnit, $locPlayer->playerId, false);
				} else if($i <= 4) {
					$locObjectId = Skeleton::create($locZoneBuildUnit, $locPlayer->playerId, false);
				} else {
					$locObjectId = DeadWorm::create($locZoneBuildUnit, $locPlayer->playerId, false);
				}
				break;
			case DungeonConstante::$PLAYER_TYPE_DUNGEON_MASTER:
				if($i <= 2) {
					$locObjectId = WarOrk::create($locZoneBuildUnit, $locPlayer->playerId, false);
				} else if($i <= 4) {
					$locObjectId = Grunt::create($locZoneBuildUnit, $locPlayer->playerId, false);
				} else {
					$locObjectId = MinerOrk::create($locZoneBuildUnit, $locPlayer->playerId, false);
				}
				break;
			case DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT:
				if($i <= 2) {
					$locObjectId = Knight::create($locZoneBuildUnit, $locPlayer->playerId, false);
				} else if($i <= 4) {
					$locObjectId = Soldier::create($locZoneBuildUnit, $locPlayer->playerId, false);
				} else {
					$locObjectId = Miner::create($locZoneBuildUnit, $locPlayer->playerId, false);
				}
				break;
			default:
				echo dgettext("dungeon", "not correct player type");
				exit(0);
		}
		$locObject = $gloObjectManager->getRealObject($locObjectId);
		$locObject->addXP(50);
		$locObject->setActionPoint($locObject->getMaxActionPoint());
		$locObject->setFightPoint($locObject->getMaxFightActionPoint());
		$locObject->setHeroMode(1);
	}
	//echo "You begin at zone: ".$locCenterZone->toString()."<br><br>";
	$res = executer("SELECT zone.zoneId FROM block, zone WHERE block.blockId=zone.blockId AND block.playerId=$locPlayer->playerId AND RAND() < 0.1");
	while($row = mysql_fetch_array($res)) {
		$locZoneForCreatingAMonster = $gloObjectManager->getZone($row[0]);
		if(!$locZoneForCreatingAMonster->isOccupedWithMovable()) {
			createRandomUnit($locZoneForCreatingAMonster, $locPlayer);
		}
	}
	 
	$locBlock->setPlayerId($locPlayer->playerId);

	$locStartCash = DungeonPlayer::selectAverageIncomeForGoldMine()*20;
	switch($parPlayerType) {
		case DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT:
			$locPlayer->setCash(max(30000, $locStartCash));
			PlayerMessage::getInstance()->addPlayerMessage(0, $locPlayer->playerId,
			dgettext("dungeon", "You are a knight, ennemies is in the underground, not in surface, so go down !"));
			break;
		default:
			$locPlayer->setCash(max(26000, $locStartCash));
	}
	$locPlayer->setCash2(60);
	$locPlayer->setShame(0);

	//Delete all monsters !
	$res = $locBlock->getAllianceObjectNear(1, 4, 4, 4);
	while($row = mysql_fetch_array($res)) {
		$locObject = $gloObjectManager->getRealObject($row[0]);
		$locObject->deleteObject();
	}
	
	
	if(substr($locPlayer->getLanguage(), 0, 2) == "fr") {
		//TODO : use get text
		PlayerMessage::getInstance()->addPlayerMessage(0, $locPlayer->playerId, "
    Bonjour,<br/>
<p>Merci d'avoir créer un compte dans dungeon. Je vous donne les premières instructions pour avoir un bon
commencement : 
<ul>
<li>cliquer sur une unité (une bordure rouge apparaît)
<li>cliquer sur l'action de déplacement <img src='image/actiontab/move.png' width='32px' height='32px'/>
<li>cliquer sur la carte
<li>un chemin bleu apparaît
</ul>
</p>
Si vous avez des questions utilisez le <a href=http://sourceforge.net/forum/?group_id=186381>forum</a>.
</p>
<p>
Pour fermer ce message, cliquer sur le boutton 'message' ou sur <img src='image/interface/closewindow.png' width='20px' height='20px'/> qui est en haut de ce panneau.
</p>
");
	} else {
		PlayerMessage::getInstance()->addPlayerMessage(0, $locPlayer->playerId, "
    Hi,<br/>
<p>
Thanks to have create an account in dungeon. I give to you first instruction to get a good begin :
<ul>
 <li>click on a unit (a red border appear)
 <li>click on move action <img src='image/actiontab/move.png' width='32px' height='32px'/>
 <li>click on the map
 <li>a blue path is now show
</ul>
</p>
<p>
If you have other question use the <a href=http://sourceforge.net/forum/?group_id=186381>forum</a>
</p>
<p>
To close this message, just click on 'message' button or on the <img src='image/interface/closewindow.png' width='20px' height='20px'/> which is in the top of this panel
</p>
");
	}
	return true;
}

function dungeonInitLanguageWithPlayer(DungeonPlayer $parPlayer) {
	dungeonInitLanguage($parPlayer->getLanguage());
}
function dungeonInitLanguage($parLanguage="en_EN") {
	initLanguage($parLanguage);
	global $PATH_TO_CODE;
	bindtextdomain("dungeon", $PATH_TO_CODE."/modules/dungeon/translation");
}
function dungeonEchoAvailableLanguage($parSelected="en") {
	$locAvailableLanguage=array("hide@address.com", "français", "en", "english");
	for($i=0; $i < count($locAvailableLanguage); $i+=2) {
		echo "<option value='".$locAvailableLanguage[$i]."'";
		if($parSelected == $locAvailableLanguage[$i]) {
			echo " SELECTED";
		}
		echo ">".$locAvailableLanguage[$i+1];
		echo "</option>";
	}
}

function createRandomUnit($parZone, $parPlayer, $parXp=0) {
	switch(rand(1, 10)) {
		case 1:
			return createDigger($parZone, $parPlayer, RealObject::$IA_MODE_LOW, $parXp);
			break;
		case 2:
		case 3:
		case 4:
			return createWarrior($parZone, $parPlayer, RealObject::$IA_MODE_MEDIUM_AGRESSIVE, $parXp);
			break;
		default:
			return createStandard($parZone, $parPlayer, RealObject::$IA_MODE_MEDIUM, $parXp);
			break;
	}
}

function createDigger($parZone, $parPlayer, $parIAMode, $parXp=0) {
	global $gloObjectManager;
	Log::info("create digger unit on $parZone->zoneId for $parPlayer->playerName");
	switch($parPlayer->playerType) {
		case DungeonConstante::$PLAYER_TYPE_UNDEAD:
			$locObjectId = DeadWorm::create($parZone, $parPlayer->playerId, false);
			break;
		case DungeonConstante::$PLAYER_TYPE_DUNGEON_MASTER:
			$locObjectId = MinerOrk::create($parZone, $parPlayer->playerId, false);
			break;
		case DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT:
			$locObjectId = Miner::create($parZone, $parPlayer->playerId, false);
		default:
	}
	$locObject = $gloObjectManager->getRealObject($locObjectId);
	$locObject->setIAMode($parIAMode);
	$locObject->addXP($parXp);
	return $locObject;
}
function createWarrior($parZone, $parPlayer, $parIAMode, $parXp=0) {
	global $gloObjectManager;
	Log::info("create warrior unit on $parZone->zoneId for $parPlayer->playerName");
	switch($parPlayer->playerType) {
		case DungeonConstante::$PLAYER_TYPE_UNDEAD:
			$locObjectId = Spectre::create($parZone, $parPlayer->playerId, false);
			break;
		case DungeonConstante::$PLAYER_TYPE_DUNGEON_MASTER:
			$locObjectId = WarOrk::create($parZone, $parPlayer->playerId, false);
			break;
		case DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT:
			$locObjectId = Knight::create($parZone, $parPlayer->playerId, false);
		default:
	}
	$locObject = $gloObjectManager->getRealObject($locObjectId);
	$locObject->setIAMode($parIAMode);
	$locObject->addXP($parXp);
	return $locObject;
}
function createStandard($parZone, $parPlayer, $parIAMode, $parXp=0) {
	global $gloObjectManager;
	Log::info("create standard unit on $parZone->zoneId for $parPlayer->playerName");
	switch($parPlayer->playerType) {
		case DungeonConstante::$PLAYER_TYPE_UNDEAD:
			$locObjectId = Skeleton::create($parZone, $parPlayer->playerId, false);
			break;
		case DungeonConstante::$PLAYER_TYPE_DUNGEON_MASTER:
			$locObjectId = Grunt::create($parZone, $parPlayer->playerId, false);
			break;
		case DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT:
			$locObjectId = Soldier::create($parZone, $parPlayer->playerId, false);
		default:
	}
	$locObject = $gloObjectManager->getRealObject($locObjectId);
	$locObject->setIAMode($parIAMode);
	$locObject->addXP($parXp);
	return $locObject;
}
?>
Return current item: PhpBlock