Advertisement
Hikooshi

Программа для робота

Sep 28th, 2016
1,272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. --pastebin get UmdpF2ZD probe
  2. local robot = require("robot")
  3. local component = require("component")
  4. local modem = component.modem
  5. local inv_c = component.inventory_controller
  6. local nav = component.navigation
  7. local gen = component.generator
  8. local geo = component.geolyzer
  9. local event = require("event")
  10. local sides = require("sides")
  11. local serialization = require("serialization")
  12. local computer = require("computer")
  13.  
  14.  
  15.  
  16. modem.open(346)
  17. address = component.get(modem.address)
  18. rName = robot.name()
  19.  
  20. authorization = true
  21. oresAndBlocks = {}
  22. analyzes = {}
  23. userAddresses = {}
  24. cmps = {}
  25. result = false
  26. activatorMain = true
  27.  
  28. function robotMoveing()
  29.  
  30. for dst,fnc in pairs(moveings) do
  31. if count == tostring(dst) then
  32. if count == "up" or count == "down" or count == "forward" or count == "back" then
  33. for i = 1, count1 do
  34. fnc()
  35. end
  36. else
  37. fnc()
  38. rFacing = nav.getFacing()
  39. if rFacing == 2 then
  40. rFacing = "North"
  41. elseif rFacing == 3 then
  42. rFacing = "South"
  43. elseif rFacing == 4 then
  44. rFacing = "West"
  45. elseif rFacing == 5 then
  46. rFacing = "East"
  47. end
  48. end
  49. end
  50. end
  51. scanAll()
  52.  
  53. end
  54.  
  55. function rSwing()
  56.  
  57. if robot.swing(tonumber(count1)) then
  58. modem.send(addressUser, 346, "This done")
  59. else
  60. modem.send(addressUser, 346, "Wrong instrument")
  61. end
  62.  
  63. end
  64.  
  65. function rPlace()
  66.  
  67. if robot.place(tonumber(count1)) then
  68. modem.send(addressUser, 346, "This done")
  69. else
  70. modem.send(addressUser, 346, "Can't place this block")
  71. end
  72.  
  73. end
  74.  
  75. function rAnalyze()
  76.  
  77. os.sleep(0.5)
  78. nlz = geo.analyze(tonumber(count1))
  79. modem.send(addressUser, 346, nlz.name.." "..nlz.metadata)
  80.  
  81. end
  82.  
  83. function rRecieve()
  84.  
  85. modem.send(addressUser, 346, serialization.serialize(oresAndBlocks))
  86.  
  87. end
  88.  
  89. function rUse()
  90.  
  91. val, val1 = robot.use(tonumber(count1))
  92. if val then
  93. modem.send(addressUser, 346, val1)
  94. end
  95.  
  96. end
  97.  
  98. function rInventory()
  99.  
  100. slotNumber = tonumber(count2)
  101. for kSlot,vSlot in pairs(slots) do
  102. if count1 == tostring(kSlot) then
  103. itemsInSlot = vSlot(slotNumber)
  104. end
  105. end
  106. if type(itemsInSlot) == "table" then
  107. modem.send(addressUser, 346, "label="..itemsInSlot.label..", ".."maxSize="..tostring(itemsInSlot.maxSize)..", ".."size="..tostring(itemsInSlot.size))
  108. elseif itemInSlot == nil then
  109. modem.send(addressUser, 346, "Slot is empty")
  110. end
  111.  
  112. end
  113.  
  114. function rGenerator()
  115.  
  116. for kGen,vGen in pairs(generatorFuncs) do
  117. if count1 == tostring(kGen) then
  118. genCount = vGen(tonumber(count2))
  119. end
  120. end
  121. modem.send(addressUser, 346, tostring(genCount))
  122.  
  123. end
  124.  
  125. function rEnergy()
  126.  
  127. rNrg = tostring(computer.energy())
  128. modem.send(addressUser, 346, rNrg)
  129.  
  130. end
  131.  
  132. function rEquip()
  133.  
  134. local val = inv_c.equip()
  135. modem.send(addressUser, 346, tostring(val))
  136.  
  137. end
  138.  
  139. generatorFuncs = {
  140. cnt = gen.count,
  141. inst = gen.insert,
  142. rmv = gen.remove}
  143.  
  144. slots = {
  145. internal = inv_c.getStackInInternalSlot,
  146. chest = inv_c.getStackInSlot,
  147. rSelect = robot.select}
  148.  
  149. moveings = {
  150. up = robot.up,
  151. down = robot.down,
  152. forward = robot.forward,
  153. back = robot.back,
  154. left = robot.turnLeft,
  155. right = robot.turnRight,
  156. around = robot.turnAround}
  157.  
  158. actions = {
  159. up = robotMoveing,
  160. down = robotMoveing,
  161. forward = robotMoveing,
  162. back = robotMoveing,
  163. left = robotMoveing,
  164. right = robotMoveing,
  165. around = robotMoveing,
  166. swing = rSwing,
  167. place = rPlace,
  168. analyze = rAnalyze,
  169. recieve = rRecieve,
  170. use = rUse,
  171. inventory = rInventory,
  172. generator = rGenerator,
  173. energy = rEnergy,
  174. equip = rEquip}
  175.  
  176. file2 = io.open("userAddresses", "r")
  177. for line in file2:lines() do table.insert(userAddresses, line) end
  178. --print(userAddresses[#userAddresses])
  179. file2:close()
  180.  
  181. function authorize()
  182.  
  183. while authorization do
  184. local _,addressTo,addressFrom,_,_,message = event.pull(0.1, "modem")
  185. if message == "Authorize me!" then
  186. addressUser = addressFrom
  187. for i = 1, #userAddresses do
  188. if addressUser == userAddresses[i] then
  189. result = true
  190. break
  191. else
  192. result = false
  193. end
  194. end
  195. if not result then
  196. file3 = io.open("userAddresses", "a")
  197. table.insert(userAddresses, addressUser)
  198. file3:write("\n"..addressUser)
  199. file3:close()
  200. print("true")
  201. else
  202. print("false")
  203. end
  204. rFacing = nav.getFacing()
  205. if rFacing == 2 then
  206. rFacing = "North"
  207. elseif rFacing == 3 then
  208. rFacing = "South"
  209. elseif rFacing == 4 then
  210. rFacing = "West"
  211. elseif rFacing == 5 then
  212. rFacing = "East"
  213. end
  214. cmps[1] = robot.inventorySize()
  215. modem.send(addressUser, 346, "Authorization complete", address, rName, serialization.serialize(cmps), rFacing)
  216. authorization = false
  217. print(addressUser)
  218. end
  219. end
  220.  
  221. end
  222.  
  223. file = io.open("oresAndBlocks", "r")
  224.  
  225. for line in file:lines() do
  226. table.insert(oresAndBlocks, line)
  227. end
  228. file:close()
  229.  
  230. function scanAll()
  231.  
  232. --file1 = io.open("oresAndBlocks", "a")
  233.  
  234. for i = 0, 5 do
  235. --if i ~= 2 then
  236. analyzeOp = geo.analyze(i)
  237. for k,v in pairs(oresAndBlocks) do
  238. if analyzeOp.name.." "..analyzeOp.metadata == v then
  239. result = true
  240. break
  241. else
  242. result = false
  243. end
  244. end
  245. if not result then
  246. file1 = io.open("oresAndBlocks", "a")
  247. table.insert(oresAndBlocks, analyzeOp.name.." "..analyzeOp.metadata)
  248. file1:write("\n"..analyzeOp.name.." "..analyzeOp.metadata)
  249. file1:close()
  250. end
  251.  
  252. table.insert(analyzes, analyzeOp.name.." "..analyzeOp.metadata)
  253. --end
  254. end
  255.  
  256. --for i = 1, #analyzes do
  257. --for l = 1, #oresAndBlocks do
  258. --if analyzes[i] == oresAndBlocks[l] then
  259. --result = true
  260. --end
  261. --if not result then
  262. --table.insert(oresAndBlocks, analyzes[i])
  263. --file1:write("\n"..analyzes[i])
  264. --end
  265. --end
  266. --end
  267. for i = 1, #analyzes do
  268. print(analyzes[i])
  269. end
  270. print(#oresAndBlocks)
  271. for i = 1, #oresAndBlocks do
  272. print(oresAndBlocks[i])
  273. end
  274. modem.send(addressUser, 346, serialization.serialize(analyzes), rFacing)
  275. analyzes = {}
  276.  
  277. --file1:close()
  278.  
  279. end
  280.  
  281. function recieving()
  282.  
  283. while activatorMain do
  284. local _,addressTo,addressFrom,_,_,message,message1,message2,message3 = event.pull(0.1, "modem")
  285. if message ~= nil then
  286. if addressFrom == addressUser then
  287. count = message
  288. count1 = message1
  289. count2 = message2
  290. count3 = message3
  291. for func,act in pairs(actions) do
  292. if message == tostring(func) then
  293. act()
  294. end
  295. end
  296. else
  297. for i = 1, #userAddresses do
  298. print(userAddresses[i])
  299. if addressFrom == userAddresses[i] then
  300. addressUser = addressFrom
  301. print(addressUser)
  302. end
  303. end
  304. end
  305. end
  306. end
  307.  
  308. end
  309.  
  310. --scanAll()
  311. authorize()
  312. recieving()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement