77wisher77

botania uniRail

Jun 6th, 2021 (edited)
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.66 KB | None | 0 0
  1. local PROGRAM_VERSION = "1.0"
  2.  
  3. local completion = require "cc.completion"
  4.  
  5. local configured --check to see if the computer has been configured, if not then prompt user, if yes then start working!
  6. local configurePath = "wisher/_states/botania_uniRail"
  7. local modes = { "source", "destination" }
  8. local sides = { "left", "right", "front", "back", "top", "bottom"}
  9. local mode --either "source" or "destination" from modes{}, source fills carts from farms, destination drains them into facilities
  10. local inputFace --redstone input
  11. local outputFace --redstone output
  12.  
  13. -- verify whether program has been configured
  14. if (fs.exists(configurePath)) then
  15.   local handle = fs.open(configurePath, "r")
  16.   local verify = handle.readLine()
  17.   handle.close()
  18.   if (verify == "") then --check to see if file is empty
  19.     configured = false
  20.   else
  21.     configured = true
  22.   end
  23. else
  24.   configured = false
  25. end
  26.  
  27.  
  28. -- can reset the config file without manually deleting it
  29. if #arg > 0 then
  30.   if (string.lower(arg[1]) == "reset") then
  31.     if (fs.exists(configurePath)) then
  32.       fs.delete(configurePath)
  33.       sleep(0.3)
  34.       if (fs.exists(configurePath)) then
  35.         configured = false
  36.         print("config reset!")
  37.       end
  38.     end
  39.   else
  40.     print("to reset config please type \"reset\" after the programname")
  41.     sleep(3)
  42.     os.reboot()
  43.   end
  44. end
  45.  
  46.  
  47.  
  48. --run configuration if not configured
  49. if not configured then
  50.   print("Please select \"source\" or \"destination\"")
  51.   print("source is where you generate the mana")
  52.   print("destination is where it is being delivered")
  53.   print("tab to complete, up/down arrow to show choices, enter to select")
  54.   mode = string.lower(read(nil, nil, function(text) return completion.choice(text, modes, false) end))
  55.   print("Which face is the redstone input, comparator side")
  56.   print("Assume you are looking at the PC screen, front would be facing you, right your right etc.")
  57.   inputFace = string.lower(read(nil, nil, completion.side))
  58.   print("Which face is the output side, powers the rail")
  59.   outputFace = string.lower(read(nil, nil, completion.side))
  60.   print("processing your inputs")
  61.   sleep(3)
  62.   shell.run("clear")
  63.  
  64.   if (not ((mode == modes[1]) or (mode == modes[2]))) then
  65.     print("incorrectly entered the 'mode', should be 'source' or 'destination'")
  66.     print("restarting in 5 seconds")
  67.     os.reboot()
  68.   end
  69.  
  70.   local inputVerify = false
  71.   local outputVerify = false
  72.   for i = 1, #sides, 1 do
  73.     if (inputFace == sides[i]) then
  74.       inputVerify = true
  75.     end
  76.     if (outputFace == sides[i]) then
  77.       outputVerify = true
  78.     end
  79.   end
  80.   if (not (inputVerify and outputVerify)) then
  81.     print("incorrectly entered the 'input or ouput', should be left, right, top, bottom, front or back")
  82.     print("restarting in 5 seconds")
  83.     os.reboot()
  84.   end
  85.  
  86.   print("saving configuration, please wait a few seconds")
  87.   local handle = fs.open(configurePath, "w")
  88.   handle.writeLine(mode)
  89.   handle.writeLine(inputFace)
  90.   handle.writeLine(outputFace)
  91.   handle.close()
  92.  
  93.   sleep(2)
  94.  
  95.   if (fs.exists(configurePath)) then
  96.     configured = true
  97.     print("configuration saved!")
  98.   else
  99.     print("something went wrong saving config, restarting")
  100.     sleep(3)
  101.     os.reboot()
  102.   end
  103.  
  104. end
  105.  
  106. if configured then
  107.  
  108.   -- import the configuration as variables
  109.   local handle = fs.open(configurePath, "r")
  110.   mode = handle.readLine()
  111.   inputFace = handle.readLine()
  112.   outputFace = handle.readLine()
  113.   handle.close()
  114.  
  115.   -- source cart
  116.   if (mode == modes[1]) then
  117.  
  118.     local boot = true
  119.     if ((rs.getAnalogInput(inputFace) == 15) and (boot)) then
  120.       rs.setAnalogOutput(outputFace, 15)
  121.       sleep(0.5)
  122.       rs.setAnalogOutput(outputFace, 0)
  123.       boot = false
  124.     end
  125.  
  126.     while true do
  127.       print("Waiting to fill cart")
  128.       os.pullEvent("redstone")
  129.       if (rs.getAnalogInput(inputFace) == 15) then
  130.         print("filled cart")
  131.         rs.setAnalogOutput(outputFace, 15)
  132.         sleep(0.5)
  133.         rs.setAnalogOutput(outputFace, 0)
  134.       end
  135.     end
  136.  
  137.  
  138.  
  139.   --destination cart
  140.   else
  141.  
  142.     rs.setAnalogOutput(outputFace, 15)
  143.     sleep(0.5)
  144.     rs.setAnalogOutput(outputFace, 0)
  145.     while true do
  146.       print("waiting for cart")
  147.       os.pullEvent("redstone")
  148.       local cart = false
  149.  
  150.       if (rs.getInput(inputFace)) then
  151.         cart = true
  152.         print("emptying cart")
  153.       end
  154.  
  155.       while cart do
  156.         os.pullEvent("redstone")
  157.         print("depleting cart")
  158.         if (rs.getAnalogInput(inputFace) == 0) then
  159.           rs.setAnalogOutput(outputFace, 15)
  160.           sleep(0.5)
  161.           rs.setAnalogOutput(outputFace, 0)
  162.           cart = false
  163.         end
  164.       end
  165.     end
  166.  
  167.   end
  168.  
  169. end
Add Comment
Please, Sign In to add comment