Advertisement
Guest User

sgdial

a guest
Mar 31st, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.19 KB | None | 0 0
  1. -- SGDial by zenithselenium - ÂĽA9 2014
  2.  
  3. -- Configuration
  4. -- Notes:
  5. --   * 'sides' are from the perspective of the way the turtle is facing
  6. --   * DIR_FROM_GATE wants the turtle's position relative to the gate.
  7. --     e.g. "The turtle is west of the gate"
  8.  
  9.  
  10. function loadConfig()
  11.   local out = {}
  12.   if not fs.exists("SGD_config.txt") then
  13.     print("Could not find config file. Please reinstall SGDial.")
  14.     return nil
  15.   end
  16.   local config = fs.open("/SGD_config.txt","r")
  17.   out.fuel = config.readLine()
  18.   print("Fuel chest: "..out.fuel)
  19.   out.sg_side   = config.readLine()
  20.   print("stargate: "..out.sg_side)
  21.   out.dir_from_gate  = config.readLine()
  22.   print("Direction from gate: "..out.dir_from_gate)
  23.  
  24.   return out
  25. end
  26.  
  27. local FUEL_CHEST_SIDE = ""
  28. local DIR_FROM_GATE = ""
  29.  
  30. local state = {["Idle"] = 0, ["Dialling"] = 1, ["Connected"] = 2}
  31.  
  32. function printHeader()
  33.   shell.run("clear")
  34.   print("#######################################")
  35.   print("#                                     #")
  36.   print("#                SGDial               #")
  37.   print("#          by: zenithselenium         #")
  38.   print("#                                     #")
  39.   print("#######################################")
  40.   print()
  41. end
  42.  
  43. function split(str, delim, maxNb)
  44.   -- Eliminate bad cases...
  45.   if string.find(str, delim) == nil then
  46.     return { str }
  47.   end
  48.   if maxNb == nil or maxNb < 1 then
  49.     maxNb = 0    -- No limit
  50.   end
  51.   local result = {}
  52.   local nb = 0
  53.   local lastPos
  54.   local working = str
  55.   local continue = true
  56.   while continue do
  57.     local i = string.find(working, delim)
  58.     if i == nil then
  59.       break
  60.     end
  61.   --  print("Delimeter found at position "..i)
  62.     local part = string.sub(working, 1, i-1)
  63.     nb = nb + 1
  64.   --  print("part "..nb..": "..part)
  65.     working = string.sub(working, i+1)
  66.   --  print("Remaining: "..working)
  67.     result[nb] = part
  68.     if nb == maxNb then break end
  69.   end
  70.   -- Handle the last field
  71.   if nb ~= maxNb then
  72.     result[nb + 1] = working
  73.   end
  74.   --for count = 1, #result do
  75.   -- print(result[count])
  76.   --end
  77.   return result
  78. end
  79.  
  80. dirSuck = {
  81.   ["front"] = turtle.suck,
  82.   ["top"] = turtle.suckUp,
  83.   ["bottom"] = turtle.suckDown
  84. }
  85.  
  86. function dial(id, addr)
  87.   if (sg.getStackInSlot(1) == nil) then
  88.     print("No fuel in gate.")
  89.     if turtle.getItemCount(1) == 0 then
  90.       print("No fuel in turtle!")
  91.       if dirSuck[FUEL_CHEST_SIDE]() then
  92.         print("got fuel from chest.")
  93.       else
  94.         print("no fuel in chest either")
  95.         print(id)
  96.         rednet.send(id, "nofuel")
  97.         return false
  98.       end
  99.     end
  100.     sg.pullItem(DIR_FROM_GATE, 1, 1)
  101.     print("Placed fuel in gate.")
  102.   end
  103.   sg.connect(addr)
  104. end
  105.  
  106. function close()
  107.   sg.disconnect()
  108. end
  109.  
  110. printHeader()
  111.  
  112. function run()
  113.   local config = loadConfig()
  114.   if config == nil then
  115.     return
  116.   end
  117.   sg = peripheral.wrap(config.sg_side)
  118.   rednet.open("left")
  119.   FUEL_CHEST_SIDE = config.fuel
  120.   DIR_FROM_GATE = config.dir_from_gate
  121.   turtle.select(1)
  122.  
  123.   while true do
  124.     local id, msg, dis = rednet.receive()
  125.   --  print("message received: "..msg)
  126.     local msgArr = split(msg, "|", 0)
  127.     stat1321 = sg.getState()
  128.     local status = state[stat1321]
  129.   --  print(status)
  130.   --  print("Message array:")
  131.   --  for i=1,#msgArr do
  132.   --    print(i..": "..msgArr[i])
  133.   --  end
  134.     if msgArr[1] == "dial" then
  135.       if status == 0 then
  136.         print(msgArr[2])
  137.         if pcall(dial, id, msgArr[2]) then
  138.           rednet.send(id, "dialing")
  139.         else
  140.           print("Unable to dial. Check fuel and verify address.")
  141.           rednet.send(id, "unable")
  142.         end
  143.       else
  144.         print("Stargate active; Closing womhole")
  145.         pcall(close)
  146.         --sleep(7)
  147.         pcall(dial, id, msgArr[2])
  148.         rednet.send(id, "notidle")
  149.       end
  150.     elseif msgArr[1] == "close" then
  151.       if status ~= 0 then
  152.         if pcall(close) then
  153.           print("Closing wormhole")
  154.         else
  155.           print("Could not close wormhole!")
  156.         end
  157.       else
  158.         print("No wormhole to close!")
  159.       end
  160.     elseif msgArr[1] == "ping" then
  161.       print("Receieve ping from #"..id)
  162.       rednet.send(id,"pong")
  163.     end
  164.   end
  165. end
  166.  
  167. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement