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, turtleType
- local translate = {[0] = 2, 5, 3, 4}
- local id,entityType
- print("\nShould the \"turtle\" be a block, entity or invisible? (b/e/i)")
- do
- local options = {i = "none", e = "entity", b = "block"}
- repeat turtleType = options[read():sub(1, 1):lower()] until turtleType
- end
- local hadAI,hadGravity,wasVulnerable,wasCreated
- if turtleType == "entity" then
- commands.exec("scoreboard objectives add turtles dummy")
- local numbID = math.random(9999999)
- id = string.format("@e[score_turtles_min=%d,score_turtles=%d]",numbID,numbID)
- local input,suc,entities
- repeat
- print("Enter an entity type e.g. \"Pig\"\nEnter nothing to create an ArmorStand")
- input = read()
- if input == "" then
- wasCreated = true
- input = "ArmorStand"
- commands.exec('/summon ArmorStand ~ ~ ~ {Invisible:1b,Invulnerable:1b,Equipment:[{},{},{},{},{id:"computercraft:CC-Turtle",Count:1b,Damage:1s}],DisabledSlots:2031616}')
- end
- suc,entities = commands.entitydata("@e[type="..input..",c=1]","{}")
- if #entities == 0 then
- print("No Entity found")
- elseif #entities == 1 and not entities[1]:match("{") then
- print(entities[1])
- elseif #entities > 1 then
- print("Specify! Found ".. #entities .." entities")
- end
- until #entities == 1 and entities[1]:match("{")
- commands.scoreboard("players","set","@e[type="..input..",c=1]","turtles",numbID)
- hadAI = commands.entitydata(id,"{NoAI:1b}")
- hadGravity = commands.entitydata(id,"{NoGravity:1b}")
- wasVulnerable = commands.entitydata(id,"{Invulnerable:1b}")
- local px,py,pz = entities[1]:match("Pos:%[0:(.-)d,1:(.-)d,2:(.-)d")
- x = math.floor(tonumber(px))
- y = math.floor(tonumber(py))
- z = math.floor(tonumber(pz))
- local rx = tonumber(entities[1]:match("Rotation:%[0:(.-)f,"))
- dir = math.floor(((rx+225)%360)/90)
- entityType = entities[1]:match("id:\"(.-)\"") or input
- else
- 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
- end
- if turtleType == "block" 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
- local function updatePos(oldX,oldY,oldZ)
- if turtleType == "block" then
- if oldX then
- commands.setblock(oldX.." "..oldY.." "..oldZ.." minecraft:air")
- end
- commands.setblock(x.." "..y.." "..z.." ComputerCraft:command_computer "..translate[dir])
- elseif turtleType == "entity" then
- local s,et =commands.tp(id,x,wasCreated and y-1 or y,z,dir*90-180,0)
- if not s then
- local upE = 3
- while not getfenv(upE).shell do upE=upE+1 end
- getfenv(upE).shell.exit()
- printError("Lost entity!")
- sleep(1)
- error()
- end
- end
- end
- turtle.turnLeft = function()
- pullEvent()
- dir = dir > 0 and (dir - 1) or 3
- updatePos()
- return true
- end
- turtle.turnRight = function()
- pullEvent()
- dir = dir < 3 and (dir + 1) or 0
- updatePos()
- 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
- updatePos(oldX,y,oldZ)
- fuel = fuel - 1
- return true
- end
- turtle.back = function()
- local oldX, oldZ = x, z
- local tx,tz = x,z
- if dir == 0 then
- tz = tz + 1
- elseif dir == 1 then
- tx = tx - 1
- elseif dir == 2 then
- tz = tz - 1
- else
- tx = tx + 1
- end
- if detect(tx,y,tz) or fuel == 0 then
- return false
- end
- x,z=tx,tz
- updatePos(oldX,y,oldZ)
- 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 result then --not nessesary
- --updatePos()
- end
- return result
- end]]
- turtle.up = function()
- if y == 255 or turtle.detectUp() or fuel == 0 then
- return false
- else
- y = y + 1
- updatePos(x,y-1,z)
- fuel = fuel - 1
- return true
- end
- end
- turtle.down = function()
- if y == 0 or turtle.detectDown() or fuel == 0 then
- return false
- else
- y = y - 1
- updatePos(x,y+1,z)
- 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 turtleType == "block" 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")
- if turtleType == "entity" then
- print(string.format("You are the most beautiful %s near %d %d %d\n",entityType or "being",x,y,z))
- end
- local path = shell.path()
- shell.setPath(path..":rom/programs/turtle")
- pcall(shell.run,"shell")
- shell.setPath(path)
- if turtleType == "entity" then
- if hadAI then
- commands.entitydata(id,"{NoAI:0b}")
- end
- if hadGravity then
- commands.entitydata(id,"{NoGravity:0b}")
- end
- if wasVulnerable then
- commands.entitydata(id,"{Invulnerable:0b}")
- end
- commands.scoreboard("players","reset",id,"turtles")
- if wasCreated then
- commands.kill(string.format("@e[x=%d,y=%d,z=%d,c=1]",x,y,z))
- end
- end
- _G.turtle = nil
- commands.getBlockPosition = oldgetBlockPosition
- if turtleType == "block" 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