renanmfd

slavecode

Feb 21st, 2014 (edited)
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.80 KB | None | 0 0
  1. -- @file slavecode.lua
  2. --
  3. -------------------------------------------------------------------------------
  4.  
  5. -- inventory slots
  6.  
  7. local INVENTORY_FUEL_CHEST = 1
  8. local INVENTORY_ITEM_CHEST = 2
  9. local INVENTORY_BUCKET     = 3
  10. local INVENTORY_STORAGE    = 4
  11. local INVENTORY_IGNORE_MAP = {
  12.     "minecraft:stone",
  13.     "minecraft:cobblestone",
  14.     "minecraft:grass_block",
  15.     "minecraft:dirt",
  16.     "minecraft:grass",
  17.     "minecraft:snow",
  18. }
  19.  
  20. -- directions
  21.  
  22. local DIRECTION_NORTH = 1
  23. local DIRECTION_EAST  = 2
  24. local DIRECTION_SOUTH = 3
  25. local DIRECTION_WEST  = 4
  26. local DIRECTION_UP    = 5
  27. local DIRECTION_DOWN  = 6
  28.  
  29. -- state and position handlers
  30.  
  31. local state  = "setting"   -- "mining" "moving" "waiting" "setting"
  32. local facing = DIRECTION_EAST
  33. local x = 0
  34. local z = 0
  35. local y = 0
  36. local movex = nil
  37. local movey = nil
  38.  
  39. -- file handler
  40.  
  41. local file = "state.cc"
  42.  
  43. -- wireless channels
  44.  
  45. local initialChannel = 100
  46. local myChannel = nil
  47.  
  48. -------------------------------------------------------------------------------
  49. -- Function declarations ------------------------------------------------------
  50.  
  51. local message
  52. local unloadInventory
  53. local checkInventory
  54. local saveState
  55. local loadState
  56.  
  57. local forward
  58. local nforward
  59. local up
  60. local down
  61. local left
  62. local right
  63. local refuel
  64. local updateLocation
  65. local updateHeight
  66. local turnTo
  67.  
  68. local setting
  69. local getNextSpot
  70. local move
  71. local excavate
  72.  
  73. -------------------------------------------------------------------------------
  74. -------------------------------------------------------------------------------
  75.  
  76. message = function(str)
  77.     print(str)
  78.     --modem.transmit( initialChannel , myChannel , "- "..str )
  79. end
  80.  
  81. -------------------------------------------------------------------------------
  82.  
  83. unloadInventory = function()
  84.     turtle.select(INVENTORY_ITEM_CHEST)
  85.     turtle.placeUp()
  86.     for i = INVENTORY_STORAGE , 16 do
  87.         turtle.select(i)
  88.         turtle.dropUp()
  89.     end
  90.     turtle.select(INVENTORY_ITEM_CHEST)
  91.     turtle.digUp()
  92. end
  93.  
  94. -------------------------------------------------------------------------------
  95.  
  96. checkInventory = function()
  97.     for i = INVENTORY_STORAGE , 16 do
  98.         turtle.select(i)
  99.         item = turtle.getItemCount(i)
  100.         if item == 0 then
  101.             return false       
  102.         end
  103.     end
  104.     unloadInventory()
  105.     return true
  106. end
  107.  
  108. -------------------------------------------------------------------------------
  109.  
  110. quickCheckInventory = function()
  111.     local item = turtle.getItemCount(16)
  112.     if item == 0 then
  113.         return false       
  114.     end
  115.     unloadInventory()
  116.     return true
  117. end
  118.  
  119. -------------------------------------------------------------------------------
  120.  
  121. saveState = function()
  122.     f = fs.open(file, "w")
  123.     f.writeLine(state)
  124.     f.writeLine(facing)
  125.     f.writeLine(tostring(x))
  126.     f.writeLine(tostring(z))
  127.     f.writeLine(tostring(y))
  128.     f.writeLine(tostring(movex))
  129.     f.writeLine(tostring(movey))
  130.     f.close()
  131. end
  132.  
  133. -------------------------------------------------------------------------------
  134.  
  135. loadState = function()
  136.     if not fs.exists(file) then
  137.         return false
  138.     end
  139.     f = fs.open( file , "r" )
  140.     state = f.readLine()
  141.     facing = f.readLine()
  142.     x = tonumber(f.readLine())
  143.     z = tonumber(f.readLine())
  144.     y = tonumber(f.readLine())
  145.     movex = tonumber(f.readLine())
  146.     movey = tonumber(f.readLine())
  147.     f.close()
  148.     return true
  149. end
  150.  
  151. -------------------------------------------------------------------------------
  152.  
  153. forward = function()
  154.     turtle.select(INVENTORY_STORAGE)
  155.     while not turtle.forward() do
  156.         if peripheral.isPresent("front") then
  157.             left()
  158.             forward()
  159.             right()
  160.             sleep(math.random(1, 7))
  161.             forward()
  162.             right()
  163.             forward()
  164.             left()
  165.         elseif not turtle.dig() then
  166.             message("Unbreakable block reached - forward()")
  167.             return false
  168.         elseif turtle.attack() then
  169.             message("Mob on my way. Die! - forward()")
  170.         elseif turtle.getFuelLevel() == 0 then
  171.             message("Low fuel - forward()")
  172.             refuel()
  173.             message("Refueled. Ready to go!")
  174.         end
  175.         quickCheckInventory()
  176.     end
  177.     updateLocation()
  178.     saveState()
  179.     return true
  180. end
  181.  
  182. -------------------------------------------------------------------------------
  183.  
  184. nforward = function(num)
  185.     if num < 0 then
  186.         message("Invalid number - nforward(num) =" .. num)
  187.         return false
  188.     elseif num == 0 then
  189.         return true
  190.     end
  191.  
  192.     -- If moving a lot forward, lets travel one level above.
  193.     if num > 6 then
  194.         forward()
  195.         up()
  196.  
  197.         -- Move
  198.         for i = 2 , num - 1 do
  199.             forward()
  200.         end
  201.  
  202.         down()
  203.         forward()
  204.     else
  205.         for i = 1 , num do
  206.             forward()
  207.         end
  208.     end
  209.     return true
  210. end
  211.  
  212. -------------------------------------------------------------------------------
  213.  
  214. down = function()
  215.     turtle.select(INVENTORY_STORAGE)
  216.     while not turtle.down() do
  217.         if peripheral.isPresent("bottom") then
  218.             left()
  219.             left()
  220.             forward()
  221.             left()
  222.             left()
  223.             sleep(math.random(1, 7))
  224.             forward()
  225.         elseif not turtle.digDown() then
  226.             message("Bedrock reached - down()")
  227.             return false
  228.         elseif turtle.attackDown() then
  229.             message("Mob on the way. Die! - down()")
  230.         elseif turtle.getFuelLevel() == 0 then
  231.             message("Low fuel - down() ")
  232.             refuel()
  233.             message("Refueled. Ready to go!")
  234.         end
  235.         quickCheckInventory()
  236.     end
  237.     updateHeight(DIRECTION_DOWN)
  238.     return true
  239. end
  240.  
  241. -------------------------------------------------------------------------------
  242.  
  243. up = function()
  244.     turtle.select(INVENTORY_STORAGE)
  245.     while not turtle.up() do
  246.         if peripheral.isPresent("top") then
  247.             left()
  248.             left()
  249.             forward()
  250.             left()
  251.             left()
  252.             sleep(math.random(1, 7))
  253.             forward()
  254.         elseif not turtle.digUp() then
  255.             message("Bedrock reached - up()")
  256.             return false
  257.         elseif turtle.attackUp() then
  258.             message("Mob on the way. Die! - up()")
  259.         elseif turtle.getFuelLevel() == 0 then
  260.             message("Low fuel - up() ")
  261.             refuel()
  262.             message("Refueled. Ready to go!")
  263.         end
  264.         quickCheckInventory()
  265.     end
  266.     updateHeight(DIRECTION_UP)
  267.     return true
  268. end
  269.  
  270. -------------------------------------------------------------------------------
  271.  
  272. left = function()
  273.     --print("turnLeft() facing=", facing," direction")
  274.     turtle.turnLeft()
  275.     if facing == DIRECTION_NORTH then
  276.         facing = DIRECTION_WEST
  277.     elseif facing == DIRECTION_SOUTH then
  278.         facing = DIRECTION_EAST
  279.     elseif facing == DIRECTION_EAST then
  280.         facing = DIRECTION_NORTH
  281.     elseif facing == DIRECTION_WEST then
  282.         facing = DIRECTION_SOUTH
  283.     else
  284.         message("Invalid facing direction - left() ", facing)
  285.     end
  286.     saveState();
  287. end
  288.  
  289. -------------------------------------------------------------------------------
  290.  
  291. right = function()
  292.     --print("turnRight() facing=", facing," direction")
  293.     turtle.turnRight()
  294.     if facing == DIRECTION_NORTH then
  295.         facing = DIRECTION_EAST
  296.     elseif facing == DIRECTION_SOUTH then
  297.         facing = DIRECTION_WEST
  298.     elseif facing == DIRECTION_EAST then
  299.         facing = DIRECTION_SOUTH
  300.     elseif facing == DIRECTION_WEST then
  301.         facing = DIRECTION_NORTH
  302.     else
  303.         message("Invalid facing direction - right() " .. facing)
  304.     end
  305.     saveState();
  306. end
  307.  
  308. -------------------------------------------------------------------------------
  309.  
  310. refuel = function()
  311.     turtle.select(INVENTORY_FUEL_CHEST)
  312.     while not turtle.place() do
  313.         if y<0 then
  314.             turtle.dig()
  315.         else
  316.             right()
  317.         end
  318.     end
  319.     turtle.suck()
  320.     turtle.refuel(64)
  321.     message("Fuel level: "..turtle.getFuelLevel() )
  322.     turtle.select(INVENTORY_FUEL_CHEST)
  323.     turtle.dig()
  324. end
  325.  
  326. -------------------------------------------------------------------------------
  327.  
  328. updateLocation = function()
  329.     if facing == DIRECTION_NORTH then
  330.         y = y - 1
  331.     elseif facing == DIRECTION_SOUTH then
  332.         y = y + 1
  333.     elseif facing == DIRECTION_EAST then
  334.         x = x + 1
  335.     elseif facing == DIRECTION_WEST then
  336.         x = x - 1
  337.     else
  338.         message("Invalid facing position - updateLocation()")
  339.     end
  340. end
  341.  
  342. -------------------------------------------------------------------------------
  343.  
  344. updateHeight = function(str)
  345.     if str == DIRECTION_DOWN then
  346.         z = z - 1
  347.     elseif str == DIRECTION_UP then
  348.         z = z + 1
  349.     else
  350.         message("Invalid moving down/up position - updateHeight()")
  351.     end
  352. end
  353.  
  354. -------------------------------------------------------------------------------
  355.  
  356. turnTo = function(dir)
  357.     if dir == DIRECTION_NORTH then
  358.         if facing == DIRECTION_NORTH then
  359.             return true
  360.         elseif facing == DIRECTION_SOUTH then
  361.             right()
  362.             right()
  363.         elseif facing == DIRECTION_EAST then
  364.             left()
  365.         elseif facing == DIRECTION_WEST then
  366.             right()
  367.         else
  368.             message("Invalid facing direction - turnTo(dir) " .. facing)
  369.         end
  370.     elseif dir == DIRECTION_SOUTH then
  371.         if facing == DIRECTION_SOUTH then
  372.             return true
  373.         elseif facing == DIRECTION_NORTH then
  374.             right()
  375.             right()
  376.         elseif facing == DIRECTION_WEST then
  377.             left()
  378.         elseif facing == DIRECTION_EAST then
  379.             right()
  380.         else
  381.             message("Invalid facing direction - turnTo(dir) " .. facing)
  382.         end
  383.     elseif dir == DIRECTION_WEST then
  384.         if facing == DIRECTION_WEST then
  385.             return true
  386.         elseif facing == DIRECTION_EAST then
  387.             right()
  388.             right()
  389.         elseif facing == DIRECTION_NORTH then
  390.             left()
  391.         elseif facing == DIRECTION_SOUTH then
  392.             right()
  393.         else
  394.             message("Invalid facing direction - turnTo(dir) " .. facing)
  395.         end
  396.     elseif dir == DIRECTION_EAST then
  397.         if facing == DIRECTION_EAST then
  398.             return true
  399.         elseif facing == DIRECTION_WEST then
  400.             right()
  401.             right()
  402.         elseif facing == DIRECTION_SOUTH then
  403.             left()
  404.         elseif facing == DIRECTION_NORTH then
  405.             right()
  406.         else
  407.             message("Invalid facing direction - turnTo(dir) " .. facing)
  408.         end
  409.     else
  410.         message("Invalid goto direction - turnTo(dir) " .. dir)
  411.     end
  412. end
  413.  
  414. -------------------------------------------------------------------------------
  415.  
  416. setting = function()
  417.     message("call setting()")
  418.  
  419.     local modem = peripheral.find("modem")
  420.     myChannel = os.getComputerID()
  421.     modem.open(myChannel)
  422.     sleep(0.5)
  423.  
  424.     modem.transmit(initialChannel, myChannel, "ready")
  425.  
  426.     local event, side, freq , reply , msg , dist = os.pullEvent("modem_message")
  427.     message(msg .. " - " .. myChannel)
  428.     while tostring(msg) ~= tostring(myChannel) do
  429.         message("Waiting for 'set' message on channel " .. myChannel)
  430.         event, side, freq , reply , msg , dist = os.pullEvent("modem_message")
  431.     end
  432.     message("Channel/ID set to " .. myChannel)
  433.  
  434.     if turtle.getFuelLevel() == 0 then
  435.         refuel()
  436.     end
  437.     modem.close(myChannel)
  438. end
  439.  
  440. -------------------------------------------------------------------------------
  441.  
  442. getNextSpot = function()
  443.     message("call getNextSpot()")
  444.     movex = 0
  445.     movey = 0
  446.  
  447.     local modem = peripheral.find("modem")
  448.     modem.open(myChannel)
  449.     sleep(0.5)
  450.  
  451.     local count = 0
  452.     while true do
  453.         message("Sending Request CH:" .. myChannel)
  454.         modem.transmit(myChannel, myChannel, "request")
  455.         os.startTimer(20)
  456.         event = { os.pullEvent() }
  457.         if event[1] == "modem_message" then
  458.             movex = event[4]
  459.             movey = tonumber(event[5])
  460.             message("Request attended " .. movex .. "-" .. movey)
  461.             break
  462.         elseif event[1] == "timer" then
  463.             count = count + 1
  464.             message("Timeout getNextSpot() " .. count)
  465.         end
  466.     end
  467.     modem.close(myChannel)
  468. end
  469.  
  470. -------------------------------------------------------------------------------
  471.  
  472. move = function()
  473.     if movex == nil or movey == nil then
  474.         message("Moving position equals to nil - move()")
  475.         return false
  476.     end
  477.     aux_x = movex - x
  478.     aux_y = movey - y
  479.     message("-- move() where to x=" .. movex .. " y=" .. movey)
  480.     message("-- move() origin   x=" .. x .. " y=" .. y)
  481.     message("-- move() how much x=" .. aux_x .. " y=" .. aux_y)
  482.  
  483.     if aux_x > 0 then
  484.         turnTo(DIRECTION_EAST)
  485.     elseif aux_x < 0 then
  486.         turnTo(DIRECTION_WEST)
  487.     end
  488.     nforward(math.abs(aux_x))
  489.  
  490.     if aux_y > 0 then
  491.         turnTo(DIRECTION_SOUTH)
  492.     elseif aux_y < 0 then
  493.         turnTo(DIRECTION_NORTH)
  494.     end
  495.     nforward(math.abs(aux_y))
  496.  
  497.     return true
  498. end
  499.  
  500. -------------------------------------------------------------------------------
  501.  
  502. excavate = function()
  503.     local ignore
  504.     state = "mining"
  505.  
  506.     while down() do
  507.         for i = 1 , 4 do
  508.             ignore = checkIgnore()
  509.            
  510.             if not ignore then
  511.                 turtle.dig()
  512.                 checkInventory()   
  513.             end
  514.  
  515.             if i ~= 4 then
  516.                 right()
  517.             end
  518.         end
  519.     end
  520.     while z ~= 0 do
  521.         up()
  522.     end
  523. end
  524.  
  525. -------------------------------------------------------------------------------
  526.  
  527. checkIgnore = function()
  528.     local index, ignored_block
  529.     local success, data = turtle.inspect()
  530.    
  531.     if not success then
  532.         -- message("turtle.inspect() failed. Probably air.")
  533.         return true
  534.     end
  535.    
  536.     if data.name == 'minecraft:lava' then
  537.         turtle.select(INVENTORY_BUCKET)
  538.         turtle.place()
  539.         turtle.refuel()
  540.         message("Lava found and used for fuel.")
  541.         turtle.select(INVENTORY_STORAGE)
  542.         return true
  543.     end
  544.  
  545.     for index, ignored_block in pairs(INVENTORY_IGNORE_MAP) do
  546.         if data.name == ignored_block then
  547.             return true
  548.         end
  549.     end
  550.     -- Do not ignore if past all checks.
  551.     return false
  552. end
  553.  
  554. -------------------------------------------------------------------------------
  555.  
  556. function rawread()
  557.     print("Press enter to continue")
  558.     while true do
  559.         local sEvent, param = os.pullEvent("key")
  560.         if sEvent == "key" then
  561.             if param == 28 then
  562.                 print("Enter detected")
  563.                 break
  564.             end
  565.         end
  566.     end
  567. end
  568.  
  569. -------------------------------------------------------------------------------
  570. -- Main -----------------------------------------------------------------------
  571.  
  572. local function main()
  573.     myChannel = os.getComputerID()
  574.  
  575.     if loadState() then
  576.         message("State loaded. Resuming task.")
  577.         sleep(10)
  578.     else
  579.         message("Slave started. Ready to mine!")
  580.         state = "setting"
  581.     end
  582.  
  583.     while true do
  584.         if state == "setting" then
  585.             message("--> Setting")
  586.             setting()
  587.             state = "waiting"
  588.         elseif state == "waiting" then
  589.             message("--> Waiting")
  590.             getNextSpot()
  591.             state = "moving"
  592.         elseif state == "moving" then
  593.             message("--> Moving")
  594.             move()
  595.             state = "mining"
  596.         elseif state == "mining" then
  597.             -- rawread()
  598.             message("--> Mining")
  599.             excavate()
  600.             state = "waiting"
  601.         else
  602.             message("Invalid state - main() " .. state)
  603.         end
  604.     end
  605. end
  606.  
  607. main()
  608.  
Add Comment
Please, Sign In to add comment