Advertisement
MeXaN1cK

Утилита для сортировщика

Apr 18th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.10 KB | None | 0 0
  1. local term=require('term')
  2. local r=require('robot')
  3. local event=require('event')
  4. local shell=require('shell')
  5. local com = require("component")
  6. local fs=require('filesystem')
  7. local sides=require('sides')
  8. local serialization=require('serialization')
  9. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  10. local function loadTbl(fl) file = io.open(fl, 'r') if not file then file = io.open(fl, 'w') file:write('{}') return {} else return serialization.unserialize(file:read('*a')) end file:close() end
  11. local is=com.inventory_controller
  12. local s = require("sides")
  13. local gpu=com.gpu
  14. local m = com.modem
  15. chn31={}
  16. m.open(1)
  17. loc ={["x"]=0,["y"]=0,["z"]=0}
  18. men=1 men2=3 abl={} abl2={} plk='▌' b={"╔═══════════════════════════╗",'Launch sort ','  Add chest ',' Chest list ','Delete chest','    Exit    ','╚═══════════════════════════╝'}
  19. function pal(men) if men==1 then gpu.set(32,1,plk) gpu.set(32,2,plk) gpu.set(32,3,plk) elseif men==2 then gpu.set(32,4,plk) gpu.set(32,5,plk) gpu.set(32,6,plk) elseif men==3 then gpu.set(32,7,plk) gpu.set(32,8,plk) gpu.set(32,9,plk) elseif men==4 then gpu.set(32,10,plk) gpu.set(32,11,plk) gpu.set(32,12,plk)  elseif men==5 then gpu.set(32,13,plk) gpu.set(32,14,plk) gpu.set(32,15,plk) end end
  20. function helps() gpu.set(34,1,'▬▬▬▬▬[Help]▬▬▬▬▬') gpu.set(34,2,'Для перемещения ') gpu.set(34,3,'по меню исполь- ') gpu.set(34,4,'зуйте стрелки.  ') gpu.set(34,5,'Выбрать: Enter .') gpu.set(34,6,'▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬') gpu.set(34,7,'Программа созда-') gpu.set(34,8,'на для личного  ') gpu.set(34,9,'пользования.    ') gpu.set(34,10,'[Авторы]:     ') gpu.set(34,11,'   MeXaN1cK    ') gpu.set(34,12,'PandaDoddo72rus') gpu.set(34,13,'     (vah)     ') gpu.set(34,14,'▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬') gpu.set(34,15,'    |hil.su|  ') gpu.set(34,16,'▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬') end
  21. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  22. local function loadTbl(fl) file = io.open(fl, 'r') if not file then file = io.open(fl, 'w') file:write('{}') return {} else return serialization.unserialize(file:read('*a')) end file:close() end
  23. function getxy()
  24. key,_,_,k2,_,message=event.pull()
  25. if key=='key_up' then return k2 end
  26. if key=='modem_message' then
  27. if message=='check' then check() end end end
  28. function pal2(men2) if men2==1 then term.setCursor(18,16) io.write(plk) elseif men2==2 then term.setCursor(30,16) io.write(plk) elseif men2==3 then term.setCursor(45,16) io.write(plk) end end
  29. wc={'1fbd74ac-c1cd-46b8-9125-a46d14ecaab0',
  30.     '1147a439-09cf-4ebb-b07b-2259aa7afc87',
  31.   '82b0b45e-d89e-49a2-a686-0ff15c9aee7a'}
  32. function delFile()
  33. term.clear()
  34. gpu.set(7,5,'═════════════╬Delete╬═════════════') gpu.set(7,9,'══════════════════════════════════')
  35. term.setCursor(27,7) gpu.set(7,7,"Enter chestname: ") chn2=tostring(io.read())
  36. chn3=loadTbl("chests.txt")
  37. for i=1,#chn3 do
  38. if chn3[i]==chn2 then delo=1 pe2=i break else delo=0 end
  39. end
  40. if delo==0 then gpu.set(5,5,'═════════════╬Delete╬═════════════') gpu.set(10,7,'Cannot find this name!') gpu.set(5,9,'══════════════════════════════════') os.sleep(2)
  41. else
  42. shell.execute('del /chests/'..chn2..".txt")
  43. chn3=loadTbl('chests.txt')
  44. chn31={}
  45. for sp=1,#chn3 do if chn3[sp]~=chn2 then chn31[#chn31+1]=chn3[sp] end end
  46.  chn3=loadTbl('chests.txt') saveTbl(chn31,"chests.txt")
  47.  term.clear()
  48.  gpu.set(5,5,'═════════════╬Delete╬═════════════')
  49.  gpu.set(15,7,'Chest: '..chn2..' deleted!')
  50.  gpu.set(5,9,'══════════════════════════════════')
  51.  os.sleep(1.7)
  52.  mes='del'..'000000'..'0000'..chn2
  53.  for i=1,#wc do m.send(wc[i],1,mes) end
  54. end
  55. end
  56.  
  57. function addChest()
  58. r.select(1)
  59. gpu.set(7,5,'═══════════╬Add chest╬════════════') gpu.set(7,9,'══════════════════════════════════')
  60. term.setCursor(27,7) gpu.set(7,7,"Enter price: ") price=tonumber(io.read())
  61. term.clear() gpu.set(5,5,'═══════════╬Add chest╬════════════') gpu.set(5,6,' Положите предмет в 1 слот робота. ')  gpu.set(5,7,' После чего нажмите любую клавишу!') gpu.set(5,8,'══════════════════════════════════') getxy()  term.clear()
  62. gpu.set(5,5,'═══════════╬Add chest╬════════════') gpu.set(5,9,'══════════════════════════════════')  term.setCursor(15,6) io.write("Enter X: ") fx=tonumber(io.read())  term.setCursor(15,7) io.write("Enter Y: ") fy=tonumber(io.read())  term.setCursor(15,8) io.write("Enter Z: ") fz=tonumber(io.read())
  63. scans = is.getStackInInternalSlot(1)
  64.  a = scans["label"]
  65.  a=string.gsub(a,' ','_')
  66.  ch=loadTbl('chests.txt')
  67.  ch[#ch+1]=a
  68.  saveTbl(ch,'chests.txt')
  69.  fil=io.open('/chests/'..a..'.txt','w')
  70.  fil:write('{["x"]='..fx..',["y"]='..fy..',["z"]='..fz..'}')
  71.  fil:close() getxy()
  72.  if price<9 then price='00000'..price
  73.  elseif price<99 and price > 9 then price='0000'..price
  74.  elseif price<999 and price>99 then price='000'..price
  75.  elseif price<9999 and price>999 then price='00'..price
  76.  elseif price<99999 and price>9999 then price='0'..price
  77.  elseif price<999999 and price>99999 then price=price end
  78.  mes='add'..price..'0000'..a
  79.  for i=1,#wc do m.send(wc[i],1,mes) end
  80. end
  81.  
  82. function chestList()
  83. ab1=loadTbl('chests.txt')
  84. for i=1,#ab1 do abl[i]=ab1[i] end
  85. page=1
  86. k=1 j=14
  87. et3=true
  88. oldPage=0
  89. while et3 do
  90. gpu.fill(1,15,50,2,' ')
  91. term.setCursor(2,16) io.write('Page: ',page)
  92. term.setCursor(19,16) io.write('<Back|Next>')
  93. term.setCursor(46,16) io.write('Exit') pal2(men2)
  94. term.setCursor(1,1)
  95. if oldPage ~= page then
  96. gpu.fill(1,1,50,14,' ')
  97. for i=k,j do
  98. os.sleep(0.000001)
  99. if ab1[#ab1]~=nil then
  100. if ab1[i]~=nil then
  101. ab2='["'..abl[i]..'"]='
  102. os.sleep(0.000001)
  103. ab3=loadTbl('/chests/'..ab1[i]..'.txt')
  104. ab2=ab2..ab3['x']..','..ab3['y']..','..ab3['z']
  105. print(ab2)
  106. end
  107. oldPage=page
  108. else
  109. term.setCursor(15,7) io.write('Пусто!') break
  110. end end end
  111. p2=getxy()
  112. if p2==203 then men2=men2-1 if men2<1 then men2=1 end
  113. elseif p2==205 then men2=men2+1 if men2>3 then men2=3 end
  114. elseif p2==28 then
  115. if men2==1 then if k~=1 then k=k-14 j=j-14 page=page-1 end
  116. elseif men2==2 then k=k+14 j=j+14 page=page+1
  117. elseif men2==3 then term.clear() et3=false end end
  118. end
  119. end
  120.  
  121. function goToHome()
  122. r.turnLeft()
  123. if loc["z"] > 0 then for i=1,loc["z"] do r.down() end end
  124. if loc["y"] > 0 then for i=1,loc["y"] do r.forward() end end r.turnLeft()
  125. if loc["x"] > 0 then for i=1,loc["x"] do r.forward() end end r.turnAround()
  126. end
  127. local ami
  128. function calc()
  129. am=0
  130. for i=1,108 do
  131. ami=is.getStackInSlot(3,i)
  132. if ami~=nil then
  133. ami1=ami['size']
  134. am=am+ami1
  135. end end
  136. return am
  137. end
  138.  
  139. function check()
  140. loc ={["x"]=0,["y"]=0,["z"]=0}
  141. amountItem=nil
  142. chests=loadTbl('chests.txt') chests1={} for i=1,#chests do chests1[i]=chests[i] end
  143. for i=1,#chests do
  144. chst=loadTbl('/chests/'..chests[i]..'.txt')
  145. goToChest(chst)
  146. amountItem=calc()
  147.  if amountItem<9 then amountItem='000'..amountItem
  148.  elseif amountItem<99 and amountItem > 9 then amountItem='00'..amountItem
  149.  elseif amountItem<999 and amountItem>99 then amountItem='0'..amountItem
  150.  elseif amountItem<9999 and amountItem>999 then amountItem=amountItem end
  151.  for los=1,#wc do m.send(wc[los],1,'add'..'000010'..amountItem..chests1[i]) end
  152. end
  153. goToHome()
  154. end
  155.  
  156. function goToChest(arr)
  157.  x=loc["x"]-arr["x"] y=loc["y"]-arr["y"]  z=loc["z"]-arr["z"]
  158.  if x < 0 then x=x*-1 for i=1, x do r.forward() loc["x"]=loc["x"]+1 end elseif x>0 then for i=1,x do loc["x"]=loc["x"]-1 r.back() end end r.turnRight()
  159.  if y < 0 then y=y*-1 for i=1, y do r.forward() loc["y"]=loc["y"]+1 end elseif y>0 then for i=1,y do loc["y"]=loc["y"]-1 r.back() end end
  160.  if z < 0 then z=z*-1 for i=1, z do r.up() loc["z"]=loc["z"]+1 end elseif z>0 then for i=1,z do loc["z"]=loc["z"]-1 r.down() end end r.turnLeft()
  161. end
  162.  
  163. function menu()
  164. term.clear() helps() for i=1,5 do print(b[1]) print('║       ' .. b[i+1] ..'        ║') print(b[7]) end pal(men) p2=getxy()
  165. if p2==208 then men=men+1 if men>5 then men=5 end
  166. elseif p2==200 then men=men-1 if men<1 then men=1 end
  167. elseif p2==28 then
  168. if men==1 then term.clear() shell.execute('sorter')
  169. elseif men==2 then term.clear() addChest()
  170. elseif men==3 then term.clear() chestList()
  171. elseif men==4 then term.clear() delFile()
  172. elseif men==5 then term.clear() os.exit() end
  173. end
  174. end
  175. getxy() while true do menu() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement