Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Name: Turtle Complete
- Author: Chris K (Coaster3000)
- Creation Date: 6/4/13
- Updated Date: 6/9/13
- Version: 0.1
- Flavor: API Replacement / wrapper
- COPYRIGHT NOTICE
- © Chris K 2013, Some Rights Reserved
- Except where otherwise noted, this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0
- You are free:
- * to Share - to copy, distribute and transmit the work in accordance that is stays under non profit distribution
- * to Remix - to adapt the work
- Under the following conditions:
- * Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
- * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
- * Non-Profit. You must distribute it in a non-profit way.
- * For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
- * Any of the above conditions can be waived if you get permission from the copyright holder.
- * Nothing in this license impairs or restricts the author's moral rights.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/.
- ]]
- local nativeassert = assert
- local function assert(condition, msg, level) level = (level or 1) + 1 if not condition then error(msg, level) end return condition end -- Custom assert function. Based of theOriginalBit's assert function link: http://theoriginalbit.net46.net/index.html
- local function yieldme()
- os.queueEvent("nothing")
- coroutine.yield()
- end
- if not turtle then
- error( "Cannot load turtle API on computer" )
- end
- native = turtle.native or turtle
- local function wrap( _sCommand )
- return function( ... )
- local id = native[_sCommand]( ... )
- local event, responseID, success
- while event ~= "turtle_response" or responseID ~= id do
- event, responseID, success = os.pullEvent( "turtle_response")
- end
- return success
- end
- end
- local turtle = {}
- turtle["getItemCount"] = native.getItemCount
- turtle["getItemSpace"] = native.getItemSpace
- for k,v in pairs( native ) do
- if type (k) == "string" and type(v) == "function" then
- if turtle[k] == nil then
- turtle[k] = wrap(k)
- end
- end
- end
- local env = getfenv()
- for k,v in pairs( turtle ) do
- env[k] = v
- end
- -- End Globalization --
- ----Data Handling----
- ------------------
- ---- Tracking ----
- -- Position data
- local position = {}
- position.x = 0
- position.y = 0
- position.z = 0
- position.dir = 0
- local function cust_fmod(num, div)
- local ret = math.fmod(num, div)
- if ret < 0 then
- ret = (div - 1) + ret
- end
- return ret
- end
- function getX()
- local ret = position.x
- return ret
- end
- function getY()
- local ret = position.y
- return ret
- end
- function getZ()
- local ret = position.z
- return ret
- end
- -- End position data
- local slot = 1
- turtle.select(1)
- function select(_slot)
- local good = turtle.select(_slot)
- if good then
- slot = _slot
- end
- return good
- end
- function getSlot()
- local s = slot
- return s
- end
- -- Breakers
- local maxAttempts = 500
- function getMaxAttempts() local a = maxAttempts return a end -- It is written this way to make sure variable is not publicized.
- function setMaxAttempts(num)
- num = num or maxAttempts
- maxAttempts = num
- end
- -- End Breakers
- ------------------
- ---- Fuel --------
- function hasFuel( ammount )
- return turtle.getFuelLeve() >= (ammount or 1)
- end
- function isFuel(slot)
- local lastSlot = getSlot()
- select(slot)
- local good = refuel(0)
- select(lastSlot)
- return good
- end
- function refuel( ammount , advanced)
- assert(type(ammount) == "number", "Invalid Argument: Expected number. Got "..type(ammount),2)
- if advanced == nil then
- advanced = true
- end
- if advanced then
- if hasFuel(ammount) then
- for i=1,16 do
- t.select(i)
- while t.getItemCount(i) > 0 do
- if not t.isFuel(i) then
- break
- end
- if hasFuel(ammount) then
- return true
- else
- t.refuel(1)
- end
- end
- end
- return false
- else
- return true
- end
- else
- return turtle.refuel(ammount)
- end
- end
- ------------------
- ---- Movement ----
- -- Rotation --
- function turnRight()
- local good = turtle.turnRight()
- if good then
- position.dir = position.dir + 1
- end
- return good
- end
- function turnLeft()
- local good = turtle.turnLeft()
- if good then
- position.dir = position.dir - 1
- end
- end
- function turnAround( _useLeft)
- if _useLeft then
- turnLeft()
- turnLeft()
- else
- turnRight()
- turnRight()
- end
- end
- function spin( _count, _useLeft )
- local count = _count or 1
- for i=1,count do
- turnAround(_useLeft)
- turnAround(_useLeft)
- end
- end
- function shakeNo(_alt)
- if _alt then
- turnLeft()
- turnAround()
- turnLeft()
- else
- turnRight()
- turnAround(true)
- turnRight()
- end
- end
- function turn(direction)
- assert(direction,"Must specify a direction!")
- direction = string.lower(direction)
- if direction == "left" then
- turnLeft()
- elseif direction == "right" then
- turnRight()
- elseif (direction == "back") or (direction == "turnaround") or (direction == "around") then
- turnAround()
- elseif (direction == "forward") or (direction == "straight") then
- return
- else
- error("'"..direction.."' is not a valid direction. See documentation.")
- end
- end
- -- Motion --
- function forward(_count)
- if not _count then
- local good = turtle.forward()
- if good then
- local m = cust_fmod(position.dir, 4)
- if m == 0 then position.x = position.x + 1
- elseif m == 1 then position.z = position.z + 1
- elseif m == 2 then position.x = position.x - 1
- elseif m == 3 then position.z = position.z - 1
- else print("An internal navigation error has occured in the custom turtle api...")
- end
- end
- return good
- end
- local good = 0
- assert(_count > 0,"Must specify a number above 0")
- assert(turtle.getFuelLevel() > _count,"Not enough fuel for '".._count.."' moves.")
- for i=1,_count do
- yieldme()
- if forward() then
- good = good + 1
- end
- end
- return good
- end
- function back(_count)
- if not _count then
- local good = turtle.back()
- if good then
- local m = cust_fmod(position.dir, 4)
- if m == 0 then position.x = position.x - 1
- elseif m == 1 then position.z = position.z - 1
- elseif m == 2 then position.x = position.x + 1
- elseif m == 3 then position.z = position.z + 1
- else print("An internal navigation error has occured in the custom turtle api...")
- end
- end
- return good
- end
- local good = 0
- assert(_count > 0,"Must specify a number above 0")
- assert(turtle.getFuelLevel() > _count,"Not enough fuel for '".._count.."' moves.")
- for i=1,_count do
- yieldme()
- if back() then
- good = good + 1
- end
- end
- return good
- end
- function up(_count)
- if not _count then
- local good = turtle.up()
- if good then
- position.y = position.y + 1
- end
- return good
- end
- local good = 0
- assert(_count > 0,"Must specify a number above 0")
- assert(turtle.getFuelLevel() > _count,"Not enough fuel for '".._count.."' moves.")
- for i=1,_count do
- yieldme()
- if up() then
- good = good + 1
- end
- end
- return good
- end
- function down(_count)
- if not _count then
- local good = turtle.down()
- if good then
- position.y = position.y - 1
- end
- return good
- end
- local good = 0
- assert(_count > 0,"Must specify a number above 0")
- assert(turtle.getFuelLevel() > _count,"Not enough fuel for '".._count.."' moves.")
- for i=1,_count do
- yieldme()
- if down() then
- good = good + 1
- end
- end
- return good
- end
- function tillBlock(direction)
- direction = direction or "forward"
- turn(direction)
- while not turtle.detect() do
- assert(hasFuel(), "Ran out of fuel.")
- forward()
- yieldme()
- end
- end
- ---------------
- ----DIGGING----
- function dig()
- local good = turtle.dig()
- local ret = good
- while good do
- sleep(0.4)
- good = turtle.dig()
- end
- return ret
- end
- function digUp()
- local good = turtle.digUp()
- local ret = good
- while good do
- sleep(0.4)
- good = turtle.digUp()
- end
- return ret
- end
- function tunnelUp(count, _refuelCheck)
- assert(type(count)=="number", "Invalid Argument: Must specify a number.",0)
- assert((count > 0)or (count == -1), "Invalid Argument: Number must be above 0 or -1 for infinite.",0)
- local refuelCheck = nil
- if _refuelCheck == nil then
- refuelCheck = true
- else
- refuelCheck = _refuelCheck
- end
- if refuelCheck and (count > 0) then
- if not refuel(count, true) then
- error("Not enough fuel...")
- end
- end
- for i=1,count do
- digUp()
- while not up() and hasFuel() do
- digUp()
- turtle.attackUp()
- end
- end
- end
- function tunnelDown(count, _refuelCheck)
- assert(type(count)=="number", "Invalid Argument: Must specify a number.",0)
- assert((count > 0)or (count == -1), "Invalid Argument: Number must be above 0 or -1 for infinite.",0)
- local refuelCheck = nil
- if _refuelCheck == nil then
- refuelCheck = true
- else
- refuelCheck = _refuelCheck
- end
- if refuelCheck and (count > 0) then
- if not refuel(count, true) then
- error("Not enough fuel...")
- end
- end
- for i=1,count do
- digDown()
- while not down() and hasFuel() do
- digDown()
- turtle.attackDown()
- end
- end
- end
- function tunnel(count, _refuelCheck)
- assert(type(count)=="number", "Invalid Argument: Must specify a number.",0)
- assert((count > 0)or (count == -1), "Invalid Argument: Number must be above 0 or -1 for infinite.",0)
- local refuelCheck = nil
- if _refuelCheck == nil then
- refuelCheck = true
- else
- refuelCheck = _refuelCheck
- end
- if refuelCheck and (count > 0) then
- if not refuel(count, true) then
- error("Not enough fuel...")
- end
- end
- for i=1,count do
- dig()
- while not forward() and hasFuel() do
- dig()
- turtle.attack()
- end
- end
- end
- print("Warning this version of the Turtle Complete api is broken!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement