Advertisement
renanmfd

sworm_slave

Dec 2nd, 2022 (edited)
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.03 KB | None | 0 0
  1. -- @file sworm_slave.lua
  2. --
  3. -------------------------------------------------------------------------------
  4.  
  5. -- ============================================================================
  6. -- Variables ------------------------------------------------------------------
  7.  
  8. -- Blocks to ignore.
  9. local INVENTORY_IGNORE_MAP = {
  10.   "minecraft:stone",
  11.   "minecraft:cobblestone",
  12.   "minecraft:netherrack",
  13.   "minecraft:soul_sand",
  14.   "minecraft:grass_block",
  15.   "minecraft:dirt",
  16.   "minecraft:gravel",
  17.   "minecraft:grass",
  18.   "minecraft:snow",
  19. }
  20.  
  21. -- state and position handlers
  22. local state  = "setting"   -- "mining" "moving" "waiting" "setting"\
  23. local spot
  24.  
  25. -- file handler
  26. local file = "state.cc"
  27.  
  28. -- wireless channels
  29. local modem = nil
  30. local initialChannel = 100
  31. local myChannel = nil
  32.  
  33. -- ============================================================================
  34. -- PRIVATE functions ----------------------------------------------------------
  35.  
  36. local function message(str)
  37.   print(str)
  38.   --modem.transmit( initialChannel , myChannel , "- "..str )
  39. end
  40.  
  41. -------------------------------------------------------------------------------
  42.  
  43. local function saveState()
  44.   f = fs.open(file, "w")
  45.   f.writeLine(state)
  46.   f.close()
  47. end
  48.  
  49. -------------------------------------------------------------------------------
  50.  
  51. local function loadState()
  52.   if not fs.exists(file) then
  53.     return false
  54.   end
  55.   f = fs.open( file , "r" )
  56.   state = f.readLine()
  57.   f.close()
  58.   return true
  59. end
  60.  
  61. -------------------------------------------------------------------------------
  62.  
  63. function rawread()
  64.   print("Press enter to continue")
  65.     while true do
  66.         local sEvent, param = os.pullEvent("key")
  67.         if sEvent == "key" then
  68.             if param == 28 then
  69.                 print("Enter detected")
  70.                 break
  71.             end
  72.         end
  73.     end
  74. end
  75.  
  76. -- ============================================================================
  77. -- PUBLIC functions -----------------------------------------------------------
  78.  
  79. -------------------------------------------------------------------------------
  80.  
  81. setting = function ()
  82.   state = "setting"
  83.   message("call setting()")
  84.  
  85.   modem.open(initialChannel)
  86.   sleep(0.1)
  87.  
  88.   modem.transmit(initialChannel, myChannel, "ready")
  89.  
  90.   modem.open(myChannel)
  91.   local event, side, freq , reply , msg , dist = os.pullEvent("modem_message")
  92.   message(msg .. " - " .. myChannel)
  93.  
  94.   while tostring(msg) ~= tostring(myChannel) do
  95.     message("Waiting for 'set' message on channel " .. myChannel)
  96.     event, side, freq , reply , msg , dist = os.pullEvent("modem_message")
  97.   end
  98.  
  99.   message("Channel/ID set to " .. myChannel)
  100.  
  101.   if turtle.getFuelLevel() < 10 then
  102.     sworm_api.refuel()
  103.   end
  104.  
  105.   modem.close(initialChannel)
  106.   modem.close(myChannel)
  107. end
  108.  
  109. -------------------------------------------------------------------------------
  110.  
  111. getNextSpot = function ()
  112.   local x, y, z
  113.   local count = 0
  114.   local event, side, freq , reply , spot , dist
  115.  
  116.   message("call getNextSpot()")
  117.   modem.open(myChannel)
  118.   sleep(0.1)
  119.  
  120.   while true do
  121.     message("Sending Request CH:" .. myChannel)
  122.     modem.transmit(myChannel, myChannel, "request")
  123.     os.startTimer(20)
  124.     event, side, freq , reply , spot , dist = os.pullEvent()
  125.     if event == "modem_message" then
  126.       x, y, z = msg
  127.       message("Request attended x=" .. spot.x .. " y=" .. spot.y .. " z=" .. spot.z)
  128.       break
  129.     elseif event == "timer" then
  130.       count = count + 1
  131.       message("Timeout getNextSpot() " .. count)
  132.       if count > 10 then
  133.         sleep(10)
  134.       end
  135.     end
  136.   end
  137.   modem.close(myChannel)
  138.   return spot
  139. end
  140.  
  141. -------------------------------------------------------------------------------
  142.  
  143. excavate = function ()
  144.   local depth = 0
  145.   local success, data
  146.  
  147.   state = "mining"
  148.  
  149.   -- Excavate tower.
  150.   while sworm_api.down() do
  151.     for i = 1 , 4 do
  152.       local ignore = checkIgnore()
  153.  
  154.       if not ignore then
  155.         turtle.dig()
  156.         sworm_api.checkInventory()  
  157.       end
  158.  
  159.       if i ~= 4 then
  160.         sworm_api.right()
  161.       end
  162.     end
  163.  
  164.     -- Check for lava on bottom coordinates.
  165.     success, data = turtle.inspectDown()
  166.     if data.name == 'minecraft:lava' then
  167.       turtle.select(sworm_api.TURTLE_SLOT_BUCKET)
  168.       turtle.place()
  169.       turtle.refuel()
  170.       message("Lava found and used for fuel.")
  171.       turtle.select(sworm_api.TURTLE_SLOT_INVENTORY)
  172.     end
  173.  
  174.     turtle.digDown()
  175.     sworm_api.quickCheckInventory()
  176.  
  177.     depth = depth + 1
  178.   end
  179.  
  180.   -- Back to origin height.
  181.   while depth > 0 do
  182.     sworm_api.up()
  183.     depth = depth - 1
  184.   end
  185.  
  186.   -- Make sure we're back on the original position.
  187.   sworm_api.moveTo(spot)
  188. end
  189.  
  190. -------------------------------------------------------------------------------
  191.  
  192. checkIgnore = function ()
  193.   local index, ignored_block
  194.   local success, data = turtle.inspect()
  195.  
  196.   if not success then
  197.     -- message("turtle.inspect() failed. Probably air.")
  198.     return true
  199.   end
  200.  
  201.   if data.name == 'minecraft:lava' then
  202.     turtle.select(sworm_api.TURTLE_SLOT_BUCKET)
  203.     turtle.place()
  204.     turtle.refuel()
  205.     message("Lava found and used for fuel.")
  206.     turtle.select(sworm_api.TURTLE_SLOT_INVENTORY)
  207.     return true
  208.   end
  209.  
  210.   for index, ignored_block in pairs(INVENTORY_IGNORE_MAP) do
  211.     if data.name == ignored_block then
  212.       return true
  213.     end
  214.   end
  215.  
  216.   -- Do not ignore if past all checks.
  217.   return false
  218. end
  219.  
  220. -------------------------------------------------------------------------------
  221. -- Main -----------------------------------------------------------------------
  222.  
  223. main = function ()
  224.   local init = false
  225.  
  226.   if init == false and state ~= "setting" then
  227.     sworm_api.init()
  228.     init = true
  229.   end
  230.  
  231.   modem = peripheral.find("modem")
  232.   myChannel = os.getComputerID()
  233.  
  234.   if loadState() then
  235.     message("State loaded. Resuming task.")
  236.     sleep(10)
  237.   else
  238.     message("Slave started. Ready to mine!")
  239.     state = "setting"
  240.   end
  241.  
  242.   while true do
  243.  
  244.     if state == "setting" then
  245.       message("--> Setting")
  246.       setting()
  247.       if init == false then
  248.         sworm_api.init()
  249.         init = true
  250.       end
  251.       state = "waiting"
  252.       saveState()
  253.  
  254.     elseif state == "waiting" then
  255.       message("--> Waiting")
  256.       spot = getNextSpot()
  257.       state = "moving"
  258.       saveState()
  259.  
  260.     elseif state == "moving" then
  261.       message("--> Moving")
  262.       if init == false then
  263.         sworm_api.init()
  264.         init = true
  265.         state = "waiting"
  266.         spot = getNextSpot()
  267.       end
  268.       spot = vector.new(spot.x, spot.y, spot.z)
  269.       sworm_api.moveTo(spot)
  270.       state = "mining"
  271.       saveState()
  272.  
  273.     elseif state == "mining" then
  274.       -- rawread()
  275.       message("--> Mining")
  276.       if init == false then
  277.         sworm_api.init()
  278.         init = true
  279.       end
  280.       sworm_api.checkInventory()
  281.       excavate()
  282.       state = "waiting"
  283.       saveState()
  284.  
  285.     else
  286.       message("Invalid state - main() " .. state)
  287.     end
  288.  
  289.   end
  290. end
  291.  
  292. os.loadAPI("sworm_api")
  293. sleep(5)
  294. main()
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement