Advertisement
Lion4ever

BombBlokes Turtle API

Dec 14th, 2015
1,218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.25 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, turtleType
  17.     local translate = {[0] = 2, 5, 3, 4}
  18.     local id,entityType
  19.  
  20.     print("\nShould the \"turtle\" be a block, entity or invisible? (b/e/i)")
  21.   do
  22.     local options = {i = "none", e = "entity", b = "block"}
  23.     repeat turtleType = options[read():sub(1, 1):lower()] until turtleType
  24.     end
  25.  
  26.   local hadAI,hadGravity,wasVulnerable,wasCreated
  27.   if turtleType == "entity" then
  28.     commands.exec("scoreboard objectives add turtles dummy")
  29.     local numbID = math.random(9999999)
  30.     id = string.format("@e[score_turtles_min=%d,score_turtles=%d]",numbID,numbID)
  31.     local input,suc,entities
  32.     repeat
  33.       print("Enter an entity type e.g. \"Pig\"\nEnter nothing to create an ArmorStand")
  34.       input = read()
  35.       if input == "" then
  36.         wasCreated = true
  37.         input = "ArmorStand"
  38.         commands.exec('/summon ArmorStand ~ ~ ~ {Invisible:1b,Invulnerable:1b,Equipment:[{},{},{},{},{id:"computercraft:CC-Turtle",Count:1b,Damage:1s}],DisabledSlots:2031616}')
  39.       end
  40.       suc,entities = commands.entitydata("@e[type="..input..",c=1]","{}")
  41.       if #entities == 0 then
  42.         print("No Entity found")
  43.       elseif #entities == 1 and not entities[1]:match("{") then
  44.         print(entities[1])
  45.       elseif #entities > 1 then
  46.         print("Specify! Found ".. #entities .." entities")
  47.       end
  48.     until #entities == 1 and entities[1]:match("{")
  49.     commands.scoreboard("players","set","@e[type="..input..",c=1]","turtles",numbID)
  50.     hadAI = commands.entitydata(id,"{NoAI:1b}")
  51.     hadGravity = commands.entitydata(id,"{NoGravity:1b}")
  52.     wasVulnerable = commands.entitydata(id,"{Invulnerable:1b}")
  53.    
  54.     local px,py,pz = entities[1]:match("Pos:%[0:(.-)d,1:(.-)d,2:(.-)d")
  55.     x = math.floor(tonumber(px))
  56.     y = math.floor(tonumber(py))
  57.     z = math.floor(tonumber(pz))
  58.        
  59.     local rx = tonumber(entities[1]:match("Rotation:%[0:(.-)f,"))
  60.     dir = math.floor(((rx+225)%360)/90)
  61.    
  62.     entityType = entities[1]:match("id:\"(.-)\"") or input
  63.   else  
  64.     print("\nEnter X position:")
  65.     repeat x = tonumber(read()) until x
  66.    
  67.     print("\nEnter Y position:")
  68.     repeat y = tonumber(read()) until y and y > -1 and y < 256
  69.    
  70.     print("\nEnter Z position:")
  71.     repeat z = tonumber(read()) until z
  72.    
  73.     print("\nEnter facing (N/S/E/W):")
  74.     repeat dir = ({n = 0, e = 1, s = 2, w = 3})[read():sub(1, 1):lower()] until dir
  75.   end
  76.    
  77.     if turtleType == "block" then
  78.     commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
  79.   end
  80.    
  81.     if fs.exists("disk\\inventory.txt") then
  82.         local input = fs.open("disk\\inventory.txt","r")
  83.         inventory = textutils.unserialise(input.readAll())
  84.         input.close()
  85.     end
  86.    
  87.     local function pullEvent()
  88.         os.queueEvent("turtle_response", events)
  89.         repeat local myEvent, par1 = os.pullEvent("turtle_response") until par1 == events
  90.         events = events + 1
  91.     end
  92.    
  93.     _G.turtle = {}
  94.    
  95.     turtle.select = function(slot)
  96.         pullEvent()
  97.         if slot < 1 or slot > 16 then error() end
  98.         selected = slot
  99.         return true
  100.     end
  101.    
  102.     turtle.getSelectedSlot = function()
  103.         return selected
  104.     end
  105.    
  106.     turtle.getFuelLevel = function()
  107.         return fuel
  108.     end
  109.    
  110.     turtle.getFuelLimit = function()
  111.         return 1000000000
  112.     end
  113.    
  114.     turtle.refuel = function()
  115.         pullEvent()
  116.         fuel = 1000000000
  117.         return true
  118.     end
  119.    
  120.     turtle.drop = function(amount)
  121.         amount = amount or 64
  122.         if amount < 0 or amount > 64 then error() end
  123.         pullEvent()
  124.         if inventory[selected] then
  125.             inventory[selected].count = inventory[selected].count - amount
  126.             if inventory[selected].count < 1 then inventory[selected] = nil end
  127.             return true
  128.         else return false end
  129.     end
  130.    
  131.     turtle.dropUp, turtle.dropDown = turtle.drop, turtle.drop
  132.    
  133.     turtle.attack = function()
  134.         pullEvent()
  135.         return false
  136.     end
  137.    
  138.     for index, value in ipairs({"attackUp", "attackDown", "suck", "suckUp", "suckDown", "equipLeft", "equipRight"}) do turtle[value] = turtle.attack end
  139.    
  140.     turtle.getItemCount = function(slot)
  141.         slot = slot or selected
  142.         if slot < 1 or slot > 16 then error() end
  143.         return (not inventory[slot]) and 0 or inventory[slot].count
  144.     end
  145.    
  146.     turtle.getItemSpace = function(slot)
  147.         slot = slot or selected
  148.         if slot < 1 or slot > 16 then error() end
  149.         return inventory[slot] and (64 - inventory[slot].count) or 64
  150.     end
  151.    
  152.     turtle.getItemDetail = function(slot)
  153.         slot = slot or selected
  154.         if slot < 1 or slot > 16 then error() end
  155.         return textutils.unserialise(textutils.serialise(inventory[slot]))
  156.     end
  157.    
  158.     turtle.compareTo = function(slot)
  159.         pullEvent()
  160.         slot = slot or selected
  161.         if slot < 1 or slot > 16 then error() end
  162.         if inventory[select] and inventory[slot] then
  163.             return inventory[select].name == inventory[slot].name and inventory[select].damage == inventory[slot].damage
  164.         else return (type(inventory[select]) == "nil" and type(inventory[slot]) == "nil") end
  165.     end
  166.    
  167.     turtle.transferTo = function(slot, amount)
  168.         pullEvent()
  169.         slot = slot or selected
  170.         amount = amount or 64
  171.         if slot < 1 or slot > 16 or amount < 0 or amount > 64 then error() end
  172.         if amount == 0 or not inventory[selected] then return true end
  173.        
  174.         if not inventory[slot] then
  175.             inventory[slot] = textutils.unserialise(textutils.serialise(inventory[selected]))
  176.             inventory[slot].count = 0
  177.         end
  178.        
  179.         if turtle.compareTo(slot) then
  180.             local toMove = math.min(inventory[selected].count, amount, turtle.getItemSpace(slot))
  181.             inventory[selected].count = inventory[selected].count - toMove
  182.             inventory[slot].count = inventory[slot].count + toMove
  183.             if inventory[selected].count < 1 then inventory[selected] = nil end
  184.             return true
  185.         else return false end
  186.     end
  187.    
  188.   local function updatePos(oldX,oldY,oldZ)
  189.     if turtleType == "block" then
  190.       if oldX then
  191.         commands.setblock(oldX.." "..oldY.." "..oldZ.." minecraft:air")
  192.       end
  193.             commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
  194.         elseif turtleType == "entity" then
  195.       local s,et =commands.tp(id,x,wasCreated and y-1 or y,z,dir*90-180,0)
  196.       if not s then
  197.         local upE = 3
  198.         while not getfenv(upE).shell do upE=upE+1 end
  199.         getfenv(upE).shell.exit()
  200.         printError("Lost entity!")
  201.         sleep(1)
  202.         error()
  203.       end
  204.     end
  205.   end
  206.  
  207.     turtle.turnLeft = function()
  208.         pullEvent()
  209.         dir = dir > 0 and (dir - 1) or 3
  210.        
  211.     updatePos()    
  212.         return true
  213.     end
  214.    
  215.     turtle.turnRight = function()
  216.         pullEvent()
  217.         dir = dir < 3 and (dir + 1) or 0
  218.        
  219.         updatePos()
  220.         return true
  221.     end
  222.    
  223.     local function frontBlockPos()
  224.         if dir == 0 then
  225.             return x, y, z - 1
  226.         elseif dir == 1 then
  227.             return x + 1, y, z
  228.         elseif dir == 2 then
  229.             return x, y, z + 1
  230.         else
  231.             return x - 1, y, z
  232.         end
  233.     end
  234.    
  235.     local indetectable = {["minecraft:air"] = true, ["minecraft:water"] = true, ["minecraft:lava"] = true, ["minecraft:flowing_water"] = true, ["minecraft:flowing_lava"] = true}
  236.    
  237.     local function detect(x, y, z)
  238.         if indetectable[commands.getBlockInfo(x, y, z).name] then return false else return true end
  239.     end
  240.    
  241.     turtle.detect = function()
  242.         return detect(frontBlockPos())
  243.     end
  244.    
  245.     turtle.detectUp = function()
  246.         return detect(x, y + 1, z)
  247.     end
  248.    
  249.     turtle.detectDown = function()
  250.         return detect(x, y - 1, z)
  251.     end
  252.    
  253.     turtle.inspect = function()
  254.         return turtle.detect() and commands.getBlockInfo(frontBlockPos()) or false
  255.     end
  256.    
  257.     turtle.inspectUp = function()
  258.         return turtle.detectUp() and commands.getBlockInfo(x, y + 1, z) or false
  259.     end
  260.    
  261.     turtle.inspectDown = function()
  262.         return turtle.detectDown() and commands.getBlockInfo(x, y - 1, z) or false
  263.     end
  264.    
  265.     local function compare(x, y, z)
  266.         local block = commands.getBlockInfo(x, y, z)
  267.         if not inventory[selected] then return block.name == "minecraft:air" end
  268.         return block.name == inventory[selected].name
  269.     end
  270.    
  271.     turtle.compare = function()
  272.         return compare(frontBlockPos())
  273.     end
  274.  
  275.     turtle.compareUp = function()
  276.         return compare(x, y + 1, z)
  277.     end
  278.  
  279.     turtle.compareDown = function()
  280.         return compare(x, y - 1, z)
  281.     end
  282.    
  283.     turtle.forward = function()
  284.         local oldX, oldZ = x, z
  285.        
  286.         if turtle.detect() or fuel == 0 then
  287.             return false
  288.         elseif dir == 0 then
  289.             z = z - 1
  290.         elseif dir == 1 then
  291.             x = x + 1
  292.         elseif dir == 2 then
  293.             z = z + 1
  294.         else
  295.             x = x - 1
  296.         end
  297.        
  298.     updatePos(oldX,y,oldZ)
  299.    
  300.         fuel = fuel - 1
  301.         return true
  302.     end
  303.    
  304.   turtle.back = function()
  305.         local oldX, oldZ = x, z
  306.         local tx,tz = x,z
  307.    
  308.     if dir == 0 then
  309.             tz = tz + 1
  310.         elseif dir == 1 then
  311.             tx = tx - 1
  312.         elseif dir == 2 then
  313.             tz = tz - 1
  314.         else
  315.             tx = tx + 1
  316.         end
  317.    
  318.         if detect(tx,y,tz) or fuel == 0 then
  319.             return false
  320.         end
  321.    
  322.     x,z=tx,tz
  323.     updatePos(oldX,y,oldZ)
  324.    
  325.         fuel = fuel - 1
  326.         return true
  327.     end
  328.  
  329.     --[[turtle.back = function()
  330.         dir = dir + 2
  331.         if dir > 3 then dir = dir - 4 end
  332.        
  333.         local result = turtle.forward()
  334.        
  335.         dir = dir + 2
  336.         if dir > 3 then dir = dir - 4 end
  337.        
  338.         if result then --not nessesary
  339.             --updatePos()
  340.         end
  341.        
  342.         return result
  343.     end]]
  344.        
  345.     turtle.up = function()
  346.         if y == 255 or turtle.detectUp() or fuel == 0 then
  347.             return false
  348.         else
  349.             y = y + 1
  350.             updatePos(x,y-1,z)
  351.       fuel = fuel - 1
  352.             return true
  353.         end
  354.     end
  355.    
  356.     turtle.down = function()
  357.         if y == 0 or turtle.detectDown() or fuel == 0 then
  358.             return false
  359.         else
  360.             y = y - 1
  361.             updatePos(x,y+1,z)
  362.       fuel = fuel - 1
  363.             return true
  364.         end
  365.     end
  366.    
  367.     local function place(x, y, z)
  368.         if detect(x, y, z) or not inventory[selected] then return false end
  369.        
  370.         local result = commands.setblock(x, y, z, inventory[selected].name, inventory[selected].damage)
  371.        
  372.         if result then
  373.             inventory[selected].count = inventory[selected].count - 1
  374.             if inventory[selected].count == 0 then inventory[selected] = nil end
  375.         end
  376.        
  377.         return result
  378.     end
  379.    
  380.     turtle.place = function()
  381.         return place(frontBlockPos())
  382.     end
  383.    
  384.     turtle.placeUp = function()
  385.         return place(x, y + 1, z)
  386.     end
  387.    
  388.     turtle.placeDown = function()
  389.         return place(x, y - 1, z)
  390.     end
  391.    
  392.     local function dig(x, y, z)
  393.         if detect(x, y, z) then
  394.             local block = commands.getBlockInfo(x, y, z)
  395.             if block.name == "minecraft:bedrock" then return false end
  396.             commands.setblock(x, y, z, "minecraft:air", 0)
  397.            
  398.             if not inventory[selected] then
  399.                 inventory[selected] = {["name"] = block.name, ["damage"] = 0, ["count"] = 1}
  400.             else for i = 1, 16 do
  401.                 if not inventory[i] then
  402.                     inventory[i] = {["name"] = block.name, ["damage"] = 0, ["count"] = 1}
  403.                     break
  404.                 elseif inventory[i].name == block.name and inventory[i].count < 64 then
  405.                     inventory[i].count = inventory[i].count + 1
  406.                     break
  407.                 end
  408.             end end
  409.            
  410.             return true
  411.         else return false end
  412.     end
  413.    
  414.     turtle.dig = function()
  415.         return dig(frontBlockPos())
  416.     end
  417.    
  418.     turtle.digUp = function()
  419.         return dig(x, y + 1, z)
  420.     end
  421.    
  422.     turtle.digDown = function()
  423.         return dig(x, y - 1, z)
  424.     end
  425.    
  426.     local native = {}
  427.     for key, value in pairs(turtle) do native[key] = value end
  428.     turtle.native = native
  429.    
  430.     local oldgetBlockPosition = commands.getBlockPosition
  431.    
  432.     commands.getBlockPosition = function()
  433.         pullEvent()
  434.         return x, y, z
  435.     end
  436.    
  437.     if turtleType == "block" then
  438.         local oldReboot, oldShutdown = os.reboot, os.shutdown
  439.        
  440.         os.reboot = function()
  441.             commands.setblock(x.." "..y.." "..z.." minecraft:air")
  442.             oldReboot()
  443.         end
  444.        
  445.         os.shutdown = function()
  446.             commands.setblock(x.." "..y.." "..z.." minecraft:air")
  447.             oldShutdown()
  448.         end
  449.     end
  450.    
  451.     term.setBackgroundColour(colours.brown)
  452.     term.clear()
  453.    
  454.     local oldTerm = term.current()
  455.    
  456.     local myWindow = window.create(oldTerm, 1, 1, 39, 13)
  457.     term.redirect(myWindow)
  458.    
  459.     print("\nI am now the PRETTIEST TURTLE!!!!\n")
  460.   if turtleType == "entity" then
  461.     print(string.format("You are the most beautiful %s near %d %d %d\n",entityType or "being",x,y,z))
  462.     end
  463.  
  464.   local path = shell.path()
  465.   shell.setPath(path..":rom/programs/turtle")
  466.  
  467.   pcall(shell.run,"shell")
  468.    
  469.   shell.setPath(path)
  470.     if turtleType == "entity" then
  471.     if hadAI then
  472.       commands.entitydata(id,"{NoAI:0b}")
  473.     end
  474.     if hadGravity then
  475.       commands.entitydata(id,"{NoGravity:0b}")
  476.     end
  477.     if wasVulnerable then
  478.       commands.entitydata(id,"{Invulnerable:0b}")
  479.     end
  480.     commands.scoreboard("players","reset",id,"turtles")
  481.     if wasCreated then
  482.       commands.kill(string.format("@e[x=%d,y=%d,z=%d,c=1]",x,y,z))
  483.     end
  484.   end
  485.  
  486.   _G.turtle = nil
  487.     commands.getBlockPosition = oldgetBlockPosition
  488.    
  489.     if turtleType == "block" then commands.setblock(x.." "..y.." "..z.." minecraft:air") end
  490.    
  491.     term.redirect(oldTerm)
  492.     term.setTextColour(colours.white)
  493.     term.setBackgroundColour(colours.black)
  494.     term.setCursorPos(1, 1)
  495.     term.clear()
  496. else error("Run me on either a turtle or a command computer.") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement