Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------Prgramme de dessin-----------------------------
- --Affichage--
- function displayLayer(building, layer, height, width)
- local mat = 0
- for i=1,width do
- for j=1,height do
- mat = building[layer][i][j]
- term.setCursorPos(i,j)
- if mat == 0 then
- term.setBackgroundColor(1)
- elseif mat == 1 then
- term.setBackgroundColor(16384)
- else
- term.setBackgroundColor(8192)
- end
- term.write(" ")
- end
- end
- end
- --Prgramme principal
- function paint(hauteur)
- --Preparation de l'affichage de l'image--
- term.setBackgroundColor(1)
- term.clear()
- term.setCursorPos(1,1)
- term.setCursorBlink(false)
- local termHeight
- local termWidth
- termWidth, termHeight = term.getSize()
- --Gestion Event
- local event = ""
- local key = 0
- local x = 0
- local y = 0
- --Données du batiment
- local mat = 1
- local cLayer = 1
- local building = {} --Creation de matrice 3D
- for i=1,hauteur do
- building[i] = {} --Creation de la matrice 2D
- for j=1,termWidth do
- building[i][j] = {} --Création de la ligne
- for k=1,termHeight-1 do
- building[i][j][k] = 0
- end
- end
- end
- --Boucle principale
- while key ~= 28 do
- --Affichage
- displayLayer(building, cLayer, termHeight-1, termWidth)
- term.setBackgroundColor(32768)
- term.setCursorPos(1,termHeight)
- write("^Turtle Couche " .. cLayer .. "/" .. hauteur .. " Mat : " .. mat)
- event, key, x, y = os.pullEvent()
- --Traitement de l'event
- if event == "mouse_drag" or event == "mouse_click" then --Souris
- if y < termHeight then
- if key == 1 then --Clic gauche
- building[cLayer][x][y] = mat
- elseif key == 2 then --Clic droit
- building[cLayer][x][y] = 0
- end
- end
- elseif event == "key" then --Clavier
- if key == 42 then --Maj
- if mat == 1 then
- mat = 2
- else
- mat = 1
- end
- elseif key == 200 then --Fleche haut
- if cLayer < hauteur then
- cLayer = cLayer + 1
- end
- elseif key == 208 then --Fleche bas
- if cLayer > 1 then
- cLayer = cLayer - 1
- end
- end
- end
- end
- --Preparation de l'affichage de la console--
- term.setBackgroundColor(32768)
- term.clear()
- term.setCursorBlink(true)
- term.setCursorPos(1,1)
- return building
- end
- -------------------Programme de construction-----------------------------
- function goForward()
- while not(turtle.forward()) do
- turtle.dig()
- end
- end
- function place(mat)
- local slot = 1
- if turtle.detectDown() then
- turtle.digDown()
- end
- if mat == 0 then
- return
- --Contage des materiaux disponibles--
- elseif mat == 1 then
- slot = 1
- while slot < 9 and turtle.getItemCount(slot) == 0 do
- slot = slot + 1
- end
- if slot == 9 then slot = 1 end --Si il n'y a plus de matériau
- elseif mat == 2 then
- slot = 9
- while slot < 17 and turtle.getItemCount(slot) == 0 do
- slot = slot + 1
- end
- if slot == 17 then slot = 9 end --Si il n'y a plus de matériau
- end
- turtle.select(slot)
- turtle.placeDown()
- end
- function construct(building, width, depth, height)
- local x = 1
- local y = depth
- local evenY = -1
- local evenX = 1
- for currentLayer=1,height do
- while x >= 1 and x <= width do-- On fait la couche
- while y >= 1 and y <= depth do-- On fait la colonne
- place(building[currentLayer][x][y])
- print(x .. " " .. y)
- if (y > 1 and evenY == -1) or (y < depth and evenY == 1) then
- goForward()
- end
- y = y + evenY
- end
- if evenY == -1 then --Demi tour
- turtle.turnRight()
- goForward()
- turtle.turnRight()
- evenY = 1
- y = 1
- elseif evenY == 1 then
- turtle.turnLeft()
- goForward()
- turtle.turnLeft()
- evenY = -1
- y = depth
- end
- x = x + 1
- end
- end
- end
- -------------------Main-----------------------------
- local hauteur = nil
- local building
- local event = ""
- local key = 0
- local x = 0
- local y = 0
- term.clear()
- term.setCursorPos(1,1)
- while hauteur == nil or hauteur < 1 do
- print("Indiquez la hauteur de votre batiment")
- hauteur = tonumber(read())
- end
- building = paint(hauteur)
- local termHeight
- local termWidth
- termWidth, termHeight = term.getSize()
- construct(building, termWidth, termHeight-1, hauteur)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement