crazy loops....

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. :slight_smile:

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]

Do you have a specific problem or question regarding your code. I don’t think anyone is going to debug or rewrite your entire game for you.

No problems I’m aware of–according to my ide-- nor would I ask for anyone to write anything for me, as that takes the fun out of it… its more of a question about theory

Use arrays mostly to build each sector, once built create your string to insert into the db.

Since you use a string and do an array conversion this will be a lot slower than it needs to be

Changed it up a bit and tested it, and was surprised to find that when testing at 15x15 tiles it had finished in absolutely no time at all… But still open to hear ideas of doing it better. :slight_smile:

[php]
$galaxyName = ‘Empire’;
$xAxis = 15;
$yAxis = 15;
$asteroid = 1;
function getObject($xAx, $yAx)
{
$sun = ‘x=11&y=10’;
$planet = array(‘x=2&y=3’, ‘x=2&y=12’, ‘x=6&y=7’, ‘x=7&y=14’, ‘x=10&y=7’, ‘x=14&y=2’);
$moon = array(‘x=2&y=4’, ‘x=4&y=3’, ‘x=4&y=12’, ‘x=6&y=8’, ‘x=7&y=5’, ‘x7=&y=13’, ‘x=14&y=3’);
$stargate = array(
‘x=2&y=8’ => ‘2’,
‘x=13&y=2’ => ‘3’,
‘x=14&y=14’ => ‘4’,
);

$loc = "x={$xAx}&y={$yAx}";
if ($sun == $loc){
    return 1;
} else if (in_array($loc, $planet))
{
    return 2;
} else if (in_array($loc, $moon))
{
    return 3;
} else if (array_key_exists($loc, $stargate))
{
    return 4;
} else {
    return 0;
}

}
echo ‘

’;
$query = (“INSERT INTO clusters (id, name, owner, status) VALUES (’’, ‘{$galaxyName}’, ‘0’, ‘1’)”);
$result = mysql_query($query);
if (!$result)
{
die(mysql_error());
}
$clusterID = mysql_insert_id();
echo “Galaxy Cluster successfully created!
”;
for ($xAx = 1; $xAx <= $xAxis; $xAx++)
{
for ($yAx = 1; $yAx <= $yAxis; $yAx++)
{
$setSun = 0;
$setPlanet = 0;
$setMoon = 0;
$setGate = 0;
$object = getObject($xAx, $yAx);
switch($object)
{
case 1:
$setSun = 1;
break;
case 2:
$setPlanet = rand(2,11);
break;
case 3:
$setMoon = rand(1, 8);
break;
case 4:
$setGate = “1”;
$thisLocation = “x={$xAx}&y={$yAx}”;
$gquery = (“INSERT INTO stargates (id, location, locGalaxy, destination, destGalaxy) VALUES (’’, ‘{$thisLocation}’, ‘{$clusterID}’, ‘’, ‘’)”);
if (!mysql_query($gquery))
{
echo “Error creating star gate” . mysql_error();
die;
}
echo “Stargate successfully created!
”;
break;
case 0:
$field = rand(1, 50);
if ($field == 1)
{
$field = rand(1,5);
} else {
$field = 0;
}
}
$squery = ("INSERT INTO galaxies
(id, galaxyName, sun, planet, moon, stargate, asteroidfield, xaxis, yaxis)
VALUES
(’’, ‘{$clusterID}’, ‘{$setSun}’, ‘{$setPlanet}’, ‘{$setMoon}’, ‘{$setGate}’, ‘{$field}’, ‘{$xAx}’, ‘{$yAx}’)
");
$nresult = mysql_query($squery);
if ($nresult)
{
$i++;
} else {
die(mysql_error());
}
}
}
[/php]

if 15x15 ran very fast, try a 50x50 or 100x100 and see how fast it really loads.

Wait… $stargate? What game is this o.o

Sponsor our Newsletter | Privacy Policy | Terms of Service