Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ComputerCraft 1.5 --
- -- create by thekenny03500
- -- Desciption : Interface de selection de monde pour portal
- -- Support : Turtle sensor + sensor inventory (Slot16)
- -- Mods dependance :
- -- - Mystcraft
- -- - OpenCCSensor
- -- ************************************************************************************ --
- --** load the API **--
- os.loadAPI("ocs/apis/sensor")
- --** peripheral attach **--
- monit = peripheral.wrap("bottom")
- sensorPort = sensor.wrap("right")
- HaveBookInPortal = sensorPort.getTargetDetails("0,0,-1").Slots[1].RawName ~= nil
- monit.setTextScale(1)
- x, y = monit.getSize()
- -- print("x :"..x.." - y: "..y)
- --** Mode d'affichage **--
- -- H - Horizontal
- -- V - Vertical
- -- C - En carre
- ModeDAffichage = 'C'
- --** Attributs **--
- BoutonTab = {}
- -- ***************** --
- --** Class bouton **--
- -- ***************** --
- Bouton = {
- x = 0,
- y = 0,
- xEnd = 0,
- yEnd = 0,
- xNum = 0,
- yNum = 0,
- Name = "",
- indexSlot = 0,
- isEmpty = false,
- new = function(self,x,xEnd,y,yEnd,indexSlot,Name,isEmpty)
- local new = {}
- setmetatable(new, {__index = self})
- new.Name = Name
- new.indexSlot = indexSlot
- new.x = x+1
- new.xEnd = xEnd
- new.y = y+1
- new.yEnd = yEnd
- new.xNum = xEnd - x
- new.yNum = yEnd - new.y
- new.isEmpty = isEmpty
- return new
- end,
- draw = function(self,drawInverted)
- if not drawInverted then
- monit.setBackgroundColor(colors.white)
- monit.setTextColor(colors.black)
- end
- if self.isEmpty then
- monit.setBackgroundColor(colors.gray)
- monit.setTextColor(colors.lightGray)
- end
- -- ligne V
- for i=1,self.yNum do
- monit.setCursorPos(self.x,(self.y+i))
- monit.write("|".. string.rep(" ",self.xNum-1) .. "|")
- end
- -- ligne H haut
- monit.setCursorPos(self.x,self.y)
- monit.write("*")
- monit.write(string.rep("-",self.xNum-1))
- monit.write("*")
- -- ligne H bas
- monit.setCursorPos(self.x,self.yEnd)
- monit.write("*")
- monit.write(string.rep("-",self.xNum-1))
- monit.write("*")
- monit.setCursorPos(((self.xNum+2)/2) + self.x - (string.len(self.Name)/2), self.y + ((self.yNum)/2))
- monit.write(self.Name)
- monit.setBackgroundColor(colors.lightGray)
- monit.setTextColor(colors.white)
- --print(self.Name.." x:"..self.x.." "..self.xEnd.." "..self.xNum.." y:"..self.y.." "..self.yEnd.." "..self.yNum)
- end,
- isThisBouton= function(self,x,y)
- if (y >=self.y and y <= self.yEnd) and (x >= self.x and x <= self.xEnd) and not self.isEmpty then
- return true
- else
- return false
- end
- end,
- OpenTheStarGate = function(self,drawInverted)
- turtle.select(self.indexSlot)
- turtle.drop()
- Loading(2500)
- turtle.suck()
- end,
- };
- --** Function **--
- function reDrawBouton()
- monit.setBackgroundColor(colors.black)
- monit.setTextColor(colors.white)
- monit.clear()
- for i=1,#BoutonTab do
- BoutonTab[i]:draw()
- end
- end
- function Loading(Milisec)
- monit.setBackgroundColor(colors.white)
- monit.clear()
- monit.setBackgroundColor(colors.black)
- nbUpdate = math.ceil(Milisec/500)
- depBarre = math.ceil(y/nbUpdate)
- for i=0,nbUpdate-1 do
- sleep(0.5)
- for j=1,depBarre do
- monit.setCursorPos(1,j+(depBarre*i))
- monit.write(string.rep(" ",x))
- end
- end
- end
- --** init **--
- monit.setBackgroundColor(colors.black)
- monit.setTextColor(colors.white)
- monit.clear()
- -- Recupe book
- if HaveBookInPortal then
- HaveFindPlace = false
- for i=1, 3 do
- if turtle.getItemCount(i) == 0 and not HaveFindPlace then
- HaveFindPlace = true
- turtle.select(i)
- turtle.suck()
- end
- end
- if not HaveFindPlace then
- error("J'ai pas de place pour un autre livre poto :c")
- end
- end
- inventoryTurtle = sensorPort.getTargetDetails("0,0,0").Slots
- diviserX = (ModeDAffichage == 'C') and 2 or ((ModeDAffichage == 'H') and 4 or 1)
- diviserY = (ModeDAffichage == 'C') and 2 or ((ModeDAffichage == 'H') and 1 or 4)
- tailleBoutonX = math.floor(x/diviserX)
- tailleBoutonY = math.floor(y/diviserY)
- index = 1
- for i=1,diviserY do
- yTempo = tailleBoutonY*(i-1)+1
- yEndTempo = tailleBoutonY*i-((ModeDAffichage == 'V') and 0 or 1)
- for j=1,diviserX do
- xTempo = tailleBoutonX*(j-1)+1
- xEndTempo = tailleBoutonX*j-((ModeDAffichage == 'V') and 2 or 1)
- slotTempo = inventoryTurtle[index]
- if slotTempo.RawName ~= "item.myst.linkbook" and slotTempo.RawName ~= "item.myst.agebook" and slotTempo.RawName ~= nil then
- error("slot : "..index.." - C'est pas un livre de teleportation ce truc!")
- end
- BoutonTab[index] = Bouton:new(xTempo,xEndTempo,yTempo,yEndTempo,index,slotTempo.Name,slotTempo.RawName == nil);
- index = index+1
- end
- end
- --** Boucle du program **--
- reDrawBouton()
- while true do
- event, side, xPos, yPos = os.pullEvent("monitor_touch")
- --monit.setCursorPos(1,1)
- --monit.write(xPos..' '..yPos..' ')
- --print(xPos..' '..yPos..' ')
- for i=1,#BoutonTab do
- if BoutonTab[i]:isThisBouton(xPos,yPos) then
- BoutonTab[i]:draw(true)
- sleep(0.15)
- BoutonTab[i]:draw()
- BoutonTab[i]:OpenTheStarGate()
- reDrawBouton()
- end
- end
- end
Add Comment
Please, Sign In to add comment