Advertisement
Galbi3000

Turtle

Mar 14th, 2015
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 33.36 KB | None | 0 0
  1. -- TURTLE v0.13 Beta
  2. --
  3. -- By Galbi3000
  4. --
  5. -- A command prompt to directly control turtles or run scripts of the turtle commands.
  6. --
  7. -- Usage:
  8. --
  9. -- Type 'turtle' at the TurtleOS command prompt to enter the turtle prompt mode.
  10. --
  11. -- Alternatively type 'turtle <name>' to run a turtle script and return to the TurtleOS
  12. -- prompt when it finishes.
  13. --
  14. local version = "0.13 beta"
  15.  
  16. local tArgs = { ... }
  17. if #tArgs > 1 then
  18.     print( "Usage: turtle [scriptName]" )
  19.     return
  20. end
  21.  
  22. -- Variables used by all functions:
  23.  
  24. local tab = string.char(9)
  25.  
  26. local tCommandHistory = {}  -- For repeating commands already typed
  27.  
  28. local advanced = false      -- Indicates an advanced turtle for colour display
  29. local running = false       -- Set to true before the main loop and stays true until the exit command is used
  30. local inScript = false      -- Set to true if a script is being run
  31. local errorText = ""        -- Used for command errors in a script
  32.  
  33. local tCommandValue = {
  34.     ["forward"] = 1,
  35.     ["forwards"] = 1,
  36.     ["for"] = 1,
  37.     ["fwd"] = 1,
  38.     ["fd"] = 1,
  39.     ["fo"] = 1,
  40.     ["f"] = 1,
  41.    
  42.     ["back"] = 2,
  43.     ["backward"] = 2,
  44.     ["backwards"] = 2,
  45.     ["bk"] = 2,
  46.     ["ba"] = 2,
  47.     ["b"] = 2,
  48.    
  49.     ["up"] = 3,
  50.     ["upward"] = 3,
  51.     ["upwards"] = 3,
  52.     ["u"] = 3,
  53.     ["rise"] = 3,
  54.    
  55.     ["down"] = 4,
  56.     ["downward"] = 4,
  57.     ["downwards"] = 4,
  58.     ["dn"] = 4,
  59.     ["do"] = 4,
  60.     ["d"] = 4,
  61.     ["lower"] = 4,
  62.     ["lwr"] = 4,
  63.     ["fall"] = 4,
  64.    
  65.     ["left"] = 5,
  66.     ["lft"] = 5,
  67.     ["lt"] = 5,
  68.     ["le"] = 5,
  69.     ["l"] = 5,
  70.    
  71.     ["right"] = 6,
  72.     ["rt"] = 6,
  73.     ["ri"] = 6,
  74.     ["r"] = 6,
  75.    
  76.     ["dig"] = 7,
  77.     ["dg"] = 7,
  78.     ["di"] = 7,
  79.    
  80.     ["place"] = 8,
  81.     ["pl"] = 8,
  82.     ["p"] = 8,
  83.    
  84.     ["drop"] = 9,
  85.     ["drp"] = 9,
  86.     ["dp"] = 9,
  87.     ["dr"] = 9,
  88.    
  89.     ["suck"] = 10,
  90.     ["sk"] = 10,
  91.     ["su"] = 10,
  92.     ["get"] = 10,
  93.     ["gt"] = 10,
  94.     ["ge"] = 10,
  95.     ["g"] = 10,
  96.    
  97.     ["select"] = 11,
  98.     ["slct"] = 11,
  99.     ["sel"] = 11,
  100.     ["sl"] = 11,
  101.     ["se"] = 11,
  102.     ["s"] = 11,
  103.     ["inventory"] = 11,
  104.     ["inv"] = 11,
  105.     ["in"] = 11,
  106.     ["i"] = 11,
  107.    
  108.     ["detect"] = 12,
  109.     ["dtct"] = 12,
  110.     ["det"] = 12,
  111.     ["dt"] = 12,
  112.     ["de"] = 12,
  113.     ["sense"] = 12,
  114.     ["sen"] = 12,
  115.    
  116.     ["attack"] = 13,
  117.     ["atk"] = 13,
  118.     ["att"] = 13,
  119.     ["at"] = 13,
  120.     ["a"] = 13,
  121.     ["hit"] = 13,
  122.     ["ht"] = 13,
  123.     ["h"] = 13,
  124.    
  125.     ["equip"] = 14,
  126.     ["eqp"] = 14,
  127.     ["eq"] = 14,
  128.     ["e"] = 14,
  129.     ["use"] = 14,
  130.    
  131.     ["refuel"] = 15,
  132.     ["ref"] = 15,
  133.     ["rfl"] = 15,
  134.     ["rf"] = 15,
  135.    
  136.     ["fuel"] = 16,
  137.     ["fl"] = 16,
  138.     ["fu"] = 16,
  139.    
  140.     ["run"] = 17,
  141.    
  142.     ["exit"] = 18,
  143.     ["quit"] = 18,
  144.     ["ex"] = 18,
  145.     ["x"] = 18,
  146.     ["qt"] = 18,
  147.     ["q"] = 18,
  148.     ["end"] = 18,
  149.    
  150.     ["edit"] = 19,
  151.     ["ed"] = 19,
  152.    
  153.     ["list"] = 20,
  154.     ["ls"] = 20,
  155.     ["dir"] = 20,
  156.     ["files"] = 20,
  157.     ["scripts"] = 20,
  158.    
  159.     ["repeat"] = 21,
  160.     ["rpt"] = 21,
  161.     ["rp"] = 21,
  162.     ["re"] = 21,
  163.    
  164.     ["print"] = 22,
  165.     ["echo"] = 22,
  166.     ["write"] = 22,
  167.     ["say"] = 22,
  168.     ["pr"] = 22,
  169.     ["ec"] = 22,
  170.     ["wr"] = 22,
  171. }
  172.  
  173. local tDirection = {
  174.     ["ahead"] = 1,
  175.     ["forward"] = 1,
  176.     ["forwards"] = 1,
  177.     ["for"] = 1,
  178.     ["fwd"] = 1,
  179.     ["fd"] = 1,
  180.     ["fo"] = 1,
  181.     ["front"] = 1,
  182.     ["fr"] = 1,
  183.     ["a"] = 1,
  184.     ["f"] = 1,
  185.    
  186.     ["up"] = 2,
  187.     ["u"] = 2,
  188.     ["above"] = 2,
  189.     ["top"] = 2,
  190.     ["t"] = 2,
  191.    
  192.     ["down"] = 3,
  193.     ["d"] = 3,
  194.     ["below"] = 3,
  195.     ["bottom"] = 3,
  196.     ["b"] = 3,
  197. }
  198.  
  199. local tSide = {
  200.     ["left"] = 1,
  201.     ["lft"] = 1,
  202.     ["lt"] = 1,
  203.     ["le"] = 1,
  204.     ["l"] = 1,
  205.    
  206.     ["right"] = 2,
  207.     ["rt"] = 2,
  208.     ["ri"] = 2,
  209.     ["r"] = 2,
  210. }
  211.  
  212. -- Functions:
  213.  
  214. local function separate(line)
  215.     -- This function splits the line into 2 lines at the first space.
  216.     -- Usually for separating the command from it's parameter but also in the DROP command to get multiple
  217.     -- parameters
  218.     local i = 1
  219.    
  220.     while string.sub(line, i, i) ~= " " and i < string.len(line) do
  221.         i = i + 1
  222.     end
  223.     local first = ""
  224.     local second = ""
  225.  
  226.     if i == string.len(line) then
  227.         -- No spaces so the whole line is returned as the first part
  228.         first = line..""
  229.     else
  230.         -- Space found so copy the first part
  231.         first = string.sub(line, 1, i - 1)..""
  232.         -- Now make sure there are no leading spaces for the parameter
  233.         while string.sub(line, i, i) == " " do
  234.             i = i + 1
  235.         end
  236.         -- Now copy the second part as the rest of the line
  237.         second = string.sub(line, i)..""
  238.     end
  239.     return first, second
  240. end
  241.  
  242. local function strip(line)
  243.     -- This function strips a string of leading/trailing spaces or tabs.
  244.     -- Tabs are checked in case a script file was created outside of Minecraft with a text editor.
  245.     local nStart = 1
  246.     local nEnd = string.len(line)
  247.     while string.sub(line, nStart, nStart) == " " or string.sub(line, nStart, nStart) == tab do
  248.         nStart = nStart + 1
  249.     end
  250.     while string.sub(line, nEnd, nEnd) == " " or string.sub(line, nEnd, nEnd) == tab do
  251.         nEnd = nEnd - 1
  252.     end
  253.     return string.sub(line, nStart, nEnd)..""
  254. end
  255.  
  256. local function forward(dist)
  257.     -- This function moves the turtle forward by 1 or by the distance specified.
  258.    
  259.     if dist == nil or dist == 1 then return turtle.forward() end
  260.    
  261.     for i = 1, dist do
  262.         if not turtle.forward() then
  263.             return false
  264.         end
  265.     end
  266.     return true
  267. end
  268.  
  269. local function back(dist)
  270.     -- This function moves the turtle back by 1 or by the distance specified.
  271.    
  272.     if dist == nil or dist == 1 then return turtle.back() end
  273.    
  274.     for i = 1, dist do
  275.         if not turtle.back() then
  276.             return false
  277.         end
  278.     end
  279.     return true
  280. end
  281.  
  282. local function up(dist)
  283.     -- This function moves the turtle up by 1 or by the distance specified.
  284.    
  285.     if dist == nil or dist == 1 then return turtle.up() end
  286.    
  287.     for i = 1, dist do
  288.         if not turtle.up() then
  289.             return false
  290.         end
  291.     end
  292.     return true
  293. end
  294.  
  295. local function down(dist)
  296.     -- This function moves the turtle down by 1 or by the distance specified.
  297.    
  298.     if dist == nil or dist == 1 then return turtle.down() end
  299.    
  300.     for i = 1, dist do
  301.         if not turtle.down() then
  302.             return false
  303.         end
  304.     end
  305.     return true
  306. end
  307.  
  308. local function left(turns)
  309.     -- This function turns the turtle left by 1 or by the number specified.
  310.    
  311.     if turns == nil or turns == 1 then return turtle.turnLeft() end
  312.    
  313.     for i = 1, turns do
  314.         turtle.turnLeft()
  315.     end
  316.     return true
  317. end
  318.  
  319. local function right(turns)
  320.     -- This function turns the turtle right by 1 or by the number specified.
  321.    
  322.     if turns == nil or turns == 1 then return turtle.turnRight() end
  323.    
  324.     for i = 1, turns do
  325.         turtle.turnRight()
  326.     end
  327.     return true
  328. end
  329.  
  330. local function runCommand(command, param)
  331.     -- This function will run a command given to it by either parse or runScript
  332.     -- 'command' is the command number (currently 1-17)
  333.     -- 'param' is the string entered after the command in the parsed line
  334.     --
  335.     -- There are 2 types of error. Critical errors are shown in red on an advanced turtle.
  336.     -- Non-critical are orange.
  337.     -- When in a script the non-critical are ignored but the critical errors stop the script.
  338.     -- An alternative less friendly error message is displayed in script errors!
  339.    
  340.     -- FORWARD command
  341.     if command == tCommandValue["forward"] then
  342.         if param == "" then param = "1" end
  343.         if tonumber(param) == nil then
  344.             -- Critical error
  345.             if inScript then
  346.                 errorText = "Non numeric parameter given"
  347.                 return false
  348.             end
  349.             if advanced then term.setTextColour(colours.red) end
  350.             print("Please specify how far numerically!")
  351.         else
  352.             local dist = tonumber(param)
  353.             if turtle.getFuelLevel() < dist then
  354.                 -- Critical error
  355.                 if inScript then
  356.                     errorText = "Not enough fuel"
  357.                     return false
  358.                 end
  359.                 if advanced then term.setTextColour(colours.red) end
  360.                 print("Not enough fuel. Please refuel at least "..param.." units")
  361.             else
  362.                 if not forward(dist) and not inScript then
  363.                     -- Non-critical error
  364.                     if advanced then term.setTextColour(colours.orange) end
  365.                     print("I seem to have run into something!")
  366.                 end
  367.             end
  368.         end
  369.  
  370.     -- BACK command
  371.     elseif command == tCommandValue["back"] then
  372.         if param == "" then param = "1" end
  373.         if tonumber(param) == nil then
  374.             -- Critical error
  375.             if inScript then
  376.                 errorText = "Non numeric parameter given"
  377.                 return false
  378.             end
  379.             if advanced then term.setTextColour(colours.red) end
  380.             print("Please specify how far numerically!")
  381.         else
  382.             local dist = tonumber(param)
  383.             if turtle.getFuelLevel() < dist then
  384.                 -- Critical error
  385.                 if inScript then
  386.                     errorText = "Not enough fuel"
  387.                     return false
  388.                 end
  389.                 if advanced then term.setTextColour(colours.red) end
  390.                 print("Not enough fuel. Please refuel at least "..param.." units")
  391.             else
  392.                 if not back(dist) and not inScript then
  393.                     -- Non-critical error
  394.                     if advanced then term.setTextColour(colours.orange) end
  395.                     print("I seem to have run into something!")
  396.                 end
  397.             end
  398.         end
  399.        
  400.     -- UP command
  401.     elseif command == tCommandValue["up"] then
  402.         if param == "" then param = "1" end
  403.         if tonumber(param) == nil then
  404.             -- Critical error
  405.             if inScript then
  406.                 errorText = "Non numeric parameter given"
  407.                 return false
  408.             end
  409.             if advanced then term.setTextColour(colours.red) end
  410.             print("Please specify how far numerically!")
  411.         else
  412.             local dist = tonumber(param)
  413.             if turtle.getFuelLevel() < dist then
  414.                 -- Critical error
  415.                 if inScript then
  416.                     errorText = "Not enough fuel"
  417.                     return false
  418.                 end
  419.                 if advanced then term.setTextColour(colours.red) end
  420.                 print("Not enough fuel. Please refuel at least "..param.." units")
  421.             else
  422.                 if not up(dist) and not inScript then
  423.                     -- Non-critical error
  424.                     if advanced then term.setTextColour(colours.orange) end
  425.                     print("I seem to have run into something!")
  426.                 end
  427.             end
  428.         end
  429.        
  430.     -- DOWN command
  431.     elseif command == tCommandValue["down"] then
  432.         if param == "" then param = "1" end
  433.         if tonumber(param) == nil then
  434.             -- Critical error
  435.             if inScript then
  436.                 errorText = "Non numeric parameter given"
  437.                 return false
  438.             end
  439.             if advanced then term.setTextColour(colours.red) end
  440.             print("Please specify how far numerically!")
  441.         else
  442.             local dist = tonumber(param)
  443.             if turtle.getFuelLevel() < dist then
  444.                 -- Critical error
  445.                 if inScript then
  446.                     errorText = "Not enough fuel"
  447.                     return false
  448.                 end
  449.                 if advanced then term.setTextColour(colours.red) end
  450.                 print("Not enough fuel. Please refuel at least "..param.." units")
  451.             else
  452.                 if not down(dist) and not inScript then
  453.                     -- Non-critical error
  454.                     if advanced then term.setTextColour(colours.orange) end
  455.                     print("I seem to have run into something!")
  456.                 end
  457.             end
  458.         end
  459.        
  460.     -- LEFT command
  461.     elseif command == tCommandValue["left"] then
  462.         if param == "" then param = "1" end
  463.         if tonumber(param) == nil then
  464.             -- Critical error
  465.             if inScript then
  466.                 errorText = "Non numeric parameter given"
  467.                 return false
  468.             end
  469.             if advanced then term.setTextColour(colours.red) end
  470.             print("Please specify how many turns numerically!")
  471.         else
  472.             left(tonumber(param))
  473.         end
  474.        
  475.     -- RIGHT command
  476.     elseif command == tCommandValue["right"] then
  477.         if param == "" then param = "1" end
  478.         if tonumber(param) == nil then
  479.             -- Critical error
  480.             if inScript then
  481.                 errorText = "Non numeric parameter given"
  482.                 return false
  483.             end
  484.             if advanced then term.setTextColour(colours.red) end
  485.             print("Please specify how many turns numerically!")
  486.         else
  487.             right(tonumber(param))
  488.         end
  489.  
  490.     -- DIG command
  491.     elseif command == tCommandValue["dig"] then
  492.         if param == "" then param = "ahead" end
  493.         local ret = false
  494.         if tDirection[param] == nil then
  495.             -- Critical error
  496.             if inScript then
  497.                 errorText = "Invalid direction provided"
  498.                 return false
  499.             end
  500.             if advanced then term.setTextColour(colours.red) end
  501.             print("Please use a valid direction.")
  502.         else
  503.             if tDirection[param] == tDirection["ahead"] then
  504.                 ret = turtle.dig()
  505.             elseif tDirection[param] == tDirection["up"] then
  506.                 ret = turtle.digUp()
  507.             elseif tDirection[param] == tDirection["down"] then
  508.                 ret = turtle.digDown()
  509.             else
  510.                 -- Critical error
  511.                 if inScript then
  512.                     errorText = "Invalid parameter in DROP"
  513.                     return false
  514.                 end
  515.                 if advanced then term.setTextColour(colours.red) end
  516.                 print("You used an invalid parameter!")
  517.             end
  518.             if not ret and not inScript then
  519.                 -- Non-critical error
  520.                 if advanced then term.setTextColour(colours.orange) end
  521.                 print("It seems I can't dig that up!")
  522.             end
  523.         end
  524.  
  525.     -- PLACE command
  526.     elseif command == tCommandValue["place"] then
  527.         if param == "" then param = "ahead" end
  528.         local ret = false
  529.         if tDirection[param] == nil then
  530.             -- Critical error
  531.             if inScript then
  532.                 errorText = "Invalid direction provided"
  533.                 return false
  534.             end
  535.             if advanced then term.setTextColour(colours.red) end
  536.             print("Please use a valid direction.")
  537.         else
  538.             if tDirection[param] == tDirection["ahead"] then
  539.                 ret = turtle.place()
  540.             elseif tDirection[param] == tDirection["up"] then
  541.                 ret = turtle.placeUp()
  542.             elseif tDirection[param] == tDirection["down"] then
  543.                 ret = turtle.placeDown()
  544.             else
  545.                 -- Critical error
  546.                 if inScript then
  547.                     errorText = "Invalid parameter in DROP"
  548.                     return false
  549.                 end
  550.                 if advanced then term.setTextColour(colours.red) end
  551.                 print("You used an invalid parameter!")
  552.             end
  553.             if not ret and not inScript then
  554.                 -- Non-critical error
  555.                 if advanced then term.setTextColour(colours.orange) end
  556.                 print("It seems I can't put that there!")
  557.             end
  558.         end
  559.  
  560.     -- DROP command
  561.     -- This command was implemented late because it has the possibility of 2 parameters and I needed to think
  562.     -- it through carefully how to proceed. The answer came when I created the separate function :)
  563.     -- It is still more complex because it needs to be user friendly. The parameters do not have to be
  564.     -- provided in a specific order! E.G. DROP DOWN ALL is the same as DROP ALL DOWN
  565.     elseif command == tCommandValue["drop"] then
  566.         local param1, param2 = separate(param)
  567.         local direction = "ahead"   -- Default direction if none specified
  568.         local number = 0            -- This will default to 'all in current slot'
  569.         local ret = false
  570.  
  571.         -- First get a direction if specified
  572.         if tDirection[param2] ~= nil then direction = param2 end
  573.         if tDirection[param1] ~= nil then
  574.             if tDirection[param2] and not inSCript then
  575.                 -- Non-critical error
  576.                 if advanced then term.setTextColour(colours.orange) end
  577.                 print("Only one direction can be specified, using the first only!")
  578.             end
  579.             direction = param1
  580.         end
  581.        
  582.         -- Now get how many items to drop, if specified
  583.         local nParam1 = tonumber(param1)
  584.         local nParam2 = tonumber(param2)
  585.         if nParam2 ~= nil then number = nParam2 end
  586.         if nParam1 ~= nil then
  587.             if nParam2 and not inScript then
  588.                 -- Non-critical error
  589.                 if advanced then term.setTextColour(colours.orange) end
  590.                 print("Only one number can be specified, using the first only!")
  591.             end
  592.             number = nParam1
  593.         end
  594.        
  595.         -- Now if ALL was used as a parameter
  596.         if param1 == "all" or param2 == "all" then
  597.             if number > 0 then
  598.                 -- Critical error
  599.                 if inScript then
  600.                     errorText = "'ALL' specified with a number in DROP!"
  601.                     return false
  602.                 end
  603.                 if advanced then term.setTextColour(colours.red) end
  604.                 print("If you specify a number as well as ALL then I do not know what to do!")
  605.                 return false
  606.             else
  607.                 local curSel = turtle.getSelectedSlot()
  608.                 for i = 1, 16 do
  609.                     local count = 0
  610.                     turtle.select(i)
  611.                     if tDirection[direction] == tDirection["ahead"] then
  612.                         if turtle.drop() then count = count + 1 end
  613.                     elseif tDirection[direction] == tDirection["up"] then
  614.                         if turtle.dropUp() then count = count + 1 end
  615.                     elseif tDirection[direction] == tDirection["down"] then
  616.                         if turtle.dropDown() then count = count + 1 end
  617.                     else
  618.                         -- Critical error
  619.                         if inScript then
  620.                             errorText = "Invalid parameter in DROP"
  621.                             return false
  622.                         end
  623.                         if advanced then term.setTextColour(colours.red) end
  624.                         print("You used an invalid parameter!")
  625.                         return false
  626.                     end
  627.                     if count > 0 then ret = true end
  628.                 end -- End of for loop
  629.                 turtle.select(curSel)
  630.             end -- End of else
  631.         else
  632.             -- If ALL was not specified
  633.             if tDirection[direction] == tDirection["ahead"] then
  634.                 if number > 0 then
  635.                     ret = turtle.drop(number)
  636.                 else
  637.                     ret = turtle.drop()
  638.                 end
  639.             elseif tDirection[direction] == tDirection["up"] then
  640.                 if number > 0 then
  641.                     ret = turtle.dropUp(number)
  642.                 else
  643.                     ret = turtle.dropUp()
  644.                 end
  645.             elseif tDirection[direction] == tDirection["down"] then
  646.                 if number > 0 then
  647.                     ret = turtle.dropDown(number)
  648.                 else
  649.                     ret = turtle.dropDown()
  650.                 end
  651.             else
  652.                 -- Critical error
  653.                 if inScript then
  654.                     errorText = "Invalid parameter in DROP"
  655.                     return false
  656.                 end
  657.                 if advanced then term.setTextColour(colours.red) end
  658.                 print("You used an invalid parameter!")
  659.                 return false
  660.             end
  661.         end -- End of if ALL specified
  662.  
  663.         if ret == false and not inScript then
  664.             -- Non-critical error
  665.             if advanced then term.setTextColour(colours.orange) end
  666.             print("Either there was nothing to suck or an inventory you are trying to suck from is empty!")
  667.         end
  668.         return ret
  669.        
  670.     -- SUCK command
  671.     elseif command == tCommandValue["suck"] then
  672.         local param1, param2 = separate(param)
  673.         local direction = "ahead"   -- Default direction if none specified
  674.         local number = 0            -- This will default to 'all in current slot'
  675.         local ret = false
  676.  
  677.         -- First get a direction if specified
  678.         if tDirection[param2] ~= nil then direction = param2 end
  679.         if tDirection[param1] ~= nil then
  680.             if tDirection[param2] and not inSCript then
  681.                 -- Non-critical error
  682.                 if advanced then term.setTextColour(colours.orange) end
  683.                 print("Only one direction can be specified, using the first only!")
  684.             end
  685.             direction = param1
  686.         end
  687.        
  688.         -- Now get how many items to suck, if specified
  689.         local nParam1 = tonumber(param1)
  690.         local nParam2 = tonumber(param2)
  691.         if nParam2 ~= nil then number = nParam2 end
  692.         if nParam1 ~= nil then
  693.             if nParam2 and not inScript then
  694.                 -- Non-critical error
  695.                 if advanced then term.setTextColour(colours.orange) end
  696.                 print("Only one number can be specified, using the first only!")
  697.             end
  698.             number = nParam1
  699.         end
  700.        
  701.         -- Now if ALL was used as a parameter
  702.         -- If ALL was not specified
  703.         if tDirection[direction] == tDirection["ahead"] then
  704.             if number > 0 then
  705.                 ret = turtle.suck(number)
  706.             else
  707.                 ret = turtle.suck()
  708.             end
  709.         elseif tDirection[direction] == tDirection["up"] then
  710.             if number > 0 then
  711.                 ret = turtle.suckUp(number)
  712.             else
  713.                 ret = turtle.suckUp()
  714.             end
  715.         elseif tDirection[direction] == tDirection["down"] then
  716.             if number > 0 then
  717.                 ret = turtle.suckDown(number)
  718.             else
  719.                 ret = turtle.suckDown()
  720.             end
  721.         else
  722.             -- Critical error
  723.             if inScript then
  724.                 errorText = "Invalid parameter in SUCK"
  725.                 return false
  726.             end
  727.             if advanced then term.setTextColour(colours.red) end
  728.             print("You used an invalid parameter!")
  729.             return false
  730.         end
  731.  
  732.         if ret == false and not inScript then
  733.             -- Non-critical error
  734.             if advanced then term.setTextColour(colours.orange) end
  735.             print("Either there was nothing to suck or an inventory you are trying to suck from is empty!")
  736.         end
  737.         return ret
  738.        
  739.     -- SELECT command
  740.     elseif command == tCommandValue["select"] then
  741.         local slot = tonumber(param)
  742.         if slot >= 1 and slot <= 16 then
  743.             turtle.select(slot)
  744.         else
  745.             -- Critical error
  746.             if inScript then
  747.                 errorText = "Invalid number. Only values 1 to 16"
  748.                 return false
  749.             end
  750.             if advanced then term.setTextColour(colours.red) end
  751.             print("Please provide an inventory box number between 1 and 16 only.")
  752.         end
  753.        
  754.     -- DETECT command
  755.     elseif command == tCommandValue["detect"] then
  756.         if param == "" then param = "ahead" end
  757.         local ret = false
  758.         if tDirection[param] == nil then
  759.             -- Critical error
  760.             if inScript then
  761.                 errorText = "Invalid direction provided"
  762.                 return false
  763.             end
  764.             if advanced then term.setTextColour(colours.red) end
  765.             print("Please use a valid direction.")
  766.         else
  767.             if tDirection[param] == tDirection["ahead"] then
  768.                 ret = turtle.dig()
  769.             elseif tDirection[param] == tDirection["up"] then
  770.                 ret = turtle.digUp()
  771.             elseif tDirection[param] == tDirection["down"] then
  772.                 ret = turtle.detectgDown()
  773.             else
  774.                 -- VERY critical error! This should never happen but best be safe than sorry :)
  775.                 if inScript then
  776.                     errorText = "Invalid parameter got through checks!"
  777.                     return false
  778.                 end
  779.                 if advanced then term.setTextColour(colours.red) end
  780.                 print("It seems an invalid parameter got through my checks!")
  781.             end
  782.             if not inScript then
  783.                 -- Info not shown in a script
  784.                 if not ret then
  785.                     if advanced then term.setTextColour(colours.lightGrey) end
  786.                     print("That direction is clear.")
  787.                 else
  788.                     if advanced then term.setTextColour(colours.lightGrey) end
  789.                     print("That direction is blocked.")
  790.                 end
  791.             end
  792.             -- We got this far ok so there are no critical errors.
  793.             -- First make sure the errorText is empty so runScript knows a return of false is
  794.             -- to show there was nothing to detect. This is for when IF is implemented
  795.             errorText = ""
  796.             return ret
  797.         end
  798.        
  799.     -- ATTACK command
  800.     elseif command == tCommandValue["attack"] then
  801.         if param == "" then param = "ahead" end
  802.         local ret = false
  803.         if tDirection[param] == nil then
  804.             -- Critical error
  805.             if inScript then
  806.                 errorText = "Invalid direction provided"
  807.                 return false
  808.             end
  809.             if advanced then term.setTextColour(colours.red) end
  810.             print("Please use a valid direction.")
  811.         else
  812.             if tDirection[param] == tDirection["ahead"] then
  813.                 ret = turtle.attack()
  814.             elseif tDirection[param] == tDirection["up"] then
  815.                 ret = turtle.attackUp()
  816.             elseif tDirection[param] == tDirection["down"] then
  817.                 ret = turtle.attackDown()
  818.             else
  819.                 -- VERY critical error! This should never happen but best be safe than sorry :)
  820.                 if inScript then
  821.                     errorText = "Invalid parameter got through checks!"
  822.                     return false
  823.                 end
  824.                 if advanced then term.setTextColour(colours.red) end
  825.                 print("It seems an invalid parameter got through my checks!")
  826.             end
  827.             if not ret and not inScript then
  828.                 -- Non-critical error
  829.                 if advanced then term.setTextColour(colours.orange) end
  830.                 print("I was unable to attack!")
  831.             end
  832.         end
  833.        
  834.     -- EQUIP command
  835.     elseif command == tCommandValue["equip"] then
  836.         local ret = false
  837.         if tSide[param] == nil then
  838.             -- Critical error
  839.             if inScript then
  840.                 errorText = "No side specified for EQUIP"
  841.                 return false
  842.             end
  843.             if advanced then term.setTextColour(colours.red) end
  844.             print("Please specify either the left or right side to equip.")
  845.         elseif tSide[param] == tSide["left"] then
  846.             ret = turtle.equipLeft()
  847.         elseif tSide[param] == tSide["right"] then
  848.             ret = turtle.equipRight()
  849.         else
  850.             -- VERY critical error! This should never happen but best be safe than sorry :)
  851.             if inScript then
  852.                 errorText = "Invalid parameter got through checks!"
  853.                 return false
  854.             end
  855.             if advanced then term.setTextColour(colours.red) end
  856.             print("It seems an invalid parameter got through my checks!")
  857.         end
  858.         if not ret then
  859.             if turtle.getItemCount() > 0 then
  860.                 -- Critical error
  861.                 if inScript then
  862.                     errorText = "Tried to equip an invalid item!"
  863.                     return false
  864.                 end
  865.                 if advanced then term.setTextColour(colours.red) end
  866.                 print("I could not equip that! Please make sure it's a valid item to equip")
  867.             elseif not inScript then
  868.                 -- Non-critical error
  869.                 if advanced then term.setTextColour(colours.orange) end
  870.                 print("There is nothing there to equip!")
  871.             end
  872.         end
  873.        
  874.     -- REFUEL command
  875.     elseif command == tCommandValue["refuel"] then  -- REFUEL
  876.         local valid = false
  877.         if param == "" then
  878.             -- Refuel from all the items in the current inventory slot
  879.             valid = turtle.refuel(0)
  880.             if valid then
  881.                 turtle.refuel()
  882.                 if not inScript then
  883.                     -- Info not shown in a script
  884.                     if advanced then term.setTextColour(colours.lightGrey) end
  885.                     print("I now have "..tonumber(turtle.getFuelLevel()).." units of fuel")
  886.                 end
  887.             end
  888.         elseif param == "all" then
  889.             valid = true
  890.             local curSel = turtle.getSelectedSlot()
  891.             local curFuel = turtle.getFuelLevel()
  892.             for i =1, 16 do
  893.                 turtle.select(i)
  894.                 turtle.refuel()
  895.             end
  896.             turtle.select(curSel)
  897.             if turtle.getFuelLevel() == curFuel and not inScript then
  898.                 -- Non-critical error
  899.                 if advanced then term.setTextColour(colours.orange) end
  900.                 print("There was nothing for me to use as fuel!")
  901.             elseif not inScript then
  902.                 -- Info not shown in a script
  903.                 if advanced then term.setTextColour(colours.lightGrey) end
  904.                 print("I now have "..tonumber(turtle.getFuelLevel()).." units of fuel")
  905.             end
  906.         elseif tonumber(param) == nil then
  907.             -- Critical error
  908.             if inScript then
  909.                 errorText = "Invalid parameter provided"
  910.                 return false
  911.             end
  912.             if advanced then term.setTextColour(colours.red) end
  913.             print("Please provide a number of items to use, or ALL to use all combustibles in the inventory")
  914.         else
  915.             local num = tonumber(param)
  916.             valid = turtle.refuel(0)
  917.             if valid then
  918.                 turtle.refuel(num)
  919.                 if not inScript then
  920.                     -- Info not shown in a script
  921.                     if advanced then term.setTextColour(colours.lightGrey) end
  922.                     print("I now have "..tonumber(turtle.getFuelLevel()).." units of fuel")
  923.                 end
  924.             end
  925.         end
  926.         if not valid and not inScript then
  927.             if advanced then term.setTextColour(colours.orange) end
  928.             if turtle.getItemCount() > 0 then
  929.                 print("I can't use those items for fuel!")
  930.             else
  931.                 print("There is nothing for me to use!")
  932.             end
  933.         end
  934.        
  935.     -- FUEL command
  936.     elseif command == tCommandValue["fuel"] then
  937.         if inScript then
  938.             -- Critical error, can't use this command in scripts! (YET, Maybe)
  939.             errorText = "Invalid use of FUEL in a script."
  940.             return false
  941.         end
  942.         if advanced then term.setTextColour(colours.lightGrey) end
  943.         print("I currently have "..tonumber(turtle.getFuelLevel()).." units of fuel")
  944.  
  945.     -- LIST command
  946.     elseif command == tCommandValue["list"] then
  947.         if inScript then
  948.             -- Critical error, can't use this command in scripts
  949.             errorText = "Can not list scripts from inside a script!"
  950.             return false
  951.         end
  952.         local dir = shell.dir()
  953.         local tAll = fs.list(dir)
  954.         local tFiles = {}
  955.  
  956.         for n, sItem in pairs(tAll) do
  957.             if string.sub( sItem, 1, 1 ) ~= "." then
  958.                 local sPath = fs.combine(dir, sItem)
  959.                 if not fs.isDir(sPath) then
  960.                     if string.sub(sItem, 1, 7) == "turtle." then
  961.                         table.insert(tFiles, string.sub(sItem, 8))
  962.                     end
  963.                 end
  964.             end
  965.         end
  966.         table.sort(tFiles)
  967.  
  968.         if advanced then term.setTextColour(colours.green) end
  969.         textutils.pagedTabulate(tFiles)
  970.  
  971.     -- EXIT command
  972.     elseif command == tCommandValue["exit"] then
  973.         if inScript then
  974.             -- Critical error, can't use this command in scripts (YET, Maybe!)
  975.             -- I might make scripts able to exit if a condition is met in the IF command
  976.             -- but that is most likely going to be processed in the runScript function.
  977.             errorText = "Invalid use of EXIT in a script."
  978.             return false
  979.         end
  980.         running = false
  981.        
  982.     else
  983.         -- VERY critical error! This should never happen but best be safe than sorry :)
  984.         if inScript then
  985.             errorText = "Unknown command value passed to runCommand!"
  986.             return false
  987.         end
  988.         if advanced then term.setTextColour(colours.red) end
  989.         print("Somehow an unknown command value was passed to runCommand!")
  990.     end
  991.     return true
  992. end
  993.  
  994. local function runScript(script)
  995.     -- This function will load a script file and parse each line calling
  996.     -- runCommand for each line that is valid. This function will also deal
  997.     -- with command blocks marked with '[' and ']'.
  998.     -- The IF command is also processed here.
  999.     --
  1000.     -- If an error is found in the script then script processing will stop
  1001.     -- with a message indicating which line has an error and what it is.
  1002.    
  1003.     -- print("Script support is not implemented yet!") return
  1004.    
  1005.     local curLine = 1
  1006.     local blockStart = 0    -- Marks the start of a block of commands
  1007.    
  1008.     -- First create the actual script file name from the given script name
  1009.     local fileName = "turtle."..script
  1010.    
  1011.     -- Now check if the file exists
  1012.     if not fs.exists(fileName) then
  1013.         if advanced then term.setTextColour(colours.red) end
  1014.         print("Script "..script.." does not exist!")
  1015.         return
  1016.     end
  1017.    
  1018.     -- Load the script into a table
  1019.     local tScript = {}
  1020.     local numLines = 0
  1021.    
  1022.     local file = fs.open(fileName, "r")
  1023.     local line = file:readLine()
  1024.     while line do
  1025.         table.insert(tScript, line)
  1026.         numLines = numLines + 1
  1027.         line = file:readLine()
  1028.     end
  1029.     file:close()
  1030.    
  1031.     -- Process the script line by line
  1032.     for curLine = 1, numLines do
  1033.         -- Check if the line is not empty
  1034.         if tScript[curLine] ~= "" then
  1035.             -- Strip any spaces or tabs from the start and end of the line
  1036.             line = string.lower(strip(tScript[curLine]))
  1037.  
  1038.             -- This next bit is similar to the parse function but with differences.
  1039.             -- The first being that the whole line is already made lower case. EDIT is not supported inside
  1040.             -- scripts so case sensitivity is not needed for the parameter.
  1041.             -- Also processing of the REPEAT command along with command blocks is done in here. REPEAT and
  1042.             -- command blocks are not supported in prompt mode.
  1043.  
  1044.             -- Now separate the command from it's parameters.
  1045.             local command, param = separate(line)
  1046.            
  1047.             -- Now process the commands
  1048.             if command == tCommandValue["run"] or command == tCommandValue["edit"] then
  1049.                 if advanced then term.setTextColour(colours.red) end
  1050.                 print("Line "..tostring(curLine)..": Invalid use of "..string.upper(command).." in a script.")
  1051.                 return
  1052.                
  1053.             -- REPEAT command
  1054.             elseif command == tCommandValue["repeat"] then
  1055.                 local iterations = tonumber(param)
  1056.                 if iterations == nil then
  1057.                     if advanced then term.setTextColour(colours.red) end
  1058.                     print("Line "..tostring(curLine)..": Non numeric parameter used for REPEAT.")
  1059.                     return
  1060.                 else
  1061.                     curLine = curLine+1
  1062.                     local inBlock = false
  1063.                     while iterations > 1 do
  1064.                         line = string.lower(strip(tScript[curLine]))
  1065.                         command, param = separate(line)
  1066.                         if command == "[" then
  1067.                             blockStart = curLine
  1068.                             curLine = curLine+1
  1069.                             inBlock = true
  1070.                         elseif command == "]" then
  1071.                             if not inBlock then
  1072.                                 if advanced then term.setTextColour(colours.red) end
  1073.                                 print("Line "..tostring(curLine)..": ']' used without '['")
  1074.                                 return
  1075.                             else
  1076.                                 curLine = blockStart
  1077.                                 inBlock = false
  1078.                             end
  1079.                         else
  1080.                             if not runCommand(tCommandValue[command], param) then
  1081.                                 if advanced then term.setTextColour(colours.red) end
  1082.                                 print("Line "..curLine..": "..errorText)
  1083.                                 return
  1084.                             end
  1085.                             curLine = curLine+1
  1086.                         end
  1087.                         if not inBlock then
  1088.                             iterations = iterations - 1
  1089.                         end
  1090.                     end
  1091.                 end
  1092.                
  1093.             -- Ignore block markers if no REPEAT was used
  1094.             elseif command == "[" or command == "]" then
  1095.  
  1096.             -- PRINT command
  1097.             elseif command == tCommandValue["print"] then
  1098.                 if advanced then term.setTextColour(colours.white) end
  1099.                 print(param)
  1100.            
  1101.             -- Process all other commands
  1102.             else
  1103.                 if not runCommand(tCommandValue[command], param) then
  1104.                     if advanced then term.setTextColour(colours.red) end
  1105.                     print("Line "..curLine..": "..errorText)
  1106.                     return
  1107.                 end
  1108.             end
  1109.         end
  1110.     end
  1111. end
  1112.  
  1113. local function parse(line)
  1114.     -- This function will parse the line entered by the user and calls runCommand
  1115.     -- if the command is valid. Otherwise it will give an unknown command error.
  1116.    
  1117.     -- First strip any spaces or tabs from the start and end of the line
  1118.     line = strip(line)
  1119.    
  1120.     -- Now separate the command from it's parameters
  1121.     local command, param = separate(line)
  1122.    
  1123.     if tCommandValue[command] == nil then
  1124.         print("That is not a recognised command!")
  1125.         return
  1126.  
  1127.     -- RUN, EDIT and PRINT are processed here so that the parameter can be case sensitive
  1128.  
  1129.     -- RUN command
  1130.     elseif tCommandValue[command] == tCommandValue["run"] then
  1131.         inScript = true
  1132.         runScript(param)
  1133.         inScript = false
  1134.  
  1135.     -- EDIT command
  1136.     elseif tCommandValue[command] == tCommandValue["edit"] then
  1137.         local fileName = "turtle."..param
  1138.         shell.run("edit", fileName)
  1139.  
  1140.     -- PRINT command
  1141.     elseif command == tCommandValue["print"] then
  1142.         if advanced then term.setTextColour(colours.white) end
  1143.         print(param)
  1144.    
  1145.     -- All other commands
  1146.     else
  1147.         runCommand(tCommandValue[command], string.lower(param))
  1148.     end
  1149. end
  1150.  
  1151. -- Now the main program
  1152.  
  1153. if term.isColour() then advanced = true end
  1154.  
  1155. if not turtle then
  1156.     if advanced then term.setTextColour(colours.red) end
  1157.     print("This program can only be run in a turtle!")
  1158.     return false
  1159. end
  1160.  
  1161. if #tArgs == 1 then
  1162.     inScript = true
  1163.     return runScript(tArgs[1])
  1164. end
  1165.  
  1166. running = true
  1167.  
  1168. term.clear()
  1169. term.setCursorPos(1,1)
  1170.  
  1171. if advanced then
  1172.     term.setTextColour(colours.lightGrey)
  1173.     write("Welcome to ")
  1174.     term.setTextColour(colours.green)
  1175.     write("Turtle")
  1176.     term.setTextColour(colours.grey)
  1177.     print(" version "..version)
  1178.     term.setTextColour(colours.lightGrey)
  1179.     write("Written by ")
  1180.     term.setTextColour(colours.white)
  1181.     print("Galbi3000")
  1182.     term.setTextColour(colours.lightGrey)
  1183. else
  1184.     print("Welcome to Turtle version "..version)
  1185.     print("Written by Galbi3000")
  1186. end
  1187. print("")
  1188. print("Type EXIT or QUIT to return to TurtleOS")
  1189. print("")
  1190.  
  1191. while running do
  1192.  
  1193.     -- Display the prompt:
  1194.     if advanced then term.setTextColour(colours.yellow) end
  1195.     write("# ")
  1196.     term.setTextColour(colours.white)
  1197.    
  1198.     local s = read(nil, tCommandHistory)
  1199.     table.insert(tCommandHistory, s)
  1200.    
  1201.     parse(s)
  1202. end
  1203.  
  1204. if advanced then
  1205.     term.setTextColour(colours.lightGrey)
  1206.     write("Thank you for using ")
  1207.     term.setTextColour(colours.green)
  1208.     print("Turtle")
  1209.     term.setTextColour(colours.white)
  1210. else
  1211.     print("Thank you for using Turtle")
  1212. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement