Advertisement
hyndgrinder

Woodhouse/bin/wpath.lua

Aug 30th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1.  
  2. --[[ This program allows sending a path to a robot in the form of a string.
  3.     i.e. "F30L2RBU3D14" where the letter represents a direction and
  4.     the number represents a distance.
  5.     Author: Hyndgrinder
  6.     Inspired by:
  7.       -CheersKevinGames' Agoraphobia series  on Youtube:
  8.          https://www.youtube.com/watch?v=7zADq9gUx4A&list=PLhiX1gKc3-5xZbBp8yj_1emGpX-mk2_Z6
  9.       -Sangar's pastebin.lua program in OpenOS/bin
  10.  ]]
  11.  
  12.  
  13. local shell = require("shell")
  14. local robot = require("robot")
  15. local c = require("component")
  16. local computer=require("computer")
  17. local term = require("term")
  18. local args, options = shell.parse(...)
  19.  
  20. local  wpath = {}
  21.  
  22. -----------------Constants-------------
  23. local function spinSwing()
  24.   robot.turnAround()
  25.   robot.swing()
  26.   robot.turnAround()
  27. end
  28.  
  29.  
  30. local actions = {
  31. --[[  actions table provides a conversion from the user inputted vector string
  32.     to a set of actions that will move the robot and clear it's path if necessary.  
  33.     The 'out' records instruct the robot to interpret the steps of the vector string.
  34.     The in records instruct the robot how to return on the same path.
  35.     Assumes the robot turns around at the end of the vector string.
  36. ]]
  37.   ["F"] = {
  38.             ["out"] = {["mvmt"] = robot.forward, ["swing"] = robot.swing},    
  39.             ["in"]  = {["mvmt"] = robot.forward, ["swing"] = robot.swing},
  40.         },
  41.   ["L"] = {
  42.             ["out"] = {["mvmt"] = robot.turnLeft},    
  43.             ["in"]  = {["mvmt"] = robot.turnRight},
  44.         },
  45.   ["R"] = {
  46.             ["out"] = {["mvmt"] = robot.turnRight},    
  47.             ["in"]  = {["mvmt"] = robot.turnLeft},
  48.         },
  49.   ["B"] = {
  50.             ["out"] = {["mvmt"] = robot.back, ["swing"] = spinSwing},
  51.             ["in"]  = {["mvmt"] = robot.back, ["swing"] = spinSwing},
  52.         },
  53.   ["U"] = {
  54.             ["out"] = {["mvmt"] = robot.up, ["swing"] = robot.swingUp},    
  55.             ["in"]  = {["mvmt"] = robot.down, ["swing"] = robot.swingDown},
  56.         },
  57.   ["D"] = {
  58.             ["out"] = {["mvmt"] = robot.down, ["swing"] = robot.swingDown},    
  59.             ["in"]  = {["mvmt"] = robot.up, ["swing"] = robot.swingUp},
  60.         },        
  61.   ["T"] = robot.turnAround,    
  62. }
  63. --You'll want to make sure that you have enough fuel
  64. local function checkEnergy()
  65. ----make this better
  66.     local NRG = math.floor(computer.energy()/computer.maxEnergy()*100)
  67.     if NRG<0.5 then
  68.         term.clearLine()
  69.         term.write("My energy levels are falling(".. NRG .."%), are you sure?")
  70.         local confirm = io.read()
  71.         if not string.upper(confirm) == 'Y' then
  72.             return false, NRG
  73.         end
  74.     else
  75.         return true, NRG
  76.     end
  77. end
  78.  
  79. local function walkPath(vectorString, inOut)
  80.   local vectors = string.gmatch(vectorString,".%d*")
  81.   for step in vectors do
  82.     local direction = string.sub(step,1,1)
  83.     local distance = tonumber(string.sub(step,2)) or 1
  84.     for i=1, distance do
  85.       repeat
  86.         local result, err = actions[direction][inOut]['mvmt']()
  87.         if err then
  88.         print(direction, distance, result, err)
  89.           if err=='entity' then
  90.             os.sleep(0.5)
  91.           else
  92.             actions[direction][inOut]["swing"]()
  93.           end
  94.         end
  95.       until result
  96.     end
  97.   end
  98.   actions["T"]["out"]()
  99. end
  100.  
  101. --[[  printVectors:  This code splits and prints the
  102.       individual vectors for user review
  103. ]]
  104. local function printVectors(vectorString)
  105.   local vectors = string.gmatch(vectorString,".%d*")
  106.   for step in vectors do
  107.         term.write(step.." ")
  108.   end
  109. end
  110.  
  111. --[[  move:  This code parses the path from the options
  112.     and walks the path.
  113. ]]
  114. local function move(vectorString)
  115.     local energyConfirm, energy = checkEnergy()
  116.     if energyConfirm then
  117.         term.write("Here is the current plan, sir: \n")
  118.         printVectors(vectorString)
  119.         term.write("We currently have "..energy.."% energy,  Shall we proceed with this ridiculous scheme, sir?\n")
  120.         local confirm = string.upper(io.read())
  121.         if confirm =='Y' then
  122.            
  123.             local returnTrip = {'out', 'in'} -- ensure the return of the robot
  124.                 for k, inOut in pairs(returnTrip) do
  125.                     walkPath(vectorString, inOut)
  126.                 end
  127.             term.write("Well played, sir, I've completed another task for you.\n")
  128.         else
  129.             term.write("Probably wise, sir\n")
  130.         end
  131.     end
  132. end
  133.  
  134.  
  135. local command = args[1]
  136. if command == "move" then
  137.   if #args >= 2 then
  138.     move(args[2])
  139.     return
  140.   end
  141. elseif command == "printVectors" then
  142.   if #args == 2 then
  143.     printVectors(args[2])
  144.     return
  145.   end
  146. end
  147.  
  148. -- If we come here there was some invalid input.
  149. io.write("Usages:\n")
  150. io.write("wpath move \"F30L2RBU3D14\"\n  --Robot waits for entities, clears blocks,\n     and returns on same path\n")
  151. io.write("wpath printVectors \"F30L2RBU3D14\"\n  --robot echos the steps on screen")
  152.  
  153. return {
  154. spinSwing = spinSwing;
  155. actions=actions;
  156. checkEnergy=checkEnergy;
  157. printVectors = printVectors;
  158. move = move
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement