Advertisement
Guest User

RunStorage

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