{"id":96,"date":"2013-03-10T21:45:42","date_gmt":"2013-03-11T05:45:42","guid":{"rendered":"http:\/\/adamantyr.com\/blog\/?p=96"},"modified":"2017-09-20T08:27:14","modified_gmt":"2017-09-20T16:27:14","slug":"battlemap-rebuild","status":"publish","type":"post","link":"http:\/\/www.adamantyr.com\/index.php\/2013\/03\/10\/battlemap-rebuild\/","title":{"rendered":"Battlemap Rebuild"},"content":{"rendered":"<p>As I continue working on the combat engine, finishing up other portions of &#8220;temporary&#8221; code has come up. One particular one is the use of battlemaps.<\/p>\n<p>Most of the 2D CRPG tactical combat games feature some kind of varied terrain to conduct battles on. Ultima&#8217;s III-V all had them, one for each different terrain type. So obviously I intended to follow suit here.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-594\" src=\"http:\/\/test.adamantyr.com\/wp-content\/uploads\/2017\/09\/bm1.png\" alt=\"\" width=\"272\" height=\"208\" \/>But, since I have the disk space for it, I intended to go much further than any of those games. Ultima V only features 16 unique battle maps. My CRPG? Oh, probably around 300+.<\/p>\n<p>I started battlemap design several years ago, when I was still working in the MESS emulator. I actually had to port my work off an old work disk to continue it, and update the tiles, which had changed a few times since I made the original file. Now I&#8217;ve gone one step further and made a separate battlemap set for each tile set. That way I can have as many as I need to make combats always potentially interesting.<\/p>\n<p>One complication I had to correct was the loading of different tile sets. Originally I had only a single set for combat. Now I&#8217;ve expanded it so that there&#8217;s a tied-in tile set for each of the major travel types. That lets me have maximum character space to do interesting features. If you&#8217;re in a town battle, lamp posts, crates, barrels, may show up on the map. A dungeon has manacles, skeletons, and other macabre items. All fun!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-full wp-image-595\" src=\"http:\/\/test.adamantyr.com\/wp-content\/uploads\/2017\/09\/bm2.png\" alt=\"\" width=\"272\" height=\"208\" \/>An added bonus with my approach is that battlemaps will be alterable by player action. Spells exist that let you create barriers, area effects of damage spells can destroy the landscape, and so forth. I don&#8217;t think any classic CRPG offered this kind of feature, keeping most of the terrain fairly static.<\/p>\n<p>Battlemaps are stored in two 128 byte records. The first 8 bytes are a compressed mini-map that indicates if the spaces are empty or occupied. The rest of the space stores the maps in RLE compression, using values lower than 128 to indicate a count of the next tile. My battlemap editor warns me if I exceed the record size. This usually happens if I get overzealous with spreading unique tiles around. (I don&#8217;t use RLE for the travel maps, because the high-byte is used to indicate lit\/unlit squares on dark maps. No real need to compress those files anyway.)<\/p>\n<p>One particular problem to solve is the placement of units on the battle map. I considered at first an array of common positions to just iterate through, but it was quickly adding up to a lot of bytes of data to store. So then I realized I could do this algorithmically with tools the code already has: the trigonometric functions.<\/p>\n<p>Using that code, all I have to supply for the map is two things: a set of coordinates and a radius. Because I want units to spread out a bit, I actually supply two sets. The number of units is divided in half, and then each half is placed in a circle around that coordinate, with the angle being based on the number of units. (4 = 90 degrees, 3=120 degrees, and so forth.) For an ambush scenario, a single set of coordinates is used for both sides, with different radius&#8217;s, so one is surrounding the other.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-596\" src=\"http:\/\/test.adamantyr.com\/wp-content\/uploads\/2017\/09\/bm3.png\" alt=\"\" width=\"272\" height=\"208\" \/>What to do if the chosen space is occupied? Well, first it will check all adjacent squares to that square for any open spots. If it finds one it takes it. If all four cardinal directions have nothing, then I just default to selecting completely random coordinates until an empty space is found. This means there&#8217;s a potential for a stray unit to end up in some bizarre place, but hey, that&#8217;s the fog of war&#8230;<\/p>\n<p>I wrote up the necessary code to do the positioning this afternoon on paper, now I just need to get it written in code and incorporated into the source. I may also need to refactor my trigonometric functions a bit; they were assuming whole word values and I&#8217;m pretty much exclusively in bytes, for sprites and coordinates.<\/p>\n<p>Oh yeah, the trigonometric functions. No, I&#8217;m not using the ROM routines. They&#8217;re difficult to implement and use, plus I&#8217;d have to convert everything to Radix 100 floating point. Instead I use whole integers, with a base of 256, and stored the main 90 degree set as values 0-255 in a data table. Ratios are then calculated by multiplying the angle by the data table value and dividing by 256. It&#8217;s obviously nowhere near as accurate as a floating-point method, but for what I&#8217;m using it for, it works pretty well.<\/p>\n<p>When I get battlemaps loading and positioning of units working, I will probably move on to getting combat ending debugged and sorted out. I&#8217;d like to have the end-to-end of it mostly done before I get into the long arduous process of spell FX.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I continue working on the combat engine, finishing up other portions of &#8220;temporary&#8221; code has come up. One particular one is the use of battlemaps. Most of the 2D CRPG tactical combat games feature some kind of varied terrain &hellip; <a href=\"http:\/\/www.adamantyr.com\/index.php\/2013\/03\/10\/battlemap-rebuild\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,4,5,11,12],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-coding","category-crpg","category-design","category-screenshots","category-ti-994a"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pgaeMJ-1y","_links":{"self":[{"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":1,"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":657,"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions\/657"}],"wp:attachment":[{"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.adamantyr.com\/index.php\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}