Advertisement
Parlocameon

Starcraft Controller Script

Dec 30th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. --ComputerCraft StarGate Control, made by Parlocameon
  2.  
  3. --Variables
  4. SMVERSION = "1.4"
  5. peripheralName = "stargate_2"
  6. defaultTermColor = colors.blue
  7. --Init
  8. function split(input)
  9.  splitTable = {}
  10.  for match in string.gmatch(input,"[^%s]+") do
  11.      table.insert(splitTable,match)
  12.  end
  13.  return splitTable
  14. end
  15.  
  16. function c_print(text,color)
  17.     term.setTextColor(color)
  18.     print(text)
  19.     term.setTextColor(defaultTermColor)
  20. end
  21.  
  22. term.setTextColor(defaultTermColor)        
  23.  
  24. stargate = peripheral.wrap(peripheralName)
  25. print("StarGate Management v"..SMVERSION)
  26. print()
  27.  
  28. addressTable = {}
  29.  
  30. if not fs.exists("stargate/") then
  31.     fs.makeDir("stargate")
  32.     print("Made stargate dir")
  33. end
  34.  
  35. if fs.exists("stargate/address.table") then
  36.     local fH = fs.open("stargate/address.table","r")
  37.     addressTable = textutils.unserialize(fH.readAll())
  38.     fH.close()
  39.     c_print("Addresses imported successfully!",colors.green)
  40.    
  41. else
  42.    
  43.     c_print("You're missing an address table!",colors.red)
  44.     c_print("Please use assign <name> <address> on",colors.red)
  45.     c_print("each address you would like to include",colors.red)
  46.     c_print("and run the export command",colors.red)
  47.    
  48. end
  49.  
  50. while true do
  51.     print()
  52.     command = read()
  53.     print()
  54.     commandArgs = split(command)
  55.    
  56.     if commandArgs[1] == "dial" then
  57.         address = string.sub(command,6)
  58.         if stargate.energyToDial(address) > stargate.energyAvailable() then
  59.             c_print("You have insufficient power",colors.red)
  60.             c_print("In order to dial this address you need "..tostring(energyToDial()).."RF",  colors.red)
  61.             c_print("The current available power is "..tostring(energyAvailable()).."RF", colors.red)            
  62.         else    
  63.             print("Dialing "..address)
  64.             stargate.dial(address)
  65.         end
  66.        
  67.     elseif command == "disconnect" then
  68.         c_print("Disconnected Stargate",colors.green)
  69.         stargate.disconnect()
  70.        
  71.     elseif commandArgs[1] == "assign" then
  72.     table.insert(addressTable,{ name = commandArgs[2], address = commandArgs[3]})
  73.     print("Assigned name "..commandArgs[2].." to address "..commandArgs[3])
  74.    
  75.     elseif command == "listGates" then
  76.             for k,table in pairs(addressTable) do
  77.                 print("ID: "..k.." Name: "..table.name.." Address: "..table.address)
  78.             end
  79.     elseif commandArgs[1] == "qdial" then
  80.    
  81.        qDTable = addressTable[tonumber(commandArgs[2])]
  82.        if stargate.energyToDial(qDTable.address) > stargate.energyAvailable() then
  83.             c_print("You have insufficient power",colors.red)
  84.             c_print("In order to dial "..qDTable.name.." you need "..tostring(stargate.energyToDial(qDTable.address)).."RF", colors.red)
  85.             c_print("The current available power is "..tostring(stargate.energyAvailable()).."RF",colors.red)
  86.        else
  87.             print("Dialing "..qDTable.name)
  88.             stargate.dial(qDTable.address)
  89.        end
  90.            
  91.     elseif command == "export" then
  92.         local fH = fs.open("stargate/address.table","w")
  93.         fH.writeLine(textutils.serialize(addressTable))
  94.        
  95.         fH.close()
  96.         c_print("Exported addresses successfully",colors.green)        
  97.     end
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement