Tilemap Storage and Retrival

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Tilemap Storage and Retrival

Post by Teonnyn »

A day ago or so I asked for help on displaying tilemaps and character movements. Now, I've gotten all of that working the way it should - but it only works with arrays.
In order to build multiple maps, I need to set up an array storage system that can call and retrieve them. I've tried serialize and that failed to work entirely.

The maps also have attributes, which can be stored separately if need be. Ideally, I want to use my SQL database to set up and pull the maps, but which method would be best? This is a sample array:

Code: Select all

$map = array(5,0,0,0,11,0,0,0,0,5,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,3,3,3,0,0,0,0,0,0,0,3,3,3,1,1,1,0,0,0,0,3,3,3,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,5,0,0,0,0,1,0,0,5,0)

In display format, they are tiled something like this:

0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000 - Although the final result actually produces a square.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Tilemap Storage and Retrival

Post by jraede »

If you use HTML and set each tile class to float:left, you can just run through the array using foreach and display each tile within that class. If you have a container div with a set width, the tiles will automatically flow to the next line.

For example:

Code: Select all

#map-container {
     width:500px;
     height:500px;
}
.tile {
     width:50px;
     height:50px;
     float:left;
     display:block;
}

.tile-1 {
     background-image:url(/images/tile1.jpg);
}

.tile-2 {
     background-image:url(/images/tile2.jpg);
}

etc...

Code: Select all

<div id="map-container">
<?php foreach($array as $tile) : ?>
     <div class="tile tile-<?=$tile?>"></div>
<?php endforeach;?>
</div>
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Tilemap Storage and Retrival

Post by Teonnyn »

I'm not asking about display - display works fine. I'm asking what the best method to store these tilemaps is in either mySQL or a flat-file database.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Tilemap Storage and Retrival

Post by John Cartwright »

I would most certainly use a mysql database over a flatfile any day (besides perhaps static content). Considering you probably want attributes at the tile level, such as terrain type, whether the area is passable, or perhaps other features, it is best you normalize your structure into a maps table (you probably want multiple maps), a tiles table (each tile for a particular map), and a terrain type table (meta data based on the particular tile). Perhaps a schema (sample data included) along the lines of

Code: Select all

--
-- Table structure for table `gamemap_maps`
--

CREATE TABLE IF NOT EXISTS `gamemap_maps` (
  `id` int(10) NOT NULL auto_increment,
  `name` varchar(255) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=2 ;

--
-- Dumping data for table `gamemap_maps`
--

INSERT INTO `gamemap_maps` (`id`, `name`) VALUES
(1, 'My First Map');

-- --------------------------------------------------------

--
-- Table structure for table `gamemap_terrain`
--

CREATE TABLE IF NOT EXISTS `gamemap_terrain` (
  `id` int(10) NOT NULL auto_increment,
  `type` enum('grass','water','mountain','plain') collate latin1_general_ci NOT NULL default 'grass',
  `passable` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `type` (`type`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ;

--
-- Dumping data for table `gamemap_terrain`
--

INSERT INTO `gamemap_terrain` (`id`, `type`, `passable`) VALUES
(1, 'grass', 1),
(2, 'water', 0),
(3, 'mountain', 0),
(4, 'plain', 1);

-- --------------------------------------------------------

--
-- Table structure for table `gamemap_tiles`
--

CREATE TABLE IF NOT EXISTS `gamemap_tiles` (
  `id` int(10) NOT NULL auto_increment,
  `map_id` int(10) NOT NULL,
  `terrain_id` int(5) NOT NULL,
  `xpos` int(5) NOT NULL,
  `ypos` int(5) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `map_id` (`map_id`,`terrain_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=17 ;

--
-- Dumping data for table `gamemap_tiles`
--

INSERT INTO `gamemap_tiles` (`id`, `map_id`, `terrain_id`, `xpos`, `ypos`) VALUES
(1, 1, 1, 1, 1),
(2, 1, 1, 2, 1),
(3, 1, 1, 3, 1),
(4, 1, 1, 4, 1),
(5, 1, 1, 1, 2),
(6, 1, 1, 2, 2),
(7, 1, 1, 3, 2),
(8, 1, 1, 4, 2),
(9, 1, 1, 1, 3),
(10, 1, 1, 2, 3),
(11, 1, 1, 3, 3),
(12, 1, 1, 4, 3),
(13, 1, 1, 1, 4),
(14, 1, 1, 2, 4),
(15, 1, 2, 3, 4),
(16, 1, 3, 4, 4);
Now that we have a normalized map database with coordinates, we can easily construct the map with indivudal tiles metadata. I.e.,

Code: Select all

SELECT * 
FROM gamemap_maps AS map
INNER JOIN gamemap_tiles AS tile ON map.id = tile.map_id 
INNER JOIN gamemap_terrain AS terrain ON tile.terrain_id = terrain.id 
WHERE map.id = 1
ORDER BY map.xpos ASC, map.ypos ASC
Which would generate a map along the lines of
[text]_____________________________________________
| x = 1 | x = 2 | x = 2 | x = 4 |
| y = 1 | y = 1 | y = 1 | y = 1 |
| grass | grass | grass | grass |
| passable | passable | passable | passable |
|__________|__________|__________|__________|
| x = 1 | x = 2 | x = 3 | x = 4 |
| y = 2 | y = 2 | y = 2 | y = 2 |
| grass | grass | grass | grass |
| passable | passable | passable | passable |
|__________|__________|__________|__________|
| x = 1 | x = 2 | x = 3 | x = 4 |
| y = 3 | y = 3 | y = 3 | y = 3 |
| grass | grass | grass | grass |
| passable | passable | passable | passable |
|__________|__________|__________|__________|
| x = 1 | x = 2 | x = 3 | x = 4 |
| y = 4 | y = 4 | y = 4 | y = 4 |
| grass | grass | water | mountain |
| passable | passable | non- | non- |
|__________|__________|_passable_|_passable_|[/text]

Let me know if you need some more explanation.
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Tilemap Storage and Retrival

Post by Teonnyn »

Thank you much, that helps some.
Post Reply