{"id":316,"date":"2023-01-13T17:09:12","date_gmt":"2023-01-13T17:09:12","guid":{"rendered":"https:\/\/trewest.dev\/blog\/?p=316"},"modified":"2023-12-05T09:34:34","modified_gmt":"2023-12-05T09:34:34","slug":"project-computer-craft-scripts","status":"publish","type":"post","link":"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/13\/project-computer-craft-scripts\/","title":{"rendered":"Project: Computer Craft Scripts"},"content":{"rendered":"<div class=\"wp-post-series-box series-project-overviews wp-post-series-box--expandable\">\n\t\t\t<input id=\"collapsible-series-project-overviews69dac26fcc7b2\" class=\"wp-post-series-box__toggle_checkbox\" type=\"checkbox\">\n\t\n\t<label\n\t\tclass=\"wp-post-series-box__label\"\n\t\t\t\t\tfor=\"collapsible-series-project-overviews69dac26fcc7b2\"\n\t\t\ttabindex=\"0\"\n\t\t\t\t>\n\t\t<p class=\"wp-post-series-box__name wp-post-series-name\">\n\t\t\tThis is post 14 of 21 in the series <em>&ldquo;Project Overviews&rdquo;<\/em>\t\t<\/p>\n\t\t\t\t\t<div class=\"wp-post-series-box__description wp-post-series-description\">\n\t\t\t\t<p>A series of posts detailing and highlighting projects that I&#8217;ve made.<\/p>\n\t\t\t<\/div>\n\t\t\t<\/label>\n\n\t\t\t<div class=\"wp-post-series-box__posts\">\n\t\t\t<ol>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/10\/project-bugtrakt\/\">Project: Bugtrakt<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/10\/project-imgalib\/\">Project: Imgalib<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/10\/project-go-fish\/\">Project: Go Fish!<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/11\/project-trewest-dev\/\">Project: trewest.dev<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/11\/arduino-light-effects\/\">Arduino Light effects<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/11\/project-mini-stacker\/\">Project: Mini Stacker<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/12\/project-arduino-nes-controller\/\">Project: Arduino NES Controller<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/12\/project-metroidvania-like\/\">Project: Metroidvania-Like<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/12\/project-susminer\/\">Project: Susminer<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/12\/project-farming-demo\/\">Project: Farming Demo<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/13\/project-vrc-basement\/\">Project: VRC Basement<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/13\/project-hash-clear\/\">Project: Hash Clear<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/13\/project-titan-help\/\">Project: Titan Help<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><span class=\"wp-post-series-box__current\">Project: Computer Craft Scripts<\/span><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/15\/project-esp8266-plant-watering\/\">Project: ESP8266 Plant Watering<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/15\/project-golf-demo\/\">Project: Golf Demo<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/15\/project-ghost-holic\/\">Project: Ghost Holic<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/03\/27\/project-multi-chat-viewer\/\">Project: Multi-Chat Viewer<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2024\/08\/22\/project-interview-simulator\/\">Project: Interview Simulator<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2024\/10\/18\/project-healers-journey\/\">Project: Healer&#8217;s Journey<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2024\/10\/18\/project-biota\/\">Project: Biota<\/a><\/li>\n\t\t\t\t\t\t\t<\/ol>\n\t\t<\/div>\n\t<\/div>\n\n<p>Status: <span style=\"color:green;\">Complete<\/span><br>Type: Game Modification, Automation<br>Technology: Lua, Minecraft, Computer Craft<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/trewest.dev\">Return to Portfolio<\/a><\/div>\n<\/div>\n\n\n\n<p>This is a case of simulated automation using video games as a tool to make scenarios to support making your own versions of technology that might otherwise be unfeasible.  In this case I&#8217;m using Minecraft to support the automation of machinery\/mechanical processes.  Using a mod called Computer Craft I can have agents in the world called Turtles (which maybe a reference to some classic ways of teaching certain programming concepts).  Turtles can do a lot of things that players can do, such as manipulate inventories, mine items, craft items, etc. making them very useful if you&#8217;re able to understand and support their abilities.  <\/p>\n\n\n\n<p>In this case I have two different programs designed for them.  One was originally supposed to take input from a chest and divide charcoal between two other chests while redirecting anything else to another chest for further processing.  This was defunct pretty quickly since I later implemented my second program to alleviate the need for the first.  <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"564\" src=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.13-1024x564.png\" alt=\"\" class=\"wp-image-318\" srcset=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.13-1024x564.png 1024w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.13-300x165.png 300w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.13-768x423.png 768w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.13-1536x846.png 1536w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.13.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">The tree farm, automated by another mod<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"564\" src=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.21-1024x564.png\" alt=\"\" class=\"wp-image-319\" srcset=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.21-1024x564.png 1024w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.21-300x165.png 300w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.21-768x423.png 768w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.21-1536x846.png 1536w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-11_17.26.21.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Beneath the Tree Farm, Charcoal production takes place.  To the right you can see the first program at work.<\/figcaption><\/figure>\n\n\n\n<p>My second program was designed to take input from a chest, and craft an item from the input, then move all the excess into another chest.  This was simple since the thing I needed to craft required one component item spread across the crafting grid (which is a surprisingly common case) this was easy to do by simply looping through an attempt to take items from the source until I had nine of them minimum, dividing it amongst the nine squares, and then crafting the item before sending everything into the output chest.  If only things were that easy in real life! <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"564\" src=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-12_07.15.27-1024x564.png\" alt=\"\" class=\"wp-image-317\" srcset=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-12_07.15.27-1024x564.png 1024w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-12_07.15.27-300x165.png 300w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-12_07.15.27-768x423.png 768w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-12_07.15.27-1536x846.png 1536w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/2023-01-12_07.15.27.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">This is a Rubber farm which is automated, the machine produces small rubber which the turtle converts into the useful large rubber before we store it.<\/figcaption><\/figure>\n\n\n\n<p>Join me next time when I take everything to a level far too complicated by making a NodeJS program interface with the turtles to automate my warehouse management for me, and another to manage mining dangerous long tunnels that feed into the warehouse system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Program One<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>state = 0 -- 0 means the turtle is facing the intake chest\r\n-- 1 is the clean output\r\n-- 2 is fuel\r\n \r\nfunction chance()\r\n    return math.floor(math.random()*2) ==1\r\nend\r\n \r\n \r\nfunction left()\r\n    state = state +1\r\n    turtle.turnLeft()\r\nend\r\n \r\nfunction right()\r\n    state = state - 1\r\n    turtle.turnRight()\r\nend\r\n \r\nfunction eat()\r\n    while state ~=0\r\n    do\r\n        if state > 0 then\r\n            right()\r\n        else\r\n            left()\r\n        end\r\n    end\r\n    while(turtle.getItemDetail(1) == nil)\r\n    do\r\n        turtle.suck()\r\n    end\r\n    distribute()\r\nend\r\n \r\nfunction distribute()\r\n    if turtle.getItemDetail(1).name == \"minecraft:coal\" then\r\n        good = chance()\r\n        if good then\r\n            left()\r\n            turtle.drop()\r\n            right()\r\n        else\r\n            left()\r\n            left()\r\n            turtle.drop()\r\n            right()\r\n            right()\r\n        end\r\n    else\r\n        if turtle.getItemDetail().name == \"minecraft:log\" then\r\n            turtle.dropUp()\r\n        else\r\n        left()\r\n        turtle.drop()\r\n        right()\r\n    end\r\n    end\r\n    eat()\r\nend\r\neat()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Program 2<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>state = 0\r\n \r\nfunction canCraft() \r\n    turtle.select(1)\r\n    return turtle.getItemCount() >= 9\r\nend\r\n \r\n \r\n \r\nfunction clearSlots()\r\n    for i=2,16,1\r\n    do\r\n        if turtle.getItemCount(i) ~= 0 then\r\n            turtle.select(i)\r\n            turtle.dropUp()\r\n        end\r\n    end\r\n    turtle.select(1)\r\nend\r\n \r\nfunction placeCrafting()\r\n    count = math.floor(turtle.getItemCount() \/9)\r\n    print(\"There should be \" .. count .. \" rubber crafted\")\r\n    turtle.transferTo(2,count)\r\n    turtle.transferTo(3,count)\r\n    turtle.transferTo(5,count)\r\n    turtle.transferTo(6,count)\r\n    turtle.transferTo(7,count)\r\n    turtle.transferTo(9,count)\r\n    turtle.transferTo(10,count)\r\n    turtle.transferTo(11,count)\r\nend\r\n \r\nfunction craft()\r\n    print(\"Crafting!\")\r\n    clearSlots()\r\n    placeCrafting()\r\n    turtle.select(16)\r\n    turtle.craft()\r\n    turtle.dropUp()\r\n    turtle.select(1)\r\nend\r\n \r\nwhile (state == 0)\r\ndo\r\n if canCraft() == false then\r\n    turtle.suck()\r\n    else\r\n        craft()\r\n    end\r\nend<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a case of simulated automation using video games as a tool to make scenarios to support making your own versions of technology that might otherwise be unfeasible.  In this case I&#8217;m using Minecraft to support the automation of machinery\/mechanical processes. &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,65,8,52],"tags":[66,67],"post_series":[53],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-coding","category-lua","category-project","category-videogame","tag-lua","tag-program","post_series-project-overviews"],"_links":{"self":[{"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=316"}],"version-history":[{"count":1,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":320,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/posts\/316\/revisions\/320"}],"wp:attachment":[{"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=316"},{"taxonomy":"post_series","embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/post_series?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}