Advertisement
NanoBob

glasses

Apr 20th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. rednet.open("top")
  2. rednet.open("right")
  3. glasses=peripheral.find("openperipheral_bridge")
  4.  
  5. local draw={}
  6. draw.box1={["type"]="box",["x"]=0,["y"]=0,["width"]=300,["height"]=200,["color"]=0,["opacity"]=0.2}
  7. draw.edit1={["type"]="memo",["x"]=0,["y"]=0,["width"]=200,["height"]=200,["color"]=colors.black,["lines"]={}}
  8. draw.edit2={["type"]="memo",["x"]=0,["y"]=200,["width"]=200,["height"]=50,["color"]=colors.black,["lines"]={}}
  9. draw.box2={["type"]="box",["x"]=0,["y"]=200,["width"]=300,["height"]=50,["color"]=0,["opacity"]=0.2}
  10. draw.turtleInventory={["type"]="inventory",["x"]=300,["y"]=0,["width"]=150,["height"]=100,["color"]=0,["items"]={},["opacity"]=0.2,["visible"]=false,}
  11.  
  12. local inventory={}
  13.  
  14. function main()
  15.     id,message,protocol=rednet.receive()
  16.     print(protocol)
  17.     if protocol~=nil and string.find(protocol,"position")~=nil then
  18.         draw.edit2.lines["position"]=message
  19.     elseif protocol~=nil and string.find(protocol,"facing")~=nil then
  20.         draw.edit2.lines["facing"]=message
  21.     elseif protocol~=nil and string.find(protocol,"powerLevel")~=nil then
  22.         draw.edit2.lines["powerLevel"]=message
  23.     elseif protocol~=nil and string.find(protocol,"inventory")~=nil then
  24.         for i=1,16 do
  25.             local itemString=splitString(message,"|",i)
  26.             local itemName=splitString(itemString,",",1)
  27.             local itemCount=splitString(itemString,",",2)
  28.             local itemDamage=splitString(itemString,",",3)
  29.             inventory[i]={["name"]=itemName,["count"]=itemCount,["damage"]=itemDamage,}
  30.         end
  31.         draw.turtleInventory.items=inventory
  32.         draw.turtleInventory.visible=true
  33.     else
  34.         draw.edit1.lines[#draw.edit1.lines+1]=message
  35.         if #draw.edit1.lines>20 then
  36.             draw.edit1.y=((#draw.edit1.lines)-20)*-10
  37.         end
  38.     end
  39.     drawFunction()
  40. end
  41.  
  42. function drawFunction()
  43.     glasses.clear()
  44.     for id,data in pairs(draw) do
  45.         if data.type=="box" then
  46.             glasses.addBox(data.x,data.y,data.width,data.height,data.color,data.opacity)
  47.         elseif data.type=="memo" then
  48.             local index=1
  49.             for id,line in pairs(data.lines) do
  50.                 local width=glasses.getStringWidth(line)
  51.                 local line="["..id.."] "..line
  52.                 if width>data.width then
  53.                     line=string.sub(line,1,string.len(line)/width*375)
  54.                 end
  55.                 glasses.addText(data.x,data.y+(index-1)*10,line,data.color)
  56.                 index=index+1
  57.             end
  58.         elseif data.type=="inventory" then
  59.             if data.visible==true then
  60.                 glasses.addBox(data.x,data.y,data.width,data.height,data.color,data.opacity)
  61.                 for i=1,16 do
  62.                     local item=inventory[i]
  63.                     if item~=nil and item.count~=nil then
  64.                         if i<=4 then
  65.                             glasses.addIcon(data.x+data.width/4*(i-1),data.y+data.height/4*0,item.name,0)
  66.                             glasses.addText(data.x+data.width/4*(i-1)+20,data.y+data.height/4*0+10,item.count,1000000000000000000)
  67.                         elseif i<=8 then
  68.                             glasses.addIcon(data.x+data.width/4*(i-1)-data.width,data.y+data.height/4*1,item.name,0)
  69.                             glasses.addText(data.x+data.width/4*(i-1)-data.width+20,data.y+data.height/4*1+10,item.count,1000000000000000000)
  70.                         elseif i<=12 then
  71.                             glasses.addIcon(data.x+data.width/4*(i-1)-data.width*2,data.y+data.height/4*2,item.name,0)
  72.                             glasses.addText(data.x+data.width/4*(i-1)-data.width*2+20,data.y+data.height/4*2+10,item.count,1000000000000000000)
  73.                         elseif i<=16 then
  74.                             glasses.addIcon(data.x+data.width/4*(i-1)-data.width*3,data.y+data.height/4*3,item.name,0)
  75.                             glasses.addText(data.x+data.width/4*(i-1)-data.width*3+20,data.y+data.height/4*3+10,item.count,1000000000000000000)
  76.                         end
  77.                     end
  78.                 end
  79.             end
  80.         end
  81.     end
  82.     glasses.sync()
  83. end
  84.  
  85. function splitString(sourceString,splittingChar,index)
  86.     results={}
  87.     currentWord=""
  88.     for i=1,string.len(sourceString) do
  89.         letter=string.sub(sourceString,i,i)
  90.         if letter==splittingChar then
  91.             results[#results+1]=currentWord
  92.             currentWord=""
  93.         else
  94.             currentWord=currentWord..letter
  95.         end
  96.     end
  97.     results[#results+1]=currentWord
  98.     return results[index]
  99. end
  100.  
  101.  
  102. drawFunction()
  103. while true do
  104.     main()
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement