kuskus Posted August 8, 2007 Share Posted August 8, 2007 Hi ppl, I need help to draw a random 2D map from hexagonal tiles. I have 5 different types of hex tiles and I want to draw a random map. Any help to randomly distribute the tiles and form an entire map of, for example, 256 hex tiles? Thank u in advance, Daniel Quote Link to comment https://forums.phpfreaks.com/topic/63971-help-to-draw-random-2d-map-from-hex-tiles/ Share on other sites More sharing options...
frost Posted August 9, 2007 Share Posted August 9, 2007 Are the tiles images? Is your info set up in a database? You could run a loop to select the image from the database at random and display it. If you wanted a random map you would also have to insert random row breaks etc. That might be enough to get you started, if you can give me some more info I would be happy to help out further. Quote Link to comment https://forums.phpfreaks.com/topic/63971-help-to-draw-random-2d-map-from-hex-tiles/#findComment-318955 Share on other sites More sharing options...
kuskus Posted August 9, 2007 Author Share Posted August 9, 2007 Hi and thank u for the help, Yes, the tiles are hexagonal images in jpg, gif, or any needed to work with, thats not the problem. The starting "raw" five types of tiles will be in a database. As we start in the development of this problem we found some others problems: - As we select the tiles randomly and as we display it, we have to know the position the random pattern has assigned to that tile (i.e. coordinates) in order to use the same random-generated map later on. - Also we have to name each tile by a logical pattern (an unique ID for each tile) for later changes of its content. - As well, we have to know the tiles that each tile has around it (hex tiles has another 6 tiles around it). - And once we have the system to know that info we have to save it to a map database. Anyway and in first place I have to manage to random select the tiles from the database and displaying a number of randomly selected tiles to form the map. Later think I can manage to solve almost all the other problems. Ty mate, Daniel Quote Link to comment https://forums.phpfreaks.com/topic/63971-help-to-draw-random-2d-map-from-hex-tiles/#findComment-319155 Share on other sites More sharing options...
gurroa Posted August 9, 2007 Share Posted August 9, 2007 function ReturnRandomFromArray(&$arr) { $randar = array(); reset($arr); while(list(,$hod)=each($arr)) $randar[] = $hod; $randar = array_values($randar); $c = count($randar); if ($c > 1) { --$c; $r = rand(0,$c); while(empty($randar[$r])) $r = rand(0,$c); return $randar[$r]; } elseif ($c == 1) return $randar[0]; else return ''; } // you will probably fill this array from your database $ardat = array( 'tile unique id', 'tile unique id'....); $armap = array(); for ($x = 0; $x < 16; ++$x) { $xx = $x * 16; for ($y = 0; $y < 16; ++$y) $armap[$xx + $y] = ReturnRandomFromArray($ardat); } Quote Link to comment https://forums.phpfreaks.com/topic/63971-help-to-draw-random-2d-map-from-hex-tiles/#findComment-319221 Share on other sites More sharing options...
frost Posted August 9, 2007 Share Posted August 9, 2007 For the unique id you could just start a count. $count =0; loop { $count++; } This would add one to the count variable and you could use that as a unique id. I am not sure I follow your coordinates request. Do you have an example of what you are trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/63971-help-to-draw-random-2d-map-from-hex-tiles/#findComment-319367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.