CreeperNukeBoom

Computercraft tollway

Dec 13th, 2019
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.55 KB | None | 0 0
  1. --Tollway by CreeperGoBoom
  2. --Fixed large tolls not working
  3. --
  4. ---------CREDITS---------
  5. --Lupus590: Up and coming hungry toll idea and programming issues.
  6. --Programming issues:
  7. --Leo - Discord
  8. --Ocawesome101 - Discord
  9. --dael - discord
  10. --SquidDev - Discord
  11. --Fatbuychummy - Discord
  12.  
  13. --Gollark - Helping fix autoupdater
  14.  
  15. local tArgs = {...}
  16. local autoUpdate=true    --toggles auto updating function
  17.  
  18. --Program vars. Do not change these
  19. local config = {} --stores all vars entered by player + some defaults
  20. config.version = "2.3 STABLE"
  21. config.conSize = 27
  22. config.statusScreenUsesTitle = false
  23. config.statusScreenShowsDebt = false
  24. -- config.redIn="bottom"
  25. -- config.redOut="bottom"
  26. -- config.redPass="bottom"
  27. -- config.bundledInColor=colors.white
  28. -- config.bundledOutColor=colors.white
  29. -- config.bundledPassColor=colors.white
  30.  
  31. if tArgs[1] == "getVersion" then
  32.   return config.version
  33. end
  34. local list = {}
  35. local results = {}
  36. local topay
  37. local count = 0
  38. local rs = redstone
  39. local statusScreen
  40. local open = false
  41. local errorNoteBlock
  42. local hungryCheck
  43.  
  44. local requiredAPIFuncs = {
  45.   "loadConfig",
  46.   "colorPrint",
  47.   "getUserInput",
  48.   "getAnswer",
  49.   "getAnswerWithPrompts",
  50.   "findPeripheral",
  51.   "peripheralCheck",
  52.   }
  53.  
  54. local function httpGet(stringURL, stringFileNameToSaveTo)
  55.   local h, err = http.get(stringURL)
  56.   if not h then printError(err) return nil end
  57.   local f = fs.open(stringFileNameToSaveTo, "w")
  58.   f.write(h.readAll())
  59.   f.close()
  60.   h.close()
  61.   return true
  62. end
  63.  
  64. if not fs.exists("apis/CGBCoreLib.lua") then
  65.   if not httpGet("https://pastebin.com/raw/xuMVS2GP", "apis/CGBCoreLib.lua") then
  66.     error("Error: Dependancy 'CGBCoreLib' could not be downloaded. Please connect your internet and restart")
  67.   end
  68. end
  69.  
  70. local core = require("apis/CGBCoreLib") --Contains complete function library used accross multiple programs and to minimize code size.
  71.  
  72. for _ , func in pairs(requiredAPIFuncs) do --For API checking to ensure not outdated
  73.   if not core[func] then
  74.     if not httpGet("https://pastebin.com/raw/xuMVS2GP", "apis/CGBCoreLib.lua") then
  75.       error("Error: Your version of CGBCoreLib is outdated! Please connect your internet and restart!")
  76.     else
  77.       os.reboot()
  78.     end
  79.   end
  80. end
  81.  
  82. local function rsControlMsg()
  83.   if config.redOutputType == "redstone" then
  84.     print("Admission query input side: ", config.redIn)
  85.     print("Player pass input side: ", config.redPass)
  86.     print("Redstone control output side: ", config.redOut)
  87.   elseif config.redOutputType == "bundled" then
  88.     print("Bundled In/Out side: ", config.bundledSide)
  89.     print("Admission query color: ", config.bundledInColor)
  90.     print("Player pass color: ", config.bundledPassColor)
  91.     print("Tollway control color: ", config.bundledOutColor)
  92.   end
  93. end
  94.  
  95. local function updateConfigFile()
  96.   config.usesErrorNoteBlock=nil
  97.   config.usesStatusScreen=nil
  98.   config.errorNoteBlockIsNetworked=nil
  99.   config.statusScreenIsNetworked=nil
  100.   config.test=nil
  101.   config.pushDir = nil
  102.   if config.chestSide and config.version ~="2.2 STABLE" and not config.destination then
  103.     repeat
  104.       print("Patcher question:")
  105.       config.destination = core.getUserInput("What is the network name of the destination chest?")
  106.       local chest2 = peripheral.wrap(config.destination)
  107.     until chest2
  108.   end
  109.   local success, sData = pcall(function() return textutils.serialize(config) end)
  110.   if not success then
  111.     error("Oops! serialization failed. Config contains a non serializable function")
  112.   elseif success then
  113.     local file = fs.open("config", "w")
  114.     file.write(sData)
  115.     file.close()
  116.   end
  117. end
  118.  
  119.  
  120. local pcSides = rs.getSides()
  121.  
  122. local function startupConfig()
  123.   local answer
  124.   local chest
  125.   local chest2
  126.   if not fs.exists("config") then
  127.     term.clear()
  128.     term.setCursorPos(1, 1)
  129.     print("Tollway by CreeperGoBoom")
  130.     print("Version: ", config.version)
  131.     print("Easy Setup Config Wizard")    --This is where all the prints for the easy setup wizard need to be
  132.     print("Toll behaviour:")
  133.     config.tollType = core.getAnswer("What type of toll do you want: freepass, perma-toll or debt-toll?", {"freepass", "debt-toll", "perma-toll"})
  134.     if config.tollType == "debt-toll" then
  135.       config.tollDebt = tonumber(core.getUserInput("How much do you want to charge in total before this tollway is set to freepass?"))
  136.       config.tollDebtRemaining = config.tollDebt
  137.     end
  138.     if config.tollType ~= "freepass" then
  139.       print("Container info:")
  140.       --local answer = core.getAnswer("Is your chest networked? y or n? (networked meaning cabled up)", {"y", "n"})
  141.       repeat
  142.         answer=core.peripheralCheck("minecraft:chest",core.getAnswerWithPrompts("I didnt find the chest networked or present, what side is it on if it is connected?",{"left","right","front","back","top","bottom","network"}))
  143.         chest = peripheral.wrap(answer)
  144.         if not chest then answer = core.getUserInput("OK. What is the full name of your networked chest?")
  145.           chest = peripheral.wrap(answer)
  146.           if not chest then
  147.             print("Oops! Nothing there yet, please place a chest or check direction and try again.")
  148.           end
  149.         end
  150.       until chest.size()
  151.       config.conSize = chest.size()
  152.       print("Chest or inventory size detected: "..chest.size().." slots!")
  153.       config.chestSide=answer
  154.       repeat
  155.         config.destination = core.getUserInput("What is the network name of the destination chest or inventory?")
  156.         chest2 = peripheral.wrap(config.destination)
  157.       until chest2
  158.       hungryCheck = core.getAnswer("Would you like to set your toll to: 'PAYP' (Pay As You Pass) or 'hungry' mode (will eat until paid and player passes)?", {"PAYP", "hungry"})
  159.       if hungryCheck == "PAYP" then
  160.         config.isHungry = false
  161.         config.tollMax = config.conSize * 64
  162.       else
  163.         config.isHungry = true
  164.       end
  165.       print("Payment Info:")
  166.       answer = core.getAnswer("Would you now like to set up your toll payment type using your chest? (Optional: Allows for checking exact item info. This is also useful for password type where you only wish to use a specific item)", {"y", "n"})
  167.       if answer == "y" then
  168.         local id, cName
  169.         repeat
  170.           print("Please deposit your desired payment type into slot 1 of your container and press enter. You can use custom named items.(This does not record qty)")
  171.           io.read()
  172.           if chest.getItemMeta(1) then
  173.             id = chest.getItemMeta(1).name
  174.             cName = chest.getItemMeta(1).displayName
  175.           else
  176.             print("No item found to record as payment type. Please ensure the item is in slot 1 and try again.")
  177.           end
  178.           print("Item Details found:")
  179.           print("ID: ", id)
  180.           print("Display Name: ", cName)
  181.           local answer2 = core.getAnswer("Is this what you want to use? y or n?", {"y", "n"})
  182.         until answer2 == "y"
  183.         config.payType = cName
  184.         config.payTypeID = id
  185.       elseif answer == "n" then
  186.         print("Security Notice: Item ID check: DISABLED")
  187.         config.payType = core.getUserInput("What is the Display Name of the item you would like to use?")
  188.       end
  189.       config.toll = core.getUserInput("How many " .. config.payType .. " would you like to charge per admission?")
  190.       config.tollMax = config.toll*64
  191.       if tonumber(config.toll) > config.tollMax and config.isHungry == false then
  192.         repeat
  193.           print("Container size verification error. Toll amount more than container size!")
  194.           print("You selected (you/container): ", config.toll, "/", config.tollMax)
  195.           print("Note: This error does not occur while hungry toll mode is on")
  196.           config.toll = core.getUserInput("How many " .. config.payType .. " would you like to charge per admission?")
  197.         until config.toll < config.tollMax
  198.       end
  199.       config.toll = tonumber(config.toll)
  200.     end --This is where redstone control questions come in
  201.     --config.redOutputType = core.getAnswerWithPrompts("What control type are you using?",{"redstone", "bundled","plethora-manipulator", "plethora-redstone-integrator"})
  202.     config.redOutputType = core.getAnswerWithPrompts("What control type are you using?",{"redstone", "bundled"})
  203.     if config.redOutputType == "redstone" then
  204.       print("Redstone dust control:")
  205.       config.redIn = core.getAnswer("What side will input for admission query?", {"front", "back", "left", "right", "top", "bottom"})
  206.       config.redPass = core.getAnswer("What side will input for player pass?", {"front", "back", "left", "right", "top", "bottom"})
  207.       config.redOut = core.getAnswer("What side will output for tollway control?", {"front", "back", "left", "right", "top", "bottom"})
  208.     elseif config.redOutputType == "bundled" then
  209.       print("Bundled cable control:")
  210.       config.bundledSide = core.getAnswer("What side is your cable attached?", {"front", "back", "left", "right", "top", "bottom"})
  211.       config.bundledInColor = colors[core.getAnswer("what color will input for player query?", {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"})]
  212.       config.bundledPassColor = colors[core.getAnswer("what color will input for player pass?", {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"})]
  213.       config.bundledOutColor = colors[core.getAnswer("what color will output for tollway control?", {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"})]
  214.     --[[elseif config.redOutputType == "plethora-redstone-integrator" then
  215.       print("Plethora Redstone Integrator Control.")
  216.       repeat
  217.         config.rsIntegrator=core.getAnswerWithPrompts("redstone_integrator",core.getAnswerWithPrompts("I didnt find the integrator networked or present, what side is it on if it is connected?",{"left","right","front","back","top","bottom","retry"}))
  218.       until config.redOutputType ~= "retry"
  219.       print("Redstone Integrator: FOUND.")
  220.       print("Configuration:")
  221.       config.rsIntegratorIn=core.getAnswerWithPrompts("What side will Input to the integrator?",{"east","west","north","south","up","down"})]]
  222.     end
  223.     print("Optional Features")
  224.     if config.tollType == "freepass" then
  225.       print("Note block: N/A")
  226.     end
  227.     answer = core.getAnswer("Would you like to set up a Status screen? y or n? Make sure monitor is networked or next to tollway for faster config", {"y", "n"})
  228.     if answer == "y" then
  229.       config.usesStatusScreen = true
  230.       if peripheral.find("monitor") then
  231.         config.statusScreenName = "monitor"
  232.         statusScreen = peripheral.find(config.statusScreenName)
  233.         statusScreen.setTextScale(0.5)
  234.         statusScreen.clear()
  235.         statusScreen.setCursorPos(1, 2)
  236.         statusScreen.write("Configuration")
  237.         statusScreen.setCursorPos(1, 3)
  238.         statusScreen.write("In Progress!")
  239.         answer = core.getAnswer("I have found your monitor and printed 'Configuration In Progress'. Is this showing on your monitor? y or n", {"y", "n"})
  240.         if answer == "n" then
  241.           answer = core.getAnswer("OK, is your monitor networked? y or n?", {"y", "n"})
  242.           if answer == "y" then
  243.             repeat
  244.               print("What is the full name of your monitor? ie: monitor_0?")
  245.               config.statusScreenName = io.read()
  246.               if not peripheral.wrap(config.statusScreenName) then
  247.                 print("Error: monitor not found. please try again")
  248.               end
  249.             until peripheral.wrap(config.statusScreenName)
  250.           else
  251.             repeat
  252.               answer = core.getAnswer("What side is your monitor?", {"front", "back", "left", "right", "top", "bottom"})
  253.               config.statusScreenName = answer
  254.               if not peripheral.wrap(config.statusScreenName) then
  255.                 print("Error: monitor not found. please try again")
  256.               end
  257.             until peripheral.wrap(config.statusScreenName)
  258.           end
  259.         end
  260.       else
  261.         answer = core.getAnswer("Is your monitor networked? y or n?", {"y", "n"})
  262.         if answer == "y" then
  263.           repeat
  264.             print("What is the full name of your monitor? ie: monitor_0?")
  265.             config.statusScreenName = io.read()
  266.             if not peripheral.wrap(config.statusScreenName) then
  267.               print("Error: monitor not found. please try again")
  268.             end
  269.           until peripheral.wrap(config.statusScreenName)
  270.         else
  271.           repeat
  272.             answer = core.getAnswer("What side is your monitor?", {"front", "back", "left", "right", "top", "bottom"})
  273.             config.statusScreenName = answer
  274.             if not peripheral.wrap(config.statusScreenName) then
  275.               print("Error: monitor not found. please try again")
  276.             end
  277.           until peripheral.wrap(config.statusScreenName)
  278.         end
  279.       end
  280.       answer = core.getAnswer("would you like to set a Title to your status screen, Eg. 'Welcoem to disneyland!' y or n", {"y", "n"})
  281.       if answer == "y" then
  282.         config.statusScreenUsesTitle = true
  283.         config.statusScreenTitle = core.getUserInput("What will be the title? Please ensure it is sorrect before pressing enter")
  284.         print("You entered: '", config.statusScreenTitle, "'. Press enter to continue")
  285.         io.read()
  286.       end
  287.       if config.tollType == "debt-toll" then
  288.         answer = core.getAnswer("You selected debt-toll. Would you like your status screen to show debt info? 'Amount left before free'", {"y", "n"})
  289.         if answer == "y" then
  290.            config.statusScreenShowsDebt = true
  291.         end
  292.       end
  293.     end
  294.    
  295.     if config.tollType ~= "freepass" then
  296.       answer = core.getAnswer("Would you like to set up a note block to sound for Insufficent funds? Make sure it is networked or next to tollway for faster config", {"y", "n"})
  297.       if answer == "y" then
  298.         repeat
  299.           answer = core.peripheralCheck("minecraft:noteblock",core.getAnswerWithPrompts("I didnt find the note block networked or present, what side is it on if it is connected?",{"left","right","front","back","top","bottom","network","retry"}))
  300.           errorNoteBlock=peripheral.wrap(answer)
  301.           if not errorNoteBlock then answer = core.getUserInput("OK. What is the full name of your noteblock?")
  302.             config.errorNoteBlock=answer
  303.             errorNoteBlock=peripheral.wrap(answer)
  304.             if not errorNoteBlock then
  305.               print("I didn't find your noteblock. please check the name and try again")
  306.             end
  307.           end
  308.         until peripheral.wrap(answer)
  309.         print("Note Block: FOUND")
  310.         repeat
  311.           config.errorSoundCount = core.getUserInput("How many times do you want your noteblock to sound?")
  312.           if not tonumber(config.errorSoundCount) then
  313.             print("Thats not a number, please try again")
  314.           end
  315.         until tonumber(config.errorSoundCount)
  316.         config.errorSoundCount = tonumber(config.errorSoundCount)
  317.         repeat
  318.           config.errorSoundDelay = core.getUserInput("What delay would you like to set? (above 0.175)")
  319.           if not tonumber(config.errorSoundDelay) then
  320.             print("Thats not a number, please try again")
  321.           elseif tonumber(config.errorSoundDelay) < 0.175 then
  322.             print("I cannot allow below 0.175, else the note block may sound too fast or only sound once. you are welcome to change this in config after ESCW is finished though to experiment... var is errorSoundDelay")
  323.           end
  324.         until tonumber(config.errorSoundDelay) and tonumber(config.errorSoundDelay) >= 0.175
  325.         config.errorSoundDelay = tonumber(config.errorSoundDelay)
  326.       end
  327.     end
  328.          
  329.     repeat
  330.       term.clear()
  331.       term.setCursorPos(1, 1)
  332.       print("Tollway by CreeperGoBoom. version: ", config.version)
  333.       print("Settings Confirmation Page")
  334.       print("Toll Type: ", config.tollType)
  335.       print("Toll Control type: ", config.redOutputType)
  336.       if config.tollType == "freepass" then
  337.         print("Chest: NOT NEEDED")
  338.         rsControlMsg() --displays redstone control prints
  339.       end
  340.       --if tollType=="freepass" then
  341.        
  342.       --elseif tollType=="debt-toll" then
  343.      
  344.       --elseif tollType=="perma-toll" then
  345.      
  346.       --end
  347.       local cDone = core.getUserInput("Please confirm your settings. Enter Y to save, Enter N to start over. Note: Non case sensitive.")
  348.       if cDone == "y" then
  349.         term.clear()
  350.         term.setCursorPos(1, 5)
  351.         print("Thankyou for using the Easy Setup Config Wizard. Have a nice day!")
  352.         print("To reconfigure. Delete 'config' and restart.")
  353.         print("Now starting...")
  354.         sleep(5)
  355.       elseif cDone == "n" then
  356.         startupConfig()
  357.       else
  358.         print("I only understand Y or N (non case sensitive). Please try again.")
  359.       end
  360.       cDone = cDone:lower()
  361.     until cDone == "y"
  362.     updateConfigFile()
  363.   else
  364.     config = core.loadConfig("config")
  365.   end
  366. end
  367.  
  368. function orderConfig()
  369.   config[1].version=config.version
  370.   config[2].toll=config.toll
  371.   updateConfigFile()
  372. end
  373.  
  374. function AutoUpdate()
  375.   if autoUpdate then
  376.     local result
  377.     local h, err = http.get("https://pastebin.com/raw/QEai09dx")
  378.     if not h then printError(err) return end
  379.     local f = fs.open("startup", "w")
  380.     f.write(h.readAll())
  381.     f.close()
  382.     h.close()
  383.     local get = loadfile("startup")
  384.     result = get("getVersion")
  385.     if result~=config.version then
  386.       config.version=result
  387.       updateConfigFile("config")
  388.       os.reboot()
  389.     end
  390.   end
  391. end
  392.  
  393. local function RingBell(count, delay)
  394.   if config.errorNoteBlock then
  395.     for _ = 1, count do
  396.       errorNoteBlock.playNote(4,2,5)
  397.       sleep(delay)
  398.     end
  399.     return true
  400.   end
  401. end
  402.  
  403. local function TollwayControl() --opens tollway if closed. also returns if it was successful.
  404.   print("getting event")
  405.   os.pullEvent("redstone")
  406.   if not open and config.redOutputType == "redstone" and rs.getInput(config.redIn, true) then
  407.     print("Now Opening")
  408.     open = true
  409.     rs.setOutput(config.redOut, true)
  410.     return open
  411.   elseif not open and config.redOutputType == "bundled" and colours.test(rs.getBundledInput(config.bundledSide), config.bundledInColor) then
  412.     print("Now Opening")
  413.     open = true
  414.     rs.setBundledOutput(config.bundledSide, config.bundledOutColor)
  415.     print("Opening")
  416.     return open
  417.   elseif open and config.redOutputType == "redstone" and rs.getInput(config.redPass, true) then
  418.     print("Now Closing")
  419.     open = false
  420.     rs.setOutput(config.redOut, false)
  421.     return open
  422.   elseif open and config.redOutputType == "bundled" and colours.test(rs.getBundledInput(config.bundledSide), config.bundledPassColor) then
  423.     print("Now Closing")
  424.     open = false
  425.     rs.setBundledOutput(config.bundledSide, 0)
  426.     print("Closing")
  427.     return open
  428.   end
  429. end
  430.  
  431.  
  432. local colorNames = {}
  433. for k, v in pairs(colors) do
  434.   colorNames[v] = k
  435. end
  436.  
  437. local function StatusScreenUpdate(stringLine1, stringLine2)
  438.   if config.statusScreenName then
  439.     statusScreen.setTextScale(0.5)
  440.     statusScreen.clear()
  441.     if config.statusScreenUsesTitle then
  442.       statusScreen.setCursorPos(1, 1)
  443.       statusScreen.write(config.statusScreenTitle)
  444.     end
  445.     statusScreen.setCursorPos(1, 2)
  446.     statusScreen.write(stringLine1)
  447.     statusScreen.setCursorPos(1, 3)
  448.     statusScreen.write(stringLine2)
  449.     if config.isHungry then
  450.       statusScreen.setCursorPos(1, 4)
  451.       statusScreen.write("Hungry Mode: " .. tostring(config.isHungry))
  452.     end
  453.     if config.statusScreenShowsDebt and config.tollType == "debt-toll" then
  454.       statusScreen.setCursorPos(1, 5)
  455.       statusScreen.write("Remaining until free: " .. config.tollDebtRemaining .. " x " .. config.payType)
  456.     end
  457.   end
  458. end
  459.  
  460. startupConfig()
  461. AutoUpdate()
  462. --orderConfig()
  463.  
  464. --Code--
  465. term.clear()
  466. term.setCursorPos(1, 1)
  467. print("Tollway by CreeperGoBoom")
  468. print("Version: ", config.version)
  469. print("Current Config Screen")
  470. print("Toll Type: ", config.tollType)
  471. print("Redstone control type: ", config.redOutputType)
  472. if config.redOutputType == "redstone" then
  473.   print("Player admit query side: " .. config.redIn)
  474.   print("Redstone out control side: " .. config.redOut)
  475.   print("Admit fulfill detection side: ", config.redPass)
  476. else
  477.   rs.setBundledOutput(config.bundledSide, 0)
  478.   print("Bundled control side: ", config.bundledSide)
  479.   print("Player query input color: ", colorNames[config.bundledInColor])
  480.   print("Player pass input color: ", colorNames[config.bundledPassColor])
  481.   print("Tollway control output color: ", colorNames[config.bundledOutColor])
  482. end
  483. if config.statusScreenName then
  484.   print("Status Screen: Yes")
  485. else
  486.   print("Status Screen: No")
  487. end
  488. if not (config.tollType == "freepass") then
  489.   if config.errorNoteBlock then
  490.     print("Insufficient funds noteblock: Yes")
  491.   else
  492.     print("Insufficient funds noteblock: No")
  493.   end
  494.   if not config.isHungry then
  495.     print("Hungry: No")
  496.     print("Maximum payment selectable: ", config.tollMax, " ", config.payType)
  497.   else
  498.     print("Hungry: Yes")
  499.   end
  500.   print("Payment Selected: ", config.payType, " x ", config.toll, ".")
  501. end
  502.  
  503. function updateDebt(amount)
  504.   if config.tollType == "debt-toll" then
  505.     config.tollDebtRemaining = config.tollDebtRemaining - amount
  506.     if config.tollDebtRemaining <= 0 then config.tollType = "freepass" end
  507.     updateConfigFile()
  508.   end
  509. end
  510.  
  511. --wraps peripherals if they are used in config, else they are not used. Allows for aesthetic tollway design.
  512. statusScreen = core.findPeripheral("monitor",config.statusScreenName)
  513. errorNoteBlock = core.findPeripheral("note_block",config.errorNoteBlock)
  514. chest = core.findPeripheral("chest",config.chestSide)
  515. if config.destination then chest2 = peripheral.wrap(config.destination) end
  516.  
  517. topay=config.toll
  518.  
  519. function condenseItems()
  520.   for i = 1,config.conSize do
  521.     chest.pushItems("self",i,64)
  522.   end
  523.   return
  524. end
  525.  
  526. function getChestInfo()
  527.   for slot = 1, config.conSize do
  528.     if config.payTypeID and output[slot] and output[slot].displayName == config.payType and output[slot].name == config.payTypeID then
  529.       results[slot] = output[slot].count
  530.     elseif meta and meta.displayName==config.payType then
  531.       results[slot] = output[slot].count
  532.     else
  533.       sleep()
  534.     end
  535.   end
  536. end
  537.  
  538. local funcs = {}
  539. local output = {}
  540. local sz
  541. if not config.chestSide then
  542. sz = 1
  543. else
  544. sz = chest.size()
  545. end
  546. local slotsPerFunc = math.ceil(sz / 12) -- lets start with 8 functions
  547.  
  548.  
  549.  
  550. for i = 1, sz, slotsPerFunc do
  551.   local function tmp()
  552.     for o = i, i + slotsPerFunc do
  553.       if o > sz then return end
  554.       output[o] = chest.getItemMeta(o)
  555.     end
  556.   end
  557.   table.insert(funcs, tmp)
  558. end
  559.  
  560. while true do
  561. sleep()
  562.   while config.tollType ~= "freepass" and config.isHungry and (open or not open) do
  563.     local remaining = config.toll
  564.     while remaining > 0 and not open do
  565.     --while not open do
  566.       --local event = os.pullEvent()
  567.       --condenseItems()
  568.       parallel.waitForAll(table.unpack(funcs))
  569.       for slot = 1, config.conSize do
  570.         if config.payTypeID and output[slot] and output[slot].displayName == config.payType and output[slot].name == config.payTypeID then
  571.           results[slot] = output[slot].count
  572.         elseif meta and meta.displayName==config.payType then
  573.           results[slot] = output[slot].count
  574.         else
  575.           sleep()
  576.         end
  577.       end
  578.       print("Using hungry mode")
  579.       StatusScreenUpdate("Status: Processing payment", "Cost: " .. config.toll .. " x " .. config.payType)
  580.       for key,_ in pairs(results) do
  581.         if key == nil then
  582.           sleep()
  583.         end
  584.         if remaining == 0 then
  585.           --print(remaining)
  586.           break
  587.         elseif chest.pushItems(config.destination,key,config.toll)==config.toll then
  588.           updateDebt(config.toll)
  589.           remaining = 0
  590.           --print(remaining)
  591.         elseif remaining < config.toll then if chest.pushItems(config.destination,key,remaining)==remaining then
  592.           updateDebt(remaining)
  593.           remaining = 0
  594.           --print(remaining)
  595.           end
  596.         elseif chest.pushItems(config.destination,key,1)==1 then
  597.           updateDebt(1)
  598.           remaining = remaining - 1
  599.           --print(remaining)
  600.         else
  601.           sleep()
  602.         end
  603.       end
  604.     end
  605.     while remaining == 0 and not open do
  606.       StatusScreenUpdate("Status: Paid", "Please proceed")
  607.       TollwayControl()
  608.     end
  609.     --end    
  610.     --while open do
  611.    
  612.     --os.pullEvent("redstone") --there is some redstone event
  613.     while remaining == 0 and open do if open and TollwayControl() then remaining=config.toll end
  614.     --os.pullEvent("redstone")
  615.     --if open then TollwayControl() end
  616.     end
  617.   end
  618.   while config.tollType ~= "freepass" and topay > 0 and config.isHungry==false do
  619.   sleep()
  620.     StatusScreenUpdate("Status: Awaiting Payment.", "Cost: " .. config.toll .. " x " .. config.payType)
  621.     if config.redOutputType == "redstone" then
  622.       rs.setOutput(config.redOut, false)
  623.     elseif config.redOutputType == "bundled" then
  624.       rs.setBundledOutput(config.bundledSide, 0)
  625.     end
  626.     os.pullEvent("redstone")
  627.     if config.redOutputType == "redstone" and
  628.     rs.getInput(config.redIn)
  629.     or config.redOutputType == "bundled"
  630.     and colours.test(rs.getBundledInput(config.bundledSide), config.bundledInColor) then
  631.       --sleep(0.25)
  632.       print("player query detected")
  633.       condenseItems()
  634.       parallel.waitForAll(table.unpack(funcs))
  635.       for slot = 1, config.conSize do
  636.         if config.payTypeID and output[slot] and output[slot].displayName == config.payType and output[slot].name == config.payTypeID then
  637.           results[slot] = output[slot].count
  638.         elseif meta and meta.displayName==config.payType then
  639.           results[slot] = output[slot].count
  640.         else
  641.           sleep()
  642.         end
  643.       end
  644.       for key, val in pairs(results) do
  645.         print(results[key])
  646.         print(key, " : ", val)
  647.         count = count + val
  648.       end
  649.       if count >= config.toll then
  650.         print("Total payment detected: ", count)
  651.         print("Amount to pay: ", topay)
  652.         --RingBell(1, 0)
  653.         repeat
  654.           --print(i," : ",v)
  655.           --sleep(0)
  656.           for key, val in pairs(results) do
  657.             if val >= topay then--This is where the tollway / item push code should go
  658.               print("Granting access: Code 1")
  659.               updateDebt(config.toll)
  660.               StatusScreenUpdate("Status: Paid", "Please proceed")
  661.               if config.redOutputType == "redstone" then
  662.                 rs.setOutput(config.redOut, true)
  663.                 open = true
  664.               elseif config.redOutputType == "bundled" then
  665.                 rs.setBundledOutput(config.bundledSide, config.bundledOutColor)
  666.                 open = true
  667.               end
  668.               print("This should only be printing once")
  669.               if chest.pushItems(config.destination, key, config.toll) then
  670.               --if chest.drop(key, config.toll, config.pushDir) then
  671.                 topay = 0
  672.                 open = true
  673.                 break
  674.               else
  675.                 print("toll not payable from current slot, moving on")
  676.               end
  677.             elseif val < topay or val >= topay then
  678.               print("Counting pennies")
  679.               StatusScreenUpdate("Status: Processing payment", "Cost: " .. config.toll .. " x " .. config.payType)
  680.               if  val < topay  then
  681.                 chest.pushItems(config.destination, key, val)
  682.                 topay = topay - val
  683.                 updateDebt(val)
  684.                 print("Amount left to pay: ", topay)
  685.                 if topay < 0 then
  686.                   error("Oops! something went wrong and chest was overcharged!")
  687.                 end
  688.               elseif val >= topay then
  689.                 chest.pushItems(config.destination, key, topay)
  690.                 updateDebt(topay)
  691.                 topay = 0
  692.                 print("Payment success!")
  693.               end
  694.             end
  695.           end
  696.         until topay == 0
  697.         -- print("This should only be printing once")
  698.         -- if config.tollType=="debt-toll" then
  699.           -- config.tollDebtRemaining=config.tollDebtRemaining-config.toll
  700.           -- if config.tollDebtRemaining <= 0 then
  701.             -- config.tollType="freepass"
  702.           -- end
  703.           -- updateConfigFile()
  704.         -- end
  705.       else
  706.         StatusScreenUpdate("Status: Access Denied!", "Insufficient Funds")
  707.         print("Access denied! Insufficient funds!")
  708.         RingBell(config.errorSoundCount, config.errorSoundDelay)
  709.         sleep(1)
  710.       end
  711.       if config.redOutputType == "redstone" then
  712.         rs.setOutput(config.redOut, true)
  713.       elseif config.redOutputType == "bundled" then
  714.         rs.setBundledOutput(config.bundledSide, config.bundledOutColor)
  715.       end
  716.       --reset everything
  717.       results = {}
  718.       count = 0
  719.     end
  720.   end
  721.   while topay == 0 and config.tollType ~= "freepass" and config.isHungry==false do
  722.     --os.pullEvent("redstone")
  723.     if TollwayControl() then
  724.       topay = config.toll
  725.       open = false
  726.       StatusScreenUpdate("Status: Awaiting Payment.", "Cost: " .. config.toll .. " x " .. config.payType)
  727.     end
  728.     sleep()
  729.   end
  730.   while config.tollType == "freepass" do
  731.     StatusScreenUpdate("Status: OK.", "Cost: Free.")
  732.     --os.pullEvent("redstone") --there is some redstone event
  733.     TollwayControl()
  734.   end
  735. end
  736.  
  737. -- while ispaid do
  738.   -- print("access granted")
  739.   -- ispaid=false
  740. -- end
Add Comment
Please, Sign In to add comment