Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- http://www.computercraft.info/forums2/index.php?/topic/22479-command-computers-turtle-api/
- if turtle and not commands then
- if not fs.isDir("disk") then error("A disk drive with an inserted floppy must be available.") end
- local inventory = {}
- for i = 1, 16 do inventory[i] = turtle.getItemDetail(i) end
- local output = fs.open("disk\\inventory.txt","w")
- output.writeLine(textutils.serialise(inventory))
- output.close()
- print("Inventory content documented to disk.")
- elseif commands then
- local inventory, selected, events, fuel = {}, 1, 1, 1000000000
- local x, y, z, dir, visible
- local translate = {[0] = 2, 5, 3, 4}
- print("\nEnter X position:")
- repeat x = tonumber(read()) until x
- print("\nEnter Y position:")
- repeat y = tonumber(read()) until y and y > -1 and y < 256
- print("\nEnter Z position:")
- repeat z = tonumber(read()) until z
- print("\nEnter facing (N/S/E/W):")
- repeat dir = ({n = 0, e = 1, s = 2, w = 3})[read():sub(1, 1):lower()] until dir
- print("\nVisible \"turtle\"? (y/n)")
- visible = read():sub(1, 1):lower() == "y"
- if visible then commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir]) end
- if fs.exists("disk\\inventory.txt") then
- local input = fs.open("disk\\inventory.txt","r")
- inventory = textutils.unserialise(input.readAll())
- input.close()
- end
- local function pullEvent()
- os.queueEvent("turtle_response", events)
- repeat local myEvent, par1 = os.pullEvent("turtle_response") until par1 == events
- events = events + 1
- end
- _G.turtle = {}
- turtle.select = function(slot)
- pullEvent()
- if slot < 1 or slot > 16 then error() end
- selected = slot
- return true
- end
- turtle.getSelectedSlot = function()
- return selected
- end
- turtle.getFuelLevel = function()
- return fuel
- end
- turtle.getFuelLimit = function()
- return 1000000000
- end
- turtle.refuel = function()
- pullEvent()
- fuel = 1000000000
- return true
- end
- turtle.drop = function(amount)
- amount = amount or 64
- if amount < 0 or amount > 64 then error() end
- pullEvent()
- if inventory[selected] then
- inventory[selected].count = inventory[selected].count - amount
- if inventory[selected].count < 1 then inventory[selected] = nil end
- return true
- else return false end
- end
- turtle.dropUp, turtle.dropDown = turtle.drop, turtle.drop
- turtle.attack = function()
- pullEvent()
- return false
- end
- for index, value in ipairs({"attackUp", "attackDown", "suck", "suckUp", "suckDown", "equipLeft", "equipRight"}) do turtle[value] = turtle.attack end
- turtle.getItemCount = function(slot)
- slot = slot or selected
- if slot < 1 or slot > 16 then error() end
- return (not inventory[slot]) and 0 or inventory[slot].count
- end
- turtle.getItemSpace = function(slot)
- slot = slot or selected
- if slot < 1 or slot > 16 then error() end
- return inventory[slot] and (64 - inventory[slot].count) or 64
- end
- turtle.getItemDetail = function(slot)
- slot = slot or selected
- if slot < 1 or slot > 16 then error() end
- return textutils.unserialise(textutils.serialise(inventory[slot]))
- end
- turtle.compareTo = function(slot)
- pullEvent()
- slot = slot or selected
- if slot < 1 or slot > 16 then error() end
- if inventory[select] and inventory[slot] then
- return inventory[select].name == inventory[slot].name and inventory[select].damage == inventory[slot].damage
- else return (type(inventory[select]) == "nil" and type(inventory[slot]) == "nil") end
- end
- turtle.transferTo = function(slot, amount)
- pullEvent()
- slot = slot or selected
- amount = amount or 64
- if slot < 1 or slot > 16 or amount < 0 or amount > 64 then error() end
- if amount == 0 or not inventory[selected] then return true end
- if not inventory[slot] then
- inventory[slot] = textutils.unserialise(textutils.serialise(inventory[selected]))
- inventory[slot].count = 0
- end
- if turtle.compareTo(slot) then
- local toMove = math.min(inventory[selected].count, amount, turtle.getItemSpace(slot))
- inventory[selected].count = inventory[selected].count - toMove
- inventory[slot].count = inventory[slot].count + toMove
- if inventory[selected].count < 1 then inventory[selected] = nil end
- return true
- else return false end
- end
- turtle.turnLeft = function()
- pullEvent()
- dir = dir > 0 and (dir - 1) or 3
- if visible then
- commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
- end
- return true
- end
- turtle.turnRight = function()
- pullEvent()
- dir = dir < 3 and (dir + 1) or 0
- if visible then
- commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
- end
- return true
- end
- local function frontBlockPos()
- if dir == 0 then
- return x, y, z - 1
- elseif dir == 1 then
- return x + 1, y, z
- elseif dir == 2 then
- return x, y, z + 1
- else
- return x - 1, y, z
- end
- end
- local indetectable = {["minecraft:air"] = true, ["minecraft:water"] = true, ["minecraft:lava"] = true, ["minecraft:flowing_water"] = true, ["minecraft:flowing_lava"] = true}
- local function detect(x, y, z)
- if indetectable[commands.getBlockInfo(x, y, z).name] then return false else return true end
- end
- turtle.detect = function()
- return detect(frontBlockPos())
- end
- turtle.detectUp = function()
- return detect(x, y + 1, z)
- end
- turtle.detectDown = function()
- return detect(x, y - 1, z)
- end
- turtle.inspect = function()
- return turtle.detect() and commands.getBlockInfo(frontBlockPos()) or false
- end
- turtle.inspectUp = function()
- return turtle.detectUp and commands.getBlockInfo(x, y + 1, z) or false
- end
- turtle.inspectDown = function()
- return turtle.detectDown and commands.getBlockInfo(x, y - 1, z) or false
- end
- local function compare(x, y, z)
- local block = commands.getBlockInfo(x, y, z)
- if not inventory[selected] then return block.name == "minecraft:air" end
- return block.name == inventory[selected].name
- end
- turtle.compare = function()
- return compare(frontBlockPos())
- end
- turtle.compareUp = function()
- return compare(x, y + 1, z)
- end
- turtle.compareDown = function()
- return compare(x, y - 1, z)
- end
- turtle.forward = function()
- local oldX, oldZ = x, z
- if turtle.detect() or fuel == 0 then
- return false
- elseif dir == 0 then
- z = z - 1
- elseif dir == 1 then
- x = x + 1
- elseif dir == 2 then
- z = z + 1
- else
- x = x - 1
- end
- if visible then
- commands.setblock(oldX.." "..y.." "..oldZ.." minecraft:air")
- commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
- end
- fuel = fuel - 1
- return true
- end
- turtle.back = function()
- dir = dir + 2
- if dir > 3 then dir = dir - 4 end
- local result = turtle.forward()
- dir = dir + 2
- if dir > 3 then dir = dir - 4 end
- if visible and result then
- commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
- end
- return result
- end
- turtle.up = function()
- if turtle.detectUp() or y == 255 or fuel == 0 then
- return false
- else
- if visible then
- commands.setblock(x.." "..y.." "..z.." minecraft:air")
- commands.setblock(x.." "..(y+1).." "..z.." ComputerCraft:command_computer "..translate[dir])
- end
- y = y + 1
- fuel = fuel - 1
- return true
- end
- end
- turtle.down = function()
- if turtle.detectDown() or y == 0 or fuel == 0 then
- return false
- else
- if visible then
- commands.setblock(x.." "..y.." "..z.." minecraft:air")
- commands.setblock(x.." "..(y-1).." "..z.." ComputerCraft:command_computer "..translate[dir])
- end
- y = y - 1
- fuel = fuel - 1
- return true
- end
- end
- local function place(x, y, z)
- if detect(x, y, z) or not inventory[selected] then return false end
- local result = commands.setblock(x, y, z, inventory[selected].name, inventory[selected].damage)
- if result then
- inventory[selected].count = inventory[selected].count - 1
- if inventory[selected].count == 0 then inventory[selected] = nil end
- end
- return result
- end
- turtle.place = function()
- return place(frontBlockPos())
- end
- turtle.placeUp = function()
- return place(x, y + 1, z)
- end
- turtle.placeDown = function()
- return place(x, y - 1, z)
- end
- local function dig(x, y, z)
- if detect(x, y, z) then
- local block = commands.getBlockInfo(x, y, z)
- if block.name == "minecraft:bedrock" then return false end
- commands.setblock(x, y, z, "minecraft:air", 0)
- if not inventory[selected] then
- inventory[selected] = {["name"] = block.name, ["damage"] = 0, ["count"] = 1}
- else for i = 1, 16 do
- if not inventory[i] then
- inventory[i] = {["name"] = block.name, ["damage"] = 0, ["count"] = 1}
- break
- elseif inventory[i].name == block.name and inventory[i].count < 64 then
- inventory[i].count = inventory[i].count + 1
- break
- end
- end end
- return true
- else return false end
- end
- turtle.dig = function()
- return dig(frontBlockPos())
- end
- turtle.digUp = function()
- return dig(x, y + 1, z)
- end
- turtle.digDown = function()
- return dig(x, y - 1, z)
- end
- local native = {}
- for key, value in pairs(turtle) do native[key] = value end
- turtle.native = native
- local oldgetBlockPosition = commands.getBlockPosition
- commands.getBlockPosition = function()
- pullEvent()
- return x, y, z
- end
- if visible then
- local oldReboot, oldShutdown = os.reboot, os.shutdown
- os.reboot = function()
- commands.setblock(x.." "..y.." "..z.." minecraft:air")
- oldReboot()
- end
- os.shutdown = function()
- commands.setblock(x.." "..y.." "..z.." minecraft:air")
- oldShutdown()
- end
- end
- term.setBackgroundColour(colours.brown)
- term.clear()
- local oldTerm = term.current()
- local myWindow = window.create(oldTerm, 1, 1, 39, 13)
- term.redirect(myWindow)
- print("\nI am now the PRETTIEST TURTLE!!!!\n")
- shell.run("shell")
- _G.turtle = nil
- commands.getBlockPosition = oldgetBlockPosition
- if visible then commands.setblock(x.." "..y.." "..z.." minecraft:air") end
- term.redirect(oldTerm)
- term.setTextColour(colours.white)
- term.setBackgroundColour(colours.black)
- term.setCursorPos(1, 1)
- term.clear()
- else error("Run me on either a turtle or a command computer.") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement