Advertisement
VaMinion

Stargate Wireless Reactor Query

Jan 14th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. -- Important: Computer must be next to a stargate computercraft interact block AND have a wireless modem on the back.
  2.  
  3. -- Current status: Handles the initial dial and sends query through. Still need to code far side listener and reply.
  4.  
  5. -- Algorithm:
  6. -- Listen for incoming modem message to send query.
  7. -- Send query through gate via sgSendMessage.
  8. -- Get reply via same method.
  9. -- Send reply out via wireless.
  10.  
  11. shell.run("clear")
  12.  
  13. -- Wrap peripherals
  14. wifi = peripheral.wrap("back")
  15. sg = peripheral.wrap("left")
  16.  
  17. -- Define variables
  18. queryType = "fuel" -- Will eventually be set via remote connection over the modem.
  19. sgState, chevrons = sg.stargateState() -- So we know whether we need to dial or not.
  20. reactorGate = true -- So we know if we need to clean up the gate afterward or not.
  21. reactorAddress = "3DIGTJU78" -- The address of the gate the reactor is at.
  22.  
  23.  
  24. -- First, check to see if the gate is open. If it is, we don't need to dial. If it's not, we need a connection.
  25. -- Check the gate to see if it's idle, remote,
  26. function gateCheck()
  27.  -- May need to shuffle logic around a bit.
  28.  sgState = sg.stargateState()
  29.  if sgState == "Connected" and sg.remoteAddress() == reactorAddress then
  30.   reactorGate = false
  31.   print("Gate is active and connected to correct address.")
  32.  elseif (sgState == "Connected" and sg.remoteAddress() ~= reactorAddress) or sgState ~= "Idle" then
  33.   reactorGate = true
  34.   print("Gate is active and connected to different address. Will dial upon disconnect.")
  35.   while true do
  36.    sgState = sg.stargateState()
  37.    if sgState == "Connected" and sg.remoteAddress() == reactorAddress then
  38.     print("Connected to correct address.")
  39.     reactorGate = true
  40.     break
  41.    elseif sgState == "Idle" then
  42.     print("Gate has gone idle. Dialling.")
  43.     stargateDial()
  44.     break
  45.    end
  46.    os.sleep(2)
  47.   end
  48.  else
  49.   reactorGate = true
  50.   print("Gate is inactive. Dialling.")
  51.   stargateDial()
  52.  end
  53.  print("Pretend the query ran here.")
  54. end
  55.  
  56. -- Custom dialler for this program
  57. function stargateDial()
  58.   -- Need to dial the gate and wait for it to be open before doing any sort of further work.
  59.   sg.dial(reactorAddress)
  60.   event, source, gateStatus, oldStatus = os.pullEvent("sgStargateStateChange")
  61.   while gateStatus ~= "Connected" do
  62.    event, source, gateStatus, oldStatus = os.pullEvent("sgStargateStateChange")
  63.   end
  64.   print("Gate open. Beginning query.")
  65. end
  66.  
  67. function gateQuery()
  68.  print("Sending query: ", queryType)
  69.  sg.sendMessage(queryType)
  70.  -- Receive reply
  71.  --event, where, value = os.pullEvent("sgMessageReceived")
  72. end
  73.  
  74. function gateCleanup(reactorGate)
  75.  if reactorGate then
  76.   print("Closing gate.")
  77.   sg.disconnect()
  78.  end
  79. end
  80.  
  81. gateCheck()
  82. gateQuery()
  83. gateCleanup(reactorGate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement