Advertisement
hkbruvold

turtleserver01.lua

Apr 29th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. local id = os.getComputerID()
  2. local clientList = {}
  3.  
  4. rednet.open("top")
  5.  
  6. function save(name, data)
  7.     local file = fs.open(name, "w")
  8.     file.write(textutils.serialize(data))
  9.     file.close()
  10. end
  11.    
  12. function load(name)
  13.     local file = fs.open(name, "r")
  14.     local data = file.readAll()
  15.     file.close()
  16.     return textutils.unserialize(data)
  17. end
  18.  
  19. if fs.exists("clientlist") then
  20.     clientList = load("clientlist")
  21. end
  22.  
  23. while true do
  24.     write("$ ")
  25.     local commandText = read()
  26.     local command = {}
  27.     for word in commandText:gmatch("%S+") do
  28.         table.insert(command, word)
  29.     end
  30.    
  31.     if command[1] == "listen" then
  32.         id, message = rednet.receive()
  33.         if message == "me turtle, you server" then
  34.             local newClient = true
  35.             for k,v in pairs(clientList) do
  36.                 if id == v then
  37.                     newClient = false
  38.                 end
  39.             end
  40.             if newClient then
  41.                 table.insert(clientList, id)
  42.             end
  43.             save("clientlist", clientList)
  44.             print("Client ID "..tostring(id).." connected")
  45.         end
  46.     elseif command[1] == "list" then
  47.         for k,v in pairs(clientList) do
  48.             print(tostring(k)..": "..tostring(v))
  49.         end
  50.     elseif command[1] == "id" then
  51.         print(os.getComputerID())
  52.     elseif command[1] == "exit" then
  53.         return
  54.     elseif command[1] == "help" then
  55.         print("id: print server ID")
  56.         print("listen: wait for new turtle to connect")
  57.         print("list:   list all connected clients")
  58.         print("use [client ID] [command] to send command")
  59.         print("send commands:")
  60.         print("move home: move turtle to home")
  61.         print("move [x] [y] [z]: move turtle to coordinate")
  62.         print("mine [x1] [y1] [z1] [x2] [y2] [z2]: mine area")
  63.         print("setposition [x] [y] [z]: reset internal coordinate")
  64.         print("sethome: set current location as home")
  65.         print("resume: resume interrupted mining")
  66.     elseif tonumber(command[1]) ~= nil then
  67.         local message = ""
  68.         if command[2] == "move" then
  69.             if command[3] == "home" then
  70.                 message = "move home"
  71.             else
  72.                 message = "move "..command[3].." "..command[4].." "..command[5]
  73.             end
  74.         elseif command[2] == "mine" then
  75.             message = "mine"
  76.             for i = 3, 8 do
  77.                 message = message.." "..command[i]
  78.             end
  79.         elseif command[2] == "sethome" then
  80.             message = "sethome"
  81.         elseif command[2] == "resume" then
  82.             message = "resume"
  83.         elseif command[2] == "setposition" then
  84.             message = "setposition "..command[3].." "..command[4].." "..command[5].." "..command[6]
  85.         end
  86.         rednet.send(tonumber(command[1]), message)
  87.     end
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement