Advertisement
Axeer

minecraft_robot

Aug 12th, 2020 (edited)
2,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.61 KB | None | 0 0
  1. local robot = require"robot"
  2. local thread = require "thread"
  3. local component = require "component"
  4. local event = require"event"
  5. local serialization = require"serialization"
  6. local filesystem = require "filesystem"
  7. local serialization = require"serialization"
  8. local computer = require"computer"
  9. local modem = component.modem
  10. local port = 512
  11. local robotPort = 1024
  12. local eeprom = component.eeprom
  13. --local navigation = component.navigation
  14. local library = require"library"
  15. local inventory = component.inventory_controller
  16. local gpu = component.gpu
  17. local dig  = loadfile("\\home\\dig.lua")
  18. local farm = loadfile("\\home\\farmScript.lua")
  19. modem.open(port)
  20. modem.open(robotPort)
  21. waypointParam = {}
  22. Control = {}
  23. Algorithms = {}
  24. RobotInventory = {}
  25. mining = {}
  26. local territory = {}
  27. robotItem = {}
  28. --config values
  29.  
  30. local minesize = 20
  31.  mining.turnSide = false
  32.      mining.minedistance =20
  33.      mining.sides = 4
  34.      mining.distanceToPlaceTorch = 16
  35. robotItem.sword = 15
  36. robotItem.torch = 16
  37. robotItem.pickaxe = 14
  38.  
  39. local serverConnecting = false
  40.  
  41. function connect()
  42.     addresses = {}
  43.     local addressesCount = 1
  44.     modem.broadcast(port, "connect")
  45.     for i = 0, 10,1 do
  46.         local _, _, from, _, _, _ = event.pull(1, "modem_message") --pull with timeout of 1 second(listen for 10 times)
  47.    
  48.     if from ~= nil then
  49.     print(from)
  50.  
  51.     addresses[addressesCount] = from
  52.     print(addressesCount)
  53.     addressesCount = addressesCount+1
  54.    
  55. else
  56.     break
  57.     print("nil address")
  58. end end end
  59.  
  60. function receiveSignal()
  61.     local _, _, from, _, _, message = event.pull("modem_message")
  62.     return message
  63. end
  64. function sendToServer(dataString, pathToSave, fileOptions)
  65.  --local filePath,dataString = read(file)
  66.  if (pathToSave == nil or pathToSave == _) then
  67. pathToSave = "temp"
  68. fileOptions = "add"
  69.  end
  70.  if (fileOptions == "add") then
  71. dataString = dataString.."$ "
  72.  end
  73. modem.send(variableCurrentAddress, port, "receiveFile")--begin
  74. modem.send(variableCurrentAddress, port, pathToSave) --path
  75. modem.send(variableCurrentAddress, port, fileOptions) --opt
  76. os.sleep(0.5)
  77. modem.send(variableCurrentAddress, port, dataString) --data
  78. end
  79.  
  80. function inventoryCount()
  81. local INVENTORY = {}
  82. for i =1, 16, 1 do
  83. table.insert(INVENTORY,(robot.count(i)))
  84. return INVENTORY
  85. end end
  86.  
  87. function toolDurability()
  88.     local TOOL = {[1] = robot.durability()}
  89.     return TOOL[cur]
  90.    
  91. end
  92.  
  93. function sendLogToServer()
  94. local toLog = {computer.energy(),
  95. computer.maxEnergy(),
  96. computer.freeMemory(),
  97. toolDurability(),
  98. inventoryCount(),
  99. }
  100.  
  101. local fileString = serialization.serialize(toLog)
  102. print("pick the path to send file to server")
  103. sendFileToServer(fileString, io.read())
  104. end
  105.  
  106. function detectLeft()
  107. robot.turnLeft()
  108. bobot.detect()
  109. robot.turnRight()
  110. end
  111. function detectRight()
  112. robot.turnRight()
  113. bobot.detect()
  114. robot.turnLeft()
  115. end
  116.  
  117. function oreAnalyze()
  118. local currentOre = nil
  119. local analyzeSide = nil
  120. local analyzedOreCoordinatres = {
  121.       [1] = {analyzedOreCoordinatreX},
  122.       [2] ={analyzedOreCoordinatreY},
  123.       [3] = {analyzedOreCoordinatreZ}
  124.     }
  125. local availableAnalyze = {
  126.     [1] = robot.detect(),
  127.     [2] = robot.detectUp(),
  128.     [3] = robot.detectDown(),
  129.     [4] = detectLeft(),
  130.     [5] = detectRight(),
  131. }
  132. local isBlockOnSide = nil
  133. local availableSides = 5 -- front up down left right
  134. for counter = 0, availableSides do
  135. --check have a block the front of robot
  136. isBlockOnSide, currentOre = analyzedOreCoordinatres[counter]
  137. sendToServer(isBlockOnSide, "analyzedBlocks", "add")
  138. sendToServer(currentOre, "analyzedBlock", "add")
  139. end end
  140. function mining.robotcolumn()
  141. robot.swingUp()
  142. robot.swingDown()
  143. robot.swing()
  144.     end
  145.  
  146. function dequipItems()
  147.  
  148.  
  149.  
  150. end
  151.  
  152. function mineForward()
  153.         mining.distanceToPlaceTorch = mining.distanceToPlaceTorch- 1
  154.         if  not robot.swing()then
  155.             print("сука хуйни понаставили Ълятт")
  156.             robot.swingUp()
  157.             robot.up()
  158.         end
  159.         robot.swingUp()
  160.         robot.swingDown()
  161.         if not robot.forward() then
  162.             print("застрял сука!!!!")
  163.             robot.swing()
  164.         end
  165.         if mining.distanceToPlaceTorch ==0then
  166.             robot.turnRight()
  167.             robot.swing()
  168.             robot.select(robotItem.torch)
  169.             robot.place(_,true)
  170.             robot.turnLeft()
  171.             mining.distanceToPlaceTorch = 16
  172.         end
  173.             mining.minedistance = mining.minedistance-1
  174.     end
  175.  
  176. function mining.turn(turnside)
  177.     if not turnside then
  178.         mining.turnSide = true
  179.         robot.turnRight()
  180.     elseif turnside then
  181.         mining.turnSide = false
  182.         robot.turnLeft()
  183.     end
  184. end
  185. --main
  186. function maketunnel()
  187.     for i =0 ,mining.sides do
  188.  
  189.         for j = 0, mining.minedistance do
  190.             if robot.detect() then
  191.                 print("хуярю столб")
  192.                 mineForward()
  193.             else
  194.              if robot.forward() then
  195.              print("астарожна воздух")
  196.              else
  197.                 print("что это?")
  198.                 for i =0, 10, 1 do
  199.                     robot.swing()
  200.                 end
  201.              end
  202.         end
  203.         --mining.minedistance = mining.minedistance-1
  204.        
  205.     end
  206.     robot.turnLeft()
  207. end
  208. end
  209.  
  210. function territoryBURNER()
  211. local x, y, z = 0
  212. size = {10, 10, 10}
  213. for h = 0, size[3] do
  214. for j =0 ,size[2] do
  215. for i=0, size[1] do
  216.     robot.swing()
  217.     robot.forward()
  218.     size[1] = size[1] -1
  219. end
  220.  if size[1]%2 ~= 0 then
  221.     robot.turnLeft()
  222.  else  robot.turnRight()
  223.  end
  224.  size[2] = size[2]+1
  225.  if size[2] == 0 then
  226.     size[3] = size[3] +1
  227.     robot.swingDown()
  228.     robot.down()
  229.  end
  230. end
  231. end
  232. end
  233.  
  234. local function remoteControl()
  235.     while true do
  236. --  local _, _, from, _, _, message = event.pull("modem_message")
  237.         local e = {event.pull()}
  238.     if e[1] == "modem_message" then
  239.         if e[4] == robotPort then
  240.                 if e[6] then print(e[6])
  241.                 if e[6] == "moveUp" then
  242.                         robot.up()
  243.                     elseif e[6] == "moveDown" then
  244.                         robot.down()
  245.                     elseif e[6] == "moveForward" then
  246.                         if  not robot.forward() then
  247.                         while robot.detect() do
  248.                          robot.swing()
  249.                         end
  250.                         robot.forward()
  251.                         end
  252.                     elseif e[6] == "moveBack" then
  253.                         robot.back()
  254.                     elseif e[6] == "moveLeft" then
  255.                         robot.turnLeft()
  256.                     elseif e[6] == "moveRight" then
  257.                         robot.turnRight()
  258.                     elseif e[6] == "analyze" then
  259.                         isPassing, detectedBlock = robot.detect()
  260.                         modem.broadcast(robotPort, detectedBlock)
  261.                     elseif e[6] == "checkKey" then
  262.                         checkkey()
  263.                     elseif e[6] == "activate" then
  264.                         robot.use()
  265.                     elseif e[6] == "OTSOS" then
  266.                         for i = 1, (inventory.getInventorySize(0) or 1) do
  267.                             inventory.suckFromSlot(0, 1)
  268.                         end
  269.                         for i = 1, (inventory.getInventorySize(1) or 1) do
  270.                             inventory.suckFromSlot(1, 1)
  271.                         end
  272.                     elseif e[6] == "swing" then
  273.                         robot.swing()
  274.                     elseif e[6] == "use" then
  275.                         robot.use()
  276.                     elseif e[6] == "VIBROSI" then
  277.                         for i = 1, robot.inventorySize() do
  278.                             robot.select(1)
  279.                             robot.drop(64)
  280.                         end
  281.                     elseif e[6] == "analyze" then
  282.                         oreAnalyze()
  283.                     elseif e[6] == "maketunnel" then
  284.                         maketunnel()
  285.                     elseif e[6] == "end" then
  286.                         return "end"
  287.                     elseif e[6] == "moveSpeedUp" then
  288.                         moveSpeed = moveSpeed + 0.1
  289.                         print("SKOROST + NA 0.1")
  290.                     elseif e[6] == "moveSpeedDown" then
  291.                         moveSpeed = moveSpeed - 0.1
  292.                         if moveSpeed < 0.1 then moveSpeed = 0.1 end
  293.                         print("SKOROST - NA 0.1")
  294.                 end
  295.             end
  296.         end
  297.     end
  298. end end
  299.  
  300. function algorithmControl()
  301.  
  302. --get signal
  303. local _, _, _, _, _, message = event.pull("modem_message")
  304. if (message == "mine")then
  305.     Algorithms.mine()
  306. else if (message == "end") then
  307.     return
  308. end end end
  309.  
  310. local function getAlgorithms()
  311.     local serializedTable = serialization.serialize(Algorithms)
  312. for i = 0, #Algorithms, 1 do
  313. print(i .. ".".. " ".. Algorithms[i])
  314. end end
  315.  
  316. --добавить прерывание
  317. --10 блоков пройдено  = 32 блоков собрано
  318.  
  319. ----------------------------------------------------------------------------------------------------|
  320. --  execute begin                                                                                   --
  321. ----------------------------------------------------------------------------------------------------|
  322. if serverConnecting then
  323.     connect()
  324.     variableCurrentAddress = currentAddress()
  325. end
  326. while true do
  327.     print("enter a name of robot function")
  328. local signal = receiveSignal()
  329. if signal == "remoteControl" then
  330.     print("start controlling")
  331.      remoteControl()
  332. elseif signal == "algorithmControl" then
  333.     algorithmControl()
  334. --elseif signal == "maketunnel" then
  335. --  print("making tunnel")
  336. --   maketunnel()
  337. elseif signal == "dig" then
  338.     digLayer(9)
  339. elseif signal == "farm" then
  340.     api.farmStart()
  341. else print("no command or parameter find")
  342. end end
  343. ----------------------------------------------------------------------------------------------------|
  344. --  execute end                                                                                     --
  345. ----------------------------------------------------------------------------------------------------|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement