Advertisement
BombBloke

Command Computer's Turtle API (ComputerCraft)

Mar 21st, 2015
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.37 KB | None | 0 0
  1. -- http://www.computercraft.info/forums2/index.php?/topic/22479-command-computers-turtle-api/
  2.  
  3. if turtle and not commands then
  4.     if not fs.isDir("disk") then error("A disk drive with an inserted floppy must be available.") end
  5.    
  6.     local inventory = {}
  7.     for i = 1, 16 do inventory[i] = turtle.getItemDetail(i) end
  8.    
  9.     local output = fs.open("disk\\inventory.txt","w")
  10.     output.writeLine(textutils.serialise(inventory))
  11.     output.close()
  12.    
  13.     print("Inventory content documented to disk.")
  14. elseif commands then
  15.     local inventory, selected, events, fuel = {}, 1, 1, 1000000000
  16.     local x, y, z, dir, visible
  17.     local translate = {[0] = 2, 5, 3, 4}
  18.    
  19.     print("\nEnter X position:")
  20.     repeat x = tonumber(read()) until x
  21.    
  22.     print("\nEnter Y position:")
  23.     repeat y = tonumber(read()) until y and y > -1 and y < 256
  24.    
  25.     print("\nEnter Z position:")
  26.     repeat z = tonumber(read()) until z
  27.    
  28.     print("\nEnter facing (N/S/E/W):")
  29.     repeat dir = ({n = 0, e = 1, s = 2, w = 3})[read():sub(1, 1):lower()] until dir
  30.    
  31.     print("\nVisible \"turtle\"? (y/n)")
  32.     visible = read():sub(1, 1):lower() == "y"
  33.    
  34.     if visible then commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir]) end
  35.    
  36.     if fs.exists("disk\\inventory.txt") then
  37.         local input = fs.open("disk\\inventory.txt","r")
  38.         inventory = textutils.unserialise(input.readAll())
  39.         input.close()
  40.     end
  41.    
  42.     local function pullEvent()
  43.         os.queueEvent("turtle_response", events)
  44.         repeat local myEvent, par1 = os.pullEvent("turtle_response") until par1 == events
  45.         events = events + 1
  46.     end
  47.    
  48.     _G.turtle = {}
  49.    
  50.     turtle.select = function(slot)
  51.         pullEvent()
  52.         if slot < 1 or slot > 16 then error() end
  53.         selected = slot
  54.         return true
  55.     end
  56.    
  57.     turtle.getSelectedSlot = function()
  58.         return selected
  59.     end
  60.    
  61.     turtle.getFuelLevel = function()
  62.         return fuel
  63.     end
  64.    
  65.     turtle.getFuelLimit = function()
  66.         return 1000000000
  67.     end
  68.    
  69.     turtle.refuel = function()
  70.         pullEvent()
  71.         fuel = 1000000000
  72.         return true
  73.     end
  74.    
  75.     turtle.drop = function(amount)
  76.         amount = amount or 64
  77.         if amount < 0 or amount > 64 then error() end
  78.         pullEvent()
  79.         if inventory[selected] then
  80.             inventory[selected].count = inventory[selected].count - amount
  81.             if inventory[selected].count < 1 then inventory[selected] = nil end
  82.             return true
  83.         else return false end
  84.     end
  85.    
  86.     turtle.dropUp, turtle.dropDown = turtle.drop, turtle.drop
  87.    
  88.     turtle.attack = function()
  89.         pullEvent()
  90.         return false
  91.     end
  92.    
  93.     for index, value in ipairs({"attackUp", "attackDown", "suck", "suckUp", "suckDown", "equipLeft", "equipRight"}) do turtle[value] = turtle.attack end
  94.    
  95.     turtle.getItemCount = function(slot)
  96.         slot = slot or selected
  97.         if slot < 1 or slot > 16 then error() end
  98.         return (not inventory[slot]) and 0 or inventory[slot].count
  99.     end
  100.    
  101.     turtle.getItemSpace = function(slot)
  102.         slot = slot or selected
  103.         if slot < 1 or slot > 16 then error() end
  104.         return inventory[slot] and (64 - inventory[slot].count) or 64
  105.     end
  106.    
  107.     turtle.getItemDetail = function(slot)
  108.         slot = slot or selected
  109.         if slot < 1 or slot > 16 then error() end
  110.         return textutils.unserialise(textutils.serialise(inventory[slot]))
  111.     end
  112.    
  113.     turtle.compareTo = function(slot)
  114.         pullEvent()
  115.         slot = slot or selected
  116.         if slot < 1 or slot > 16 then error() end
  117.         if inventory[select] and inventory[slot] then
  118.             return inventory[select].name == inventory[slot].name and inventory[select].damage == inventory[slot].damage
  119.         else return (type(inventory[select]) == "nil" and type(inventory[slot]) == "nil") end
  120.     end
  121.    
  122.     turtle.transferTo = function(slot, amount)
  123.         pullEvent()
  124.         slot = slot or selected
  125.         amount = amount or 64
  126.         if slot < 1 or slot > 16 or amount < 0 or amount > 64 then error() end
  127.         if amount == 0 or not inventory[selected] then return true end
  128.        
  129.         if not inventory[slot] then
  130.             inventory[slot] = textutils.unserialise(textutils.serialise(inventory[selected]))
  131.             inventory[slot].count = 0
  132.         end
  133.        
  134.         if turtle.compareTo(slot) then
  135.             local toMove = math.min(inventory[selected].count, amount, turtle.getItemSpace(slot))
  136.             inventory[selected].count = inventory[selected].count - toMove
  137.             inventory[slot].count = inventory[slot].count + toMove
  138.             if inventory[selected].count < 1 then inventory[selected] = nil end
  139.             return true
  140.         else return false end
  141.     end
  142.    
  143.     turtle.turnLeft = function()
  144.         pullEvent()
  145.         dir = dir > 0 and (dir - 1) or 3
  146.        
  147.         if visible then
  148.             commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
  149.         end
  150.        
  151.         return true
  152.     end
  153.    
  154.     turtle.turnRight = function()
  155.         pullEvent()
  156.         dir = dir < 3 and (dir + 1) or 0
  157.        
  158.         if visible then
  159.             commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
  160.         end
  161.        
  162.         return true
  163.     end
  164.    
  165.     local function frontBlockPos()
  166.         if dir == 0 then
  167.             return x, y, z - 1
  168.         elseif dir == 1 then
  169.             return x + 1, y, z
  170.         elseif dir == 2 then
  171.             return x, y, z + 1
  172.         else
  173.             return x - 1, y, z
  174.         end
  175.     end
  176.    
  177.     local indetectable = {["minecraft:air"] = true, ["minecraft:water"] = true, ["minecraft:lava"] = true, ["minecraft:flowing_water"] = true, ["minecraft:flowing_lava"] = true}
  178.    
  179.     local function detect(x, y, z)
  180.         if indetectable[commands.getBlockInfo(x, y, z).name] then return false else return true end
  181.     end
  182.    
  183.     turtle.detect = function()
  184.         return detect(frontBlockPos())
  185.     end
  186.    
  187.     turtle.detectUp = function()
  188.         return detect(x, y + 1, z)
  189.     end
  190.    
  191.     turtle.detectDown = function()
  192.         return detect(x, y - 1, z)
  193.     end
  194.    
  195.     turtle.inspect = function()
  196.         return turtle.detect() and commands.getBlockInfo(frontBlockPos()) or false
  197.     end
  198.    
  199.     turtle.inspectUp = function()
  200.         return turtle.detectUp and commands.getBlockInfo(x, y + 1, z) or false
  201.     end
  202.    
  203.     turtle.inspectDown = function()
  204.         return turtle.detectDown and commands.getBlockInfo(x, y - 1, z) or false
  205.     end
  206.    
  207.     local function compare(x, y, z)
  208.         local block = commands.getBlockInfo(x, y, z)
  209.         if not inventory[selected] then return block.name == "minecraft:air" end
  210.         return block.name == inventory[selected].name
  211.     end
  212.    
  213.     turtle.compare = function()
  214.         return compare(frontBlockPos())
  215.     end
  216.  
  217.     turtle.compareUp = function()
  218.         return compare(x, y + 1, z)
  219.     end
  220.  
  221.     turtle.compareDown = function()
  222.         return compare(x, y - 1, z)
  223.     end
  224.    
  225.     turtle.forward = function()
  226.         local oldX, oldZ = x, z
  227.        
  228.         if turtle.detect() or fuel == 0 then
  229.             return false
  230.         elseif dir == 0 then
  231.             z = z - 1
  232.         elseif dir == 1 then
  233.             x = x + 1
  234.         elseif dir == 2 then
  235.             z = z + 1
  236.         else
  237.             x = x - 1
  238.         end
  239.        
  240.         if visible then
  241.             commands.setblock(oldX.." "..y.." "..oldZ.." minecraft:air")
  242.             commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
  243.         end
  244.        
  245.         fuel = fuel - 1
  246.         return true
  247.     end
  248.    
  249.     turtle.back = function()
  250.         dir = dir + 2
  251.         if dir > 3 then dir = dir - 4 end
  252.        
  253.         local result = turtle.forward()
  254.        
  255.         dir = dir + 2
  256.         if dir > 3 then dir = dir - 4 end
  257.        
  258.         if visible and result then
  259.             commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
  260.         end
  261.        
  262.         return result
  263.     end
  264.        
  265.     turtle.up = function()
  266.         if turtle.detectUp() or y == 255 or fuel == 0 then
  267.             return false
  268.         else
  269.             if visible then
  270.                 commands.setblock(x.." "..y.." "..z.." minecraft:air")
  271.                 commands.setblock(x.." "..(y+1).." "..z.." ComputerCraft:command_computer "..translate[dir])
  272.             end
  273.            
  274.             y = y + 1
  275.             fuel = fuel - 1
  276.             return true
  277.         end
  278.     end
  279.    
  280.     turtle.down = function()
  281.         if turtle.detectDown() or y == 0 or fuel == 0 then
  282.             return false
  283.         else
  284.             if visible then
  285.                 commands.setblock(x.." "..y.." "..z.." minecraft:air")
  286.                 commands.setblock(x.." "..(y-1).." "..z.." ComputerCraft:command_computer "..translate[dir])
  287.             end
  288.            
  289.             y = y - 1
  290.             fuel = fuel - 1
  291.             return true
  292.         end
  293.     end
  294.    
  295.     local function place(x, y, z)
  296.         if detect(x, y, z) or not inventory[selected] then return false end
  297.        
  298.         local result = commands.setblock(x, y, z, inventory[selected].name, inventory[selected].damage)
  299.        
  300.         if result then
  301.             inventory[selected].count = inventory[selected].count - 1
  302.             if inventory[selected].count == 0 then inventory[selected] = nil end
  303.         end
  304.        
  305.         return result
  306.     end
  307.    
  308.     turtle.place = function()
  309.         return place(frontBlockPos())
  310.     end
  311.    
  312.     turtle.placeUp = function()
  313.         return place(x, y + 1, z)
  314.     end
  315.    
  316.     turtle.placeDown = function()
  317.         return place(x, y - 1, z)
  318.     end
  319.    
  320.     local function dig(x, y, z)
  321.         if detect(x, y, z) then
  322.             local block = commands.getBlockInfo(x, y, z)
  323.             if block.name == "minecraft:bedrock" then return false end
  324.             commands.setblock(x, y, z, "minecraft:air", 0)
  325.            
  326.             if not inventory[selected] then
  327.                 inventory[selected] = {["name"] = block.name, ["damage"] = 0, ["count"] = 1}
  328.             else for i = 1, 16 do
  329.                 if not inventory[i] then
  330.                     inventory[i] = {["name"] = block.name, ["damage"] = 0, ["count"] = 1}
  331.                     break
  332.                 elseif inventory[i].name == block.name and inventory[i].count < 64 then
  333.                     inventory[i].count = inventory[i].count + 1
  334.                     break
  335.                 end
  336.             end end
  337.            
  338.             return true
  339.         else return false end
  340.     end
  341.    
  342.     turtle.dig = function()
  343.         return dig(frontBlockPos())
  344.     end
  345.    
  346.     turtle.digUp = function()
  347.         return dig(x, y + 1, z)
  348.     end
  349.    
  350.     turtle.digDown = function()
  351.         return dig(x, y - 1, z)
  352.     end
  353.    
  354.     local native = {}
  355.     for key, value in pairs(turtle) do native[key] = value end
  356.     turtle.native = native
  357.    
  358.     local oldgetBlockPosition = commands.getBlockPosition
  359.    
  360.     commands.getBlockPosition = function()
  361.         pullEvent()
  362.         return x, y, z
  363.     end
  364.    
  365.     if visible then
  366.         local oldReboot, oldShutdown = os.reboot, os.shutdown
  367.        
  368.         os.reboot = function()
  369.             commands.setblock(x.." "..y.." "..z.." minecraft:air")
  370.             oldReboot()
  371.         end
  372.        
  373.         os.shutdown = function()
  374.             commands.setblock(x.." "..y.." "..z.." minecraft:air")
  375.             oldShutdown()
  376.         end
  377.     end
  378.    
  379.     term.setBackgroundColour(colours.brown)
  380.     term.clear()
  381.    
  382.     local oldTerm = term.current()
  383.    
  384.     local myWindow = window.create(oldTerm, 1, 1, 39, 13)
  385.     term.redirect(myWindow)
  386.    
  387.     print("\nI am now the PRETTIEST TURTLE!!!!\n")
  388.     shell.run("shell")
  389.    
  390.     _G.turtle = nil
  391.     commands.getBlockPosition = oldgetBlockPosition
  392.    
  393.     if visible then commands.setblock(x.." "..y.." "..z.." minecraft:air") end
  394.    
  395.     term.redirect(oldTerm)
  396.     term.setTextColour(colours.white)
  397.     term.setBackgroundColour(colours.black)
  398.     term.setCursorPos(1, 1)
  399.     term.clear()
  400. else error("Run me on either a turtle or a command computer.") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement