Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. -------------------Prgramme de dessin-----------------------------
  2. --Affichage--
  3. function displayLayer(building, layer, height, width)
  4. local mat = 0
  5. for i=1,width do
  6. for j=1,height do
  7. mat = building[layer][i][j]
  8. term.setCursorPos(i,j)
  9. if mat == 0 then
  10. term.setBackgroundColor(1)
  11. elseif mat == 1 then
  12. term.setBackgroundColor(16384)
  13. else
  14. term.setBackgroundColor(8192)
  15. end
  16. term.write(" ")
  17. end
  18. end
  19. end
  20. --Prgramme principal
  21. function paint(hauteur)
  22. --Preparation de l'affichage de l'image--
  23. term.setBackgroundColor(1)
  24. term.clear()
  25. term.setCursorPos(1,1)
  26. term.setCursorBlink(false)
  27. local termHeight
  28. local termWidth
  29. termWidth, termHeight = term.getSize()
  30.  
  31. --Gestion Event
  32. local event = ""
  33. local key = 0
  34. local x = 0
  35. local y = 0
  36. --Données du batiment
  37. local mat = 1
  38. local cLayer = 1
  39. local building = {} --Creation de matrice 3D
  40. for i=1,hauteur do
  41. building[i] = {} --Creation de la matrice 2D
  42. for j=1,termWidth do
  43. building[i][j] = {} --Création de la ligne
  44. for k=1,termHeight-1 do
  45. building[i][j][k] = 0
  46. end
  47. end
  48. end
  49. --Boucle principale
  50. while key ~= 28 do
  51. --Affichage
  52. displayLayer(building, cLayer, termHeight-1, termWidth)
  53. term.setBackgroundColor(32768)
  54. term.setCursorPos(1,termHeight)
  55. write("^Turtle Couche " .. cLayer .. "/" .. hauteur .. " Mat : " .. mat)
  56.  
  57. event, key, x, y = os.pullEvent()
  58.  
  59. --Traitement de l'event
  60. if event == "mouse_drag" or event == "mouse_click" then --Souris
  61. if y < termHeight then
  62. if key == 1 then --Clic gauche
  63. building[cLayer][x][y] = mat
  64. elseif key == 2 then --Clic droit
  65. building[cLayer][x][y] = 0
  66. end
  67. end
  68. elseif event == "key" then --Clavier
  69. if key == 42 then --Maj
  70. if mat == 1 then
  71. mat = 2
  72. else
  73. mat = 1
  74. end
  75. elseif key == 200 then --Fleche haut
  76. if cLayer < hauteur then
  77. cLayer = cLayer + 1
  78. end
  79. elseif key == 208 then --Fleche bas
  80. if cLayer > 1 then
  81. cLayer = cLayer - 1
  82. end
  83. end
  84. end
  85. end
  86.  
  87. --Preparation de l'affichage de la console--
  88. term.setBackgroundColor(32768)
  89. term.clear()
  90. term.setCursorBlink(true)
  91. term.setCursorPos(1,1)
  92. return building
  93. end
  94.  
  95. -------------------Programme de construction-----------------------------
  96. function goForward()
  97. while not(turtle.forward()) do
  98. turtle.dig()
  99. end
  100. end
  101.  
  102. function place(mat)
  103. local slot = 1
  104. if turtle.detectDown() then
  105. turtle.digDown()
  106. end
  107. if mat == 0 then
  108. return
  109. --Contage des materiaux disponibles--
  110. elseif mat == 1 then
  111. slot = 1
  112. while slot < 9 and turtle.getItemCount(slot) == 0 do
  113. slot = slot + 1
  114. end
  115. if slot == 9 then slot = 1 end --Si il n'y a plus de matériau
  116. elseif mat == 2 then
  117. slot = 9
  118. while slot < 17 and turtle.getItemCount(slot) == 0 do
  119. slot = slot + 1
  120. end
  121. if slot == 17 then slot = 9 end --Si il n'y a plus de matériau
  122. end
  123. turtle.select(slot)
  124. turtle.placeDown()
  125. end
  126.  
  127. function construct(building, width, depth, height)
  128. local x = 1
  129. local y = depth
  130. local evenY = -1
  131. local evenX = 1
  132. for currentLayer=1,height do
  133. while x >= 1 and x <= width do-- On fait la couche
  134. while y >= 1 and y <= depth do-- On fait la colonne
  135. place(building[currentLayer][x][y])
  136. print(x .. " " .. y)
  137. if (y > 1 and evenY == -1) or (y < depth and evenY == 1) then
  138. goForward()
  139. end
  140. y = y + evenY
  141. end
  142. if evenY == -1 then --Demi tour
  143. turtle.turnRight()
  144. goForward()
  145. turtle.turnRight()
  146. evenY = 1
  147. y = 1
  148. elseif evenY == 1 then
  149. turtle.turnLeft()
  150. goForward()
  151. turtle.turnLeft()
  152. evenY = -1
  153. y = depth
  154. end
  155. x = x + 1
  156. end
  157. end
  158. end
  159. -------------------Main-----------------------------
  160. local hauteur = nil
  161. local building
  162. local event = ""
  163. local key = 0
  164. local x = 0
  165. local y = 0
  166.  
  167. term.clear()
  168. term.setCursorPos(1,1)
  169. while hauteur == nil or hauteur < 1 do
  170. print("Indiquez la hauteur de votre batiment")
  171. hauteur = tonumber(read())
  172. end
  173. building = paint(hauteur)
  174. local termHeight
  175. local termWidth
  176. termWidth, termHeight = term.getSize()
  177. construct(building, termWidth, termHeight-1, hauteur)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement