Jump to content

Recommended Posts

I have created a 5000x by 5000y map with 2,500,000 rows in my DB for my game where players can explore and such.

What I want to do is display an image where a player is currently standing.

 

For example:

 

You are at 10X 10Y on the map, a player is at 15X 15Y

What I want to do is display an icon at 15x 15y showing they are standing there.

 

It's been done before, and when I try it, it echoes out the entire 2,500,000 squares and lags me for like 30 minutes where I can do nothing. I have no idea why because the code to do so has nothing to do with the while loop.

 

Table: 'travel_map'

fields: 'x', 'y'

 

Table: 'players'

fields: 'x', 'y'

 

All help is much appreciated, thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/184798-displaying-players-on-a-map/
Share on other sites

Say what? you say the code you have has nothing to-do with a while loop yet you say it is echoing out all squares. Perhaps show some code...

 

Sure,

 

$x = $player->x + 6;
$y = $player->y + 6;
$x2 = $player->x - 6;
$y2 = $player->y - 6;

$query = $db->execute("select * from travel where x<$x and y<$y and x>$x2 and y>$y2");
echo "<table border=0><tr><td width=10%><table border=0>";
for($i = 1;$map = $query->fetchrow(); $i++)
{
echo "<td><center>";
if ($player->x == $map[x] && $player->y == $map[y])
{
echo "<img src=/images/player.gif>";
}else{
echo "<a href=explore.php?x=$map[x]&y=$map[y]><img src=$map[image] title='$map[x]X, $map[y]Y'></a>";
}
if ($i % 11 == 0)
{
echo "<tr>";
}


}

 

 

As you can see, it will replace the square with a player.gif icon IF you are on those coords.

I'd like to do the same for other players, but it's a struggle.

So just keep adding in a bit of code to the loop until it does something out of the ordinary, I can't hold your hand all the way through. If it's printing out 1 - 121 then you should never have the loop run more than 121 times, which means it should only ever echo out 121 squares. At no point inside the loop do you appear to change the value of $map, which is the only thing I can think of that could make it run any more times.

So just keep adding in a bit of code to the loop until it does something out of the ordinary, I can't hold your hand all the way through. If it's printing out 1 - 121 then you should never have the loop run more than 121 times, which means it should only ever echo out 121 squares. At no point inside the loop do you appear to change the value of $map, which is the only thing I can think of that could make it run any more times.

 

It just occurred to me that I was using a while loop inside a for loop.

I selected * from players, did a while loop on it and i think that may be why.

Is there a better way to show what players are standing where?

And thank you for the help.

Frankly I don't know. You say one thing, your code contradicts it. I asked to see the relative code, you provide code, I commented on it. You then say you have a while loop inside a for loop. Of course that's going to cause problems. I'm not psychic though and the code you provided hasn't got a while loop insight. If you want help with something you will need to explain in a clear concise manner what you are doing.

Frankly I don't know. You say one thing, your code contradicts it. I asked to see the relative code, you provide code, I commented on it. You then say you have a while loop inside a for loop. Of course that's going to cause problems. I'm not psychic though and the code you provided hasn't got a while loop insight. If you want help with something you will need to explain in a clear concise manner what you are doing.

 

Basically I am making a map where players either click north, south, west, or east.

What I am wanting to do is display an icon(player.gif) where a player is standing on the map.

There is a table named travel which has all the x and y coordinates, then there is a table named players which has all the players info, but all that is needed is the x and y fields.

Let's say you are standing on the coords 10x and 10y, it will show an icon with your name on top of it, to show where you are standing.

What I want to do is also show where other players are standing, so let's say coords 15x and 15y are a grass area, but then a player goes to those coords and you refresh the page, you would then see that that player is standing on those coords.

 

I can't really explain much because I've only finished like 5% of the code, and still need to add loads of other things.

But basically all there is right now is 121 squares beside each other with grass, other than your 1 square which shows where you are.

 

Maybe you also want to consider a more performant solution then storing 2.5M rows in your db.. For example consider the map to be virtual virtual (if that exists) This way you don't store 2.5M rows and only PHP knows the boundry of your map 5k² then when a player travels store his coördinates along with his character data and load the corresponding image (auto-generated or picked from an images directory)

 

This will give you an enormous increase in performance.

Note: Uses my previous post as a starting point

 

Basically I am making a map where players either click north, south, west, or east.

What I am wanting to do is display an icon(player.gif) where a player is standing on the map.

 

You need a little Math (cartesian|polar coördinate system) to accomplish this you use the coördinates of the player and calculate the name of the tile the player is standing on for example 125-125-250-250.png (all tiles are 125x125) and the player's coördinates are for example 137,195

 

Code example to clearify the map name:

list($x1, $y1, $x2, $y2) = explode('-', pathinfo('125-125-250-250.png' PATHINFO_FILENAME));

 

What I want to do is also show where other players are standing, so let's say coords 15x and 15y are a grass area, but then a player goes to those coords and you refresh the page, you would then see that that player is standing on those coords.

 

Then this becomes easy:

SELECT * FROM players WHERE x BETWEEN $xmin AND $xmax AND y BETWEEN $ymin AND $ymax AND NOT id = $playerid

Note: Uses my previous post as a starting point

 

Basically I am making a map where players either click north, south, west, or east.

What I am wanting to do is display an icon(player.gif) where a player is standing on the map.

 

You need a little Math (cartesian|polar coördinate system) to accomplish this you use the coördinates of the player and calculate the name of the tile the player is standing on for example 125-125-250-250.png (all tiles are 125x125) and the player's coördinates are for example 137,195

 

Code example to clearify the map name:

list($x1, $y1, $x2, $y2) = explode('-', pathinfo('125-125-250-250.png' PATHINFO_FILENAME));

 

What I want to do is also show where other players are standing, so let's say coords 15x and 15y are a grass area, but then a player goes to those coords and you refresh the page, you would then see that that player is standing on those coords.

 

Then this becomes easy:

SELECT * FROM players WHERE x BETWEEN $xmin AND $xmax AND y BETWEEN $ymin AND $ymax AND NOT id = $playerid

 

Im sorry but I have no idea what to do with this, im not very advanced with PHP and still have a ways to go before my learning is done. Thanks for the help :)

No worries just make sure you keep active on the forum. Help other people! It will increase your learning speed (something I didn't knew when I first started) This tip is based on: "Learn from mistakes of others as you may not make them all yourself". This wil help and guide you in your future career as in "been there, done that".

 

Equally true: by remaining active on the forum you will learn to spot errors more quickly in your own code because you have the advantage of looking at million lines of bugged code every day ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.