{"id":767,"date":"2014-06-20T15:35:01","date_gmt":"2014-06-20T07:35:01","guid":{"rendered":"http:\/\/www.gridsagegames.com\/blog\/?p=767"},"modified":"2019-03-28T09:54:06","modified_gmt":"2019-03-28T01:54:06","slug":"mapgen-tunneling-algorithm","status":"publish","type":"post","link":"https:\/\/www.gridsagegames.com\/blog\/2014\/06\/mapgen-tunneling-algorithm\/","title":{"rendered":"Mapgen: Tunneling Algorithm"},"content":{"rendered":"<p>Cogmind&#8217;s main dungeon maps are excavated by &#8220;tunnelers&#8221; that dig corridors and rooms, much in the way a dungeon architect would build a home for their master&#8217;s minions. An empty map is seeded by one or more tunnelers, and they travel around that map opening up all the areas that will become occupiable space, e.g. corridors, doors, rooms, halls.<\/p>\n<p>I like tunneling algorithms because with the right parameters they can create fairly realistic environment.<\/p>\n<div id=\"attachment_768\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_map_main.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-768\" class=\"size-full wp-image-768 \" title=\"Cogmind Map Generation: Main Dungeon\" alt=\"cogmind_map_main\" src=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_map_main.png\" width=\"400\" height=\"800\" \/><\/a><p id=\"caption-attachment-768\" class=\"wp-caption-text\">Behold an underground complex ruled by robots!<\/p><\/div>\n<h2>Inspiration<\/h1>\n<p>First I must give credit where it is due. Tunneling algorithms didn&#8217;t interest me before because when used in expansive dungeons they tend to produce boring repetitive layouts. Then a few years ago I came across <a title=\"DungeonMaker\" href=\"http:\/\/dungeonmaker.sourceforge.net\/DM2_Manual\/index.html\" target=\"_blank\">this project<\/a>, which introduced me to the key concept that tunnelers can <em>change<\/em> their own parameters as they move (there&#8217;s a good overview of the idea on that site). The result is a more varied dungeon environment, especially across larger maps like those Cogmind uses.<\/p>\n<p>That particular algorithm has some oddities--what&#8217;s with all the useless and redundant corridors? Why doesn&#8217;t it ever produce even-width corridors?* I suppose many of its drawbacks never came to the forefront because the creator never applied it to an actual game:<\/p>\n<div id=\"attachment_752\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/mapgen_tunneler.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-752\" class=\"wp-image-752 \" title=\"DungeonMaker\" alt=\"mapgen_tunneler\" src=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/mapgen_tunneler.png\" width=\"400\" height=\"400\" \/><\/a><p id=\"caption-attachment-752\" class=\"wp-caption-text\">Looks cool, but not very realistic.<\/p><\/div>\n<p>*I suspect this has to do with symmetrical tunnelers being easier to work with in code. While I&#8217;ve written my own algorithm to handle arbitrary widths, because tunnels logically tend to shrink and grow two cells at a time, even-width corridors eventually become odd-width corridors anyway after shrinking to 1-cell wide then growing again!<\/p>\n<div id=\"attachment_769\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_map_tunnelers_even.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-769\" class=\"size-full wp-image-769 \" title=\"Cogmind Map Generation: Even vs. Odd Width Corridors\" alt=\"cogmind_map_tunnelers_even\" src=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_map_tunnelers_even.png\" width=\"400\" height=\"400\" \/><\/a><p id=\"caption-attachment-769\" class=\"wp-caption-text\">A dungeon excavated by a single even-width tunneler (begins top center) eventually creates more odd-width corridors (blue) after its initial run of even-width corridors (orange).<\/p><\/div>\n<p>I may not bother using even-width corridors since they are more likely to produce off-center connections that don&#8217;t look quite as nice. I do like 2-width corridors from a gameplay point of view since they are narrow yet still leave enough room to maneuver around other units, but 3-width corridors are probably better as a standard for a game like Cogmind in which battles are most commonly fought from a distance--there needs to be enough room to set up a decent firing line.<\/p>\n<p>In any case, over the years I&#8217;ve been working on my own algorithm based on the same principle: that tunnelers creating the dungeon can evolve over time to &#8220;mix things up.&#8221;<\/p>\n<h2>Parameters<\/h1>\n<p>There is a wide variety of parameters to control for tunneler behavior: Width, direction, speed, chance to turn, chance to create rooms, size and shape of rooms to create, how much space to leave between self and other dungeon objects, when to quit&#8230;<\/p>\n<div id=\"attachment_770\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_map_tunnelers_padded.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-770\" class=\"size-full wp-image-770 \" title=\"Cogmind Map Generation: Padding Example\" alt=\"cogmind_map_tunnelers_padded\" src=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_map_tunnelers_padded.png\" width=\"400\" height=\"400\" \/><\/a><p id=\"caption-attachment-770\" class=\"wp-caption-text\">Example: Increased padding between both corridors and rooms might be desirable if you don&#8217;t want it to be too easy for the player to blow their way into another room\/pathway. In my case I keep padding low because what good is destructible terrain if you can&#8217;t use it to your advantage to make situations more dynamic? (But look out, because a very determined enemy may come blasting through a wall, too!)<\/p><\/div>\n<p>Anyone familiar with procedural map generation will know that the same algorithm can produce significantly different results by tweaking its parameters. Later I&#8217;ll be showing more features used in conjunction with this algorithm to make it even more dynamic, but this is essentially all we need for the core game.<\/p>\n<h2>Size<\/h1>\n<p>Many maps in Cogmind are going to be even larger than in the 7DRL, and include more open spaces. The main dungeon used to be 100&#215;100 per map, which will be increasing to up to 200&#215;200 for the largest maps. That&#8217;s <em>four<\/em> times as large:<\/p>\n<div id=\"attachment_771\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmand_map_tunnelers_huge.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-771\" class=\"size-full wp-image-771 \" title=\"Cogmind Map Generation: Main Dungeon (Huge)\" alt=\"cogmand_map_tunnelers_huge\" src=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmand_map_tunnelers_huge.png\" width=\"800\" height=\"800\" \/><\/a><p id=\"caption-attachment-771\" class=\"wp-caption-text\">The largest likely map. Layout\/style is still very WIP, but the algorithm is 90% complete--still some cleanup to do to reduce overall jaggedness. (click for full size)<\/p><\/div>\n<p>Why larger maps? Well, first of all Cogmind always needed the space since combat is mostly ranged--you are generally fighting enemies an average of 15 or more cells away. Maps are growing even larger mostly to accommodate changes in game content.<\/p>\n<p>The world will be larger with more places to visit, so we need to spread out the main maps such that area transitions aren&#8217;t too frequent. Battles might be bigger depending on your play style; who knows, raising a small robot army is likely to start a little war? In short there&#8217;s more you can do now, and we need more space to do it in.<\/p>\n<p>There&#8217;s also a greater emphasis on intelligence gathering. Aside from sensor-type parts seen in the 7DRL, you&#8217;ll be able to learn about the map via terminals. Larger maps will encourage players to take advantage of these sources of information when possible. This should make the stealth route more feasible since you have more means of learning about the layout and enemy movements, but the map isn&#8217;t small enough that this knowledge will make success too easy.<\/p>\n<p>On a more fundamental level, many areas of the map that would have been empty in the 7DRL will now be occupied by machines, so less of the map space is actually as &#8220;occupiable&#8221; as it appears. The current stage of map generation is only interested in connectivity, flow, and open vs. closed space; we&#8217;ll get around to placing objects later.<\/p>\n<h2>Composition<\/h1>\n<p>As stated earlier (and you can see above), the new maps have a fair number of wide corridors and open areas.<\/p>\n<p>The 7DRL contained a much higher ratio of corridors and places to hide, making it seem easier to hide than it actually was--the enemy could track you down without much difficulty, or even head you off at a shortcut because after all they know the dungeon better than you do!<\/p>\n<p>Opening up the map forces you to think a bit more before attracting attention, since in doing so you may end up committed to a confrontation until it&#8217;s resolved.<\/p>\n<h2>Layout<\/h1>\n<p>During the generation process tunnelers are responsible for laying the paths that take the player from an entrance to an exit, of which there are usually several with a guaranteed amount of space between them. How tunnelers move and what they decide to do pretty much determines what most of the map will look like.<\/p>\n<p>When possible most tunnelers try to dig out rooms along their edges. These serve as places to hide\/wait or find parts. They may also contain machines, which are sometimes just for atmosphere, <a title=\"Machines\" href=\"http:\/\/www.gridsagegames.com\/blog\/2013\/10\/machines\/\" target=\"_blank\">or not<\/a>. Rooms with multiple doors are usually an intersection of sorts, giving access to areas other than their primary corridor.<\/p>\n<p>Tunnelers also sometimes build junctions when turning or changing width. These aren&#8217;t purely aesthetic enhancements--junctions can contain terminals from which operator class bots supervise local activity.<\/p>\n<div id=\"attachment_776\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_map_tunnelers_junctions.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-776\" class=\"size-full wp-image-776 \" title=\"Cogmind Map Generation: Junctions\" alt=\"cogmind_map_tunnelers_junctions\" src=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_map_tunnelers_junctions.png\" width=\"500\" height=\"700\" \/><\/a><p id=\"caption-attachment-776\" class=\"wp-caption-text\">Those junctions located along main corridors will almost certainly have terminal access.<\/p><\/div>\n<div id=\"attachment_777\" style=\"width: 505px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_mockup_machines_terminal.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-777\" class=\"size-full wp-image-777 \" title=\"Cogmind Map Generation: Terminal Junctions\" alt=\"cogmind_mockup_machines_terminal\" src=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmind_mockup_machines_terminal.png\" width=\"495\" height=\"409\" \/><\/a><p id=\"caption-attachment-777\" class=\"wp-caption-text\">The idea of having some terminals located at corridor junctions was seen in the very first mockup, as above.<\/p><\/div>\n<p>Tunnelers also spill into and contribute to larger areas called &#8220;halls.&#8221; If you&#8217;re trying to stay undetected you&#8217;ll want to think twice about traveling through halls, since a passing patrol or scout is more likely to spot you.<\/p>\n<div id=\"attachment_778\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmand_map_tunnelers_halls.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-778\" class=\"wp-image-778 \" title=\"Cogmind Map Generation: Halls\" alt=\"cogmand_map_tunnelers_halls\" src=\"https:\/\/www.gridsagegames.com\/blog\/gsg-content\/uploads\/2014\/06\/cogmand_map_tunnelers_halls.png\" width=\"500\" height=\"700\" \/><\/a><p id=\"caption-attachment-778\" class=\"wp-caption-text\">Halls will usually contain larger machines to further subdivide them and provide obstacles behind which to hide.<\/p><\/div>\n<p>We&#8217;ll be taking another look at tunneler-generated maps in later posts on other map-related topics.<\/p>\n<p><em>This is the second in a five-part series on procedural maps:<\/em><\/p>\n<ul>\n<li><em>Part 1: <a title=\"Procedural Map Generation\" href=\"http:\/\/www.gridsagegames.com\/blog\/2014\/06\/procedural-map-generation\/\">Procedural Map Generation<\/a><\/em><\/li>\n<li><em>Part 3: <a title=\"Mapgen: Cellular Automata\" href=\"http:\/\/www.gridsagegames.com\/blog\/2014\/06\/mapgen-cellular-automata\/\">Cellular Automata<\/a><\/em><\/li>\n<li><em>Part 4: <a title=\"Dungeon Metrics\" href=\"http:\/\/www.gridsagegames.com\/blog\/2014\/07\/dungeon-metrics\/\">Dungeon Metrics<\/a><\/em><\/li>\n<li><em>Part 5: <a title=\"Dungeon Prefabs\" href=\"http:\/\/www.gridsagegames.com\/blog\/2014\/07\/dungeon-prefabs\/\">Dungeon Prefabs<\/a><em><br \/>\n<\/em><\/em><\/li>\n<\/ul>\n<p><em>Update 190306: There&#8217;s also an additional article now which covers <a title=\"Roguelike Level Design Addendum: Procedural Layouts\" href=\"https:\/\/www.gridsagegames.com\/blog\/2019\/03\/roguelike-level-design-addendum-procedural-layouts\/\">procedural layouts as they related to level design<\/a> in Cogmind.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cogmind&#8217;s main dungeon maps are excavated by &#8220;tunnelers&#8221; that dig corridors and rooms, much in the way a dungeon architect would build a home for their master&#8217;s minions. An empty map is seeded by one or more tunnelers, and they travel around that map opening up all the areas that will become occupiable space, e.g. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":770,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,92],"tags":[78,4,76,79],"class_list":["post-767","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-design","category-dev-series-procedural-maps","tag-algorithm","tag-cogmind","tag-map-generation","tag-tunneling-algorithm"],"_links":{"self":[{"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/posts\/767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/comments?post=767"}],"version-history":[{"count":13,"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/posts\/767\/revisions"}],"predecessor-version":[{"id":3432,"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/posts\/767\/revisions\/3432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/media\/770"}],"wp:attachment":[{"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/media?parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/categories?post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gridsagegames.com\/blog\/wp-json\/wp\/v2\/tags?post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}