lord_thekenny

portalControler

Feb 15th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.64 KB | None | 0 0
  1. -- ComputerCraft 1.5 --
  2. -- create by thekenny03500
  3. -- Desciption : Interface de selection de monde pour portal
  4. -- Support : Turtle sensor + sensor inventory (Slot16)
  5. -- Mods dependance :
  6. --          - Mystcraft
  7. --          - OpenCCSensor
  8. -- ************************************************************************************ --
  9.  
  10. --** load the API **--
  11. os.loadAPI("ocs/apis/sensor")
  12.  
  13. --** peripheral attach **--
  14. monit = peripheral.wrap("bottom")
  15. sensorPort = sensor.wrap("right")
  16. HaveBookInPortal = sensorPort.getTargetDetails("0,0,-1").Slots[1].RawName ~= nil
  17. monit.setTextScale(1)
  18. x, y = monit.getSize()
  19. -- print("x :"..x.." - y: "..y)
  20.  
  21. --** Mode d'affichage **--
  22. -- H - Horizontal
  23. -- V - Vertical
  24. -- C - En carre
  25. ModeDAffichage = 'C'
  26.  
  27.  
  28. --** Attributs **--
  29. BoutonTab = {}
  30.  
  31.  
  32. -- ***************** --
  33. --** Class bouton **--
  34. -- ***************** --
  35. Bouton = {
  36.     x = 0,
  37.     y = 0,
  38.     xEnd = 0,
  39.     yEnd = 0,
  40.     xNum = 0,
  41.     yNum = 0,
  42.     Name = "",
  43.     indexSlot = 0,
  44.     isEmpty = false,
  45.  
  46.     new = function(self,x,xEnd,y,yEnd,indexSlot,Name,isEmpty)
  47.         local new = {}
  48.         setmetatable(new, {__index = self})
  49.         new.Name = Name
  50.         new.indexSlot = indexSlot
  51.         new.x = x+1
  52.         new.xEnd = xEnd
  53.         new.y = y+1
  54.         new.yEnd = yEnd
  55.         new.xNum = xEnd - x
  56.         new.yNum = yEnd - new.y
  57.         new.isEmpty = isEmpty
  58.         return new
  59.     end,
  60.  
  61.     draw = function(self,drawInverted)
  62.  
  63.         if not drawInverted then
  64.             monit.setBackgroundColor(colors.white)
  65.             monit.setTextColor(colors.black)
  66.         end
  67.  
  68.         if self.isEmpty then
  69.             monit.setBackgroundColor(colors.gray)
  70.             monit.setTextColor(colors.lightGray)
  71.         end
  72.        
  73.         -- ligne V
  74.         for i=1,self.yNum do
  75.             monit.setCursorPos(self.x,(self.y+i))
  76.             monit.write("|".. string.rep(" ",self.xNum-1) .. "|")
  77.         end
  78.  
  79.         -- ligne H haut
  80.         monit.setCursorPos(self.x,self.y)
  81.         monit.write("*")
  82.         monit.write(string.rep("-",self.xNum-1))
  83.         monit.write("*")
  84.  
  85.         -- ligne H bas
  86.         monit.setCursorPos(self.x,self.yEnd)
  87.         monit.write("*")
  88.         monit.write(string.rep("-",self.xNum-1))
  89.         monit.write("*")
  90.  
  91.         monit.setCursorPos(((self.xNum+2)/2) + self.x - (string.len(self.Name)/2), self.y + ((self.yNum)/2))
  92.         monit.write(self.Name)
  93.  
  94.         monit.setBackgroundColor(colors.lightGray)
  95.         monit.setTextColor(colors.white)
  96.         --print(self.Name.." x:"..self.x.." "..self.xEnd.." "..self.xNum.." y:"..self.y.." "..self.yEnd.." "..self.yNum)
  97.     end,
  98.  
  99.     isThisBouton= function(self,x,y)
  100.         if (y >=self.y and y <= self.yEnd) and (x >= self.x and x <= self.xEnd) and not self.isEmpty then
  101.             return true
  102.         else
  103.             return false
  104.         end
  105.     end,
  106.  
  107.     OpenTheStarGate = function(self,drawInverted)
  108.         turtle.select(self.indexSlot)
  109.         turtle.drop()
  110.         Loading(2500)
  111.         turtle.suck()
  112.     end,
  113. };
  114.  
  115. --** Function **--
  116. function reDrawBouton()
  117.     monit.setBackgroundColor(colors.black)
  118.     monit.setTextColor(colors.white)
  119.     monit.clear()
  120.  
  121.     for i=1,#BoutonTab do
  122.         BoutonTab[i]:draw()
  123.     end
  124. end
  125.  
  126. function Loading(Milisec)
  127.     monit.setBackgroundColor(colors.white)
  128.     monit.clear()
  129.     monit.setBackgroundColor(colors.black)
  130.     nbUpdate = math.ceil(Milisec/500)
  131.     depBarre = math.ceil(y/nbUpdate)
  132.  
  133.     for i=0,nbUpdate-1 do
  134.         sleep(0.5)
  135.         for j=1,depBarre do
  136.             monit.setCursorPos(1,j+(depBarre*i))
  137.             monit.write(string.rep(" ",x))
  138.         end
  139.     end
  140. end
  141.  
  142. --** init **--
  143. monit.setBackgroundColor(colors.black)
  144. monit.setTextColor(colors.white)
  145. monit.clear()
  146.  
  147. -- Recupe book
  148. if HaveBookInPortal then
  149.     HaveFindPlace = false
  150.     for i=1, 3 do
  151.         if turtle.getItemCount(i) == 0 and not HaveFindPlace then
  152.             HaveFindPlace = true
  153.             turtle.select(i)
  154.             turtle.suck()
  155.         end
  156.     end
  157.  
  158.     if not HaveFindPlace then
  159.         error("J'ai pas de place pour un autre livre poto :c")
  160.     end
  161. end
  162. inventoryTurtle = sensorPort.getTargetDetails("0,0,0").Slots
  163.  
  164. diviserX = (ModeDAffichage == 'C') and 2 or ((ModeDAffichage == 'H') and 4 or 1)
  165. diviserY = (ModeDAffichage == 'C') and 2 or ((ModeDAffichage == 'H') and 1 or 4)
  166.  
  167. tailleBoutonX = math.floor(x/diviserX)
  168. tailleBoutonY = math.floor(y/diviserY)
  169.  
  170. index = 1
  171. for i=1,diviserY do
  172.     yTempo = tailleBoutonY*(i-1)+1
  173.     yEndTempo = tailleBoutonY*i-((ModeDAffichage == 'V') and 0 or 1)
  174.  
  175.     for j=1,diviserX do
  176.         xTempo =  tailleBoutonX*(j-1)+1
  177.         xEndTempo = tailleBoutonX*j-((ModeDAffichage == 'V') and 2 or 1)
  178.  
  179.         slotTempo = inventoryTurtle[index]
  180.  
  181.         if slotTempo.RawName ~= "item.myst.linkbook" and slotTempo.RawName ~= "item.myst.agebook" and  slotTempo.RawName ~= nil then
  182.             error("slot : "..index.." - C'est pas un livre de teleportation ce truc!")
  183.         end
  184.  
  185.         BoutonTab[index] = Bouton:new(xTempo,xEndTempo,yTempo,yEndTempo,index,slotTempo.Name,slotTempo.RawName == nil);
  186.         index = index+1
  187.     end
  188. end
  189.  
  190.  
  191. --** Boucle du program **--
  192. reDrawBouton()
  193.  
  194. while true do
  195.     event, side, xPos, yPos = os.pullEvent("monitor_touch")
  196.  
  197.     --monit.setCursorPos(1,1)
  198.     --monit.write(xPos..' '..yPos..' ')
  199.     --print(xPos..' '..yPos..' ')
  200.  
  201.     for i=1,#BoutonTab do
  202.         if BoutonTab[i]:isThisBouton(xPos,yPos) then
  203.             BoutonTab[i]:draw(true)
  204.             sleep(0.15)
  205.            BoutonTab[i]:draw()
  206.            BoutonTab[i]:OpenTheStarGate()
  207.            reDrawBouton()
  208.         end
  209.     end
  210.        
  211. end
Add Comment
Please, Sign In to add comment