Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require "component"
- local sides = require "sides"
- local inventory = component.inventory_controller
- local robot = require "robot"
- local vector = require "vector"
- local generator = component.generator
- local class = {}
- class.ID = { coal = 263 }
- class.position = vector.new(0, 0, 0)
- class.direction = vector.new(1, 0, 0)
- local UP = vector.new(0, 1, 0)
- -----------------------------------------
- -- Returns a directional vector based on the given facing
- -- facing (string): either "north", "east", "south" or "west"
- -- Return: vector with x, y, z set to point into the requested direction
- -----------------------------------------
- function class.facingToDirection(facing)
- local d = string.upper(facing)
- if "EAST" == d then
- return vector.new(1, 0, 0)
- elseif "SOUTH" == d then
- return vector.new(0, 0, 1)
- elseif "WEST" == d then
- return vector.new(-1, 0, 0)
- elseif "NORTH" == d then
- return vector.new(0, 0, -1)
- else
- assert(false, "Unknown facing "..tostring(facing)..". Must be either EAST, SOUTH, WEST or NORTH")
- end
- end
- -----------------------------------------
- -- Returns the current facing of this robot as string
- -- Returns: string either "north", "east", "south" or "west"
- -----------------------------------------
- function class.getFacing()
- if class.direction.x == 0 then
- if class.direction.z == 1 then
- return "SOUTH"
- else
- return "NORTH"
- end
- elseif class.direction.x == 1 then
- return "EAST"
- else
- return "WEST"
- end
- end
- -----------------------------------------
- -- Returns the current direction as vector.
- -- Returns: vector of length 1 pointing in the current look direction.
- -----------------------------------------
- function class.getDirection()
- return class.direction:copy()
- end
- -----------------------------------------
- -- Sets the current position and facing of this robot.
- -- There are no checks if the given values are the actual position and facing.
- -- facing (string): either "north", "east", "south" or "west"
- -- x, y, z (number - optional): the current location. If not present the current location is not changed.
- -----------------------------------------
- function class.setPosition(facing, x, y, z)
- local newX = x or class.position.x
- local newY = y or class.position.y
- local newZ = z or class.position.z
- assert(type(newX) == "number", "Invalid x value")
- assert(type(newY) == "number", "Invalid y value")
- assert(type(newZ) == "number", "Invalid z value")
- assert(type(facing) == "string", "Invalid facing")
- class.position = vector.new(newX, newY, newZ)
- class.direction = class.facingToDirection(facing)
- end
- -----------------------------------------
- -- Returns the current position.
- -- Returns: vector containing the current position relative to what this robot considers 0, 0, 0.
- -----------------------------------------
- function class.getPosition()
- return class.position:copy()
- end
- -----------------------------------------
- -- Returns the current position as x y z.
- -- Returns: x, y and z of the current position relative to what this robot considers 0, 0, 0.
- -----------------------------------------
- function class.getPositionXYZ()
- return class.position.x, class.position.y, class.position.z
- end
- -----------------------------------------
- -- Turns into the requested direction.
- -- facing (string): either "north", "east", "south" or "west"
- -----------------------------------------
- function class.turn(facing)
- local newDirection = class.facingToDirection(facing)
- if (class.direction.x == newDirection.x) and (class.direction.z == newDirection.z) then
- return
- end
- if (newDirection.x == class.direction.x) or (newDirection.z == class.direction.z) then
- if (math.random(0, 1) == 0) then -- because we are robot!
- class.turnRight(2) -- turn around
- else
- class.turnLeft(2) -- turn around
- end
- elseif ((class.direction.x == 1) and (newDirection.z == 1)) or
- ((class.direction.z == 1) and (newDirection.x == -1)) or
- ((class.direction.x == -1) and (newDirection.z == -1)) or
- ((class.direction.z == -1) and (newDirection.x == 1)) then
- class.turnRight(1)
- else
- class.turnLeft(1)
- end
- end
- -----------------------------------------
- -- Turn left with direction tracking
- -----------------------------------------
- function class.turnLeft(steps)
- for step = 1,(steps or 1) do
- if (robot.turnLeft()) then
- if (class.direction.x == 0) then
- if (class.direction.z == 1) then
- class.direction.x = 1
- else
- class.direction.x = -1
- end
- class.direction.z = 0
- else
- if class.direction.x == 1 then
- class.direction.z = -1
- else
- class.direction.z = 1
- end
- class.direction.x = 0
- end
- else
- return false
- end
- end
- return true
- end
- -----------------------------------------
- -- Turn right with direction tracking
- -----------------------------------------
- function class.turnRight(steps)
- for step = 1,(steps or 1) do
- if (robot.turnRight()) then
- if (class.direction.x == 0) then
- if (class.direction.z == 1) then
- class.direction.x = -1
- else
- class.direction.x = 1
- end
- class.direction.z = 0
- else
- if class.direction.x == 1 then
- class.direction.z = 1
- else
- class.direction.z = -1
- end
- class.direction.x = 0
- end
- else
- return false
- end
- end
- return true
- end
- -----------------------------------------
- -- Moves forward with position tracking
- -----------------------------------------
- function class.forward(steps)
- local success, reason
- for step=1,(steps or 1) do
- if (not class.checkFuel()) then
- return false
- end
- success, reason = robot.forward()
- if success then
- class.position:add(class.direction)
- else
- return false, reason
- end
- end
- end
- -----------------------------------------
- -- Moves backwards with position tracking
- -----------------------------------------
- function class.back(steps)
- for step=1,(steps or 1) do
- if (not class.checkFuel()) then
- return false
- end
- success, reason = robot.back()
- if success then
- class.position:sub(class.direction)
- else
- return false, reason
- end
- end
- end
- -----------------------------------------
- -- Moves upwards with position tracking
- -----------------------------------------
- function class.up(steps)
- for step=1,(steps or 1) do
- if (not class.checkFuel()) then
- return false
- end
- success, reason = robot.up()
- if success then
- class.position:add(UP)
- else
- return false, reason
- end
- end
- end
- -----------------------------------------
- -- Moves upwards with position tracking
- -----------------------------------------
- function class.down(steps)
- for step=1,(steps or 1) do
- if (not class.checkFuel()) then
- return false
- end
- success, reason = robot.down()
- if success then
- class.position:sub(UP)
- else
- return false, reason
- end
- end
- end
- -----------------------------------------
- function class.getInventorySize()
- return inventory.getInventorySize(sides.back)
- end
- -----------------------------------------
- function class.isFuel()
- if (inventory) then
- local item = inventory.getStackInSlot(sides.back, robot.select())
- if (item) then
- return (item.id == class.ID.coal ) -- coal only, as pickaxes burn too
- else
- return false
- end
- else
- local result,_ = generator.insert(0)
- return result
- end
- end
- -----------------------------------------
- function class.selectFuel()
- for slot = 1,class.getInventorySize() do
- if (robot.count(slot) > 0) then
- robot.select(slot)
- if (class.isFuel()) then
- return true
- end
- end
- end
- return false
- end
- -----------------------------------------
- function class.shouldRefill()
- return (generator.count() < 1)
- end
- -----------------------------------------
- function class.refill()
- if (class.selectFuel()) then
- return generator.insert(1)
- end
- return false
- end
- -----------------------------------------
- function class.checkFuel()
- if (class.shouldRefill()) then
- return class.refill()
- else
- return true
- end
- end
- return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement