PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,512 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,946 people online right now. Registration is fast and FREE... Join Now!




Image Map for a Game

 

Image Map for a Game

brandon99337

23 Nov, 2008 - 08:02 PM
Post #1

D.I.C Head
**

Joined: 14 Feb, 2008
Posts: 167



Thanked: 3 times
My Contributions
I've been trying to think up ways to make a map for a PHP driven browser game that I'm creating. I've tried everything from a CSS divs to HTML tables to place the squares of my map.

We've already made up the code for a very big randomized map, we're driving for something like the Travian map...

It's going to be multiplayer so once the map is created it has to be the same map being used for all the players...

I believe that if I switch to an image map, my previous work will be of no use, as the end result was something like this
Created Map

This is our random map generator (which would only be ran once every other year or so)

CODE

<?php
// map_test v1.0.0 started 11th October 2007
// This will generate random maps.

$start_time = time();
$direction_array = array(9,8,1,2,7,3,6,5,4);

$dbh=mysql_connet ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db_name");

$direction = -1;
$land_count = 0;

// Find out if we have some data already and if so start checking the oldest islands.
$query = "SELECT * FROM map_master ORDER BY id";
$result = mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");

if (mysql_num_rows($result) > 0)
    {
    // We've got some data already so need to find a blank location.
    while ( $dbrow = mysql_fetch_array($result) )
        {
        $id = $dbrow['id'];
        $x_coord = $dbrow['x'];
        $y_coord = $dbrow['y'];
        $direction = -1;
        $continue = 'true';
        while ($continue == 'true')
        {
            if ($direction == -1)  //Only do this the first time.
                {
                $direction = rand(1,8); //Pick a direction from NW to W to look in.
                }
            switch ($direction)
                {
                case 1: //NW
                $x_check = $x_coord+1;
                $y_check = $y_coord-1;
                break;
                case 2: //N
                $x_check = $x_coord+1;
                $y_check = $y_coord;
                break;
                case 3: //NE
                $x_check = $x_coord+1;
                $y_check = $y_coord+1;
                break;
                case 4: //E
                $x_check = $x_coord;
                $y_check = $y_coord+1;
                break;
                case 5: //SE
                $x_check = $x_coord-1;
                $y_check = $y_coord+1;
                break;
                case 6: //S
                $x_check = $x_coord-1;
                $y_check = $y_coord;
                break;
                case 7: //SW
                $x_check = $x_coord-1;
                $y_check = $y_coord-1;
                break;
                case 8: //W
                $x_check = $x_coord;
                $y_check = $y_coord-1;
                break;
                }

            // Do a select to see if this island has been generated
            $query2 = "SELECT * FROM map_master WHERE x = '$x_check' AND y = '$y_check'";
echo "<br>$query</br>\n";
            $result2 = mysql_query($query2) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query2");

            if (mysql_num_rows($result2) == 0)
                {
                 // we can generate an island here, break out of the loop.
                 $x_coords = $x_check;
                 $y_coords = $y_check;
                 break;
                }
            else
                {
                $found++;
                if ($found == 8)
                    {
                    $found = 0;
                    $continue = 'false';
                     // We looked in all 8 adjacent squares and all are populated so break out.
                        }
                }

            $direction++;
            if ($direction==9)
                {
                $direction = 1;
                }
            }
        }
    }
else  // there is no data in the table so start with 0,0.
    {
    $x_coords = 0;
    $y_coords = 0;
    }

// We've now got the coordinates for a blank island so generate it.

echo "<br>We're going to generate an island at $x_coords , $y_coords<br>\n";

// Calculate the starting coordinates, the center is 0, 0 and each island is 101 x 101 in size.
$x_start = ($x_coords*100) - 50;
$y_start = ($y_coords*100) - 50;

// Insert a new row into map_master for this island.

$query = "INSERT into map_master (x, y) VALUES ('$x_coords','$y_coords')";
mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");
echo "<br>$query<br>\n";

// We need to get the id for this new location.

$query = "SELECT * FROM map_master WHERE x = '$x_coords' AND y = '$y_coords'";
$result = mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");

$id = mysql_result($result,0,'id');

// Loop through all the locations for this island making them sea, height -1.

$x_end = $x_start + 101;
$y_end = $y_start + 101;

// This is all sea so set to -1;

$height = -1;

echo "<br>x_start =  $x_start and x_end = $x_end<br>\n";

echo "<br>y_start =  $y_start and y_end = $y_end<br>\n";


for ($x_loop=$x_start; $x_loop < $x_end; $x_loop++)
    {
      // horizontal loop

    for ($y_loop = $y_start; $y_loop < $y_end; $y_loop++)
        {
         // vertical loop.
         $query = "INSERT into map_terrain (map_no,x, y, terrain, height) VALUES ('$id','$x_loop','$y_loop', 'sea', '$height')";
         mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");

        }    // end of $y_loop

    }      // end of $x_loop

// Find the approximate centre of this map and plot a height

$x_centre=$x_start+50;
$y_centre=$y_start+50;

//$x_centre=$x_start+(rand(40,60));
//$y_center=$y_start+(rand(40,60));
$height=rand(500,9000);

    $terrain="plains";
            if ($height>1000)
                {
                $terrain="hills";
                }

            if ($height>5000)
                {
                $terrain="mountains";
                }

$query = "UPDATE map_terrain SET terrain='$terrain', height='$height' WHERE x='$x_centre' AND y='$y_centre'";
mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");

echo "<br>$query<br>\n";

$range=0;

// These arrays are used to work out the coordinates for the loop, each edge.
$x_array=array(1,0,-1,0);
$y_array=array(0,-1,0,1);
$range=0;

// Generate terrain until we get to sea level.  Loop out from the centre.

while(($height>-1) && ($test<15))
    {
    $test++;
    $range++;

    // Plot the top left location

    $x_coord=$x_centre-$range;
    $y_coord=$y_coord+1;
    $highest=$height-(rand(1,($height/10)));
    $lowest=$height-(rand(($height/10)+2,($height/5)));

    echo "High: $highest<br>\n";
    echo "Low: $lowest<br>\n";

    $height=rand($lowest,$highest);
    $terrain="plains";
    if ($height>1000)
        {
        $terrain="hills";
        }

    if ($height>5000)
        {
        $terrain="mountains";
        }

    $query = "UPDATE map_terrain SET terrain='$terrain', height='$height' WHERE x='$x_coord' AND y='$y_coord'";
    mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");
    echo "<br>$query<br>\n";

//    $x_coord++;

    // Plot each side in turn, North, East, South and West.
    for ($side=0;$side<4;$side++)
        {
        // Plot each location in this side.
        for ($location=1;$location<=($range*2);$location++)
            {
            $x_coord=$x_coord+$x_array[$side];
            $y_coord=$y_coord+$y_array[$side];
            $height=rand($lowest,$highest);
            $terrain="plains";
            if ($height>1000)
                {
                $terrain="hills";
                }

            if ($height>5000)
                {
                $terrain="mountains";
                }

            $query = "UPDATE map_terrain SET terrain='$terrain', height='$height' WHERE x='$x_coord' AND y='$y_coord'";
            mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");
            echo "<br>$query<br>\n";
            }
        }
    }

$now = time();

$elapsed = $now - $start_time;

echo "<br>First section took $elapsed seconds<br>\n";

// Find out how many plains there are to work out how many cities we can have.

$query="SELECT * FROM map_terrain WHERE map_no='$id' AND terrain='plains'";
$result = mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");

$plains=mysql_num_rows($result);

$cities=round($plains/100);
$max_players=round($cities/3)+1;
// We need to record this number in the master table
$query = "INSERT into map_master (max_players) VALUES ('$max_players')";
mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");

$forest=round($plains/(rand(10,50)));

echo "Cities: $cities and Forests: $forest<br>\n";

// Loop through cities and forests

