Advertisement
tobast

[CC] chest search

Jul 26th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.83 KB | None | 0 0
  1. MODEM_SIDE = 'top'
  2. sensorId = nil
  3.  
  4. function clearScreen()
  5.     term.clear()
  6.     term.setCursorPos(1,1)
  7. end
  8.  
  9. function mainScreen()
  10.     term.setBackgroundColor(colors.black)
  11.     clearScreen()
  12.     term.setBackgroundColor(colors.blue)
  13.     term.write("> ")
  14.     width = term.getSize()
  15.     for i=3,width do
  16.         term.write(' ')
  17.     end
  18.  
  19.     term.setBackgroundColor(colors.black)
  20.     term.setCursorPos(3,1)
  21. end
  22.  
  23. function retreiveData()
  24.     rednet.send(sensorId, {type='query'})
  25.     while true do
  26.         snd,data = rednet.receive()
  27.         if(snd == sensorId) then
  28.             return data
  29.         end
  30.     end
  31. end
  32.  
  33. function processRequest(req)
  34.     data = retreiveData()
  35.  
  36.     result = {}
  37.  
  38.     for label,val in pairs(data) do
  39.         for slotId,content in pairs(val.Slots) do
  40.             if(string.lower(content.Name):find(req) ~= nil) then
  41.                 result[#result+1] = val.Position
  42.                 break
  43.             end
  44.         end
  45.     end
  46.     return result
  47. end
  48.  
  49. function displayResult(res)
  50.     _,height = term.getSize()
  51.     if(#res > 0) then
  52.         for line = 2, math.min(#res+1, height) do
  53.             term.setCursorPos(1,line)
  54.             pos = res[line - 1]
  55.             rangee = 0
  56.             if(pos.Z == 0) then
  57.                 rangee = 2
  58.             elseif(pos.Z > 0) then
  59.                 rangee = 1
  60.             else
  61.                 rangee = 3
  62.             end
  63.  
  64.             profondeur = math.floor((pos.X + 5) / 2) + 1
  65.  
  66.             hauteur = pos.Y + 8
  67.  
  68.             term.write('Rangée '..rangee..' ; profondeur '..profondeur..' ; hauteur '..hauteur)
  69.         end
  70.     else
  71.         term.setCursorPos(1,2)
  72.         term.write("Aucun résultat.")
  73.     end
  74.     term.setCursorPos(3,1)
  75. end
  76.  
  77. function main()
  78.     clearScreen()
  79.     print("Loading...")
  80.  
  81.     rednet.open(MODEM_SIDE)
  82.     os.sleep(3)
  83.     sensorId = rednet.lookup('sensor', 'chests')
  84.     if(sensorId == nil) then
  85.         print("Sensor unavailable.")
  86.         os.sleep(5)
  87.         return
  88.     end
  89.  
  90.     mainScreen()
  91.  
  92.     while(true) do
  93.         term.setBackgroundColor(colors.blue)
  94.         req = read()
  95.         mainScreen()
  96.        
  97.         res = processRequest(req)
  98.         displayResult(res)
  99.     end
  100. end
  101. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement