Advertisement
Axeer

minecraft_robot_remote_controller

Aug 12th, 2020
2,386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.85 KB | None | 0 0
  1. local component = require "component"
  2. local serialization = require"serialization"
  3. local thread = require"thread"
  4. local computer = require"computer"
  5. local filesystem = require"filesystem"
  6. local modem = component.modem
  7. local gpu = component.gpu
  8. port = 512
  9. robotPort = 1024
  10. local event = require "event"
  11. local modemAddress = component.modem.address
  12. local eeprom = component.eeprom
  13. local w, h = gpu.getResolution()
  14. local eepromSize = nil
  15. local isPrepareFinished = false
  16. local library = loadfile "library"
  17. local gui = loadfile("GUIlibrary.lua")
  18. --local farm = require("farm")
  19. --config values
  20. local connectingToServer = false
  21. local keys = {
  22.     [17] = "moveForward",   --W
  23.     [31] = "moveBack",      --S
  24.     [30] = "moveLeft",      --A
  25.     [32] = "moveRight",     --D
  26.     [42] = "moveDown",      --SHIFT
  27.     [57] = "moveUp",        --SPACE
  28.     [18] = "OTSOS",         --E
  29.     [33] = "VIBROSI",       --F
  30.     [16] = "analyze",       --Q
  31.     [19] = "checkKey",      --R
  32.     [20] = "activate",      --T
  33.     [51] = "end",           --,
  34.     [52] = "analyze",       --.
  35.     [53] = "maketunnel"     --/
  36. }
  37. Colors = {Black = 0x000000, White = 0xFFFFFF, Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF, Yellow = 0xFFDB00}
  38. local addresses = {}
  39. local Options = {Server = {}}
  40. local Figures = {}
  41.  
  42.  
  43.     modem.open(port)
  44.     modem.open(robotPort)
  45.  
  46. function Options.communicateWithServer()
  47.  
  48.     modem.send(variableCurrentAddress, port, (io.read()))
  49.     repeat
  50.     local _, _, from, port, _, message = event.pull("modem_message")
  51.     print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message))
  52. until message == "end"
  53. print ("end of cycle")
  54. end
  55.  
  56. function countMemory()
  57.    
  58.     print(computer.freeMemory() .. '/' .. computer.totalMemory())
  59. end
  60.  
  61. function Options.ShowDEBUGInfo()
  62.     gpu.setBackground(Colors.Yellow)
  63. gpu.fill(140, 0, w, h, " ")
  64.     gpu.setForeground(Colors.Black)
  65. gpu.set(140, 4,tostring(computer.freeMemory() .. '/' .. computer.totalMemory()))
  66. end
  67.  
  68. function prepare()
  69.  
  70.     gpu.setBackground(0x0000FF)
  71.     gpu.setForeground(0xFF0000)
  72.    
  73.      if (gpu.fill(1, 1, w, h, " ")) == true then
  74.         isPrepareFinished = true
  75.     return isPrepareFinished
  76. else
  77.     isPrepareFinished = false
  78.     return isPrepareFinished
  79. end
  80. end
  81.  
  82. function test(isPrepareFinished)
  83.         local eepromLabel = "eepromLabel"
  84.         local  eepromByteArray = "eepromByteArray"
  85.          local eepromSize = "eepromSize"
  86.          local eepromDataSize = "eepromDataSize"
  87.          local eepromChecksum = "eepromChecksum"
  88.         EEPROMdata = {
  89.         [eepromLabel] = eeprom.getLabel(),
  90.         [eepromByteArray] = eeprom.get(),
  91.         [eepromSize] = eeprom.getSize(),
  92.         [eepromDataSize] = eeprom.getDataSize(),
  93.         --[eepromChecksum] = eeprom.getChecksum()
  94.     }
  95.         local fileWriteName = "/home/eepromInfo"
  96.         print("label of the eeprom: "..EEPROMdata[eepromLabel])
  97.         print("size the eeprom: "..EEPROMdata[eepromSize])
  98.         print("dataSize the eeprom: "..EEPROMdata[eepromDataSize])
  99.         --print("checksum the eeprom: "..EEPROMdata[eepromChecksum])
  100.         print("send the log on server?[Y\\N]")
  101.         if ((io.read() or "n").."y"):match("^%s*[Yy]") then
  102.     print("\nSending!\n")
  103.     print("enter a name of file to send")
  104. local fileString = serialization.serialize(EEPROMdata)
  105. --if (modem.send(variableCurrentAddress, port, fileString)) ==true then
  106.     sendToServer(fileString, io.read())
  107.     print("file has been successfully sended"..fileString)
  108.  
  109. end
  110.     end
  111.  
  112.     function read(filePath)
  113. local fileRead = {"data"}
  114. local Path = ""
  115. local data = ""
  116. table.insert(fileRead,(io.open(filePath, "rb")))
  117. --fileRead[Path] = io.open(filePath, "rb")
  118. fileRead[data] = fileRead[2]:read("*a")
  119. print(fileRead[data])
  120. local fileString = serialization.serialize(fileRead[data]) 
  121. return {filePath,fileString}
  122. end
  123.  
  124.     function writeInEEPROM(byteArray)
  125.         eeprom.set(byteArray)
  126. end
  127.  
  128. function Options.cleanup()
  129.     local previosBackground = gpu.setBackground(0x000000)
  130.      gpu.fill(1, 1, w, h, " ")
  131.  
  132.      print(previosBackground)
  133. end
  134.  
  135. function sendToServer(file, pathToSave)
  136.     modem.send(variableCurrentAddress, port, "receiveFile")--begin
  137.     modem.send(variableCurrentAddress, port, pathToSave) --path
  138.     os.sleep(0.5)
  139.     modem.send(variableCurrentAddress, port, file) --data
  140. end
  141. --[[function options()
  142. while true do
  143.      local e = {event.pull()}
  144.      if e[1] == "key_down" then
  145.             local currentOption = tonumber((e[4] -1))
  146. end]]--
  147.  
  148. local function robotControl()
  149.     --modem.open(robotPort)
  150.     modem.broadcast(robotPort, "remoteControl")
  151. while true do
  152.      local e = {event.pull()}
  153.     if e[1] == "key_down" then
  154.         if keys[e[4]] ~= nil then
  155.             if keys[e[4]] ~= "end" then
  156.  
  157.         print("KOMANDA robotu: " .. keys[e[4]])
  158.         modem.broadcast(robotPort, keys[e[4]])
  159.             else modem.broadcast(robotPort, "end") break
  160.  
  161.             end
  162.         end
  163.     else if e[1] == "touch" then
  164.         local _, _, x, y, button, playerName = event.pull()
  165.         if button == 0 then
  166.         modem.broadcast(robotPort, "swing")
  167.     else if button == 1 then
  168.         modem.broadcast(robotPort, "use")
  169.     end end end end
  170. end
  171. end
  172.  
  173. local function robotOSControl()
  174.     while true do
  175.     local OsControlSignal = io.read()
  176.     if OsControlSignal == "end" then
  177.     break
  178. else
  179.     modem.broadcast(robotPort, OsControlSignal)
  180. end end end
  181.  
  182. local function robotAlgorithm()
  183.     modem.broadcast(robotPort, "algorithmControl")
  184. while true do
  185.     local OsAlgorithmSignal = io.read()
  186.     if OsAlgorithmSignal == "end" then
  187.     break
  188. else
  189.     modem.broadcast(robotPort, OsAlgorithmSignal)
  190. end end end
  191.  
  192. local function maketunnel()
  193.     modem.broadcast(robotPort, "maketunnel")
  194. end
  195. local function senddig()
  196.     modem.broadcast(robotPort, "dig")
  197.     end
  198. local function mRecord()
  199.     local endRec = false
  200.     while not endRec do
  201.         local e = {event.pull()}
  202.         if e[1] == "key_down" then
  203.             if e[3] ~= nil then
  204.                 print(e[3] .. "->key pressed\t" .. e[4] .."->is code")
  205.                 if e[3] ~= keyboard.keys.numpad0 then
  206.                     print("\n do you want to associadethis button with some robot operation?[Y\\N]")
  207.                     if ((io.read() or "n").."y"):match("^%s*[Yy]") then
  208.                         print("\nwhat operation must robot on this button?")
  209.                         local strRobOp = io.read()
  210.                             if strRobOp ~= nil then
  211.                                 for k, v in keys do
  212.                                     if keys[k] == strRobOp then
  213.                                         --перезапись таблицы по ключу
  214.                                         keys[k] = nil
  215.                                         keys[e[3]] = strRobOp
  216.                                 end
  217.                             end
  218.                         else
  219.                             print("\nyou don't enter the string. returning...")
  220.                             return
  221.                         end
  222.                     end
  223.                 else
  224.                 print("\n record interrupted. exiting")
  225.                 return
  226.                 end
  227.             end
  228.         end
  229.     end
  230. end
  231. -----------------------------------------------------------------------------------------------|
  232. -----------------------------------------------------------------------------------------------|
  233.     if connectingToServer then
  234.     connect()
  235.     variableCurrentAddress = currentAddress() end
  236. while true do
  237. print("pick the option")
  238. local chose = io.read()
  239.     if chose == "communicate" then
  240.     Options.communicateWithServer()
  241. else if chose == "show" then
  242.     Options.ShowDEBUGInfo()
  243. else if chose == "ram" then
  244.     countMemory()
  245. else if chose == "test" then
  246.     prepare()
  247.     test(isPrepareFinished)
  248. else if chose == "write" then
  249.     print("choose file to write data in EEPROM(full path)")
  250.     fileToRead = io.read()
  251.     local byteArray = read(fileToRead)
  252.     writeInEEPROM(byteArray)
  253.     print("data successfully writed")
  254. else if chose == "clean" then
  255.     Options.cleanup()
  256. else if chose == "rctrl" then
  257.     print ("connecting to the robot")
  258.     robotControl()
  259. else if chose == "oscontrol" then
  260.     robotOSControl()
  261. else if chose == "robotalgorithm" then
  262.     robotAlgorithm()
  263. else if chose == "maketunnel" then
  264.     maketunnel()
  265. else if chose == "dig" then
  266.     senddig()
  267. else if chose == "mrec" then
  268.     print("\nnow trying record macross for robot")
  269.     mRecord()
  270. else if chose == "farm" then
  271.     print("\nstart farming")
  272.     sendCommand(farm)
  273. end end end end end end end end end end end end end end
  274. -----------------------------------------------------------------------------------------------|
  275. -----------------------------------------------------------------------------------------------|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement