Advertisement
Guest User

runStorage

a guest
Nov 25th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local jarPeripheral=nil
  2.  
  3. function getJar()
  4.   return peripheral.wrap("back")
  5. end
  6.  
  7. function getAspect()
  8.   if (jarPeripheral==nil) then
  9.     jarPeripheral=getJar()
  10.   end
  11.   local tableAspect= jarPeripheral.getAspects()[1]
  12.   if(tableAspect~=nil) then
  13.     return tableAspect["name"]
  14.   else
  15.     return nil
  16.   end
  17. end
  18.  
  19. function getAspectAmount()
  20.   local Aspect= getAspect()
  21.   if( Aspect==nil) then
  22.     return nil
  23.   else
  24.     return jarPeripheral.getAspectCount(Aspect)
  25.   end
  26. end
  27.  
  28. function openNetwork()
  29.   if(rednet.isOpen("front")==false) then
  30.     rednet.open("front")
  31.   end
  32. end
  33.  
  34. function split(str,pat)
  35.   local t={}
  36.   local fpat="(.-)"..pat
  37.   local last_end=1
  38.   local s, e, cap = str:find(fpat, 1)
  39.   while s do
  40.     if s ~= 1 or cap ~= "" then
  41.       table.insert(t,cap)
  42.     end
  43.     last_end=e+1
  44.     s, e, cap = str:find(fpat, last_end)
  45.   end
  46.   if last_end <= #str then
  47.     cap = str:sub(last_end)
  48.     table.insert(t, cap)
  49.   end
  50.   return t
  51. end
  52.  
  53. function canDoStorage(qty)
  54.   if (getAspectAmount()+qty<=64) then
  55.     return true
  56.   else
  57.     return false
  58.   end
  59. end
  60.  
  61. function openStorage()
  62.   redstone.setOutput("top",true)
  63. end
  64.  
  65. function closeStorage()
  66.   redstone.setOutput("top",false)
  67. end
  68.  
  69. function mkStrAspectData()
  70.   local AspectType=getAspect()
  71.   if (AspectType==nil) then
  72.     return "aspectdata-untyped"
  73.   end
  74.   return "aspectdata-"..AspectType.."-"..getAspectAmount()
  75. end
  76.  
  77. function setStartup()
  78.   if(fs.exists("startup")==false) then
  79.     local startupFile=fs.open("startup","w")
  80.     startupFile.write("os.run({}, \""..shell.getRunningProgram().."\")")
  81.     startupFile.close()
  82.   end
  83. end
  84.  
  85. function listenNetwork()
  86.   openNetwork()
  87.   while(true) do
  88.     write("test")
  89.     senderId, rawMessage, distance = rednet.receive()
  90.     local messageTable=split(rawMessage,"-")
  91.     --write(rawMessage)
  92.     --write(textutils.serialize(senderId))
  93.     if(messageTable[1]=="wherestore") then
  94.       if(messageTable[2]==getAspect()) then
  95.         if (canDoStorage(messageTable[3])) then
  96.           write("Inform provider i'm avaible for this\n")
  97.           rednet.send(senderId,"here")
  98.         end          
  99.       end
  100.     elseif (messageTable[1]=="store") then
  101.       if (messageTable[2]==getAspect()) then
  102.         write("Begin to store\n")
  103.         openStorage()
  104.       end
  105.     elseif(messageTable[1]=="stop") then
  106.       write("End to store\n")
  107.       closeStorage()
  108.     elseif(messageTable[1]=="howmany") then
  109.       write("Begin to store\n")
  110.       rednet.send(senderId, mkStrAspectData())
  111.     end      
  112.   end
  113. end
  114.  
  115. setStartup()
  116. listenNetwork()
  117. --write(getAspect())
  118. --write(textutils.serialize(canDoStorage(16)))
  119. --write(textutils.serialize(split("store-magic","-")))
  120. --write(textutils.serialize(getAspectAmount()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement