Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Z X are horizontal, Y is height, vertial.
- -- Facing options: N, E, S, W
- -- North is -Z East is +X South +Z West -X
- nsp = require("nsp_core")
- nsp_turtle = {}
- --Primes the turtle, makes sure it has a location var and file--
- function nsp_turtle.prime_turtle()
- turtle_location = nsp_core.unserialize("turtle_location")
- if turtle_location == nil then
- nsp_core.serialize(turtle_location_blank, "turtle_location")
- turtle_location = {["x"] = 0,["z"] = 0,["y"] = 0,["f"] = "N",["name"] = os.getComputerLabel()}
- end
- end
- --move forwards, update location file--
- function nsp_turtle.fwd()
- if turtle.forward() then
- if turtle_location["f"] == "N" then
- turtle_location["z"] = turtle_location["z"]-1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "E" then
- turtle_location["x"] = turtle_location["x"]+1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "S" then
- turtle_location["z"] = turtle_location["z"]+1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "W" then
- turtle_location["x"] = turtle_location["x"]-1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- end
- else
- return false
- end
- end
- --moves backwards, updates location file--
- function nsp_turtle.bwd()
- if turtle.forward() then
- if turtle_location["f"] == "N" then
- turtle_location["z"] = turtle_location["z"]+1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "E" then
- turtle_location["x"] = turtle_location["x"]-1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "S" then
- turtle_location["z"] = turtle_location["z"]-1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "W" then
- turtle_location["x"] = turtle_location["x"]+1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- end
- else
- return false
- end
- end
- --turns left, updates location--
- function nsp_turtle.turn_l()
- if turtle.turnLeft() then
- if turtle_location["f"] == "N" then
- turtle_location["f"] = "W"
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "E" then
- turtle_location["f"] = "N"
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "S" then
- turtle_location["f"] = "E"
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "W" then
- turtle_location["f"] = "S"
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- end
- else
- return false
- end
- end
- --turns right--
- function nsp_turtle.turn_r()
- if turtle.turnRight() then
- if turtle_location["f"] == "N" then
- turtle_location["f"] = "E"
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "E" then
- turtle_location["f"] = "S"
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "S" then
- turtle_location["f"] = "W"
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- elseif turtle_location["f"] == "W" then
- turtle_location["f"] = "N"
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- end
- else
- return false
- end
- end
- function nsp_turtle.up()
- if turtle.up() then
- turtle_location["y"] = turtle_location["y"]+1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- else
- return false
- end
- end
- function nsp_turtle.down()
- if turtle.down() then
- turtle_location["y"] = turtle_location["y"]-1
- nsp_core.serialize(turtle_location, "turtle_location")
- return true
- else
- return false
- end
- end
- function nsp_turtle.fwds(amount)
- print("Movin:" .. amount)
- --Move forwards amount
- if type(amount) == "number" then
- local moved = 0
- for i = 1, amount do
- if nsp_turtle.fwd() then
- moved = moved + 1
- else
- -- couldn't move --
- return(moved)
- end
- end
- return(moved)
- else
- --not a number, what the fuck?
- return(0)
- end
- end
- function nsp_turtle.bwds(amount)
- print("Movin:" .. amount)
- --Move forwards amount
- if type(amount) == "number" then
- local moved = 0
- for i = 1, amount do
- if nsp_turtle.bwd() then
- moved = moved + 1
- else
- -- couldn't move --
- return(moved)
- end
- end
- return(moved)
- else
- --not a number, what the fuck?
- return(0)
- end
- end
- function nsp_turtle.ups(amount)
- print("Movin:" .. amount)
- --Move forwards amount
- if type(amount) == "number" then
- local moved = 0
- for i = 1, amount do
- if nsp_turtle.up() then
- moved = moved + 1
- else
- -- couldn't move --
- return(moved)
- end
- end
- return(moved)
- else
- --not a number, what the fuck?
- return(0)
- end
- end
- function nsp_turtle.downs(amount)
- print("Movin:" .. amount)
- --Move forwards amount
- if type(amount) == "number" then
- local moved = 0
- for i = 1, amount do
- if nsp_turtle.down() then
- moved = moved + 1
- else
- -- couldn't move --
- return(moved)
- end
- end
- return(moved)
- else
- --not a number, what the fuck?
- return(0)
- end
- end
- --return home--
- function nsp_turtle.setCurrentLocation(name)
- if type(name) == "string" then
- nsp_core.serialize(turtle_location, name)
- return true
- else
- return false
- end
- end
- function nsp_turtle.face(direction)
- if direction == turtle_location["f"] then
- return true
- elseif turtle_location["f"] == "N" then
- if direction== "E" then
- return nsp_turtle.turn_r()
- elseif direction == "S" then
- if nsp_turtle.turn_r() then
- return nsp_turtle.turn_r()
- else
- return false
- end
- elseif direction == "W" then
- return nsp_turtle.turn_l()
- end
- elseif turtle_location["f"] == "E" then
- if direction == "S" then
- return nsp_turtle.turn_r()
- elseif direction == "W" then
- if nsp_turtle.turn_r() then
- return nsp_turtle.turn_r()
- else
- return false
- end
- elseif direction == "N" then
- return nsp_turtle.turn_l()
- end
- elseif turtle_location["f"] == "S" then
- if direction == "W" then
- return nsp_turtle.turn_r()
- elseif direction == "N" then
- if nsp_turtle.turn_r() then
- return nsp_turtle.turn_r()
- else
- return false
- end
- elseif direction == "E" then
- return nsp_turtle.turn_l()
- end
- elseif turtle_location["f"] == "W" then
- if direction == "N" then
- return nsp_turtle.turn_r()
- elseif direction == "E" then
- if nsp_turtle.turn_r() then
- return nsp_turtle.turn_r()
- else
- return false
- end
- elseif direction == "S" then
- return nsp_turtle.turn_l()
- end
- else
- print("Error: nsp_turtle.face requires string argument N E S W")
- end
- end
- function nsp_turtle.goX(number)
- number = number-turtle_location["x"]
- local negativeNumber
- if type(number) == "number" then
- if number == 0 then
- return 0
- elseif number > 0 then
- negativeNumber = false
- if nsp_turtle.face("E") == false then
- print("Failed at turning east somehow")
- return number
- end
- elseif number < 0 then
- negativeNumber = true
- if nsp_turtle.face("W") == false then
- print("Failed at turning west somehow")
- return number
- end
- end
- number = math.abs(number)
- number = number - nsp_turtle.fwds(number)
- if negativeNumber then
- nubmer = number * -1
- end
- return number
- else
- print("goX requires a number")
- return 0
- end
- end
- function nsp_turtle.goZ(number)
- number = number-turtle_location["z"]
- local negativeNumber
- if type(number) == "number" then
- if number == 0 then
- return 0
- elseif number > 0 then
- negativeNumber = false
- if nsp_turtle.face("S") == false then
- print("Failed at turning east somehow")
- return number
- end
- elseif number < 0 then
- negativeNumber = true
- if nsp_turtle.face("N") == false then
- print("Failed at turning west somehow")
- return number
- end
- end
- number = math.abs(number)
- number = number - nsp_turtle.fwds(number)
- if negativeNumber then
- number = number * -1
- end
- return number
- else
- print("goZ requires a number")
- return 0
- end
- end
- function nsp_turtle.goY(number)
- number = number - turtle_location["y"]
- if type(number) == "number" then
- if number == 0 then
- return 0
- elseif number > 0 then
- number = number - nsp_turtle.ups(number)
- elseif number < 0 then
- number = math.abs(number)
- number = number - nsp_turtle.downs(number)
- nubmer = -number
- end
- return number
- else
- print("goY requires a number")
- return 0
- end
- end
- function nsp_turtle.goTo(location)
- math.randomseed (os.time("local"))
- turtle_location = nsp_core.unserialize("turtle_location")
- local x_remaining = location["x"] - turtle_location["x"]
- local z_remaining = location["z"] - turtle_location["z"]
- local y_remaining = location["y"] - turtle_location["y"]
- local at_location = x_remaining + z_remaining + y_remaining == 0
- local total_changed = 0
- while at_location == false do
- local init_location = turtle_location
- x_remaining = nsp_turtle.goX(location["x"])
- turtle_location = nsp_core.unserialize("turtle_location")
- local x_changed = init_location["x"] - turtle_location["x"]
- print("X Changed: ".. x_changed)
- z_remaining = nsp_turtle.goZ(location["z"])
- turtle_location = nsp_core.unserialize("turtle_location")
- local z_changed = init_location["z"] - turtle_location["z"]
- print("Z Changed: ".. z_changed)
- y_remaining = nsp_turtle.goY(location["y"])
- turtle_location = nsp_core.unserialize("turtle_location")
- local y_changed = init_location["y"] - turtle_location["y"]
- print("Y Changed: ".. y_changed)
- total_changed = x_changed + z_changed + y_changed
- print(total_changed)
- turtle_location = nsp_core.unserialize("turtle_location")
- x_remaining = math.abs(location["x"] - turtle_location["x"])
- z_remaining = math.abs(location["z"] - turtle_location["z"])
- y_remaining = math.abs(location["y"] - turtle_location["y"])
- at_location = x_remaining + z_remaining + y_remaining == 0
- end
- nsp_turtle.face(location["f"])
- end
- function nsp_turtle.dig()
- return turtle.dig()
- end
- function nsp_turtle.dig_down()
- return turtle.digDown()
- end
- function nsp_turtle.dig_up()
- return turtle.digUp()
- end
- --mine infront--
- --mine infront up down--
- return nsp_turtle
Add Comment
Please, Sign In to add comment