//while (($city_gen<$cities) && ($forest_gen<$forest))
for ($city_gen=0;$city_gen<$cities;$city_gen++)
    {
    // Pick random location (plains) and see if it's suitable
    $query="SELECT * FROM map_terrain WHERE map_no='$id' AND terrain='plains'";
    $result = mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $query");

    while ( $dbrow = mysql_fetch_array($result) )
        {
        $for_chance=5;
        $cit_chance=5;
        $x=$dbrow['x'];
        $y=$dbrow['y'];
//        if ($forest_gen<$forest)
//            {
            // Find out what the surrounding terrain is like.
            for ($y_loop=$y+1;$y_loop>=($y-1);$y_loop--)
                {
            for ($x_loop=$x-1;$x_loop<=($x+1);$x_loop++)
                {
                $adj_query="SELECT * FROM map_terrain WHERE map_no='$id' AND x='$x_loop' AND y='$y_loop'";
                $adj_result = mysql_query($adj_query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $adj_query");
                $adj_terrain=mysql_result($adj_result,0,'terrain');
                if ($adj_terrain=='forest')
                    {
                    $for_chance=$for_chance+5;
                    }
                if ($adj_terrain=='sea')
                    {
                    $cit_chance=$cit_chance+5;
                    }
                if ($adj_terrain=='city')
                    {
                    // This will force so cities can never be adjacent
                    $cit_chance=-100;
                    }
                }
            }
        if ($forest_gen<$forest)
            {
            // Checked all surrounding terrain, see if we get a forest
            $forest_chance=rand(1,100);
            if ($forest_chance<$for_chance)
                {
                // Yes, we've got new forest here.
                $forest_gen++;
                $for_query="UPDATE map_terrain SET terrain='forest' WHERE map_no='$id' AND x='$x' AND y='$y'";
                echo "<br>$for_query<br>\n";
                mysql_query($for_query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $for_query");
                }
        //    } // I'm not sure we need this bracket
            }
        if($city_gen<$cities)
            {
            // Checked all surrounding terrain, see if we get a city
            $city_chance=rand(1,100);
            if ($city_chance<$cit_chance)
                {
                // Yes, we've got new city here.
                $city_gen++;
                $cit_query="UPDATE map_terrain SET terrain='city' WHERE map_no='$id' AND x='$x' AND y='$y'";
                echo "<br>$cit_query<br>\n";
                mysql_query($cit_query) or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $cit_query");
                }
        //    } // I'm not sure we need this bracket
            }

        }
    }
?>



If you pretty much know how the Travian map works, that would be very useful.

Ask any questions as most likely this is all very unclear, I'm tired and out of time.

Thanks!



Edit;

Ok I now fully understand how an image map works, how would it be possible to make one dynamic with different images (for the squares) and such?

This post has been edited by brandon99337: 23 Nov, 2008 - 09:11 PM

User is offlineProfile CardPM
+Quote Post


BetaWar

RE: Image Map For A Game

24 Nov, 2008 - 09:39 AM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
Moved to PHP
User is offlineProfile CardPM
+Quote Post

edwardkieran

RE: Image Map For A Game

19 Mar, 2009 - 06:01 AM
Post #3

New D.I.C Head
*

Joined: 19 Mar, 2009
Posts: 2

Hi,
Have you found a solution to how a map like the one in Travian can be accomplished?
User is offlineProfile CardPM
+Quote Post

brandon99337

RE: Image Map For A Game

19 Mar, 2009 - 06:23 AM
Post #4

D.I.C Head
**

Joined: 14 Feb, 2008
Posts: 167



Thanked: 3 times
My Contributions
Wow this was such a long time ago, I think I ended up using the GD library to create the maps needed, then I used the image map for the links.
User is offlineProfile CardPM
+Quote Post

edwardkieran

RE: Image Map For A Game

19 Mar, 2009 - 06:36 AM
Post #5

New D.I.C Head
*

Joined: 19 Mar, 2009
Posts: 2

Thank you for the answer, though your post was a bit older your answer was pretty quick and I appreciate it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 05:15AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month