Started working on a game I’ve been meaning to create for a long time and put together this map generator… I haven’t tested any of this and quite frankly I’m a little scared to as I’m worried about it timing out, so I figured since I don’t need it for a while I’d throw it up here and see if anyone had any suggestions for making it more efficient or if you see any errors/things obviously missing feel free to throw that out as well.
Oh and don’t mind the variables being empty =P
[php]$galaxyName = null; //enter galaxy name
$sun = null; //enter coordinates: ‘x=1&y=1’
$planet = null; //enter array of locations ‘x=1&y=1’
$moon = null; //enter array of locations ‘x=1&y=1’
$xAxis = null; //enter number or rows on the xAxis
$yAxis = null; //enter number of the rows on the yAxis
$stargate = null; //enter array of locations ‘x=1&y=1 => destination galaxy(must have a 2 way connection)’
$asteroid = null; //enter 1 to include random asteroids, 2 to disclude them.
/**
*create galaxy and insert into db
*/
for ($xAx = 1; $xAx <= $xAxis; $xAx++)
{
for ($yAx = 1; $yAx <= $yAxis; $yAx++)
{
//check if this sector should be a sun
if (isset($sunLoc))
{
$sunLoc = explode("&", $sun);
foreach (sunLoc as $k => $v)
{
$sunGetLoc = explode("=", $v);
foreach ($sunGetLoc as $key => $value)
{
$$key = $value;
}
}
if ($xAx == $x && $yAx == $y)
{
$setSun = 1;
} else {
$setSun = 0;
}
}
//check if this sector should be a planet
if (isset($planet)) {
foreach ($planet as $k => $v)
{
$pLoc = explode("&", $v);
foreach ($pLoc as $key => $value)
{
$plLoc = explode("=", $value);
foreach ($plLoc as $kk => $vv)
{
$$kk = $vv;
}
if ($xAx == $x && $yAx == $y)
{
$setPlanet = 1;
break;
} else {
$setPlanet = 0;
}
}
if ($setPlanet == 1)
{
break;
}
}
}
//check if this sector should be a moon
if (isset($moon)) {
foreach ($moon as $k => $v)
{
$mLoc = explode("&", $v);
foreach ($mLoc as $key => $value)
{
$moLoc = explode("=", $value);
foreach ($moLoc as $kk => $vv)
{
$$kk = $vv;
}
if ($xAx == $x && $yAx == $y)
{
$setMoon = 1;
break;
} else {
$setMoon = 0;
}
}
if ($setMoon == 1)
{
break;
}
}
}
//check if this sector should be a stargate
if (isset($stargate)) {
foreach ($stargate as $k => $v)
{
$starLoc = explode("&", $v);
foreach ($starLoc as $key => $value)
{
$staLoc = explode("=", $key);
foreach ($staLoc as $kk => $vv)
{
$$kk = $vv;
}
if ($xAx == $x && $yAx == $y)
{
$setGate = “1&{$value}”;
break;
} else {
$setGate = 0;
}
}
if ($setGate != 0)
{
break;
}
}
}
//check if this sector isn’t occupied and decide if should be an asteroid field
if ($asteroid == 1 && $setSun != 1 && $setPlanet != 1 && $setMoon !=1 && $setGate != 1)
{
$field = rand(1,20);
if ($field != 1)
{
$field = 0;
}
}
$query = ("INSERT INTO galaxies
(id
, galaxyName
, sun
, planet
, moon
, stargate
, asteroidfield
, xaxis
, yaxis
)
VALUES
(’’, ‘{$galaxyName}’, ‘{$setSun}’, ‘{$setPlanet}’, ‘{$setMoon}’, ‘{$setGate}’, ‘{$field}’, ‘{$xAx}’, ‘{$yAx}’)
");
$result = mysql_query($query);
if ($result)
{
echo “Coordinate Created
”;
}
}
}[/php]