Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("JAPI/JUtils.lua")
- FORWARD = 0
- LEFT = 1
- BACK = 2
- RIGHT = 3
- POS_Y = 0
- NEG_X = 1
- NEG_Y = 2
- POS_X = 3
- local DIR_TYPES_STR = { "FORWARD", "LEFT", "BACK", "RIGHT", }
- local DIR_TYPES_STR_V2 = { "POS_Y", "NEG_X", "NEG_Y", "POS_X", }
- -- +y (FORWARD)
- -- /\
- -- |
- -- |
- -- |
- -- ---------> +x (RIGHT)
- local cx = 0 -- X position
- local cy = 0 -- Y position
- local cz = 0 -- Z position
- local cdir = 0 -- 0: FORWAD, 1: LEFT, 2: BACK, 3: RIGHT
- local dirDisplacement = 0
- local refuelAmount = 1
- local minFuelLevel = 5
- local fuelType = "minecraft:coal"
- -- Set the position (x, y, z), it will not move to it.
- function setPos(x, y, z)
- cx = x
- cy = y
- cz = z
- end
- function displaceDir(dir)
- if not dir then JUtils.err("Error: Dir was nil in displaceDir!") end
- if type(dir) ~= "number" then JUtils.err("Error: Dir was a number in displaceDir! dir type:", type(dir)) end
- --dir = dir-dirDisplacement
- --if dir < 0 then dir = 4+dir end
- local d = (dirDisplacement+dir) % 4
- return d
- end
- -- Check if dir is a valid direction, this can be a string or a number. This will return a bool and a table which holds the type and the dir.
- function isDir(dir)
- if type(dir) == "string" then
- local dirStr = string.upper(dir)
- for i = 1, #DIR_TYPES_STR do
- if dirStr == DIR_TYPES_STR[i] then return true, {type="string", dir=(i-1)} end
- end
- for i = 1, #DIR_TYPES_STR_V2 do
- if dirStr == DIR_TYPES_STR_V2[i] then return true, {type="string", dir=(i-1)} end
- end
- dir = tonumber(dir)
- end
- if type(dir) == "number" then
- local last = #DIR_TYPES_STR-1
- for i = 0, last do
- if dir == i then return true, {type="number", dir=(i)} end
- end
- end
- return false, nil
- end
- local function getDirNum(dir)
- local d = -1
- local success, data = isDir(dir)
- if success then
- if data.type == "number" then d = dir
- elseif data.type == "string" then d = data.dir end
- else
- JUtils.err("Error: that is not a valid direction! dir:".. dir)
- end
- if type(d) ~= "number" then JUtils.err("Error: Dir is not a number!") end
- return d
- end
- -- Set the start direction, it will not turn to it.
- function setPOSY(dir)
- cdir = getDirNum(dir)
- print("POS_Y was set to", cdir)
- if not dir then JUtils.err("Error: Dir was nil in displaceDir!") end
- --dirDisplacement = 4-cdir
- --cdir = 0
- end
- function getX()
- return cx
- end
- function getX()
- return cy
- end
- function getX()
- return cz
- end
- -- Make a string of the position vector.
- function posToStr()
- if not cx then JUtils.err("Error: cx is nil!")
- elseif not cy then JUtils.err("Error: cy is nil!")
- elseif not cz then JUtils.err("Error: cz is nil!") end
- return "(" .. cx .. ", " .. cy .. ", " .. cz .. ")"
- end
- -- Set the refuel information to use when refueling.
- function setRefuelInfo(fuelTypeI, minFuelLevelI, refuelAmountI)
- fuelType = fuelTypeI
- minFuelLevel = minFuelLevelI
- refuelAmount = refuelAmountI
- end
- -- Refuel with the set information
- function refuel()
- JUtils.refuel(fuelType, minFuelLevel, refuelAmount)
- end
- -- Turn the turtle to the specified direction.
- function turnTo(dir)
- dir = getDirNum(dir)
- --dir = displaceDir(d)
- if cdir == dir then
- return
- end
- while (cdir ~= dir) do
- turtle.turnLeft()
- cdir = (cdir + 1) % 4
- end
- end
- -- Move to position (x, y, z) from position (cx, cy, cz). The turtle will move in straight lines.
- function moveTo(x, y, z)
- local dx = x - cx
- local dy = y - cy
- local dz = z - cz
- -- Down
- while dz < 0 do
- refuel()
- turtle.down()
- cz = cz - 1
- dz = z - cz
- end
- -- Up
- while dz > 0 do
- refuel()
- turtle.up()
- cz = cz + 1
- dz = z - cz
- end
- -- Right
- while dx > 0 do
- refuel()
- turnTo(RIGHT)
- turtle.forward()
- cx = cx + 1
- dx = x - cx
- end
- -- Left
- while dx < 0 do
- refuel()
- turnTo(LEFT)
- turtle.forward()
- cx = cx - 1
- dx = x - cx
- end
- -- Forward
- while dy > 0 do
- refuel()
- turnTo(FORWARD)
- turtle.forward()
- cy = cy + 1
- dy = y - cy
- end
- -- Back
- while dy < 0 do
- refuel()
- turnTo(BACK)
- turtle.forward()
- cy = cy - 1
- dy = y - cy
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement