Advertisement
Hikooshi

Программа для компьютера

Sep 28th, 2016
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.43 KB | None | 0 0
  1. --pastebin get 1bqpedUA rcRobot
  2. local component = require("component")
  3. local modem = component.modem
  4. local gpu = component.gpu
  5. local event = require("event")
  6. local unicode = require("unicode")
  7. local term = require("term")
  8. local serialization = require("serialization")
  9.  
  10.  
  11.  
  12. modem.open(346)
  13.  
  14.  
  15.  
  16. activatorSearch = true
  17. result = false
  18. namesAndAddresses = {}
  19. analyzes = {}
  20. rCmps = {}
  21. columnPositions = {1, 5, 9, 13}
  22. textures = {}
  23. filler = "▀"
  24. areas = {{73,30},{73,14},{92,32},{73,22},{89,22},{57,22}}
  25. rSide = "3"
  26. range = "1"
  27.  
  28.  
  29.  
  30. file = io.open("addresses", "r")
  31. for word in file:lines() do
  32. if word:match("%/(%w+)") == "robot" then
  33. for name,type in word:gmatch("(%w+)=(%w+%-%w+%-%w+%-%w+%-%w+)") do namesAndAddresses[name]=type end
  34. end
  35. end
  36. file:close()
  37.  
  38. --for kN,vN in pairs(namesAndAddresses) do
  39. --print(kN,vN)
  40. --end
  41.  
  42.  
  43.  
  44. fileTextures = io.open("Textures", "r")
  45. for line in fileTextures:lines() do table.insert(textures, serialization.unserialize(line)) end
  46. fileTextures:close()
  47. gpu.set(134, 12, ">")
  48.  
  49. function authorization()
  50.  
  51. while true do
  52. modem.broadcast(346, "Authorize me!")
  53. local _,addressTo,addressFrom,_,_,message,message1,message2,message3,message4 = event.pull(0.1, "modem")
  54. if message == "Authorization complete" then
  55. rAddress = message1
  56. robotName = message2
  57. rCmps = serialization.unserialize(message3)
  58. rFacing = message4
  59. gpu.set(76, 11, rFacing)
  60. slotRaw = 1
  61. slotColumn = 1
  62. for i = 1, rCmps[1] do
  63. table.insert(buttons, {x=columnPositions[slotColumn], y=15+slotRaw, text="["..tostring(i).."]", func=rSelect, slot=i, active = true, cFore = 0xFFFFFF, cBack = 0x555555})
  64. slotColumn = slotColumn + 1
  65. if slotColumn == 5 then
  66. slotRaw = slotRaw + 1
  67. slotColumn = 1
  68. end
  69. end
  70. table.insert(buttons, {x=columnPositions[slotColumn], y=15+slotRaw+1, text=" + ", func=rSelect, slotType="robotSlot", active = true, cFore = 0xFFFFFF, cBack = 0x777777})
  71. drawButtons()
  72. for name1,type1 in pairs(namesAndAddresses) do
  73. if robotName == tostring(name1) and rAddress == type1 then
  74. result = true
  75. break
  76. else
  77. result = false
  78. end
  79. end
  80. if not result then
  81. file1 = io.open("addresses", "a")
  82. words = robotName.."="..rAddress
  83. for name2,type2 in words:gmatch("(%w+)=(%w+%-%w+%-%w+%-%w+%-%w+)") do namesAndAddresses[name2]=type2 end
  84. file1:write(robotName.."="..rAddress.."/robot")
  85. file1:close()
  86. --print(rAddress, robotName)
  87. --activatorSearch = false
  88. end
  89. break
  90. end
  91. end
  92.  
  93. end
  94.  
  95. function exit()
  96.  
  97. term.clear()
  98. os.exit()
  99.  
  100. end
  101.  
  102. function moving(direction)
  103.  
  104. if rAddress ~= nil then
  105. --if modem.send(rAddress, 346, direction, "1") then
  106. --print("true")
  107. --else
  108. --print("false")
  109. --end
  110. modem.send(rAddress, 346, direction, range)
  111. end
  112. local _,_,_,_,_,message,message2 = event.pull(25, "modem")
  113. if message ~= nil then
  114. analyzes = serialization.unserialize(message)
  115. for kTextures,vTextures in pairs(textures) do
  116. for i = 1, #analyzes do
  117. if analyzes[i] == vTextures[1] then
  118. analyzes[i] = vTextures[2]
  119. end
  120. end
  121. end
  122. if message2 ~= nil then
  123. gpu.setForeground(0xFFFFFF)
  124. gpu.setBackground(0x000000)
  125. gpu.set(76, 11, " ")
  126. rFacing = message2
  127. gpu.set(76, 11, rFacing)
  128. end
  129. drawTexture()
  130. end
  131.  
  132. end
  133.  
  134. function analyze()
  135.  
  136. if rAddress ~= nil then
  137. modem.send(rAddress, 346, "analyze", rSide)
  138. end
  139. local _,_,_,_,_,message = event.pull(1.5, "modem")
  140. gpu.set(50, 30, message)
  141.  
  142. end
  143.  
  144. function robotSwing()
  145.  
  146. if rAddress ~= nil then
  147. modem.send(rAddress, 346, "swing", rSide)
  148. end
  149. local _,_,_,_,_,message = event.pull(1.5, "modem")
  150. if message == "Wrong instrument" then
  151. gpu.set(30, 40, "Wrong instrument")
  152. end
  153.  
  154. end
  155.  
  156. function robotPlace()
  157.  
  158. if rAddress ~= nil then
  159. modem.send(rAddress, 346, "place", rSide)
  160. end
  161. local _,_,_,_,_,message = event.pull(1.5, "modem")
  162. if message == "Can't place this block" then
  163. gpu.set(30, 40, "Can't place this block")
  164. end
  165.  
  166. end
  167.  
  168. function rRecieve()
  169.  
  170. file2 = io.open("oresAndBlocks", "a")
  171. modem.send(rAddress, 346, "recieve")
  172. local _,_,_,_,_,message = event.pull("modem")
  173. oresAndBlocks = serialization.unserialize(message)
  174. for i = 1, #oresAndBlocks do
  175. file2:write(oresAndBlocks[i].."\n")
  176. end
  177. file2:close()
  178.  
  179. end
  180.  
  181. function rConnect()
  182.  
  183. count = 1
  184. for name3,type3 in pairs(namesAndAddresses) do
  185. table.insert(buttons, {x=1, y=5+count, text=tostring(name3), rType=type3, active=true, cFore = 0xFFFFFF, cBack = 0x000000})
  186. count = count+1
  187. end
  188. count = 1
  189. drawButtons()
  190.  
  191. end
  192.  
  193. function robotUse()
  194.  
  195. gpu.setForeground(0xFFFFFF)
  196. gpu.setBackground(0x000000)
  197. if rAddress ~= nil then
  198. modem.send(rAddress, 346, "use", rSide)
  199. end
  200. local _,_,_,_,_,message = event.pull(1.5, "modem")
  201. if messageLength ~= nil then
  202. for i = 0, messageLength do
  203. gpu.set(57+i, 33, " ")
  204. end
  205. end
  206. if message ~= nil then
  207. gpu.set(57, 33, message)
  208. messageLength = unicode.wlen(message)
  209. else
  210. gpu.set(57, 33, "false")
  211. messageLength = 5
  212. end
  213.  
  214. end
  215.  
  216. function rSelect(slotNumber, slotType)
  217.  
  218. if slotType == nil then
  219. modem.send(rAddress, 346, "inventory", "rSelect", tostring(slotNumber))
  220. for i = buttonsCount, #buttons do
  221. if buttons[i].slot == newSlotNumber then
  222. buttons[i].cFore = 0xFFFFFF
  223. end
  224. end
  225. newSlotNumber = slotNumber
  226. drawButtons()
  227. elseif slotType == "robotSlot" then
  228. modem.send(rAddress, 346, "inventory", "internal", tostring(slotNumber))
  229. local _,_,_,_,_,message = event.pull(0.8, "modem")
  230. gpu.fill(15, 40, 45, 2, " ")
  231. if message ~= "Slot is empty" then
  232. itemsInSlot = {}
  233. for nSlot,vSlot in message:gmatch("(%w+)=(%w*%s*%w*%s*%w*%s*%w*%s*%w*)") do itemsInSlot[nSlot]=vSlot end
  234. --for kInv,vInv in pairs(itemsInSlot) do print(kInv,vInv) end
  235. gpu.set(15, 40, itemsInSlot.label)
  236. gpu.set(15, 41, itemsInSlot.size)
  237. gpu.set(17, 41, "/")
  238. gpu.set(18, 41, itemsInSlot.maxSize)
  239. end
  240. else
  241. gpu.set(15, 40, "Slot is empty")
  242. end
  243.  
  244. end
  245.  
  246. function rGenerator()
  247.  
  248. if gCount == nil then
  249. modem.send(rAddress, 346, "generator", "cnt")
  250. else
  251. modem.send(rAddress, 346, "generator", gAct, gCount)
  252. end
  253. --gAct = nil
  254. --gCount = nil
  255. local _,_,_,_,_,message = event.pull(0.5, "modem")
  256. if gAct == nil then
  257. gpu.set(157, 16, " ")
  258. gpu.set(157, 16, message)
  259. elseif gAct == "rmv" then
  260. gpu.set(151, 21, message)
  261. elseif gAct == "inst" then
  262. gpu.set(151, 19, message)
  263. end
  264. gAct = nil
  265. gCount = nil
  266.  
  267. end
  268.  
  269. function gInsert()
  270.  
  271. gpu.set(151, 19, " ")
  272. term.setCursor(151, 19)
  273. gCount = io.read()
  274. gAct = "inst"
  275. rGenerator()
  276.  
  277. end
  278.  
  279. function gRemove()
  280.  
  281. gpu.set(151, 21, " ")
  282. term.setCursor(151, 21)
  283. gCount = io.read()
  284. gAct = "rmv"
  285. rGenerator()
  286.  
  287. end
  288.  
  289. function rEnergy()
  290.  
  291. modem.send(rAddress, 346, "energy")
  292. local _,_,_,_,_,message = event.pull(0.5, "modem")
  293. rNrg = math.ceil(tonumber(message))
  294. gpu.set(151, 23, tostring(rNrg))
  295.  
  296. end
  297.  
  298. function drawTexture()
  299.  
  300. gpu.setForeground(0xFFFFFF)
  301. gpu.setBackground(0x000000)
  302. for i1 = 1, #areas do
  303. gpu.fill(areas[i1][1], areas[i1][2], 16, 8, " ")
  304. end
  305. for i = 1, #analyzes do
  306. if type(analyzes[i]) == "table" then
  307. for kDrawTexture,vDrawTexture in pairs(analyzes[i]) do
  308. gpu.setForeground(vDrawTexture.fPixel)
  309. gpu.setBackground(vDrawTexture.bPixel)
  310. gpu.set(vDrawTexture.xPixel+areas[i][1]-1, vDrawTexture.yPixel+areas[i][2]-1, filler)
  311. end
  312. elseif type(analyzes[i]) == "string" then
  313. gpu.setForeground(0xFFFFFF)
  314. gpu.setBackground(0x000000)
  315. gpu.set(areas[i][1], areas[i][2], analyzes[i]:match("%w*%:*"))
  316. gpu.set(areas[i][1], areas[i][2]+1, analyzes[i]:match("%:(%w*)"))
  317. gpu.set(areas[i][1], areas[i][2]+2, analyzes[i]:match("%s(%w*)"))
  318. end
  319. end
  320.  
  321. end
  322.  
  323. function chooseSide(sideValue, xButton, yButton)
  324.  
  325. gpu.setForeground(0xFFFFFF)
  326. gpu.setBackground(0x000000)
  327. rSide = tostring(sideValue)
  328. for i = 1, 3 do
  329. gpu.set(134, 11+i-1, " ")
  330. end
  331. gpu.set(xButton-1, yButton, ">")
  332.  
  333. end
  334.  
  335. function robotRange(rangeLimit, xArea, yArea)
  336.  
  337. gpu.setForeground(0xFFFFFF)
  338. gpu.setBackground(0x000000)
  339. if newRange ~= nil then
  340. for i = 1, unicode.wlen(newRange) do
  341. gpu.set(xArea+i-1, yArea+1, " ")
  342. end
  343. end
  344. term.setCursor(xArea, yArea+1)
  345. newRange = io.read()
  346. if tonumber(newRange) > rangeLimit then
  347. newRange = tostring(rangeLimit)
  348. end
  349. gpu.set(xArea, yArea+1, " ")
  350. gpu.set(xArea, yArea+1, newRange)
  351. range = newRange
  352.  
  353. end
  354.  
  355. function rangeReset()
  356.  
  357. gpu.setForeground(0xFFFFFF)
  358. gpu.setBackground(0x000000)
  359. gpu.set(146, 4, " ")
  360. range = "1"
  361. gpu.set(146, 4, range)
  362.  
  363. end
  364.  
  365. function rEquip()
  366.  
  367. modem.send(rAddress, 346, "equip")
  368. local _,_,_,_,_,message = event.pull(2, "modem")
  369. gpu.set(139, 27, " ")
  370. gpu.set(139, 27, message)
  371.  
  372. end
  373.  
  374.  
  375.  
  376.  
  377.  
  378. buttons = {{x=4, y=4, text="Authorize", func=authorization, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  379. {x=160, y=1, text="X", func=exit, active=true, cFore = 0x00FFFF, cBack = 0xFF0000},
  380. {x=155, y=3, text=" E ", direction="up", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
  381. {x=155, y=7, text=" Q ", direction="down", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
  382. {x=148, y=3, text=" W ", direction="forward", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
  383. {x=148, y=7, text=" S ", direction="back", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
  384. {x=145, y=5, text=" A ", direction="left", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
  385. {x=151, y=5, text=" D ", direction="right", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
  386. {x=155, y=5, text=" R ", direction="around", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
  387. {x=145, y=9, text="Сканировать", func=analyze, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  388. {x=145, y=13, text="Swing", func=robotSwing, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  389. {x=145, y=11, text="Place", func=robotPlace, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  390. {x=145, y=25, text="Recieve", func=rRecieve, active=false, cFore = 0xFFFFFF, cBack = 0x000000}, --кнопка нужна для того, чтобы получить данные о блоках, которые тот уже собрал (при первом запуске программы)
  391. {x=145, y=30, text="Connect", func=rConnect, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  392. {x=151, y=13, text="Use", func=robotUse, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  393. {x=151, y=16, text="Count", func=rGenerator, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  394. {x=151, y=18, text="Insert", func=gInsert, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  395. {x=151, y=20, text="Remove", func=gRemove, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  396. {x=151, y=22, text="Energy", func=rEnergy, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  397. {x=135, y=11, text="Top", func=chooseSide, sideOfRobot=1, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  398. {x=135, y=12, text="Front", func=chooseSide, sideOfRobot=3, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  399. {x=135, y=13, text="Bottom", func=chooseSide, sideOfRobot=0, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  400. {x=146, y=3, text="+", func=robotRange, limitRange=10, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  401. {x=139, y=3, text="Reset", func=rangeReset, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  402. {x=145, y=27, text="Equip", func=rEquip, active=true, cFore = 0xFFFFFF, cBack = 0x000000}}
  403.  
  404.  
  405.  
  406. function drawButtons()
  407.  
  408. for k,v in pairs(buttons) do
  409. if v.active == true then
  410. gpu.setForeground(v.cFore)
  411. gpu.setBackground(v.cBack)
  412. gpu.set(v.x, v.y, v.text)
  413. end
  414. end
  415. gpu.setForeground(0xFFFFFF)
  416. gpu.setBackground(0x000000)
  417.  
  418. end
  419.  
  420. function search()
  421.  
  422. while activatorSearch do
  423. local _,_,x,y = event.pull("touch")
  424. for k1,v1 in pairs(buttons) do
  425. if (x >= v1.x and x < (v1.x+unicode.len(v1.text))) and y == v1.y and v1.active then
  426. if v1.direction ~= nil and v1.rType == nil then
  427. v1.func(v1.direction)
  428. elseif v1.direction == nil and v1.rType ~= nil then
  429. rAddress = v1.rType
  430. print(rAddress)
  431. elseif v1.slot ~= nil then
  432. v1.cFore = 0x00FB00
  433. drawButtons()
  434. newSlot = v1.slot
  435. v1.func(v1.slot)
  436. elseif v1.slotType ~= nil then
  437. v1.func(newSlot, v1.slotType)
  438. elseif v1.sideOfRobot ~= nil then
  439. v1.func(v1.sideOfRobot, v1.x, v1.y)
  440. elseif v1.limitRange ~= nil then
  441. v1.func(v1.limitRange, v1.x, v1.y)
  442. else
  443. v1.func()
  444. end
  445. end
  446. end
  447. end
  448.  
  449. end
  450.  
  451. buttonsCount = #buttons
  452. print(buttonsCount)
  453.  
  454. drawButtons()
  455. search()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement