MySQL Forums
Forum List  »  Newbie

Optimisch queries
Posted by: fdas fdsa
Date: November 26, 2015 02:58PM

Hello,
I have made a php script that generates a map. A tempel is short of a control tower the tower with more believe(power) will win more land. There are a lot of other scripts involved but they are not intresting.
The generation of the map will take about 6 minutes but the cpu usage is only at 20%. Is there a better way to make this script.

The table Bl_Chunks is the map.
It has X and Y varibles for location.
BelieveFromArea is for how much control is there
Faction is who is the owner of the land.

CREATE TABLE `Bl_Chunks` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`X` smallint(6) NOT NULL,
`Z` smallint(6) DEFAULT NULL,
`HasTemple` tinyint(1) DEFAULT '0',
`TotalBelieve` int(10) unsigned DEFAULT '0',
`BelieveFromArea` int(10) unsigned DEFAULT '0',
`BelieveIncrease` int(10) unsigned NOT NULL DEFAULT '0',
`Faction` text NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `unique_ID` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=334843 DEFAULT CHARSET=latin1


-------------------------------------------------

<?php
echo "<br>";
$start = microtime(true);
set_time_limit(10000);

//Sends a query
function SqlGet($conn,$sql,$return) {
try {
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
if ($return) {
return $stmt->fetchAll();
} else {
return array();
}


}catch (PDOException $e){
echo "Connection failed: " . $e->getMessage();
return array();
}
}

//This funtion will calculate how much believe a piece of land gets from a tempel based on the distance and the power of the tempel
function GetEffect($chunk,$tempel) {
$tempelpower = $tempel['TotalBelieve'];
$distance = sqrt(pow($tempel['X'] - $chunk['X'], 2) + pow($tempel['Z'] - $chunk['Z'], 2));
$distance = $distance/5;
if ($distance>=20) {
return 0;
}else {
return $tempelpower / (pow(2, $distance));
}
}

//Gets all the tempels
$tempels = SqlGet($conn,"SELECT * FROM Bl_Chunks WHERE HasTemple=1",true);








//Goes trow each line.
//You can't select all of the chunks(pieces of land) because it would be to much data.
for ($x=-182; $x<= 193; $x++) {
//Select a line of chunks
$chunks = SqlGet($conn,"SELECT * FROM Bl_Chunks WHERE X=$x AND HasTemple=0",true);
foreach ($chunks as $chunk) {
$bestvalue = 0;
$faction = 0;
$id = $chunk['ID'];
//Goes throw each tempels and looks wich is the best.
foreach ($tempels as $tempel) {
$value = GetEffect($chunk, $tempel);
if ($value > $bestvalue) {
$bestvalue = $value;
$faction = $tempel['Faction'];
}
}

$intbest = 0;
if (floor($bestvalue)==0) {
$intbest = 0;
$faction = 0;
}else {
$intbest=floor($bestvalue);
}
//Updates the chunk
$sql = "UPDATE Bl_Chunks SET BelieveFromArea=$intbest,Faction=$faction WHERE ID= $id";
SqlGet($conn, $sql, false);
}
SqlGet($conn, $sql , false);
}

?>

-------------------------------------------------

Thank you in advanced

Options: ReplyQuote


Subject
Written By
Posted
Optimisch queries
November 26, 2015 02:58PM
November 28, 2015 03:13PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.