Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ This program allows sending a path to a robot in the form of a string.
- i.e. "F30L2RBU3D14" where the letter represents a direction and
- the number represents a distance.
- Author: Hyndgrinder
- Inspired by:
- -CheersKevinGames' Agoraphobia series on Youtube:
- https://www.youtube.com/watch?v=7zADq9gUx4A&list=PLhiX1gKc3-5xZbBp8yj_1emGpX-mk2_Z6
- -Sangar's pastebin.lua program in OpenOS/bin
- ]]
- local shell = require("shell")
- local robot = require("robot")
- local c = require("component")
- local computer=require("computer")
- local term = require("term")
- local args, options = shell.parse(...)
- local wpath = {}
- -----------------Constants-------------
- local function spinSwing()
- robot.turnAround()
- robot.swing()
- robot.turnAround()
- end
- local actions = {
- --[[ actions table provides a conversion from the user inputted vector string
- to a set of actions that will move the robot and clear it's path if necessary.
- The 'out' records instruct the robot to interpret the steps of the vector string.
- The in records instruct the robot how to return on the same path.
- Assumes the robot turns around at the end of the vector string.
- ]]
- ["F"] = {
- ["out"] = {["mvmt"] = robot.forward, ["swing"] = robot.swing},
- ["in"] = {["mvmt"] = robot.forward, ["swing"] = robot.swing},
- },
- ["L"] = {
- ["out"] = {["mvmt"] = robot.turnLeft},
- ["in"] = {["mvmt"] = robot.turnRight},
- },
- ["R"] = {
- ["out"] = {["mvmt"] = robot.turnRight},
- ["in"] = {["mvmt"] = robot.turnLeft},
- },
- ["B"] = {
- ["out"] = {["mvmt"] = robot.back, ["swing"] = spinSwing},
- ["in"] = {["mvmt"] = robot.back, ["swing"] = spinSwing},
- },
- ["U"] = {
- ["out"] = {["mvmt"] = robot.up, ["swing"] = robot.swingUp},
- ["in"] = {["mvmt"] = robot.down, ["swing"] = robot.swingDown},
- },
- ["D"] = {
- ["out"] = {["mvmt"] = robot.down, ["swing"] = robot.swingDown},
- ["in"] = {["mvmt"] = robot.up, ["swing"] = robot.swingUp},
- },
- ["T"] = robot.turnAround,
- }
- --You'll want to make sure that you have enough fuel
- local function checkEnergy()
- ----make this better
- local NRG = math.floor(computer.energy()/computer.maxEnergy()*100)
- if NRG<0.5 then
- term.clearLine()
- term.write("My energy levels are falling(".. NRG .."%), are you sure?")
- local confirm = io.read()
- if not string.upper(confirm) == 'Y' then
- return false, NRG
- end
- else
- return true, NRG
- end
- end
- local function walkPath(vectorString, inOut)
- local vectors = string.gmatch(vectorString,".%d*")
- for step in vectors do
- local direction = string.sub(step,1,1)
- local distance = tonumber(string.sub(step,2)) or 1
- for i=1, distance do
- repeat
- local result, err = actions[direction][inOut]['mvmt']()
- if err then
- print(direction, distance, result, err)
- if err=='entity' then
- os.sleep(0.5)
- else
- actions[direction][inOut]["swing"]()
- end
- end
- until result
- end
- end
- actions["T"]["out"]()
- end
- --[[ printVectors: This code splits and prints the
- individual vectors for user review
- ]]
- local function printVectors(vectorString)
- local vectors = string.gmatch(vectorString,".%d*")
- for step in vectors do
- term.write(step.." ")
- end
- end
- --[[ move: This code parses the path from the options
- and walks the path.
- ]]
- local function move(vectorString)
- local energyConfirm, energy = checkEnergy()
- if energyConfirm then
- term.write("Here is the current plan, sir: \n")
- printVectors(vectorString)
- term.write("We currently have "..energy.."% energy, Shall we proceed with this ridiculous scheme, sir?\n")
- local confirm = string.upper(io.read())
- if confirm =='Y' then
- local returnTrip = {'out', 'in'} -- ensure the return of the robot
- for k, inOut in pairs(returnTrip) do
- walkPath(vectorString, inOut)
- end
- term.write("Well played, sir, I've completed another task for you.\n")
- else
- term.write("Probably wise, sir\n")
- end
- end
- end
- local command = args[1]
- if command == "move" then
- if #args >= 2 then
- move(args[2])
- return
- end
- elseif command == "printVectors" then
- if #args == 2 then
- printVectors(args[2])
- return
- end
- end
- -- If we come here there was some invalid input.
- io.write("Usages:\n")
- io.write("wpath move \"F30L2RBU3D14\"\n --Robot waits for entities, clears blocks,\n and returns on same path\n")
- io.write("wpath printVectors \"F30L2RBU3D14\"\n --robot echos the steps on screen")
- return {
- spinSwing = spinSwing;
- actions=actions;
- checkEnergy=checkEnergy;
- printVectors = printVectors;
- move = move
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement