/**
* Calculate planet diameter.
*
* [USER=850422]return[/USER] int
*/
public function calculatePlanetDiameter()
{
return (int)floor(1000 * sqrt($this->calculatePlanetMaxFields()));
}
/**
* Calculate galaxy, system, planet.
*/
private function calculatePlanetCoordinates()
{
//Get last set positions.
$lastSetGalaxy = settings('last_galaxy_position');
$lastSetSystem = settings('last_system_position');
$lastSetPlanet = settings('last_planet_position');
//If counter is bigger that max galaxy, return counter to 1.
if ($lastSetGalaxy > settings('max_galaxy')) {
$lastSetGalaxy = 1;
}
//If counter is bigger that max systems, return counter to 1.
if ($lastSetSystem > settings('max_systems')) {
$lastSetSystem = 1;
}
//Do while position is not free.
do {
$position = mt_rand(round(settings('max_planets') * 0.2), round(settings('max_planets') * 0.8));
if ($lastSetPlanet < 3) {
$lastSetPlanet += 1;
} else {
if ($lastSetSystem >= settings('max_systems')) {
$lastSetSystem = 1;
if ($lastSetGalaxy >= settings('max_galaxy')) {
$lastSetGalaxy = 1;
} else {
$lastSetGalaxy += 1;
}
} else {
$lastSetSystem += 1;
}
}
//Check is position free?
$isPositionFree = (Planet::where('galaxy', $lastSetGalaxy)->where('system', $lastSetSystem)->where('planet', $position)->get()->count() > 1) ? $isPositionFree = false : $isPositionFree = true;
//Do loop, if position is found, stop.
} while ($isPositionFree === false);
//Position found, write them to variable.
$this->galaxy = $lastSetGalaxy;
$this->system = $lastSetSystem;
$this->planet = $position;
//Update last valid generated positons.
$this->updateLastSetPositions($lastSetGalaxy, $lastSetSystem, $position);
}