Advertisement
jille_Jr

CC: Locked Turtle + backtrack to start

Sep 7th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.80 KB | None | 0 0
  1. --DETTA SKRIPT ÄR SKRIVET
  2. --PÅ HELLO WORLD!
  3. --
  4. --DU FÅR GÄRNA LÄSA DET
  5. --MEN BLI INTE FÖR DISTRAHERAD
  6. --DET ÄR ETT GANSKA AVANCERAT SKRIPT
  7.  
  8. --(( SETTINGS ))--
  9.  
  10. local turtle_path_file = ".path"
  11.  
  12. local turtle_goal = "minecraft:lit_redstone_lamp"
  13. local turtle_disabled = {
  14. --  "turnLeft",
  15. --  "turnRight",
  16.     "up",
  17.     "down",
  18. }
  19.  
  20. local turtle_goal_success = "Du klarade pusslet! Tjohoo!"
  21. local turtle_goal_fail = "Ajaj, du klarade det inte. Tryck valfri knapp för att återvända till början..."
  22.  
  23. local hiddenFiles = {
  24.     "startup.lua",
  25.     turtle_path_file
  26. }
  27.  
  28. -- Colours
  29. local promptColour, textColour, bgColour, errColour
  30. if term.isColour() then
  31.     promptColour = colours.yellow
  32.     textColour = colours.white
  33.     bgColour = colours.black
  34.     errColour = colours.red
  35. else
  36.     promptColour = colours.white
  37.     textColour = colours.white
  38.     bgColour = colours.black
  39.     errColour = colours.white
  40. end
  41.  
  42. --(( VARIABLES ))--
  43.  
  44. local turtle_path = nil
  45. local turtle_reverse = {
  46.     forward = turtle.back,
  47.     back = turtle.forward,
  48.     up = turtle.down,
  49.     down = turtle.up,
  50.     turnRight = turtle.turnLeft,
  51.     turnLeft = turtle.turnRight,
  52. }
  53. local turtle_track = {
  54.     "forward",
  55.     "back",
  56.     "up",
  57.     "down",
  58.     "turnLeft",
  59.     "turnRight",
  60. }
  61.  
  62. --(( FUNCTIONS ))--
  63.  
  64. local function tryRefuel()
  65.     local oldSlot = turtle.getSelectedSlot()
  66.     for slot=1,16 do
  67.         if turtle.getItemCount(slot) > 0 then
  68.             turtle.select(slot)
  69.             if turtle.refuel(1) then
  70.                 turtle.select(oldSlot)
  71.                 return true
  72.             end
  73.         end
  74.     end
  75.     turtle.select(oldSlot)
  76.     return false
  77. end
  78.  
  79. local function forceRefuel()
  80.     if turtle.getFuelLevel() == 0 and not tryRefuel() then
  81.         print("Slut på bränsle! Mata mig kol så jag kan åka hem.")
  82.         repeat
  83.             os.pullEvent("turtle_inventory")
  84.             tryRefuel()
  85.         until turtle.getFuelLevel() ~= 0
  86.         print("Tack så mycket :)")
  87.     end
  88. end
  89.  
  90. local function savePath()
  91.     local file = fs.open(turtle_path_file, "w")
  92.     file.write(textutils.serialize(turtle_path))
  93.     file.close()
  94. end
  95.  
  96. local function resetPath()
  97.     turtle_path = {}
  98.     savePath()
  99. end
  100.  
  101. local function removeFromPath(index)
  102.     table.remove(turtle_path, index)
  103.     savePath()
  104. end
  105.  
  106. local function addToPath(name)
  107.     table.insert(turtle_path, name)
  108.     savePath()
  109. end
  110.  
  111. local function loadPath()
  112.     if fs.exists(turtle_path_file) then
  113.         local file = fs.open(turtle_path_file, "r")
  114.         local data = file.readAll()
  115.         file.close()
  116.         turtle_path = textutils.unserialize(data)
  117.     else
  118.         resetPath()
  119.     end
  120. end
  121.  
  122. -- Reverse
  123. local function reversePath()
  124.     for i=#turtle_path,1,-1 do
  125.         local func = turtle_path[i]
  126.         local reverse = turtle_reverse[func]
  127.         forceRefuel()
  128.         repeat until reverse()
  129.         removeFromPath(i)
  130.     end
  131. end
  132.  
  133. local function hasReachedGoal()
  134.     rs.setOutput("front", true)
  135.     local succ, data = turtle.inspect()
  136.     if succ and data and data.name == turtle_goal then
  137.         return true
  138.     end
  139.     rs.setOutput("bottom", true)
  140.     succ, data = turtle.inspectDown()
  141.     if succ and data and data.name == turtle_goal then
  142.         return true
  143.     end
  144.     return false
  145. end
  146.  
  147. -- keep tracks
  148. local function trackTurtleFunc(func, name)
  149.     return function(...)
  150.         local res = {func(...)}
  151.         if res[1] then
  152.             addToPath(name)
  153.         elseif res[2] == "Out of fuel" then
  154.             error("Fanns inget bränsle kvar vid turtle."..name.."()", 2)
  155.         elseif res[2] == "Movement obstructed" then
  156.             error("Något var ivägen för turtle."..name.."()", 2)
  157.         end
  158.         return unpack(res)
  159.     end
  160. end
  161.  
  162. local function pause()
  163.     term.setCursorBlink(true)
  164.     repeat until os.pullEvent() == "key"
  165.     term.setCursorBlink(false)
  166.     print()
  167. end
  168.  
  169. local function tableRemoveFirst(tbl, elem)
  170.     for k,v in pairs(tbl) do
  171.         if v == elem then
  172.             return table.remove(tbl, k)
  173.         end
  174.     end
  175. end
  176.  
  177. local function setupTurtleFuncs()
  178.     -- lock functions
  179.     for i,v in ipairs(turtle_disabled) do
  180.         turtle[v] = function()
  181.             error("turtle."..v.."() är avstängd för denna turtle!", 0)
  182.             return false
  183.         end
  184.         tableRemoveFirst(turtle_track, v)
  185.     end
  186.  
  187.     -- Track the functions
  188.     for i,v in ipairs(turtle_track) do
  189.         turtle[v] = trackTurtleFunc(turtle[v], v)
  190.     end
  191. end
  192.  
  193. local function setupHideFiles()
  194.     -- Hide startup.lua from listing
  195.     local _fsList = fs.list
  196.     function fs.list(...)
  197.         local args = {...}
  198.         local res = {_fsList(...)}
  199.  
  200.         -- filter out /startup.lua
  201.         if res[1] and args[1] == "" then
  202.             for _, file in ipairs(hiddenFiles) do
  203.                 tableRemoveFirst(res[1], file)
  204.             end
  205.         end
  206.  
  207.         return unpack(res)
  208.     end
  209. end
  210.  
  211. local function setupDisableEditRun()
  212.     -- Pretty haxy, but the edit.lua program checks
  213.     -- if shell.openTab then //add run button
  214.     shell.openTab = nil
  215. end
  216.  
  217. local function checkGoal()
  218.     if #turtle_path > 0 then
  219.         term.setBackgroundColor(bgColour)
  220.         term.setTextColour(promptColour)
  221.         if hasReachedGoal() then
  222.             print(turtle_goal_success)
  223.             resetPath()
  224.         else
  225.             write(turtle_goal_fail)
  226.             term.setTextColour(textColour)
  227.             pause()
  228.             reversePath()
  229.         end
  230.     end
  231. end
  232.  
  233. local function main()
  234.     loadPath()
  235.     checkGoal()
  236.  
  237.     -- Read commands and execute them
  238.     local tCommandHistory = {}
  239.     while true do
  240.         term.setBackgroundColor(bgColour)
  241.         term.setTextColour(promptColour)
  242.         write(shell.dir() .. "> ")
  243.         term.setTextColour(textColour)
  244.  
  245.         local sLine
  246.         if settings.get("shell.autocomplete") then
  247.             sLine = read(nil, tCommandHistory, shell.complete)
  248.         else
  249.             sLine = read(nil, tCommandHistory)
  250.         end
  251.         if sLine:match("%S") and tCommandHistory[#tCommandHistory] ~= sLine then
  252.             table.insert(tCommandHistory, sLine)
  253.         end
  254.         shell.run(sLine)
  255.        
  256.         checkGoal()
  257.     end
  258. end
  259.  
  260. --(( SETUP ))--
  261.  
  262. setupHideFiles()
  263. setupTurtleFuncs()
  264. setupDisableEditRun()
  265.  
  266. --(( MAIN PROGRAM ))--
  267.  
  268. local succ,err = pcall(main)
  269.  
  270. -- Print error if any
  271. term.setTextColour(errColour)
  272. term.setCursorBlink(false)
  273. print(err)
  274. shell.run("shutdown")
  275.  
  276. --(( EOF ))--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement