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/6/13
- Version: 1.0 Pre-Release
- 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/.
- ]]
- function _G.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 cust_fmod(num, div)
- local b = math.fmod(num, div)
- if b < 0 then
- b = (div-1) + b
- end
- return b
- end
- function createAPI()
- local t = {}
- t.posx = 0
- t.posy = 0
- t.posz = 0
- t.dir = 0
- t.slot = 1
- turtle.select(1)
- t.attemptLimit = 500
- function t.setLimit(num)
- t.attemptLimit = num or t.attemptLimit
- end
- function t.getLimit()
- return t.attemptLimit
- end
- function t.getPosition()
- local b = {}
- b.x = t.posx
- b.y = t.posy
- b.z = t.posz
- b.dir = t.dir
- return b
- end
- function t.dump()
- printError("Memory Dump of data...")
- printError("posx: "..t.posx.." posy: "..t.posy.." posz: "..t.posz)
- printError("dir: "..t.dir.." slot: "..t.slot)
- printError("fuel: "..t.getFuelLevel().." Max Attempt Limit: "..t.attemptLimit)
- printError("End of dump...")
- end
- function t.back(moveCount)
- if not moveCount then
- local good = turtle.back()
- if good then
- local b = cust_fmod(t.dir, 4)
- if b == 0 then t.posx = t.posx - 1
- elseif b == 1 then t.posz = t.posz - 1
- elseif b == 2 then t.posx = t.posx + 1
- elseif b == 3 then t.posz = t.posz + 1
- else
- printError("Turtle Complete system had a severe error.")
- t.dump()
- error("",0)
- end
- end
- return good
- else
- local count = 0
- for i=1,moveCount do
- if t.forward() then
- count = count + 1
- end
- end
- return count
- end
- end
- function t.forward(moveCount)
- if not moveCount then
- local good = turtle.forward()
- if good then
- local b = cust_fmod(t.dir, 4)
- if b == 0 then t.posx = t.posx + 1
- elseif b == 1 then t.posz = t.posz + 1
- elseif b == 2 then t.posx = t.posx - 1
- elseif b == 3 then t.posz = t.posz - 1
- else
- printError("Turtle Complete system had a severe error.")
- t.dump()
- error("",0)
- end
- end
- return good
- else
- local count = 0
- for i=1,moveCount do
- if t.forward() then
- count = count + 1
- end
- end
- return count
- end
- end
- function t.turnRight(turnCount)
- if not turnCount then
- local good = turtle.turnRight()
- if good then
- t.dir = t.dir + 1
- end
- return good
- else
- local count = 0
- for i=1,turnCount do
- if t.turnRight() then
- count = count + 1
- end
- end
- return count
- end
- end
- function t.turnLeft(turnCount)
- if not turnCount then
- local good = turtle.turnLeft()
- if good then
- t.dir = t.dir - 1
- end
- return good
- else
- local count = 0
- for i=1,turnCount do
- if t.turnLeft() then
- count = count + 1
- end
- end
- return count
- end
- end
- function t.dig()
- local count = 0
- while turtle.dig() do
- count = count + 1
- sleep(0.5)
- if count > 500 then
- return count
- end
- end
- if count == 0 then
- return false
- else
- return count
- end
- end
- function t.digDown()
- local count = 0
- while turtle.digDown() do
- count = count + 1
- if count > 500 then
- return count
- end
- sleep(0.5)
- end
- if count == 0 then
- return false
- else
- return count
- end
- end
- function t.digUp()
- local count = 0
- while turtle.digUp() do
- count = count + 1
- if count > 500 then
- return count
- end
- sleep(0.5)
- end
- if count == 0 then
- return false
- else
- return count
- end
- end
- function t.up(moveCount)
- if not moveCount then
- local good = turtle.up()
- if good then
- t.posy = t.posy + 1
- end
- return good
- else
- local count = 0
- for i=1,moveCount do
- if t.up() then
- count = count + 1
- end
- end
- return count
- end
- end
- function t.down(moveCount)
- if not moveCount then
- local good = turtle.down()
- if good then
- t.posy = t.posy - 1
- end
- return good
- else
- local count = 0
- for i=1,moveCount do
- if t.down() then
- count = count + 1
- end
- end
- return count
- end
- end
- function t.tryDown(_break, _attack)
- local attempts = 0
- if _break == nil then _break = false end
- if _attack == nil then _attack = false end
- while attempts <= t.attemptLimit do
- local good = t.down()
- if good then
- return true
- end
- if _break then t.digDown() end
- if _attack then turtle.attackDown() end
- attempts = attempts + 1
- local isleep = 0.2
- if _break then isleep = isleep + 0.3 end
- sleep(isleep)
- end
- return false
- end
- function t.tryForward(_break, _attack)
- local attempts = 0
- if _break == nil then _break = false end
- if _attack == nil then _attack = false end
- while attempts <= t.attemptLimit do
- local good = t.forward()
- if good then
- return true
- end
- if _break then t.dig() end
- if _attack then turtle.attack() end
- attempts = attempts + 1
- local isleep = 0.2
- if _break then isleep = isleep + 0.3 end
- sleep(isleep)
- end
- return false
- end
- function t.tryUp(_break, _attack)
- local attempts = 0
- if _break == nil then _break = false end
- if _attack == nil then _attack = false end
- while attempts <= t.attemptLimit do
- local good = t.up()
- if good then
- return true
- end
- if _break then t.digUp() end
- if _attack then turtle.attackUp() end
- attempts = attempts + 1
- local isleep = 0.2
- if _break then isleep = isleep + 0.3 end
- sleep(isleep)
- end
- return false
- end
- function t.getFuelLevel()
- return turtle.getFuelLevel()
- end
- function t.select(i)
- local good = turtle.select(i)
- if good then
- t.slot = i
- end
- return good
- end
- function t.craft(quantity)
- return turtle.craft(quantity)
- end
- function t.place(signText)
- return turtle.place(signText)
- end
- function t.placeDown()
- return turtle.placeDown()
- end
- function t.placeUp()
- return turtle.placeUp()
- end
- function t.detect()
- return turtle.detect()
- end
- function t.compare()
- return turtle.compare()
- end
- function t.compareUp()
- return turtle.compareUp()
- end
- function t.compareDown()
- return turtle.compareDown()
- end
- function t.compareTo(slot)
- return turtle.compareTo(slot)
- end
- function t.drop(count)
- return turtle.drop(count)
- end
- function t.dropUp(count)
- return turtle.dropUp(count)
- end
- function t.dropDown(count)
- return turtle.dropDown(count)
- end
- function t.suck(all)
- if all then
- while turtle.suck() do
- sleep(0.1)
- end
- return true
- end
- return turtle.suck()
- end
- function t.suckUp(all)
- if all then
- while turtle.suckUp() do
- sleep(0.1)
- end
- return true
- end
- return turtle.suckUp()
- end
- function t.suckDown(all)
- if all then
- while turtle.suckDown() do
- sleep(0.1)
- end
- return true
- end
- return turtle.suckDown()
- end
- function t.attack()
- return turtle.attack()
- end
- function t.attackUp()
- return turtle.attackUp()
- end
- function t.attackDown()
- return turtle.attackDown()
- end
- function t.getSlot()
- return t.slot
- end
- function t.isFuel(_slot)
- local retslot = t.getSlot()
- _slot = _slot or retslot
- t.select(_slot)
- local bFuel = t.refuel(0)
- t.select(retslot)
- return bFuel
- end
- function t.getItemCount(slotNum)
- slotNum = slotNum or t.getSlot()
- return turtle.getItemCount(slotNum)
- end
- function t.getItemSpace(slotNum)
- slotNum = slotNum or t.getSlot()
- return turtle.getItemSpace(slotNum)
- end
- function t.transferTo(slot, quantity)
- return turtle.transferTo(slot, quantity)
- end
- function t.refuel(ammount, advanced)
- local advanced = advanced or false
- if advanced then
- if ammount > t.getFuelLevel() then
- for i=1,16 do
- t.select(i)
- while t.getItemCount(i) > 0 do
- if not t.isFuel(i) then
- break
- end
- if t.getFuelLevel() > ammount then
- return true
- else
- t.refuel(1)
- end
- end
- end
- end
- else
- return turtle.refuel(ammount)
- end
- end
- --[[ Start: Attribution of code
- This section of code is based off of the default "go" program.
- It has been modified to include additional actions for this api to run.
- It also has been modified to run through the api to keep the internal position tracking intact.
- ]]
- function t.go(...) --Code must go on bottome due to shortcuts to other functions
- local args = {...}
- assert(#args > 0, "Arguments must be specified.",2)
- local tHandlers = {
- ["forward"] = t.tryForward,
- ["fwd"] = t.tryForward,
- ["back"] = t.back,
- ["bck"] = t.back,
- ["left"] = t.turnLeft,
- ["lft"] = t.turnLeft,
- ["right"] = t.turnRight,
- ["rt"] = t.turnRight,
- ["down"] = t.tryDown,
- ["up"] = t.tryUp,
- ["dig"] = t.dig,
- ["dg"] = t.dig,
- ["digdown"] = t.digDown,
- ["digup"] = t.digUp,
- ["place"] = t.place,
- ["placedown"] = t.placeDown,
- ["placeup"] = t.placeUp,
- ["strafeleft"] = t.strafeLeft,
- ["straferight"] = t.strafeRight
- }
- local nArg = 1
- local calls = {}
- local fuelNeeded = 0
- while nArg <= #args do
- local sDirection = args[nArg]
- local count = 1
- if nArg < #tArgs then
- local num = tonumber( tArgs[nArg + 1] )
- if num then
- count = num
- nArg = nArg + 1
- end
- end
- local fnHandler = assert(tHandlers[string.lower(sDirection)], sDirection.." is not a valid direction.", 2)
- if (fnHandler == t.tryForward) or (fnHandler == t.back) or (fnHandler == t.tryUp) or (fnHandler == t.tryDown) then
- fuelNeeded = fuelNeeded + count
- end
- for i=1,count do
- calls[#calls+1] = fnHandler
- end
- end
- local good = nil
- if fuelNeeded > 0 then
- good = t.refuel(fuelNeeded, true)
- end
- assert(good, "Do not have enough fuel to make it all the way through calls...\n Fuel Level: "..t.getFuelLevel().."\nFuel Needed: "..fuelNeeded,2)
- for i=1,#calls do
- local good, ret = pcall(calls[i])
- if not good then
- error(ret,3)
- end
- end
- end
- -- End of attributed code
- return t
- end
Advertisement
Add Comment
Please, Sign In to add comment