{"id":20,"date":"2024-04-01T10:24:44","date_gmt":"2024-04-01T10:24:44","guid":{"rendered":"https:\/\/nona.games\/blog\/?p=20"},"modified":"2024-04-01T10:24:44","modified_gmt":"2024-04-01T10:24:44","slug":"what-if-you-were-a-behaviour-tree","status":"publish","type":"post","link":"https:\/\/nonanonanona.com\/blog\/2024\/04\/01\/what-if-you-were-a-behaviour-tree\/","title":{"rendered":"What if you were a behaviour tree"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">So! Going on the idea of the &#8220;genome editing&#8221; prototype, I need to decide how the player will be able to edit colony\/forager behavior. I also need to figure out how to implement the behavior to begin with. Behavior trees sound like a good idea for this I think.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How BTs (behavior trees) work is by segmenting decisionmaking into modular nodes. Every frame of the game (roughly speaking), an ant will look at the root node of the BT and figure out what it should be doing in that moment based on priority. Priority is determined by the order that a node&#8217;s children are in (roughly speaking).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"3000\" height=\"1688\" src=\"https:\/\/nona.games\/blog\/wp-content\/uploads\/2024\/04\/BT-FORAGER-1-edited.png\" alt=\"\" class=\"wp-image-26\" srcset=\"https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-FORAGER-1-edited.png 3000w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-FORAGER-1-edited-300x169.png 300w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-FORAGER-1-edited-1024x576.png 1024w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-FORAGER-1-edited-768x432.png 768w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-FORAGER-1-edited-1536x864.png 1536w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-FORAGER-1-edited-2048x1152.png 2048w\" sizes=\"auto, (max-width: 3000px) 100vw, 3000px\" \/><figcaption class=\"wp-element-caption\">Behavior tree for a forager.<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">In the above diagram, our forager&#8217;s root node is a [?], which represents a <strong>Fallback Selector<\/strong>. Selector nodes don&#8217;t tell the ant to do anything by themselves, that&#8217;s their children&#8217;s job (or grandchildren&#8217;s job. or great-grandchildren&#8217;s job). The selector simply asks its children to either say, <strong>&#8220;success&#8221;<\/strong>,<strong>  &#8220;failure&#8221;<\/strong>,<strong> <\/strong>or <strong>&#8220;in progress&#8221;<\/strong>, starting with its first child (leftmost). In the case of the fallback selector, if the child says either &#8220;success&#8221; or &#8220;in progress&#8221;, the selector itself will declare a &#8220;success&#8221;. If the child says &#8220;failure&#8221;, then the selector will ask the next child to respond. Only if all of its children respond &#8220;failure&#8221;, will the fallback selector declare &#8220;failure&#8221; itself. For a root node, &#8220;success&#8221; means, &#8220;I&#8217;m going to do something now&#8221;. &#8220;failure&#8221; means &#8220;I couldn&#8217;t figure out anything to do right now&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we look at our root node&#8217;s children, they&#8217;re mostly all [->]&#8217;s, which are <strong>Sequence Selectors<\/strong>. Sequence selectors differ from fallback selectors in that they won&#8217;t accept failure from any of their children. Sequence selectors only declare &#8220;success&#8221; if all of their children respond &#8220;success&#8221; in sequence. <em>[A child responding &#8220;in progress&#8221; is fine too, in that case the sequence selector won&#8217;t ask any of its other children for a response, because it needs to know the final result of the in-progress node first. It&#8217;ll still report &#8220;success&#8221;, because nothing has gone wrong yet.]<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What does it mean that our fallback selector root node&#8217;s children are mostly sequence selectors? It means that we want our ant to act on just one of its sequences of tasks. The root node has five sequence selector children, so five possible task lisks (ignore [wander] for now). If its first child selector reports a success, then the root node won&#8217;t look at any of its other children, because it only needs to know of one success. This creates priority.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first sequence has two children nodes, [danger?] and [react]. If the ant senses immediate danger (a predator, a hurt sister, etc), the [danger?] node will report &#8220;success&#8221;, and then the selector will move onto the [react] node, which will tell the ant to react to the situation in some way. We don&#8217;t care if the reaction remediates the situation, only that the ant was able to react. If it was, then [react] reports &#8220;success&#8221;, and the selector node is satisfied. It reports back to the root node, &#8220;success&#8221;, and tick&#8217;s decision ends. If for some reason the selector reported &#8220;failure&#8221;, either because there was no danger, or that the ant had no way to respond to the danger, the root node will then try its next child.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That next child, also a sequence selector, describes the task of returning home once the forager&#8217;s inventory is full. [full?]&#8217;s sibling is (trail home). The circle represents the root node of the tree below.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"3000\" height=\"1688\" src=\"https:\/\/nona.games\/blog\/wp-content\/uploads\/2024\/04\/BT-TRAIL-1-edited.png\" alt=\"\" class=\"wp-image-24\" srcset=\"https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-TRAIL-1-edited.png 3000w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-TRAIL-1-edited-300x169.png 300w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-TRAIL-1-edited-1024x576.png 1024w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-TRAIL-1-edited-768x432.png 768w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-TRAIL-1-edited-1536x864.png 1536w, https:\/\/nonanonanona.com\/blog\/wp-content\/uploads\/2024\/04\/BT-TRAIL-1-edited-2048x1152.png 2048w\" sizes=\"auto, (max-width: 3000px) 100vw, 3000px\" \/><figcaption class=\"wp-element-caption\">Sub behavior tree for when ants are actively following a trail.<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Ants follow trails both towards and away from home, so I seperated the &#8220;follow trail&#8221; decision making into its own tree, so I can reuse it in multiple places instead of writing the tree twice. That&#8217;s all I&#8217;m using subtrees for, just code re-use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the ant doesn&#8217;t have a full belly, it&#8217;ll look around to see if there&#8217;s food to collect. If it can&#8217;t find food, it&#8217;ll check its own hunger. If it&#8217;s hungry and a sister is near, it&#8217;ll ask her to share some of her food. If the ant isn&#8217;t hungry, or if it couldn&#8217;t get food from a sister, then it&#8217;ll look around for a nearby trail, and then follow it. And finally if it can&#8217;t find a trail, it&#8217;ll just wander around, which is that last [wander] node. Because wandering is a simple task, it doesn&#8217;t have siblings or a selector parent. It just responds whether the ant was able to wander or not.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These nodes are rearrangable, so I could make ants that prioritize returning home with resources over responding to immediate threats by changing the order that those sequence selector nodes are in.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The colony&#8217;s background economy\/production will also use a BT to decide things like ant spawn rate and resource allocation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Eventually, I want to make a user interface so that the player can edit\/rearrange the BTs themselves, allowing them to modify the colony&#8217;s behavior to best suit the environment. Maybe a visual tree with drag-and-drop nodes? The neat thing about the Godot Engine is that the code that runs the Editor user interface is the same code that built games use for their own user interfaces. So if I make a visual BT editor for the player, I could use it that same visual editor to make BTs in the editor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So that&#8217;s the plan for now! Implement behavior trees! Make them tell ants what to do! Make them editable!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So! Going on the idea of the &#8220;genome editing&#8221; prototype, I need to decide how the player will be able to edit colony\/forager behavior. I also need to figure out how to implement the behavior to begin with. Behavior trees sound like a good idea for this I think. How BTs (behavior trees) work is [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"https:\/\/nonanonanona.com\/blog\/2024\/04\/01\/what-if-you-were-a-behaviour-tree\/\">Read More&#8230;<span class=\"screen-reader-text\"> from What if you were a behaviour tree<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,4],"tags":[],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-ant-game","category-development"],"_links":{"self":[{"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":1,"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":27,"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions\/27"}],"wp:attachment":[{"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nonanonanona.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}