Advertisement
jesusthekiller

uTurtle

Jun 17th, 2013
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.30 KB | None | 0 0
  1. --[[
  2.     Project info:
  3.    
  4.     Name: uTurtle
  5.     Creator: Jesusthekiller
  6.     Language: Lua (CC)
  7.     Website: None
  8.     License: GNU GPL
  9.         License file can be fount at www.jesusthekiller.com/license-gpl.html
  10.  
  11.     Version: 0.1 Beta
  12. ]]--
  13.  
  14. --[[
  15.     Changelog:
  16.       0.1:
  17.         Initial Release
  18. ]]--
  19.  
  20. --[[
  21.     LICENSE:
  22.    
  23.     uTurtle
  24.     Copyright (c) 2013 Jesusthekiller
  25.  
  26.     This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  27.  
  28.     This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  29.  
  30.     See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. ]]--
  32.  
  33. local ar = {...}
  34.  
  35. if #ar < 2 then
  36.     print("Usage: uturtle <file in> <file out> [line]")
  37.     return
  38. end
  39.  
  40. local s = " "
  41.  
  42. if ar[3] == "line" then
  43.     s = "\n"
  44. end
  45.  
  46. if ar[1] == ar[2] then
  47.     error("<file in> is same as <file out>", 0)
  48. end
  49.  
  50. local fin = fs.open(ar[1], "r") or error("Error while opening file "..ar[1], 0)
  51. local fout = fs.open(ar[2], "w") or error("Error while opening file "..ar[2], 0)
  52.  
  53. local function p(s)
  54.     fout.write(s)
  55. end
  56.  
  57. local c = {
  58.     [";go"] = "turtle.forward()",
  59.     [";back"] = "turtle.back()",
  60.     [';left'] = "turtle.turnLeft()",
  61.     [';l'] = "turtle.turnLeft()",
  62.     [';right'] = "turtle.turnRight()",
  63.     [';r'] = "turtle.turnRight()",
  64.     [';up'] = "turtle.up()",
  65.     [';down'] = "turtle.down()",
  66.    
  67.     -- Loop
  68.    
  69.     [';gol'] = "while not turtle.forward() do sleep(0.8) end",
  70.     [';backl'] = "while not turtle.back() do sleep(0.8) end",
  71.     [';upl'] = "while not turtle.up() do sleep(0.8) end",
  72.     [';downl'] = "while not turtle.down() do sleep(0.8) end",
  73.    
  74.     -- Dig
  75.    
  76.     [';dig'] = "turtle.dig()",
  77.     [';digup'] = "turtle.digUp()",
  78.     [';digdown'] = "turtle.digDown()",
  79.    
  80.     -- Dig Loop
  81.    
  82.     [';god'] = "while not turtle.forward() do turtle.dig() end",
  83.     [';backd'] = "turtle.turnLeft() turtle.turnLeft() while not turtle.forward() do turtle.dig() end turtle.turnLeft() turtle.turnLeft()",
  84.     [';upd'] = "while not turtle.up() do turtle.digUp() end",
  85.     [';downd'] = "while not turtle.down() do turtle.digDown() end",
  86.    
  87.     -- Attack
  88.    
  89.     [';att'] = "turtle.attack()",
  90.     [';attup'] = "turtle.attackUp()",
  91.     [';attdown'] = "turtle.attackDown()",
  92.    
  93.     -- Attack Suck
  94.    
  95.     [';atts'] = "turtle.attack() turtle.suck()",
  96.     [';attups'] = "turtle.attackUp() turtle.suckUp()",
  97.     [';attdowns'] = "turtle.attackDown() turtle.suckDown()",
  98.    
  99.     -- Attack Loop
  100.    
  101.     [';goa'] = "while not turtle.forward() do turtle.attack() end",
  102.     [';backa'] = "turtle.turnLeft() turtle.turnLeft() while not turtle.forward() do turtle.attack() end turtle.turnLeft() turtle.turnLeft()",
  103.     [';upa'] = "while not turtle.up() do turtle.attackUp() end",
  104.     [';downa'] = "while not turtle.down() do turtle.attackDown() end",
  105.    
  106.     -- Suck
  107.    
  108.     [';suck'] = "turtle.suck()",
  109.     [';suckup'] = "turtle.suckUp()",
  110.     [';suckdown'] = "turtle.suckDown()",
  111.     [';sucka'] = "turtle.suck() turtle.suckUp() turtle.suckDown()",
  112.    
  113.     -- Detect
  114.    
  115.     [';detect'] = "turtle.detect()",
  116.     [';detectup'] = "turtle.detectUp()",
  117.     [';detectdown'] = "turtle.detectDown()",
  118.    
  119.     -- Place
  120.    
  121.     [';pl'] = "turtle.place()",
  122.     [';plup'] = "turtle.placeUp()",
  123.     [';pldown'] = "turtle.placeDown()",
  124.    
  125.     -- Compare
  126.    
  127.     [';comp'] = "turtle.compare()",
  128.     [';compup'] = "turtle.compareUp()",
  129.     [';compdown'] = "turtle.compareDown()",
  130.    
  131.     -- Fuel
  132.    
  133.     [';fuel'] = "turtle.getFuelLevel()",
  134.    
  135.     -- Multi functions
  136.       -- Slot
  137.     [';sel'] = true,
  138.     [';count'] = true,
  139.     [';space'] = true,
  140.     [';transfer'] = true,
  141.     [';compto'] = true, --
  142.     [';craft'] = true,
  143.       -- Fuel
  144.     [';refuel'] = true, --
  145.       -- Drop
  146.     [';drop'] = true,
  147.     [';dropup'] = true,
  148.     [';dropdown'] = true,
  149. }
  150.  
  151. local t = {}
  152.  
  153. local sin = fin.readAll()
  154.  
  155. for v in string.gmatch(sin, "[^%s]+") do -- Explode
  156.     table.insert(t, v)
  157. end
  158.  
  159. fin.close()
  160.  
  161. while true do
  162.     local v = table.remove(t, 1) -- FIFO
  163.    
  164.     if v == nil then break end -- If end of table, break
  165.    
  166.     if c[v] then
  167.         if c[v] == true then
  168.             if v == ';sel' then
  169.                 p("turtle.select("..table.remove(t, 1)..")") -- ;sel slot
  170.             elseif v == ';count' then
  171.                 p("turtle.getItemCount("..table.remove(t, 1)..")") -- ;count slot
  172.             elseif v == ';space' then
  173.                 p("turtle.getItemSpace("..table.remove(t, 1)..")") -- ;space slot
  174.             elseif v == ';refuel' then
  175.                 p("turtle.refuel("..table.remove(t, 1)..")") -- ;refuel qty
  176.             elseif v == ';transfer' then
  177.                 p("turtle.transferTo("..table.remove(t, 1)..", "..table.remove(t, 1)..")") -- ;transfer slot qty
  178.             elseif v == ";drop" then
  179.                 p("turtle.drop("..table.remove(t, 1)..")") -- ;drop qty
  180.             elseif v == ";dropup" then
  181.                 p("turtle.dropUp("..table.remove(t, 1)..")") -- ;dropup qty
  182.             elseif v == ";dropdown" then
  183.                 p("turtle.dropDown("..table.remove(t, 1)..")") -- ;dropdown qty
  184.             elseif v == ";compto" then
  185.                 p("turtle.compareTo("..table.remove(t, 1)..")") -- ;compto slot
  186.             elseif v == ";craft" then
  187.                 p("turtle.craft("..table.remove(t, 1)..")") -- ;craft qty
  188.             end
  189.         else
  190.             p(c[v])
  191.         end
  192.     else
  193.         p(v)
  194.     end
  195.    
  196.     p(s)
  197. end
  198.  
  199. print(#c)
  200.  
  201. fin.close()
  202. fout.close()
  203.  
  204. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement