Maps in PHP and MySQL

Miscellaneous Forums/General Discussion/Maps in PHP and MySQL

Anyone know the best way for storing a map of tiles, lets say a max of 100 by 100 tiles with 2 layers, where all the info for the tiles is in strings in a database?

as it is that would be 10,000 characters if each string were only 1 letter long. I'm afraid that this will make for too much wasted space, and also very rough to load the map.

The undermap which is just tiles could store them by their ID, so that map would be smaller, the upper map could store with ID too, but also needs to denote from what database to load from, NPC's, Doors, Chests, and other such things

something as simple as 1-101 would be fine, however, that is still 5 characters long and could be longer (or shorter) if the numbers get larger... if I end up with over 999 maps or NPCs or Items for example. Especially when you consider that all things that generate text are held as NPCs (pictures, points of interest)

If anyone has any bright ideas, that would be great. I'm not opposed to things that are creative or require more code to decifer.

I was thinking about holding them in a text field, but 10,000 items would mean I couldn't ever stray from 1-100@1-100@1-100 very much into 1-1000 or it will over load the 65,000 char limit pretty fast. being that 1-100@ = 6 characters which makes for 60,000 total and 1-1000@ = 7 and makes for 70,000 I could get away with it only by luck, if I had a more than 1000'th item heavy map, it would crash bad... I don't want to play with luck. and I'd like to not have to lower the map's size... although if you think dropping to 75 by 75 is the only solution, I will have to.

Why don't you use an image to store the map information? and use the Red, Green and Blue channels to store Map or Layer Information?

<?php
$im = ImageCreateFromPng("rockym.png");
$rgb = ImageColorAt($im, 100, 100); // Pixel co-ords
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
?> 


how would I store the data tho? I 've never done that, and aren't sure whats going on in your code...

I mean.. I gather that $r $g and $b will be the rgb values, but what would I do with them... and how would I edit the map?

You could prefabricate the maps and have them stored as images on the server. Or you could create and write to them through php using the imagesetpixel command.

What I am suggesting is that you use the R G B values to store your map data, each of these values has a range of 0-255 and that is enough room to store your map data. So in effect you could have a 3 layer map represented by a single 100x100 pixel image.

no... cause I have more than 255 values to store... so I could use red for the tiles and waste space, use blue to pick the top layer's database and green to select from up to 255 choices... any easy way to double up on a color and use 2 images?

just use the red for the tiles on one pic and use red for the database and blue and green together (adding?) for the ... ok.. that would work

How would I do setpixel... what is the syntax?


something as simple as 1-101 would be fine

Maybe I misunderstood what you wanted, an image of 100x100 pixels gives you 10,000 tiles, each pixel can have a value between 0 and 16,581,374 - and that's not enough?

I suggest you read up on PHP and what you can and can not do, and in particular the two commands I mentioned above.

Well you would set the color, then do a plot. Then readback all the pixels on loading of the level.

However I would assume that doing it in a binary text file would be alot faster (both in downloading and processing)?

[edit] ignore this if its a php only answer you want[/edit]

@boomboom, this is all serverside so there is no download time and processing will be quicker than pulling from mySQL. Besides what is an image if it's not a binary file?

Indiepath... I don't wanna read up on nuffin.. I just want the syntax for imagesetpixel.

Then use google


What I am suggesting is that you use the R G B values to store your map data, each of these values has a range of 0-255 and that is enough room to store your map data. So in effect you could have a 3 layer map represented by a single 100x100 pixel image.



255 isn't enough to save my map data.

1-101 is not a range. it is a string. database 1 item 101 @ separates each tile in the map every 100 @s there is a new line.


I can store my data in a color with a number from 0 - 16mil, I just wanted to know what exactly $rgb would = in your code snip.

If it is an integer from 0-16mil I can use it. If not, I can't.


That is what I am saying. a 3 layer map, as you said, would have only 255 possible values for each tile, which is not enough.

If I could use the $rgb var as a number, I could do that... otherwise I would need to recombine $r $g and $b into $rgb and make the pixel = that. in other words.. $r=1 $g=245 and $b=255 would be database 1, item 500. but so could $rgb = 10500. I'd rather do $rgb = 10500 than the other, since I understand it better and it can store more. However, I do not know if that would be valid.

because i do not know what ImageColorAt(); actually returns.


I don't wanna read up on nuffin.. I just want the syntax for imagesetpixel.



So basically you want other people to do all the hard work for you so you can cut and paste code and claim it as your own?. If you were given the syntax for this command, no doubt you'd just ask for an example of how to use it.

If you're not willing to read and learn, how do you expect to be capable of things you can not do at this present time?

Anyway, here's a link to the imagesetpixel

http://uk.php.net/imagesetpixel

No, Qube, I was confused because I thought indie understood what I wanted but he did not, and I thought he was giving me lip.

as in. He was fed up with me and didn't want to give me any help.

However.. in reality, he was as confused by my responses to him as I was with his suggestions..




also... you have no idea how much work I am doing... I have more than 6 pages of just planning for the databases behind the maps. I was hoping people had better ideas for how to handle the maps, which indie has. I just need to know (and can't find) what that 1 single piece of code returns. I just need to know what $rgb will equal. Because I don't understand how the code is going about breaking it into r g and b.

And I don't care to know how it is doing it cause it won't effect anything if I can just use $rgb.

EDIT

I'm just going to assume it returns a plain old everyday int and if it doesn't like it when I set color to 10500 I'll deal with it then.

I'm tired of getting insulted when I ask possibly dumb questions. I only ask when I can't find it on my own.

I was not insulting you, I was just commenting on your "I don't wanna read up on nuffin" statement. You'd find people more willing to help if you didn't make those kind of statements :)

Battle Empire map section has a preview possibility that creates an image of maps that have been created with the Map Editor written in BlitzPlus.

Not Battle Empire compatible functions(just to show how it can be done in PHP):
$TileSize = 32;

$file = fopen('map.map', 'rb');
if($file){
	//Header...
	$Width = FReadShort($file);
	$Height = FReadShort($file);
	$MapName = FReadString($file);
	$MapAuthor = FReadString($file);
	// ...and so on

	// Create map image
	$thumb = imagecreatetruecolor($Width * $TileSize, $Height * $TileSize);

	// Load tile table
	$source = imagecreatefromjpeg('tiles.jpg');

	// Draw tiles to map image
	for($y = 0; $y < $Height; $y++){
		for($x = 0; $x < $Width; $x++){
			imagecopyresized($thumb, $source, $x * $TileSize, $y * $TileSize, FReadByte($file) * $TileSize, 0, $TileSize, $TileSize, $TileSize, $TileSize);
		}
	}
	
	// Output (will send the map image)
	imagejpeg($thumb);
}

function FReadString($file){
	$a = fread($file, 4);
	$len = ord(substr($a, 0, 1)) + ord(substr($a, 1, 1)) * 255 + ord(substr($a, 2, 1)) * 255 * 255 + ord(substr($a, 3, 1)) * 255 * 255 * 255;
	if($len > 0){$txt = fread($file, $len);}else{$txt = '';}
	return $txt;
}

function FReadShort($file){
	$a = fread($file, 2);
	return ord(substr($a, 0, 1)) + ord(substr($a, 1, 1)) * 255;
}

function FReadByte($file){
	$a = fread($file, 1);
	return ord($a);
}