{"id":1154,"date":"2022-12-19T12:00:00","date_gmt":"2022-12-19T20:00:00","guid":{"rendered":"https:\/\/letsbuildroboticswithshadow8472.com\/?p=1154"},"modified":"2022-12-19T11:50:14","modified_gmt":"2022-12-19T19:50:14","slug":"on-the-specification-of-state-machines","status":"publish","type":"post","link":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/2022\/12\/19\/on-the-specification-of-state-machines\/","title":{"rendered":"On the Specification of State Machines"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Good Morning from my Robotics Lab! This is Shadow_8472 with a smaller project segment for the week. Let\u2019s get started!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019m learning Godot, a free\/open source game engine. My chosen project is a Sonic fan game where Cream the Rabbit overhauls a wild chao garden overrun with pollution. Previously, I coded up a cube I could move around in 3D.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What I didn\u2019t cover was how everything flopped when I introduced delta, a variable to adjust calculations to account for the time between frames. While I dream of posting this game as Wii homebrew (3rd party shareware), I understand that most people interested in Chao these days are on PC. Even if this were an exclusive title to a locked-down platform, lag would still need to account for framerate drops. I tried adjusting my jump related constants so many times, but since I missed a delta or two, I was never going to get it right. When I initially gave up, I directed my efforts towards designing a state machine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s almost silly how many states a 3D platformer player character can be in. Walking, running, sprinting, standing still, holding an item \u2013 and that doesn\u2019t even touch the number of jump states I can expect Cream or other characters to find themselves in. I used a massive if-else structure to decide what string to describe state. At a certain point, I studied the character movement and animations of Sonc 3 &amp; Knuckles (Sonic Megacollection for GameCube, run on a Wii) and Sonic Adventure 2: Battle (SA2B) as inspiration. In both games, holding jump powers you upwards until you either release or reach a maximum height; it just happens so fast (unless you\u2019re in a low gravity section) that it feels natural. I had to re-think my state machine a few times, but I settled on a 32 bit number to chop up into a mixture of 1 bit flags and other data as needed. In particular, I focused on the least significant byte (the last 8 bits).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I went through several iterations over a couple of days, but the two rightmost bits were consistently used to denote a character\u2019s vertical (Y) and horizontal (XZ) movements as I figured they would be most useful for telling other aspects about a state. Moving left, the next four bits are for progressively more detailed information about what a character is doing \u201cenvironment\u201d \u2013 you can be walking.on_ground.skidding, swimming_underwater.bottom_walking.normally, or using a character\u2019s special move assigned to a reserved set of states.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A good chunk of thought went into representing all these states in a visually digestible format. It wasn\u2019t until I began thinking of the XYZ motion flags as dependent on my environment states that I reduced them to four columns in a chart of sixteen rows that I was readily able to count my vacant states. As of writing, my environment variable uses two bits for \u201crough\u201d and one each for \u201cfine\u201d and \u201cspecial.\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The 128\u2019s and 64\u2019s bits are penciled in as a counter for idle animations or how many double jumps a flying character has remaining. Beyond that, I have 24 bits remaining as the game grows. For example: eight bits affords up to 255 unique held items plus a reserved id for when a character holds nothing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Takeaway<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I did not write a single line of code while working on the main event today despite it being a programming project. I did start on the next part where I began implementing this standard, but that felt like I was starting a whole new section, so I\u2019m saving it for later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Programming programs and composing literature (in the broadest sense) are more similar than people realize. What happened here today is analogous to a novelist making an outline ahead of a first draft. An important puzzle was solved and in important internal standard established.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Question<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">How many basic states are there for an average character in your favorite game?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good Morning from my Robotics Lab! This is Shadow_8472 with a smaller project segment for the week. Let\u2019s get started! I\u2019m learning Godot, a free\/open source game engine. My chosen project is a Sonic fan game where Cream the Rabbit overhauls a wild chao garden overrun with pollution. Previously, I coded up a cube I &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/2022\/12\/19\/on-the-specification-of-state-machines\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;On the Specification of State Machines&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1154","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/comments?post=1154"}],"version-history":[{"count":1,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1154\/revisions"}],"predecessor-version":[{"id":1155,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1154\/revisions\/1155"}],"wp:attachment":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/media?parent=1154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/categories?post=1154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/tags?post=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}