Link712011

controller

Aug 17th, 2017
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.25 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw;
  2.  
  3. local sg
  4. local chest
  5.  
  6. local AUTO_UPDATE = false
  7. local SG_WRAP_TRY_SLEEP = 5
  8. local BAD_ADDRESS_SLEEP = 3
  9. local SU_RF_CONVERSION = 80
  10. local BASE_RF = 1000000
  11. local REDSTONE_PER_BASE_RF = 4
  12. local PAYMENT_ITEM_NAME = 'redstone'
  13. local PAYMENT_TIMOUT_SECONDS = 120
  14. local PAYMENT_CHECK_SLEEP_LOOP = 5
  15. local TRASH_DIRECTION = 'south'
  16. local ENDER_CHEST_DIRECTION = 'down'
  17. local CHEST_WRAP = 'bottom'
  18. local TIME_VORTEX_ACTIVATION_SECONDS = 12
  19. local PASTE_CODE = 'rc5RBMYA'
  20. local FILE_PATH = 'controller'
  21.  
  22. -- Free adresses
  23. local FREE_ADRESSES = {}
  24.  
  25. FREE_ADRESSES[#FREE_ADRESSES + 1] = 'CQTA-103-2Q' -- Warp minage
  26. FREE_ADRESSES[#FREE_ADRESSES + 1] = '0SFE-TCI-SV' -- Spawn
  27.  
  28. function is_elem_in_list(list, elem)
  29.     local i
  30.  
  31.     i = 1
  32.     while i <= #list do
  33.         if list[i] == elem then
  34.             return true
  35.         end
  36.         i = i + 1
  37.     end
  38.     return false
  39. end
  40.  
  41. function get_trash_orientation()
  42.     local addr
  43.  
  44.     addr = sg.localAddress()
  45.     if addr == '1XABWDJZL' then
  46.         TRASH_DIRECTION = 'south'
  47.     end
  48. end
  49.  
  50. function init()
  51.     if AUTO_UPDATE then
  52.         print('Checking updates...')
  53.         if not update() then
  54.             print('Failed to update.')
  55.         else
  56.             print('Updated successfully.')
  57.         end
  58.     end
  59.     sg = peripheral.find('stargate')
  60.     while not sg do
  61.         print('Stargate not found.')
  62.         sleep(SG_WRAP_TRY_SLEEP)
  63.         sg = peripheral.find('stargate')
  64.     end
  65.     --get_trash_orientation()
  66.     --chest = peripheral.wrap(CHEST_WRAP)
  67.     --[[if not chest then
  68.         print('Error, can\'t wrap chest')
  69.         read()
  70.         os.reboot()
  71.     end]]
  72.     print('Switching to FR language...')
  73. end
  74.  
  75. function update()
  76.     local response
  77.     local sResponse
  78.     local file
  79.  
  80.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(PASTE_CODE))
  81.     if not response then
  82.         return false
  83.     end
  84.     sResponse = response.readAll()
  85.     response.close()
  86.     file = fs.open(FILE_PATH, "w")
  87.     file.write(sResponse)
  88.     file.close()
  89.     return true
  90. end
  91.  
  92. function check_input(str, type, free)
  93.     if not str then
  94.         return false
  95.     end
  96.     if type == 'address' then
  97.         --[[if true then
  98.         if (sg.localAddress() == '1XABWDJZL' and str == 'THE_REDCRAFT_PRTOX76') or
  99.            (sg.localAddress() == 'CTIPV3SCJ' and str == 'patronus') or
  100.             str == 'HOYOHOYO' then
  101.             if free then
  102.                 print('Code deja utilise.')
  103.                 return false
  104.             end
  105.             return 1
  106.         end]]
  107.         if not sg.energyToDial(str) or tonumber(sg.energyToDial(str)) <= 4000 then
  108.             print('Adresse invalide.')
  109.             sleep(BAD_ADDRESS_SLEEP)
  110.             return false
  111.         elseif sg.energyAvailable() < tonumber(sg.energyToDial(str)) then
  112.             print('Pas assez d\'energie, (' .. math.floor((sg.energyAvailable() * 100) / tonumber(sg.energyToDial(str))) .. '% de l\'energie requise')
  113.             return false
  114.         end
  115.     elseif type == 'confirmation' then
  116.         str = str:lower()
  117.         if str ~= 'o' and str ~= 'oui' and  str ~= 'y' and  str ~= 'yes' then
  118.             return false
  119.         end
  120.     end
  121.     return true
  122. end
  123.  
  124. function is_payment_done()
  125.     local slot
  126.     local data
  127.     local amount
  128.  
  129.     slot = chest.getInventorySize()
  130.     amount = 0
  131.     chest.condenseItems()
  132.     while slot > 0 do
  133.         data = chest.getStackInSlot(slot)
  134.         if data and data.name == PAYMENT_ITEM_NAME then
  135.             amount = amount + data.qty
  136.         elseif data then
  137.             chest.pushItem(TRASH_DIRECTION, slot)
  138.         end
  139.         slot = slot - 1
  140.     end
  141.     return amount
  142. end
  143.  
  144. function push_payment()
  145.     local slot
  146.     local data
  147.  
  148.     slot = chest.getInventorySize()
  149.     chest.condenseItems()
  150.     while slot > 0 do
  151.         data = chest.getStackInSlot(slot)
  152.         if data and data.name == PAYMENT_ITEM_NAME then
  153.             chest.pushItem(ENDER_CHEST_DIRECTION, slot)
  154.         elseif data then
  155.             chest.pushItem(TRASH_DIRECTION, slot)
  156.         end
  157.         slot = slot - 1
  158.     end
  159.     return amount
  160. end
  161.  
  162. function check_payment(amount)
  163.     local time
  164.     local tmp
  165.  
  166.     time = 0
  167.     while time < PAYMENT_TIMOUT_SECONDS do
  168.         tmp = is_payment_done()
  169.         if tmp >= amount then
  170.             print('Paiement accepte.')
  171.             return true
  172.         end
  173.         print('Payment: ' .. tmp .. '/' .. amount .. ' ' .. PAYMENT_ITEM_NAME)
  174.         time = time + PAYMENT_CHECK_SLEEP_LOOP
  175.         sleep(PAYMENT_CHECK_SLEEP_LOOP)
  176.     end
  177.     return false
  178. end
  179.  
  180. function trade(address)
  181.     local redstone_amount
  182.  
  183.     if is_elem_in_list(FREE_ADRESSES, address) then
  184.         return true
  185.     end
  186.     redstone_amount = ((sg.energyToDial(address) * SU_RF_CONVERSION * REDSTONE_PER_BASE_RF)/ BASE_RF)
  187.     if ((sg.energyToDial(address) * SU_RF_CONVERSION * REDSTONE_PER_BASE_RF)) % BASE_RF ~= 0 then
  188.         redstone_amount = redstone_amount + REDSTONE_PER_BASE_RF
  189.     end
  190.     redstone_amount = math.floor(redstone_amount)
  191.     print("10 secondes d'ouverture coutera " .. redstone_amount ..  ' redstones.')
  192.     print('Voulez vous continuer ? Oui/non')
  193.     if check_input(read(), 'confirmation') then
  194.         print('Vous avez ' .. PAYMENT_TIMOUT_SECONDS .. ' secondes pour jetter vos redstones dans le dropper.')
  195.         return check_payment(redstone_amount)
  196.     end
  197.     return false
  198. end
  199.  
  200. function secure_opening()
  201.     local engaged
  202.  
  203.     engaged = 0
  204.     while engaged < 5 do
  205.         sleep(0.5)
  206.         _, engaged, _ = sg.stargateState()
  207.     end
  208.     sg.closeIris()
  209.     engaged = "Dialling"
  210.     while engaged == "Dialling" or engaged == "Opening" do
  211.         sleep(0.5)
  212.         engaged, _, _ = sg.stargateState()
  213.     end
  214.     sg.openIris()
  215. end
  216.  
  217. function dial(address)
  218.     local success
  219.     local reason
  220.  
  221.     success, reason = sg.dial(address)
  222.     if not success then
  223.         print('')
  224.         print('Impossible d\'etablir un vortex :')
  225.         print(reason)
  226.         print('Vous n\'avez pas ete encaisse, vous pouvez reessayer ou tenter de contacter une autre destination.')
  227.         print('')
  228.         sleep(2)
  229.         return false
  230.     end
  231.     --push_payment()
  232.     secure_opening()
  233.     print("Le vortex se fermera dans " .. TIME_VORTEX_ACTIVATION_SECONDS .. " secondes.")
  234.     sleep(TIME_VORTEX_ACTIVATION_SECONDS)
  235.     sg.disconnect()
  236.     return true
  237. end
  238.  
  239. function event_loop()
  240.     local str
  241.     local arg
  242.     local ret_ci
  243.  
  244.     --print('Entrez une adresse de portail :')
  245.     --str = read('*')
  246.     --str = read()
  247.     --ret_ci = check_input(str, 'address')
  248.     --if ret_ci then
  249.     --    if ret_ci == 1 then
  250.             print('Entrez une adresse de portail :')
  251.             --str = read('*')
  252.             str = read()
  253.             if check_input(str, 'address', true) then
  254.                 dial(str)
  255.             end
  256.     --    elseif trade(str) then
  257.     --        dial(str)
  258.     --    end
  259.     --end
  260.     term.clear()
  261.     term.setCursorPos(1,1)
  262. end
  263.  
  264. function core()
  265.     local state
  266.     local direction
  267.  
  268.     while true do
  269.         state, _, direction = sg.stargateState()
  270.         if state == 'Connected' and direction == 'Outgoing' then
  271.             sg.disconnect()
  272.         end
  273.         event_loop()
  274.         sleep(1)
  275.     end
  276. end
  277.  
  278. init()
  279. core()
Add Comment
Please, Sign In to add comment