DragonZBW

turtleClient

Apr 16th, 2021 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. -- PASTE 2TLTxYmF
  2. local modem = peripheral.find("modem")
  3. modem.open(1)
  4.  
  5. -- Check connection of this turtle
  6. local function checkConnection(channel, replyChannel, params, dist)
  7. modem.transmit(replyChannel,channel,"CheckConnection true")
  8. end
  9.  
  10. -- Get this turtle's local map
  11. local function getMap(channel, replyChannel, params, dist)
  12. if ~fs.exists("map") then
  13. local file = fs.open("map","w")
  14. file.write("0 0")
  15. file.close()
  16. modem.transmit(replyChannel,channel,"GetMap 0 0")
  17. else
  18. local file = fs.open("map","r")
  19. modem.transmit(replyChannel,channel,"GetMap " .. file.readAll())
  20. file.close()
  21. end
  22. end
  23.  
  24. -- Set this turtle's local map
  25. local function setMap(channel, replyChannel, params, dist)
  26. local file = fs.open("map","w")
  27. local content = ""
  28. for i,v in ipairs(params) do
  29. content = content .. v
  30. end
  31. file.write(content)
  32. file.close()
  33. end
  34.  
  35. -- Move to absolute coordinates
  36. local function goAbsolute(channel, replyChannel, params, dist)
  37.  
  38. end
  39.  
  40. -- Move to relative coordinates
  41. local function goRelative(channel, replyChannel, params, dist)
  42.  
  43. end
  44.  
  45. -- Explore and map surroundings
  46. local function explore(channel, replyChannel, params, dist)
  47.  
  48. end
  49.  
  50. local COMMANDS = {
  51. {name = "CheckConnection", func = checkConnection},
  52. {name = "GetMap", func = getMap},
  53. {name = "SetMap", func = setMap}
  54. {name = "GoAbsolute", func = goAbsolute},
  55. {name = "GoRelative", func = goRelative},
  56. {name = "Explore", func = explore}
  57. }
  58.  
  59. -- Main program logic
  60. while true do
  61. -- Wait for commands from server
  62. local event, peripheral_name, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  63. -- When a command is received, look through the COMMANDS table to see if it is valid
  64. local commandName = string.gsub(string.sub(message, 1, string.find(message, " ") or string.len(message)), " ", "")
  65. for i, v in ipairs(COMMANDS) do
  66. -- If the command is found, check for params then execute
  67. if v.name == commandName then
  68. -- Check for params
  69. local spaceI = string.find(message, " ")
  70. local params = {}
  71. if spaceI then
  72. local p = ""
  73. for i = spaceI + 1, string.len(message) do
  74. local curChar = string.sub(message, i, i)
  75. if curChar ~= " " then
  76. p = p .. curChar
  77. else
  78. params[#params + 1] = p
  79. p = ""
  80. end
  81. end
  82. params[#params + 1] = p
  83. end
  84. -- Execute the command
  85. v.func(channel, replyChannel, params, distance)
  86. end
  87. end
  88. end
Add Comment
Please, Sign In to add comment