Advertisement
BombBloke

Hive Inventory Server (ComputerCraft)

Jun 1st, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.87 KB | None | 0 0
  1. -- Barrel Tracking
  2. -- ----------------------------------------------------------
  3.  
  4. -- Returns information about barrels in my network, such as
  5. -- where a given resource is kept and if a barrel is full.
  6.  
  7. -- ----------------------------------------------------------
  8.  
  9. -- Initialise important values:
  10.  
  11. local side = {{"back",-2},{"bottom",-2},{"top",-1},{"right",-1}}
  12.  
  13. local combbarrel = {{{"tin comb",  4,4},    -- Row 1 (top left)
  14.     {"bronze comb",  3,4},
  15.     {"platinum comb",  2,7},
  16.     {"radioactive comb",  2,5},
  17.     {"lapis comb",  4,9,  4,10},
  18.     {"diamond comb",  1,7}},
  19.    
  20.     {{"copper comb",  3,1},    -- Row 2 (bottom left)
  21.     {"titanium comb",  1,15},
  22.     {"gold comb",  2,15},
  23.     {"fossilised comb",  4,8},
  24.     {"iron comb",  4,1},
  25.     {"ruby comb",  1,5}},
  26.    
  27.     {{"simmering comb",  4,7,  4,16},    -- Row 3 (top right)
  28.     {"propolis",  3,12},
  29.     {"stringy comb", 3,12},
  30.     {"leaden comb",  2,12},
  31.     {"invar comb",  2,9},
  32.     {"silver comb",  1,12}},
  33.    
  34.     {{"dripping comb",  3,16},    -- Row 4 (bottom right)
  35.     {"static comb",  3,14,  2,6},
  36.     {"clay comb",  4,12},
  37.     {"brazen comb",  3,7},
  38.     {"glowing comb",  2,6},
  39.     {"electrum comb",  1,9}}}
  40.    
  41. local barrel = {{{"cobble",  1,2},    -- Row 1 (top left)
  42.     {"stone",  1,3},
  43.     {"stone brick",  1,4},
  44.     {"chiseled stone brick"},
  45.     {"tiny pile of ruby dust",  1,6},
  46.     {"ruby dust"},
  47.     {"tiny pile of diamond dust",  1,8},
  48.     {"diamond dust"},
  49.     {"tiny pile of electrum dust",  1,10},
  50.     {"electrum dust",  1,11},
  51.     {"electrum ingot"},
  52.     {"tiny pile of silver dust",  1,13},
  53.     {"silver dust",  1,14},
  54.     {"silver ingot"},
  55.     {"tiny pile of titanium dust",  1,16},
  56.     {"titanium dust",  1,17},
  57.     {"titanium ingot"}},
  58.    
  59.     {{""},    -- Row 2 (bottom left)
  60.     {""},
  61.     {""},
  62.     {""},
  63.     {"uranium"},
  64.     {"redstone"},
  65.     {"tiny pile of platinum dust",  2,8},
  66.     {"platinum dust"},
  67.     {"tiny pile of invar dust",  2,10},
  68.     {"invar dust",  2,11},
  69.     {"invar ingot"},
  70.     {"tiny pile of lead dust",  2,13},
  71.     {"lead dust",  2,14},
  72.     {"lead ingot"},
  73.     {"tiny pile of gold dust",  2,16},
  74.     {"gold dust",  2,17},
  75.     {"gold ingot"}},
  76.    
  77.     {{"tiny pile of copper dust",  3,2},    -- Row 3 (top right)
  78.     {"copper dust",  3,3},
  79.     {"copper ingot"},
  80.     {"tiny pile of bronze dust",  3,5},
  81.     {"bronze dust",  3,6},
  82.     {"bronze ingot"},
  83.     {"tiny pile of brass dust",  3,8},
  84.     {"brass dust",  3,9},
  85.     {"brass ingot"},
  86.     {"glowstone dust",  3,11},
  87.     {"glowstone"},
  88.     {"sticky resin",  3,13},
  89.     {"rubber"},
  90.     {"energy drop"},
  91.     {"honey drop"},
  92.     {"honeydew"},
  93.     {"pollen"}},
  94.    
  95.     {{"tiny pile of iron dust",  4,2},    -- Row 4 (bottom right)
  96.     {"iron dust",  4,3},
  97.     {"iron ingot"},
  98.     {"tiny pile of tin dust",  4,5},
  99.     {"tin dust",  4,6},
  100.     {"tin ingot"},
  101.     {"phosphor"},
  102.     {"coal dust"},
  103.     {"lazurite dust"},
  104.     {"lapis lazuli",  4,11},
  105.     {"lapis lazuli block"},
  106.     {"clay dust",  4,13},
  107.     {"clay"},
  108.     {"magic wax"},
  109.     {"beeswax"},
  110.     {"refractory wax"},
  111.     {"royal jelly"}}}
  112.  
  113. local result = {0,0,0,0,false}
  114. local sender = 0
  115. local incoming = ""
  116. local found = false
  117.  
  118. rednet.open("left")
  119.  
  120. print("")
  121. print("I'm an inventory server.")
  122. print("Other computers talk to me over rednet.")
  123. print("")
  124. print("Go find something else to play with and leave me alone!")
  125. print("")
  126.  
  127. while true do
  128.     sender, incoming = rednet.receive()
  129.    
  130.     if incoming == "Hello InvServer" then
  131.         -- A new client wants to know my ID.
  132.         sleep(1) -- Give the client time to switch to receiving mode.
  133.         rednet.send(sender,"Hello yourself!")
  134.     else
  135.         -- Client should be asking about a barrel.
  136.         sleep(1) -- Give the client time to switch to receiving mode.
  137.        
  138.         found = false
  139.        
  140.         for i=1,4 do
  141.             for j=1,6 do
  142.                 if incoming == combbarrel[i][j][1] then
  143.                     result[2] = 593           -- X co-ord of barrel.
  144.                     if i > 2 then result[2] = 600 end
  145.                    
  146.                     result[3] = -236 - j      -- Y co-ord of barrel.
  147.                    
  148.                     result[4] = 66            -- Z co-ord of barrel.
  149.                     if i==2 or i==4 then result[4] = 63 end
  150.                    
  151.                     result[1] = 1             -- Node closest to barrel.
  152.                    
  153.                     result[5] = true          -- Whether the barrels that hold the resources
  154.                                               -- produced from the contents of THIS barrel
  155.                                               -- can take more produce.
  156.                    
  157.                     if table.getn(combbarrel[i][j]) > 1 then
  158.                         result[5] = false
  159.                         for k=1,((table.getn(combbarrel[i][j])-1)/2) do
  160.                             if bit.band(rs.getBundledInput(side[combbarrel[i][j][k*2]][1]),2^(combbarrel[i][j][(k*2)+1]+side[combbarrel[i][j][k*2]][2])) == 2^(combbarrel[i][j][(k*2)+1]+side[combbarrel[i][j][k*2]][2]) then result[5] = true end
  161.                         end
  162.                     end
  163.                    
  164.                     found = true
  165.                     i = 4
  166.                     j = 6
  167.                 end
  168.             end
  169.         end
  170.        
  171.         if not found then      
  172.             for i=1,4 do
  173.                 for j=1,17 do
  174.                     if incoming == barrel[i][j][1] then
  175.                         result[2] = 575 + j   -- X co-ord of barrel.
  176.                         if i > 2 then result[2] = result[2] + 25 end
  177.  
  178.                         result[3] = -236      -- Y co-ord of barrel.
  179.  
  180.                         result[4] = 66        -- Z co-ord of barrel.
  181.                         if i==2 or i==4 then result[4] = 63 end
  182.  
  183.                         result[1] = 12        -- Node closest to barrel.
  184.                         if result[2] > 584 then result[1] = 10 end
  185.                         if result[2] > 592 then result[1] = 3 end
  186.                         if result[2] > 606 then result[1] = 5 end
  187.  
  188.                         result[5] = true      -- Whether the barrels that hold the resources
  189.                                       -- produced from the contents of THIS barrel
  190.                                       -- can take more produce.
  191.  
  192.                         if table.getn(barrel[i][j]) > 1 then
  193.                             result[5] = false
  194.                             for k=1,((table.getn(barrel[i][j])-1)/2) do
  195.                                 if bit.band(rs.getBundledInput(side[barrel[i][j][k*2]][1]),2^(barrel[i][j][(k*2)+1]+side[barrel[i][j][k*2]][2])) == 2^(barrel[i][j][(k*2)+1]+side[barrel[i][j][k*2]][2]) then result[5] = true end
  196.                             end
  197.                         end
  198.  
  199.                         found = true
  200.                         i = 4
  201.                         j = 17
  202.                     end
  203.                 end
  204.             end
  205.         end
  206.        
  207.         if found then
  208.             rednet.send(sender,textutils.serialize(result))
  209.         else
  210.             print("Got a signal I didn't recognise: "..incoming)
  211.             print("")
  212.         end
  213.     end
  214. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement